PagedAttention & KV-Cache Virtual Memory Simulator
Explore how operating system virtual memory paging transforms high-throughput LLM inference. Simulate dynamic physical block allocation, watch logical-to-physical block table translations ($L_i \to P_k$), observe Prefix Caching with Copy-on-Write (CoW), and inspect real-time mathematical comparisons between naive contiguous over-allocation and zero-waste PagedAttention.
Higher batch throughput in identical VRAM
Saved by deduplicating system prompts
Over-reserved for ungenerated tokens
Bounded strictly by tail block slack (< 3.5%)
Contiguous Static Pre-Allocation
Allocators must reserve contiguous virtual memory for the request's maximum possible sequence length (max_tokens) upfront. Ungenerated slots remain locked and unusable by concurrent requests.
PagedAttention On-Demand Allocation
KV-cache is partitioned into fixed-size physical blocks (16 tokens). Blocks are allocated on-demand as tokens decode. Non-contiguous blocks are stitched via a logical-to-physical block table, eliminating all external fragmentation.
Memory per token is calculated as 2 × n_layers (32) × n_heads (32) × head_dim (128) × 2 bytes = 128 KB/token. In contiguous allocation, reserving 100 max tokens allocates 12.8 MB upfront. With PagedAttention (block size 16), only 2048 KB is allocated at a time. Internal fragmentation is bounded by (B - (tokens % B)) < 16 tokens per sequence, yielding a sustained 2.2× concurrency boost.
128 KB/tok•Active: 4 reqsGPU VRAM Physical Memory Block Pool (64 Blocks · 128 MB)
0x000000 (0.0 MB mark)Slack: 0 tokens (0.00 MB unconsumed)
Shared across multiple sequences
Immutable prompt hash
Sequence Block Tables (Logical L_i → Physical P_k Translation)
PagedAttention Memory Manager Event Stream
Prefix Caching & CoW
Load the Copilot (Prefix) preset. Observe how requests #1, #2, and #3 share identical system prompt blocks (P_0 and P_1) with a refCount = 3 badge, completely eliminating duplicate VRAM usage.
Block Size Trade-Offs (16 vs 32)
Toggle Block Sizebetween 16 and 32 tokens. At 16 tokens, internal fragmentation is minimized to < 3.5% tail slack. At 32 tokens, block table lookup overhead is halved, but average tail waste increases proportionally to $B / 2$.
Contiguous Waste vs Paged Concurrency
Examine the Memory Efficiency Meters. In contiguous allocation, 60%–85% of VRAM is wasted waiting for ungenerated tokens. PagedAttention eliminates external fragmentation completely, delivering a sustained 2.5×–4× concurrency gain.
How PagedAttention & KV-Cache Virtual Memory Simulator Works
PagedAttention eliminates up to 60–80% of KV-cache VRAM waste by decoupling logical sequence token blocks from non-contiguous physical HBM blocks via a per-request block table. Instead of reserving contiguous max-sequence slots per request, physical pages allocate on demand and share identical system prompts via Copy-on-Write reference counting.
Frequently Asked Engineering Questions
- Why does contiguous KV-cache allocation waste GPU VRAM?
- Because output length is unknown at prefill time, naive allocators must reserve contiguous slots up to max_model_len for every active sequence, causing internal fragmentation inside reserved slabs and external fragmentation across varying request sizes.
- How does PagedAttention enable Copy-on-Write prefix sharing?
- Shared system prompts or few-shot prefixes map distinct requests' logical block indices to the same physical KV-cache block IDs with reference count > 1, allocating private copy blocks only when decoding diverges.
- What is the block size trade-off in PagedAttention?
- Smaller block sizes (such as 16 tokens per block) minimize trailing internal waste per sequence, whereas larger blocks amortize block-table metadata lookups and coalesce global memory reads inside the attention CUDA kernel.
FS-101Model Serving and Inference
Understand inference engines memory scheduling batching routing scaling distributed execution lifecycle controls observability and economics.
Get the weekly systems teardown
One technical note per week covering inference bottlenecks, retrieval harnesses, and commit-pinned teardowns.