KV Cache Optimization

KV Cache Optimization refers to the family of techniques that reduce the memory footprint, computational cost, and latency of storing and retrieving the key-value tensors generated during autoregressive inference in transformer-based language models. It matters because, without caching, every new token generation requires recomputing attention over the entire context, making inference quadratically expensive — a bottleneck that dominates the cost of production LLM serving. The counterintuitive fact: in many large-scale deployments, KV cache memory consumption — not model weights — is the primary constraint on batch size and throughput.

The core insight

During autoregressive decoding, the attention keys and values for all previously generated tokens never change — they are deterministic functions of tokens that have already been committed. Instead of recomputing these tensors on every forward pass, you compute them once and store them; each new token only needs to compute its own K and V vectors and attend over the cached history. The entire science of KV cache optimization is then about answering one question: how do you make that cache smaller, faster, or smarter without hurting model quality?

How It Works

Standard Attention and the Recomputation Problem

In a decoder-only transformer, the scaled dot-product attention at layer ℓ\ell for query position tt over context positions 1…t1 \dots t is:

1
Attention(Qt,K1:t,V1:t)=softmax ⁣(QtK1:t⊤dk)V1:t\quad \text{Attention}(Q_t, K_{1:t}, V_{1:t}) = \text{softmax}\!\left(\frac{Q_t K_{1:t}^\top}{\sqrt{d_k}}\right) V_{1:t}

Explanation: The query vector for the current token attends over all cached key vectors, producing a weighted sum of value vectors — and dkd_k is the per-head key dimension used to stabilize gradients. Here Qt∈RdkQ_t \in \mathbb{R}^{d_k} is the query for token tt, K1:t∈Rt×dkK_{1:t} \in \mathbb{R}^{t \times d_k} is the stacked key matrix for all tokens seen so far, and V1:t∈Rt×dvV_{1:t} \in \mathbb{R}^{t \times d_v} is the stacked value matrix. Without caching, computing K1:tK_{1:t} and V1:tV_{1:t} from scratch at every decoding step costs O(t)O(t) linear projections per layer, giving O(L⋅t)O(L \cdot t) total work per token where LL is the number of layers.

KV Cache Memory Footprint

The total memory required to store the KV cache for a single sequence of length TT is:

2
MKV=2×L×H×dk×T×sizeof(dtype)\quad M_{\text{KV}} = 2 \times L \times H \times d_k \times T \times \text{sizeof}(\text{dtype})

Explanation: Factor of 2 accounts for both keys and values; LL layers each have HH attention heads each with dkd_k-dimensional projections, accumulated over TT tokens. For a concrete example: LLaMA-2 70B has L=80L=80 layers, H=64H=64 heads, dk=128d_k=128, using float16 (2 bytes). A single sequence of T=4096T=4096 tokens requires 2×80×64×128×4096×2≈10.7 GB2 \times 80 \times 64 \times 128 \times 4096 \times 2 \approx 10.7\text{ GB}.

Multi-Query and Grouped-Query Attention

Multi-Query Attention (MQA) and Grouped-Query Attention (GQA) reduce cache size by sharing key and value heads across query heads. With GG key-value groups and HH total query heads:

3
MGQA=2×L×G×dk×T×sizeof(dtype),G≤H\quad M_{\text{GQA}} = 2 \times L \times G \times d_k \times T \times \text{sizeof}(\text{dtype}), \quad G \leq H

Explanation: By sharing K and V across H/GH/G query heads each, you reduce KV cache by a factor of H/GH/G with minimal quality degradation when G≥8G \geq 8. MQA sets G=1G=1 (maximum compression); GQA uses G∈{4,8}G \in \{4, 8\} as a quality-efficiency sweet spot — LLaMA-3 and Mistral both use GQA.

Quantized KV Cache

Quantizing cached K and V tensors from float16 to int8 or int4 reduces memory by 2× or 4× respectively. For a quantization scale ss and zero-point zz, the dequantization is:

4
x^=s⋅(xq−z)\quad \hat{x} = s \cdot (x_q - z)

Explanation: Each stored integer xqx_q is mapped back to a floating-point approximation x^\hat{x} using per-channel or per-token scales, introducing bounded quantization error. The attention computation then proceeds on dequantized tensors, adding a small but measurable perplexity penalty that becomes acceptable at int8 precision for most production use cases.

Attention Score Sparsity and Eviction

Empirical studies (Zhang et al., StreamingLLM; Ge et al., FastGen) show that attention weight distributions are highly sparse — only a small fraction of cached tokens receive meaningful attention weight. This motivates eviction policies that drop low-importance tokens. A common importance score for token ii at decoding step tt is its cumulative attention mass:

5
score(i)=∑j=itαj→i,αj→i=softmax ⁣(qjki⊤dk)\quad \text{score}(i) = \sum_{j=i}^{t} \alpha_{j \to i}, \quad \alpha_{j \to i} = \text{softmax}\!\left(\frac{q_j k_i^\top}{\sqrt{d_k}}\right)

Explanation: Tokens that have accumulated high attention weight across many decoding steps are considered important and retained; tokens with near-zero cumulative weight are candidates for eviction.

researcher · Ashish Vaswani | b. 1983 | Invented the Transformer Architecture

Vaswani led the team at Google Brain that published "Attention Is All You Need" (2017), introducing the scaled dot-product attention mechanism and multi-head attention that underlies every modern LLM. His work directly created the architectural context in which KV caching became both necessary and possible — without the autoregressive transformer, there would be no KV cache to optimize. He later co-founded Inceptive, a company applying transformers to RNA biology.

researcher · Noam Shazeer | b. 1975 | Invented Multi-Query Attention

Shazeer, one of the original "Attention Is All You Need" co-authors, published "Fast Transformer Decoding: One Write-Head is All You Need" in 2019, introducing Multi-Query Attention as a direct solution to KV cache memory pressure during inference. His insight — that query heads need not share their key and value projections — reduced KV cache by up to 64× while preserving most model quality, and became the foundation for GQA. He later co-founded Character.AI.

Figure
KV Cache Data Flow During Autoregressive Decoding
KV Cache: Prefill vs. Decode Phase PREFILL PHASE "The" "cat" "sat" "on" Transformer Layer (all tokens) store K,V KV Cache K: [4 × d_k] V: [4 × d_v] DECODE PHASE "mat" →? Attn(Q_new, K_cache, V_cache) read + append new K,V Next Token Output Memory Reduction Strategies MQA/GQA ÷ H/G heads Quantization int8 = ÷2, int4 = ÷4 Token Eviction drop low-attn tokens PagedAttention virtual KV pages
Figure
GQA Head Sharing and Memory Compression
MHA vs. GQA vs. MQA — KV Head Sharing Multi-Head Attention (MHA) H=4 KV heads Q₁ Q₂ Q₃ Q₄ KV₁ KV₂ KV₃ KV₄ Cache: 4 × T × d_k × 2 Memory: 1× (baseline) Grouped-Query Attention (GQA) G=2 KV groups, H=4 Q heads Q₁ Q₂ Q₃ Q₄ KV_group₁ KV_group₂ Cache: 2 × T × d_k × 2 Memory: ½× baseline Multi-Query Attn (MQA) G=1 KV group, H=4 Q heads Q₁ Q₂ Q₃ Q₄ Single KV Cache: 1 × T × d_k × 2 Memory: ¼× baseline Quality: MHA ≥ GQA ≈ MQA   |   Used in: GPT-3 (MHA) · LLaMA-3 (GQA) · Gemini (MQA)

Intuition

The Mental Model

Imagine you are translating a long novel and, on every page, you need to remember the names of all characters introduced so far. Without a notepad (no cache), you would re-read the entire novel from page 1 every time you need to recall a name — obviously insane. With a notepad (the KV cache), you jot down each character name once and only consult your notes when needed. KV cache optimization is then about making that notepad more compact: MQA/GQA is like sharing one notepad among multiple translators working in parallel (they all need the same character list, so why keep separate copies?); quantization is like using shorthand instead of full spellings (saves space with slightly less precision); token eviction is like crossing out minor characters who were mentioned once in chapter 2 and never again, because experience shows you will never need them; and PagedAttention is like using a binder with removable pages instead of a fixed notebook, so you can give each translator exactly as many pages as they currently need, no more, no less, and recycle pages when a translation job is done.

Historical Development

Origins · 1 minhow this was discovered
History
2017
Vaswani et al., "Attention Is All You Need" — introduces scaled dot-product attention and the transformer architecture, implicitly defining the K and V tensors whose caching becomes critical.
2019
Shazeer, "Fast Transformer Decoding: One Write-Head is All You Need" — introduces Multi-Query Attention, reducing KV cache by H×H\times by sharing a single K/V head across all query heads.
2020
Radford et al. (OpenAI) deploy GPT-3 with standard multi-head attention; KV caching is used in inference but cache memory becomes a visible bottleneck at scale for the first time.
2022
Pope et al., "Efficiently Scaling Transformer Inference" (Google) — systematic analysis of inference memory bounds, introducing the arithmetic intensity framework that formalizes when inference is memory-bound