PagedAttention and Virtual Memory Management: Mathematical Foundations of Non-Contiguous KV-Cache Allocation, Dynamic Block Translation, and Copy-on-Write Forking Mechanics
In autoregressive transformer serving, the generation phase is fundamentally constrained by GPU memory capacity and memory bandwidth rather than raw compute throughput. While the initial prefill phase processes the prompt in parallel with high arithmetic intensity, the subsequent token-by-token decoding phase computes attention for only a single new token per sequence at each step. At each decoding step, the model must read all historical Key (K) and Value (V) activation tensors from GPU High Bandwidth Memory (HBM) into on-chip Static Random-Access Memory (SRAM).
Before the introduction of PagedAttention by Kwon et al. (2023), serving runtimes required key-value cache memory to reside in contiguous physical memory buffers. Because request completion lengths cannot be known in advance, systems pre-allocated contiguous memory chunks sized for maximum context limits or expected sequence bounds. This static allocation pattern introduced severe memory fragmentation, causing 60% to 80% of allocated KV-cache HBM to sit idle and restricting maximum serving concurrency.
PagedAttention resolves this bottleneck by adapting classical operating system virtual memory paging to transformer KV caches. By partitioning key and value tensors into fixed-size physical blocks and managing access through per-sequence page tables, PagedAttention eliminates external fragmentation, constrains internal fragmentation to under 4%, and enables zero-copy prefix sharing and parallel sampling via Copy-on-Write (CoW) mechanics.

1. The Mathematical Modeling of KV-Cache Memory Consumption
To understand the memory wall in autoregressive serving, consider a standard decoder-only transformer architecture parameterized by:
- : Number of transformer layers
- : Number of query attention heads per layer
- : Number of key-value attention heads per layer (where in Multi-Head Attention, and in Grouped-Query Attention)
- : Hidden dimension per attention head
- : Byte precision per parameter (e.g., for FP16/BF16, for FP8)
- : Sequence length in tokens
At sequence length , the total key-value cache memory consumed by a single request across both key and value projections is defined as:
For modern frontier and open-weight architectures, this footprint scales rapidly:
- Llama-3-8B (): . A context of 8,192 tokens consumes of HBM per concurrent stream.
- Llama-3-70B (): . A context of 8,192 tokens consumes of HBM per stream.
- Llama-2-70B ( using dense MHA): . An 8,192-token context consumes of HBM per stream.
When serving a batch of concurrent requests with dynamic sequence lengths , total memory allocated under contiguous allocation systems is governed by maximum capacity reservations rather than actual consumed tokens.
2. Taxonomy and Formal Proofs of KV-Cache Memory Waste
In traditional serving frameworks like FasterTransformer and early versions of Orca (Yu et al., 2022), memory allocators suffer from three distinct fragmentation mechanisms:
Traditional Contiguous Pre-Allocation:
[ Token 1 .. Token 200 (Active) | Unused Reserved Slots (Waste) -> Max Context (4096) ]
\____________________________________________________/
Internal + Reservation Waste
PagedAttention Dynamic Block Allocation:
Logical Cache: [ Block 0 (16) ] -> [ Block 1 (16) ] -> [ Block 2 (16) ] ...
Physical Pool: [ Phys 47 ] ... [ Phys 12 ] ... [ Phys 93 ] (Non-contiguous HBM)
Waste: Only partially filled final block (< 16 tokens).Internal Fragmentation
Internal fragmentation occurs when memory is allocated in fixed or pre-reserved boundaries that exceed the actual sequence length generated by the model.
If a runtime allocates a contiguous buffer sized for maximum sequence length , the internal memory waste for request terminating at length is:
For a batch of requests drawn from a sequence length distribution with probability density function , the expected aggregate internal waste is:
In enterprise workloads where prompt lengths average 500 to 1,000 tokens and completions average 200 tokens within an 8,192-token limit window, routinely exceeds 75% of total allocated cache memory.
Reservation Fragmentation
Even when runtimes implement dynamic memory resizing (reallocating buffers when sequences grow), allocators must reserve memory for future generation steps ahead of time to avoid per-token memory allocation overhead.
If memory is reserved in increments of , at any arbitrary decoding step , the reserved ungenerated capacity is:
External Fragmentation
External fragmentation occurs when memory is dynamically allocated and freed in variable chunk sizes. Over time, physical HBM becomes peppered with non-contiguous free memory segments.
Let GPU HBM contain a set of free disjoint memory segments where each segment has contiguous capacity . Total free memory is:
Under contiguous allocation constraints, an incoming request requiring contiguous capacity will be rejected (or forced to wait) if:
This condition represents complete allocation stalls despite substantial aggregate unallocated memory on the device.
3. The PagedAttention Architecture: Logical-to-Physical Address Translation
PagedAttention solves all three fragmentation modalities by eliminating the requirement for physical contiguity. The key-value cache of each sequence is treated as a sequence of Logical KV Blocks, which are mapped to Physical KV Blocks managed in a unified global GPU memory pool.
Mathematical Formulation of Block Partitioning
Let block size denote the fixed number of tokens stored in a single physical block (typically ).
For a sequence of length , the logical KV cache is partitioned into logical blocks:
For any arbitrary token position within the sequence:
- Logical Block Index ():
- Intra-Block Token Offset ():
Physical Block Representation
A physical block is a contiguous memory region allocated in GPU HBM sized to hold exactly token vectors for both key and value tensors across all layers and heads:
For , , , , a single physical block occupies exactly:
For an 80-layer model, one physical block across all layers consumes .
The Block Table Mapping
Each active sequence maintains a private Block Table , which acts as an operating system page table:
where is the total number of physical blocks available in the global GPU allocator.
Token Index t = 38, Block Size B = 16
1. Logical Block Index: j = floor(38 / 16) = 2
2. Intra-Block Offset: o = 38 mod 16 = 6
3. Block Table Lookup: T_r[2] -> Physical Block 93
4. Physical Address: BasePtr(Pool) + (93 * BlockSizeBytes) + (6 * TokenBytes)Minimization of Internal Waste
Because physical blocks are allocated dynamically on demand as tokens are generated, only the final logical block of a sequence can contain unwritten token slots.
The maximum internal fragmentation per sequence is strictly bounded by:
Assuming sequence completion lengths are uniformly distributed over , the expected internal memory waste per sequence is:
For , the expected waste is exactly tokens of KV-cache memory per sequence. For a sequence length , the internal fragmentation percentage is:
This represents a reduction in memory waste from over 70% in contiguous static allocation systems to under 2% to 4% in PagedAttention.
4. Kernel Execution and Gather-Scatter Attention Mechanics
During autoregressive generation at decoding step , the attention engine receives a new query vector for head and must compute scaled dot-product attention against all previous key vectors and aggregate value vectors for .
In standard contiguous attention kernels (e.g. FlashAttention-2 by Dao, 2023), the kernel reads directly from a continuous memory pointer . In PagedAttention, the kernel performs non-contiguous gather operations via the sequence's block table.
Vectorized Address Computation
Let denote the array of physical block identifiers for the current sequence. For token position , the physical memory addresses for the key and value vectors at layer and head are:
Fused PagedAttention Algorithm
# Algorithmic formulation of PagedAttention forward decode pass
def paged_attention_decode(
query: Tensor, # Shape: [batch_size, num_heads, head_dim]
key_pool: Tensor, # Shape: [num_blocks, num_kv_heads, block_size, head_dim]
value_pool: Tensor, # Shape: [num_blocks, num_kv_heads, block_size, head_dim]
block_tables: Tensor, # Shape: [batch_size, max_num_blocks_per_seq]
seq_lens: Tensor, # Shape: [batch_size]
scale: float, # 1.0 / sqrt(head_dim)
block_size: int # Tokens per physical block (e.g., 16)
) -> Tensor:
batch_size, num_heads, head_dim = query.shape
output = torch.zeros_like(query)
for b in range(batch_size):
seq_len = seq_lens[b]
num_blocks = (seq_len + block_size - 1) // block_size
q = query[b] # [num_heads, head_dim]
# Accumulators for online softmax
m_prev = torch.full((num_heads, 1), -float('inf'))
l_prev = torch.zeros((num_heads, 1))
acc = torch.zeros((num_heads, head_dim))
for blk_idx in range(num_blocks):
phys_block_id = block_tables[b, blk_idx]
tokens_in_block = min(block_size, seq_len - blk_idx * block_size)
# Gather physical block KV tensors
k_block = key_pool[phys_block_id, :, :tokens_in_block, :] # [num_kv_heads, tokens, head_dim]
v_block = value_pool[phys_block_id, :, :tokens_in_block, :] # [num_kv_heads, tokens, head_dim]
# Compute logits: S = (q * K^T) * scale
scores = torch.einsum("hd,ktd->hkt", q, k_block) * scale
# Online softmax reduction across blocks
m_curr = torch.maximum(m_prev, torch.max(scores, dim=-1, keepdim=True)[0])
p_prev = torch.exp(m_prev - m_curr)
p_curr = torch.exp(scores - m_curr)
l_curr = p_prev * l_prev + torch.sum(p_curr, dim=-1, keepdim=True)
acc = acc * p_prev + torch.einsum("hkt,ktd->hd", p_curr, v_block)
m_prev = m_curr
l_prev = l_curr
output[b] = acc / l_prev
return outputParallel Partitioning Across GPU Warps
In production CUDA implementations (such as vLLM's vllm/csrc/attention/attention_kernels.cu), executing PagedAttention over long sequences requires partitioning the reduction across thread warps:
- Intra-Block Parallelism: Thread warps inside a CUDA threadblock are assigned distinct physical blocks. Each thread loads a 128-bit vectorized chunk (8 consecutive FP16 values) from GPU HBM.
- Intermediate Reduction: Each threadblock computes intermediate maximum values , sum of exponentials , and partial output vectors .
- Cross-Block Reduction Kernel: A second lightweight reduction kernel combines the intermediate statistics across all assigned physical blocks using online softmax rescaling:
Because memory accesses inside each physical block are fully contiguous and coalesced, PagedAttention achieves over 95% of the memory bandwidth saturation of monolithic, contiguous attention kernels while operating entirely over non-contiguous physical allocations.
5. Copy-on-Write (CoW) Forking and Complex Decoding Strategies
Modern LLM workflows frequently deploy advanced inference patterns that diverge from simple linear sequence generation:
- Parallel Sampling: Generating candidate completions from a single shared prompt.
- Beam Search: Maintaining active candidate paths at each step.
- Tree-Structured Decoding & Speculative Tree Verification: Evaluating multiple speculative token branches concurrently.
- Multi-Turn Agent Interactions: Multiple independent agent steps sharing identical system instructions and tool definitions.
Under contiguous memory allocation architectures, each branch requires a complete, independent physical duplicate of the entire prompt's KV cache.
Reference-Counted Physical Blocks
PagedAttention introduces operating-system-level Copy-on-Write (CoW) mechanics by augmenting every physical block in the global block allocator with an atomic reference counter:
When a block is free, . When assigned to an active sequence, .
Parallel Sampling with Copy-on-Write (Prompt Length = 32 tokens, B = 16):
Shared Prompt: [ Logical 0 (Phys 10, Ref=2) ] -> [ Logical 1 (Phys 14, Ref=2) ]
/ \
Branch A (Token 33): Allocates Phys 88 (Ref=1) <-----------+ +------------> Branch B (Token 33): Allocates Phys 92 (Ref=1)The Copy-on-Write Forking Protocol
When a request forks from a parent sequence into child sequences (or when parallel sampling initializes streams from a single prompt):
- Page Table Duplication: The system creates new Block Tables , copying the parent's physical block pointers:
- Reference Increment: For every physical block referenced in the table, its counter is incremented by :
- Execution Cost: Step 1 and Step 2 operate exclusively on lightweight metadata (integers in host or device control memory). The physical KV tensors in GPU HBM are never copied, executing in time with zero memory allocation overhead.
Write Execution and Block Splitting
When child sequence generates a new token at position :
- Let and .
- Case 1: Exclusive Ownership ():
The child sequence owns the physical block exclusively. The new key and value vectors are written directly into slot of physical block .
- Case 2: Shared Block Mutation ():
The block is shared with other active sequences. Writing directly would corrupt state for the other branches. The memory manager executes Copy-on-Write:
- Allocates a new physical block from the free pool ().
- Copies the existing token vectors from to :
- Decrements the reference counter of the original block:
- Updates the child's block table:
- Writes the new token's key and value vectors into slot of .
In beam search and parallel sampling benchmarks, CoW memory sharing reduces KV-cache memory consumption by up to 55%, directly translating to a 2.2x increase in maximum serving throughput on identical hardware.
6. Memory Exhaustion, Eviction, and Preemption Dynamics
In high-concurrency production serving, the aggregate memory demand of active decoding sequences can exceed total physical GPU HBM capacity ().
When the global physical block allocator runs out of free blocks (), the scheduler must handle memory exhaustion deterministically without crashing or dropping active requests.
PagedAttention enables two distinct preemption policies:
GPU HBM Saturation Handling:
[ All Physical Blocks Allocated ]
|
+------------------+------------------+
| |
[ Swapping Policy ] [ Recomputation Policy ]
Async DMA copy to Host CPU RAM. Evict physical blocks immediately.
Free GPU blocks for active jobs. On resume, re-run prefill forward pass.
Swap back when memory frees. Optimal for short contexts & high FLOPS.1. Swapping (GPU HBM Host CPU RAM)
The serving system allocates an auxiliary physical block pool in CPU host memory. Because CPU RAM is significantly larger and cheaper than GPU HBM (e.g., 512 GB to 2 TB CPU RAM vs. 80 GB GPU HBM), the scheduler can evict victim sequences to host memory:
- The scheduler selects a victim request (typically using First-In-First-Out or Last-In-First-Out priority).
- It transfers the victim's physical blocks from GPU HBM to Host RAM via asynchronous PCIe DMA transfers.
- The freed GPU blocks are returned to the active pool.
- When GPU capacity recovers, the blocks are transferred back to GPU HBM, and generation resumes without losing decoding progress.
2. Recomputation (Discard and Prefill)
In scenarios where PCIe bandwidth is saturated or prompt evaluation is fast, the scheduler can discard the victim's physical KV blocks entirely:
- The scheduler frees all physical blocks in , retaining only the original prompt tokens and the sequence of generated token IDs.
- When GPU memory becomes available, the system resubmits the concatenated tokens as a single prefill request.
- Because prefill executes at compute-bound arithmetic intensity (saturating GPU tensor cores), recomputing 500 tokens often takes less elapsed time than transferring megabytes of KV tensors across a contended PCIe bus.
7. Architectural Comparison and Systems Evolution
The table below contrasts PagedAttention with historical and subsequent memory management architectures in LLM serving:
| Architecture | Memory Allocation Unit | Internal Fragmentation | External Fragmentation | Zero-Copy Prefix Sharing | CoW Parallel Sampling | Serving Engine | | :--- | :--- | :--- | :--- | :--- | :--- | :--- | | Static Pre-Allocation | Monolithic buffer () | Extreme () | Severe | Unsupported | Unsupported | FasterTransformer, TensorRT-LLM (v1) | | Dynamic Resizing (Orca) | Contiguous resized chunks | High () | Severe | Unsupported | Unsupported | Orca (Yu et al., 2022) | | PagedAttention | Fixed non-contiguous blocks () | Minimal () | Zero | Supported | Supported ( fork) | vLLM (Kwon et al., 2023) | | RadixAttention | Trie-structured paged blocks | Minimal () | Zero | Recursive Radix Tree | Supported with LRU Eviction | SGLang (Zheng et al., 2024) | | vAttention | OS Virtual Memory Remapping | Zero (byte-level) | Low | Supported | Supported | vAttention (Prabhu et al., 2024) |
Subsequent architectures build directly on PagedAttention's foundations. SGLang's RadixAttention extends PagedAttention by retaining physical KV blocks in a radix tree across independent API calls, enabling automatic multi-turn cache hits and prefix reuse. vAttention leverages low-level Linux kernel page table manipulation to provide contiguous virtual memory addresses to standard attention kernels while dynamically mapping physical GPU pages underneath.
By eliminating memory fragmentation and uncoupling logical token positions from physical HBM layout, PagedAttention shifted LLM serving from memory-wasteful static reservations to high-density, multi-tenant execution engines.
Sources
- Efficient Memory Management for Large Language Model Serving with PagedAttention (Kwon et al., SOSP 2023 / arXiv:2309.06180)
- SGLang: Efficient Execution of Structured Language Model Programs (Zheng et al., 2024 / arXiv:2312.07104)
- Orca: A Distributed Serving System for Transformer-Based Generative Models (Yu et al., OSDI 2022)
- FlashAttention: Fast and Memory-Efficient Exact Attention with IO-Awareness (Dao et al., NeurIPS 2022 / arXiv:2205.14135)
- FlashAttention-2: Faster Attention with Better Parallelism and Work Partitioning (Dao, 2023 / arXiv:2307.08691)
- vAttention: Dynamic Memory Management for Serving LLMs without PagedAttention (Prabhu et al., ASPLOS 2024)



