第五章:性能优化¶
显存优化¶
调整显存利用率¶
限制序列长度¶
使用量化¶
# AWQ 量化(推荐)
llm = LLM(
model="TheBloke/Qwen2-7B-Instruct-AWQ",
quantization="awq",
)
# FP8 量化
llm = LLM(
model="Qwen/Qwen2-7B-Instruct",
quantization="fp8",
enforce_eager=True,
)
批处理优化¶
增大批处理大小¶
llm = LLM(
model="Qwen/Qwen2-7B-Instruct",
max_num_batched_tokens=32768, # 增大批处理
max_num_seqs=256, # 最大并发序列数
)
批量生成¶
内核优化¶
使用 Flash Attention¶
CUDA Graph¶
推理参数优化¶
from vllm import SamplingParams
sampling_params = SamplingParams(
temperature=0.0, # 确定性输出更快
max_tokens=100,
top_p=1.0,
use_beam_search=False, # 禁用束搜索
)
监控指标¶
# 启用指标
python -m vllm.entrypoints.openai.api_server \
--model Qwen/Qwen2-7B-Instruct \
--enable-metrics
# 访问指标
curl http://localhost:8000/metrics
关键指标¶
| 指标 | 说明 |
|---|---|
| vllm:num_requests_running | 运行中的请求数 |
| vllm:num_requests_waiting | 等待中的请求数 |
| vllm:gpu_cache_usage_perc | GPU 缓存使用率 |
| vllm:time_to_first_token_seconds | 首 token 延迟 |
小结¶
本章学习了 vLLM 的性能优化。下一章学习分布式推理。