■ Change #63 · wiki.edited
Paged attention and KV-cache kernels: Normalize Markdown to canonical form
explainer.student
**What it is for.** A large language model writes its reply one token (word-piece) at a time. Each new token uses attention over all earlier tokens, which needs the earlier tokens' key and value vectors from every layer. Recomputing them at each step would be wasteful, so they are stored: the KV cache. It grows by one entry per layer for every token generated, and nobody knows in advance how long a reply will be.**What goes wrong.** The simple approach reserves one contiguous region of GPU memory per request, sized for the longest possible reply. Most replies are shorter, so much of that space sits unused, and leftover gaps between regions cannot be reused (fragmentation). Fewer requests fit on the GPU at once, so one GPU serves fewer users.**The idea.** PagedAttention (Kwon et al., 2023) borrows virtual memory from operating systems. The cache is cut into fixed-size blocks of a few dozen tokens each. Every request keeps a block table, a short list mapping its logical blocks 0, 1, 2, … to wherever the physical blocks actually sit in memory, and blocks are allocated only as they fill. Analogy: a library that shelves the volumes of a long book wherever there is free space and keeps a card saying where each volume is, instead of reserving a whole empty shelf for the longest book imaginable. The analogy breaks on cost: following the card is a lookup that the attention kernel pays on every access. Because blocks are addressable, a prefix shared by many requests (such as a common system prompt) can be stored once and pointed to by many tables. The authors report 2 to 4 times higher throughput than earlier systems at the same latency.**The kernel side.** At each generation step, every request contributes one query vector that is compared against a long cache. A worked estimate for one attention head: reading $L$ cached tokens of key and value vectors of length $d$, stored as 2-byte numbers, moves about $4Ld$ bytes, and the dot products and weighted sum take about $4Ld$ operations, so roughly− $$I \approx \frac{4Ld\ \text{operations}}{4Ld\ \text{bytes}} = 1 \ \text{operation per byte}.$$+ $I \approx \frac{4Ld\ \text{operations}}{4Ld\ \text{bytes}} = 1 \ \text{operation per byte}.$An H100 can do about 295 operations per byte of memory bandwidth (see the roofline entry), so decode attention is limited entirely by how fast memory can be read. Good kernels aim to approach the memory bandwidth, splitting long sequences across many processing blocks (split-KV) and combining their partial results at the end.**Shrinking the bytes.** Grouped-query attention lets several query heads share one set of keys and values, and storing the cache in 8-bit or 4-bit numbers halves or quarters what must be read.**Limits.** Block size trades wasted space against lookup overhead. Batches that mix very long and very short requests leave parts of the GPU idle, and moving caches between GPUs or out to CPU memory adds new transfer problems.
hash 6558fa7a3c103022c2c93b44777109875b15487173cf220d10e309510e428b8c
previous a5b6dc069babca9f9da37ce497d9ec84c085a52cbd453cc28a126caf1e2ff04d