8. The transformer block
Attention plus a feed-forward network, wrapped in residual connections and normalisation. This unit, repeated dozens of times, is the entire model.
By the end of this lesson you can
- Draw a transformer block from memory, in the correct order
- Explain what residual connections do for gradients and for representation
- Explain LayerNorm and RMSNorm and why pre-norm won
- Describe the residual stream as a shared communication channel
The block, in four lines
Modern (pre-norm) transformer block, exactly as implemented:
x = x + Attention(Norm(x))
x = x + FFN(Norm(x))
Two sub-layers. Each one normalises a copy of the input, computes something, and adds the result back to the untouched original. Note what is not happening: nothing is ever overwritten. Each sub-layer contributes an increment.
The division of labour is clean:
- Attention moves information between positions. It is the only part of the model that looks sideways.
- The FFN transforms each position independently. It never looks sideways at all.
Interleaving them is what lets a stack of blocks build progressively richer, context-aware representations: gather, process, gather, process.
Dataflow through a block
Follow a vector through both sub-layers. Toggle the residual connections off to see what breaks.
Residual connections
The x + is easy to overlook and is doing two crucial jobs.
Gradient flow. In a plain deep stack, the gradient is multiplied by each layer's Jacobian on the way back; over 96 layers those factors compound and the signal explodes or vanishes. A residual connection gives the gradient an identity path — an additive route straight from the loss to the earliest layers. Without it, models this deep would not train at all.
The residual stream as a channel. The more modern reading: the vector flowing through the model is a shared communication bus. Every sub-layer reads from it (through its projection matrices) and writes to it (by adding). An early layer can deposit a piece of information that a layer 40 blocks later retrieves, without any intervening layer needing to preserve it deliberately.
This picture also explains a practical fact: because everything is added into one stream, features occupy directions in that space, and different circuits communicate by writing to and reading from different directions.
Normalisation, and where to put it
LayerNorm takes the vector at a position, subtracts its mean, divides by its standard deviation, then scales and shifts by learned parameters γ and β. It keeps activation magnitudes in a stable range so that training does not drift into overflow or collapse.
RMSNorm drops the mean subtraction and the shift, dividing only by the root-mean-square. It is cheaper, empirically just as good, and is what Llama and most recent models use.
The placement question turned out to matter more than the choice of norm:
- Post-norm (original 2017):
x = Norm(x + Sublayer(x)). The residual path itself gets normalised, which weakens the clean identity route. Deep post-norm models need careful learning-rate warmup and are prone to divergence. - Pre-norm (everything modern):
x = x + Sublayer(Norm(x)). The residual path stays untouched all the way from input to output. Far more stable, trains at higher learning rates, scales to great depth.
One consequence of pre-norm: the residual stream itself is never normalised on its way through, so its magnitude grows steadily with depth — visible in the figure below. Every pre-norm model therefore ends with a final norm after the last block, before the output projection. It is not an optional extra; without it the unbounded stream hits the output projection directly.
Pre-norm vs post-norm
Simulated activation magnitude through a deep stack under both placements.
Counting the parameters
For one block with d = d_model and a 4× FFN, ignoring biases and norm parameters:
| Component | Shapes | Parameters |
|---|---|---|
| W_Q, W_K, W_V, W_O | four of [d, d] | 4d² |
| FFN up and down | [4d, d] and [d, 4d] | 8d² |
| Total per block | 12d² |
So a 32-layer model with d_model = 4096 holds roughly 32 × 12 × 4096² ≈ 6.4 billion parameters in its blocks, plus the embedding table. The useful rule of thumb: parameters ≈ 12 × n_layers × d_model², with about two thirds of it in the feed-forward networks.
Lesson in one breath
A block is: normalise, attend, add back; normalise, feed forward, add back. The residual stream carries information straight through; each sub-layer reads it, computes something, and adds a correction. Pre-norm placement is what makes very deep stacks trainable.
Practice
Answers are checked in your browser and saved to this device. Get one wrong and you can retry as many times as you like.
Put the operations of one modern pre-norm transformer block in order.
What is the primary reason residual connections make very deep transformers trainable?
Which sub-layer of a transformer block moves information between token positions?
Using the rule 12 × n_layers × d_model², estimate the block parameters of a model with 40 layers and d_model = 5120. Give the answer in billions, to one decimal place.
Why did the field move from post-norm to pre-norm placement?
Done with this lesson?
A lesson counts as complete once it is marked read and every exercise is solved.
Tip: press ← and → to move between lessons.