How an Old Computer Memory Trick Lets One GPU Serve More AI Users (PagedAttention)
Every token (a chunk of text, about three quarters of a word) your model generates leaves a trail. The key-value cache, or KV cache, stores the keys and values (lists of numbers that let the model look back at earlier tokens) the model computed for every token so far, so it does not recompute them when it generates the next token. Without it, inference, the work of producing an answer from a trained model, would be impossibly slow. With it, memory becomes the bottleneck.
New to these terms? Every word is explained in the series glossary.
The arithmetic is simple. Take Llama-3-8B, an open model from Meta: 32 layers (stages in the model's stack), 8 key-value heads, 128 dimensions per head (the head dimension), 2 bytes per value in half precision (16-bit numbers). Each token costs 2 times 32 times 8 times 128 times 2 bytes, which is 131,072 bytes. At 4,096 tokens that is about 0.54 GB. At 32,768 tokens it is 4.3 GB. At 131,072 tokens it is 17.2 GB. That is for one request. Serve a hundred requests at once (answer a hundred users together) and the cache eats the whole GPU, the graphics chip that runs the model.
The waste is worse than the size. Naive serving systems reserve one contiguous block of memory (one unbroken stretch) sized for the maximum sequence length, the number of tokens in a request, even though most requests never get that long. The unused space sits idle. When requests finish at different times, the free space fragments into pieces too small to reuse. And when many requests share the same system prompt, the hidden instructions an app sends first, each one stores its own copy of the same keys and values.
PagedAttention fixes this by borrowing an idea from operating systems, the base software such as Windows, macOS or Linux that shares a computer between programs. An OS does not give a process (a running program) one big contiguous chunk of RAM, the computer's main memory. It splits memory into fixed-size pages and maps them through a page table, a lookup list of where each page sits. PagedAttention does the same for the KV cache. It splits the cache into fixed-size blocks, allocates them on demand as the sequence grows, and lets the blocks sit anywhere in memory. Nothing is reserved for tokens that do not exist yet. When two requests share a prefix, they share the same blocks, and a block is only copied when one of them writes to it. This is called copy on write.
Animation: Naive serving reserves a max length slab per request. PagedAttention hands out fixed size blocks only as tokens arrive.
vLLM, the serving system built on PagedAttention, reports near-zero waste in KV cache memory and flexible sharing within and across requests. In the paper's evaluations, vLLM improved throughput (work done per second) of popular LLMs (large language models) by 2 to 4 times with the same latency (delay) compared to FasterTransformer and Orca, two earlier serving systems. The improvement is more pronounced with longer sequences, which is exactly where the cache matters most.
The same idea extends further. Prefix caching reuses blocks for shared system prompts across requests. Chunked prefill splits long prompts (the text sent in) into pieces during prefill, the step where the model first reads the whole prompt, so a single request cannot starve the queue. Continuous batching swaps requests in and out at every decoding step, each time a new token is written. Together these are the production baseline for LLM serving now.
sequenceDiagram autonumber participant R as New request participant S as Scheduler participant B as Block manager participant G as GPU R->>S: Prompt arrives S->>B: Blocks for the prompt only B-->>S: Block table, blocks anywhere in memory S->>G: Prefill in chunks Note over S,G: Chunked prefill: one long prompt cannot starve the queue loop Every decoding step S->>G: Decode step for the whole batch G->>B: Block full, need one more B-->>G: Next free block, on demand end Note over B: Shared system prompt: same blocks, copy on write G-->>S: Request finished S->>B: Free its blocks S->>G: Swap a waiting request in Note over S: Continuous batching: swap in and out each step
Diagram: How a paged server handles a request: blocks on demand, chunked prefill, continuous batching.
My read is that the KV cache is the real serving bottleneck, not the model. The model is a fixed cost. The cache grows with every token and every user. Paging did not make the model smarter. It made the memory honest, and that was enough to multiply throughput.
LLM serving throughput vs state of the artData from the sources listed below.
Try this
First, run the KV cache arithmetic yourself in Python.
pythonlayers, kv_heads, head_dim, bytes_per = 32, 8, 128, 2 per_token = 2 * layers * kv_heads * head_dim * bytes_per for tokens in [4096, 32768, 131072]: print(tokens, 'tokens ->', round(per_token * tokens / 1e9, 3), 'GB')
Then run vLLM with prefix caching turned on. You need a GPU with about 16GB of VRAM, the memory on the graphics card. The commands below use git clone to download the code and pip install to install it. The Python then loads an Instruct model (one trained to follow instructions) and asks one question at temperature 0, the setting for no randomness.
bashgit clone https://github.com/vllm-project/vllm pip install vllm
pythonfrom vllm import LLM, SamplingParams llm = LLM(model='Qwen/Qwen2.5-7B-Instruct', enable_prefix_caching=True) out = llm.generate( ['What is paging in operating systems?'], SamplingParams(temperature=0, max_tokens=100), ) print(out[0].outputs[0].text)
