Rúben GalvãoThe Rabbit Hole

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

QK normalisation (QK-Norm) and soft-capping

Query-key normalisation (QKNorm) and logit soft-capping control attention scores. What happens when one score overwhelms the others?

In “out of the burrow the rabbit hopped”, the token “hopped” can attend to every token in the sentence. Its attention is spread: some on “rabbit”, some on “burrow”, a little everywhere.

Further into training, the same sentence gives a very different picture. Nearly all of that attention lands on one token. Did the model become sure, or did something else change?

One score runs away

A score is the dot product of a query and a key. Make both vectors four times larger, keeping their directions, and every score becomes sixteen times larger.

Larger queries and keys make one score tower over the restThe query for "hopped" compared with seven keys. With smaller vectors, its attention is spread, with at most 23% on one token. With queries and keys four times larger, every score is sixteen times larger and "hopped" takes 94% of the attention.Smaller queries and keys“hopped” asks; bars are its attention14%out0.49%of-0.110%the0.016%burrow0.510%the0.019%rabbit0.723%hopped0.9scoreQueries and keys 4× larger“hopped” asks; bars are its attention0%out6.60%of-1.50%the0.01%burrow8.80%the0.05%rabbit11.094%hopped13.9score
The same tokens and the same directions. Only the vectors' size changed, and the scores grew sixteen times.

Softmax turns large gaps between scores into near-certainty. The tokens did not change, only the vectors’ size. The model has stopped weighing its options, and a choice this one-sided gives training little to learn from.

Large models can drift this way while they train. How do we keep the scores in range?

Compare directions, not sizes

The scores grew because the vectors grew. We have met this problem before: RMSNorm divides a vector by its own size.

So apply it to every query and every key just before they are compared. This is QK-norm.

Query for “hopped”4× largerEarlier context is dimmed; outlined columns are current.Current token computation.RMSNormEarlier context is dimmed; outlined columns are current.Current token computation.192192Key for “rabbit”4× largerEarlier context is dimmed; outlined columns are current.Current token computation.RMSNormEarlier context is dimmed; outlined columns are current.Current token computation.192192·Score from normalised directions
RMSNorm divides each query and key by its own size first, so however large they grow, the dot product compares only their directions.

Now, making the vectors larger without changing their directions leaves the scores unchanged. A learned scale lets the model decide how sharp its attention should be.

Or bend the score itself

We could leave the vectors alone and limit the score instead. Clipping at a fixed value would work, but it stops gradients dead past the limit.

A smoother way: divide the score by a cap, squash it with tanh, and multiply by the cap again. This is soft-capping.

Soft-capping bends large scores towards a fixed limitA plot of cap times tanh of score over cap, with a cap of 4. Near zero it follows the dashed line where the output equals the input; further out it flattens towards plus or minus 4.-404-12-8-404812score inscore outcap 4dashed: unchanged
Small scores pass almost unchanged; large ones bend towards the cap of 4 and can never cross it.

Normal scores pass almost untouched, and no score can run past the cap.

Grow the vectors and compare

Make the queries and keys larger, then try each fix.

Grow the queries and keys

Interactive
No fix“hopped” asks; bars are its attention0%out6.60%of-1.50%the0.01%burrow8.80%the0.05%rabbit11.094%hopped13.9score

Without a fix, one token soon takes almost everything. With QK-norm, growing the vectors alone no longer changes the attention weights. Soft-capping holds the scores under the cap, though once the largest all reach it, they start to look alike. That is why real caps sit far above ordinary scores: Gemma 2 caps attention scores at 50, and its final token scores at 30.

The cap is an extra step inside attention, which the fastest attention code did not support when Gemma 2 was released. Gemma 3 replaced it with QK-norm.

Even with scores in range, softmax must put all of its attention somewhere. Next, see where it goes when nothing fits: attention sinks.

Sources and assumptions

The figures and the widget use one query and seven keys of 4 numbers each, chosen by hand: each key has the same size and its own angle to the query, so that attention starts spread out. Scores are dot products divided by √4, as in attention. QK-norm applies RMSNorm to the query and each key and multiplies by a fixed scale of 0.46, standing in for the learned one. The cap is 4 here so that its bend shows at these small scores. All numbers are computed from those vectors.

← Browse all subjectsBack to the beginning ↑