Efficient Memory Management for Large Language Model Serving with PagedAttention
TL;DR. LLM serving is limited by KV-cache memory. Earlier systems reserved one contiguous chunk per request, sized for the maximum length, and wasted most of it. vLLM splits each sequence’s cache into small fixed-size blocks that can live anywhere in GPU memory, tracked by a per-sequence block table, just as an operating system pages virtual memory. Waste falls to under one block per sequence, blocks can be shared copy-on-write, and throughput rises 2–4×.
Why it matters now. vLLM is the default open-source engine for generating RL rollouts, running RAG readers and serving LLM judges in research code. If you train a search agent with GRPO, run a reranker over thousands of queries, or evaluate with an LLM judge, the memory model on this page sets how many sequences you can run at once.
Key idea
Section titled “Key idea”Split the KV cache of each sequence into blocks of tokens, , and compute attention block by block:
The result is exactly standard attention. What changes is memory management:
- A block table per sequence maps logical blocks to physical blocks and records how many slots are filled. A new physical block is allocated only when the last one is full.
- Waste per sequence is at most slots, all in the last block.
- Physical blocks carry a reference count. Parallel samples and beam candidates share prompt blocks; a block is copied only when a sequence needs to write into a shared one (copy-on-write).
- When memory runs out, the scheduler preempts whole sequences (all-or-nothing) and either swaps their blocks to CPU memory or recomputes them later.
Key results
Section titled “Key results”- Basic sampling on ShareGPT (§6.2): 1.7–2.7× higher sustainable request rate than Orca (Oracle), 2.7–8× than Orca (Max), and up to 22× than FasterTransformer at similar latency. For OPT-13B, vLLM batches 2.2× more requests than Orca (Oracle).
- Sharing (§6.3, OPT-13B on Alpaca): 6.1–9.8% memory saved for parallel sampling and 37.6–55.2% for beam search; on ShareGPT 16.2–30.5% and 44.3–66.3%.
- Shared prefix (§6.4): 1.67× higher throughput than Orca (Oracle) with a one-shot prefix and 3.58× with a five-shot prefix.
- Cost (§7.1): attention kernel latency is 20–26% higher than FasterTransformer’s because of block-table lookups.
- Preemption (§7.3): recomputation overhead is never more than 20% of swapping’s latency; the two are similar for block sizes 16–64.
Limitations & open questions
Section titled “Limitations & open questions”- The gain needs a memory-bound workload. With large memory and short sequences (OPT-175B on Alpaca), the system becomes compute-bound and the advantage shrinks.
- Paging adds indirection that compute-bound workloads, or training with static shapes, do not need (§8).
- The Orca baselines are re-implementations, and Orca (Oracle) assumes known output lengths.
- Only identical prefixes can share KV. Reusing the cache of a passage that appears at different positions in different RAG prompts is still open.
Connections
Section titled “Connections”- Alongside: GQA shrinks bytes per token, which multiplies with paging’s lower waste; speculative decoding reduces full-model passes per token; FlashAttention handles prefill.
- RL and agents: rollout generation for PPO-style and group-sampling RL is a parallel-sampling workload that benefits from block sharing.
- IR link: the question of reusing passage KV across queries mirrors independent versus contextual chunk encoding in contextual retrieval and late chunking.
- Evaluation: RULER itself ran its 17 models with vLLM (RULER).
Questions to think through
These questions have no single right answer. Write or say your answer before you open the hints. Mark a question once you could defend your answer in a reading group.
- Derive / compute
OPT-13B needs 800 KB of KV cache per token, and Table 1 gives 12 GB of KV memory (15.7K slots) on one A100. A request has a 200-token prompt and produces 300 tokens. Compare a system that reserves the 2048-token maximum per request with PagedAttention at block size 16: slots used, slots wasted, bytes wasted per request, and how many such requests fit at once.
What a strong answer covers
- Check the 800 KB: 2 (K, V) × 5120 hidden × 40 layers × 2 bytes = 819,200 B ≈ 800 KiB. 15.7K × 800 KiB ≈ 12 GiB, consistent with Table 1.
- Max reservation: 2048 slots reserved, 500 used, 1548 wasted (76%). That is 1548 × 800 KiB ≈ 1.2 GiB of idle memory per request. Only ⌊15.7K / 2048⌋ = 7 requests fit.
- Paged, B = 16: ⌈500/16⌉ = 32 blocks = 512 slots, 12 wasted (2.3%). Waste is at most B − 1 = 15 slots, only in the last block. About ⌊15.7K / 512⌋ = 30 requests fit.
- Batch size rises about 4×, and since decoding is memory-bound, throughput rises with it until compute becomes the limit.
- Paged blocks are also allocated on demand: at the start the request holds only ⌈200/16⌉ = 13 blocks, so even more requests can start than the final count suggests, at the risk of preemption later.
- Mechanism
Write the blockwise attention that PagedAttention computes for one query token, and explain how the kernel finds keys stored in non-contiguous physical blocks. Why is the extra indirection worth its 20–26% higher attention-kernel latency?
What a strong answer covers
- Split keys and values into blocks K_j, V_j of B tokens. Scores A_ij = exp(q·K_j / √d) / Σ_t exp(q·K_t / √d) are computed per block, and o = Σ_j V_j A_ijᵀ. The math is identical to ordinary attention.
- Each sequence has a block table mapping logical block j to a physical block id plus a filled count. The kernel reads the table, then fetches each physical block; within a block the B tokens are contiguous, which keeps reads coalesced.
- The overhead comes from table lookups, extra branches and variable lengths (Fig. 18a, compared with FasterTransformer).
- It only slows the attention operator. The memory saved lets the scheduler batch several times more requests, and decoding throughput scales with batch size, so the end-to-end gain is 2–4×.
- What if…
Section 7.2 sweeps the block size and §7.3 compares swapping and recomputation for preempted sequences. Explain the trade-off on each side of the block-size choice, why the best size differs between the ShareGPT and Alpaca traces, and why recomputation wins at small block sizes.
What a strong answer covers
- Too small: each block gives too little parallel work to the kernel, so the GPU is under-used while reading the cache.
- Too large: internal fragmentation grows (up to B − 1 slots per sequence) and fewer blocks can be shared.
- ShareGPT has long sequences, so 16–128 all work well. Alpaca has short ones, so large blocks are mostly empty and hurt; 16–32 work. vLLM defaults to 16.
- Swapping moves blocks over PCIe; small blocks mean many tiny transfers and poor bandwidth. Recomputation re-runs the prompt plus generated tokens as one prefill and does not depend on block size; its overhead never exceeded 20% of swapping's latency, and the two are similar for 16–64.
- Critique
vLLM's headline gains are measured against FasterTransformer and three Orca variants that the authors re-implemented, on ShareGPT and Alpaca traces. Critique what this setup can and cannot show, and propose an experiment that would separate the gain from paging from the gain from scheduling.
What a strong answer covers
- Orca is not public, so all three versions are the authors' implementation with an assumed buddy allocator. Orca (Oracle) knows true output lengths, an upper bound no real system reaches.
- The FasterTransformer gap (up to 22×) mixes memory management with its coarse request-level scheduling, so it overstates the paging effect.
- On OPT-175B with Alpaca the advantage over Orca (Oracle) shrinks because there is enough memory and the system becomes compute-bound. The gain depends on the workload being memory-bound.
- Experiment: keep the iteration-level scheduler fixed and swap only the allocator (contiguous max, contiguous oracle, paged with B = 16). Report throughput at matched latency across output-length distributions, including long RAG prompts with short answers.
- Design
You train a search agent with GRPO: each prompt (system prompt plus 4K tokens of retrieved passages) is sampled G = 8 times, and each sample adds about 1K tokens. Estimate the KV memory saved by block sharing, explain what copy-on-write does at the first divergent token, and name a case where sharing breaks down.
What a strong answer covers
- Without sharing: 8 × (4K + 1K) = 40K token slots. With the prompt blocks shared: 4K + 8 × 1K = 12K slots, a 70% saving. This is the parallel-sampling case that vLLM's block sharing targets.
- The shared prompt blocks have reference count 8. If the last prompt block is only partly full, the first sample to write into it gets a fresh copy (copy-on-write) and the count drops; full blocks are never copied.
- Once the agent issues a tool call and appends different retrieved text in each sample, the trajectories diverge and only the original prefix stays shared.
- Sharing needs an identical token prefix. The same passage at a different position or after different text has different KV values, so it cannot be reused across queries.
- Measure peak KV blocks, rollouts per second and preemption counts; preemption means the sharing estimate was too optimistic.
- Research idea
In RAG, the same popular passages appear in many prompts, but at different positions and after different preceding text, so vLLM-style prefix sharing cannot reuse their KV cache. State precisely why, and propose a research direction for reusing passage KV across queries, including how you would test that answer quality is not harmed.
What a strong answer covers
- A token's keys and values depend on all earlier tokens through attention in every lower layer, and on its absolute position through the positional encoding. Only an identical prefix gives identical KV.
- Direction: encode each passage independently (or with a fixed short header), cache its KV, and concatenate at query time, with some fix-up for positions and cross-passage attention, such as recomputing a few tokens or fine-tuning the reader to accept independently encoded chunks.
- This is close to the IR question of contextual versus independent chunk encodings: the contextual-retrieval and late-chunking pages show what is lost when chunks ignore their surroundings.
- Test: compare full recomputation against cached-and-concatenated KV on multi-document QA with the gold passage at varied positions, and report accuracy, time to first token and memory. Lost-in-the-middle-style position sweeps catch subtle damage.