LLM Learning Roadmap
✨ Awesome-Anything
🖼️ Digital Image Processing
🍃 LLM Components
🌱 LLM Pre-training
☘️ LLM Post-Training
🍀 LLM Popular Models
🪴 LLM Applications
🌿 LLM Optimization
🌾 LLM Compression
🌵 LLM Hands-on Practice
🌴 LLM Must-read Papers
🌳 LLM Q&A
🐝 VLM Image Encoders
📝 MISC.
6.4 PagedAttention
6.4 PagedAttention
6.4.1 Mechanism
Leverage the concept of virtual memory and make good use of fragmented memory.
The performance bottleneck in large language model inference mainly comes from memory.
- First, a large amount of memory is needed to cache the
KandVduring the auto-regressive decoding process. For example, in LLaMA-13B, a single sequence input can require 1.7GB of memory.
- Second, memory usage is dynamic and depends on the input sequence length. Due to fragmentation and pre-allocation overhead, current systems waste 60%-80% of memory.
PagedAttention is inspired by virtual memory and paging at the operating system level. It allows caching the
KV in non-contiguous memory blocks while ensuring logically contiguous access.Specifically, PagedAttention splits each sequence's KV cache into multiple blocks, each block contains a fixed-length of tokens, allowing the attention computation to efficiently locate and retrieve the relevant blocks.
Each fixed-length block can be viewed as a page in virtual memory; tokens can be seen as bytes, and sequences as lines of text. This way, one can use a block table to map logically continuous blocks to physically non-contiguous memory blocks, and the physical blocks can be allocated based on generated tokens.
6.4.2 Inference Workflows
- For single request
- For multiple requests
After all sequences are split into blocks, only the final block may potentially waste memory (in practice, the memory waste is less than 4%). The benefits of high memory utilization are obvious: the system can input more sequences per batch, improving GPU utilization and significantly increasing throughput.
Another advantage of PagedAttention is efficient memory sharing. For example, during parallel sampling, a single prompt may need to generate multiple output sequences. In such cases, both computation and memory for that prompt can be shared across the output sequences.
Block tables naturally enable memory sharing. Similar to how processes share physical pages, in PagedAttention, sequences in different logical blocks can point to the same physical blocks, enabling shared blocks. To ensure safe sharing, PagedAttention tracks reference counts to physical blocks and implements a Copy-on-Write mechanism. Memory sharing reduces memory usage by 55%, significantly lowering the memory footprint of sampling algorithms while boosting throughput by up to 2.2×.
Copy-on-Write: When multiple processes or tasks share the same block of memory (such as a piece of data), they share the memory in a read-only manner. Only when one task needs to modify that memory does the system actually create a copy for it, to prevent affecting other tasks.
<ins/>
6.4.3 Usages in Prefilling and Decoding

A request is sent to the model, asking it to continue a prompt and produce three different responses. This is called parallel sampling.
- Prefilling stage: The vLLM receives sample A1 and sample A2. Based on the prompt content, it allocates logical and physical blocks for them. In this stage, A1 and A2 share block0 and block1 (because they have the same initial prompt).
- Decoding stage: When the tokens generated by the two samples start to differ, copy-on-write is triggered — a new memory block is created in physical memory. At this point, only the logical block1 of A2 is mapped to physical block1, and its ref count is reduced by 1; physical block3 is only mapped to the logical block1 of A1, and its ref count is set to 1.
Below is the model inference of prefilling and decoding stages; it works just as it was, all virtual pages and mappings are managed by the PagedAttention, which is integrated into vLLM.

Prev
6.3 FlashAttention
Next
6.5 Packing for Efficient Training
Loading...
