SGLang 部署 Qwen3 — 生产级高性能推理引擎
SGLang 是什么
SGLang 是一个专为大规模语言模型设计的高性能推理引擎。相比原生 Transformers,它提供了:
- 更高的吞吐量:通过 RadixAttention(前缀缓存)实现请求间的前缀复用
- 更低的延迟:更高效的 KV Cache 管理和调度
- 更好的 MoE 支持:对混合专家架构有专门优化
- 完整的思考模式控制:原生支持 Qwen3 的 thinking mode 参数
安装
# CUDA 12.x(推荐)
pip install sglang[all]
# CUDA 11.8
pip install sglang[all] --extra-index-url https://flashinfer.ai/whl/cu118/torch2.5
# 最小安装(不含 flashinfer 加速)
pip install sglang
验证安装:
python -c "import sglang; print(sglang.__version__)"
启动服务
基础启动
python -m sglang.launch_server \
--model-path Qwen/Qwen3-8B \
--port 30000 \
--host 0.0.0.0
指定思考模式默认值
# 默认强制关闭思考(适合低延迟场景)
python -m sglang.launch_server \
--model-path Qwen/Qwen3-8B \
--thinking-type disabled \
--port 30000
# 默认强制开启思考
python -m sglang.launch_server \
--model-path Qwen/Qwen3-8B \
--thinking-type always \
--port 30000
--thinking-type 可选值:
auto(默认):由模型自行判断disabled:强制关闭思考always:强制开启思考
启动 MoE 模型
python -m sglang.launch_server \
--model-path Qwen/Qwen3-30B-A3B \
--tp-size 2 \ # Tensor Parallelism,2 卡并行
--port 30000
MoE 模型推荐至少 2 卡启动。--tp-size 表示张量并行的 GPU 数量。
多卡并行
# 4 卡张量并行
python -m sglang.launch_server \
--model-path Qwen/Qwen3-32B \
--tp-size 4 \
--port 30000
# 或设置 CUDA_VISIBLE_DEVICES
CUDA_VISIBLE_DEVICES=0,1,2,3 python -m sglang.launch_server \
--model-path Qwen/Qwen3-32B \
--tp-size 4 \
--port 30000
显存紧张时的技巧
# 降低上下文长度限制
python -m sglang.launch_server \
--model-path Qwen/Qwen3-8B \
--mem-fraction-static 0.8 \ # 只用 80% 显存
--context-length 8192 \ # 降低最大上下文
--port 30000
API 调用
SGLang 提供 OpenAI 兼容的 HTTP API。
Python(OpenAI SDK)
from openai import OpenAI
client = OpenAI(
base_url="http://localhost:30000/v1",
api_key="EMPTY"
)
# 强制关闭思考
response = client.chat.completions.create(
model="Qwen/Qwen3-8B",
messages=[
{"role": "user", "content": "1 + 1 = ?"}
],
max_tokens=512,
extra_body={
"thinking": {"type": "disabled"}
}
)
print(response.choices[0].message.content)
# 强制开启思考
response = client.chat.completions.create(
model="Qwen/Qwen3-8B",
messages=[
{"role": "user", "content": "证明勾股定理。"}
],
max_tokens=2048,
extra_body={
"thinking": {"type": "always"}
}
)
print(response.choices[0].message.content)
Python(SGLang 原生 API)
from sglang import Engine, ChatCompletionRequest, SamplingParams
engine = Engine(
model_path="Qwen/Qwen3-8B",
dtype="auto"
)
# 强制关闭思考
req = ChatCompletionRequest(
model="Qwen/Qwen3-8B",
messages=[
{"role": "user", "content": "1 + 1 = ?"}
],
thinking={"type": "disabled"},
sampling_params=SamplingParams(
max_tokens=512,
temperature=0.7
)
)
out = engine.chat.completions.create(req)
print(out.choices[0].message.content)
# 流式输出
req_stream = ChatCompletionRequest(
model="Qwen/Qwen3-8B",
messages=[
{"role": "user", "content": "介绍一下 Qwen3。"}
],
thinking={"type": "disabled"},
sampling_params=SamplingParams(
max_tokens=1024
),
stream=True
)
for chunk in engine.chat.completions.create(req_stream):
if chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")
cURL 调用
# 关闭思考
curl http://localhost:30000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen3-8B",
"messages": [{"role": "user", "content": "1 + 1 = ?"}],
"extra_body": {"thinking": {"type": "disabled"}},
"max_tokens": 512
}'
# 开启思考
curl http://localhost:30000/v1/chat/completions \
-H "Content-Type: application/json" \
-d '{
"model": "Qwen/Qwen3-8B",
"messages": [{"role": "user", "content": "证明勾股定理"}],
"extra_body": {"thinking": {"type": "always"}},
"max_tokens": 2048
}'
性能对比
SGLang 相比原生 Transformers 的性能提升:
| 场景 | Transformers | SGLang | 提升 |
|---|---|---|---|
| 单请求延迟 | 1x | 1.2-1.5x | 更低延迟 |
| 批量请求吞吐量 | 1x | 2-3x | 更高吞吐 |
| MoE 模型 | 一般 | 显著优化 | 路由加速 |
| 多轮对话(相同前缀) | 1x | 5-10x | RadixAttention 前缀复用 |
RadixAttention 是 SGLang 的杀手级特性——当多个请求有相同的前缀(如相同的 system prompt),SGLang 只会计算一次前缀的 KV Cache,后续请求直接复用。这在批量生产环境中吞吐量提升巨大。
思考模式的流式处理
SGLang 对流式思考模式有特殊处理。当思考模式开启时,extra_body 可以设置:
extra_body={
"thinking": {
"type": "auto",
# 设置思考 token 上限(防止思考链过长)
"max_thinking_tokens": 4096
}
}
如果你的客户端能解析 <think> 和 </think> 标签,可以区分思考内容和最终答案。否则直接展示即可。
部署建议
单卡场景
- Qwen3-8B 4bit 量化 → 单卡 8GB 显存
- Qwen3-14B 4bit 量化 → 单卡 12GB 显存
多卡场景
- Qwen3-30B-A3B → 2 卡 24GB(激活参数 3B,计算量小)
- Qwen3-32B → 2 卡 24GB 或 4 卡 16GB
- Qwen3-235B-A22B → 至少 8 卡 48GB
生产环境 Checklist
- [ ] 设置
--mem-fraction-static 0.9充分利用显存 - [ ] 设置合理的
--context-length - [ ] 用
--thinking-type disabled作默认值,复杂请求时单次开启 - [ ] 配置健康检查端点:
GET /health - [ ] 设置
--host 0.0.0.0让其他机器能访问
常见问题
启动报 CUDA OOM
- 换更小的模型
- 降低
--mem-fraction-static - 降低
--context-length - 开启量化(
--quantization fp8或手动加载量化模型)
MoE 模型加载慢
MoE 模型有大量小权重文件。首次加载后会有缓存,第二次会快很多。可以设置 CUDA_MODULE_LOADING=LAZY 加速首次加载:
CUDA_MODULE_LOADING=LAZY python -m sglang.launch_server ...
thinking 参数不生效
确认 SGLang 版本 ≥ 0.4.x。老版本可能不支持 Qwen3 的思考模式控制。
相关阅读
- 05-vLLM 推理服务:另一个主流推理框架
- 03-Transformers 代码调用:原生 Python 调用
- 02-LM Studio 图形界面:不想部署服务?LM Studio 内置 API