A language model writes text token by token, and every attention step needs the keys and values of all previous tokens. The KV cache is what avoids recomputing them; it also decides how much memory an inference server needs, because the cache grows with the context length and can take up a large share of GPU memory, limiting how many sequences fit in a batch.
What is stored and why
In the attention layer (self-attention) each token is turned into three vectors: a query (Q), a key (K) and a value (V). To produce its output, a new token needs its own Q, plus the K and V of all previous tokens. Without a cache, the whole context would have to be run through the model again at every step, making generation quadratic in length. So after each layer the keys and values are saved into the KV cache; there is no need to store queries (Q).
| Tensor | Stored | Purpose |
|---|---|---|
Keys K of each layer | yes | the new token “looks at” previous tokens through them |
Values V of each layer | yes | the attention output is assembled from them |
Queries Q | no | needed only for the current step and recomputed on the spot |
The size is easy to work out: for each token there are two tensors (K and V) per layer, each holding kv_heads × head_dim numbers, multiplied by the size of one number in bytes:
bytes_per_token = 2 × layers × kv_heads × head_dim × bytes_per_number # Llama 3 8B: 32 layers, 8 kv_heads, head_dim 128, bf16 (2 bytes)# 2 × 32 × 8 × 128 × 2 = 131,072 bytes = 128 KiB per token# an 8,192-token context → about 1 GiB per sequencePagedAttention in vLLM
An inference server has to keep caches for dozens of sequences of different lengths, and that length is not known in advance. The vLLM authors calculated that existing systems wasted 60–80% of the cache memory through fragmentation and over-reserving “to be on the safe side”. The vLLM engine borrowed the idea of virtual memory: the KV cache of each sequence is cut into fixed-size blocks that are handed out as the context grows. This mechanism is called PagedAttention.
- almost no cache memory is wasted — more sequences fit in a single batch
- a shared prefix (for example, a system prompt) can be stored once and shared between requests
- continuous batching can add and remove sequences without reshaping memory
How the KV cache is reduced
- architecturally: GQA and multi-query attention — fewer heads for keys and values
- cache quantisation: storing in
FP8instead ofFP16halves the size - prefix caching: the shared system prompt is computed once and reused
- eviction: a sliding window or dropping old tokens when the whole context is not needed
Sources
the term is written from these pages- vLLM: Easy, Fast, and Cheap LLM Serving with PagedAttention (blog.vllm.ai, 2023)blog.vllm.ai/2023/06/20/vllm.html(opens in a new tab)
- Kwon et al. Efficient Memory Management for Large Language Model Serving with PagedAttention (SOSP 2023)arxiv.org/abs/2309.06180(opens in a new tab)
- Ainslie et al. GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpointsarxiv.org/abs/2305.13245(opens in a new tab)
Verified 9 Nov