Skip to content

13.1 Pre-LN and RMSNorm

The 2017 transformer you built in Encoder-decoder Transformer: post-LN and pre-LN, Noam, label smoothing puts a LayerNorm after each residual add, and the LayerNorm demo in that section showed what that costs: without a learning-rate warmup, the post-LN model diverged. Every model you will load from here on (SmolLM2, Llama, Mistral, Qwen) makes two changes to that block. The norm moves in front of each sublayer, which is called Pre-LN, and LayerNorm becomes RMSNorm.

Concepts from other sections (10)

This section uses these ideas. Each has a short definition and a link to the section that explains it in full. Tap a dotted term in the text to preview it.

This chapter rebuilds the block piece by piece toward SmolLM2-135M, and that model has 61 RMSNorms. If one of them differs from Hugging Face’s by where an eps sits, SmolLM2’s logits drift past the 1e-3 tolerance against Hugging Face. So this section gets the norm and the residual step exactly right, down to the placement of one constant.

You will need the Module system from Module system and basic layers, the ops F.mean and F.sum from The op library and gradcheck_all, and the Tensor with its ** operator from Tensor, broadcasting backward, and no_grad. Two sections of Mathematics are useful companion reading: Matrix differentials, the trace trick, and closed-form VJPs derives the VJP of a norm by hand, and Stable numerics explains why a tiny denominator needs an eps. The reference solution is python/tinyllm/modern/norm.py, which provides RMSNorm and pre_norm_residual, and the interface it implements is the contract course/contracts/py/tinyllm/modern/norm.pyi.

Learning objectives

  • Compute RMSNorm by hand and explain why it drops LayerNorm’s mean subtraction and bias.
  • Place eps inside the square root and explain why the placement shows on small activations.
  • Reproduce Gemma’s stored-gain convention with an offset and pick the matching initialization.
  • Write the Pre-LN residual step and explain why it gives the gradient an identity path.

The section uses a handful of symbols, collected here for reference.

SymbolMeaningType / shape
x∈Rdx \in \mathbb{R}^done token’s hidden vectorfloat32[..., d]
ddwidth of the vectorint
rms⁡(x)\operatorname{rms}(x)1d∑ixi2+ϵ\sqrt{\frac{1}{d}\sum_i x_i^2 + \epsilon}scalar per vector
ϵ\epsiloneps, a small constant that keeps the root away from 0float
wwthe learned gain, weightfloat32[d]
oooffset: 0 for Llama, 1 for Gemmafloat
yythe output, x/rms⁡(x)⊙(o+w)x / \operatorname{rms}(x) \odot (o + w)float32[..., d]
FFa sublayer (attention or MLP)function

A deep network adds a branch output to its hidden vector at every layer, so the size of that vector drifts with depth. A norm puts every vector back on a common scale before a sublayer reads it. LayerNorm, which you built in Module system and basic layers, subtracts the mean and divides by the standard deviation. Zhang and Sennrich found that the re-centering contributes little: dividing by the root mean square alone, rms⁡(x)=mean⁡(x2)+ϵ\operatorname{rms}(x) = \sqrt{\operatorname{mean}(x^2) + \epsilon}, works as well and is cheaper.

After the division (with ϵ=0\epsilon = 0) the mean of the squares is exactly 1, and scaling xx by any c>0c > 0 changes nothing. The sublayer sees the direction of xx, not its length. Every token is normalized by its own statistic, over the last axis only.

A unit-RMS vector would force every feature to the same scale, so a learned per-feature gain ww restores what the model needs:

y=xrms⁡(x)⊙w.y = \frac{x}{\operatorname{rms}(x)} \odot w.

Llama initializes ww to ones. Gemma stores w−1w - 1 instead (initialized to zeros) and computes xrms⁡(x)⊙(1+w)\frac{x}{\operatorname{rms}(x)} \odot (1 + w); weight decay then pulls the gain toward 1, not toward 0. The contract covers both conventions with a single offset parameter:

y=xrms⁡(x)⊙(o+w).y = \frac{x}{\operatorname{rms}(x)} \odot (o + w).

You do not need to write a backward for RMSNorm. Every step is an op from The op library and gradcheck_all (x⋅xx \cdot x, F.mean, a power of −12-\frac12, two products), so the autograd engine produces the gradient. Matrix differentials, the trace trick, and closed-form VJPs derives it by hand, and the result has a clean reading: the gradient with respect to xx removes the component of the upstream gradient along xx (moving along xx only rescales it, which the norm undoes) and divides by the RMS. The gain receives xrms⁡(x)⊙g\frac{x}{\operatorname{rms}(x)} \odot g summed over tokens.

With the norm defined, the remaining question is where it goes. Post-LN (2017) computes norm⁡(x+F(x))\operatorname{norm}(x + F(x)): the residual stream itself is normalized after every block, so the gradient that reaches early layers passes through every norm on the way. Pre-LN computes

x+F(norm⁡(x)).x + F(\operatorname{norm}(x)).

The stream xx is never normalized; only the branch input is. The derivative of the step with respect to xx is I+∂F(norm⁡(x))∂xI + \frac{\partial F(\operatorname{norm}(x))}{\partial x}, so the gradient always has a clean identity path from the loss to the embedding. Xiong et al. show this is why Pre-LN trains at full learning rate without warmup. The price is that the stream grows with depth, so a Pre-LN model applies one more RMSNorm after the last block, before the lm_head, as you will see in The Llama-Family Model: Config, Weight Loading, and Download.

Take d=4d = 4, ϵ=0\epsilon = 0, and x=(3,4,0,0)x = (3, 4, 0, 0).

  1. The squares are 9,16,0,09, 16, 0, 0, with mean 25/4=6.2525 / 4 = 6.25.
  2. So rms⁡(x)=6.25=2.5\operatorname{rms}(x) = \sqrt{6.25} = 2.5.
  3. Dividing gives x/rms⁡(x)=(1.2,1.6,0,0)x / \operatorname{rms}(x) = (1.2, 1.6, 0, 0). As a check, the mean of the squares is (1.44+2.56)/4=1(1.44 + 2.56) / 4 = 1.
  4. With gain w=(1,0.5,2,2)w = (1, 0.5, 2, 2), the output is y=(1.2,0.8,0,0)y = (1.2, 0.8, 0, 0).

LayerNorm on the same xx would first subtract the mean 1.75 and give a different vector.

The contract has one class and one function:

class RMSNorm(Module):
def __init__(self, d: int, eps: float = 1e-6, offset: float = 0.0) -> None: ... # weight [d]
def forward(self, x: Tensor) -> Tensor: ... # [..., d] -> [..., d]
def pre_norm_residual(x: Tensor, norm: Module, sublayer) -> Tensor: ... # x + sublayer(norm(x))

RMSNorm owns one parameter named weight, float32, initialized to ones or zeros depending on the offset; that name is the safetensors key of every norm in a Hugging Face checkpoint. forward normalizes over the last axis and leaves every other axis alone. pre_norm_residual has no parameters of its own; it wires a norm and a sublayer into the residual step.

Start from what the sublayer needs. It should see a vector on a fixed scale whatever the depth, and dividing by a per-vector size statistic does that; the root mean square is the cheapest statistic that makes the output scale-invariant. The statistic must be over the last axis only, one per token, so tokens never influence each other’s normalization.

A zero vector has RMS 0, so the denominator needs an ϵ\epsilon. Placing it inside the root, mean⁡(x2)+ϵ\sqrt{\operatorname{mean}(x^2) + \epsilon}, is the convention every Llama-family checkpoint was trained with; placing it outside changes the output on small activations.

A learned per-feature gain restores scale where the model needs it. Storing ww or w−1w - 1 is a checkpoint convention, so offset reproduces both, and the initialization follows from it: ones for Llama, zeros for Gemma, so that a fresh norm is the plain normalization in both cases.

The backward needs no hand derivation as long as the forward is composed from differentiable ops (x * x, F.mean, ** -0.5, products). The one trap is that the gain must be used as a Tensor so it receives its gradient. Pre-LN is then one line: normalize only the branch input and add the branch to the untouched stream.

Here is the class, annotated:

class RMSNorm(Module):
def __init__(self, d: int, eps: float = 1e-6, offset: float = 0.0) -> None:
super().__init__()
# ... d a positive integer (bools refused); eps >= 0 ...
self.d = int(d)
self.eps = float(eps)
self.offset = float(offset)
# Llama starts the gain at 1; Gemma stores gain - 1 and starts it at 0.
init = np.ones(self.d) if self.offset == 0.0 else np.zeros(self.d)
# Named `weight`: the safetensors key HF uses for every norm.
self.weight = Tensor(init.astype(np.float32), requires_grad=True)
def forward(self, x: Tensor) -> Tensor:
if x.shape[-1] != self.d:
raise ValueError(f"RMSNorm({self.d}) got last axis {x.shape[-1]}")
# eps goes inside the root: sqrt(mean(x^2) + eps), as Llama and T5.
ms = F.mean(x * x, axis=-1, keepdims=True) # one statistic per token; keepdims to broadcast
y = x * (ms + self.eps) ** -0.5 # multiply by rsqrt rather than divide by sqrt
# offset + w as a Tensor op, so the gradient still reaches self.weight.
gain = self.weight + self.offset if self.offset != 0.0 else self.weight
return y * gain

Trace the worked example through it: x * x is (9,16,0,0)(9, 16, 0, 0), ms is 6.25, (ms + 0) ** -0.5 is 0.4, y is (1.2,1.6,0,0)(1.2, 1.6, 0, 0), and the gain (1,0.5,2,2)(1, 0.5, 2, 2) gives (1.2,0.8,0,0)(1.2, 0.8, 0, 0). Notice that the code uses the mean, not the sum: the sum would give 25=5\sqrt{25} = 5 and outputs d=2\sqrt d = 2 times too small.

The residual step is a single expression:

def pre_norm_residual(x, norm, sublayer):
# The residual stream x is never normalized: only the branch sees norm(x).
return x + sublayer(norm(x))

With a branch that outputs zeros, this returns xx exactly and the gradient with respect to xx is exactly 1: the identity path from the Pre-LN discussion above. The full solution is in norm.py.

The cost is O(d)O(d) per token: one square, one mean, one rsqrt, two multiplies. LayerNorm needs a second pass for the mean and a bias; RMSNorm drops both, which matters in a fused kernel where memory traffic dominates.

Composing the forward from autograd ops rather than writing a VJP by hand means the correctness of the backward is inherited from the op library, at the cost of a few temporary arrays. The hand derivation in Mathematics produces the same gradient for the C and Rust ports. One class with offset, rather than separate Llama and Gemma norms, works because the two formulas differ only by a constant added to the gain. And Pre-LN is a function, not a module, because it has no parameters; the norm and the sublayer own theirs.

The tradeoff you accept is that Pre-LN lets the stream grow with depth, so models need a final norm before the head.

A correct solution has to survive a specific set of checks. It must reproduce the hand-worked vector with and without the gain, so everyone agrees on the formula. Its output and gradients must match transformers’ LlamaRMSNorm, which is what SmolLM2’s norms depend on, and with offset = 1 it must match GemmaRMSNorm for Gemma-style checkpoints. Inputs of size 1e-3, where eps dominates, test that eps is inside the root, which matters for the tiny activations of deep layers. Float64 central differences check the gradients of xx and the gain at both offsets, since everything before the norm learns through it. Property tests check that the output has unit RMS and is scale-invariant, so the sublayer sees directions, and that each vector is normalized on its own, with per-token statistics over the last axis and no leakage across positions. A constant vector must stay constant, because RMSNorm is not LayerNorm. There must be exactly one parameter named weight, float32, initialized to ones or zeros. For the residual step, a zero branch must give exactly xx with gradient 1 (deep stacks depend on it), and a bounded branch must be added as x+F(norm⁡(x))x + F(\operatorname{norm}(x)), as the Llama block requires. Finally, bad arguments (a wrong width, zero width, negative eps) must fail loudly so wiring bugs surface early. A model test suite whose oracle is the formula in numpy float64, x / sqrt(mean(x**2, -1) + eps) * (offset + w), is in test_norm.py.

Warning: The most common bug is eps outside the root, x/(mean⁡(x2)+ϵ)x / (\sqrt{\operatorname{mean}(x^2)} + \epsilon). It passes every test on ordinary inputs and then makes small activations come out several percent off. Write (ms + self.eps) ** -0.5.

The other mistakes each have a recognizable symptom, summarized here:

PitfallSymptomHow the solution avoids it
eps outside the rootsmall activations come out several percent off(ms + self.eps) ** -0.5
subtracting the mean (LayerNorm statistics)a constant vector becomes zerosno mean is computed; only mean(x * x)
ignoring Gemma’s offseta fresh Gemma norm outputs zerosgain = self.weight + self.offset when offset != 0
post-LN inside pre_norm_residualthe stream is normalized; the identity path is gonereturns x + sublayer(norm(x)), never norm(x + ...)
statistic over the wrong axistokens are normalized by each otheraxis=-1, keepdims=True
gain initialized the wrong waya fresh Llama norm outputs zerosones when offset == 0, zeros otherwise
sum of squares instead of the meanoutputs d\sqrt{d} times too smallF.mean, not F.sum
normalizing the stream, then adding the branchthe identity path carries norm⁡(x)\operatorname{norm}(x)the stream term is the raw x
the gain as a constant (.data)the gain never trainsthe gain is the parameter Tensor itself

The norm you just wrote appears all over the rest of the book. In Multi-head latent attention, q_a_layernorm and kv_a_layernorm normalize MLA’s query and key-value latents. In The Llama-Family Model: Config, Weight Loading, and Download, it becomes the input_layernorm, post_attention_layernorm, and final norm of every Llama model, where SmolLM2-135M’s logits must match Hugging Face. Later, the C kernel tl_rmsnorm_f32 in Elementwise kernels in C must match this implementation to float32 tolerance, and the Rust forward in Candle model runner calls that C norm.

  • Zhang and Sennrich, “Root Mean Square Layer Normalization” (2019).
  • Xiong et al., “On Layer Normalization in the Transformer Architecture” (2020), sections 3 and 4.
  • Hugging Face LlamaRMSNorm in transformers/models/llama/modeling_llama.py: upcasts bf16 input to float32 for the statistic and casts back before the gain.
  • Fused RMSNorm kernels (Apex, Liger, vLLM): one pass over memory, fused with the residual add. See vLLM csrc/layernorm_kernels.cu (fused_add_rms_norm).
  • DeepNorm, sandwich norm, and QK-norm: scaled residuals for 1000-layer models and extra norms on queries and keys (OLMo 2, Qwen3). See Wang et al. 2022, “DeepNet”, and the OLMo 2 report.
  • RMSNorm: divides a vector by its root mean square plus eps (inside the root) and multiplies by a learned gain; no mean subtraction, no bias.
  • Gain offset: a constant added to the stored gain; 1 reproduces Gemma’s convention of storing w−1w - 1.
  • Pre-LN: the residual step x+F(norm⁡(x))x + F(\operatorname{norm}(x)), which normalizes only the branch input.
  • Post-LN: the 2017 step norm⁡(x+F(x))\operatorname{norm}(x + F(x)), which normalizes the residual stream itself.
  • Identity path: the II term in the Pre-LN Jacobian that carries the gradient straight from the loss to the embedding.
  • RMSNorm divides each vector by its root mean square and multiplies by a learned gain; it subtracts no mean and has no bias.
  • The eps goes inside the square root, and the difference shows on small activations.
  • Gemma stores the gain minus one: offset = 1 with a zeros init reproduces it.
  • Pre-LN normalizes only the branch input, so the residual stream is an identity path in value and in gradient; the cost is a final norm before the head.
  • Composing the forward from autograd ops gives the backward for free, as long as the gain is used as a Tensor.
  1. Why must ϵ\epsilon be inside the square root to match Hugging Face?
    AnswerThe checkpoints were trained with $x / \sqrt{\operatorname{mean}(x^2) + \epsilon}$. For activations whose mean square is comparable to $\epsilon$ (around $10^{-6}$), $\sqrt{m + \epsilon}$ and $\sqrt m + \epsilon$ differ by a large relative amount, so outputs drift by percents and the logits fail a 1e-3 parity check.
  2. Why does a Gemma norm start its weight at zeros while a Llama norm starts at ones?
    AnswerGemma computes the gain as $1 + w$. A zero init makes the gain 1, the same starting function as Llama's $w = 1$. Initializing Gemma's weight to ones would double every output.
  3. Why is the gain written as self.weight + self.offset and not built from self.weight.data?
    AnswerUsing `.data` would produce a constant array with no link to the parameter, so the gain would never receive a gradient and never train. Adding a Python float to the Tensor is an op that passes the gradient through unchanged.
  4. What does a constant vector (c,c,c,c)(c, c, c, c) become, and why does that distinguish RMSNorm from LayerNorm?
    AnswerRMSNorm gives $(1, 1, 1, 1) \odot w$ (for $c > 0$, with $\epsilon$ small): its RMS is $|c|$. LayerNorm subtracts the mean first and gives zeros. RMSNorm keeps the direction, including any constant component.
  5. Why does Pre-LN train without warmup while Post-LN does not?
    AnswerIn Pre-LN the step's Jacobian is $I + \partial F(\operatorname{norm}(x))/\partial x$, so the gradient reaches every layer through an identity path. In Post-LN it passes through a norm at every layer, which at initialization makes the last layers' gradients large and early updates unstable.
  6. What goes wrong if you use F.sum instead of F.mean for the statistic?
    AnswerThe statistic becomes $\sum_i x_i^2$ instead of its mean, so the output is $\sqrt d$ times too small. On the worked example the denominator would be $\sqrt{25} = 5$ instead of 2.5, halving every output.