PagedAttention: Mathematical Foundations, Virtual Memory Block Allocation, and Zero-Waste KV Cache Serving
In autoregressive transformer inference, serving throughput is bounded primarily by high-bandwidth memory (HBM) capacity and memory bandwidth rather than raw floating-point operations. As large language models generate tokens sequentially, each newly sampled token requires caching the key and value projection vectors across all preceding tokens in the sequence. In legacy serving engines, this key-value (KV) cache is allocated contiguously for the theoretical maximum sequence length, resulting in severe internal fragmentation, external fragmentation, and redundant memory duplication during complex decoding routines.
Introduced by Kwon et al. from UC Berkeley and formalized in the vLLM serving engine, PagedAttention adapts classic virtual memory and paging principles to the attention operator. By partitioning key and value tensors into fixed-capacity, non-contiguous physical blocks managed via dynamic page tables, PagedAttention reduces KV cache memory waste from over 60% down to under 4%, unlocking 2x to 4x throughput improvements on identical accelerator hardware.

1. The Memory Wall in Autoregressive Serving
Transformer inference operates in two distinct phases with radically different computational profiles:
- The Prefill (Prompt) Phase: The model processes the full prompt sequence in parallel. The self-attention matrix multiplication maps efficiently to tensor cores as dense compute-bound GEMM (General Matrix Multiply) operations.
- The Decode (Generation) Phase: The model generates tokens sequentially one token at a time. To evaluate the attention output at step , the query vector must be multiplied by all historical keys and values and .
Because each decoding step generates only one token per sequence, the matrix-vector multiplications (GEMV) exhibit low arithmetic intensity. GPU compute units spend most of their cycles waiting for weights and KV cache tensors to be loaded from high-bandwidth memory into fast on-chip SRAM.
KV Cache Memory Footprint
For a transformer model with layers, hidden dimension , and attention heads with head dimension , each token requires storing two vectors (key and value) per layer in half-precision format (16-bit / 2 bytes per parameter). The memory footprint required per token across all layers is:
For an OPT-13B model (), each token consumes:
For a sequence of length 2,048 tokens, the KV cache alone demands 1.64 GB of GPU memory. In a single NVIDIA A100 GPU with 40 GB of HBM, the static model weights in FP16 consume 26 GB, leaving only 12 to 14 GB for runtime activations and KV cache. Under this memory ceiling, a server can concurrently host fewer than 10 maximum-length sequences if memory is allocated statically.
2. Memory Inefficiencies in Contiguous Allocation
Prior to PagedAttention, deep learning serving systems such as FasterTransformer and early versions of Orca stored the KV cache for each sequence in contiguous physical GPU memory. Because output sequence lengths are non-deterministic and unknown prior to generation, legacy systems suffered from three major forms of memory waste:
Legacy Contiguous Pre-allocation (Severe Fragmentation):
+-------------------------------------------------------------------------+
| Prompt KV (Used) | Generated KV (Used) | Reserved for Max Length (Wasted) |
+-------------------------------------------------------------------------+
<------------------------ Static Pre-allocated Chunk -------------------->
PagedAttention Dynamic Block Allocation (Zero Waste):
Logical Blocks: [ Block 0 ] -> [ Block 1 ] -> [ Block 2 (Partial) ]
Physical Pages: Page #7 Page #1 Page #14 (Allocated on-demand)- Reserved Memory Waste: Systems pre-allocate memory buffers based on the requested maximum generation length (e.g., 2,048 or 4,096 tokens). Throughout early generation steps, the majority of the allocated buffer sits idle, preventing the scheduler from admitting new requests.
- Internal Fragmentation: When a sequence emits an end-of-sequence token (
<eos>) well before reaching , the unutilized portion of the pre-allocated contiguous chunk is permanently lost for that request lifetime. - External Fragmentation: Dynamic memory allocators (such as buddy allocators) create non-contiguous free memory holes between variable-sized contiguous requests, leaving chunks that are too small to satisfy new maximum-length allocations.
Empirical profiling of production workloads by Kwon et al. (2023) showed that legacy contiguous serving frameworks utilized only 20.4% to 38.2% of allocated KV cache memory for actual token states, wasting over 60% to 79% of GPU memory capacity.
3. Mathematical Formulation of PagedAttention
Standard multi-head attention computes output vector at sequence position via scaled dot-product attention over historical representations:
Block-Wise Partitioning
PagedAttention divides the sequence of key vectors and value vectors into discrete blocks of fixed token size (typically ).
Let and represent the -th key block and value block respectively:
For a sequence of length , the total number of blocks is . The attention score calculation is transformed into a block-wise dot product. The row vector of unnormalized attention logits for the -th block is:
The normalized attention weight vector across all historical blocks is computed via block-wise softmax normalization:
The final output vector is computed as the sum of matrix products across all physical value blocks:
Because and are fetched independently per block index , the physical memory addresses storing and do not need to be contiguous in GPU DRAM.
4. Virtual Memory Architecture and Block Tables
PagedAttention decouples logical sequence representation from physical GPU allocation by mirroring operating system paging architectures:
- Logical KV Blocks: A contiguous virtual index space representing the sequence tokens.
- Physical KV Blocks: Fixed-size page slots allocated in a pre-reserved pool of GPU DRAM.
- Block Table: A metadata lookup structure maintaining the logical-to-physical mapping, filled token counts, and physical block reference counters.
Request Sequence: "The transformer attention mechanism uses virtual memory paging"
Token Indices: [0 .. 3] [4 .. 7] [8 .. 11] [12 .. 15]
Logical Blocks: Block 0 Block 1 Block 2 Block 3 (Unallocated)
| | |
Block Table: [ Log 0 -> Phys 12, #filled=4 ]
[ Log 1 -> Phys 4, #filled=4 ]
[ Log 2 -> Phys 19, #filled=2 ]
| | |
Physical DRAM: Page #4 Page #12 Page #19 (2 slots free)Allocation Lifecycle
- Prompt Prefill: When a request arrives with prompt length , the manager computes the required blocks . It pulls free physical blocks from the free list and assigns them in the block table.
- Decoding Step: During step , the engine appends the newly computed key and value to the last allocated physical block.
- Block Overflow: When the last physical block reaches capacity (), the scheduler allocates a single new physical block from the free list and appends a new row to the request block table.
- Completion: Upon encountering
<eos>, all mapped physical blocks decrement their reference counters. Blocks with a reference count of 0 return immediately to the free page pool.
Internal fragmentation is bounded strictly to the final block of each active sequence, creating an average memory waste of less than tokens per sequence. With , the maximum waste per sequence is 15 token slots (under 12 KB in OPT-13B), eliminating external fragmentation entirely.
5. Advanced Memory Sharing: Branching and Prefix Caching
PagedAttention natively supports fine-grained memory sharing across sequences through block reference counting and copy-on-write (CoW) semantics.
Parallel Sampling
In scenarios where a single prompt generates independent completion samples (such as code generation or best-of- verification):
- The prompt prefill phase executes once, storing the prompt KV cache in shared physical blocks with reference count set to .
- All output sequence streams initialize their block tables pointing to the identical prompt physical blocks.
- During generation, each stream writes new tokens to its own private physical blocks.
- If a stream needs to write to a partially filled shared block, a CoW kernel copies the shared block into a new physical block, decrements the shared block reference count, and updates the stream block table.
Prompt Blocks: [ Phys 7 (Ref=2) ] -> [ Phys 1 (Ref=2) ]
/ \
Sample 1 (CoW on split): [ Phys 3 (Ref=1) ] [ Phys 1 (Ref=1) ] (Sample 2)Beam Search
Beam search continuously forks candidate hypotheses and prunes low-probability beams at each step. In legacy engines, pruning and branching required massive memory copies across GPU buffers.
In PagedAttention, beam candidate transitions update only block table pointers and reference counts:
- Discarded candidate beams decrement reference counts on their assigned physical blocks.
- Surviving candidate beams share historical prefix blocks without copying data.
- Memory copies occur strictly when branching within a shared block boundary, reducing memory movement overhead by over 90%.
System Prompt Prefix Sharing
For multi-tenant workloads with long invariant system instructions (e.g., 2,000-token enterprise system prompts), PagedAttention allows the system prompt KV blocks to be pre-computed once and pinned in physical memory. Incoming user queries map their initial logical blocks directly to the pinned physical blocks, eliminating redundant prefill computation and saving significant GPU memory across concurrent sessions.
6. Preemption and Memory Recovery Dynamics
When concurrent request demand exceeds physical GPU block capacity, the serving engine must preempt active requests to maintain execution stability without dropping queries. vLLM implements a First-Come-First-Serve (FCFS) gang-scheduling strategy using two recovery mechanisms:
| Strategy | Mechanism | Latency Impact | Bandwidth / Compute Cost | | :--- | :--- | :--- | :--- | | Swapping | Evicts physical blocks across PCIe from GPU HBM to Host CPU RAM. | Minimal TTFT penalty upon resume; bounded by PCIe bandwidth. | Memory bandwidth bounded (e.g., PCIe Gen4/Gen5 transfer rates). | | Recomputation | Discards physical blocks and recalculates KV cache from original prompt and generated tokens. | Recalculates all historical tokens in a single parallel prefill step. | Compute bounded; highly efficient on short-to-medium sequence lengths. |
Because transformer prefill operates at high compute intensity using GEMM operations, recomputing the KV cache for a 500-token sequence often executes faster than reading 400 MB of cache tensors across a congested PCIe bus, making recomputation the preferred default for compute-heavy accelerators.
7. Custom GPU Kernel Optimization
Executing non-contiguous attention requires specialized CUDA kernels to prevent memory access serialization. Standard cuBLAS or cuDNN attention implementations assume contiguous tensor strides . PagedAttention introduces three custom kernel fusions:
- Fused Reshape and Block Write: Instead of writing raw projection tensors and later reshaping them into head-major layouts, the projection write kernel immediately transposes and stores new keys and values directly into their physical block destinations based on block table indices.
- Warp-Aligned Attention Gathering: During decoding, a GPU thread warp is assigned to iterate across physical block pointers. The kernel fetches key blocks , computes partial vector dot-products , maintains running softmax normalizers using online softmax reduction, and accumulates value vectors directly in GPU registers.
- Batched Block Copy-on-Write: Memory duplication operations triggered by branching are batched into a single CUDA kernel launch, avoiding hundreds of tiny asynchronous
cudaMemcpyAsyncinvocations.
Pseudocode for PagedAttention Decoding Kernel:
---------------------------------------------------------------------------------
def paged_attention_decode_kernel(q, block_table, physical_k_cache, physical_v_cache):
# q: [head_dim], block_table: [num_blocks]
acc_o = zeros(head_dim)
running_max = -infinity
running_sum = 0.0
for block_idx in block_table:
# Fetch non-contiguous physical block
phys_k = physical_k_cache[block_idx] # [block_size, head_dim]
phys_v = physical_v_cache[block_idx] # [block_size, head_dim]
# Compute block attention scores
scores = dot_product(q, phys_k.T) / sqrt(head_dim) # [block_size]
# Online softmax update
block_max = max(scores)
new_max = max(running_max, block_max)
rescale = exp(running_max - new_max)
running_sum = running_sum * rescale + sum(exp(scores - new_max))
acc_o = acc_o * rescale + dot_product(exp(scores - new_max), phys_v)
running_max = new_max
return acc_o / running_sum8. Empirical Benchmarks and Systems Impact
In empirical evaluations conducted across OPT (13B, 66B, 175B) and LLaMA models on NVIDIA A100 clusters, PagedAttention demonstrated decisive throughput gains over FasterTransformer and Orca:
Serving Throughput Comparison (Requests per Second on A100):
ShareGPT Trace (OPT-13B):
FasterTransformer : [====] 1.0x (Baseline)
Orca (Oracle) : [======] 1.5x
vLLM (PagedAttn) : [================] 3.6x
Alpaca Trace (OPT-13B):
FasterTransformer : [====] 1.0x (Baseline)
Orca (Oracle) : [========] 2.1x
vLLM (PagedAttn) : [==================] 4.3x- Memory Efficiency: PagedAttention reduced KV cache memory waste to below 4%, enabling effective batch sizes 2x to 4x larger than legacy contiguous systems.
- Serving Throughput: Across ShareGPT (long prompts, variable outputs) and Alpaca (instruction following) workloads, vLLM achieved 2x to 4.3x higher request throughput while sustaining identical per-token latency.
- Complex Decoding Gains: In parallel sampling () and beam search (), memory sharing reduced total KV cache consumption by 55%, yielding up to 4.8x higher throughput compared to unshared execution engines.
9. Architectural Takeaways
PagedAttention fundamentally changed the systems architecture of LLM serving by demonstrating that memory management, rather than raw floating-point computation, is the defining bottleneck of modern inference.
By separating logical sequence dependencies from physical hardware layouts, PagedAttention provides:
- Near-Zero Memory Waste: Predictable, bounded memory overhead that scales linearly with active tokens rather than theoretical maximum lengths.
- Unified Memory Sharing: Frictionless support for prefix caching, speculative decoding trees, multi-sample verification, and multi-agent conversations without redundant tensor allocations.
- Standardized Serving Infrastructure: The foundational memory abstraction powering contemporary high-throughput inference engines across the industry, including vLLM, SGLang, Hugging Face TGI, and TensorRT-LLM.
Sources
- Kwon, W., Li, Z., Zhuang, S., Sheng, Y., Zheng, L., Yu, C. H., Gonzalez, J. E., Zhang, H., & Stoica, I. (2023). Efficient Memory Management for Large Language Model Serving with PagedAttention. Proceedings of the 29th ACM Symposium on Operating Systems Principles (SOSP '23). arXiv:2309.06180
- Vaswani, A., Shazeer, N., Parmar, N., Uszkoreit, J., Jones, L., Gomez, A. N., Kaiser, Ł., & Polosukhin, I. (2017). Attention Is All You Need. Advances in Neural Information Processing Systems (NeurIPS 2017). arXiv:1706.03762
- Yu, G. I., Jeong, J. S., Kim, G. W., Kim, S., & Chun, B. G. (2022). Orca: A Distributed Serving System for Transformer-Based Generative Models. 16th USENIX Symposium on Operating Systems Design and Implementation (OSDI 22). USENIX OSDI 2022
- Dao, T., Fu, D. Y., Ermon, S., Rudra, A., & Ré, C. (2022). FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness. Advances in Neural Information Processing Systems (NeurIPS 2022). arXiv:2205.14135
- Shoeybi, M., Patwary, M., Puri, R., LeGresley, P., Casper, J., & Catanzaro, B. (2019). Megatron-LM: Training Multi-Billion Parameter Language Models Using Model Parallelism. arXiv:1909.08053



