Rúben GalvãoThe Rabbit Hole

The Rabbit Hole / AI and machine learning / Language models / Remembering context efficiently

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.

Memory reserved for the longest chat against what a short chat usesA wide bar shows the memory reserved for a chat of the model's longest context. A thin sliver at its left end shows the memory a chat of 500 tokens fills.
What to notice: A slab sized for the longest chat is almost empty when the chat is short. The green sliver is what a 500-token chat actually uses.

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.

Paged attention: block tables map each request onto scattered blocks of memoryRequest A has three logical blocks and request B has two. A block table sends each to a physical block of GPU memory. A's first block and B's first block hold the same prompt, so both point to the same physical block, which is stored once.Request ARequest BA0to 3A1to 0A2to 2B0to 3B1to 5block table: logical block to physical blockA10free1A22A0=B03free4B15free6free7shared by 2GPU memory: physical blocks of 16 tokens
What to notice: Follow each request through its block table to physical memory. Blocks can be scattered; the green prefix block is shared. No full-length slab is reserved.

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.

Reserve the maximum, or hand out blocks

Interactive

Tick chats · same starts, tokens and finishes

Reserve the maximum: every request gets a slab for 1,024 tokens

Unused reserved
Served
Running

Paged: reserve another block when it is needed

Dark blocks are reserved but unused. Solid colour shows stored tokens. Grey blocks are available in the pool for reuse.

Unused reserved
Served
Running

Press Play.

Illustrative simulation: 28 requests and 4,096 tokens of memory.

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

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.

← Browse all subjectsBack to the beginning ↑