Inference Engineering

Lesson 7 of 14

Paged KV memory

When a chatbot server says its memory is full, most of that memory can be sitting empty, booked for replies that are never written. This lesson shows how vLLM fixed it by handing memory out in small boxes, one at a time, so almost every booked slot ends up holding something.

The same shelf, booked two ways

One shot, with real numbers for Llama 3.1 8B on one 80 GB NVIDIA H100. The shelf is to scale along its length; in the first stage a booking's height shows how much of it is filled. It plays by itself; press a stage to replay it, or drag to look around.

01 · Booked ahead

Room for the longest reply

A chat’s notes, its KV cache from lesson 4, grow by one token with every token the model writes, and nobody knows when a reply will stop. Early servers played safe. Each chat got one unbroken stretch of GPU memory, big enough for its question plus the longest reply it was allowed, booked the moment it arrived.

It had to be one piece because the program reading the notes found each one by counting from the start of the stretch. Most replies stop far short of the limit, so most of each stretch stays empty. To the server, booked counts as full.

Eight chats, each booked 2,048 slots

  • Slots holding a token's notes
  • Booked, still empty
  • Held in boxes, still empty

02 · Stranded gaps

Free, but in the wrong place

When a chat ends, its stretch is freed and leaves a gap between its neighbours. A new chat needs one stretch of its full size, so two gaps that add up to enough are still no use if neither is big enough alone.

Enough room in total, none in one piece

  • Holding a token (one colour per chat)
  • Booked, still empty
  • Free

03 · Boxes

Boxes and a list

PagedAttention, from the vLLM project in 2023, cuts all of memory into identical small boxes, each holding the notes of 16 tokens. A chat starts with just enough boxes for its question and takes one more, from anywhere, whenever its last box fills. Its block table lists its boxes in order, and the model follows that list to read the notes.

A chat now leaves empty at most part of its last box, and any free box suits any chat. Looking up each box makes reading the notes measurably slower; fitting more chats more than repays it.

Reading chat B's notes through its block table

GPU memory, 16 boxes of 16 tokens

  • Chat B's boxes
  • Other chats' boxes
  • Free

Chat B's block table

EntryBoxTokens

04 · What it buys

3.6 times as many chats

Take 8 chats caught mid-reply, each booked 2,048 slots. Only 27% of the slots they hold contain a token; with boxes of 16, 99% do. On one 80 GB H100 running Llama 3.1 8B, that is 759 chats at once instead of 208, counting memory alone.

Paging stops waste; it does not add memory. If boxes run out mid-reply, the server pauses the newest chat and frees its boxes, to rebuild its notes later.

One real GPU: how many chats fit

Llama 3.1 8B on one NVIDIA H100. Plays by itself; move a slider to take over.

  • The model
  • Notes the chats hold
  • Booked, empty
  • Kept free (10%)

05 · Sharing

Two answers, one question

A block table is only a list of box numbers, so two chats can list the same box. Ask for two answers to one question and both point at the question’s boxes, stored once. When one of them needs to write into a shared box, it first gets its own copy: copy-on-write.

Two answers sharing one question's boxes

Boxes in memory, 4 tokens each

The vLLM paper's own example: a 7-token question, boxes of 4 tokens, two answers. Only the partly filled box is ever copied, and only once.

06 · Playground

Run both ways yourself

Send chats and run decode steps; the left side books stretches, the right hands out boxes. Try boxes of 4 and of 32, and pick a chat to see its block table.

Booked stretches versus boxes, step by step

Booked ahead, one stretch each

Paged, one box at a time

  • token stored
  • booked, not yet written
  • booked, never used
  • free, but no gap fits the next chat

Running chats. Pick one, or hover or tap a cell, to see its block table.

Each cell is one token slot; colours tell chats apart. Left: each chat owns one outlined stretch sized for its longest reply. Lightly shaded cells will receive later tokens; hatched cells never will, and hatched free space is too broken up for the chat waiting at the head of the queue. Right: outlined boxes, handed out from a shuffled free list.

07 · Glossary

The vocabulary, in one place

Token
The unit a model reads and writes: a piece of a word, about three-quarters of one on average.
KV cache
The notes a model keeps on every token of a conversation so it never reprocesses them: 131 KB per token for Llama 3.1 8B (lesson 4).
GPU memory
The chip's own storage, 80 GB on an H100. The model and every conversation's notes share it; the server keeps 10% free as a safety margin.
Slot
Room for one token's notes, across all of the model's layers.
Booking ahead
The old way: each chat gets one unbroken stretch of slots, sized for its question plus the longest reply it may write, held until it ends.
Stranded gap
Free memory between two stretches that is too short for the next chat. Engineers call it external fragmentation.
Box (block)
A fixed-size piece of memory holding the notes of 16 tokens, vLLM's default. Any free box suits any chat.
Block table
A chat's list of its boxes, in order. The model follows it to read the notes, so the boxes can sit anywhere.
Copy-on-write
Chats share a box until one of them needs to write into it; that chat first gets its own copy.
Preemption
When boxes run out mid-reply, the server pauses the newest chat and frees its boxes, to rebuild its notes later.

You can now

  • Explain why booking each chat's maximum length (question plus longest reply) leaves GPU memory full but mostly empty, and name the three kinds of waste.
  • Compute how many sequences fit on a GPU under contiguous reservation and under paging with a given block size, and the worst-case waste per sequence (block size minus one slots).
  • Decide on a block size by weighing last-block waste against kernel efficiency, and explain how a block table lets a sequence's blocks sit anywhere in memory and lets parallel samples share a prompt through copy-on-write.

Open the workbook: the formulas, the exact numbers, graded exercises and the sources behind this page.