Sub topic 4
PagedAttention: Managing the KV Cache in Blocks
Prerequisites: you should understand autoregressive decoding, GPU memory, and the purpose of a key-value (KV) cache. You should also know that a request can generate tokens at different speeds and lengths.
Why the KV Cache Needs a Different Allocator
During generation, a transformer stores attention keys and values for tokens it has already processed. This storage is the KV cache. Each new token reads the earlier entries instead of recomputing them.
A request's cache grows one token at a time, but a GPU memory allocator usually reserves larger contiguous regions. If several requests have different lengths, unused gaps appear between allocations. This is external fragmentation. Memory may remain technically free while no sufficiently large contiguous region is available for a new request.
A second problem is over-reservation. If the server reserves space for a request's maximum possible length, short requests waste most of that space. These problems reduce the number of requests that can run together.
How PagedAttention Works
PagedAttention divides the KV cache into fixed-size blocks. A block holds the keys and values for a small number of consecutive tokens. Blocks do not need to be adjacent in physical GPU memory.
For each sequence, vLLM keeps a block table. The table maps logical block positions, such as block 0 or block 1 in a sequence, to physical blocks in the GPU cache pool.
When attention runs, the kernel follows the table, gathers the required blocks, and computes attention over them. A sequence can therefore grow by receiving another free block rather than by moving its entire cache to a larger contiguous region.
The final block may be only partly full. That small amount of internal waste is usually much smaller than the unused capacity caused by reserving a large contiguous region.
Sharing and Scheduling Benefits
Block-based storage also makes sharing possible. If two requests have the same prompt prefix, they can refer to the same read-only blocks. When one request needs to modify a shared block, the system can copy it first. This is called copy-on-write.
PagedAttention does not, by itself, make the model compute faster for one isolated request. Its main benefit is better memory utilization under concurrent workloads. More usable KV-cache memory allows vLLM's scheduler to keep more sequences active, which raises throughput.
| Allocation strategy | Main risk | Effect under mixed request lengths |
|---|---|---|
| Contiguous cache per request | Fragmentation and over-reservation | Lower concurrency |
| Fixed-size paged blocks | Small partial-block waste | More predictable utilization |
Common Errors
Do not assume that setting a larger context limit makes every request use that much memory immediately. The cache grows as tokens are processed, but the configured limit affects the maximum possible allocation.
Do not compare PagedAttention with quantization as if they were alternatives. Quantization reduces the size of weights or cache values. PagedAttention changes how cache memory is allocated and shared. A deployment can use both.
Exercise
Why can paged allocation admit a new request when contiguous allocation cannot, even if both report similar free memory?
Show answer
Paged allocation can use many separated free blocks, while contiguous allocation needs one sufficiently large adjacent region.