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.
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.
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.
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.
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
- Query-Key Normalization for Transformers, Henry and colleagues, 2020: normalising queries and keys before their product, with a learned scale, so softmax is less prone to saturating.
- Scaling Vision Transformers to 22 Billion Parameters, Dehghani and colleagues, 2023: very large attention scores made training diverge, and normalising queries and keys fixed it.
- Gemma 2 configuration: attention scores capped at 50 and final logits at 30, as cap · tanh(score ÷ cap). Hugging Face’s Gemma 2 release notes explain that soft-capping was incompatible with Flash Attention and SDPA at release.
- Gemma 3 report: “we replace the soft-capping of Gemma 2 with QK-norm.” Qwen3 and Gemma 3 apply RMSNorm to each head’s queries and keys; OLMo 2 normalises the whole query and key projections.
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.