The transformer block
Residual stream, normalization and the gated MLP
Chapter 7 of 21 · II. The transformer · about 40 minutes. Builds on: 2. Neural networks and the multilayer perceptron, 6. Attention.
The block and the residual stream
Section titled “The block and the residual stream”A transformer is a stack of identical-shaped blocks. Each block does two things to the token vectors: first it lets tokens exchange information (attention, Chapter 6), then it processes each token individually (an MLP, Chapter 2). Both are wrapped in residual connections and normalization:
Because every sublayer adds its output to , there is one vector per token that runs the full height of the network, the residual stream. Layers read from it (after normalization) and write back to it. Early layers write simple features, later layers write more abstract ones, and the last layer’s stream is turned into logits. This “pre-norm” placement, with the norm inside the residual branch, trains stably at depth: show that with the norm placed after the residual (the original arrangement) gradients at initialization are large near the output and need careful warmup, while the pre-norm arrangement is well behaved.
Normalization
Section titled “Normalization”Left alone, activations can drift in scale from layer to layer. Layer normalization normalizes each token’s vector across its components:
RMSNorm drops the mean subtraction and the bias, keeping only the rescaling:
It is cheaper and works as well, which is why Llama, DeepSeek and gpt-oss all use it. The gain has only parameters per norm, negligible next to the matrices.
The MLP, and why it is “gated” now
Section titled “The MLP, and why it is “gated” now”Chapter 2’s MLP was with a hidden width of 4d. Current models use a gated variant from . With SiLU as the activation (called SwiGLU):
There are two parallel up-projections. One passes through the activation and acts as a per-feature “gate” that multiplies the other. Three matrices instead of two means each hidden unit costs 1.5× as much, so the hidden width is reduced (about instead of 4d) to keep parameter counts comparable: Llama 3 8B uses and hidden width 14,336 (3.5d).
What does the MLP do? argue it acts as a key–value memory: each row of the first matrix is a “key” that fires on a certain pattern in the input, and each column of the second matrix is a “value” written into the residual stream when its key fires. Much of a model’s stored factual knowledge appears to sit in these matrices. Mixture-of-experts models (Chapter 14) split the MLP rather than attention, since it holds most of the parameters and works on each token independently.
Counting parameters
Section titled “Counting parameters”With model width , layers, vocabulary and full multi-head attention:
Attention counts . The embedding term doubles if the output matrix is not tied to the input one.
Use the calculator. Choose a preset and check the result against the published size; then modify the settings.
Two things to notice. First, the MLP holds most of the layer parameters: two thirds in the classic design and about 80% in Llama 3 8B. Second, grouped-query attention (Chapter 11) reduces the key/value matrices, so real models have fewer than attention parameters per layer.
Variations you will meet
Section titled “Variations you will meet”- Clamping and tweaks. The gpt-oss model card notes its SwiGLU implementation is unconventional (it clamps values and includes a residual connection) . Reading the exact formula in model code, not just the name, matters when you reproduce or analyze a model.
- Normalization of queries and keys and other stability tricks are common in recent models.
- Final norm. After the last block, one more normalization is applied before the output matrix.
Check yourself
Section titled “Check yourself”What is the gradient path from the loss back to layer 1 in a pre-norm residual stack?
The identity path through all residual additions, plus paths through each sublayer. The identity path gives a gradient that does not shrink with depth.
Why does RMSNorm have no bias term, and what does it lose compared with LayerNorm?
It only rescales; it does not recenter. Empirically the recentering contributes little, and skipping it saves computation.
Estimate parameters: L = 32, d = 4096, d_ff = 14336 (gated), 8 KV heads of 128, 32 query heads, V = 128,256, untied.
Attention per layer: () (, ) () M. MLP: M. Per layer B. Embeddings B. Total B.
Papers
Section titled “Papers”- Section 3 for the original block.
- Layer normalization.
- RMSNorm.
- Why pre-norm trains more easily than post-norm.
- Gated MLPs; a short paper that is worth reading in full.
- MLPs as key–value memories.
- Table 3 lists the exact dimensions of Llama 3 8B, 70B and 405B used above.