1. What the KV Cache Stores and Why It Grows Linearly
Autoregressive transformers attend to every previous token in the sequence when predicting the next token. Recomputing Key (K) and Value (V) projections for all prior tokens on every step would scale quadratically. Caching those K and V tensors in VRAM turns generation into a linear-memory lookup—at the cost of VRAM growing proportionally with your configured context window.
2. How GQA and MLA Shrink KV Memory Footprints
Early transformers used Multi-Head Attention (MHA), allocating a dedicated K and V head for every query head. Grouped-Query Attention (GQA) shares each K/V head across 4 to 8 query heads, slashing KV cache size by 75–87.5% with almost no accuracy loss. DeepSeek’s Multi-Head Latent Attention (MLA) goes further by compressing KV states into a shared low-rank latent vector.
3. Quantizing the KV Cache (FP16 → Q8_0 / Q4_0)
Even with GQA, pre-allocating a 32K or 64K FP16 KV cache on a 12GB GPU leaves almost no room for model weights. Quantizing KV cache tensors to Q8_0 reduces KV VRAM by 50% with negligible perplexity impact, while Q4_0 saves 75% for extreme context lengths on entry-level cards.