Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Language models / Keeping values in range

RMSNorm: keeping a token's scale under control

Root mean square layer normalisation (RMSNorm) controls vector scale. How does it work, and what does it simplify compared with LayerNorm?

Each residual addition keeps a token’s representation and adds another update. After many layers, the coordinates can arrive at quite different scales.

How can the next network read the pattern without having to cope with every change in its overall size?

Turn the whole vector down together

Imagine multiplying every coordinate by two. The proportions between them stay the same; the vector just gets larger.

We could divide all coordinates by one measure of the vector’s size. A larger input would then receive a larger divisor.

Normalise one column vector, then apply learned coordinate scalesToken vectorEarlier context is dimmed; outlined columns are current.Current token computation.SquareEarlier context is dimmed; outlined columns are current.Current token computation.Mean, then √1.46One RMSKeep thewhole vector÷Earlier context is dimmed; outlined columns are current.Current token computation.Rescaled together7,1687,168
One measured size divides every coordinate. Signs and proportions stay together.

The measure used here is the root mean square: square the coordinates, average them, then take the square root.

Squaring lets positive and negative coordinates both contribute to the size. Dividing by that size rescales the vector together, keeping its pattern of signs and proportions.

That is the normalisation step of RMSNorm. It controls the vector’s overall size.

Change the scale, keep the pattern

Make the input larger below. Its colours become stronger, but compare the vector after normalisation.

A larger input, the same normalised pattern

Interactive
Input magnitude
Input vector
↓
1 · NormaliseDivide every coordinate by the same RMS
↓
Normalised vector

The input grows, but its normalised pattern stays almost unchanged.

So far, every coordinate has received the same divisor. What if the model needs to make some coordinates more prominent than others?

Let the model choose the final balance

After normalising, multiply each coordinate by its own learned scale. The model can still emphasise some channels more than others.

The two steps of RMSNorm

Interactive
Input magnitude
Input vector
↓
1 · NormaliseDivide every coordinate by the same RMS
↓
Normalised vector
↓
2 · Apply learned scalesMultiply corresponding coordinates
Learned scales
↓
Output vector

Follow the two operations separately. The first rescales the whole vector. The second changes individual coordinates using learned weights.

Set all scales to 1 to see the second operation leave the normalised vector unchanged.

Some models store each scale as 1 plus a learned weight that starts at zero. The layer then begins as the plain normalisation, and training nudges the scales away from 1.

A small constant in the denominator keeps the calculation defined when the input is zero. The compact formula is:

yi=giximean⁡(x2)+ϵy_i=g_i\frac{x_i}{\sqrt{\operatorname{mean}(x^2)+\epsilon}}

The older LayerNorm does one more thing first: it subtracts the vector’s average, then divides by the spread around it, and adds a learned bias at the end. RMSNorm skips the average and the bias, which makes it cheaper and works about as well.

Read the pattern, keep the stream

Place RMSNorm before a branch, and that branch reads a normalised input to calculate its update.

RMSNorm before attention and feed-forward, beside the residual streamRunning token representationResidual streamRMSNormAttentionUpdateRMSNormFeed-forwardUpdateUpdated token representation
Each branch reads a normalised input. Its update is added to the running residual stream.

Follow the green stream past each branch. RMSNorm changes the input read by the branch; the stream receives the branch’s update at the addition circle.

The norm can also sit on the branch output, changing which values are normalised before the residual addition.

Sources

Sources: RMSNorm paper, LayerNorm paper, Gemma implementation for scales stored as 1 plus a weight, Llama reference implementation, OLMo 2 implementation.

← Browse all subjectsBack to the beginning ↑