Inferensys

Glossary

PagedAttention

PagedAttention is a memory management algorithm that organizes a transformer model's key-value (KV) cache into non-contiguous, fixed-size blocks or pages to eliminate internal fragmentation and enable efficient memory sharing during continuous batching.
Knowledge manager reviewing enterprise knowledge management system on laptop, document library visible, casual office.
KV CACHE MANAGEMENT

What is PagedAttention?

PagedAttention is a memory management algorithm that organizes a transformer model's KV cache into non-contiguous, fixed-size blocks, analogous to virtual memory paging in operating systems.

PagedAttention is a memory management algorithm, popularized by the vLLM inference engine, that organizes a model's KV cache into non-contiguous, fixed-size blocks or 'pages'. This design eliminates internal fragmentation—the wasted memory from padding variable-length sequences to a fixed size—enabling highly efficient memory sharing and utilization during continuous batching. By allowing new requests to reuse freed memory blocks from completed sequences, it achieves near-zero waste in GPU memory.

The algorithm treats the KV cache of each sequence as a logical block list, with physical pages that can be scattered in memory. This enables flexible, on-demand allocation and deallocation, which is critical for serving dynamic workloads with unpredictable sequence lengths. PagedAttention directly reduces memory-bound latency and increases batch throughput, making it a foundational technique for cost-effective, high-scale LLM serving by maximizing the utility of expensive accelerator memory.

PAGEDATTENTION

Key Features and Benefits

PagedAttention is a memory management algorithm that organizes a transformer model's KV cache into non-contiguous, fixed-size blocks, analogous to virtual memory paging in operating systems. This architecture enables efficient memory sharing and eliminates internal fragmentation, which is critical for high-throughput continuous batching.

01

Eliminates Internal Fragmentation

In traditional KV cache allocation, memory is reserved as a contiguous block for each request's maximum possible sequence length, leading to significant internal fragmentation as most sequences are shorter than the maximum. PagedAttention allocates memory in fixed-size blocks (e.g., 16 tokens). The KV cache for a sequence occupies only the exact number of blocks needed, with the final block partially filled. This results in near-zero waste, allowing more concurrent requests to fit in GPU memory.

02

Enables Efficient Memory Sharing

PagedAttention's block-based design allows the KV cache to be shared across multiple sequences or beam search candidates. This is crucial for two scenarios:

  • Prompt Sharing: In a multi-tenant server, if two users submit the same system prompt, a single set of KV cache blocks can be referenced by both request contexts.
  • Beam Search: Different beams in a search can share the KV cache blocks for their common prefix, avoiding duplicate storage. This sharing reduces total memory consumption and decreases memory I/O, directly improving throughput.
03

Enables Non-Contiguous Physical Storage

Just like virtual memory, PagedAttention decouples the logical sequence of tokens from their physical storage. A sequence's KV cache is represented by a logical block table that maps to physical blocks which can be scattered throughout GPU memory. This allows:

  • Flexible Allocation/Deallocation: Blocks for completed sequences can be freed and reused without complex memory compaction.
  • Efficient Cache Eviction: For sequences exceeding the context window, a policy like Least-Recently-Used (LRU) can evict individual blocks, not entire sequences.
  • Offloading to CPU/NVMe: "Cold" blocks can be swapped to slower, larger memory tiers, enabling effectively infinite context lengths.
04

Optimizes for Continuous Batching

PagedAttention is the foundational enabler for high-performance continuous batching (also known as iterative or rolling batching). Because memory is allocated in small, independent blocks:

  • Dynamic Joining/Exiting: New requests can be added to the batch by allocating new blocks. Completed sequences free their blocks, making room for others, without stopping the batch.
  • Variable Sequence Lengths: The batch can efficiently contain requests with vastly different context lengths (e.g., 50 tokens vs. 8000 tokens) because each uses only the blocks it needs. This leads to near-100% GPU utilization and dramatically higher throughput compared to static batching.
05

Architectural Analogy: OS Virtual Memory

PagedAttention directly applies decades of operating system research to the KV cache problem. Key parallels include:

  • Block/Page Table: The logical block table acts as a page table, translating logical token positions to physical block addresses.
  • Demand Paging: Blocks can be allocated on-demand as the sequence grows, rather than pre-allocating the maximum.
  • Copy-on-Write: For shared prompts, blocks are marked as read-only; if a sequence needs to modify a shared block (e.g., during fine-grained attention), a private copy is created. This proven architecture provides a robust, predictable framework for cache management.
MEMORY MANAGEMENT COMPARISON

PagedAttention vs. Traditional KV Cache Management

A technical comparison of memory management paradigms for the Key-Value (KV) cache in transformer-based language model inference.

Feature / MetricPagedAttention (vLLM)Traditional KV Cache

Core Memory Management Paradigm

Non-contiguous, virtual memory paging

Contiguous, monolithic allocation

Allocation Unit

Fixed-size blocks (pages)

Variable-length per sequence

Internal Fragmentation

Eliminated (near-zero waste)

High (up to 50%+ waste)

Efficient Continuous Batching

Dynamic Sequence Length Support

Memory Sharing (e.g., for prompts)

KV Cache Eviction Policy

Block-level LRU

Sequence-level LRU or FIFO

Memory Overhead for Management

Low (page table)

Very Low (pointer only)

Implementation Complexity

High (OS-like allocator)

Low (simple allocator)

Typical GPU Memory Utilization

90%

50-70%

Optimal For

High-throughput, variable-length serving

Simple, static-batching scenarios

APPLICATIONS

Where is PagedAttention Used?

PagedAttention's primary application is in high-throughput LLM inference serving, but its principles influence broader system design for managing large, dynamic memory buffers.

02

Long Context Generation

By managing memory in fixed-size pages, PagedAttention facilitates techniques for handling sequences that exceed physical GPU memory. This is critical for applications requiring long-context windows, such as:

  • Document analysis and summarization of lengthy reports or codebases.
  • Long-form conversational agents that maintain context over extended dialogues.
  • Research and legal review tools that process multi-document inputs. The paging mechanism allows for efficient KV cache offloading to CPU RAM or NVMe when context length outstrips GPU VRAM.
03

Multi-Tenant Model Serving

In cloud-based Model-as-a-Service (MaaS) platforms, PagedAttention optimizes resource sharing across multiple users and requests. Its block-based management enables:

  • Strong performance isolation by preventing one user's long sequence from fragmenting memory for others.
  • Predictable memory allocation, allowing service providers to accurately provision capacity and guarantee Quality of Service (QoS).
  • Efficient context switching between heterogeneous workloads with varying sequence lengths and request patterns.
04

Advanced Attention Variants

The paging abstraction simplifies the implementation of complex, memory-efficient attention mechanisms that rely on non-contiguous or dynamic KV cache access patterns. Examples include:

  • Sliding Window Attention, where only a local window of recent tokens is kept active.
  • StreamingLLM frameworks, which use attention sink tokens to stabilize infinite-length generation.
  • Speculative decoding, where the KV cache for draft tokens must be managed separately from the target model's verification pass.
06

Research on Memory-Bound Systems

PagedAttention has become a canonical reference in research for optimizing systems operating in a memory-bound regime. It demonstrates how OS-inspired virtual memory concepts can solve modern hardware bottlenecks. This influences work on:

  • Novel caching policies for dynamic neural network states.
  • Scheduling algorithms that are cache-aware to improve locality.
  • Hardware-software co-design for future accelerators, where efficient, fine-grained memory management is a first-class design constraint.
PAGEDATTENTION

Frequently Asked Questions

PagedAttention is a foundational memory management algorithm for transformer inference. These questions address its core mechanisms, benefits, and implementation details for engineers optimizing KV cache usage.

PagedAttention is a memory management algorithm that organizes a transformer model's KV cache into non-contiguous, fixed-size blocks or pages, analogous to virtual memory in operating systems. During the decode phase, the attention mechanism's queries reference these pages via a block table, which maps logical token positions to physical memory blocks. This abstraction allows the system to allocate pages independently for different requests in a continuous batching scenario and to share pages between sequences with common prefixes, eliminating internal fragmentation and enabling near-100% memory utilization for the KV cache.

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.