44 KV-Cache: Autoregressive Inference Efficiency
← Back to: BERT & GPT
44.1 Why decoding recomputes
In autoregressive decoding the model appends one token per step and re-runs attention. Naively, generating token \(t\) recomputes \(K\) and \(V\) for all positions \(1..t\) — but those projections never change for tokens already produced. Only the new token’s query needs the fresh past. Caching \(K,V\) turns each step from \(O(t)\) recompute into \(O(1)\) projection plus an \(O(t)\) attention read.
\[ \text{step } t:\quad q_t = x_t W_Q,\quad K_{1:t} = [\,K_{1:t-1}\,;\, x_t W_K\,],\quad V_{1:t} = [\,V_{1:t-1}\,;\, x_t W_V\,]. \]
The bracketed history is read from cache; only the last row is computed.
44.2 The memory bottleneck
The cache size grows with everything at once:
\[ \text{KV memory} = 2 \times b \times L \times h \times n \times d_k \times \text{(bytes)}, \]
for batch \(b\), layers \(L\), heads \(h\), sequence \(n\). For a long-context LLM this dwarfs the parameter memory and caps batch size — inference becomes memory-bandwidth bound, not compute bound. Reducing the K/V term is therefore the highest-leverage inference optimization.
44.3 Attention-head reduction lineage
All three methods keep full query heads but share or compress K/V:
- MQA (Shazeer 2019 (Shazeer, 2019)) — all query heads share a single K/V head. Shrinks the cache by a factor of \(h\), at some quality cost.
- GQA (Ainslie 2023 (Ainslie et al., 2023)) — query heads share \(G\) K/V groups, interpolating between MHA (\(G{=}h\)) and MQA (\(G{=}1\)). The pragmatic default in LLaMA-2/3 and most open models — most of the savings, little quality loss.
- MLA (DeepSeek-V2 2024 (DeepSeek-AI, 2024)) — low-rank joint compression of K and V into a small latent that is cached and re-expanded per head, cutting cache further while retaining multi-head expressivity.