PagedAttention: managing the KV cache like virtual memory
Some chats stay short; others run on for pages. How does a server make room for both without leaving most of its memory empty?
A server is about to start a chat. The cache will grow by one entry per token, but nobody knows how long the chat will be.
Where does it put the cache?
Reserve room for the longest chat
The safe answer is one unbroken slab, big enough for the longest chat the model allows.
Most chats stop long before that. The unused part of the slab sits empty, and no other chat can use it.
In the PagedAttention paper, the servers it compared against filled only about 20 to 38 percent of their cache memory with real keys and values. The rest was reserved but empty, or split into gaps too small to use: fragmentation.
Operating systems solved this long ago
An operating system does not give a program one huge slab. It hands out small pages as the program needs them, and keeps a table that says where each page lives. The program still sees one continuous memory.
Do the same for the cache. Cut it into small blocks of a few tokens, and give each chat a block table that lists where its blocks are.
Follow a chat through its table. Its blocks can be scattered, and a new block is taken only when the chat needs one. When it ends, its blocks go straight back.
Back to the memory pool, ready for another chat. Returning a block makes it available again without releasing its backing memory.
Watch it happen
Press Play. The same chats start, grow and finish together in both layouts. Only the way they take up memory changes.
At most one block per chat is ever partly empty: its last. The paper reports waste under 4 percent.
Share the start
Two chats that begin with the same prompt can point their tables at the same blocks, so a shared system prompt is stored once. If one chat has to change a shared block, it gets its own copy first. That is copy-on-write, and it is the green block above.
Paging does not shrink the keys and values. It lets a server fit more chats into the memory it has.
A window asks a different question: can we stop keeping some entries at all? Next: sliding-window attention.
Sources and assumptions
- Kwon and colleagues: Efficient Memory Management for Large Language Model Serving with PagedAttention (2023), the paper that introduced PagedAttention and vLLM. It reports the 20 to 38 percent use of cache memory in earlier systems, and waste under 4 percent with paging.
- vLLM’s explanation of PagedAttention, for the reported 60 to 80 percent waste from reservation and fragmentation in its example comparison.
- TensorRT-LLM paged cache documentation, for a second implementation. Block sizes, kernels and sharing policies differ between serving systems; check the backend you use.
The reservation is a deliberately extreme policy chosen to make the mismatch easy to see; not every server allocates like that. The reservation and the 500-token example use the selected model’s cache at 16 bits per number and its longest context. Models that cache a compressed latent use that. The simulated requests in the memory widget are illustrative: 28 requests with a fixed random seed, 4,096 tokens of memory and replies of up to 1,024 tokens, so the pattern shows in seconds. The percentages there are not the paper’s. Waste depends on block size and request lengths; a nearly empty last block can waste far more than 4 percent of one short request’s allocation.
PagedAttention adapts paging’s indirection and sharing to GPU attention; it does not mean attention is serviced through ordinary operating-system page faults.