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
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.
Rescale each token by its own statistics
The two formulas
Worked example, d = 4
Both norms in numpy
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
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