11 · Attention
The Whole Model
Stacking the blocks, and the two pieces between a stream of vectors and a word.
From a stack of blocks to a word
A block takes an
Depth buys something specific here. Block 1 reads the raw sentence, so its attention can only match one word's embedding against another's. Block 2 reads what block 1 wrote, where chased already carries part of mouse, so its queries and keys are built from vectors that are partly about their neighbours. Each block asks its questions of a sentence the blocks below it have already annotated, which is how relations that span several steps — a pronoun to its antecedent, by way of the verb between them — get resolved at all. One block cannot do it, because one round of attention has only the embeddings to work with.
Pre-norm leaves one loose end. The stream arrives at the top carrying
One piece is still missing, and the masking chapter has been waiting for it. The stack produces one vector per position; the task asks for a word. So add one more learned matrix, of shape
What the vocabulary actually is
The entries are not quite words. A vocabulary of English words would be enormous and still incomplete, since there is always a name or a typo it has not seen, so the list is built from subword pieces: common words get an entry to themselves while rare ones are spelled out of smaller parts. Every string can be written and the list stays finite.
The original Transformer used about 37,000 entries, GPT-2 used 50,257, and recent models run past 100,000. That number is not free, because it appears twice: the embedding table is
Everything, in one chain
Read the figure left to right and every box in it was built somewhere in this tutorial: the embedding table, the position vectors added to it, the blocks, the final norm that pre-norm made necessary, and the matrix and softmax just added.
Follow the highlighted row. (3) chased goes in as a token id, collects its context in every block on the way, and comes out as a distribution putting 0.55 on the — the word at position (4), which is exactly what the next-token task asked position (3) to predict.
The complaint this tutorial opened with, that everything a model knew about a sentence had to fit inside one fixed vector, has turned into a model that reads a sentence and says what comes next. Nothing was memorised into a summary. Each position kept its own vector, looked at the others whenever it had a reason to, and computed with what it found.
What it costs
The answer has a bill attached. Every block builds an
Generating text is worse. The model produces one token at a time, and each new token means pushing the whole prefix through all
Both problems come from the same decision, taken in the very first chapter: keep the sentence around and look at all of it whenever anything is asked. The last chapter of this tutorial is about what that costs and what can be done about it.