AI Skills AI技能 1h ago Updated 57m ago 更新于 57分钟前 50

The 1960s Operating System Trick That Makes vLLM 4x Faster 让vLLM提速4倍的1960年代操作系统技巧

vLLM applies virtual memory paging from operating systems to LLM inference, eliminating the 60-80% GPU memory waste caused by static KV Cache pre-allocation in legacy serving engines PagedAttention partitions KV Cache into flexible 16-token blocks mapped via page tables, reducing memory waste from ~70% to under 4% Three core optimizations enable massive throughput gains: dynamic on-demand block allocation, Copy-on-Write for parallel sampling (sharing up to 30% memory), and reference-counted beam vLLM借鉴操作系统虚拟内存分页技术,将KV Cache划分为16 token的小块,动态按需分配GPU内存,将内存浪费从70%降至4%以下 PagedAttention支持三种核心优化:自回归按需分配、并行采样的Copy-on-Write共享内存、束搜索的动态树剪枝释放内存 当GPU内存超限时,vLLM通过FCFS调度器预empt请求,并可选择Swapping(PCIe传输到CPU内存)或Recomputation(重新prefill)恢复 vLLM面向企业级GPU集群(A100/H100)高吞吐API服务,而Ollama面向本地消费级设备单用户场景,两者定位完全不同 软件架构创新(内存管理

70
Hot 热度
75
Quality 质量
72
Impact 影响力

Analysis 深度分析

TL;DR

  • vLLM applies virtual memory paging from operating systems to LLM inference, eliminating the 60-80% GPU memory waste caused by static KV Cache pre-allocation in legacy serving engines
  • PagedAttention partitions KV Cache into flexible 16-token blocks mapped via page tables, reducing memory waste from ~70% to under 4%
  • Three core optimizations enable massive throughput gains: dynamic on-demand block allocation, Copy-on-Write for parallel sampling (sharing up to 30% memory), and reference-counted beam search pruning (37-55% savings)
  • A centralized FCFS scheduler handles overcommitment through GPU-CPU swapping or recomputation, preventing catastrophic OOM crashes during traffic surges
  • vLLM targets enterprise GPU clusters (A100/H100) with 2x-4x throughput improvements, while Ollama remains optimized for local single-user consumer hardware

Why It Matters

This article demonstrates that software-level memory architecture innovations can unlock performance gains comparable to hardware scaling, a critical insight as GPU supply constraints persist. For AI practitioners deploying LLMs in production, understanding vLLM's paging mechanism is essential for building cost-efficient, high-throughput inference pipelines that serve concurrent users without exponential memory costs. The comparison with Ollama also provides practical guidance on tool selection based on deployment scale and infrastructure.

Technical Details

  • PagedAttention Algorithm: Replaces contiguous KV Cache allocation with a page-table-based system where logical blocks (each holding 16 tokens) map to scattered physical GPU memory pages, enabling dynamic "pay-as-you-go" memory allocation as tokens are generated
  • Copy-on-Write (CoW) Optimization: When generating multiple outputs from the same prompt (e.g., parallel sampling), vLLM shares physical memory blocks across all output streams until divergence points, duplicating only the blocks that change—saving up to 30% memory
  • Beam Search with Reference Counting: Candidate beams sharing history map to identical physical blocks; pruning low-probability beams decrements reference counts, and blocks reaching zero are immediately garbage-collected back to the free pool, yielding 37-55% memory savings
  • Preemption Recovery Strategy: When GPU memory is overcommitted, vLLM uses FCFS scheduling to pause requests and either swaps blocks to CPU memory (faster for large blocks that saturate PCIe bandwidth) or recomputes via a single prefill pass (faster for small blocks where PCIe overhead dominates)
  • Architecture: Centralized orchestrator manages a request queue, translation map (logical-to-physical block table), GPU VRAM pool, and CPU swap space; custom CUDA kernels bypass paging overhead; scales across multiple GPUs via Megatron-LM tensor parallelism

Industry Insight

  • The vLLM approach validates that operating system principles—long proven in general computing—remain highly applicable to modern AI infrastructure, suggesting untapped opportunities for borrowing techniques from other systems domains (e.g., database buffer management, network packet scheduling)
  • For engineering teams evaluating inference serving solutions, the Ollama-vs-vLLM distinction is decisive: Ollama excels for local development and prototyping on consumer hardware, but any production deployment targeting concurrent users or GPU cluster utilization should default to vLLM to avoid the 4-5x throughput penalty of static batching
  • As LLM-based agents generate increasingly long and variable-length sequences, memory-efficient serving becomes the primary scaling bottleneck rather than raw compute—investing in paging-aware infrastructure now positions teams to handle multi-step agentic workloads without proportional hardware cost increases

TL;DR

  • vLLM借鉴操作系统虚拟内存分页技术,将KV Cache划分为16 token的小块,动态按需分配GPU内存,将内存浪费从70%降至4%以下
  • PagedAttention支持三种核心优化:自回归按需分配、并行采样的Copy-on-Write共享内存、束搜索的动态树剪枝释放内存
  • 当GPU内存超限时,vLLM通过FCFS调度器预empt请求,并可选择Swapping(PCIe传输到CPU内存)或Recomputation(重新prefill)恢复
  • vLLM面向企业级GPU集群(A100/H100)高吞吐API服务,而Ollama面向本地消费级设备单用户场景,两者定位完全不同
  • 软件架构创新(内存管理)可释放与硬件扩展同等量级的性能提升,2-4倍吞吐量增益

为什么值得看

本文系统阐述了vLLM如何通过操作系统经典分页技术解决LLM推理的核心瓶颈——GPU内存浪费,为AI从业者理解生产级推理引擎架构提供了清晰的视角。对于需要部署大规模LLM服务的团队,掌握vLLM的内存管理机制是优化吞吐量和降低成本的关键。

技术解析

  • PagedAttention核心机制:将KV Cache从传统连续大块预分配改为16 token的KV Block小块,通过逻辑块表映射到分散的物理GPU内存页,实现动态按需分配,彻底消除静态预分配的内存浪费。

  • 三大生成场景优化:①自回归"按用付费"——每次仅申请一个16 token物理块,填满后才申请下一个;②并行采样Copy-on-Write——多路输出共享提示词物理块,仅在分叉时复制变更块,节省约30%内存;③束搜索动态剪枝——通过引用计数管理块生命周期,剪枝时直接回收内存,节省37%-55%。

  • 内存超限安全机制:当VRAM耗尽时,FCFS调度器预empt最新请求,提供两种恢复策略:小Block优先Recomputation(避免PCIe带宽瓶颈),大Block优先Swapping(充分利用PCIe传输带宽)。

  • 架构对比:Ollama基于llama.cpp,针对Apple Silicon等统一内存架构优化,适合本地单用户;vLLM采用集中式调度器、自定义CUDA内核、支持Megatron-LM张量并行,面向多GPU企业级部署。

行业启示

  • 内存管理成为LLM推理的核心竞争力:随着模型规模增长,计算速度已非主要瓶颈,软件层面的内存优化(如分页、共享、回收)比单纯堆硬件更能提升吞吐量。

  • 生产部署需按场景选型:本地开发/演示用Ollama等轻量工具,企业级高并发API服务必须采用vLLM等支持动态内存管理的推理引擎,否则内存浪费将严重制约并发能力。

  • 操作系统经典技术持续赋能AI基础设施:vLLM的成功证明,将成熟OS概念(虚拟内存分页)迁移到AI推理领域可产生巨大价值,未来更多跨领域架构创新有望解决AI系统的工程瓶颈。

Disclaimer: The above content is generated by AI and is for reference only. 免责声明:以上内容由 AI 生成,仅供参考。

LLM 大模型 Inference 推理 GPU GPU Open Source 开源 Deployment 部署