Inferensys

Glossary

PagedAttention

PagedAttention is a memory management algorithm, introduced by vLLM, that organizes the KV cache in transformer models into non-contiguous, paged blocks to eliminate fragmentation and waste during inference.
ML engineer running AI model benchmarks, performance charts on multiple screens, late night home office setup.
INFERENCE OPTIMIZATION

What is PagedAttention?

PagedAttention is a memory management algorithm for transformer-based large language models (LLMs) that dramatically reduces memory waste and increases serving throughput by managing the Key-Value (KV) cache in non-contiguous, paged blocks.

PagedAttention is a memory management algorithm, introduced by the vLLM serving engine, that manages the KV cache in non-contiguous, paged blocks. This approach is directly analogous to how operating systems manage virtual memory using paging. By allocating memory in fixed-size blocks, it eliminates the internal fragmentation caused by variable-length sequences, allowing for highly efficient GPU memory utilization and enabling much higher request concurrency during inference.

The algorithm treats the KV cache of each request's sequence as a set of blocks that can be physically scattered in memory but logically contiguous. This paged KV cache allows for near-zero memory waste, as new sequences can reuse freed blocks from completed ones. This efficient management is the core innovation enabling vLLM to achieve state-of-the-art throughput and support advanced features like continuous batching with variable-length sequences without performance degradation.

PAGEDATTENTION

Key Features and Benefits

PagedAttention is a memory management algorithm for transformer inference that treats the KV cache like virtual memory, organizing it into non-contiguous, fixed-size blocks. This approach directly addresses the core inefficiencies of serving variable-length sequences at scale.

01

Eliminates Memory Fragmentation

Traditional KV cache allocation reserves a contiguous block of memory for the maximum possible sequence length of each request, leading to severe internal fragmentation as most sequences are shorter. PagedAttention allocates memory in fixed-size blocks (pages). Only the blocks actually containing KV data are allocated, dramatically reducing wasted memory. This allows more concurrent requests to be served on the same GPU hardware.

02

Enables Efficient Memory Sharing

PagedAttention's block-level abstraction enables powerful optimizations:

  • Shared Prompts: In multi-tenant or chatbot scenarios, many requests may share a common system prompt or context. PagedAttention allows these shared tokens' KV caches to be stored in read-only memory blocks, referenced by all relevant requests. This eliminates redundant storage and computation.
  • Beam Search: During beam search, multiple candidate sequences share a common prefix. PagedAttention stores the prefix's KV cache once, with all beams holding references to the same physical blocks.
03

Optimizes for Variable-Length Sequences

Real-world LLM workloads are highly variable; input prompts and output generations have unpredictable lengths. PagedAttention's paged design is inherently flexible:

  • New blocks are allocated on-demand as generation progresses.
  • Finished requests release their blocks immediately, making memory available for new requests.
  • This dynamic allocation is analogous to an operating system managing heap memory for processes, providing optimal utilization for irregular, real-time workloads.
04

Increases Serving Throughput & Concurrency

By drastically reducing memory waste, PagedAttention directly increases the number of concurrent requests a single GPU can handle. This translates to higher Tokens Per Second (TPS) at the system level and lower cost per token. For example, vLLM demonstrated throughput increases of up to 24x compared to prior systems like Hugging Face Transformers by leveraging PagedAttention to serve more users simultaneously without additional hardware.

05

Analogy to Virtual Memory

PagedAttention's mechanism is a direct translation of OS virtual memory concepts to the KV cache:

  • KV Cache Block = Physical Memory Page: A fixed-size unit of storage.
  • Logical Block Table = Page Table: Each request maintains a table mapping its logical token positions to physical block IDs.
  • Block Allocation = Memory Allocation: The system manages a free list of available blocks. This proven abstraction provides a robust, predictable foundation for managing the LLM's most memory-intensive component during inference.
MEMORY MANAGEMENT COMPARISON

PagedAttention vs. Traditional KV Cache Allocation

This table compares the core architectural differences between the PagedAttention algorithm and traditional contiguous KV cache allocation, highlighting their impact on memory efficiency, performance, and operational flexibility during LLM inference.

Feature / MetricTraditional KV Cache AllocationPagedAttention (vLLM)

Allocation Strategy

Contiguous, monolithic blocks per sequence

Non-contiguous, paged blocks (analogous to OS virtual memory)

Memory Fragmentation

High (due to variable sequence lengths and pre-allocation)

Near-zero (pages are allocated and freed independently)

Memory Waste

Significant (up to 60-80% in practice due to over-provisioning)

Minimal (memory used is proportional to actual cached tokens)

Handling of Long Sequences

Inefficient; requires large contiguous free space

Efficient; sequences can span multiple non-adjacent pages

Support for Variable-Length & Concurrent Requests

Poor; static allocation leads to underutilization

Excellent; dynamic page allocation maximizes GPU utilization

Throughput (Tokens/Second) Impact

Lower; limited by memory fragmentation and waste

Higher (up to 23x reported); enables greater request concurrency

Implementation Complexity

Lower (simple pre-allocation)

Higher (requires a memory manager and block table)

Optimal Use Case

Simple, predictable workloads with fixed sequence lengths

Production serving with highly variable, concurrent requests

PAGEDATTENTION

Implementation and Ecosystem

PagedAttention is not just an algorithm but a foundational component of modern, high-performance LLM serving systems. Its implementation has catalyzed a new generation of inference engines and optimization techniques.

01

Core Algorithm: Virtual Memory for KV Cache

PagedAttention manages the KV (Key-Value) cache by dividing it into fixed-size blocks, analogous to memory pages in an operating system. These blocks are allocated non-contiguously in physical GPU memory.

  • Block Table: Each request's sequence is tracked via a block table that maps logical blocks to physical memory addresses.
  • On-Demand Allocation: Blocks are allocated only when new tokens are generated, eliminating pre-allocation waste.
  • Shared Memory: Identical prompt prefixes across requests can share the same physical blocks, a powerful form of memory deduplication. This virtual memory abstraction is what enables vLLM to achieve near-zero memory waste and support very long context windows efficiently.
03

Memory Efficiency & Fragmentation Reduction

The primary engineering benefit of PagedAttention is the drastic reduction of memory fragmentation, which directly translates to cost savings and higher concurrency.

  • Traditional Problem: Sequential, contiguous allocation for variable-length sequences leads to unusable gaps between requests (external fragmentation) and wasted reserved space within requests (internal fragmentation).
  • PagedAttention Solution: By using small, fixed-size blocks, free memory is pooled and can be allocated to any request. This eliminates external fragmentation and minimizes internal waste to at most one block per request.
  • Result: GPU memory utilization can approach 99%, allowing more concurrent requests or longer contexts on the same hardware.
04

Enabling Advanced Serving Features

PagedAttention's block-level abstraction unlocks sophisticated serving optimizations that are difficult or impossible with contiguous cache management.

  • Shared Prompts: As mentioned, common system prompts or few-shot examples are stored once in physical memory and referenced by multiple user requests.
  • Parallel Sampling: Techniques like beam search or parallel sampling of multiple outputs from a single prompt can share the cached prompt context blocks.
  • Efficient Cache Eviction & Swapping: The block table enables LRU (Least Recently Used) or other policies to evict or swap out less-used blocks to CPU RAM, enabling effective management of extremely long contexts that exceed GPU memory.
05

Integration with Continuous Batching

PagedAttention is highly synergistic with continuous batching (or in-flight batching), forming the core of modern serving stacks.

  • Dynamic Workloads: Continuous batching groups requests of different lengths and adds new requests to a running batch. PagedAttention's non-contiguous allocation is essential to handle this dynamic, heterogeneous mix efficiently.
  • Eliminating Padding: Without PagedAttention, variable-length sequences in a batch require padding to the longest sequence, wasting compute and memory. PagedAttention's block-based approach inherently requires no padding for the KV cache.
  • Combined Benefit: Together, they maximize GPU utilization for both compute (through batching) and memory (through paging), delivering optimal throughput.
06

Ecosystem & Alternative Implementations

The success of PagedAttention has influenced the design of other inference systems and sparked research into related optimizations.

  • Inspired Systems: Frameworks like LMDeploy (by InternLM) and TensorRT-LLM have incorporated similar block-based KV cache management strategies, validating the core concept.
  • Research Extensions: Work like FlashInfer explores kernel-level optimizations for PagedAttention-like operations. VLLM's Blocked KV Cache design is a direct influence.
  • Hardware Considerations: The algorithm influences thinking about future GPU memory architectures, highlighting the importance of efficient, fine-grained memory management for LLM inference. This ecosystem growth confirms PagedAttention as a foundational primitive for efficient LLM serving.
PAGEDATTENTION

Frequently Asked Questions

PagedAttention is a foundational memory management algorithm for efficient LLM inference. These questions address its core mechanics, benefits, and practical applications.

PagedAttention is a memory management algorithm for transformer-based large language models that manages the KV cache in non-contiguous, paged blocks, analogous to virtual memory in operating systems. It works by decoupling the logical sequence of tokens from their physical memory location. Instead of allocating a single, contiguous block of memory for each request's KV cache—which leads to fragmentation due to variable sequence lengths—PagedAttention divides the cache into fixed-size blocks (pages). These blocks are allocated dynamically from a shared pool as new tokens are generated. The system maintains a page table for each request to map its logical token positions to the physical blocks holding their key and value vectors. This allows the GPU's high-bandwidth memory to be utilized near its theoretical maximum, enabling higher batch sizes and concurrency.

Prasad Kumkar

About the author

Prasad Kumkar

CEO & MD, Inference Systems

Prasad Kumkar is the CEO & MD of Inference Systems and writes about AI systems architecture, LLM infrastructure, model serving, evaluation, and production deployment. Over 5+ years, he has worked across computer vision models, L5 autonomous vehicle systems, and LLM research, with a focus on taking complex AI ideas into real-world engineering systems.

His work and writing cover AI systems, large language models, AI agents, multimodal systems, autonomous systems, inference optimization, RAG, evaluation, and production AI engineering.