Ascend NPUs#

您可以通过以下任何一种方法安装 SGLang。请阅读系统设置部分,确保集群以最高性能运行。如果您遇到任何问题或有任何疑问,欢迎在sglang 这里留下问题。

系统设置#

CPU 性能电源方案#

Ascend 硬件上的默认电源方案是 ondemand,这可能会影响性能,建议将其更改为 performance

echo performance | sudo tee /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor

# 确保更改已成功应用
cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor # 显示 performance

禁用 NUMA 平衡#

sudo sysctl -w kernel.numa_balancing=0

# 检查
cat /proc/sys/kernel/numa_balancing # 显示 0

防止系统内存被交换出去#

sudo sysctl -w vm.swappiness=10

# 检查
cat /proc/sys/vm/swappiness # 显示 10

安装 SGLang#

方法 1:从源代码安装并满足先决条件#

Python 版本#

目前仅支持 python==3.11。如果您不想破坏系统预安装的 python,可以尝试使用 conda 安装。

conda create --name sglang_npu python=3.11
conda activate sglang_npu

MemFabric 适配器#

待办事项:MemFabric 仍然是一个正在进行的项目,计划在2025年底开源。目前我们将它作为预构建的 wheel 包发布。

MemFabric 适配器是 Mooncake 传输引擎的替代品,可在 Ascend NPU 集群上实现 KV 缓存传输。

pip install mf-adapter==1.0.0

Ascend 上的 Pytorch 和 Pytorch 框架适配器#

PYTORCH_VERSION="2.8.0"
TORCHVISION_VERSION="0.23.0"
pip install torch==$PYTORCH_VERSION torchvision==$TORCHVISION_VERSION --index-url https://download.pytorch.org/whl/cpu

PTA_VERSION="2.8.0"
pip install torch-npu==$PTA_VERSION

Ascend 上的 Triton#

注意: 由于 Triton-Ascend 开发迅速,我们建议从源代码安装 triton-ascend。目前 PYPI 上的版本无法跟上。这个问题将在 2025 年 9 月解决,之后 pip install 将是唯一的安装方法。

请按照 Triton-on-Ascend 的从源代码安装指南安装最新的 triton-ascend 包。

DeepEP 兼容库#

我们还提供了一个 DeepEP 兼容库,作为 deepseek-ai 的 DeepEP 库的替代品,请查看安装指南

从源代码安装 SGLang#

# 使用最新的发布分支
git clone -b v0.5.5.post3 https://github.com/sgl-project/sglang.git
cd sglang

pip install --upgrade pip
rm -vf python/pyproject.toml && mv python/pyproject_other.toml python/pyproject.toml
pip install -e python[srt_npu]

方法 2:使用 docker#

注意: RDMA 需要 --privileged--network=host,这通常是 Ascend NPU 集群所需要的。

注意: 以下 docker 命令基于 Atlas 800I A3 机器。如果您使用的是 Atlas 800I A2,请确保只将 davinci[0-7] 映射到容器中。

# 克隆 SGLang 仓库
git clone https://github.com/sgl-project/sglang.git
cd sglang/docker

# 构建 docker 镜像
docker build -t <image_name> -f npu.Dockerfile .

alias drun='docker run -it --rm --privileged --network=host --ipc=host --shm-size=16g \
    --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/davinci8 --device=/dev/davinci9 --device=/dev/davinci10 --device=/dev/davinci11 \
    --device=/dev/davinci12 --device=/dev/davinci13 --device=/dev/davinci14 --device=/dev/davinci15 \
    --device=/dev/davinci_manager --device=/dev/hisi_hdc \
    --volume /usr/local/sbin:/usr/local/sbin --volume /usr/local/Ascend/driver:/usr/local/Ascend/driver \
    --volume /usr/local/Ascend/firmware:/usr/local/Ascend/firmware \
    --volume /etc/ascend_install.info:/etc/ascend_install.info \
    --volume /var/queue_schedule:/var/queue_schedule --volume ~/.cache/:/root/.cache/'

drun --env "HF_TOKEN=<secret>" \
    <image_name> \
    python3 -m sglang.launch_server --model-path meta-llama/Llama-3.1-8B-Instruct --attention-backend ascend --host 0.0.0.0 --port 30000

示例#

运行 DeepSeek-V3#

在 2 台 Atlas 800I A3 上使用 PD 分离运行 DeepSeek。模型权重可以在这里找到。

预填充阶段:

export PYTORCH_NPU_ALLOC_CONF=expandable_segments:True
export ASCEND_MF_STORE_URL="tcp://<PREFILL_HOST_IP>:<PORT>"

drun <image_name> \
    python3 -m sglang.launch_server --model-path State_Cloud/DeepSeek-R1-bf16-hfd-w8a8 \
    --trust-remote-code \
    --attention-backend ascend \
    --mem-fraction-static 0.8 \
    --quantization w8a8_int8 \
    --tp-size 16 \
    --dp-size 1 \
    --nnodes 1 \
    --node-rank 0 \
    --disaggregation-mode prefill \
    --disaggregation-bootstrap-port 6657 \
    --disaggregation-transfer-backend ascend \
    --dist-init-addr <PREFILL_HOST_IP>:6688 \
    --host <PREFILL_HOST_IP> \
    --port 8000

解码阶段:

export PYTORCH_NPU_ALLOC_CONF=expandable_segments:True
export ASCEND_MF_STORE_URL="tcp://<PREFILL_HOST_IP>:<PORT>"
export HCCL_BUFFSIZE=200
export SGLANG_DEEPEP_NUM_MAX_DISPATCH_TOKENS_PER_RANK=24
export SGLANG_NPU_USE_MLAPO=1

drun <image_name> \
    python3 -m sglang.launch_server --model-path State_Cloud/DeepSeek-R1-bf16-hfd-w8a8 \
    --trust-remote-code \
    --attention-backend ascend \
    --mem-fraction-static 0.8 \
    --quantization w8a8_int8 \
    --enable-deepep-moe \
    --deepep-mode low_latency \
    --tp-size 16 \
    --dp-size 1 \
    --ep-size 16 \
    --nnodes 1 \
    --node-rank 0 \
    --disaggregation-mode decode \
    --disaggregation-transfer-backend ascend \
    --dist-init-addr <DECODE_HOST_IP>:6688 \
    --host <DECODE_HOST_IP> \
    --port 8001

Mini_LB:

drun <image_name> \
    python -m sglang.srt.disaggregation.launch_lb \
    --prefill http://<PREFILL_HOST_IP>:8000 \
    --decode http://<DECODE_HOST_IP>:8001 \
    --host 127.0.0.1 --port 5000