KV cache and inference memory
During autoregressive generation, each new token requires attending to all previous tokens. Storing previously computed key and value vectors (the "KV-cache") avoids recomputation, but grows linearly with sequence length.
Memory footprint
For a model with layers, key-value heads of dimension (so , which equals under standard MHA), and batch size , the memory for a sequence of length is:
Without GQA (i.e. using standard MHA with , ), a 70B-scale model at a 128K token context ( tokens) in BF16/FP16 requires (or for tokens) — over 4× the memory of a single 80 GB H100. With GQA, Llama 3 70B uses 8 KV heads (), reducing that 8× to for a single 128K request.
Attacking the raw size of the KV cache
- Multi-Head Attention (MHA): Traditional approach. Uses separate Key and Value projections for each head, requiring the most memory.
- Multi-Query Attention (MQA) (2019 — Fast Transformer Decoding):
- Shares a single key-value head across all query heads, while still having multiple Query projections.
- Reduces KV-cache size by 8–32× depending on head count.
- Low memory, but degrades quality.
- Grouped-Query Attention (GQA) (2023 — GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints):
- Divides query heads into groups that share key-value projections.
- Llama 2's 70B model uses 8 key-value heads for 64 query heads, resulting in an 8× cache reduction with minimal quality loss.
- A compromise between memory and performance; standard in Llama 2/3, Mistral, and most modern inference-optimised models.
PagedAttention
- 2023 — Efficient Memory Management for Large Language Model Serving with PagedAttention
- Traditional approaches pre-allocate contiguous memory for the maximum sequence length, wasting 60–80% of KV-cache memory.
- Borrows from operating system virtual memory:
- Divides the KV-cache into fixed-size blocks (e.g. 16 tokens).
- A "Block Table" maps the logical sequence (tokens 1, 2, 3...) to physical blocks scattered non-contiguously in GPU memory.
- Memory is allocated block-by-block only as the sequence grows.
- Achieves near-zero waste (under 4%) and enables 2–4× throughput improvements.
KV-cache compression and eviction
- Quantisation: INT8 quantisation halves memory with negligible quality loss; INT4 quarters it with minor degradation.
- KIVI: Per-channel key quantisation and per-token value quantisation achieves 2-bit compression with minimal quality degradation on most benchmarks.
- H2O (Heavy Hitter Oracle):
- Exploits the fact that attention is often highly sparse (empirically ~95% in many layers).
- Dynamically retains only recent tokens plus "heavy hitters" (tokens with the highest accumulated attention), enabling 5–20× cache reduction.
StreamingLLM
- 2023/2024 — Efficient Streaming Language Models with Attention Sinks
- Initial tokens serve as "attention sinks", absorbing unused attention probability mass due to softmax normalisation.
- Maintaining these sinks plus a sliding window enables theoretically infinite generation without fine-tuning, though it does not provide true long-term memory.
- Tested to 4 million tokens.
Tags: AI