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
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.
Keep the main road clean
Pre-norm vs post-norm, precisely
12 blocks: 1/4096 vs 1
Both blocks in numpy
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
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.
Bytewise
All courses