Sub topic 3
The KV Cache and Why Memory Limits Throughput
This chapter assumes you know what tokens, prompts, completions, and GPUs are. You should also understand that an autoregressive language model generates one token at a time after reading the prompt.
What the KV cache stores
During generation, each transformer layer computes attention using three values: queries (Q), keys (K), and values (V). The query is used for the current token. The keys and values from earlier tokens are reused by later tokens.
The KV cache is GPU memory that stores those earlier keys and values. Without it, the model would recompute attention data for the entire sequence every time it generated a token.
Generation has two phases:
- Prefill processes the complete prompt. It computes and stores K and V for every prompt token.
- Decode generates completion tokens one at a time. Each new token adds its K and V to the cache and reads the cached values for attention.
The cache grows with the number of active sequences and the number of tokens in each sequence. This growth often limits concurrency before the model's weights do.
Estimating KV-cache memory
A useful approximation for the cache size is:
cache bytes = 2 × layers × tokens × KV heads × head dimension × bytes per valueThe first factor of 2 accounts for keys and values. With grouped-query attention, KV heads can be smaller than the number of attention heads.
For example, a model with 32 layers, 8 KV heads, a head dimension of 128, and two-byte values uses about 512 MiB for 4,096 cached tokens:
layers = 32
tokens = 4096
kv_heads = 8
head_dim = 128
bytes_per_value = 2
cache_bytes = 2 * layers * tokens * kv_heads * head_dim * bytes_per_value
cache_mib = cache_bytes / (1024 ** 2)
print(f"{cache_mib:.0f} MiB")This estimate covers only one sequence. Several concurrent requests multiply the token count. The model weights, temporary activations, CUDA workspace, and runtime allocations also need GPU memory.
Why the cache limits throughput
A request with a long prompt consumes cache space during prefill. During decode, every active request repeatedly reads its cached K and V data while appending new entries. As the cache grows, memory traffic grows too.
When GPU memory is nearly full, the server must admit fewer sequences, reject requests, or reduce the maximum sequence length. Fewer active sequences can leave the GPU underused. Allowing too many sequences can cause out-of-memory errors.
vLLM manages cache storage in blocks rather than requiring one large contiguous allocation. This reduces wasted space from different sequence lengths and makes scheduling more flexible. It does not remove the underlying memory requirement: every cached token still needs storage.
A simple memory model
| Memory consumer | Grows with | Typical consequence |
|---|---|---|
| Model weights | Model size | Determines the baseline GPU requirement |
| KV cache | Active tokens and sequences | Limits concurrency and context length |
| Temporary workspaces | Kernel and batch shape | Reduces available headroom |
The best setting depends on the workload. Short prompts and short answers permit more concurrent requests. Long contexts require more cache per request and usually reduce throughput.
Exercise
A workload has twice as many active cached tokens but the same model and data type. What happens to the approximate KV-cache memory requirement?
Show answer
It approximately doubles, because the cache size is linear in the number of cached tokens.