After this chapter you will be able to
follow four numbers through every step of one transformer block and say what each step is for;
read two attention heads and say what a causal mask does to them;
explain why the residual connection and layer norm are placed as they are;
count the parameters of a block and say what weight tying saves;
compute the gradient of the loss on the logits and take one step.
This chapter builds on Chapter 17, which defines the operations this chapter runs by hand.
Chapter 17 took the transformer apart and explained each piece, and this chapter puts one back together and runs it with numbers small enough to check on paper. The model here has four dimensions, two heads, one block and a vocabulary of eight, which comes to 172 parameters against GPT-3’s 175 billion, and every equation is the same. That last sentence is the reason for the chapter, because scale changes what a model can do without changing what a model is. If you can follow four numbers through sixteen steps then you have followed the architecture. The only thing left to learn about the large ones is what emerges when the same arithmetic is run at a size no one can trace. The prompt is four tokens in and one prediction out.
a woodchuck would chuck ?
The vocabulary is a, chuck, how, much, wood, woodchuck, would and the full stop, which is eight tokens indexed alphabetically from zero. One warning about the weights before we start. They are chosen and not trained, because every matrix in this chapter was written by hand so that its job is visible, and the chapter says what each one is for. A trained model reaches its weights by the process of Chapter 8 and they are never this tidy. What is genuinely the same is the arithmetic they go through, which is the subject here.
Nothing in a model consumes text. The tokeniser of Chapter 5 converts the string to integers, and here every word already sits in the vocabulary, which makes the mapping a lookup.
| position | token | id |
|---|---|---|
| 0 | a | 0 |
| 1 | woodchuck | 5 |
| 2 | would | 6 |
| 3 | chuck | 1 |
The embedding table is , with one row per token and four numbers per row, and the lookup is not a multiplication because the row is the answer. To make the arithmetic readable the four dimensions have been given meanings, namely thing, action, quantifier and wood. A trained table has no such labelled columns.
| token | thing | action | quantifier | wood |
|---|---|---|---|---|
| a | 0.0 | 0.0 | 1.0 | 0.0 |
| woodchuck | 1.0 | 0.0 | 0.0 | 0.5 |
| would | 0.0 | 1.0 | 0.0 | 0.0 |
| chuck | 0.0 | 1.0 | 0.0 | 0.5 |
Woodchuck is a thing that is half about wood while chuck is an action that is half about wood, and that overlap is what makes the example worth running.
Self-attention takes a set of vectors, so permuting the input permutes the output with it and changes nothing else. A woodchuck and woodchuck a would therefore be identical, which is a fatal flaw.
The fix is to make position part of the vector, and Chapter 17 gave the sinusoids:
With there are two frequencies, one that turns once per token and one a hundred times slower. Together they give each position a distinct signature.
| position | sin | cos | sin | cos |
|---|---|---|---|---|
| 0 | 0.0000 | 1.0000 | 0.0000 | 1.0000 |
| 1 | 0.8415 | 0.5403 | 0.0100 | 1.0000 |
| 2 | 0.9093 | 0.0200 | 0.9998 | |
| 3 | 0.1411 | 0.0300 | 0.9996 |
The fast pair separates neighbours, while the slow pair barely moves across four tokens and would be doing the work if the context were a thousand long.
The two are added and not concatenated, and that choice deserves a pause. The model gets four dimensions in total, for meaning and position together, and it has to share them.
| token | ||||
|---|---|---|---|---|
| a | 0.0000 | 1.0000 | 1.0000 | 1.0000 |
| woodchuck | 1.8415 | 0.5403 | 0.0100 | 1.5000 |
| would | 0.9093 | 0.5839 | 0.0200 | 0.9998 |
| chuck | 0.1411 | 0.0100 | 0.0300 | 1.4996 |
This is the block’s input, and everything that follows is a function of these sixteen numbers. Nothing else enters the block.
Each row is normalised on its own across its four dimensions, with no reference to the other tokens or to the batch. Row a has mean and variance , so its first entry becomes with . That stops a row of identical values, whose variance is zero, from dividing by nothing.
| token | ||||
|---|---|---|---|---|
| a | 0.5773 | 0.5773 | 0.5773 | |
| woodchuck | 1.1856 | 0.7194 | ||
| would | 0.7324 | 0.9683 | ||
| chuck | 1.7265 |
Normalising before the sublayer, not after it is the pre-norm arrangement. It is the reason a stack of ninety-six blocks can be trained at all, since the residual path from the input to the loss passes through no normalisation and gradients therefore reach the early layers undiminished. This is the gradient highway of Chapter 13, built again in a different architecture.
Each head sees the same normalised rows through three different matrices, one per projection. The two heads differ only in those matrices.
Head 1 was written to ask what thing am I acting on, so its query reads the action dimension while its key reads the thing dimension, and head 2 asks what else here is about wood. The comparison is then a scaled dot product, and the scores below are head 1’s.
| query key | a | woodchuck | would | chuck |
|---|---|---|---|---|
| a | 0.7777 | 0.6943 | 0.5226 | |
| woodchuck | 1.0170 | 0.1867 | 1.0647 | |
| would | 0.5369 | 0.3956 | 0.6030 | 1.2186 |
| chuck | 1.5083 | 0.3283 | 0.8423 | 2.3148 |
The division by looks cosmetic at and is nothing of the kind at . Chapter 17 measured why. The dot product of two vectors of unit variance has standard deviation , and a softmax fed large numbers saturates onto one key before training has begun.
The upper triangle is set to before the softmax, so that and no weight survives there.
| query key | a | woodchuck | would | chuck |
|---|---|---|---|---|
| head 1 | ||||
| a | 1.0000 | . | . | . |
| woodchuck | 0.7588 | 0.2412 | . | . |
| would | 0.3405 | 0.2957 | 0.3638 | . |
| chuck | 0.2462 | 0.0757 | 0.1265 | 0.5516 |
| head 2 | ||||
| a | 1.0000 | . | . | . |
| woodchuck | 0.1382 | 0.8618 | . | . |
| would | 0.0401 | 0.3684 | 0.5915 | . |
| chuck | 0.0674 | 0.1847 | 0.2821 | 0.4658 |
Three things stand out here. The first row of each head is and it has no choice. The first token can attend to nothing but itself, so the softmax is over a single value, and whatever the model knows about a at this point it knew before attention ran.
Every row sums to one, the softmax doing its job, and the reason attention is called a weighted average, not a sum. The two heads disagree. This is the argument for having more than one. Looking from woodchuck, head 1 sends three quarters of its weight back to a, the determiner that introduced it. Head 2 keeps on woodchuck itself, because it is asking about wood and woodchuck is the wood-bearing token so far. One mechanism, two questions, in parallel.
The plain reading of the last row is that both heads keep most of their weight on the current token, which is what untrained attention does. Specialisation is something training produces, and the heads that get published as clean pictures of syntax are heads that were trained into that shape.
Each head returns a weighted average of its values, and the heads are then concatenated back to width four and mixed by .
Then comes the residual connection, a single addition. It lets the block start as a small correction to the identity.
| token | after the residual | ||||
|---|---|---|---|---|---|
| a | 1.0000 | 1.0000 | |||
| woodchuck | 0.8132 | 0.5403 | 0.0100 | 2.7299 | |
| would | 0.9365 | 0.5839 | 0.0200 | 3.0060 | |
| chuck | 0.0100 | 0.0300 | 3.2513 | ||
Read the last column, which was near for chuck before attention and is after, because the wood dimension has been amplified by looking at woodchuck and its wood-ish neighbours. Information has moved between positions, the one thing attention is for.
Four dimensions in, eight hidden and four out, with every position going through this independently, using the same weights and with no reference to any other position.
| token | ||||||||
|---|---|---|---|---|---|---|---|---|
| a | 0.000 | 0.953 | 0.953 | 0.000 | 0.000 | 0.453 | 0.453 | 0.000 |
| woodchuck | 0.000 | 0.000 | 0.000 | 1.662 | 0.000 | 0.000 | 0.000 | 1.162 |
| would | 0.000 | 0.000 | 0.000 | 1.658 | 0.000 | 0.000 | 0.000 | 1.158 |
| chuck | 0.000 | 0.000 | 0.000 | 1.723 | 0.000 | 0.000 | 0.000 | 1.223 |
Count the zeros. Of the 32 hidden values, 22 are zero, which is per cent, and they are zero because the ReLU cut them off. Note that the pattern is uneven: row a keeps four of its eight units alive while the other three rows keep only two each. This sparsity is not an accident of a toy, because the feed-forward layers of real models are sparsely active in this way, and that observation makes mixture-of-experts routing possible, a thread Chapter 25 picks up.
Note also that the division of labour is now complete. Attention is the only part of the block that moves information sideways. The feed-forward layer is the only part that transforms a position on its own, and two thirds of the parameters in a real transformer sit in this second job. For this reason the feed-forward block is where capacity lives.
Equations (18.8) and (18.10) together are the block, and a large model is this pair ninety-six times over with wider matrices.
The block produced four vectors and only the last one is used, because only the last one has seen the whole prompt. The other three were needed to build it. After a final layer norm the last position is
To turn four numbers into a distribution over eight tokens, multiply by a matrix, and the transformer uses one it already has in , the embedding table read the other way.
This is weight tying, and the argument for it is the distributional hypothesis. If a row of says what a token means on the way in, then the same row can score how well the output matches that token on the way out. It also saves parameters, which is 32 here and 38 million in GPT-2.
| token | logit | probability | |
|---|---|---|---|
| wood | 1.0480 | 0.3107 | 31.1% |
| chuck | 0.3360 | 0.1524 | 15.2% |
| woodchuck | 0.1838 | 0.1309 | 13.1% |
| . | 0.0000 | 0.1089 | 10.9% |
| how | 0.0840 | 8.4% | |
| much | 0.0840 | 8.4% | |
| a | 0.0648 | 6.5% | |
| would | 0.0642 | 6.4% |
The model’s answer is wood, at 31 per cent, and the reason is not that the model knows the tongue twister. The last position accumulated a large value in the wood dimension by attending to woodchuck and chuck. The row of with the largest wood component is wood, and the dot product in Equation (18.11) is doing nothing more than measuring that agreement.
Note that how and much tie at , because they have identical embedding rows and nothing anywhere in the model can ever separate them. Two tokens with the same vector are the same token, whatever the spelling.
The target is wood, and cross-entropy asks one question: what probability did you give the right answer? A confident wrong model pays a large price.
The derivative is the reason softmax and cross-entropy are always paired. For a softmax followed by cross-entropy the gradient with respect to the logits collapses to something you can write down without any chain rule at all.
where is the one-hot target, so it is the predicted probability minus what it should have been, and nothing else is needed.
| token | target | after one step | ||
|---|---|---|---|---|
| a | 0.0648 | 0 | 0.0648 | 0.0575 |
| chuck | 0.1524 | 0 | 0.1524 | 0.1294 |
| how | 0.0840 | 0 | 0.0840 | 0.0738 |
| much | 0.0840 | 0 | 0.0840 | 0.0738 |
| wood | 0.3107 | 1 | 0.4017 | |
| woodchuck | 0.1309 | 0 | 0.1309 | 0.1123 |
| would | 0.0642 | 0 | 0.0642 | 0.0570 |
| . | 0.1089 | 0 | 0.1089 | 0.0945 |
One gradient is negative and seven are positive, so the negative one raises the right answer while the positive ones lower every wrong one, and their magnitudes are the probabilities that were wrongly assigned. A step of size takes the loss from to , an improvement of .
This is one step, on one example, on the logits alone. Training pushes this same vector backwards through Equation (18.11), then (18.10), (18.9), (18.8), (18.7), (18.6), (18.5), (18.4), (18.3) and into itself, by the chain rule of Chapter 8, and repeats it for every token of every document in the corpus.
| part | shape | parameters |
|---|---|---|
| embedding , tied | 32 | |
| attention, per head | 24 | |
| attention, two heads | 48 | |
| output projection | 16 | |
| feed-forward | 40 | |
| feed-forward | 36 | |
| total | 172 |
| model | layers | heads | parameters | |
|---|---|---|---|---|
| this chapter | 4 | 1 | 2 | 172 |
| GPT-2 small | 768 | 12 | 12 | 123,578,112 |
| GPT-2 medium | 1024 | 24 | 16 | 353,575,936 |
| GPT-3 | 12288 | 96 | 96 | 174,569,631,744 |
Only three numbers changed the parameter count: the width, the number of blocks and the vocabulary. The head count moved too, and it is free, for the reason Chapter 17 gave. No equation in this chapter was added to, removed or altered, so a billion-parameter model is Equations (18.2) through (18.11) run wider and more often.
What does change is what the weights end up containing. This is the subject of the rest of the book. Chapter 19 asks what happens when this block is trained on a corpus instead of written by hand, and what changes when the causal mask is removed.
Run it.
code/worked_examples/tiny_transformer.pyproduces every table in this chapter. Run it with no arguments for all sixteen steps in order, or take one stage at a time:--embedfor the lookup and the sinusoids,--attentionfor both heads with their masks,--ffnfor the sparsity of the hidden layer,--logitsfor the tied unembedding,--lossfor the gradient step, and--paramsfor the count.Change one weight and run it again. The fastest way to believe the architecture is to break it: set the mask aside and watch the model see its own answer, or zero the positional encoding and watch a woodchuck and woodchuck a become the same prompt.
Then build the same block as
torch.nn.TransformerEncoderLayer(d_model=4, nhead=2, dim_feedforward=8)and sum its parameters. It comes to , the same total as Step 16, and not one of its parts matches. The library spends nothing on an embedding and spends the that buys it here on attention biases and two layer norms instead. Itemise both columns until you can say where every parameter went, because a total that agrees for the wrong reasons is the easiest kind of number to trust by mistake.
Summary
This chapter followed four numbers through every step of one transformer block, with hand-chosen weights so that each job is visible. Tokens become integers and rows of a tied embedding matrix, a sinusoid adds position, and layer norm precedes a two-head causal attention whose weights show the heads asking different questions. A residual connection adds the sublayer back, a feed-forward layer widens, rectifies and narrows each position on its own, and a second residual follows. Only the last position is used, and the tied embedding scores every token to give a softmax. For softmax with cross-entropy the gradient on the logits is the predicted probability minus the target, and one step lowers the loss from to . The model has parameters. The same equations at GPT-2 and GPT-3 sizes change only the width, the number of blocks and the vocabulary.
Key terms
residual connection. an addition that carries a sublayer’s input around it, so that gradients have a direct path.
pre-norm. normalising the input of each sublayer instead of its output.
feed-forward sublayer. a position-wise widen, rectify and narrow map applied to each token on its own.
weight tying. using the embedding matrix as the output projection.
logit. the unnormalised score of a token before the softmax.
Vaswani et al. (2017) is the transformer paper, and its Section 3 is
the arithmetic of this chapter at full size. Alammar (2018) is the
diagram-led walkthrough most practitioners learn from. Jurafsky and
Martin (2025) give a textbook treatment of the transformer block. The
block in this chapter is what
torch.nn.TransformerEncoderLayer implements, and the last
problem below asks you to find where the two differ.
18.1 Count a bigger toy. Take , two heads of , a feed-forward width of and the same vocabulary of eight with tied embeddings. Fill the table of Step 16 for this model and give the total.
18.2 What tying saves. Weight tying saves parameters. Compute the saving for GPT-2 small, with a vocabulary of and width , and say what share of its parameters that is.
18.3 A masked row. A query at position has raw scores against positions , and . Apply the causal mask and the softmax, and give the three weights. Which equation of this chapter did you use?
18.4 One residual. A sublayer receives and returns . Compute the output of the residual connection, and say what the sublayer would have to return for the block to leave unchanged.
18.5 Why two tokens tie. Explain why how and much receive the same logit in this chapter and nowhere in the model can be made to separate them. Name the one change that would.
Build and measure
18.6 Reproduce a stage. Run
tiny_transformer.py with --attention and
confirm both heads’ tables. Then change one entry of
in head 1 and report which rows of which table move.
18.7 Break the positional encoding. Before running, predict whether a woodchuck and woodchuck a give different logits when the positional encoding is zero. Then run it and compare, and say what the answer shows about attention.
18.8 Remove the mask. Predict what happens to the weights in the last row when the mask is removed, and to the logits. Then run it, and say what a model trained this way would learn to do at generation time.
18.9 The library’s block. Build
torch.nn.TransformerEncoderLayer(d_model=4, nhead=2, dim_feedforward=8),
sum its parameters, and itemise them against the table of Step 16 until
every parameter is accounted for.
In practice
18.10 Doubling the width. A team doubles of a model and keeps the layer count and vocabulary. Say roughly how the parameters of each part, attention, feed-forward and embedding, change, and which part dominates at the new size.
18.11 Why pre-norm? A stack of ninety-six blocks trains with pre-norm and diverges with post-norm. Use the residual path described in this chapter to explain why, and say what you would check first if a deep model diverges.
18.12 Two small models. Two models share the equations of this chapter and differ only in the number of heads, and at the same width. Say what changes in the parameter count, in the cost of one forward pass, and in what the heads can represent.
Each one reproduces the tables above. Run it in the page, change an input and watch which numbers move.
tiny_transformer.pyA transformer, small enough to check
One prompt through every layer of a four-dimensional model, ending in a prediction, a loss and one step of learning.
runs in your browser Run itFrom Natural Language Processing: From Corpus Statistics to Grounded LLMs, by Ramaseshan Ramachandran.