12. MacaRT-SGLang

12.1. MacaRT-SGLang介绍

MacaRT-SGLang是在曦云系列GPU上适配官方SGLang的推理工具,基于MXMACA后端对SGLang方法进行了兼容适配和Kernel优化。 使用MacaRT-SGLang在曦云系列GPU上进行大模型推理,其方法和功能与官方SGLang兼容。当前兼容版本为SGLang 0.4.6,PyTorch 要求为2.6。

12.2. MacaRT-SGLang功能

支持的模型如下表所示。

表 12.1 支持的模型

DeepSeek-R1-BF16

Qwen3-235B-A22B

Qwen3-32B

DeepSeek-R1-W8A8

Qwen3-235B-A22B-W8A8

Qwen3-coder-480B-A35B-Instruct-W8A8

DeepSeek-R1-AWQ

Qwen3-30B-A3B

DeepSeek-R1-Distill-Qwen-1.5B

Qwen3-30B-A3B-GTPQ-W8A8

DeepSeek-R1-W8A8、Qwen3-30B-A3B-GTPQ-W8A8、Qwen3-235B-A22B-W8A8和Qwen3-coder-480B-A35B-Instruct-W8A8模型需要根据沐曦发布的文档自行量化。 比如,DeepSeek-R1-W8A8可参考 相关推理部署手册 中“W8A8模型转换”章节。

12.3. MacaRT-SGLang使用流程

本章节介绍MacaRT-SGLang的使用步骤,主要分为在线推理、吞吐测试和精度测试。

12.3.1. 环境准备

使用MacaRT-SGLang进行推理需要准备好环境。目前仅提供镜像方式。

12.3.1.1. 使用SGLang镜像

从发布的软件包中获取SGLang镜像并启动。详情参见《曦云® 系列通用计算GPU用户指南》中“容器相关场景支持”章节。示例如下:

docker run -itd --privileged=true --device=/dev/dri --device=/dev/mxcd --group-add video --name ${container_name}
--device=/dev/mem --net=host --uts=host --ipc=host --security-opt seccomp=unconfined --security-opt apparmor=unconfined
--shm-size '100gb' --ulimit memlock=-1 -v /mnt/hdd/SGLang:/software ${images_id} /bin/bash
  • ${container_name} 为指定的容器名称

  • /mnt/hdd/SGLang:/software 为指定的挂载目录

  • ${images_id} 为指定的镜像ID,如果需要多机启动,则基于该镜像在多个机器上使用如上指令启动相应的容器

12.3.1.2. 设置环境变量

单机场景下,仅需在一台机器的容器内执行操作;多机场景时,需在所有容器中分别执行一次。

export MACA_SMALL_PAGESIZE_ENABLE=1
export PYTORCH_ENABLE_PG_HIGH_PRIORITY_STREAM=1
export MACA_VISIBLE_DEVICE=7,5,1,3,4,0,6,2 #按机器实际互联顺序设置
export TRITON_ENABLE_MAC_A_OPT_MOVE_DOT_OPERANDS_OUT_LOOP=1
export TRITON_ENABLE_MAC_A_CHAIN_DOT_OPT=1

多机场景下,还需设置以下环境变量:

export GLOO_SOCKET_IFNAME=网口名

对于 GLOO_SOCKET_IFNAME 环境变量,需在宿主机上执行 ifconfig-a 指令获取与该宿主机IP地址对应的网口。

通过 mx-smi topo -m 查看GPU拓扑,不同机器互联的顺序可能不一样。 如果没有按照 0,1,2,3 | 4,5,6,7 顺序排列,那就需要设置环境变量指定GPU的顺序,具体根据 mx-smi topo -m 给出的互联顺序设置,例如:

export MACA_VISIBLE_DEVICE=7,5,1,3,4,0,6,2

12.3.1.3. 启动Server

  • 在性能测试时添加 --disable-radix-cache --disable-chunked-prefix-cache 来禁用缓存可以减少测试时的随机性,实际使用时可以打开。

  • 部分模型支持 EP 并行,通过添加 --enable-ep-moe 来启用。若出现 OOM,可尝试按 0.01 的梯度不断降低 --mem-fraction-static 的值(默认值可从终端打印的启动参数查询),直到运行正常。

不同模型的启动示例如下所示。

  • 对于DeepSeek-R1-BF16模型,以4机32卡为例:

    当前运行建议按照 tp 32 dp 8 的切分方式。

    python3 -m sglang.launch server --model-path ${Model_path} --tp 32 --dp 8 --dist-init-addr 192.168.2.11:5000 --nnodes 4 --node-rank 0 --trust-remote-code --attention-backend flashinfer
    --enable-dp-attention --speculative-algo NEXTN --speculative-draft ${Draft_model_path} --speculative-num-steps 2 --speculative-eagle-topk 1 --speculative-num-draft-tokens 3
    
    python3 -m sglang.launch server --model-path ${Model_path} --tp 32 --dp 8 --dist-init-addr 192.168.2.11:5000 --nnodes 4 --node-rank 1 --trust-remote-code --attention-backend flashinfer
    --enable-dp-attention --speculative-algo NEXTN --speculative-draft ${Draft_model_path} --speculative-num-steps 2 --speculative-eagle-topk 1 --speculative-num-draft-tokens 3
    
    python3 -m sglang.launch server --model-path ${Model_path} --tp 32 --dp 8 --dist-init-addr 192.168.2.11:5000 --nnodes 4 --node-rank 2 --trust-remote-code --attention-backend flashinfer
    --enable-dp-attention --speculative-algo NEXTN --speculative-draft ${Draft_model_path} --speculative-num-steps 2 --speculative-eagle-topk 1 --speculative-num-draft-tokens 3
    
    python3 -m sglang.launch server --model-path ${Model_path} --tp 32 --dp 8 --dist-init-addr 192.168.2.11:5000 --nnodes 4 --node-rank 3 --trust-remote-code --attention-backend flashinfer
    --enable-dp-attention --speculative-algo NEXTN --speculative-draft ${Draft_model_path} --speculative-num-steps 2 --speculative-eagle-topk 1 --speculative-num-draft-tokens 3
    
    • --tp 32 表示tp并行的切分数量为32

    • --enable-dp-attention 表示开启dp并行

    • --dp 8 表示dp并行的切分数量为8

    • --model-path XXX 指定模型存放路径

    • --attention-backend flashinfer 指定attention后端使用flashinfer

    • --dist-init-addr 100.79.153.153:5000 指定主节点的IP和端口号(可默认为5000),其他三个节点和主节点保持一致

    • --nnodes 4 表示节点数量

    • --node-rank 0 表示当前机器所属的节点索引 0。需要注意的是必须先启动主节点,然后才能启动其他节点

    • --speculative-algo NEXTN 表示开启MTP

    • --speculative-draft ${Draft_model_path} 指定Draft模型存放路径

    • --speculative-num-steps--speculative-eagle-topk 以及 --speculative-num-draft-tokens 为MTP参数

  • 对于DeepSeek-R1-W8A8量化模型,使用16卡即可运行。以2机16卡为例:

    当前运行建议按照 tp 16 dp 4 的切分方式。

    python3 -m sglang.launch server --model-path ${Model_path} --tp 16 --dp 4 --dist-init-addr 192.168.2.11:5000 --nnodes 2 --node-rank 0 --trust-remote-code --attention-backend flashinfer
    --enable-dp-attention --speculative-algo NEXTN --speculative-draft ${Draft_model_path} --speculative-num-steps 2 --speculative-eagle-topk 1 --speculative-num-draft-tokens 3
    
    python3 -m sglang.launch server --model-path ${Model_path} --tp 16 --dp 4 --dist-init-addr 192.168.2.11:5000 --nnodes 2 --node-rank 1 --trust-remote-code --attention-backend flashinfer
    --enable-dp-attention --speculative-algo NEXTN --speculative-draft ${Draft_model_path} --speculative-num-steps 2 --speculative-eagle-topk 1 --speculative-num-draft-tokens 3
    
  • 对于DeepSeek-R1-AWQ模型,使用8卡可运行,建议按照 tp 8 dp 4 的切分方式:

    python3 -m sglang.launch server --model-path ${Model_path} --tp 8 --dp 4 --dist-init-addr 192.168.2.11:5000 --nnodes 2 --node-rank 0 --trust-remote-code --attention-backend flashinfer
    --enable-dp-attention --speculative-algo NEXTN --speculative-draft ${Draft_model_path} --speculative-num-steps 2 --speculative-eagle-topk 1 --speculative-num-draft-tokens 3
    
  • 对于DeepSeek-R1-Distil-Qwen-1.5B模型,使用单卡即可运行。以单卡为例:

    python3 -m sglang.launch_server --model-path ${Model_path} --tp 1 --trust-remote-code --attention-backend flashinfer --enable-flashinfer-mla
    
  • 对于 Qwen-235B-A22B 模型,使用 16 卡即可运行。以 16 卡为例:

    python3 -m sglang.launch_server --model-path ${Model_path} --trust-remote-code --attention-backend flashinfer --tp 16 --dp 2 --enable-dp-attention --mem-fraction-static 0.86 --cuda-graph-max-bs 256
    
    • --mem-fraction-static 0.86 表示模型权重与 KV Cache 等静态资源最多占用显存大小的 86%

    • --cuda-graph-max-bs 256 表示 cuda-graph 支持的 BatchSize 最大到 256

  • 对于 Qwen-30B-A3B-GPTQ-W8A8 模型,使用单卡或双卡即可运行。以单卡为例(若要使用双卡运行,设置 --tp 2):

    python3 -m sglang.launch_server --model-path ${Model_path} --trust-remote-code --attention-backend flashinfer --tp 1 --cuda-graph-max-bs 256
    
  • 对于 Qwen-30B-A3B 模型,使用双卡或 4 卡即可运行。

    • 双卡示例:

      python3 -m sglang.launch_server --model-path ${Model_path} --trust-remote-code --attention-backend flashinfer --tp 2 --cuda-graph-max-bs 256
      
    • 4 卡示例:

      python3 -m sglang.launch_server --model-path ${Model_path} --trust-remote-code --attention-backend flashinfer --tp 4 --cuda-graph-max-bs 512
      

12.3.2. 在线推理

待端口顺利启动后,向 Sever 发送请求即可(另起终端)。以下示例简单验证推理服务。

import requests
url = "http://localhost:30000/generate"
data = {"text": "What is the capital of France?"}
response = requests.post(url, json=data)
print(response.json())

12.3.3. 吞吐测试

python3 -m sglang.bench_serving --backend sglang --dataset-name random m --random-input-len ${input-len} --random-output-len ${output-len} --random-range-ratio 1.0 --dataset-path ./dataset/ShareGPT_V3_unfiltered_cleaned_split.json --num-prompt ${batch_size}
  • ${input-len} 表示输入长度

  • ${output-len} 表示输出长度

  • ${batch_size} 指定输入批次数量

12.3.4. 精度测试

12.3.4.1. MMLU精度测试

如果使用MMLU数据集进行精度测试,需要准备data数据。下载 data.tar 文件,解压并拷贝至 dataset 路径。

此外,如果机器无法访问外网,还需要下载 cl100k_base.tiktoken 文件,重命名为 9b5ad71b2ce5302211f9c61530b329a4922fc6a4 后放入容器内任意位置。

  1. 离线安装tiktoken:

    export TIKTOKEN_CACHE_DIR=${path}
    

    ${path} 表示 cl100k_base.tiktoken 文件所在的路径,不需要包含文件名。

  2. 执行 bench_sglang.py 进行精度测试,使用的测试命令行如下:

    python code/bench_sglang.py --nsub 10 --data_dir ./dataset/data
    

    --nsub 指定测试问题的数量,最多为60。如不指定,默认是60。

12.3.4.2. C-Eval精度测试

  1. 执行 run_ceval_client.py 进行精度测试,使用的测试命令行如下:

    python code/run_ceval_client.py --model ${Model_path} --test_jsonl ./dataset/ceval_val_cmcc.jsonl --batch_size 64
    
    • -model ${Model_path} 为model所在的路径。

    • --test_jsonl ./dataset/ceval_val_cmcc.json1 为C-Eval数据集的路径。

该脚本对接的 Server 端口号为 8000,所以需要在启动 SGLang Server 时加上 --port 8000,示例如下:

python -m sglang.launch_server --model ${Model_path} --tp 32 --dist-init-addr 100.79.153.153:5000 --nnodes 4 --node-rank 0 --trust-remote-code --isable-cuda-graph --port 8000