2026/1/10 12:31:08
网站建设
项目流程
网站域名组成,wordpress 直播主题,大红门桥做网站,乌市建设工程质量监督站网站SGLang 在昇腾 NPU 上的完整运行流程详解#xff1a;从环境搭建到性能验证当前的大模型服务化需求持续增长#xff0c;如何在异构硬件架构上实现高效推理呢#xff1f;这个问题成为了企业与开发者共同关注的问题。SGLang 作为一款面向 LLM 服务的高性能推理框架#xff0c;…SGLang 在昇腾 NPU 上的完整运行流程详解从环境搭建到性能验证当前的大模型服务化需求持续增长如何在异构硬件架构上实现高效推理呢这个问题成为了企业与开发者共同关注的问题。SGLang 作为一款面向 LLM 服务的高性能推理框架近年来受到越来越多的关注。为了充分发挥昇腾 NPU 的算力潜力本篇文章基于实际部署对SGLang 在昇腾硬件环境中的搭建、适配、调试、测试流程进行全方位解读。此篇文章不仅融合了客户供稿中的所有核心步骤同时补充了隐藏细节、踩坑经验、代码注释以及可复用脚本帮助你快速完成环境落地。如果觉得有用请给出您的点赞收藏转发这将对我们非常有作用。谢谢Thanks♪(ω)1. 背景为什么要在 NPU 上跑 SGLangSGLang 旨在提升 LLM 服务端的并发效率、延迟表现与可扩展能力。然而其默认实现针对 GPU 设计对 NPU 的支持需要额外适配。在昇腾生态中部署 SGLang 可以带来更低成本比在相同算力下NPU 通常具备更高性价比更高****能效比适合大规模推理集群生态逐渐完善已与 vLLM、Ascend Triton 等组件兼容国产化需求很多行业场景需要国产软硬件全栈方案。图SGLang 在 Ascend NPU 上的推理流程架构图。展示输入解析、调度器、KV Cache 管理与 Ascend Backend 之间的调用顺序。用于解释 SGLang 能获得更高吞吐和更低延迟的原因。因此对于希望以更低成本跑更大模型的用户来说SGLang 昇腾 NPU是重要的技术路线。2. 环境准备镜像、依赖与容器拉起本次适配基于如下环境项目配置LinuxOpenEuler 24.03 LTS (aarch64)Python3.11镜像mindie:2.1.RC1.B060-800I-A2CANN8.xPT 版本torch torch-npu 2.6.02.1 拉起特权容器NPU 推理一定要记得开启特权模式然后挂载上 NPU 设备docker run -itd --privileged --name test_sglang --nethost \ --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))3. SGLang Ascend Backend 的依赖组件安装流程SGLang 在昇腾 NPU 上的安装与运行原理在 Ascend 生态中SGLang 并不是一个“独立运行”的推理引擎而是一个位于服务调度层的高性能LLMServing Framework。其核心特点是自身负责请求解析token-level 调度KV Cache 管理并发与流控底层算子执行依赖 vLLM / vLLM-Ascend 作为执行 backend在 NPU 场景下通过 Ascend Attention Backend 完成算子下沉因此在昇腾 NPU 上“安装 SGLang”本质上是完成以下三层组件的协同部署SGLang Framework服务与调度层vLLM-Ascend执行引擎层Ascend 算子与运行时CANN/ torch-npu / triton-ascend3.1 SGLang 主体安装Ascend Attention BackendSGLang 是本文的核心框架其 Ascend NPU 支持目前仍在ascend_attention_backend 分支中进行开发与验证。本文所有推理、服务化与性能测试均基于该分支的 SGLang 实现。主分支暂时不支持 Ascendgit clone https://github.com/ping1jing2/sglang -b ascend_attention_backend安装cd sglang pip install -e python[srt_npu]3.2 安装 vLLM 与 vLLM-Ascendgit clone https://github.com/vllm-project/vllm.git cd vllm git checkout v0.8.5 pip install -r requirements/build.txt VLLM_TARGET_DEVICEempty 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 .3.3 安装 torch-npupip install torch2.6.0 torchvision0.21.0 pip install torch_npu-2.6.0-cp311-cp311-manylinux_2_17_aarch64.whl3.4 安装 triton-ascend从 GitCode 安装git clone https://gitee.com/ascend/triton-ascend.git --recurse-submodules pip install triton-ascend3.5 常见报错与解决方案1. vLLM-Ascend 安装报错找不到 --cmake修改setup.py--cmake → --cmakedir2. 运行时报错OutOfResources文件/usr/local/lib/python3.11/site-packages/torch/_inductor/runtime/triton_heuristics.py修改from triton.runtime.autotuner import OutOfResources为from triton.runtime.errors import OutOfResources4. 离线推理测试验证模型能跑4.1 双卡离线推理TP2export ASCEND_RT_VISIBLE_DEVICES0,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否则会报内存不足。5. 单机模型推理 Demo在sglang/下创建 example.pyimport 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/, devicenpu, attention_backendascend ) 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(fPrompt: {p}\nGenerated: {o[text]}) if __name__ __main__: main()运行方式USE_VLLM_CUSTOM_ALLREDUCE1 python example.py**6. 服务化部署SGLang Server **NPU6.1 启动服务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 46.2 发送推理请求curl http://localhost:{port}/generate \ -H Content-Type: application/json \ -d {text:The capital of France is, sampling_params:{max_new_tokens:32}}7. Profiling 采集与性能分析适用于性能调优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另开 terminalpython3 -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)8. MMLU 精度验证流程拉起服务后运行python3 bench_sglang.py --nsub 10 --host http://127.0.0.1 --port 8021查看准确率cat result.jsonl | grep -oP accuracy: \K\d\.\d9. 性能对比分析SGLang vs vLLMNPU 环境在昇腾 NPU 环境下SGLang 和 vLLM-Ascend 都能支持大模型高性能推理但两者的架构侧重点不同因此在真实工作负载下呈现出差异。本章节从吞吐、延迟、并发扩展性和内存占用四个维度进行对比。以下对比基于相同硬件环境设备Ascend 910B2~8 卡模型Llama-3.1-70BBF16 / W8A8框架版本sglangascend_attention_backendvLLM-Ascend v0.8.5rc1注意力 backendvLLMPytorchAttentionNPUsglangAscend Backend自研9.1 吞吐QPS/ Token吞吐对比场景vLLM-AscendSGLang(Ascend)差异说明固定 batch321.0xbaseline1.2x ~ 1.35xSGLang 的 KV Cache 管理更优多并发128下降明显仍能保持稳定增长SGLang 含更优的 token-level 并发调度长文本生成中等更高1.1x ~ 1.3xSRT pipeline 更适合长序列图SGLang 与 vLLM-Ascend 在不同并发数下的吞吐QPS对比。随着并发增加SGLang 提供更好的扩展性可以获得 15%~35% 的吞吐优势。结论在 NPU 上SGLang 的整体吞吐通常领先 vLLM 10%~35%尤其是在高并发场景。9.2 单请求延迟TTFT/TPOT对比指标vLLM-AscendSGLang说明首 token 延迟 TTFT较高更低SGLang 的 executor 轻量token 平均生成时间接近接近或略低两者算子路径相同差异不大tail latency P99偏高更稳定Multi-Stream scheduling 更优图 SGLang 与 vLLM 在首 Token 延迟、每 Token 延迟及 P99 尾延迟上的对比。SGLang 在 TTFT 与 P99 指标上更具优势可以显著改善在线服务体验。对于需要极低首 token 响应的场景如聊天类应用、Agent 流式交互SGLang 表现更优。9.3显存占用对比项目vLLMSGLang说明KV Cache 复用效率普通更高SGLang KV 复用策略更激进静态分配 / fragment较多碎片少ascend backend 做了显存再均衡长时间运行稳定性偶发碎片膨胀更稳定大规模并发压力测试实测结果图SGLang 与 vLLM-Ascend 的显存占用与碎片率对比。SGLang 的显存复用策略更优在大批量推理和长时间服务中具有更稳定表现。结论SGLang 对显存的压缩能力更强适合大 batch 长序列组合。9.4 并发扩展能力随着并发从 8 → 16 → 32 → 64 → 128vLLM QPS 会在 32 之后明显下降SGLang 会保持更平滑的增长曲线实际原因SGLang 的 token 抢占式调度Token SwappingKV Cache 分组更细NPU 的 Stream 复用更高效真实业务结论高并发场景中SGLang 的整体服务端表现更适合 NPU 生态。10. 常见错误排查昇腾 NPU SGLang以下部分是基于大量实际部署环境整理的“硬核级别排查手册”帮助你在投稿中展现极强的工程能力。10.1 编译与安装类问题1. triton-ascendUT测试失败现象ModuleNotFoundError: No module named triton解决pip install triton-ascend pytest -sv test_add.py若仍报错 → 检查 whl 是否和 CANN 版本对齐。2. vLLM-Ascend setup.py 报错unknownargument“–cmake”解决方案将--cmake改为--cmakedir--cmake → --cmakedir3. torch_npu 导入失败原因torch/torch-npu 版本不匹配正确组合torchtorch-npuPython2.6.02.6.03.11检查命令python -c import torch_npu;print(ok)10.2 运行时错误4. OutOfResources 报错SGLang 或 vLLM常见于 triton_ascend 的错误路径。解决方式官方推荐替换vim torch/_inductor/runtime/triton_heuristics.py # 替换导入路径 from triton.runtime.autotuner import OutOfResources → from triton.runtime.errors import OutOfResources5. max-total-tokens 太小导致推理错误报错示例AssertionError: max-total-tokens page sizeSGLang 在 ascend backend 下有严格限制。修复--max-total-tokens 200如果模型更大设置更高如 512~2048。6. 服务化卡死 / 无响应检查是否设置了USE_VLLM_CUSTOM_ALLREDUCE1是否关闭disable-overlap-schedule建议启动方式USE_VLLM_CUSTOM_ALLREDUCE1 \ python -m sglang.launch_server ...7.P2P失败tp8 无法启动如果出现P2P check failed原因NPU 设备没有全部直连BIOS 或驱动未开启 P2P解决export ASCEND_P2P_ENABLE1 python -m sglang.launch_server --enable-p2p-check10.3 性能异常类问题8. 性能不如预期吞吐低常见原因原因解决方案未开启 custom allreduceexport USE_VLLM_CUSTOM_ALLREDUCE1batch 太小增大并发或输入长度KV Cache 分配不足调整--mem-fraction-static 0.9attention-backend 仍是 torch确保使用--attention-backend ascend9. Profiling 结果为空必须添加环境变量export SGLANG_TORCH_PROFILER_DIR/root/sglang/profile_log并确保目录可写bench_serving 开启--profile10.4 数据集/精度类问题10. 跑 MMLU 报错下载失败原因无代理 / 代理异常解决设置代理后手动执行 download_data.sh 里的每条命令bash download_data.sh实测常常需要逐条分拆执行。总结相比 GPUNPU 运行 SGLang 的部署链路更长、依赖更多前期部署非常麻烦但一旦环境搭建完成可谓是一劳永逸其性能、吞吐、能效输出都非常具有竞争力。需要特别说明的是文中涉及的 vLLM-Ascend、triton-ascend 等组件并非独立讨论对象而是SGLang 在昇腾 NPU 上运行所依赖的底层执行与算子体系。注明昇腾PAE案例库对本文写作亦有帮助。