All courses

LayerNorm and RMSNorm

Compute LayerNorm (subtract the per-token mean, divide by the std, apply a learned gain and bias) and RMSNorm (divide by the RMS, apply a gain). Explain how normalizing keeps activation scale stable.

Builds on: Initialization and activation scale, What an LLM computes, end to end

▶ Watch this lesson Start the whole course

What the animation says

Scale drifts

Careful initialization keeps each layer's activations at a steady size, but only at the start. Once training moves the weights, nothing holds that size in place. Say a layer shrinks its input to point nine of its size. Fifty layers in, the signal has almost vanished. Now say a layer grows its input by just one point one times. Stack fifty of those layers. How much bigger is the signal now?

LayerNorm, one token

About one hundred seventeen times: growth compounds. The fix: before a layer reads a token, rescale it using only that token's own numbers. Take one token with four entries: minus one, five, five, seven. LayerNorm first subtracts the mean, which is four. That leaves minus five, one, one, three. Square those and average them: nine. Its square root, three, is the spread. Divide by three. Now the entries have mean zero and spread one. Now feed in the same token times ten. What comes out?

Scale discarded, then set

The output is the same. Times ten makes the mean forty and the spread thirty. Every ratio is unchanged, so whatever scale arrived from earlier layers is thrown away. Shrink or blow up the input by any positive factor, and the next layer still sees entries of about size one. Then a learned gain per feature, starting at one, and a learned bias, starting at zero, set the scale deliberately. A tiny epsilon goes inside the square root, only to avoid dividing by zero.

RMSNorm: skip the mean

RMSNorm, used in most recent LLMs, is simpler: it skips the mean. Same token: minus one, five, five, seven. Square each entry: one, twenty-five, twenty-five, forty-nine. Average those squares: one hundred over four, twenty-five. RMS stands for root mean square. So which number do we divide every entry by?

Output, code, recap

Divide by five: minus point two, one, one, one point four. Times ten? The RMS becomes fifty, and the output stays the same. In numpy, each row is one token. This line takes the mean of the squares along the last axis. The next line divides every entry by its root and applies the gain. LayerNorm centres, divides by the spread, then adds gain and bias. RMSNorm just divides by the RMS and applies gain, so it's cheaper.

Why does a deep model need rescaling?

Careful init (variance $\propto 1/\text{fan\_in}$) keeps activations at a steady size **at the start**. Once the weights move away from their initial values, nothing preserves that. A layer that amplifies its input by 1.1×, stacked 50 times, grows the scale about 117×. One that shrinks it by 0.9× drives signals toward zero. Can we guarantee that every layer receives inputs of a known size, whatever the earlier layers did?

Rescale each token by its own statistics

Before a layer reads a token's vector, look only at that vector's $d$ entries and rescale them to a standard size. **LayerNorm** shifts the entries to mean 0, then divides by their spread, so they have std 1. **RMSNorm** skips the shift and divides by the root-mean-square, so they have RMS 1. Multiplying the input by any $c>0$ leaves the output unchanged (up to the tiny $\epsilon$), so the incoming scale is discarded. A learned per-feature gain then sets the scale on purpose.

The two formulas

For one token's row $x\in\mathbb{R}^d$: $$\mu=\tfrac1d\textstyle\sum_i x_i,\quad s^2=\tfrac1d\sum_i (x_i-\mu)^2$$ $$\text{LN}(x)_i=g_i\,\frac{x_i-\mu}{\sqrt{s^2+\epsilon}}+b_i$$ $$\text{RMS}(x)=\sqrt{\tfrac1d\textstyle\sum_i x_i^2+\epsilon},\quad \text{RMSNorm}(x)_i=g_i\frac{x_i}{\text{RMS}(x)}$$ $g,b\in\mathbb{R}^d$ are the learned gain and bias, starting at 1 and 0. $\epsilon\approx10^{-5}$ prevents division by zero. In $X\in\mathbb{R}^{T\times d}$, each row is normalized on its own.

Worked example, d = 4

$x=[-1,5,5,7]$, $g=1$, $b=0$. **LayerNorm:** $\mu=16/4=4$. Deviations are $[-5,1,1,3]$. $s^2=(25+1+1+9)/4=9$, so $s=3$. Output: $[-\tfrac53,\tfrac13,\tfrac13,1]$, which has mean 0 and std 1. **RMSNorm:** mean square $=(1+25+25+49)/4=25$, so RMS $=5$. Output: $[-0.2,1,1,1.4]$. Now use $10x$: $\mu$, $s$ and RMS all grow 10×, so both outputs stay the same (up to the tiny $\epsilon$).

Both norms in numpy

Each row (token) is normalized over its last axis. The second row is the first ×10, and it gives identical outputs.
import numpy as np
def layernorm(X, g, b, eps=1e-5):
    mu = X.mean(-1, keepdims=True)
    var = ((X - mu)**2).mean(-1, keepdims=True)
    return g * (X - mu) / np.sqrt(var + eps) + b
def rmsnorm(X, g, eps=1e-5):
    rms = np.sqrt((X**2).mean(-1, keepdims=True) + eps)
    return g * X / rms
X = np.array([[-1., 5, 5, 7], [-10., 50, 50, 70]])
g, b = np.ones(4), np.zeros(4)
print(layernorm(X, g, b).round(3))  # rows identical
print(rmsnorm(X, g).round(3))       # rows identical

What to remember

Normalization rescales each token's vector by its own statistics. The next layer then always sees entries of size about 1, no matter how large or small the upstream scale has become, and the learned gain (and bias) set the scale deliberately. **LayerNorm**: subtract the mean, divide by the std, apply gain and bias. **RMSNorm**: divide by the RMS, apply gain. It is cheaper, and most recent LLMs use it.

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.