Skip to content

๐Ÿง  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_heads

FP8 KV cache halves the bytes, so 2 ร— G.

VariantYear / labIdeaResultCatch
MHAoriginaleach head has its own K,VG=1 โ†’ AIโ‰ˆ1huge cache (70B: 2.5 MB/token)
MQA2019 ยท Shazeer1 KV head for all queriesdecoder 46 โ†’ 3.8 ยตs/token (12ร—), BLEU โ‰ˆ unchangedtraining instability, worse on long-input fine-tunes
GQA2023 ยท Googlegroups of queries share a KV headT5-XXL 1.5 s โ†’ < 0.3 s (โˆ’81%), quality within noiseneeds uptraining: 5% of pretrain steps (mean-pool the KV heads in each group)
MLADeepSeek V2/V3cache one compressed latent per tokenKV โˆ’93%, peak gen throughput ~5.8ร— vs their own GQA-67B; quality stronger than MHAextra 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.

TechniqueWhat it gives up
Windowdistant tokens
Evictionwhatever left the cache
Learned sparsitytrusts a score that can be wrong
Hybridno 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.

ModelKV/token1 ร— 128K convoFits in 30 GB
Llama 3.1 8B (32 L ร— 8 KV ร— 128)~131 KB~17 GB1
Llama 3.1 405B (126 L ร— 8 KV ร— 128)~516 KB~66 GB0 โŒ
DeepSeek V3 671B (MLA, 512 + 64)~70 KB~9 GB3 โœ…

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

  1. Open config.json โ†’ compute G = heads / kv_heads and KV bytes/token
  2. The attention design is fixed at training time. Converting needs about 5% of pretraining, so choose it when you choose the model.
  3. Check the KV cache precision (FP8 doubles G's effective value)

<<>> with โ™ฅ๏ธ by S@Nchit