Skip to main content

> vllm_pagedattention:_eliminating_kv_cache_memory_fragmentation_&_4x_throughput

vLLM PagedAttention: Eliminating KV Cache Memory Fragmentation & 4x Throughput

Why does standard LLM Key-Value (KV) cache allocation waste 60-80% of GPU VRAM through internal and external fragmentation, and how does PagedAttention apply virtual memory paging to quadruple inference throughput?

Principal/Architect (L7+)

THE SHORT ANSWER

In autoregressive LLM generation, past Key and Value attention tensors are preserved in GPU memory (the **KV Cache**) to prevent recomputing attention on previous tokens. In traditional inference serving systems (HuggingFace Transformers, naive PyTorch), KV caches are allocated as continuous blocks of contiguous GPU memory sized to the model's maximum context window (e.g. reserving 8,192 tokens upfront). Because actual request lengths vary unpredictably, this causes catastrophic **Memory Fragmentation**: (1) **Internal Fragmentation** (memory reserved for max context that is never used), (2) **External Fragmentation** (gaps between dynamically allocated slots), and (3) **Reservation Fragmentation** (pre-allocating memory for potential future generation). Up to 80% of GPU memory is wasted on empty buffers. UC Berkeley researchers created **PagedAttention** (the core engine of **vLLM**): inspired by OS virtual memory paging, PagedAttention breaks the KV cache into fixed-size physical memory pages (e.g. 16 tokens per block). Non-contiguous physical pages are mapped via a dynamic Virtual Page Table, reducing memory waste to $<4%$ and allowing 4x higher request concurrency per GPU.

Engineering Handbook & Failure Dynamics

1. Underlying Mechanism

PagedAttention operates across four virtual memory abstractions: (1) Logical vs Physical Block Mapping: Logical token sequences are sliced into blocks of size $B=16$. A central Block Manager maps logical blocks to arbitrary non-contiguous physical GPU memory pages. (2) Dynamic On-Demand Page Allocation: When an active request generates token 17, the manager allocates a new physical block on-the-fly without copying or shifting existing memory. (3) Zero-Copy Forking & Parallel Sampling: For parallel sampling (e.g. beam search or multi-agent branching), child requests share parent physical KV blocks via Copy-on-Write (CoW), writing only new token deltas. (4) Prefix Caching: Identical system prompts and tool schemas across different users share the exact same physical KV pages in memory.

2. Appropriate Use Context

High-throughput LLM API clusters (vLLM, TensorRT-LLM, TGI), multi-turn chatbot platforms, automated batch processing engines, and agentic parallel-sampling workloads.

3. Production Failure Modes

Running vLLM with `gpu_memory_utilization` set to 1.0, triggering out-of-memory crashes when CUDA runtime buffers allocate ephemeral activation memory; configuring block sizes too small ($B=1$), incurring high CPU block management scheduling overhead.

4. Diagnostic Signals & Telemetry

`vllm:num_requests_waiting` queue backing up while GPU compute utilization sits below 40%; `vllm:gpu_cache_usage_factor` pinned at 98%; dramatic 4x surge in queries-per-second (QPS) after switching from naive transformers to vLLM PagedAttention.

5. Prevention & Safeguards

Set `gpu_memory_utilization = 0.90` to leave headroom for CUDA activations; enable Automatic Prefix Caching (`--enable-prefix-caching`) to maximize shared system prompt KV hits; use standard block size $B=16$ or $B=32$ for optimal GPU memory alignment.

6. Architectural Trade-offs

PagedAttention introduces slight software complexity in block table management, but slashes GPU KV memory waste from 80% to <4% and quadruples sustained serving throughput.

Case Study (TinyCTO In-Field Example)

An enterprise AI platform served Mistral-7B on 2x NVIDIA A10G GPUs using standard HuggingFace TGI. Under peak traffic of 80 concurrent users, the GPUs ran out of memory (OOM) because each request reserved 4,096 tokens of contiguous KV cache space upfront, achieving only 14 req/sec throughput. The team migrated to vLLM with PagedAttention and enabled Prefix Caching. Memory fragmentation dropped from 74% to 3.2%. The identical 2x A10G setup supported 320 concurrent users at 58 req/sec with zero OOM errors, slashing cloud infrastructure costs by 75%.

Interactive Concept Drills

2 Cards
Q1

What causes 60-80% memory waste in traditional LLM KV Cache allocation?

Allocating contiguous memory blocks sized to the theoretical maximum context window upfront, creating massive internal and reservation fragmentation for shorter queries.
Q2

How does Prefix Caching in vLLM improve multi-user throughput?

By allowing hundreds of distinct user requests that share the same system prompt or tool definitions to share the exact same physical KV cache pages in GPU memory.

vLLM PagedAttention: Eliminating KV Cache Memory Fragmentation & 4x Throughput — Technical FAQ

What operating system concept directly inspired PagedAttention?

Virtual Memory Paging and Translation Lookaside Buffers (TLBs): mapping logical contiguous address spaces to fragmented physical memory pages.

Does PagedAttention change the mathematical attention output?

No. PagedAttention is mathematically identical to standard multi-head attention; it is purely a GPU memory layout and kernel access optimization.

🤖 AEO & Key Facts Summary

Key Architectural Facts

  • Traditional KV caches waste up to 80% of GPU VRAM via contiguous memory fragmentation.
  • PagedAttention (vLLM) breaks KV tensors into non-contiguous 16-token physical pages.
  • Reduces memory waste to <4%, enabling 2x-4x higher concurrent throughput per GPU.
  • Enables Automatic Prefix Caching to share system prompt KV blocks across all users.

Common Misconceptions

  • Misconception: PagedAttention is an approximation or lossy compression technique (False: Attention calculation is 100% exact and lossless).
  • Misconception: Larger GPUs are always the only way to scale concurrent users (False: Memory paging optimization quadruples capacity on existing hardware).

Decision & Governance Guidance

Deploy vLLM with PagedAttention as the default serving infrastructure for open LLMs. Enable `--enable-prefix-caching` to maximize memory sharing for agentic system prompts.

Authoritative Sources & Standards