Efficient Memory Management for Large Language Model Serving with PagedAttention
Kwon et al., 2023, Efficient Memory Management for Large Language Model Serving with PagedAttention (Proceedings of the 29th Symposium on Operating Systems Principles (SOSP ’23))
Subscriber deck — free during early access
15 cards
1 Introduction
- What limits the throughput of LLM serving, according to Kwon et al.?How many requests can be batched, which is bounded by GPU memory for the KV cache. Existing systems wasted most of it: only 20–38% of KV cache memory held actual token states.
- Kwon et al., 2023, Efficient Memory Management for Large Language Model Serving with PagedAttention (Proceedings of the 29th Symposium on Operating Systems Principles (SOSP ’23)), §1
- Why did existing serving systems waste KV cache memory?They stored each request’s KV cache contiguously and pre-allocated a chunk for the maximum possible length, because the output length is unknown. This causes reserved-but-unused slots, internal fragmentation (requests end shorter than reserved) and external fragmentation between chunks of different sizes, and prevents sharing between sequences.
- Kwon et al., 2023, Efficient Memory Management for Large Language Model Serving with PagedAttention (Proceedings of the 29th Symposium on Operating Systems Principles (SOSP ’23)), §1
- What is PagedAttention?An attention algorithm that lets a sequence’s keys and values live in non-contiguous memory: the KV cache is split into fixed-size blocks, like pages in an operating system’s virtual memory (blocks as pages, tokens as bytes, requests as processes), allocated on demand.
- Kwon et al., 2023, Efficient Memory Management for Large Language Model Serving with PagedAttention (Proceedings of the 29th Symposium on Operating Systems Principles (SOSP ’23)), §1
2.2 LLM Service & Autoregressive Generation
- What are the two phases of LLM generation, and how do they differ computationally?The prompt phase processes all prompt tokens at once (parallel matrix–matrix work, GPU-efficient) and builds their KV cache. The generation phase produces one token per iteration using the cached keys and values (matrix–vector work), which is memory-bound and dominates latency.
- Kwon et al., 2023, Efficient Memory Management for Large Language Model Serving with PagedAttention (Proceedings of the 29th Symposium on Operating Systems Principles (SOSP ’23)), §2.2
3 Memory Challenges in LLM Serving
- How large is the KV cache, for example for OPT-13B?Per token: 2 (key and value) × 5,120 (hidden size) × 40 (layers) × 2 bytes (FP16) = 800 KB; a 2,048-token sequence needs up to 1.6 GB, so even a whole GPU fits only a few tens of requests. GPU compute also grows faster than memory capacity.
- Kwon et al., 2023, Efficient Memory Management for Large Language Model Serving with PagedAttention (Proceedings of the 29th Symposium on Operating Systems Principles (SOSP ’23)), §3
4.1 PagedAttention
- How is attention computed over blocks in PagedAttention?With block size \(B\), key block \(K_j\) holds keys of tokens \((j-1)B+1 \ldots jB\), likewise \(V_j\). The kernel fetches each block wherever it is stored, computes the query’s attention scores against \(K_j\) and combines them with \(V_j\); the result is identical to ordinary attention.
- Kwon et al., 2023, Efficient Memory Management for Large Language Model Serving with PagedAttention (Proceedings of the 29th Symposium on Operating Systems Principles (SOSP ’23)), §4.1
4.2 KV Cache Manager
- How does vLLM map a request’s KV cache to GPU memory?Each request has logical KV blocks filled left to right; a block table maps them to physical blocks anywhere in GPU memory and records how many slots are filled. A new physical block is allocated only when the last one is full, so waste per request is at most one partly filled block.
A block table maps each sample’s logical KV blocks to scattered physical blocks; shared blocks carry a reference count - Kwon et al., 2023, Efficient Memory Management for Large Language Model Serving with PagedAttention (Proceedings of the 29th Symposium on Operating Systems Principles (SOSP ’23)), §4.2
4.4 Application to Other Decoding Scenarios
- How does vLLM share memory in parallel sampling?Samples from the same prompt map their prompt blocks to the same physical blocks with a reference count. When a sample needs to write into a shared block, copy-on-write gives it its own copy, as when an operating system forks a process.
- Kwon et al., 2023, Efficient Memory Management for Large Language Model Serving with PagedAttention (Proceedings of the 29th Symposium on Operating Systems Principles (SOSP ’23)), §4.4
- Why does beam search benefit even more from PagedAttention?Beam candidates share not only the prompt but also long common prefixes, and the sharing pattern changes each step like a process tree; blocks whose reference count drops to zero are freed. Previous systems had to copy large parts of the KV cache between candidates. Memory savings reached 37–55% for beam search vs 6–10% for parallel sampling on Alpaca.
- Kwon et al., 2023, Efficient Memory Management for Large Language Model Serving with PagedAttention (Proceedings of the 29th Symposium on Operating Systems Principles (SOSP ’23)), §4.4
- How can vLLM speed up requests that share a common prefix such as a system prompt or few-shot examples?The provider can keep the prefix’s KV cache in reserved physical blocks, like a shared library across processes; each request maps its first logical blocks to them (last block copy-on-write), so only the request-specific part of the prompt is computed.
- Kwon et al., 2023, Efficient Memory Management for Large Language Model Serving with PagedAttention (Proceedings of the 29th Symposium on Operating Systems Principles (SOSP ’23)), §4.4
4.5 Scheduling and Preemption
- What does vLLM do when GPU memory runs out?With first-come-first-served scheduling, it preempts the latest requests, evicting all blocks of a sequence group at once (all-or-nothing, since they are always used together), and later recovers them by swapping blocks back from CPU memory or by recomputing the KV cache from prompt plus generated tokens.
- Kwon et al., 2023, Efficient Memory Management for Large Language Model Serving with PagedAttention (Proceedings of the 29th Symposium on Operating Systems Principles (SOSP ’23)), §4.5
6.2 Basic Sampling
- How much did vLLM improve serving throughput?At similar latency it sustained 2–4× the request rate of state-of-the-art systems such as Orca and up to 22× that of FasterTransformer; e.g. on ShareGPT with OPT-13B it batched 2.2× more requests than Orca with oracle knowledge of output lengths. Gains were largest for long sequences, large models and complex decoding.
- Kwon et al., 2023, Efficient Memory Management for Large Language Model Serving with PagedAttention (Proceedings of the 29th Symposium on Operating Systems Principles (SOSP ’23)), §6.2
7.1 Kernel Microbenchmark
- What does PagedAttention cost at the kernel level?Accessing block tables, extra branches and variable sequence lengths made the attention kernel 20–26% slower than FasterTransformer’s, but this affects only the attention operator, and end-to-end throughput is far higher.
- Kwon et al., 2023, Efficient Memory Management for Large Language Model Serving with PagedAttention (Proceedings of the 29th Symposium on Operating Systems Principles (SOSP ’23)), §7.1
7.2 Impact of Block Size
- How should the KV block size be chosen?Too small and the kernel can’t exploit GPU parallelism; too large and internal fragmentation grows and sharing becomes less likely. Block size 16 worked well across workloads and is vLLM’s default.
- Kwon et al., 2023, Efficient Memory Management for Large Language Model Serving with PagedAttention (Proceedings of the 29th Symposium on Operating Systems Principles (SOSP ’23)), §7.2
7.3 Comparing Recomputation and Swapping
- When is recomputation better than swapping for recovering evicted KV blocks?Swapping suffers with small blocks, because many small CPU–GPU transfers underuse PCIe bandwidth; recomputation’s cost is independent of block size. Recomputation wins for small blocks, swapping for large ones; for 16–64 they perform similarly.
- Kwon et al., 2023, Efficient Memory Management for Large Language Model Serving with PagedAttention (Proceedings of the 29th Symposium on Operating Systems Principles (SOSP ’23)), §7.3
Source
- Woosuk Kwon, Zhuohan Li, Siyuan Zhuang, Ying Sheng, Lianmin Zheng, Cody Hao Yu, Joseph E. Gonzalez, Hao Zhang and Ion Stoica. 2023. Efficient Memory Management for Large Language Model Serving with PagedAttention. In Proceedings of the 29th Symposium on Operating Systems Principles (SOSP ’23). doi:10.1145/3600006.3613165 https://arxiv.org/abs/2309.06180