The KV Cache.
Keep every past token’s keys and values. It saves the recomputation and spends the memory.
A new token attends to every earlier token’s key and value vectors in every layer (plate IV.2). Those vectors do not change once computed — a causal model never revises the past — so servers keep them: the key–value cache. Without it, generating each new token would mean running the whole prefix through the model again; with it, each step processes one token and reads the stored vectors.
The price is memory. Per token the cache holds a key and a value for each key–value head in each layer: 2 × layers × KV heads × head size × bytes. Architecture decides how much: multi-head attention keeps one key–value pair per query head; grouped-query attention (plate VII.2) shares each pair among a group of query heads; DeepSeek’s multi-head latent attention stores one compressed vector per layer. The workshop computes the cache of published configurations and how many sequences of a given length fit beside Llama 3 8B on an 80 GB GPU.
Below: cache bytes per token for five published models on a logarithmic scale; readouts give the cache for one sequence of the chosen length and how many such sequences fit.
- Explain what the KV cache stores.
- Compute its size per token.
- Compare MHA, GQA and MLA.
- Relate cache size to concurrency.
Memory that grows with every token
Llama 3 8B stores 131.1 KB of keys and values per token, so one 8 192-token conversation holds 1.1 GB and a full 131 072-token context 17.2 GB — more than the model’s own weights. After the weights, an 80 GB GPU has room for about 52 conversations of 8 192 tokens. GPT-3’s multi-head attention needed 36 times as much per token as Llama 3 8B; DeepSeek-V3’s latent attention needs 1.9 times less than Llama 3 8B despite being a far larger model.
Recomputing instead would cost the whole prefix’s forward pass at every step: generating 500 tokens after a 1 000-token prompt would process about 1,250 tokens per output token on average instead of one. No production server does that. The cache instead makes memory the resource that limits how many users a GPU serves at once, which is why the attention variants of Vol. VI were invented, why caches are quantised (plate XXIV.3), and why their allocation is the subject of the next plate.
Terms
- KV cache
- Stored keys and values of past tokens for every layer.
- Grouped-query attention
- Sharing each key–value head among several query heads.
- Multi-head latent attention
- Caching one compressed vector per layer instead of keys and values.
- Concurrency
- How many sequences are served at once.
Sources
- Brown et al., NeurIPS 2020, Table 2.1 (GPT-3).
- Llama Team, AI @ Meta, arXiv:2407.21783, 2024, Table 3.
- DeepSeek-AI, “DeepSeek-V3 Technical Report”, arXiv:2412.19437, 2024, section 2.1.1.
- Ainslie et al., “GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints”, EMNLP 2023.