什么是 vLLM?#

vLLM 是一个高性能的 LLM 推理引擎,由 UC Berkeley 开发。其核心创新是 PagedAttention 算法,解决了 KV Cache 内存碎片化问题。

传统推理的问题#

1
2
3
4
5
6
7
8
9
# 传统方式:预分配固定大小的 KV Cache
max_seq_len = 2048
batch_size = 32
kv_cache = torch.zeros(batch_size, max_seq_len, hidden_dim)

# 问题:
# 1. 内存浪费:实际序列长度可能远小于 max_seq_len
# 2. 碎片化:不同请求的序列长度不同
# 3. 无法动态扩展

PagedAttention 原理#

PagedAttention 借鉴了操作系统的虚拟内存分页机制:

┌─────────────────────────────────────────┐
│           Physical KV Cache             │
│  ┌─────┐ ┌─────┐ ┌─────┐ ┌─────┐       │
│  │ P0  │ │ P1  │ │ P2  │ │ P3  │  ...  │
│  └─────┘ └─────┘ └─────┘ └─────┘       │
└─────────────────────────────────────────┘
          ↑         ↑         ↑
          │         │         │
┌─────────┴─────────┴─────────┴───────────┐
│              Block Table                 │
│  Request A: [P0, P2]                    │
│  Request B: [P1, P3]                    │
└─────────────────────────────────────────┘

核心优势#

  1. 零内存浪费:按需分配 block
  2. 高效共享:Prefix caching,多个请求共享相同前缀
  3. 动态批处理:Continuous batching

实际性能对比#

1
2
3
4
5
6
7
8
$ python benchmark.py --model llama-2-7b

# 基准测试结果
Framework    Throughput (tokens/s)   Memory (GB)
-------------------------------------------------
HuggingFace  120                     32
TensorRT     450                     28
vLLM         1200                    24    ✓ Winner

快速上手#

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
from vllm import LLM, SamplingParams

# 加载模型
llm = LLM(model="meta-llama/Llama-2-7b-chat-hf")

# 配置采样参数
sampling_params = SamplingParams(
    temperature=0.8,
    top_p=0.95,
    max_tokens=256
)

# 批量推理
prompts = ["Hello, my name is", "The capital of France is"]
outputs = llm.generate(prompts, sampling_params)

for output in outputs:
    print(output.outputs[0].text)

生产部署建议#

  1. GPU 内存管理:设置 gpu_memory_utilization=0.9
  2. 并发控制:根据显存调整 max_num_seqs
  3. 量化加速:使用 AWQ/GPTQ 量化模型
  4. 监控指标:关注 time_to_first_tokeninter_token_latency

总结#

vLLM 通过 PagedAttention 实现了:

  • 24x 更高的吞吐量(相比 HuggingFace)
  • 50% 更低的内存占用
  • 近乎零的内存碎片

下一篇我们将探讨如何在 Kubernetes 上部署 vLLM 服务。

1
$ echo "Stay tuned for more AI Infra content!"