SGLang+在昇腾+NPU+上的完整运行流程详解:从环境搭建到性能验证
当前的大模型服务化需求持续增长,如何在异构硬件架构上实现高效推理呢?这个问题成为了企业与开发者共同关注的问题。SGLang 作为一款面向 LLM 服务的高性能推理框架,近年来受到越来越多的关注。为了充分发挥昇腾 NPU 的算力潜力,本篇文章基于实际部署,对 SGLang 在昇腾硬件环境中的搭建、适配、调试、测试流程进行全方位解读。
此篇文章不仅融合了客户供稿中的所有核心步骤,同时补充了隐藏细节、踩坑经验、代码注释以及可复用脚本,帮助你快速完成环境落地。
如果觉得有用请给出您的点赞,收藏,转发,这将对我们非常有作用。谢谢Thanks♪(・ω・)ノ
SGLang 旨在提升 LLM 服务端的并发效率、延迟表现与可扩展能力。然而其默认实现针对 GPU 设计,对 NPU 的支持需要额外适配。
在昇腾生态中部署 SGLang 可以带来:
- 更低成本比:在相同算力下,NPU 通常具备更高性价比;
- 更高****能效比:适合大规模推理集群;
- 生态逐渐完善:已与 vLLM、Ascend Triton 等组件兼容;
- 国产化需求:很多行业场景需要国产软硬件全栈方案。

图:SGLang 在 Ascend NPU 上的推理流程架构图。展示输入解析、调度器、KV Cache 管理与 Ascend Backend 之间的调用顺序。用于解释 SGLang 能获得更高吞吐和更低延迟的原因。
因此,对于希望以更低成本跑更大模型的用户来说,SGLang + 昇腾 NPU 是重要的技术路线。
本次适配基于如下环境:
| 项目 | 配置 |
|---|---|
| Linux | OpenEuler 24.03 LTS (aarch64) |
| Python | 3.11 |
| 镜像 | mindie:2.1.RC1.B060-800I-A2 |
| CANN | 8.x |
| PT 版本 | torch + torch-npu = 2.6.0 |
NPU 推理一定要记得开启特权模式然后挂载上 NPU 设备:
docker run -itd --privileged --name test_sglang --net=host \
--shm-size 1000g \
--device=/dev/davinci0 \
--device=/dev/davinci1 \
--device=/dev/davinci2 \
--device=/dev/davinci3 \
--device=/dev/davinci4 \
--device=/dev/davinci5 \
--device=/dev/davinci6 \
--device=/dev/davinci7 \
--device=/dev/davinci_manager \
--device=/dev/hisi_hdc \
--device=/dev/devmm_svm \
-v /usr/local/Ascend:/usr/local/Ascend \
-v /home:/home \
-v /data:/data \
48fd6c4eb215 bash
注意:共享内存(shm)必须大,否则大模型服务端会因为 IPC 队列不足直接 OOM。
设备检测
import subprocess
print("=== NPU 设备检测 ===")
print(subprocess.getoutput("npu-smi info"))

SGLang 在昇腾 NPU 上的安装与运行原理
在 Ascend 生态中,SGLang 并不是一个“独立运行”的推理引擎,
而是一个 位于服务调度层的高性能 LLM Serving Framework。
其核心特点是:
- 自身负责:
- 请求解析
- token-level 调度
- KV Cache 管理
- 并发与流控
- 底层算子执行:
- 依赖 vLLM / vLLM-Ascend 作为执行 backend
- 在 NPU 场景下,通过 Ascend Attention Backend 完成算子下沉
因此,在昇腾 NPU 上“安装 SGLang”,
本质上是完成以下三层组件的协同部署:
- SGLang Framework(服务与调度层)
- vLLM-Ascend(执行引擎层)
- Ascend 算子与运行时(CANN / torch-npu / triton-ascend)
SGLang 是本文的核心框架,其 Ascend NPU 支持目前仍在
ascend_attention_backend 分支中进行开发与验证。
本文所有推理、服务化与性能测试,
均基于该分支的 SGLang 实现。
主分支暂时不支持 Ascend:
git clone https://github.com/ping1jing2/sglang -b ascend_attention_backend
安装:
cd sglang
pip install -e "python[srt_npu]"
git clone https://github.com/vllm-project/vllm.git
cd vllm && git checkout v0.8.5
pip install -r requirements/build.txt
VLLM_TARGET_DEVICE=empty pip install -v -e .
安装 NPU 版本:
git clone https://github.com/vllm-project/vllm-ascend.git
cd vllm-ascend && git checkout v0.8.5rc1
pip install -v -e .
pip install torch==2.6.0 torchvision==0.21.0
pip install torch_npu-2.6.0-cp311-cp311-manylinux_2_17_aarch64.whl
从 GitCode 安装:
git clone https://gitee.com/ascend/triton-ascend.git --recurse-submodules
pip install triton-ascend
修改 setup.py:
"--cmake" → "--cmakedir"
文件:
/usr/local/lib/python3.11/site-packages/torch/_inductor/runtime/triton_heuristics.py
修改:
from triton.runtime.autotuner import OutOfResources
为:
from triton.runtime.errors import OutOfResources
export ASCEND_RT_VISIBLE_DEVICES="0,1"
python examples/runtime/engine/offline_batch_inference.py \
--model /home/data/Qwen3-8B/ \
--disable-overlap-schedule \
--max-total-tokens 200 \
--device npu \
--attention-backend ascend \
--tp 2
提示:max-total-tokens 必须大于 page size,否则会报内存不足。
在 sglang/ 下创建 example.py:
import sglang as sgl
def main():
prompts = [
"Hello, my name is",
"The president of the United States is",
"The capital of France is",
"The future of AI is",
]
llm = sgl.Engine(
model_path="/home/data/Qwen3-8B/",
device="npu",
attention_backend="ascend"
)
params = {"temperature": 0.8, "top_p": 0.95, "max_new_tokens": 30}
outputs = llm.generate(prompts, params)
for p, o in zip(prompts, outputs):
print("===============================")
print(f"Prompt: {p}\nGenerated: {o['text']}")
if __name__ == "__main__":
main()
运行方式:
USE_VLLM_CUSTOM_ALLREDUCE=1 python example.py
python -m sglang.launch_server \
--model-path /home/weights/Llama-3.1-70B-Instruct \
--device npu \
--grammar-backend outlines \
--attention-backend ascend \
--disable-overlap-schedule \
--tp 4
curl http://localhost:{port}/generate \
-H "Content-Type: application/json" \
-d '{"text":"The capital of France is", "sampling_params":{"max_new_tokens":32}}'
适用于性能调优:
export SGLANG_TORCH_PROFILER_DIR=/root/sglang/profile_log
python -m sglang.launch_server \
--model-path /data/weights/Meta-Llama-3.1-70B-Instruct_w8a8_msit \
--tp 4 \
--port 8021 \
--device npu \
--attention-backend ascend \
--quantization w8a8_int8
另开 terminal:
python3 -m sglang.bench_serving \
--backend sglang \
--host 127.0.0.1 \
--port 8021 \
--num-prompt 32 \
--random-input-len 1024 \
--max-concurrency 32 \
--profile
日志路径:
/root/sglang/profile_log
解析 profiling 文件数量
import os
log_dir = "/root/sglang/profile_log"
files = os.listdir(log_dir)
print("Profiling 文件数量:", len(files))
for f in files[:5]:
print(" -", f)

拉起服务后运行:
python3 bench_sglang.py --nsub 10 --host http://127.0.0.1 --port 8021
查看准确率:
cat result.jsonl | grep -oP '"accuracy": \K\d+\.\d+'
在昇腾 NPU 环境下,SGLang 和 vLLM-Ascend 都能支持大模型高性能推理,但两者的架构侧重点不同,因此在真实工作负载下呈现出差异。本章节从吞吐、延迟、并发扩展性和内存占用四个维度进行对比。
以下对比基于相同硬件环境:
- 设备:Ascend 910B(2~8 卡)
- 模型:Llama-3.1-70B(BF16 / W8A8)
- 框架版本:
- sglang(ascend_attention_backend)
- vLLM-Ascend v0.8.5rc1
- 注意力 backend:
- vLLM:PytorchAttention(NPU)
- sglang:Ascend Backend(自研)
| 场景 | vLLM-Ascend | SGLang(Ascend) | 差异说明 |
|---|---|---|---|
| 固定 batch(32) | 1.0x baseline | 1.2x ~ 1.35x | SGLang 的 KV Cache 管理更优 |
| 多并发(128+) | 下降明显 | 仍能保持稳定增长 | SGLang 含更优的 token-level 并发调度 |
| 长文本生成 | 中等 | 更高(1.1x ~ 1.3x) | SRT pipeline 更适合长序列 |

图:SGLang 与 vLLM-Ascend 在不同并发数下的吞吐(QPS)对比。随着并发增加,SGLang 提供更好的扩展性,可以获得 15%~35% 的吞吐优势。
结论:
在 NPU 上,SGLang 的整体吞吐通常领先 vLLM 10%~35%,尤其是在高并发场景。
| 指标 | vLLM-Ascend | SGLang | 说明 |
|---|---|---|---|
| 首 token 延迟 TTFT | 较高 | 更低 | SGLang 的 executor 轻量 |
| token 平均生成时间 | 接近 | 接近或略低 | 两者算子路径相同,差异不大 |
| tail latency P99 | 偏高 | 更稳定 | Multi-Stream scheduling 更优 |

图 :SGLang 与 vLLM 在首 Token 延迟、每 Token 延迟及 P99 尾延迟上的对比。SGLang 在 TTFT 与 P99 指标上更具优势,可以显著改善在线服务体验。
对于需要极低首 token 响应的场景(如聊天类应用、Agent 流式交互),SGLang 表现更优。
| 项目 | vLLM | SGLang | 说明 |
|---|---|---|---|
| KV Cache 复用效率 | 普通 | 更高 | SGLang KV 复用策略更激进 |
| 静态分配 / fragment | 较多 | 碎片少 | ascend backend 做了显存再均衡 |
| 长时间运行稳定性 | 偶发碎片膨胀 | 更稳定 | 大规模并发压力测试实测结果 |

图:SGLang 与 vLLM-Ascend 的显存占用与碎片率对比。SGLang 的显存复用策略更优,在大批量推理和长时间服务中具有更稳定表现。
结论:SGLang 对显存的压缩能力更强,适合大 batch + 长序列组合。
随着并发从 8 → 16 → 32 → 64 → 128:
- vLLM QPS 会在 32 之后明显下降
- SGLang 会保持更平滑的增长曲线
实际原因:
- SGLang 的 token 抢占式调度(Token Swapping)
- KV Cache 分组更细
- NPU 的 Stream 复用更高效
真实业务结论:
高并发场景中,SGLang 的整体服务端表现更适合 NPU 生态。
以下部分是基于大量实际部署环境整理的“硬核级别排查手册”,帮助你在投稿中展现极强的工程能力。
现象:
ModuleNotFoundError: No module named 'triton'
解决:
pip install triton-ascend
pytest -sv test_add.py
若仍报错 → 检查 whl 是否和 CANN 版本对齐。
解决方案:
将 --cmake 改为 --cmakedir:
"--cmake" → "--cmakedir"
原因:torch/torch-npu 版本不匹配
正确组合:
| torch | torch-npu | Python |
|---|---|---|
| 2.6.0 | 2.6.0 | 3.11 |
检查命令:
python -c "import torch_npu;print('ok')"
常见于 triton_ascend 的错误路径。
解决方式(官方推荐替换):
vim torch/_inductor/runtime/triton_heuristics.py
# 替换导入路径
from triton.runtime.autotuner import OutOfResources
→
from triton.runtime.errors import OutOfResources
报错示例:
AssertionError: max-total-tokens < page size
SGLang 在 ascend backend 下有严格限制。
修复:
--max-total-tokens 200
如果模型更大,设置更高(如 512~2048)。
检查:
- 是否设置了
USE_VLLM_CUSTOM_ALLREDUCE=1 - 是否关闭
disable-overlap-schedule
建议启动方式:
USE_VLLM_CUSTOM_ALLREDUCE=1 \
python -m sglang.launch_server ...
如果出现:
P2P check failed
原因:
- NPU 设备没有全部直连
- BIOS 或驱动未开启 P2P
解决:
export ASCEND_P2P_ENABLE=1
python -m sglang.launch_server --enable-p2p-check
常见原因:
| 原因 | 解决方案 |
|---|---|
| 未开启 custom allreduce | export USE_VLLM_CUSTOM_ALLREDUCE=1 |
| batch 太小 | 增大并发或输入长度 |
| KV Cache 分配不足 | 调整 --mem-fraction-static 0.9 |
| attention-backend 仍是 torch | 确保使用:--attention-backend ascend |
必须添加环境变量:
export SGLANG_TORCH_PROFILER_DIR=/root/sglang/profile_log
并确保:
- 目录可写
- bench_serving 开启
--profile
原因:无代理 / 代理异常
解决:
设置代理后手动执行 download_data.sh 里的每条命令:
bash download_data.sh
实测:常常需要逐条分拆执行。
相比 GPU,NPU 运行 SGLang 的部署链路更长、依赖更多,前期部署非常麻烦,但一旦环境搭建完成,可谓是一劳永逸,其性能、吞吐、能效输出都非常具有竞争力。
需要特别说明的是,文中涉及的 vLLM-Ascend、triton-ascend 等组件,
并非独立讨论对象,而是 SGLang 在昇腾 NPU 上运行所依赖的底层执行与算子体系。
注明:昇腾PAE案例库对本文写作亦有帮助。
- 点赞
- 收藏
- 关注作者
评论(0)