Multi-head Attention vs Grouped-Query Attention vs Multi-query Attention
As of 2026-11-24
Multi-head Attention vs Grouped-Query Attention vs Multi-query Attention: what is the difference?
Switching from MHA to GQA-8 at equal model size cuts KV-cache memory by the Q:KV ratio (8x for 64 Q / 8 KV) at a small quality cost to measure on your task, raising sustainable long-context concurrency up to ~8× when KV memory is the binding constraint — the real reason Mistral and Llama 3 serve cheaper than equivalent MHA models.
What it is
Multi-head Attention (MHA), Grouped-Query Attention (GQA) and Multi-Query Attention (MQA) are three points on the same trade-off curve: how many distinct Key/Value projections does each layer compute, given a fixed number of Query heads. The choice trades some representational diversity in the K/V projections for memory: it changes how much KV-cache memory inference burns per token, and therefore the per-request cost and the maximum concurrency a given GPU can serve.
Why it exists. The original Transformer (2017) used MHA — every Query head has its own Key and Value head. That maximises representational diversity but also multiplies KV-cache memory by n_q/n_kv versus GQA (8× for a 64-head model grouped into 8), which is the dominant memory cost at long context (companion “KV Cache”). MQA (Shazeer 2019) collapses all heads to a single shared K/V — minimal KV memory, with a quality cost that varies by study: "only minor" in Shazeer, neutral in PaLM, possible degradation and training instability in Ainslie et al. GQA (Ainslie et al. 2023) is the practical compromise adopted by many open-weight models since Llama 2 70B; it is not universal: Llama 2 7B and 13B kept MHA, and DeepSeek-V2 uses Multi-Head Latent Attention instead.
Why it matters
- MHA (Llama 1 65B: 64 Q, 64 KV) maximises representational diversity but multiplies KV-cache memory by n_q/n_kv versus GQA (8× against GQA-8); MQA collapses to one shared K/V head with a quality cost to measure on your own task.
- GQA (Llama 3 70B: 64 Q, 8 KV) is the practical compromise adopted by many open-weight models since Llama 2 70B; it is not universal (Llama 2 7B and 13B kept MHA, DeepSeek-V2 uses MLA).
- If you self-host or fine-tune via SAP AI Core, this single architectural choice controls sustainable concurrency per GPU — invisible to a hosted-API consumer, but essential to know why some models cost less to serve.
Key points
- Three points on one curve — MHA (n_kv = n_q), GQA (n_kv = n_q / g), MQA (n_kv = 1) — trading KV memory for quality.
- GQA is the default for Llama 3, Mistral 7B, Mixtral 8x7B and Gemma 2, but the Q:KV ratio varies: 8:1 for Llama 3 70B (64 Q / 8 KV), 4:1 for Llama 3 8B, Mistral 7B and Mixtral 8x7B (32 / 8), 2:1 for Gemma 2. The KV memory reduction vs MHA equals that ratio; ~8× holds only for 64 Q / 8 KV.
- MQA (n_kv = 1) maximises memory savings but can degrade quality (Ainslie 2023; PaLM reports a neutral effect): measure it on your own tasks.
- KV-cache memory at inference = 2 · n_layers · n_kv · d_head · seq_len · batch · bytes — n_kv is the dominant lever.
- Quality preservation requires GQA training from scratch or careful 'uptraining' from an MHA checkpoint (Ainslie 2023).
- Closed frontier models do not disclose their head configuration, so no GQA default can be asserted for them; Claude's configuration is not publicly confirmed.
- Multi-Head Latent Attention (MLA), introduced in DeepSeek-V2 (2024), compresses K/V into a shared low-rank latent vector, pushing KV-cache reduction further than GQA-8 at comparable quality — the frontier of this same curve.
- On SAP AI Core, the MHA/GQA/MQA choice is invisible for every model served as a managed generative AI hub endpoint; it becomes a real consulting decision only when self-hosting or fine-tuning an open-weight checkpoint.
Terms used on this page
- n_q / n_kv
- The number of Query heads (n_q) and Key/Value heads (n_kv) in a Transformer layer. MHA has n_kv = n_q; GQA has n_kv < n_q; MQA has n_kv = 1.
- KV cache
- The per-request memory store of computed Key and Value tensors for all past tokens, reused on each generation step to avoid recomputation. Dominates GPU memory at long context.
- Uptraining
- The procedure (Ainslie et al. 2023) for converting an existing MHA checkpoint into a GQA model with a small amount of extra training, preserving most original quality at a fraction of from-scratch cost.
- Concurrency
- The number of simultaneous user sessions a fixed GPU pool can serve at target latency. Inversely proportional to KV-cache memory per session — the key business KPI GQA improves.
- Attention head ratio (Q:KV)
- The ratio n_q / n_kv gives the KV-memory saving versus MHA for the same model; absolute KV memory also depends on n_layers, d_head, context length and precision (Llama 3 8B and 70B both have n_kv = 8 but very different KV memory per token). Worth asking for n_layers, n_kv and d_head in any open-weight model card or self-hosting RFP.
- AI Units (SAP)
- SAP's commercial unit for Premium AI features. Generative AI hub calls in SAP AI Core are metered in GenAI tokens converted into capacity units; either way, pricing abstracts the underlying model's n_kv configuration away from the customer's bill, which is why a self-hosted deployment's cost behaviour can surprise a team used to hosted-endpoint pricing.
- Multi-Head Latent Attention (MLA)
- A further compression beyond GQA, introduced in DeepSeek-V2 (2024): keys and values are projected into a shared low-rank latent vector before caching, cutting KV-cache memory even further than GQA-8 at comparable quality — the current frontier of the memory-vs-quality curve this page describes.
Sources
- Ainslie et al. — GQA: Training Generalized Multi-Query Transformer Models (2023)
- Meta AI — Llama 3 Model Card and Architecture Notes
- Jiang et al. — Mistral 7B (2023), arXiv 2310.06825
- SAP Generative AI — official product page
- arXiv — Fast Transformer Decoding: One Write-Head is All You Need (Shazeer, 2019 — origin of MQA)
- Mistral AI — Announcing Mistral 7B (2023): confirms GQA + sliding window attention
- SAP Help Portal — Generative AI hub in SAP AI Core (hosted model endpoints)
- SAP Help Portal — Metering and pricing for generative AI on SAP AI Core (GenAI tokens, capacity units)
- Databricks — Model Serving documentation (self-hosting open-weight checkpoints)
- arXiv — FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness (Dao et al., 2022)
- SAP — AI Units pricing page (Premium AI consumption model)
- arXiv — DeepSeek-V2: introduces Multi-Head Latent Attention (MLA) (2024)
Full card with the Consultant plan — €19.90/month. What the full card adds: the full decision framework · the SAP vs Snowflake / Databricks / Fabric comparison · the common pitfalls and their fix · the cheat sheet · the architecture schemas · the code blocks · the facts worth quoting.