Grouped-query and latent attention
Cutting the cache without cutting quality
Chapter 11 of 21 · III. Fast attention and inference · about 35 minutes. Builds on: 8. Position and rotary embeddings, 10. Generating text: the KV cache and inference cost.
The cache is the bottleneck
Section titled “The cache is the bottleneck”Chapter 10 showed that the cache size is 2 × layers × KV heads × head size per token. Standard multi-head attention (MHA) has as many key/value heads as query heads. Reducing that number cuts memory and the bytes streamed each decode step.
Multi-query and grouped-query attention
Section titled “Multi-query and grouped-query attention”Multi-query attention (MQA) keeps all query heads but shares a single key head and a single value head across them. The cache shrinks by the number of heads, with some quality loss. Grouped-query attention (GQA) interpolates: partition the query heads into groups, and give each group one key/value head. is MHA, is MQA. The paper also shows an existing MHA model can be converted by averaging the heads in each group and then training briefly.
GQA is the default in current dense models: Llama 3 uses 8 key/value heads at all three sizes , and gpt-oss-120b has 64 query heads sharing 8 key/value heads .
Multi-head latent attention (MLA)
Section titled “Multi-head latent attention (MLA)”DeepSeek-V2 took a different route: keep distinct keys and values for every head, but compress them. Each token’s hidden state is projected down to a small latent vector , and keys and values are reconstructed from it by up-projection matrices:
Here has numbers and is what gets cached; and are the content keys and values for all heads, computed from ; queries are compressed too.
In DeepSeek-V3, has 7,168 numbers, has , there are 128 heads with 128-dimensional content keys and values, and queries are compressed to 1,536 . Per token and layer, MLA caches 512 + 64 numbers (the 64 are the rotary key below), against for a plain multi-head design with the same head sizes. The DeepSeek-V2 paper tabulates cache elements per token and layer as: MHA , GQA , MQA , and MLA , and reports MLA performing better than MHA in its ablations.
The absorption trick
Section titled “The absorption trick”Rebuilding every cached key from its latent at every step would waste the saving. But the content part of a score is
so can be folded into the query: compute once for the new token, and take dot products directly against the cached latents. The same works on the value side: , and can then be merged into the output matrix . Full-size keys and values never have to be materialized. Verify numerically, then watch what happens when position information enters:
Why a separate rotary key
Section titled “Why a separate rotary key”RoPE (Chapter 8) rotates the key by an angle that depends on its position . If it is applied to the expanded key, the score becomes . The position-dependent matrix now sits between and , so can’t be folded into a fixed query; each cached token would need its own folded query, and the savings disappear (the second table in the widget shows the different folded queries). DeepSeek’s solution is decoupled RoPE: add a small extra query and key, = RoPE() per head and = RoPE() shared by all heads, of dimension 64 in V3. The final per-head query and key are the concatenations [; ] and [; ], so a score is the sum of a position-free content term (absorbable) and a position-aware term (cheap, small). Scores are scaled by . Only and are cached.
Comparison
Section titled “Comparison”| Design | Cache per token per layer | Used by |
|---|---|---|
| Multi-head | 2 · heads · head dim | original transformer |
| Multi-query | 2 · head dim | fast-decoding models |
| Grouped-query | 2 · groups · head dim | Llama 3, gpt-oss |
| Latent (MLA) | (512 + 64 in V3) | DeepSeek-V2, V3, V3.2; Kimi Linear’s full-attention layers |
For the numbers in context, open the KV-cache calculator in Chapter 10 and switch between the three presets: 128 KiB per token for Llama 3 8B, about 70 KB per token for DeepSeek-V3.
Check yourself
Section titled “Check yourself”For 32 query heads and 8 KV heads, by what factor does GQA shrink the cache versus MHA?
4×.
What would break if RoPE were applied directly to the expanded keys in MLA?
The rotation matrix, which depends on position, would sit between the query and , preventing from being absorbed into a single fixed folded query; keys would have to be re-expanded for every cached token at every step.
Which matrices does the MLA absorption trick fold together?
into the query side () and into the output projection .
Papers
Section titled “Papers”- Multi-query attention.
- Grouped-query attention.
- MLA: section 2.1 and its appendix on absorption.
- The V3 dimensions used above.