Linear algebra for LLMsPart 6: The rest of the transformer

LayerNorm and RMSNorm

7 min

LayerNorm

LN⁡(x)=x−μσ2+ϵ⊙γ+β\displaystyle \operatorname{LN}(x) = \frac{x - \mu}{\sqrt{\sigma^2 + \epsilon}} \odot \gamma + \beta

μ\mu and σ2\sigma^2 are the mean and variance of one token’s dd features (Part 4: over dim=-1). ϵ\epsilon is a tiny constant against dividing by zero. γ\gamma and β\beta are learned, one per feature.

Write

LN⁡(x)=x−μσ2+ϵ⊙γ+β\operatorname{LN}(x) = \frac{x - \mu}{\sqrt{\sigma^2 + \epsilon}} \odot \gamma + \beta, per token over dd.

Step 1: center

−μ=5\mu = 5=

The mean of [2,4,6,8][2, 4, 6, 8] is 5. Subtracting it leaves values that sum to 0.

Step 2: divide by the standard deviation

σ2=9+1+1+94=5,σ=5≈2.236\displaystyle \sigma^2 = \tfrac{9 + 1 + 1 + 9}{4} = 5, \quad \sigma = \sqrt 5 \approx 2.236
÷ 2.2362.236 =

Now the features have mean 0 and variance 1 (ignoring ϵ\epsilon). Then γ\gamma and β\beta rescale and shift each feature, so the model can undo any of it that it doesn’t want.

Write

[2,4,6,8]→[−1.342,−0.447,0.447,1.342][2, 4, 6, 8] \to [-1.342, -0.447, 0.447, 1.342] (before γ,β\gamma, \beta).

What it does geometrically

x−μ1  =  project out the direction 1/d\displaystyle x - \mu\mathbf{1} \;=\; \text{project out the direction } \mathbf{1}/\sqrt{d}
mean 0, variance 1  ⇒  ∣x^∣=d\displaystyle \text{mean } 0,\ \text{variance } 1 \;\Rightarrow\; |\hat{x}| = \sqrt{d}

Centering removes the all-ones direction (Part 3’s projections). Dividing by σ\sigma then puts every token on a sphere of radius d\sqrt d: here 4=2\sqrt 4 = 2, and indeed 2(1.3422)+2(0.4472)≈42(1.342^2) + 2(0.447^2) \approx 4. Then γ\gamma stretches each axis (a diagonal matrix, Part 2).

Write

LayerNorm: remove the mean direction, scale onto the sphere ∣x^∣=d|\hat x| = \sqrt d, then ⊙ γ\odot\,\gamma.

RMSNorm skips the centering

RMSNorm⁡(x)=x1d∑ixi2+ϵ⊙γ\displaystyle \operatorname{RMSNorm}(x) = \frac{x}{\sqrt{\tfrac{1}{d}\sum_i x_i^2 + \epsilon}} \odot \gamma
4+16+36+644=30≈5.477\displaystyle \sqrt{\tfrac{4 + 16 + 36 + 64}{4}} = \sqrt{30} \approx 5.477
÷ 5.4775.477 =

No mean, no β\beta: just divide by the root mean square and apply the gain. It’s cheaper and works about as well (Zhang & Sennrich, 2019). Llama uses RMSNorm.

Write

RMSNorm⁡(x)=x/rms⁡(x)⊙γ\operatorname{RMSNorm}(x) = x / \operatorname{rms}(x) \odot \gamma. No centering, no bias.

✏️ Your turn

Normalize this, both ways

Take γ=1\gamma = 1, β=0\beta = 0 and ignore ϵ\epsilon. Write LN⁡(x)\operatorname{LN}(x) and RMSNorm⁡(x)\operatorname{RMSNorm}(x).

Work it out on paper, then go to the next card to check.

Answer: the same, this time

The mean is already 0, so centering does nothing; σ=rms⁡=2\sigma = \operatorname{rms} = 2. LayerNorm and RMSNorm only differ when the mean isn’t 0.

In PyTorch

nn.LayerNorm(d)      # (x - mean) / sqrt(var + eps) * weight + bias
nn.RMSNorm(d)        # x / sqrt(mean(x**2) + eps) * weight

Both normalize over the last axis: one token at a time, never across the batch.

📌 Recap

LayerNorm and RMSNorm in one card

  • LayerNorm: subtract the mean, divide by the standard deviation, then ⊙ γ+β\odot\,\gamma + \beta.
  • RMSNorm: divide by the root mean square, then ⊙ γ\odot\,\gamma.
  • Per token, over its dd features.
  • Geometry: remove the mean direction, land on a sphere of radius d\sqrt d, rescale each axis.

Write

The four lines above.