我的征尘是星辰大海。。。
The dirt and dust from my pilgrimage forms oceans of stars...
-------当记忆的篇章变得零碎,当追忆的图片变得模糊,我们只能求助于数字存储的永恒的回忆
作者:黄教授
手机视频列表
被误读的PagedAttention
视频
音频
原始脚本
被误读的 Page Detention AI 操作系统的虚拟内存神话与真相引言现代大模型服务的本质是一套运行在 GPU 之上的 AI 操作系统。 传统操作系统的核心是两件事,进程调度与虚拟内存管理。 AI 操作系统的核心同样是两件事,多用户会话调度与 kv cache 管理。 过去几年,Pytorch Detection 被封为长上下文革命,被类比为 GPU 版虚拟内存,号称能让每个绘画拥有理论无限长上下文。 但在实际高并发推理中,它从未单独生效,它只是高效搬运工,而非打破算力瓶颈的救世主。 真正让长上下文落地的,是一套给 Transformer 强行植入局部性的妥协方案。 一、虚拟内存为什么能成立?因为程序天生有局部性。 传统操作系统的虚拟内存能让32位进程拥有4GB空间。 六十四位进程近乎无限寻址。 核心依赖局部性原理 locality 一、时间局部性,刚访问的数据短期内会被反复访问。 二、空间局部性。 访问某块内存后,大概率会访问相邻区域。 这意味着一个进程即便占用几十 GB 虚拟空间。 每时每刻真正参与计算的只有极小一部分,不活跃的内存页可以被换出到磁盘内存,需要时再按需再入。 多进程切换只搬运少量热页,而非全量数据。 虚拟内存不是把所有数据塞进内存,而是用慢速空间扩容,用局部性保证效率。 这是多任务 OS 能运行半个世纪的底层逻辑。 二、原生 Transformer 彻底违背局部性,虚拟内存逻辑直接失效,大模型推理里。 KV cache 对应进程内存,上下文长度对应虚拟地址空间。 但原生 Transformer 有一个致命特性,每生成一个新 token 都必须对历史所有 token 做一次完整全局 attention 计算。 也就是说,上下文一 k 要算一 k 乘一 k 的注意力,上下文128K必须把128K个 kv 全部载入显存。 算128K乘128K。 上下文 em 必须全量载入,否则矩阵无法计算。 它没有任何局部性。 不访问则已,一访问就是全量遍历。 在这种结构下,所谓分页、换入换出毫无意义。 多用户切换等于把几十 g bkv 全量 swap out。 再载入另一个用户全量 kv 显存带宽瞬间打爆,吞吐量直接归零。 这就是为什么只靠 page detection 根本撑不起长上下文。 三, Page Attention 到底解决了什么?只解决显存碎片化。 Page Attention 来自 VLLM,它的真实贡献非常清晰。 一,把 kv cache 切成固定大小的页,不再需要连续显存。 二,解决传统推理中显存碎片严重、利用率低的问题。 三、让多用户 kv 可以更紧凑的排布,提升并发数。 它是更高效的显存管理器,但不是算力加速器。 它不改变 attention 计算范围。 不减少参与计算的 token 数量,更不能让全量计算变成局部计算。 一句话, Page Detention 优化了搬运方式,却不能减少要搬运的山。 四、让掌上下文可行的真相。 强行植入局部性 AI 操作系统,要想跑起来,必须让 Transformer 变得像普通程序一样拥有局部性。 行业用了三类最现实、最粗暴,也最有效的手段。 一、 sliding window attention 滑动窗口,只保留最近 N 个 token。 如 4K、 8K、 16K。 参与 attention 更早的 kv 直接不计算。 逻辑上下文可以很长,物理计算永远只看最近一小段。 旧 kv 可以安心放在 CPU 内存磁盘,无需再入显存。 二、 sparse attention 稀疏注意力。 不做全量两两计算,只保留局部窗口加少量全局锚点 token 本质依然是减少必须参与计算的 kv 数量,制造局部性。 三、固定配额加 fifo 截断给每个用户会话设置硬显存配额,上下文超限就丢弃最旧 kv 这是最底层的安全锁。 不让任何绘画把显存吃满,保证多用户调度不崩。 这三类方案共同完成了一件事,把全局 attention 降级为局部 attention。 只有这时,Page Attention 的分页、换页、按需再入才有价值。 五、AI 操作系统的完整真相,组合才是灵魂,真实商用大模型服务。 没有例外,都是这套铁三角架构。 一、 Page Detention 做虚拟内存式分页管理,提升显存利用率。 二、滑动窗口到稀疏注意力,制造局部性,控制每次计算规模。 三、绘画调度加 kv 换入换出,模拟进程切换。 实现高并发。 它和传统 OS 完美对应,用户会话等于进程,KV cache 等于进程,虚拟内存减 GPU 显存。 等于物理内存减 CPU 内存,除以磁盘等于 swap 分区减 sliding window 等于局部性约束,page detention 等于页表加内存管理器。 所谓百万上下文,无限上下文,真实含义是逻辑可存储,历史可以存在冷存储,并非全量计算。 永远只加在局部热页参与推理。 解语 pitch 的 tension 不是神话,它是 AI 操作系统的虚拟内存底座。 但虚拟内存能成立,从来不是因为分页技术本身,而是因为程序有局部性。 放到大模型里。 没有滑动窗口,吸收注意力,长度配额, pitch detention 一文不值。 AI 操作系统的长上下文从来不是把所有历史都算一遍。 而是记住所有历史,只算最近的一部分。 这不是技术的妥协,这是物理硬件的终极法则。
修正脚本
被误读的 Page Attention AI 操作系统的虚拟内存神话与真相引言现代大模型服务的本质是一套运行在 GPU 之上的 AI 操作系统。 传统操作系统的核心是两件事,进程调度与虚拟内存管理。 AI 操作系统的核心同样是两件事,多用户会话调度与 kv cache 管理。 过去几年,Paged Attention 被封为长上下文革命,被类比为 GPU 版虚拟内存,号称能让每个会话拥有理论无限长上下文。 但在实际高并发推理中,它从未单独生效,它只是高效搬运工,而非打破算力瓶颈的救世主。 真正让长上下文落地的,是一套给 Transformer 强行植入局部性的妥协方案。 一、虚拟内存为什么能成立?因为程序天生有局部性。 传统操作系统的虚拟内存能让32位进程拥有4GB空间。 六十四位进程近乎无限寻址。 核心依赖局部性原理 locality:一、时间局部性,刚访问的数据短期内会被反复访问。 二、空间局部性。 访问某块内存后,大概率会访问相邻区域。 这意味着一个进程即便占用几十 GB 虚拟空间,每时每刻真正参与计算的只有极小一部分,不活跃的内存页可以被换出到磁盘,需要时再按需换入。 多进程切换只搬运少量热页,而非全量数据。 虚拟内存不是把所有数据塞进内存,而是用慢速空间扩容,用局部性保证效率。 这是多任务 OS 能运行半个世纪的底层逻辑。 二、原生 Transformer 彻底违背局部性,虚拟内存逻辑直接失效。大模型推理里, KV cache 对应进程内存,上下文长度对应虚拟地址空间。 但原生 Transformer 有一个致命特性,每生成一个新 token 都必须对历史所有 token 做一次完整全局 attention 计算。 也就是说,上下文一 k 要算一 k 乘一 k 的注意力,上下文128K必须把128K个 kv 全部载入显存。 算128K乘128K。 上下文都必须全量载入,否则矩阵无法计算。 它没有任何局部性。 不访问则已,一访问就是全量遍历。 在这种结构下,所谓分页、换入换出毫无意义。 多用户切换等于把几十GB KV 全量 swap out。 再载入另一个用户全量 kv 显存带宽瞬间打爆,吞吐量直接归零。 这就是为什么只靠 page attention 根本撑不起长上下文。 三、 Page Attention 到底解决了什么?只解决显存碎片化。 Page Attention 来自 VLLM,它的真实贡献非常清晰。 一、把 kv cache 切成固定大小的页,不再需要连续显存。 二、解决传统推理中显存碎片严重、利用率低的问题。 三、让多用户 kv 可以更紧凑的排布,提升并发数。 它是更高效的显存管理器,但不是算力加速器。 它不改变 attention 计算范围。 不减少参与计算的 token 数量,更不能让全量计算变成局部计算。 一句话, Page Attention 优化了搬运方式,却不能减少要搬运的山。 四、让长上下文可行的真相:强行植入局部性。AI 操作系统要想跑起来,必须让 Transformer 变得像普通程序一样拥有局部性。 行业用了三类最现实、最粗暴,也最有效的手段。 一、 sliding window attention 滑动窗口,只保留最近 N 个 token。 如 4K、 8K、 16K。 参与 attention 更早的 kv 直接不计算。 逻辑上下文可以很长,物理计算永远只看最近一小段。 旧 kv 可以安心放在 CPU 内存、磁盘,无需再入显存。 二、 sparse attention 稀疏注意力。 不做全量两两计算,只保留局部窗口加少量全局锚点 token,本质依然是减少必须参与计算的 kv 数量,制造局部性。 三、固定配额加 fifo 截断给每个用户会话设置硬显存配额,上下文超限就丢弃最旧 kv,这是最底层的安全锁。 不让任何会话把显存吃满,保证多用户调度不崩。 这三类方案共同完成了一件事,把全局 attention 降级为局部 attention。 只有这时,Page Attention 的分页、换页、按需换入才有价值。 五、AI 操作系统的完整真相:组合才是灵魂。真实商用大模型服务, 没有例外,都是这套铁三角架构。 一、 Page Attention 做虚拟内存式分页管理,提升显存利用率。 二、滑动窗口到稀疏注意力,制造局部性,控制每次计算规模。 三、会话调度加 kv 换入换出,模拟进程切换。 实现高并发。 它和传统 OS 完美对应:用户会话等于进程,KV cache 对应进程内存,GPU 显存对应物理内存,CPU 内存加磁盘对应 swap 分区,sliding window 对应局部性约束,page attention 等于页表加内存管理器。 所谓百万上下文,无限上下文,真实含义是逻辑可存储,历史可以存在冷存储,并非全量计算。 永远只让局部热页参与推理。 结语:Page Attention 不是神话,它是 AI 操作系统的虚拟内存底座。 但虚拟内存能成立,从来不是因为分页技术本身,而是因为程序有局部性。 放到大模型里。 没有滑动窗口、稀疏注意力、长度配额,Page Attention 一文不值。 AI 操作系统的长上下文从来不是把所有历史都算一遍。 而是记住所有历史,只算最近的一部分。 这不是技术的妥协,这是物理硬件的终极法则。
英文翻译
The Misinterpreted Page Attention: The Myth and Truth of Virtual Memory in AI Operating Systems Introduction The essence of modern large model services is an AI operating system running on GPUs. The core of traditional operating systems consists of two things: process scheduling and virtual memory management. The core of AI operating systems similarly consists of two things: multi-user session scheduling and KV cache management. In the past few years, Paged Attention has been hailed as a revolution in long contexts, likened to a GPU version of virtual memory, claiming to give each session theoretically infinite context length. However, in actual high-concurrency inference, it has never worked alone. It is merely an efficient transporter, not the savior that breaks the computational bottleneck. What truly enables long contexts is a compromise solution that forcibly implants locality into Transformers. 1. Why Does Virtual Memory Work? Because Programs Inherently Have Locality. The virtual memory of traditional operating systems allows a 32-bit process to have 4GB of space. A 64-bit process has nearly unlimited addressing. The core reliance is on the principle of locality: First, temporal locality—recently accessed data is likely to be accessed again in the short term. Second, spatial locality—after accessing a memory block, nearby memory areas are likely to be accessed. This means that even if a process occupies tens of GB of virtual space, only a tiny portion is actively involved in computation at any moment. Inactive memory pages can be swapped out to disk and swapped back in when needed. Multiprocess switching moves only a small number of hot pages, not the entire dataset. Virtual memory does not cram all data into memory; it expands capacity using slower storage and ensures efficiency through locality. This is the underlying logic that has allowed multitasking OS to run for half a century. 2. The Native Transformer Completely Violates Locality, Rendering Virtual Memory Logic Useless. In large model inference, KV cache corresponds to process memory, and context length corresponds to virtual address space. But the native Transformer has a fatal characteristic: every time a new token is generated, it must perform a complete global attention computation over all previous tokens. In other words, for a context of 1K, it must compute 1K × 1K attention; for a context of 128K, all 128K KV entries must be loaded into GPU memory. It computes 128K × 128K. The entire context must be loaded, otherwise the matrix cannot be computed. It has no locality whatsoever. When it accesses, it accesses everything. Under this structure, paging, swapping in and out are meaningless. Multiuser switching means swapping out tens of GB of KV cache entirely, then loading another user’s entire KV cache—instantly saturating GPU memory bandwidth, and throughput drops to zero. This is why Page Attention alone cannot support long contexts. 3. What Does Page Attention Actually Solve? Only GPU Memory Fragmentation. Page Attention comes from VLLM. Its real contribution is very clear: 1. It splits the KV cache into fixed-size pages, no longer requiring contiguous GPU memory. 2. It solves the severe fragmentation and low utilization of GPU memory in traditional inference. 3. It allows multiple users’ KV caches to be arranged more compactly, increasing concurrency. It is a more efficient memory manager, but not a compute accelerator. It does not change the scope of attention computation. It does not reduce the number of tokens involved in computation, nor does it turn full computation into local computation. In a word, Page Attention optimizes the way data is moved, but cannot reduce the mountain that needs to be moved. 4. The Truth Behind Making Long Contexts Feasible: Forcibly Implanting Locality. For an AI operating system to work, Transformers must be made to have locality like ordinary programs. The industry has adopted three most practical, straightforward, and effective approaches: 1. Sliding window attention: Keeps only the most recent N tokens (e.g., 4K, 8K, 16K) for attention computation. Earlier KV entries are simply not computed. The logical context can be very long, but the physical computation always looks only at a small recent window. Old KV can be safely stored in CPU memory or disk without needing to re-enter GPU memory. 2. Sparse attention: Instead of full pairwise computation, it retains only a local window plus a few global anchor tokens. Essentially, it reduces the number of KV entries that must participate in computation, creating locality. 3. Fixed quota plus FIFO truncation: Sets a hard GPU memory quota for each user session. When the context exceeds the limit, the oldest KV is discarded. This is the bottom-line safety lock, preventing any session from exhausting GPU memory and ensuring multiuser scheduling stability. These three approaches together accomplish one thing: downgrading global attention to local attention. Only then do Page Attention’s paging, swapping, and on-demand loading become valuable. 5. The Complete Truth of the AI Operating System: The Combination is the Soul. Real commercial large model services, without exception, follow this iron triangle architecture: 1. Page Attention provides virtual-memory-style paging management, improving GPU memory utilization. 2. Sliding window to sparse attention creates locality, controlling the scale of each computation. 3. Session scheduling plus KV swapping simulates process switching to achieve high concurrency. It perfectly mirrors the traditional OS: User sessions = processes; KV cache = process memory; GPU memory = physical memory; CPU memory + disk = swap partition; sliding window = locality constraints; Page Attention = page table + memory manager. The so-called million-token context, infinite context, in reality means that history can be stored logically, and historical data can reside in cold storage—not that all of it is computed. Only local hot pages are ever involved in inference. Conclusion: Page Attention is not a myth. It is the virtual memory foundation of the AI operating system. But virtual memory works not because of paging technology itself, but because programs have locality. Applied to large models, without sliding window, sparse attention, and length quotas, Page Attention is worthless. The long context of an AI operating system has never been about recomputing all history. It is about remembering all history but computing only the most recent part. This is not a technological compromise; it is the ultimate law of physical hardware.
back to top