qwen3 中文文档 下载 App

SGLang 部署 Qwen3 — 生产级高性能推理引擎

SGLang 是什么

SGLang 是一个专为大规模语言模型设计的高性能推理引擎。相比原生 Transformers,它提供了:

安装

# 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 可选值:

启动 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 的性能提升:

场景TransformersSGLang提升
单请求延迟1x1.2-1.5x更低延迟
批量请求吞吐量1x2-3x更高吞吐
MoE 模型一般显著优化路由加速
多轮对话(相同前缀)1x5-10xRadixAttention 前缀复用

RadixAttention 是 SGLang 的杀手级特性——当多个请求有相同的前缀(如相同的 system prompt),SGLang 只会计算一次前缀的 KV Cache,后续请求直接复用。这在批量生产环境中吞吐量提升巨大。

思考模式的流式处理

SGLang 对流式思考模式有特殊处理。当思考模式开启时,extra_body 可以设置:

extra_body={
    "thinking": {
        "type": "auto",
        # 设置思考 token 上限(防止思考链过长)
        "max_thinking_tokens": 4096
    }
}

如果你的客户端能解析 <think> 和 </think> 标签,可以区分思考内容和最终答案。否则直接展示即可。

部署建议

单卡场景

多卡场景

生产环境 Checklist

常见问题

启动报 CUDA OOM

MoE 模型加载慢

MoE 模型有大量小权重文件。首次加载后会有缓存,第二次会快很多。可以设置 CUDA_MODULE_LOADING=LAZY 加速首次加载:

CUDA_MODULE_LOADING=LAZY python -m sglang.launch_server ...

thinking 参数不生效

确认 SGLang 版本 ≥ 0.4.x。老版本可能不支持 Qwen3 的思考模式控制。

相关阅读