A decoder has layers, query heads of dimension () and stores keys and values in 16-bit floats ( bytes). How large is the KV cache for one sequence of tokens with multi-head attention (MHA), grouped-query attention with KV heads (GQA), and multi-query attention (MQA, one KV head)? How many parameters does GQA save in the and projections?
Sequence models and transformers, Inference
Level L3 Intermediate. Length Medium. Nature Mat Mathematical.
What's being asked
Whether you can count the cache entries per token and see how sharing key and value heads reduces memory and parameters.
Key idea
KV cache bytes , so the cache is proportional to the number of KV heads .
Steps
- MHA has . Per token per layer, and together store numbers.
- MHA cache: bytes bytes GiB.
- GQA with : each KV head is shared by query heads. Cache GiB GiB.
- MQA with : cache GiB MiB.
- Parameters: in MHA, and are each , so per layer. In GQA they are , so per layer.
- Saving per layer: . Over layers: , about billion parameters.
- Smaller cache means less memory traffic per generated token (decoding is memory-bound) and a larger batch size at the same GPU memory. The quality loss from GQA is small; MQA loses a little more.
Common mistakes
- Forgetting the factor for keys and values.
- Multiplying by the query heads instead of the KV heads for GQA, and forgetting that the cache is stored for every layer.
- Thinking GQA reduces the FLOPs of the attention scores; it mainly reduces memory and bandwidth.
How long your answer should be
Half a page of arithmetic; 4–5 minutes.
Last checked on 4 December 2026.
Read this
Previous in Sequence models and transformers: Let be attention weights over scores . Derive the Jacobian and the backward rule where . For and , compute , and again for the peaky . What does this say about saturated attention?
Next in Sequence models and transformers: Show that the cross-entropy of a model on text from the true distribution equals , so perplexity can never go below . Why can the perplexities of two models with different tokenisers not be compared directly, and how do you convert to bits per character? For a model with perplexity per token and characters per token, what is the bits per character?