AI / ML Theory
Chapter 6 · 18 min

6.10 — Pre-norm vs post-norm Transformer block

Goal: Compare post-LN (original AIAYN) and pre-LN (GPT-2 onward) block diagrams; understand why pre-norm trains stably without warmup, what it changes in the residual stream, and why every modern LLM is pre-norm.

1. Why this session exists

6.9 gave us residual connections; 3.8 gave us LayerNorm and RMSNorm. A Transformer block uses both, but a small architectural choice — where the norm sits relative to the residual — has outsized consequences. The original AIAYN block puts the norm after the residual addition (post-LN); GPT-2 and every LLM since put the norm before the sub-layer (pre-LN).

The two diagrams differ by the position of one box, but they train very differently:

  • Post-LN. Stable up to ~12 layers; needs a learning-rate warmup; gradient norms blow up near the input on deep stacks; sensitive to initialisation.
  • Pre-LN. Trains stably at 96+ layers; no warmup required; gradient norms are well-behaved across depth; tolerates a wider learning rate range.

This session shows both block diagrams, derives why pre-norm preserves the identity path of 6.9 while post-norm breaks it, and surveys the evidence that every modern LLM (GPT-2/3/4, LLaMA, Mistral, Qwen, Gemini, Claude) uses pre-norm. We will be writing pre-norm blocks for the rest of the course.[1, 3]


2. The two block variants

Let SubLayer(x)\text{SubLayer}(x) stand for either the multi-head attention sub-layer (6.4) or the FFN sub-layer (6.8). Both map Rd→Rd\mathbb{R}^d \to \mathbb{R}^d, and both are wrapped in a residual.

2.1 Post-LN (original AIAYN)

 y=LN ⁣(x+SubLayer(x)). \boxed{\ y = \text{LN}\!\Big(x + \text{SubLayer}(x)\Big). \ }

The norm acts after the residual addition. In a Transformer block this looks like

x′=LN ⁣(x+MHA(x)),y=LN ⁣(x′+FFN(x′)).\begin{aligned} x' &= \text{LN}\!\big(x + \text{MHA}(x)\big), \\ y &= \text{LN}\!\big(x' + \text{FFN}(x')\big). \end{aligned}

This is the diagram in Attention Is All You Need §3.1 (Vaswani et al., 2017[2]) — the one usually drawn with "Add & Norm" boxes after each sub-layer.

2.2 Pre-LN (GPT-2 and modern LLMs)

 y=x+SubLayer ⁣(LN(x)). \boxed{\ y = x + \text{SubLayer}\!\big(\text{LN}(x)\big). \ }

The norm acts before the sub-layer, inside the residual branch. Per block:

x′=x+MHA ⁣(LN1(x)),y=x′+FFN ⁣(LN2(x′)).\begin{aligned} x' &= x + \text{MHA}\!\big(\text{LN}_1(x)\big), \\ y &= x' + \text{FFN}\!\big(\text{LN}_2(x')\big). \end{aligned}

Same residuals, same sub-layers; the norms have moved one box. A final LNfinal\text{LN}_\text{final} is usually applied to xLx_L before the output projection — so that the last "raw" residual stream is rescaled exactly once at the end.

Side-by-side diagrams of post-norm and pre-norm Transformer blocks. Both show an input x flowing into a sub-layer with a residual addition. Post-norm has 'LayerNorm' on the trunk after the addition; pre-norm has 'LayerNorm' on the residual branch before the sub-layer, with the trunk untouched. A final LayerNorm appears at the top of the pre-norm stack.
Post-LN (left) versus pre-LN (right) Transformer block. In post-LN the norm sits on the main path, after the residual addition; in pre-LN the norm sits inside the residual branch, before the sub-layer.

3. The residual stream argument

The difference between post-LN and pre-LN is best read through the residual stream of 6.9 §5.

3.1 Pre-norm preserves the identity path

Stack LL pre-LN blocks. Write fℓ(⋅)=SubLayerℓ(LNℓ(⋅))f_\ell(\cdot) = \text{SubLayer}_\ell(\text{LN}_\ell(\cdot)) for the ℓ\ell-th residual branch. Then

xL=x0+∑ℓ=1Lfℓ(xℓ−1).x_L = x_0 + \sum_{\ell = 1}^{L} f_\ell(x_{\ell-1}).

This is the residual-stream formula. The Jacobian ∂xL/∂x0\partial x_L / \partial x_0 contains the identity term from 6.9 §3.2:

∂xL∂x0=∏ℓ=L1(I+Jℓ(f))=I + (corrections).\frac{\partial x_L}{\partial x_0} = \prod_{\ell = L}^{1} \big(I + J_\ell^{(f)}\big) = I \,+\, \text{(corrections)}.

The gradient flows un-attenuated along the skip path. The norms sit inside each residual branch — they affect fℓf_\ell but they do not touch the identity term. Pre-norm is the same gradient story as plain residual.

3.2 Post-norm breaks the identity path

Stack LL post-LN blocks. Define gℓ(x)=x+SubLayerℓ(x)g_\ell(x) = x + \text{SubLayer}_\ell(x), so xℓ=LN(gℓ(xℓ−1))x_\ell = \text{LN}(g_\ell(x_{\ell-1})). The Jacobian is

∂xℓ∂xℓ−1=∂LN∂gℓ⏟the norm’s Jacobian⋅(I+Jℓ(f))⏟residual term.\frac{\partial x_\ell}{\partial x_{\ell-1}} = \underbrace{\frac{\partial \text{LN}}{\partial g_\ell}}_{\text{the norm's Jacobian}} \cdot \underbrace{\big(I + J_\ell^{(f)}\big)}_{\text{residual term}}.

The norm's Jacobian is not identity. LayerNorm subtracts the mean and divides by the standard deviation, with a learnable scale γ\gamma that at initialisation is 11. As shown by Xiong et al. (2020)[6] — see §5 — for d≫1d \gg 1,

∂LN∂g≈1∥g∥/d (I−1d11⊤−1dg^g^⊤),\frac{\partial \text{LN}}{\partial g} \approx \frac{1}{\|g\|/\sqrt{d}}\, \Big(I - \tfrac{1}{d}\mathbf{1}\mathbf{1}^\top - \tfrac{1}{d}\hat{g}\hat{g}^\top\Big),

which has spectral norm typically less than 11, and which kills two specific directions of the input: the all-ones direction (mean subtraction) and the input's own normalised direction. Stacking LL such factors brings back the multiplicative-collapse problem of 2.9 — the precise thing residual connections were meant to avoid.

In short: post-LN puts a non-identity Jacobian on the main path of every block, so the multiplicative chain rule resumes its old behaviour.


4. Empirical consequence: warmup and depth

4.1 The warmup problem of AIAYN

The original AIAYN paper specifies a learning-rate warmup of 40004000 steps for 66-layer encoder/decoder pairs. The reason is the §3.2 gradient instability: at t=0t = 0, with random initialisation, the gradient magnitudes near the input of a deep post-LN stack are very different from those near the output, and a small initial learning rate is needed to let the parameter scales equilibrate. Without warmup, post-LN models diverge on the first few hundred steps.

This was tolerable at 6 layers. At 12 layers (BERT-base) it was already annoying. At 96 layers (GPT-3) it would have been a constant tuning burden — and that is if it converged at all.

4.2 Pre-LN removes the warmup

Empirically — and provably, via the analysis below — pre-LN models train stably from step 1 with any reasonable learning rate. Constant-LR training works, and a cosine schedule (3.7) or WSD (3.7) without warmup converges to within a couple of percent of the warmed-up baseline. The first paper to attribute GPT-2's stability to its pre-LN choice was Wang et al. (2019)[5], Improving deep Transformer with depth-scaled initialization and merged attention (arXiv:1908.11365).

4.3 What about post-LN's quality at convergence?

A subtle wrinkle: on small / shallow models (≤12 layers, BERT-base, vanilla translation), post-LN trained well if you warmed up and gave slightly better validation loss than pre-LN. This is because the inner norms in pre-LN partially constrain the residual stream's geometry (every sub-layer sees a unit-norm input), which is a mild regularisation. At scale (≥24 layers) this advantage is dwarfed by post-LN's training instability, and pre-LN dominates.

The frontier-model story is unambiguous: every LLM at ≥100\geq 100M parameters since GPT-2 is pre-LN, including GPT-2/3/4, LLaMA, Mistral, Qwen, Gemini and Claude. BERT (post-LN) is the last meaningfully-used post-LN model in the wild, and successor encoder models (DeBERTa-v3, modern E5/BGE) use pre-LN too.


5. Sketch of Xiong et al.'s argument

The standard reference is Xiong, Yang, He, Zheng, Xing, Zhang, Lan, Wang, Liu, On layer normalization in the Transformer architecture (2020, arXiv:2002.04745). They prove two claims under mild regularity assumptions:

Claim 1 (Post-LN gradient grows with depth at initialisation). For a post-LN Transformer of depth LL with random Gaussian initialisation, the expected gradient norm at the first sub-layer scales as

E[∥∂L/∂W1∥]=Θ(L),\mathbb{E}\Big[\big\|\partial \mathcal{L}/\partial W_1\big\|\Big] = \Theta(\sqrt{L}),

i.e. it grows with depth. This means deep post-LN models have gradient signals near the input that overwhelm a constant-LR optimiser — the rationale for warmup.

Claim 2 (Pre-LN gradient is depth-independent). Under the same assumptions, the pre-LN gradient norm at the first sub-layer scales as

E[∥∂L/∂W1∥]=Θ(1)\mathbb{E}\Big[\big\|\partial \mathcal{L}/\partial W_1\big\|\Big] = \Theta(1)

with no depth dependence. Constant-LR training is well-posed at any LL.

The proofs use the residual-stream expansion of 6.9 §3.2 and the fact that pre-LN's identity path commutes with the LayerNorm Jacobian on the residual branch only. We will not redo the calculation here — Xiong et al.'s presentation is clean and short (their §3).

The lesson is the one we already saw informally: putting the norm inside the residual leaves the identity path untouched; putting the norm outside the residual rescales it. Depth-independent gradient norms follow.


6. The norm-placement rule, generalised

The same lesson recurs throughout deep-learning architecture. Whenever a normalisation operation sits on the main path between two residual stages, the multiplicative-chain-rule problem returns; whenever the normalisation sits inside a residual branch, gradient flow remains depth-independent.

This is one rule with multiple instances:

  • ResNet-v1 (post-activation BN): the BN sits between two residuals on the main path → harder to train at extreme depth.
  • ResNet-v2 (pre-activation BN, He et al., 2016[4], Identity mappings in deep residual networks, arXiv:1603.05027): the BN moves into the residual branch → trains to 1000+ layers with the same recipe.
  • Pre-LN Transformer: the LN moves into the residual branch → 96+ layers train stably.

The pattern is structural, not specific to Transformers. The same proof template applies to any residual architecture.


7. The final layer norm

A pre-LN stack ends with one extra normalisation:

yout=LNfinal(xL) Wout.y_\text{out} = \text{LN}_\text{final}(x_L)\, W_\text{out}.

Reason: each block's residual branch normalises its input but the trunk xℓx_\ell itself is un-normalised — the residual additions cumulate without bound. As LL grows, ∥xL∥\|x_L\| drifts upward; if we projected the un-normalised xLx_L directly to vocabulary logits, the softmax (and the loss) would see an arbitrary scale. One LN at the very end fixes this without disturbing the gradient story.

Equivalently: pre-LN's invariant is "every sub-layer sees a unit-norm input"; the trunk does not have to be unit-norm at intermediate layers — only the inputs to sub-layers. The final LN converts the trunk to the same standard.


8. Hybrid and exotic variants (for context)

A few architectural offsprings worth knowing the names of, mostly outside the scope of this chapter:

  • DeepNorm (Wang et al., 2022, arXiv:2203.00555)[7]. A post-norm variant with carefully chosen residual scaling that trains at 10001000 layers. Used in some Microsoft/Tsinghua research papers; not in current frontier LLMs.
  • Sandwich-norm (Ding et al., 2021, GLM). Adds both pre-norm and post-norm. Stable but more parameters.
  • NormFormer (Shleifer et al., 2021, arXiv:2110.09456)[8]. Adds an extra LN after attention output. Reduces gradient norm in deeper layers.

All build on the pre-LN baseline. None has displaced it in any frontier LLM. For the rest of this course, "Transformer block" means pre-LN with one LNfinal\text{LN}_\text{final} at the top.


9. What we keep going forward

One Transformer block (pre-LN){x′=x+MHA ⁣(LN1(x)),y=x′+FFN ⁣(LN2(x′)).\text{One Transformer block (pre-LN)} \quad \begin{cases} x' = x + \text{MHA}\!\big(\text{LN}_1(x)\big), \\ y = x' + \text{FFN}\!\big(\text{LN}_2(x')\big). \end{cases}

Two residual additions, two LayerNorms placed inside the residual branches, no warmup needed, depth-independent gradient norms. Next session assembles the full decoder-only stack (LL such blocks, embedding + LM head + final norm) and pairs it with a guided read of Attention Is All You Need.

Self-test

(a) Write out the post-LN block y=LN(x+SubLayer(x))y = \text{LN}(x + \text{SubLayer}(x)) in pseudocode (5 lines) and the pre-LN block y=x+SubLayer(LN(x))y = x + \text{SubLayer}(\text{LN}(x)) in pseudocode (5 lines). Identify, in each, the gradient path that bypasses SubLayer\text{SubLayer}.

(b) For pre-LN, derive that the Jacobian of one block factorises as I+JSubLayer⋅JLNI + J_\text{SubLayer} \cdot J_\text{LN}. For post-LN, derive that the Jacobian factorises as JLN⋅(I+JSubLayer)J_\text{LN} \cdot (I + J_\text{SubLayer}). Which of these has an identity term that does not depend on JLNJ_\text{LN}?

(c) Suppose ∥JLN∥=0.5\|J_\text{LN}\| = 0.5 uniformly (a rough average for a wide LN). Bound the spectral norm of the post-LN Jacobian over L=24L = 24 stacked blocks. Compare to the pre-LN bound.

(d) Read Xiong et al. (2020, arXiv:2002.04745) §3 and reproduce, in your notes, the two-line argument that the post-LN gradient grows as L\sqrt{L} and the pre-LN gradient is O(1)O(1).

(e) Argue why pre-LN requires a final LNfinal\text{LN}_\text{final} before the output projection but post-LN does not.


References

  1. [1]

    Raschka, S. (2024). Build a Large Language Model (From Scratch), Chapters 3–4. Manning. — Self-attention implementation (6.3, 6.4), causal masking (6.5), pre-norm block (6.10–6.11).

    ↩
  2. [2]

    Vaswani, A., Shazeer, N., Parmar, N., et al. (2017). "Attention is all you need". NeurIPS. arXiv:1706.03762 — The Transformer paper. Scaled dot-product attention (6.2), multi-head attention (6.4), encoder–decoder cross-attention (6.3), sinusoidal positional encoding (Appendix 6.A), the FFN sub-layer (6.8), post-LN block diagram (6.10), full architecture read (6.11).

    ↩
  3. [3]

    Radford, A., Wu, J., Child, R., et al. (2019). "Language models are unsupervised multitask learners" (GPT-2). OpenAI technical report. — Learned absolute positional encoding (Appendix 6.B), pre-LN variant (6.10), the architectural skeleton of every modern decoder-only LLM (6.11).

    ↩
  4. [4]

    He, K., Zhang, X., Ren, S., & Sun, J. (2016). "Identity mappings in deep residual networks". arXiv:1603.05027 — Pre-activation ResNet; the structural blueprint behind pre-LN Transformer (6.10 §6).

    ↩
  5. [5]

    Wang, Q., Li, B., Xiao, T., et al. (2019). "Improving deep transformer with depth-scaled initialization and merged attention" (a.k.a. learning deep Transformers). arXiv:1908.11365 — Early analysis of post-LN instability; motivation for pre-LN (6.10 §4.2).

    ↩
  6. [6]

    Xiong, R., Yang, Y., He, D., et al. (2020). "On layer normalization in the Transformer architecture". arXiv:2002.04745 — Theoretical analysis: post-LN gradient grows as √L, pre-LN is O(1) (6.10 §5).

    ↩
  7. [7]

    Wang, H., Ma, S., Dong, L., et al. (2022). "DeepNet: Scaling Transformers to 1000 layers" (DeepNorm). arXiv:2203.00555 — Stable post-norm variant via residual scaling (6.10 §8).

    ↩
  8. [8]

    Shleifer, S., Weston, J., & Ott, M. (2021). "NormFormer: Improved transformer pretraining with extra normalization". arXiv:2110.09456 — Additional norm placements for deeper models (6.10 §8).

    ↩