Start studying
Module 5 · Transformers Module contents
Module 5  ·  Card 13 / 19intermediate

Pre-Norm

Pre-norm applies layer normalization before the attention or feed-forward block.

The original transformer applied it afterwards, which is called post-norm.

Post-norm needs a careful warmup, or very deep models fail to train at all.

Pre-norm keeps the residual path clean, so gradients still reach the early layers.

Almost every large model built since uses pre-norm for that reason.

Learn in depth ↓
Module 05 · Transformers · 19 concepts
In depth  /  15 lines

Pre-Norm, in more detail

Pre-norm and post-norm differ only in where the normalisation sits.

Post-norm runs the sublayer, adds the residual, then normalises the result.

Pre-norm normalises the input, runs the sublayer, then adds the residual.

The consequence is about what happens to the residual path.

In post-norm the path passes through a normalisation at every layer.

Gradients are rescaled at each step and, deep in the stack, arrive unhelpfully small.

In pre-norm the path is a plain chain of additions from input to output.

A gradient can travel the whole model without being rescaled once.

Post-norm models need a careful warmup schedule or they diverge in the first few hundred steps.

That fragility grows with depth, and beyond a few dozen layers it becomes acute.

Pre-norm trains reliably at eighty layers or more with far less ceremony.

The original 2017 transformer used post-norm, which is why the warmup recipe became folklore.

GPT-2 moved to pre-norm and essentially every large model since has followed.

Post-norm is sometimes argued to reach slightly better final quality when it can be trained.

Its instability is the reason almost nobody takes that bet.

Want the full technical article? Wikipedia ↗
Deep dive

What is underneath Pre-Norm

Where the normalisation sits

post-norm:xl+1=LN(xl+F(xl))\text{post-norm} : \mathbf{x}_{l+1} = \mathrm{LN}\big( \mathbf{x}_l + F(\mathbf{x}_l) \big)
pre-norm:xl+1=xl+F(LN(xl))\text{pre-norm} : \mathbf{x}_{l+1} = \mathbf{x}_l + F\big( \mathrm{LN}(\mathbf{x}_l) \big)
  • FF: the sublayer, either attention or the feed-forward block.
  • LN\mathrm{LN}: layer normalisation.

The original transformer normalised after adding the residual. Every model since about 2019 normalises before the sublayer instead.

The difference is one line and it decides whether a deep transformer trains at all.

Why the residual path must stay clean

xL=x0+∑l=0L−1F(LN(xl))\mathbf{x}_L = \mathbf{x}_0 + \sum_{l=0}^{L-1} F\big(\mathrm{LN}(\mathbf{x}_l)\big)

Under pre-norm the residual stream runs from the input to the output with nothing applied to it, so every block writes into a shared, unmodified channel.

∂xL∂x0=I+∑l∂Fl∂x0\frac{\partial \mathbf{x}_L}{\partial \mathbf{x}_0} = I + \sum_{l} \frac{\partial F_l}{\partial \mathbf{x}_0}

The gradient therefore reaches layer zero through a path that multiplies by the identity, whatever the depth.

post-norm:∂xL∂x0=∏l=1L∂LN∂⋅(I+∂Fl∂⋅)\text{post-norm} : \frac{\partial \mathbf{x}_L}{\partial \mathbf{x}_0} = \prod_{l=1}^{L} \frac{\partial \mathrm{LN}}{\partial \cdot} \left( I + \frac{\partial F_l}{\partial \cdot} \right)

Post-norm puts a normalisation on that path at every layer, so the gradient is rescaled LL times and the product can shrink geometrically again.

What that changes in practice

Post-norm transformers need a careful learning rate warmup, and without it the loss diverges in the first few hundred steps.

Pre-norm trains with a short warmup or none, tolerates a larger learning rate, and scales past a hundred layers where post-norm stalls near twenty.

Var(xl)∝l\mathrm{Var}(\mathbf{x}_l) \propto l

The cost is that the residual stream grows as blocks keep adding to it, so activations at the last layer are much larger than at the first.

That is why a pre-norm stack ends with one final normalisation before the output projection. Without it the logits inherit the accumulated scale.

One block, in order

  1. Normalise the incoming residual stream.
  2. Run attention on the normalised copy.
  3. Add the result back into the unnormalised stream.
  4. Normalise again, run the feed-forward block, and add that back.
  5. After the last block, normalise once more before the language modelling head.

The refinements on top

RMSNorm(x)=x1d∑ixi2⋅g\mathrm{RMSNorm}(\mathbf{x}) = \frac{\mathbf{x}}{\sqrt{\frac{1}{d}\sum_i x_i^2}} \cdot \mathbf{g}

RMSNorm drops the mean subtraction and keeps only the rescaling, which is cheaper and works as well, so most current models use it in place of layer norm.

xl+1=xl+αlF(LN(xl))\mathbf{x}_{l+1} = \mathbf{x}_l + \alpha_l F\big(\mathrm{LN}(\mathbf{x}_l)\big)

Some large models scale each residual branch by a small factor at initialisation, so the stack begins close to the identity and the blocks learn to contribute gradually.

Post-norm has not disappeared entirely. It produces slightly better final quality when it can be trained, which is why a few models use hybrid schemes that normalise in both places.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
Why did large models move from post-norm to pre-norm?
Transformers

19 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close