LLM Kitchen
Explanations

Part IV ยท Context

Ch 10. The transformer block

Assembling the pieces

Youโ€™ve now met every ingredient of a : 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:

Step through

Build a transformer

Computed in your browser
word vector+ position vector
Parameters so far
39,383,808

Start: each token becomes its word vector (Ch 5) plus a position vector (Ch 9). This running list of 768 numbers per token is the stream.

Step 1 of 6 or use โ† โ†’ keys

Start: each token becomes its word vector (Ch 5) plus a position vector (Ch 9). This running list of 768 numbers per token is the stream.

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 x a tokenโ€™s vector on the highway, one GPT-2 block computes

xโ€ฒ=x+Attnโก(LNโก(x)),xโ€ฒโ€ฒ=xโ€ฒ+MLPโก(LNโก(xโ€ฒ))

where Attnโก is the attention from Ch 9, MLPโก is the two layers of neurons from Ch 6, and xโ€ฒโ€ฒ goes on to the next block. Layer norm rescales the 768 numbers of a vector by their own mean ฮผ and variance ฯƒ2:

LNโก(x)=ฮณโŠ™xโˆ’ฮผฯƒ2+ฯต+ฮฒ

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 LNโก(x+Attnโก(x)); 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 (4ร—7682 plus 4ร—768 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:

50,257ร—768+1,024ร—768+12ร—7,087,872+2ร—768=124,439,808

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 . 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 . 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โ€:

Try it

Watch the guess form

Recorded ยท GPT-2 small

โ€œThe cat sat on the___โ€: the stream for the last token, ยทthe, block by block.

Top guesses if the model stopped at each stage
StageStream (first 48 of 768 numbers)Top guesses if it stopped here
start +720ยทdestro 62%ยทmathemat 24%ยทlivest 7.3%
block 1 +720ยทsame 70%ยทfirst 3.2%ยทlatter 2.9%
block 2 +720ยทsame 89%ยทfirst 1.8%ยทworld 1.2%
block 3 +720ยทsame 95%ยทlast 0.6%ยทworld 0.5%
block 4 +720ยทsame 93%ยทopposite 0.9%ยทlast 0.6%
block 5 +720ยทsame 88%ยทtable 1.2%ยทnext 1.1%
block 6 +720ยทsame 75%ยทtable 4.3%ยทopposite 3.6%
block 7 +720ยทsame 53%ยทshoulders 8.5%ยทtable 8.2%
block 8 +720ยทfloor 25%ยทedge 11%ยทtable 10%
block 9 +720ยทground 23%ยทfloor 20%ยทtable 19%
block 10 +720ยทfloor 46%ยทsofa 8.5%ยทbed 8.1%
block 11 +720ยทfloor 33%ยทsofa 14%ยทbed 11%
block 12 +720ยทfloor 7.6%ยทbed 6.5%ยทcouch 5.4%

Read after block 12 (the end), the best guess is ยทfloor (7.6%).

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 xโ„“ the way the model reads its last one: apply the final layer norm, then score every token against the token table WE and take softmax,

pโ„“=softmaxโก(LNfโก(xโ„“)WEโŠค)

where LNfโก is the final layer norm and WEโŠค 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 x+Attnโก(LNโก(x)), 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

  1. Ashish Vaswani et al. (2017). Attention Is All You Need. NeurIPS 2017.
  2. Kaiming He et al. (2016). Deep Residual Learning for Image Recognition. CVPR 2016.
  3. Jimmy Lei Ba et al. (2016). Layer Normalization. arXiv.
  4. nostalgebraist (2020). interpreting GPT: the logit lens. LessWrong. Accessed October 23, 2026.

Sources

  1. Ashish Vaswani et al. (2017). Attention Is All You Need. NeurIPS 2017.
  2. Kaiming He et al. (2016). Deep Residual Learning for Image Recognition. CVPR 2016.
  3. Jimmy Lei Ba et al. (2016). Layer Normalization. arXiv.
  4. Alec Radford et al. (2019). Language Models are Unsupervised Multitask Learners. OpenAI.
  5. nostalgebraist (2020). interpreting GPT: the logit lens. LessWrong. Accessed October 23, 2026.

Sources

  1. Ashish Vaswani et al. (2017). Attention Is All You Need. NeurIPS 2017.
  2. Kaiming He et al. (2016). Deep Residual Learning for Image Recognition. CVPR 2016.
  3. Jimmy Lei Ba et al. (2016). Layer Normalization. arXiv.
  4. Alec Radford et al. (2019). Language Models are Unsupervised Multitask Learners. OpenAI.
  5. OpenAI and Hugging Face (2019). GPT-2 small configuration (config.json). Hugging Face. Accessed October 23, 2026.
  6. nostalgebraist (2020). interpreting GPT: the logit lens. LessWrong. Accessed October 23, 2026.