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 LL layers, nkvn_{kv} key-value heads of dimension dheadd_{head} (so dkv=nkv⋅dheadd_{kv} = n_{kv} \cdot d_{head}, which equals dmodeld_{model} under standard MHA), and batch size BB, the memory for a sequence of length NN is:

MemoryKV=2×B×N×L×dkv×Precision (bytes)\text{Memory}_{KV} = 2 \times B \times N \times L \times d_{kv} \times \text{Precision (bytes)}

Without GQA (i.e. using standard MHA with L=80L=80, dkv=dmodel=8192d_{kv} = d_{model} = 8192), a 70B-scale model at a 128K token context (131,072131{,}072 tokens) in BF16/FP16 requires 2×1×131,072×80×8192×2≈344 GB2 \times 1 \times 131{,}072 \times 80 \times 8192 \times 2 \approx 344 \text{ GB} (or ≈335 GB\approx 335 \text{ GB} for 128,000128{,}000 tokens) — over 4× the memory of a single 80 GB H100. With GQA, Llama 3 70B uses 8 KV heads (dkv=8×128=1024d_{kv} = 8 \times 128 = 1024), reducing that 8× to ≈43 GB\approx 43 \text{ GB} 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