LLM Library
Inside the TransformerAnimated
Sharing Keys and Values: MQA, GQA and MLA
Cut the KV cache by sharing keys and values between heads.
The idea
In multi-query attention all heads share one K/V head. Grouped-query attention uses a few groups. DeepSeek's multi-head latent attention stores a compressed latent and expands it when needed. The goal is a smaller KV cache and faster inference.
Watch it work
Press play, pause, or step through. Some animations have settings you can change.
Grouped-query attention: query heads are split into 2 groups, each sharing one K/V head. A good middle ground used by Llama, Qwen and Mistral.
1/2
Lines connect each query head to the key/value head it reads.
Step by step
- 1Start with full multi-head attention
- 2Let several query heads share a K/V head
- 3Or compress K and V into a small latent
- 4Measure cache size and quality
Where it is used
Llama 3, Qwen, Mistral (GQA); DeepSeek V2 and V3 (MLA).
Watch out for
Too much sharing lowers quality, so the group count is a tuned trade-off.