Knowledge note

PagedAttention

An attention algorithm that stores a sequence's KV cache in fixed-size non-contiguous blocks addressed through a per-request block table, enabling paged memory management.

Updated September 16, 2026Confidence: High

PagedAttention

PagedAttention is an attention algorithm that stores a sequence’s keys and values in fixed-size non-contiguous blocks, addressed through a per-request block table, so KV-cache memory can be allocated, shared, and freed at block granularity.

Why It Exists

Autoregressive serving is memory-bound: KV cache grows with every generated token, and pre-reserving contiguous chunks wastes 60-80% of KV memory through reserved, internal, and external fragmentation. PagedAttention makes KV memory behave like OS virtual memory so waste is bounded and freed blocks are reusable.

How It Works

The sequence’s KV cache is partitioned into blocks of $B$ tokens each (default 16). A logical block $j$ holds $K_j$ and $V_j$ and maps through the request’s block table to an arbitrary physical block. Attention computes blockwise:

\[A_{ij} = \frac{\exp(q_i^\top K_j / \sqrt{d})}{\sum_{t=1}^{\lceil i/B \rceil} \exp(q_i^\top K_t \mathbf{1}/\sqrt{d})}, \quad o_i = \sum_{j=1}^{\lceil i/B \rceil} V_j A_{ij}^\top.\]

The kernel fetches each block separately, and a new physical block is allocated only when the previous one fills — bounding per-request waste to at most one block.

Tradeoffs

Block size trades kernel parallelism against fragmentation: larger blocks read more positions in parallel but waste more on short sequences. The kernel pays extra overhead for block-table indirection and variable sequence lengths.

Common Confusions

  • PagedAttention vs. RadixAttention: PagedAttention pages memory at fixed block size and shares via reference counts; SGLang’s RadixAttention additionally organizes cached prefixes in a radix tree for finer-grained reuse.
  • PagedAttention vs. FlashAttention: FlashAttention optimizes the attention compute (tiling, IO); PagedAttention optimizes the KV storage (paging). They are orthogonal and both appear in vLLM.

Where It Appears

  • KV Cache — The stored keys/values that PagedAttention pages.
  • Block Table — The logical-to-physical mapping PagedAttention relies on.
  • Continuous Batching — The iteration-level scheduling that consumes paged capacity.