Inferensys

Glossary

PagedAttention

PagedAttention is an attention algorithm that manages the Key-Value (KV) cache in non-contiguous, paged memory blocks, enabling efficient memory sharing and utilization for variable-length sequences in large language model serving.
MLOps engineer reviewing model serving infrastructure on laptop, container orchestration visible, technical workspace.
KV CACHE OPTIMIZATION

What is PagedAttention?

PagedAttention is an algorithm for managing the Key-Value (KV) cache in large language model inference, enabling efficient memory sharing for variable-length sequences.

PagedAttention is an attention algorithm, introduced with the vLLM inference engine, that manages the Key-Value (KV) cache in non-contiguous, paged memory blocks analogous to virtual memory in operating systems. This design decouples the logical sequence of tokens from their physical memory layout, allowing for dynamic memory allocation and eliminating the need for pre-allocating large, contiguous buffers for the maximum possible sequence length.

The paged approach enables critical optimizations like memory sharing for identical prompts across multiple requests and highly efficient continuous batching with variable sequence lengths. By reducing internal memory fragmentation and waste, PagedAttention dramatically increases the number of concurrent requests a server can handle, directly improving throughput and reducing the cost per token for LLM serving.

KV CACHE MANAGEMENT

Key Features of PagedAttention

PagedAttention is the core memory management algorithm in the vLLM inference serving engine. It treats the Key-Value (KV) cache of a Transformer model like an operating system manages virtual memory, enabling high throughput for variable-length sequences.

01

Non-Contiguous Memory Paging

PagedAttention divides the KV cache for each sequence into fixed-size blocks, analogous to memory pages. These blocks can be allocated in non-contiguous physical memory (GPU DRAM). This eliminates the need for large, contiguous memory reservations and prevents fragmentation when sequences are of different lengths or are processed in continuous batching.

  • Key Benefit: Enables efficient sharing of GPU memory across multiple requests.
  • Analogy: Similar to how an OS allows a program's memory to be scattered across physical RAM.
02

Efficient Memory Sharing

The algorithm enables two powerful forms of memory sharing, drastically reducing total memory consumption:

  • Shared Prompts: In multi-user or multi-tenant scenarios, identical system prompts or shared context across requests (e.g., a document preamble) are stored in read-only KV cache blocks. All sequences referencing this context point to the same physical memory.
  • Parallel Sampling: When generating multiple outputs (candidates) from a single prompt (e.g., for beam search or sampling), the shared prompt's KV cache is not duplicated. Each candidate sequence reuses these blocks, diverging only in newly generated tokens.

This sharing is managed via a block table that maps logical blocks in a sequence to physical blocks in memory.

03

Block Table & Virtual Addressing

Each sequence maintains a block table, a central data structure that acts as a page table. It maps the sequence's logical KV cache blocks to physical blocks in GPU memory.

  • Logical Blocks: Represent the sequence's KV cache in order.
  • Physical Blocks: The actual allocated memory locations, which may be non-contiguous or shared.
  • Function: During attention computation, the kernel uses this table to gather the required keys and values from potentially scattered physical blocks, performing the equivalent of a virtual-to-physical address translation.

This indirection is what enables flexible allocation, sharing, and on-the-fly memory optimization.

04

Optimized Attention Kernels

PagedAttention requires custom GPU kernels to execute efficiently. These kernels are designed to:

  • Interpret Block Tables: Read the mapping and gather KV data from the dispersed physical blocks.
  • Handle Variable-Length Sequences: Seamlessly compute attention for sequences where the last block may be partially filled.
  • Minimize Overhead: The gather operation is fused into the attention computation, avoiding significant performance penalties compared to naive implementations.

These kernels are a key enabler, making the paging abstraction practical for high-performance inference.

05

Dynamic Allocation & Eviction

Blocks are allocated dynamically as a sequence generates more tokens. When GPU memory is exhausted, vLLM can employ eviction policies to reclaim space.

  • Allocation: New blocks are assigned from a free pool as needed.
  • Eviction: Least-recently-used (LRU) or other policies can swap out blocks of completed or low-priority sequences. In a future extension, blocks could be paged to CPU RAM.
  • Benefit: This allows the system to serve more concurrent requests than would fit if all KV caches were pre-allocated contiguously, maximizing GPU utilization and throughput.
06

Enabler for Continuous Batching

PagedAttention is foundational for continuous batching (iteration-level scheduling). In this paradigm, new requests are added to a running batch as others finish, creating a ragged batch of sequences at different generation stages.

  • Challenge: Traditional contiguous KV cache management cannot handle this efficiently.
  • Solution: PagedAttention's block-based approach allows each sequence to manage its cache independently. The scheduler can add a new sequence by simply allocating a new block table and a few initial blocks, without reorganizing the memory of existing sequences.

This combination is what allows vLLM to achieve near-100% GPU utilization during inference.

KV CACHE MANAGEMENT COMPARISON

PagedAttention vs. Traditional KV Cache Management

A technical comparison of memory management strategies for the Key-Value (KV) cache during autoregressive generation in large language models.

Feature / MetricTraditional KV Cache (Contiguous)PagedAttention (vLLM)

Memory Allocation

Contiguous per sequence

Non-contiguous, paged blocks

Memory Sharing

Handles Variable-Length Sequences

Inefficient, causes fragmentation

Efficient, eliminates internal fragmentation

Memory Utilization

Low (typically 20-40%)

High (near 100%)

Maximum Batch Size

Limited by worst-case sequence length

Dramatically increased, limited by total physical memory

Implementation Complexity

Lower

Higher (requires virtual memory management)

Sequential Decoding Speed

Baseline

Comparable or faster (reduced memory pressure)

Support for Advanced Sampling

Limited (beam search is complex)

Native support for parallel sampling/beam search

PAGEDATTENTION

Frequently Asked Questions

PagedAttention is a foundational algorithm for efficient large language model serving, enabling high-throughput inference by optimizing memory management for the Key-Value (KV) cache. These questions address its core mechanics, benefits, and practical applications.

PagedAttention is an attention algorithm that manages the Key-Value (KV) cache in non-contiguous, paged memory blocks, analogous to virtual memory paging in operating systems. It works by decoupling the logical sequence of tokens from their physical memory layout. Instead of allocating a single contiguous block of memory for each request's KV cache—which leads to fragmentation and wasted capacity due to variable sequence lengths—PagedAttention divides the cache into fixed-size blocks. A block table for each request maps its logical tokens to these physical blocks, allowing blocks to be shared across sequences (e.g., in prompt sharing) and allocated on-demand. This enables the vLLM serving engine to achieve near-zero internal fragmentation and high GPU utilization.

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.