Paged Attention and vLLM
Paged Attention is a memory management algorithm for large language model inference that partitions the KV cache into fixed-size, non-contiguous physical memory blocks—analogous to virtual memory paging in operating systems—enabling near-zero KV cache fragmentation and dynamic memory allocation across concurrent requests. vLLM is the open-source serving system built atop this primitive, achieving throughput improvements of 2–4× over prior systems such as FasterTransformer and Orca by eliminating the static, pre-allocated memory waste that plagued earlier LLM serving stacks. The counterintuitive fact: the dominant bottleneck in LLM serving is not compute (GPU FLOPs) but memory bandwidth and fragmentation of the KV cache, meaning that an OS-level insight—virtual memory—unlocks more practical throughput than most architectural innovations.
Every autoregressive generation step requires reading the full KV cache for all active tokens, so the memory layout of that cache determines serving throughput more than almost any other factor. Prior systems over-provision contiguous GPU memory per sequence—wasting 60–80% of KV cache capacity to internal and external fragmentation—because they had no mechanism to share or reuse non-contiguous blocks. Paged Attention decouples the logical KV cache (contiguous from the model's perspective) from the physical KV cache (arbitrary block addresses on device), exactly as virtual memory decouples process address spaces from DRAM pages. The "aha moment": the GPU memory manager can now handle a dynamic, unpredictable workload of variable-length sequences without any static worst-case reservation.
How It Works
KV Cache Fundamentals
During autoregressive decoding of a transformer, each layer maintains key and value projections for all previously generated tokens. For a sequence of current length , layer , head , the key cache is:
Explanation: the key cache for one head at one layer is simply the concatenation of per-token key vectors, growing by one row at every generation step. Here where is the number of attention heads. The memory footprint per token across all layers and heads is:
Explanation: factor of 2 accounts for both keys and values, layers and heads each contribute elements per token. For LLaMA-2 70B in FP16: , , , giving bytes MB per token. A batch of 256 sequences each generating 512 tokens consumes GB—exceeding a single A100 node.
Block Table and Logical-to-Physical Mapping
Paged Attention partitions the physical KV cache into blocks of fixed size tokens (typically ). Let denote the pool of physical blocks. Each sequence maintains a block table mapping logical block index to a physical block:
Explanation: the block table is a level of indirection—the attention kernel uses to fetch the right physical memory addresses regardless of where they reside. The attention computation for query at decode step with Paged Attention becomes:
Explanation: the query attends over keys gathered from non-contiguous physical blocks via the block table, with the gather operation fused into the attention kernel to avoid materializing a contiguous buffer.
Memory Efficiency: Fragmentation Analysis
Define memory utilization as the fraction of allocated KV cache memory actually storing useful token data. For a system with sequences and per-sequence max-length reservation , actual mean length :
Explanation: paged allocation wastes at most slots in the final block per sequence, so for long sequences and small , fragmentation vanishes to near zero. This gap——directly multiplies the number of concurrent sequences that fit in GPU VRAM, explaining the throughput gains.
Continuous Batching and Scheduler
vLLM implements continuous batching (also called iteration-level scheduling, Orca 2022), where the scheduler makes a new batching decision at every forward pass iteration rather than at request granularity. Combined with Paged Attention, the scheduler can: 1. Preempt a low-priority sequence by evicting its physical blocks to CPU (block-granularity swap). 2. Resume it later by swapping blocks back, incurring only the cost of the evicted blocks rather than the full sequence. 3. Share blocks across sequences with a common prefix (copy-on-write semantics for prefix caching). The copy-on-write rule: physical block is shared by sequences ; a write (new token appended) to by sequence triggers allocation of fresh block , copy of , and update .
Paged Attention CUDA Kernel
The paged attention kernel (v1/v2, Kwon et al. 2023) partitions each sequence's key-value blocks across thread blocks. For each query token , a thread block processes one physical KV block, accumulating a partial softmax via the log-sum-exp trick (identical in spirit to FlashAttention's tiling). Thread block computes:
Explanation: each thread block independently computes a numerically stable partial attention output; a reduction pass then combines tuples across blocks using the online softmax merge formula. The global output merges partial results from blocks:
Explanation: this is the standard parallel prefix-softmax reduction—exponentially re-weighted partial sums yield the exact same result as a sequential softmax over the full sequence.
Woosuk Kwon is a PhD student at UC Berkeley (RISE Lab) whose 2023 SOSP paper "Efficient Memory Management for Large Language Model Serving with PagedAttention" introduced the paged attention algorithm and the vLLM system. His key insight was drawing a direct formal analogy between OS virtual memory management and LLM KV cache management, borrowing both the block table abstraction and copy-on-write semantics wholesale. A surprising fact: the paper was submitted and accepted at SOSP—the premier systems conference—rather than a machine learning venue, reflecting vLLM's identity as a systems contribution first and an ML one second.
Zhuohan Li is a PhD student at UC Berkeley who co-authored vLLM and previously built Alpa (2022), a system for automated inter/intra-operator parallelism for large model training. His contribution to vLLM included the continuous batching scheduler design and the multi-GPU tensor-parallel execution runtime. Remarkably, the vLLM GitHub repository accumulated over 20,000 stars within three months of its June 2023 release, driven almost entirely by organic practitioner adoption before any major corporate endorsement.
Intuition
Think of GPU memory as a hotel, and each LLM request