4.1.10 · D1Transformer Architecture

Foundations — Encoder vs decoder vs encoder-decoder

2,484 words11 min readBack to topic

This page is the toolbox. Before you can read the parent note, you need to know what every squiggle means. We build each symbol from nothing, anchor it to a picture, and say why the topic needs it. Read top to bottom — each item stands on the one before it.


0. What is a "token" and a "sequence"?

Before any math, we need the raw material.

Picture a row of boxes, each holding one word:

Figure — Encoder vs decoder vs encoder-decoder

We write a sequence as .

  • The bold means "the whole list at once."
  • Each (read "x-sub-i") is the token sitting in box number .
  • is simply how many tokens there are — the length of the sentence.

Why the topic needs it: everything a transformer does is "take a sequence in, produce a sequence out." tells us how many boxes we are juggling.


1. The subscript , , — pointing at a box

We use different letters only to keep two fingers apart:

  • usually means "the box I am currently thinking about."
  • usually means "some other box I am comparing against."
  • means "the current time step" — used when we generate one word after another.

Why the topic needs it: attention is all about pairs of tokens ("how much should box care about box ?"). You cannot talk about a pair without two pointers.


2. The embedding vector — a word becomes a list of numbers

A computer cannot add the word "bank." So we turn each token into a list of numbers.

Read as: "" is the set of all ordinary (real) numbers, and the little means " of them in a row."

Figure — Encoder vs decoder vs encoder-decoder

The symbol is the meaning of token expressed as numbers. The superscript in (read "h-sub-i, layer L") just says "this is the version of that vector after layer number ."

Why the topic needs it: the whole point of a transformer is to improve these number-lists layer by layer, so that by the end already "knows" whether we mean a river or money.


3. The dot product — measuring "how similar?"

To decide how much box should care about box , we need a similarity score between two vectors. The tool for that is the dot product.

Figure — Encoder vs decoder vs encoder-decoder

Why this tool and not another? We need a single number that says "how aligned are these two meanings?" Distance would also compare vectors, but the dot product is cheap (one multiply-add per slot) and it grows when directions agree — exactly the "relevance" signal attention wants.


4. , , — the three roles a token plays

Each token vector is copied into three different vectors by three learned recipes:

Think of a library. Your query is your search request; each book's key is its title on the spine; the value is the book's contents. You match your query against every spine, then read the books that matched best.

The dot product therefore reads: "how well does what box wants match what box offers?"

Why the topic needs it: splitting each token into these three roles is what lets attention be selective — a token can ask for one thing while offering another.


5. , , — the recipes (matrices)

Where do , , come from? From the embedding multiplied by a matrix.

  • turns a token into its query.
  • turns a token into its key.
  • turns a token into its value.

These grids are exactly the numbers the network learns during training. Everything else in attention is fixed arithmetic; the intelligence lives in these matrices.

Why the topic needs it: without learnable matrices, every token would ask and offer the same thing. The matrices let the model discover useful notions of relevance from data.


6. Softmax — turning scores into a "how-much-I-care" distribution

We now have raw scores for every other box . We want to turn them into weights that are all positive and add up to 1 (so they behave like "percent of attention").

Read it piece by piece:

  • (the exponential) makes every score positive — even negative scores become small positive numbers, never zero.
  • The bottom, (read "sum over all "), adds up all those positive numbers so we can divide by the total. This is the normalising step that forces the outputs to sum to 1.
Figure — Encoder vs decoder vs encoder-decoder

The result (Greek letter alpha) is the attention weight: the fraction of box 's focus spent on box . All the 's for a fixed add to 1.


7. Putting it together — one attention output

Now the payoff. Box 's new vector is a weighted blend of everyone's values:

The (read "sum as goes from 1 to ") means: walk through every box , take a -sized scoop of its value , and pour them all together. Boxes you care about a lot contribute most.


8. The scale — why we divide

is just the dimension of the key vectors — how long each is. The square root is the exact amount that cancels the natural growth of a sum of random products.


9. The mask — deciding who may look at whom

This is the symbol that separates the three architectures.

Why ? Because : after softmax, a forbidden pair gets weight exactly zero. It is completely blocked.

Two shapes matter:

  • All-ones (bidirectional): every box may see every box. Used by the encoder. Good for understanding a whole sentence at once.
  • Lower-triangular (causal): box may see only boxes (itself and the past), never the future. Used by the decoder. Needed for generation, because a word can't peek at words not yet written.
Figure — Encoder vs decoder vs encoder-decoder

Why the topic needs it: change the mask and the same attention machine becomes an understander or a generator. The mask is the whole plot of the parent note. See Attention Masking.


10. The product and conditional — probability of a sentence

Decoders generate one token at a time, so we need to talk about probabilities of sequences.

The big (capital Greek pi) means "multiply all these together," just as means "add all together."


11. Cross-attention — a second sentence enters

Encoder–decoder models have two sequences (e.g. English → French). Cross-attention is attention where the query comes from one sequence and the keys/values from another.

It is the same attention formula from §7 — only the source of differs from the source of . This is how the French decoder "reads" the English encoding. See Cross-Attention.


Prerequisite map

Tokens and sequence x1..xn

Embedding vector h_i in R^d

Dot product measures similarity

Query Key Value roles

Learned matrices W_Q W_K W_V

Softmax gives weights alpha_ij

Scale by sqrt d_k

Weighted blend of values

Mask M chooses who sees whom

Bidirectional = Encoder

Causal = Decoder

Cross attention two sequences

Product of conditionals P sentence

Encoder vs Decoder vs Encoder-Decoder

Related building blocks in the vault: Self-Attention Mechanism, Positional Encoding, Multi-Head Attention, Masked Language Modeling, Sequenceto-Sequence Models, Autoregressive Generation, T5.


Equipment checklist

Read the question, answer in your head, then reveal.

What does point to?
The token sitting in box number of the sequence.
What is ?
The length of the sequence — how many tokens there are.
What does mean in plain words?
Token 's meaning written as a list of real numbers (a vector).
Why does a big dot product mean "relevant"?
The two vectors point the same way, so their matching slots agree and sum to a large positive number.
What are the three roles a token is split into, and their jobs?
Query (what I'm looking for), Key (what I advertise), Value (the info I hand over).
Where does the model's actual learning live?
In the matrices that build the queries, keys, and values.
What two properties does softmax guarantee about its outputs?
They are all positive and they sum to 1.
Why divide scores by ?
Large inflates dot products; dividing keeps softmax from spiking and keeps gradients healthy.
What value in the mask blocks a pair, and why exactly zero after softmax?
, because .
What mask shape gives bidirectional (encoder) attention vs causal (decoder) attention?
All-ones vs lower-triangular ().
Why is a full sentence's probability a product of conditionals?
Chain rule: you need each next word right given all previous words, and "and" becomes multiplication.
How is cross-attention different from self-attention?
The query comes from the decoder while keys and values come from the encoder — otherwise it's the same formula.