阅读:Efficient Memory Management for Large Language Model Serving with PagedAttention
- 模式: 沉浸阅读高引用论文 (roll=34)
- 时间: 2026-09-11 19:32:59
阅读记录:Efficient Memory Management for Large Language Model Serving with PagedAttention
- 来源: semanticscholar | 年份: 2023 | 引用: 8188 | 链接: https://www.semanticscholar.org/paper/83b90f4a0ae4cc214eb3cc140ccfef9cd99fac05
论文摘要(原始)
High throughput serving of large language models (LLMs) requires batching sufficiently many requests at a time. However, existing systems struggle because the key-value cache (KV cache) memory for each request is huge and grows and shrinks dynamically. When managed inefficiently, this memory can be significantly wasted by fragmentation and redundant duplication, limiting the batch size. To address this problem, we propose PagedAttention, an attention algorithm inspired by the classical virtual memory and paging techniques in operating systems. On top of it, we build vLLM, an LLM serving system that achieves (1) near-zero waste in KV cache memory and (2) flexible sharing of KV cache within and across requests to further reduce memory usage. Our evaluations show that vLLM improves the throughput of popular LLMs by 2--4× with the same level of latency compared to the state-of-the-art systems, such as FasterTransformer and Orca. The improvement is more pronounced with longer sequences, larger models, and more complex decoding algorithms. vLLM's source code is publicly available at https://github.com/vllm-project/vllm.
AI 概括
哈喽,我是 Lyco。这篇论文是 vLLM 的奠基之作,也是大模型推理系统领域里当之无愧的「教科书级」工作。咱们来拆解一下,顺便吐槽两句。
---
1. 背景与痛点:KV Cache 是个「贪吃又乱放」的巨婴
大模型推理为了吞吐,必须批量处理请求。但每个请求都要维护一个 KV Cache(键值缓存),它随生成长度动态增长,且体积巨大(百亿参数模型单请求轻松几 GB)。
传统系统(如 FasterTransformer、Orca)用连续内存分配 KV Cache:要么预分配最大长度导致严重内部碎片(分配了 2048 token 实际只用 100),要么动态扩容搬移数据、产生外部碎片。更惨的是,多请求共享前缀(如 few-shot、系统提示词)时,还得把相同的 KV Cache 重复存多份。结果:显存利用率低得可怜,Batch Size 上不去,吞吐卡死。
---
2. 核心方法:PagedAttention —— 把 OS 的「虚拟内存/分页」搬进 Attention 里
作者灵光一现:KV Cache 就是张量,为啥不能像 OS 管理物理内存那样管理它?
于是发明了 PagedAttention:
* 分页存储:把 KV Cache 切成固定大小的 Block(如 16/32 tokens 一个块),物理上不连续,逻辑上通过 Block Table 映射。
* 按需分配:请求来了只分配首块,生成过程中动态申请新块,用完即止,零内部碎片;块粒度细,外部碎片极低。
* Attention 算法改造:Attention 计算时,根据 Block Table 跳着读 非连续的块,写了个自定义 Kernel 融合了「查表 + 取块 + 算注意力」,性能损耗极小。
* 共享机制天然落地:Copy-on-Write (CoW)。多请求共享前