Attention Evolution & KV Cache Compression
In autoregressive Large Language Model (LLM) inference, the Self-Attention decoding stage is strictly memory-bandwidth bound. Under ultra-long context generation, Key-Value (KV) Cache memory scales linearly with sequence length, rapidly surpassing the memory footprint of the static model weights themselves.
1. Architectural Evolution: MHA to GQA and MLA
2. Memory & Computational Complexity Comparison
For hidden dimension , head count , head dimension , layer count , and sequence length :
2.1 Multi-Head Attention (MHA)
Standard MHA allocates independent Key and Value heads for each Query head:
- Bottleneck: With and , a single request KV Cache exceeds 50 GB, causing rapid VRAM exhaustion.
2.2 Grouped-Query Attention (GQA)
GQA groups Query heads into shared Key/Value heads (typically ):
- Trade-off: Achieves a fixed compression ratio of (typically –), but head sharing incurs minor expressiveness degradation in complex multi-document retrieval.
3. DeepSeek MLA (Multi-Head Latent Attention) Architecture
DeepSeek MLA introduces low-rank joint compression, achieving over 93% KV Cache reduction while preserving full multi-head expressive power during inference.
3.1 Low-Rank Projection & Latent Vector Caching
Instead of caching individual multi-head Keys and Values, MLA projects the hidden state into a shared low-rank latent vector where :
- Inference Invariant: In VRAM, only the single latent vector is stored. The up-projection matrices and are absorbed directly into Query projection matrices during inference, completely eliminating the need to materialize multi-head Value caches in memory!
3.2 Decoupled RoPE (Rotary Position Embedding)
Because positional embeddings contain relative rotation matrices that cannot be absorbed into static linear up-projections, MLA introduces a dedicated decoupled Key head carrying positional encodings:
| Architecture | Single-Token Layer KV Cache (fp16) | Relative Memory Footprint | Expressive Capacity |
|---|---|---|---|
| MHA | (Baseline) | Full Independent Multi-Head | |
| GQA () | Grouped Constrained Multi-Head | ||
| DeepSeek MLA | (98.2% Reduction) | Full Unconstrained Multi-Head |
4. Sparse Attention Mechanics: SnapKV and PyramidKV
In ultra-long context inference (128k–1M Tokens), sparse attention algorithms provide orthogonal memory savings via dynamic eviction and layer-aware allocation:
4.1 SnapKV: Observation Window Fingerprints
- Core Insight: Attention heads exhibit highly clustered attention fingerprints. Historical Key tokens that interact frequently within an Observation Window (approx. 32 tokens at the prompt suffix) consistently dominate decoding attention weights.
- Dynamic Pruning: Based on observation window clustering scores, SnapKV evicts 80% of inactive historical tokens, retaining only 20% critical anchors with negligible perplexity degradation ().
4.2 PyramidKV: Layer-Aware Hierarchical Allocation
- Core Insight: Shallow Transformer layers perform broad token extraction and require wider KV windows; deeper layers focus on high-order semantic reasoning, displaying lower sensitivity to distant historical tokens.
- Hierarchical Budgeting: PyramidKV allocates generous KV budgets to shallow layers and linearly decreases cache limits in deeper layers, slashing overall VRAM usage by 50%–70%.
5. Engineering Recommendations
- High-Throughput Services & 100k+ Context: Default to native MLA models (such as DeepSeek-V3 / DeepSeek-R1) to fundamentally eliminate KV Cache bandwidth cliffs.
- Single-GPU Edge Inference: For standard GQA models (e.g. Qwen2.5/3, Llama-3), apply SnapKV sparse kernels or 4-bit KV Cache quantization to compress 64k context footprints from 16 GB down to .
- Anti-Pattern Warning: Avoid naive stride truncation without attention fingerprint verification, which causes critical information dropouts in multi-document retrieval tasks.