Lazy loaded image10.5 What is QK-Norm?

Motivation

This paper proposed a way to handle the attention mechanism, especially the part where we use softmax on dot products of queries and keys.
The key issue of the standard dot-product attention is that: the values going into softmax can get super large, which ends up making the attention too sharp or too “winner-take-all” like below:
Where both the 760 and 12 take most “attention”. That can actually hurt learning, especially when we want the model to spread its attention around a bit more. Also, the softmax only cares about the differences between values instead of their magnitudes.

Query-Key Normalization (QK-Norm)

Instead of using raw dot products between queries and keys, they normalize both vectors using L2 norm (so now we're working with cosine similarity).
However, the scale in the softmax still somehow matters since the range of cosine similarity is [-1, 1]. The expressive ability may be insufficient when cosine similarity is combined with Softmax.
Therefore, adding a learnable temperature parameter or something similar is important. The specific operation of the QK Norm is to
  • first normalize and and
  • then multiply them with a learnable scalar weight.
That way, the values going into softmax stay in a more useful range: not too big, not too small.
So normally:
With QK Norm:
where is that learnable scale factor and the denominator can be ignored; where and are and with -normalization applied along their head dimensions.

Difference with usual Normalizatiom

Note that in standard normalization techniques such as batch normalization, layer normalization, etc., we are actually doing standardization like below:
But in this QK Norm, we are doing real normalization, specifically the L2 norm.
The authors also mentioned that this QK Norm can be used with LayerNorm instead of replacing it.
Models like OLMoE, ViT-22B, and dots.llm1 use the QK Norm
Prev
10.4 Left Padding or Right Padding?
Next
10.6 Why LLM training rarely adopts Dropout?
Loading...
Article List
LLM Learning Roadmap
✨ Awesome-Anything
🖼️ Digital Image Processing
🍃 LLM Components
🌱 LLM Pre-training
☘️ LLM Post-Training
🍀 LLM Popular Models
🪴 LLM Applications
🌿 LLM Optimization
🌾 LLM Compression
🌵 LLM Hands-on Practice
🌴 LLM Must-read Papers
🌳 LLM Q&A
🐝 VLM Image Encoders
📝 MISC.