Prefill
All prompt positions are processed in parallel. Their projected keys and values are stored once for every layer.
The KV cache prevents every new token from recomputing the entire past. Build a prompt, decode token by token, and watch memory, latency, prefix reuse, and eviction change together.
All prompt positions are processed in parallel. Their projected keys and values are stored once for every layer.
Each generated token adds one key and one value per layer. Attention reads the existing cache instead of recreating it.
Many query heads share fewer KV heads. Reducing KV heads from 32 to 8 cuts cache memory by 4× without changing query-head count.
Every cached token contributes a key and value at every layer. The visual groups layers into rack shelves; longer trails mean more cached positions and more GPU memory.
New positions enter at the right. Under a sliding window, old positions leave at the left. Sink-token mode preserves a small beginning region while evicting the middle.
Prefix-matched positions glow. A server can reuse their KV tensors, avoiding repeat prefill compute. Reuse requires an identical prefix and compatible model configuration.
Calculate cache bytes before choosing concurrency. A model that fits alone may run out of memory when many sequences decode together.
Put reusable system instructions and shared documents first. Volatile timestamps or user IDs near the beginning destroy prefix-cache hits.
Sliding windows save memory but remove evidence. Long-context evaluation must include recall from early, middle, and recent positions.
During attention, the new query scores cached keys to find relevant positions, then combines the corresponding cached values. Both are required.
No. Model knowledge lives in weights. KV cache entries are request-specific activations representing the current sequence.
If a matching prefix was already processed, the server can load its stored keys and values and prefill only the unmatched suffix.
Vaswani et al., Attention Is All You Need defines the query, key, and value attention mechanism.
Shazeer, Fast Transformer Decoding explains shared keys and values for faster incremental inference.
Ainslie et al., GQA studies grouped KV heads between multi-head and multi-query attention.
Xiao et al., Efficient Streaming Language Models motivates preserving initial sink tokens during streaming eviction.