Advanced
The KV cache
Autoregressive decoding stores past attention keys and values so each new token avoids recomputing the entire prefix.
Updated
1
Concept
A decoder-only language model generates one token, appends it to the context, and repeats. A naive implementation would run the entire Transformer over the growing sequence on every step. Most work on earlier positions would be identical. The KV cache stores the attention states that future tokens need, turning that repeated prefix computation into reusable memory.
In one attention layer, every token produces query, key, and value projections. During causal decode, only the newest position needs a new output. Its query can attend to keys from all allowed past positions and use the resulting weights to mix their values. Past queries are not needed again: they already produced their own outputs. Therefore the cache retains past keys and values, while the new token computes its own , , and .
For one head, the decode operation resembles
where cached matrices grow when and are appended. This happens at every cached layer. The cache is a consequence of exact causal attention, not an approximation to it.
Ignoring allocator overhead, an intuitive memory model is
where the factor two represents keys and values, is layer count, cached positions, KV heads, head dimension, and bytes per element. Multiply again by active sequences. This formula explains why long context can exhaust memory even though model weights remain unchanged.
Multi-head attention uses separate K/V heads alongside query heads. Multi-query and grouped-query attention share K/V across query heads, reducing and therefore cache traffic and capacity. Quantized cache formats reduce , with accuracy and kernel-support trade-offs. Sliding windows or eviction reduce effective , but then old tokens are no longer available to ordinary attention.
Cache layout matters to performance. Decode reads a long history to produce a small amount of new computation, so moving K/V data can dominate. Tensors need layouts that kernels consume efficiently, while a server needs flexible allocation for sequences that grow unpredictably. Reserving every request’s maximum length wastes memory; repeatedly moving contiguous buffers creates fragmentation and copies. Paged allocation addresses this ownership problem.
Prefix caching shares already computed blocks when requests begin with exactly the same token prefix, such as a common system prompt. Safe sharing requires the same model, adapter, positional treatment, and token sequence. Text that looks identical after rendering may tokenize differently. Cache keys must never cross authorization boundaries when the cached representation or timing can reveal private context.
Beam search and speculative decoding complicate ownership. Several candidates may share a prefix, branch, and discard paths. Copy-on-write block tables avoid copying the common prefix. Cancellation must decrement references exactly once. A leak gradually removes serving capacity; premature reuse corrupts one request with another’s state.
The cache does not make decode constant-time in context length. Projection of the old prefix disappears, but the new query still reads and attends over retained K/V states unless the architecture uses a bounded window or different mechanism. The durable model is a transcript: computation already performed becomes memory, saving arithmetic at the price of growing bandwidth, capacity, lifecycle, and privacy responsibilities.
2
Explain it like I am five
A courtroom stenographer keeps an indexed transcript of everything already said. When a new question arrives, the judge consults the transcript instead of asking every witness to repeat the hearing from the beginning. The transcript grows each turn and must preserve the exact order and case identity. The KV cache is that transcript for attention: prior keys are the index entries, prior values are the stored testimony, and the new query reads them.
3
Teach it back
Explain what is stored in a decoder KV cache, why it accelerates autoregressive generation, and why its memory grows with active sequence length.
Minimum: 80 characters and 15 words. Your text stays only in this browser.
Saved only on this device.
Show a model answer
For each cached layer and past token position, the server stores the key and value projections needed by future attention. At decode, the new token computes only its own Q, K, and V; its query attends to cached keys and mixes cached values, avoiding repeated projection of the prefix. Since another key and value are appended at every layer for every generated token, memory grows with sequence length, batch size, layer count, KV-head count, head dimension, and bytes per element.
4
Check your understanding
Complete the teach-back and answer the quiz correctly to finish this lesson.
Sources
- Woosuk Kwon et al. (2023). Efficient Memory Management for Large Language Model Serving with PagedAttention.