Attention Variants
From Multi-Head Attention to GQA, MLA, linear attention, and state-space models — how attention architectures evolve to meet infrastructure constraints.
The original multi-head attention (MHA) works well but is expensive — especially at inference time where KV cache memory scales with head count. This chapter traces the evolution of attention variants, each designed to solve a specific infrastructure bottleneck.
1. The KV Cache Problem
During autoregressive generation, the model stores Key and Value tensors for all previous tokens (the KV cache) to avoid recomputation. Per token per layer:
For LLaMA-2-7B with MHA (, FP16):
- Per token per layer: KB
- Per token (32 layers): KB = 512 KB per token
- For 4K context: KB = 2 GB of KV cache alone
At 128K context, that’s 64 GB — most of an H100’s memory consumed by KV cache for a single request. Every attention variant below exists to reduce this cost.
2. Multi-Query Attention (MQA)
Idea: All Q heads share a single K and V head.
KV cache reduction: smaller (e.g., 32× for 32 heads).
Tradeoff: Quality degrades because all Q heads share a single K/V representation. Different heads can no longer learn independent attention patterns (e.g., one for syntax, another for semantics). Used by PaLM, Falcon, but largely superseded by GQA.
3. Grouped-Query Attention (GQA)
Idea: A middle ground — group Q heads and have each group share one K/V head.
Each group of Q heads shares one K/V head.
| Config | Q heads | KV heads | Group size | KV cache reduction |
|---|---|---|---|---|
| MHA | 64 | 64 | 1 | 1× (baseline) |
| GQA-8 | 64 | 8 | 8 | 8× |
| MQA | 64 | 1 | 64 | 64× |
Used by: LLaMA-2-70B (8 KV heads), Mistral-7B (8 KV heads), LLaMA-3 (8 KV heads).
Quality vs MHA: Negligible difference in practice — the KV cache savings far outweigh any quality loss.
4. Multi-Latent Attention (MLA)
Idea (DeepSeek-V2/V3): Instead of storing full K and V, compress them into a low-dimensional latent vector and reconstruct on the fly.
Only the compressed (dimension ) is stored in KV cache.
KV cache reduction: With vs (GQA-8), MLA achieves ~2× further reduction on top of GQA.
Key insight for infrastructure: The reconstruction adds compute (extra matrix multiplies during attention), but eliminates memory — trading compute for memory. At long contexts where KV cache is the bottleneck, this is a winning trade.
5. Linear Attention
Standard attention is in both compute and memory (due to the attention matrix). Linear attention avoids materializing this matrix:
By changing the order of operations (compute first, which is , then multiply by ), complexity drops from to .
Tradeoff: Linear attention approximates softmax attention — quality is generally lower, especially for tasks requiring precise token-to-token attention patterns. No production LLM uses pure linear attention, but hybrid architectures are emerging.
6. State-Space Models (Mamba)
Idea: Replace attention entirely with a recurrence that processes tokens sequentially with a fixed-size state:
Where are discretized state transition matrices and is a fixed-size hidden state.
Advantages:
- compute and memory (linear in sequence length)
- No KV cache — fixed state size regardless of context length
- Efficient parallel training via the “parallel scan” algorithm
Disadvantages:
- Cannot do exact “lookup” attention (e.g., “find the name mentioned in paragraph 3”)
- Quality gap vs Transformers on retrieval-heavy tasks
Hybrid architectures (Jamba, Zamba) interleave Mamba layers with attention layers. Typical ratio: 1 attention layer per 4-8 Mamba layers. Attention handles retrieval-heavy operations (“find the name in paragraph 3”) while Mamba handles bulk sequential processing. Jamba (AI21, 2024) uses a 1:7 ratio with 52B parameters, achieving competitive quality with much lower KV cache than a pure Transformer.
Infrastructure Implications
| Architecture | Memory (inference) | Compute (inference) | Best for |
|---|---|---|---|
| MHA | KV cache | per step | Quality-critical, short context |
| GQA | KV cache | per step | Production LLMs (LLaMA, Mistral) |
| MLA | KV cache | + reconstruct | Very long context (DeepSeek) |
| Linear Attn | state | per step | Streaming, infinite context |
| SSM (Mamba) | state | per step | Very long context, streaming |
Self-Check
- Calculate KV cache size for MHA, GQA, and MLA for a given model and context length
- Explain why GQA has minimal quality loss compared to MHA
- Explain MLA’s “compress then reconstruct” approach and the compute-memory tradeoff
- Explain why linear attention changes the order of operations
- Explain why SSMs have O(1) memory per token but struggle with retrieval tasks
- Explain why hybrid architectures (Mamba + Attention) exist
References
- GQA: Training Generalized Multi-Query Transformer Models (Ainslie et al., 2023)
- DeepSeek-V2: A Strong, Economical, and Efficient Mixture-of-Experts Language Model
- Mamba: Linear-Time Sequence Modeling with Selective State Spaces (Gu & Dao, 2023)
- Jamba: A Hybrid Transformer-Mamba Language Model (AI21, 2024)