vLLM PagedAttention: Paging KV Cache to Slash LLM Inference Memory Waste by 70%
This article explains how vLLM's PagedAttention uses OS-style paging to eliminate KV Cache memory fragmentation in LLM inference, reducing waste from 70% to under 4%, enabling 2-4x throughput gains via copy-on-write sharing for parallel sampling and beam search.
Reading font
KV Cache Memory Waste in LLM Inference
LLMs generate text autoregressively, one token at a time. To avoid recomputing attention over all previous tokens, inference engines store the Key (K) and Value (V) projection vectors for every past token in GPU memory, forming the KV Cache. Traditional serving frameworks such as FasterTransformer and Orca require this cache to occupy contiguous memory. At request start, the system pre-allocates a single contiguous block sized for the maximum possible sequence length (e.g., 2,048 tokens), even though the actual output may be far shorter.
This design creates three major sources of waste:
Over-provisioning (Reservation Waste) : Space for 2,048 tokens is reserved even if only 50 tokens are generated.
Internal Fragmentation : When the sequence ends with an <eos> token, the remaining slots in the last allocated block stay unused.
External Fragmentation : Free GPU memory becomes scattered into small, non-contiguous regions that cannot accommodate a new request's contiguous allocation.
Traditional systems waste 60%–80% of usable KV Cache memory.
PagedAttention: OS-Style Paging for KV Cache
vLLM borrows the virtual memory paging concept from operating systems. A sequence's KV Cache is split into fixed-size KV Blocks (block size B, default 16 tokens). Each block stores keys and values for exactly B tokens. Physical blocks can be scattered across GPU DRAM. vLLM maintains a Block Table mapping logical block indices (L0, L1, ...) to physical block addresses (P7, P3, ...). The model accesses cache sequentially via logical blocks while the underlying physical addresses need not be contiguous.
Scenario 1: Single Sequence Generation (Autoregressive Loop)
[Logical Block L0] ---> Block Table (L0 -> P7) ---> [Physical Block P7 in DRAM]
[Logical Block L1] ---> Block Table (L1 -> P3) ---> [Physical Block P3 in DRAM]After receiving the prompt, vLLM runs the prefill phase. Example: prompt "Four score and seven years" (5 tokens), block size B=4.
vLLM creates logical block L0, stores tokens "Four", "score", "and", "seven", maps L0 to a randomly chosen physical block P7.
Remaining token "years" goes into logical block L1, mapped to physical block P3. L1 now uses 1/4 slots.
Decoding generates "ago"; vLLM writes it into the next free slot of L1/P3, occupancy becomes 2/4.
When L1 fills to 4/4, vLLM requests a new physical block (e.g., P11), maps it to logical block L2, and continues writing.
Result: Physical blocks are allocated on demand. Unused slots concentrate only in the last partially filled block. Memory waste drops from ~70% to under 4%.
Scenario 2: Parallel Sampling with Copy-on-Write (CoW)
A single prompt may produce multiple completions (e.g., a coding assistant returning three code suggestions). Traditional implementations duplicate the prompt's KV Cache in separate contiguous buffers.
PagedAttention lets different branches share undiverged cache blocks:
Two output branches map prompt logical blocks L0, L1 to the same physical blocks P7, P3.
Shared physical blocks have a reference count of 2.
When branch A and branch B generate different tokens, vLLM triggers block-level copy-on-write:
Copy shared block data to a newly allocated physical block (e.g., P3 → P12).
Decrement reference count of original P3.
Each branch now writes subsequent tokens to its own exclusive physical block.
This sharing saves up to 30% of KV memory during parallel generation.
Scenario 3: Beam Search with Pruning
Beam search maintains multiple candidate sequences (beams), pruning low-probability paths each decoding step. Standard implementations copy large memory regions to synchronize the surviving beams' KV Cache.
PagedAttention manages blocks like a dynamic process tree:
Beams sharing history map their logical blocks to the same physical blocks.
When a candidate is pruned, its logical blocks are destroyed and the corresponding physical blocks' reference counts decrement.
Physical blocks with reference count zero are immediately returned to the free GPU DRAM pool.
When a beam diverges, only the diverging block is copied (copy-on-write).
For beam search workloads, memory savings range from 37% to 55%.
Summary and Impact
By eliminating fragmentation, vLLM fits more requests into the same GPU memory. On identical hardware, the effective batch size (concurrent requests) increases up to 4×, yielding 2–4× higher overall inference throughput.
The author provides an interactive dashboard simulator covering single-sequence generation, parallel sampling, and beam search. It supports step-by-step execution to visualize block table updates and copy-on-write events.
Simulator repository: https://github.com/rahulprasanth487/PagedAttention
For questions about attribution, corrections or content rights, please contactand we will review it promptly.
Published
AI-assisted summary