Bookmarks

KV cache

An area of memory where a language model, while generating text, stores the attention keys and values it has already computed so that it does not have to recompute them for every new token.

Also: key-value cache, K/V cache, KV caching

checked 9 Nov

All terms

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).

TensorStoredPurpose
Keys K of each layeryesthe new token “looks at” previous tokens through them
Values V of each layeryesthe attention output is assembled from them
Queries Qnoneeded 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:

text
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 sequence

PagedAttention 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 FP8 instead of FP16 halves 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

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • context window

    The context window is how many tokens the model is able to see; the KV cache is the memory holding the already computed keys and values of those tokens.

In depth in the references

where the topic is covered in detail