๐ง Chapter 5 โ Attention: The Architecture Side โ
Episodes 22 โ 24
1. One number explains MHA, MQA, GQA and MLA โ
Write out decode attention's FLOPs รท bytes and almost everything cancels: layers, sequence length, batch size, head size. What's left:
โญ The group size G
Decode attention arithmetic intensity โ G = query_heads / kv_headsFP8 KV cache halves the bytes, so 2 ร G.
| Variant | Year / lab | Idea | Result | Catch |
|---|---|---|---|---|
| MHA | original | each head has its own K,V | G=1 โ AIโ1 | huge cache (70B: 2.5 MB/token) |
| MQA | 2019 ยท Shazeer | 1 KV head for all queries | decoder 46 โ 3.8 ยตs/token (12ร), BLEU โ unchanged | training instability, worse on long-input fine-tunes |
| GQA | 2023 ยท Google | groups of queries share a KV head | T5-XXL 1.5 s โ < 0.3 s (โ81%), quality within noise | needs uptraining: 5% of pretrain steps (mean-pool the KV heads in each group) |
| MLA | DeepSeek V2/V3 | cache one compressed latent per token | KV โ93%, peak gen throughput ~5.8ร vs their own GQA-67B; quality stronger than MHA | extra math; one latent โ duplicated across TP cards |
๐ค Why does Llama 3 have 8 KV heads at every size? โ
Query heads go 32 โ 64 โ 128 (8B โ 70B โ 405B), while KV heads stay 8. That 8 reflects the deployment, not the model. (Meta never says this outright; the GQA paper makes the duplication argument.)
๐งฉ MLA's absorb trick โ
Matrix multiplication is associative, so fold the up-projection onto the one query instead of the 100K cached latents. Decode traffic then looks like MQA while the model keeps MHA's capacity. vLLM uses both paths: compute-friendly for prefill, data-movement-friendly (absorbed) for decode.
๐ Where each variant lands on an H100 (decode, FP16) โ
After seven years, three labs and three architectures, the best one still uses only ~โ of the card. To go further you have to cut memory trips โ FlashAttention.
2. Sparse attention: the attention you don't compute โ
Every technique here gives something up, and it's never the same thing.
1๏ธโฃ Sliding window attention โ
- Each token only attends to the last W tokens (Mistral: 4096). Information still travels back one window per layer, so 32 layers ร 4096 reaches about 131K.
- KV becomes a rolling buffer of W slots, so 8ร less cache at 32K. 2ร faster at 16K, with a window-aware kernel.
The config line's history:
Each change was right when it was made, because the binding constraint kept moving. Google: all-global layers cost about 60% KV overhead at 32K; interleaving brings it < 15% (measured at 3:1 local:global, so treat it as a direction, not the shipped number).
2๏ธโฃ DeepSeek Sparse Attention (V3.2) โ
- Attention cost goes from O(Lยฒ) to O(Lยทk). The indexer only needs a roughly right ranking, hence FP8 + ReLU "for throughput."
- Costs: a second cache (128 values/token next to the latent's 576), and the indexer is still O(Lยฒ) during prefill. The quadratic term is cheaper but not gone, so speedups land below the complexity math.
- Benchmarks on par; API prices cut > 50% on launch day.
3๏ธโฃ Eviction & attention sinks (StreamingLLM) โ
- A pure rolling window collapses (perplexity explodes) once the first token slides out. Why? Softmax must sum to 1, so the model dumps unneeded attention on the first tokens, the only ones visible to everyone under the causal mask.
- Fix: pin 4 sink tokens + a rolling window. Up to 22.2ร faster per token than sliding window with recomputation.
- Those same first tokens carry massive activations (Llama-2-7B: max ~2000 vs median ~0.2), which matters for quantization.
โ Myth: "StreamingLLM gives 4M tokens of context"
Paper: it does not extend the models' context window. You get infinite stable streaming, not long-range memory. Anything 2M tokens back is gone.
4๏ธโฃ Hybrids: layers with no KV cache โ
Linear attention / state-space (Mamba) layers keep a fixed-size state, so only the attention fraction of layers grows the cache.
Remember the ratio, not the model name.
| Technique | What it gives up |
|---|---|
| Window | distant tokens |
| Eviction | whatever left the cache |
| Learned sparsity | trusts a score that can be wrong |
| Hybrid | no attention in most layers |
Ask whether your workload needs the far end. Long-document QA or summarization โ a window is the wrong tool. A long chat that mostly needs recent context โ a window is nearly free.
3. ๐ฉบ Clinic: which LLM can you actually serve? โ
Setup: 128K-token conversations, the same 30 GB KV budget for each model, FP16, one copy.
| Model | KV/token | 1 ร 128K convo | Fits in 30 GB |
|---|---|---|---|
| Llama 3.1 8B (32 L ร 8 KV ร 128) | ~131 KB | ~17 GB | 1 |
| Llama 3.1 405B (126 L ร 8 KV ร 128) | ~516 KB | ~66 GB | 0 โ |
| DeepSeek V3 671B (MLA, 512 + 64) | ~70 KB | ~9 GB | 3 โ |
The biggest model holds the most conversations: 3ร as many as a model 80ร smaller. You can't read KV cost off the parameter count.
Why did your quick benchmark say all three were fine? It used 1 request, a short prompt and a short answer. 1K tokens on Llama-8B is about 130 MB of KV against 16 GB of weights, so the KV cache barely showed up. Benchmark at your real operating point.
โ Monday checklist
- Open
config.jsonโ compute G = heads / kv_heads and KV bytes/token - The attention design is fixed at training time. Converting needs about 5% of pretraining, so choose it when you choose the model.
- Check the KV cache precision (FP8 doubles G's effective value)
