Part IV ยท Context
Ch 10. The transformer block
Assembling the pieces
Youโve now met every ingredient of a transformertransformer: The model design used by modern LLMs: a stack of repeated blocks built around attention. Ch 10: token vectors (Ch 5), layers of neurons (Ch 6), and attention (Ch 9). A transformer arranges them into a block and stacks that block many times.[1][1][1] Build GPT-2 small one piece at a time:
Two pieces are new. The โ+โ circles mean attention and the neuron layer (the MLP) add a change to each tokenโs vector instead of replacing it, so the original passes straight through on a highway.[2] Layer norm rescales the numbers to a steady size before each step.[3] Both make big networks much easier to train.
Two pieces are new:
- The โ+โ circles. Attention and the neuron layer (called the MLP) donโt replace a tokenโs vector; they add a change to it. The original passes straight through on a highway, and each block adds its contribution. This trick, called a residual connection, makes very deep networks much easier to train.[2][2]
- Layer norm. Before each step, the numbers are rescaled to a steady size, which also helps training stay stable.[3][3] GPT-2 puts it at the start of each step, plus once more at the very end.[4][4]
A block, written down
With a tokenโs vector on the highway, one GPT-2 block computes
where is the attention from Ch 9, is the two layers of neurons from Ch 6, and goes on to the next block. Layer norm rescales the 768 numbers of a vector by their own mean and variance :
where and are 768 learned numbers each, multiplies number by number, and is a tiny constant (0.00001 in GPT-2) that avoids dividing by zero.[3][5] The original transformer put the norm after each addition instead, as ; GPT-2 moved it to the start of each step.[1][4]
The counter shows where GPT-2โs 124 million weights live: most are in the token table at the start and in the MLPs. Attention, the star of the last chapter, has fewer than a quarter.
The counter shows where GPT-2โs 124 million weights live. About a third are in the token and position tables at the start. Almost half are in the MLPs, which take up two thirds of every block. Attention, the star of the last chapter, has fewer than a quarter.
Counted exactly, each block has 2,362,368 weights in attention ( plus biases), 4,722,432 in the MLP, and 3,072 in its two layer norms: 7,087,872 in all. Adding the token table (50,257 ร 768), the position table (1,024 ร 768), the 12 blocks, and the final layer norm (2 ร 768) gives the total on the counter:
Thereโs no separate output table: the last step reuses the token table, so it isnโt counted twice.
Watch a guess take shape
That highway is called the residual streamresidual stream: The running vector for each token that every block reads from and adds its result to. Ch 10. Every block reads from it and writes back to it, and the final stream becomes the prediction. You can also stop partway and ask for a prediction from the middle, a trick called the logit lens.[4] Here it is on โThe cat sat on theโ:
That highway is called the residual streamresidual stream: The running vector for each token that every block reads from and adds its result to. Ch 10. Every block reads from it and writes back to it, and the final stream becomes the prediction. So what if you stopped early and asked for a prediction from the middle of the stack? Researchers do exactly that, a trick called the logit lens.[5][6] Here it is on โThe cat sat on theโ:
Early on, the top guess is โ sameโ, as in โsat on the sameโ: a sensible kind of word, but not about cats. Later, โ floorโ rises to the top and furniture appears. The last block ends at โ floorโ, โ bedโ, and โ couchโ, the guesses from Ch 0.
At the start, the stream is just the vectors for โ theโ and its position, and read as a guess itโs nonsense. From block 1 to block 7 the top guess is โ sameโ, as in โsat on the sameโ: a sensible kind of word, but not about cats. From block 8 on, โ floorโ is at or near the top (โ groundโ edges ahead once), and furniture appears: โ tableโ, โ sofaโ, โ bedโ. The last block spreads the bets out again, ending at โ floorโ, โ bedโ, and โ couchโ, the same guesses you saw in Ch 0.
The lens reads block โs stream the way the model reads its last one: apply the final layer norm, then score every token against the token table and take softmax,
where is the final layer norm and is the token table turned on its side, so each tokenโs score is its vector times the stream.[6] The lens is only a rough view: the middle blocks werenโt trained to be read this way.
No single block โknowsโ the answer. Each one nudges the stream, and the prediction sharpens as it goes.
Recipe card
Recap
- A transformer block is attention plus an MLP, each added onto the residual stream, with layer norm before each.
- GPT-2 small stacks 12 blocks. Most of its weights are in the MLPs and the token table, not in attention.
- Each block makes a small change; reading the stream partway through shows the guess forming.
- A block is , then the same with the MLP: 7,087,872 weights each in GPT-2.
Next question: the model gives probabilities for one next token. How does that become a whole paragraph?
Sources
- Ashish Vaswani et al. (2017). Attention Is All You Need. NeurIPS 2017.
- Kaiming He et al. (2016). Deep Residual Learning for Image Recognition. CVPR 2016.
- Jimmy Lei Ba et al. (2016). Layer Normalization. arXiv.
- nostalgebraist (2020). interpreting GPT: the logit lens. LessWrong. Accessed October 23, 2026.
Sources
- Ashish Vaswani et al. (2017). Attention Is All You Need. NeurIPS 2017.
- Kaiming He et al. (2016). Deep Residual Learning for Image Recognition. CVPR 2016.
- Jimmy Lei Ba et al. (2016). Layer Normalization. arXiv.
- Alec Radford et al. (2019). Language Models are Unsupervised Multitask Learners. OpenAI.
- nostalgebraist (2020). interpreting GPT: the logit lens. LessWrong. Accessed October 23, 2026.
Sources
- Ashish Vaswani et al. (2017). Attention Is All You Need. NeurIPS 2017.
- Kaiming He et al. (2016). Deep Residual Learning for Image Recognition. CVPR 2016.
- Jimmy Lei Ba et al. (2016). Layer Normalization. arXiv.
- Alec Radford et al. (2019). Language Models are Unsupervised Multitask Learners. OpenAI.
- OpenAI and Hugging Face (2019). GPT-2 small configuration (config.json). Hugging Face. Accessed October 23, 2026.
- nostalgebraist (2020). interpreting GPT: the logit lens. LessWrong. Accessed October 23, 2026.