All courses

The pre-norm transformer block

Write a block as r ← r + Attn(Norm(r)); r ← r + FFN(Norm(r)). Explain why this pre-norm form trains more stably than the original post-norm arrangement, which normalized after each residual addition, r ← Norm(r + Attn(r)), and so put the normalization on the main path itself.

Builds on: Residual connections, LayerNorm and RMSNorm, Multi-head attention, The position-wise feed-forward sublayer

▶ Watch this lesson Start the whole course

What the animation says

One call, two places

The original transformer normalized after each residual addition. Modern models move that same Norm to the input of each sublayer. Same call, same cost. Yet deep post-norm stacks often fail to train, while pre-norm stacks train reliably. Picture the running vector as a main road. Each sublayer is a side workshop: it reads from the road and adds its result back. Post-norm puts a checkpoint on the road that rescales everything after each addition. Pre-norm puts the Norm only on the side road. So what does one checkpoint actually do to the road?

One checkpoint, in numbers

Take post-norm with gain one. Right after a Norm, the road has RMS one. A sublayer adds an output that also has RMS one, uncorrelated with the road. Variances add: one plus one is two, so the sum has RMS root two. The Norm divides by root two. The old content on the road is now scaled by about zero point seven one. Twelve blocks means twenty-four sublayers, so twenty-four checkpoints. What scale does the original embedding signal end up with?

It compounds, both ways

The factors multiply. One over root two, twenty-four times, is one over two to the twelve: about one in four thousand. That hits the embedding information travelling forward. And it hits gradients coming back. The backward pass crosses the same Norms, and each one scales the gradient down the same way. Precisely: the Norm removes the part along the road's own direction, and scales every other direction by gain over RMS. More blocks, more checkpoints, smaller factor. That's why deep post-norm needs such gentle handling.

Pre-norm: a clean road

Pre-norm moves the Norm onto the side road. Each workshop reads a normalized copy of the road. The road itself is just a running sum. Its identity factor is exactly one at every sublayer, forward and backward. So the road grows. Embeddings of RMS one, plus twenty-four uncorrelated outputs of RMS one: variance twenty-five, RMS five. One final Norm after the last block tidies it up for the output. But after twelve blocks, what RMS does each workshop actually read?

Both blocks in code

Every workshop reads RMS one, even while the road grows. Now the code. Here's the pre-norm block. In both lines, rmsnorm wraps only the sublayer's input, and the road is plain addition. Here's the post-norm block. Here rmsnorm wraps the whole sum, so it sits on the road. Run twelve blocks of each with stand-in sublayers: pre-norm's road grows, post-norm's stays near one. Your turn: drag the Norm in this post-norm line to the spot that makes it pre-norm.

Why does moving one Norm call matter?

The original transformer normalized **after** each residual addition: $r \leftarrow \text{Norm}(r + \text{Attn}(r))$. Stacked deep, this version often fails to train unless it is handled very gently. Modern models move the Norm to the **input of each sublayer** instead, and deep stacks train reliably. The call is the same and the cost is essentially the same. Why does its position decide whether a 50-block model trains at all?

Keep the main road clean

Picture the running vector $r$ as a main road. Each sublayer is a side workshop: it reads from the road and adds its result back. **Post-norm** puts a checkpoint *on the road* that rescales everything after each addition. Old content and gradients get divided down again and again, so the shrinkage compounds with depth. **Pre-norm** puts the Norm only on the side road. The workshop reads a normalized copy, and the road itself stays a clean running sum.

Pre-norm vs post-norm, precisely

Pre-norm block $\ell$, with $r = r^{(\ell-1)}$: $$u = r + \text{Attn}(\text{Norm}_1(r)),\quad r^{(\ell)} = u + \text{FFN}(\text{Norm}_2(u))$$ Each sublayer has Jacobian $I + \partial f(\text{Norm}(r))/\partial r$, so the identity term is exact. Unrolled, $r^{(L)} = r^{(0)} + \sum$ (all sublayer outputs). One final Norm follows block $L$. Post-norm, $r \leftarrow \text{Norm}(r + f(r))$, has Jacobian $J_{\text{Norm}}(I + \partial f/\partial r)$. For RMSNorm, $J_{\text{Norm}}$ removes the component along the input's own direction. In every other direction it scales by $g/\text{RMS}(r+f(r))$.

12 blocks: 1/4096 vs 1

**Post-norm.** After a Norm, $r$ has RMS 1 (gain $g=1$). Say $f(r)$ also has RMS 1 and is uncorrelated with $r$. Variances add, so $r+f(r)$ has RMS $\sqrt2$. The Norm divides by $\sqrt2$, so the identity path is scaled by $0.71$ per sublayer. With 12 blocks there are 24 sublayers: $(1/\sqrt2)^{24} = 2^{-12} \approx 1/4096$. This hits both embedding information going forward and gradients going back. **Pre-norm.** The factor is exactly 1 at every sublayer. With embeddings of RMS 1 and 24 uncorrelated RMS-1 sublayer outputs, $r$ grows to RMS $\sqrt{1+24}=5$. Yet every sublayer still reads an RMS-1 input.

Both blocks in numpy

Stand-in maps replace attention and the FFN. Compare where `rmsnorm` sits in each version. After 12 blocks, pre-norm's $r$ has grown, because its main path is a plain sum. Post-norm's $r$ is held at RMS ≈ 1 by the Norms on its main path.
import numpy as np
def rmsnorm(x, g): return g * x / np.sqrt((x**2).mean(-1, keepdims=True) + 1e-6)
def pre_block(r, attn, ffn, g1, g2):
    r = r + attn(rmsnorm(r, g1))      # Norm only on the branch input
    return r + ffn(rmsnorm(r, g2))    # main path: plain addition
def post_block(r, attn, ffn, g1, g2):
    r = rmsnorm(r + attn(r), g1)      # Norm sits on the main path
    return rmsnorm(r + ffn(r), g2)
rng = np.random.default_rng(0); T, d = 4, 8
W1, W2 = rng.normal(size=(2, d, d)) / np.sqrt(d)
attn, ffn, g = (lambda x: x @ W1), (lambda x: np.maximum(x @ W2, 0)), np.ones(d)
rms = lambda x: np.sqrt((x**2).mean())
r_pre = r_post = rng.normal(size=(T, d))
for _ in range(12):
    r_pre = pre_block(r_pre, attn, ffn, g, g)
    r_post = post_block(r_post, attn, ffn, g, g)
print(rms(r_pre), rms(r_post))   # pre: grows with depth; post: about 1

What to remember

Pre-norm: $r \leftarrow r + \text{Attn}(\text{Norm}(r))$; $r \leftarrow r + \text{FFN}(\text{Norm}(r))$, plus one final Norm at the end. Normalizing only the branch input keeps the identity path exact, so signal and gradients cross any number of blocks unscaled. Post-norm puts Norm on the main path and multiplies that path by about $g/\text{RMS}$ at every sublayer, which compounds with depth.

On Bytewise this lesson comes in small steps with a check at the end, and the app brings it back right before you would forget it.