↑↓ move Enter open ⌘K find from any page 256 of 256 tabs
Loading the recording…

What the KV cache is

A decoder writes one token at a time, and each new token attends to every token before it. Under the causal mask the keys and values of earlier positions never change once they are computed: position 10's key is the same whether the sequence ends at 11 or at 1,000. The KV cache keeps every layer's keys and values for all positions so far, so each new step only computes the new token's own query, key and value and reads the rest from memory. Generation splits into a prefill, which runs the whole prompt at once and fills the cache, and decode steps, which add one row per layer each.

This page records Qwen2.5-0.5B-Instruct answering a 52-token prompt, "The password for the guest Wi-Fi is maple-river-42. What is the guest Wi-Fi password?" in Qwen's chat template, greedily, with an 18-token answer. Its cache is drawn for one layer and one key/value head at a time; the toolbar picks the layer and the query head.

Prefill and decode

For one head of dimension , the query at position reads the cached keys and values of positions :

The prefill computes the whole lower triangle of this attention for the 52 prompt positions and writes rows 0–51 of every layer's cache. Each decode step feeds the last answer token, writes one row (yellow in The cache) and computes one row of the triangle; the circles beside the cache are the selected query head's weights , summing to 1. The keys are stored after RoPE has rotated them, so a cached key already carries its position.

What this step computes sets the cached step against recomputing the whole sequence without a cache. Counted from the tensor shapes, a decode step needs about 39 times fewer FLOPs than the recompute at the first step and 50 times fewer at the last, and the gap grows with the length; on this Mac's CPU in float32 the cached steps took 22 to 39 ms against 95 to 226 ms. The vocabulary projection runs once per step either way.

Memory

The cache trades that compute for memory. Per position it holds a key and a value for every layer and key/value head:

with layers, key/value heads and bytes in bfloat16: 512 bytes per layer and 12 KiB for all 24, as the Memory panel counts, measured from transformers' DynamicCache. After 17 decode steps the cache holds 69 rows. At the model's full context of 32,768 positions one sequence's cache would take 384 MiB, against 942 MiB of weights, and a server batching many sequences holds one cache each.

The cache is this small because Qwen2.5 uses grouped-query attention: its 14 query heads share 2 key/value heads, seven to each (the GQA tab). With a key and a value for every query head the same cache would take 84 KiB per position, seven times as much. Multi-query attention, one key/value head for all query heads, was proposed for exactly this reason.

Paged blocks

A server cannot know in advance how long an answer will be. Reserving the full context for every request wastes most of it, and growing a contiguous buffer fragments memory. PagedAttention, the scheme of vLLM, cuts each sequence's cache into fixed-size blocks, here of 16 positions, taken from a shared pool when the last one fills; a block table maps the sequence's logical blocks to physical blocks anywhere in the pool, as an operating system's page table maps virtual memory.

The Paged blocks panel shows this sequence growing from four blocks after the prefill to five, with the empty slots of the last block the only waste. One request takes blocks in order; with many requests the table points anywhere in the pool, and identical prefixes, such as a shared system prompt, can share physical blocks.

Why decoding is memory-bound

A decode step multiplies every weight matrix by one token's vector and reads the whole cache for one query row, so it moves far more bytes than it computes with. Pope et al. analysed this for large models on TPUs: decode speed is set by how fast weights and cache can be read, which is why the cache's size matters for speed and not only for memory. The tabs that follow attack it: KV quant stores the cache in fewer bits, KV eviction keeps fewer positions, HiCache moves cached prefixes between GPU memory, host memory and storage, and DP attention gives each GPU the MLA cache of its own requests instead of a copy of every request's cache.

Where it came from

The Transformer (2017) defined the masked decoder whose past keys and values the cache stores; caching them turns each decode step from a pass over the whole sequence into a pass over one token. Shazeer's multi-query attention (2019) shrank the cache to one key/value head, and grouped-query attention (2023) found the middle ground that Qwen2.5 and most current models use. PagedAttention (2023) made the cache a paged pool so that a server can batch many requests; its paper reports 2–4 times the throughput of earlier serving systems at the same latency.

Papers

  1. Attention Is All You Need Ashish Vaswani et al. · NeurIPS 2017 The masked decoder whose keys and values never change for past positions, which is what makes caching them possible.
  2. Efficient Memory Management for Large Language Model Serving with PagedAttention Woosuk Kwon, Zhuohan Li, Siyuan Zhuang, Ying Sheng et al. · SOSP 2023 PagedAttention and vLLM: the cache in fixed-size blocks with a block table, the layout of the Paged blocks panel.
  3. Efficiently Scaling Transformer Inference Reiner Pope, Sholto Douglas, Aakanksha Chowdhery, Jacob Devlin et al. · MLSys 2023 Prefill against decode, and why the cache's size and memory bandwidth set decoding speed in large models.
  4. Fast Transformer Decoding: One Write-Head is All You Need Noam Shazeer · arXiv 2019 Multi-query attention: one key/value head for all query heads, to shrink the cache that incremental decoding reads.
  5. GQA: Training Generalized Multi-Query Transformer Models from Multi-Head Checkpoints Joshua Ainslie, James Lee-Thorp, Michiel de Jong, Yury Zemlyanskiy, Federico Lebrón, Sumit Sanghai · EMNLP 2023 Grouped-query attention, the sharing of 2 key/value heads among 14 query heads that sets this cache's size.
  6. Qwen2.5 Technical Report An Yang et al. (Qwen Team) · arXiv 2024 The Qwen2.5 models, including the 0.5B instruct model whose cache this page records.

See also