Ray 分布式推理(Qwen3-235B-A22B)#
多节点推理适用于模型无法部署在单台机器上的场景。此时,可通过张量并行或流水线并行对模型进行分布式部署。具体的并行策略将在后续章节中介绍。要成功部署多节点推理,需要完成以下三个步骤:
验证多节点通信环境
搭建并启动 Ray 集群
在多节点上启动在线推理服务
验证多节点通信环境#
物理层要求#
物理机器必须位于同一局域网内,且网络互通。
所有 NPU 均已通过光模块连接,且连接状态必须正常。
验证流程#
依次在每个节点上执行以下命令。结果必须全部为 success,且状态必须为 UP:
# Check the remote switch ports
for i in {0..7}; do hccn_tool -i $i -lldp -g | grep Ifname; done
# Get the link status of the Ethernet ports (UP or DOWN)
for i in {0..7}; do hccn_tool -i $i -link -g ; done
# Check the network health status
for i in {0..7}; do hccn_tool -i $i -net_health -g ; done
# View the network detected IP configuration
for i in {0..7}; do hccn_tool -i $i -netdetect -g ; done
# View gateway configuration
for i in {0..7}; do hccn_tool -i $i -gateway -g ; done
# View NPU network configuration
cat /etc/hccn.conf
NPU 互联验证#
1.获取 NPU IP 地址#
for i in {0..7}; do hccn_tool -i $i -ip -g | grep ipaddr; done
2.跨节点 PING 测试#
# Execute on the target node (replace with actual IP)
hccn_tool -i 0 -ping -g address 10.20.0.20
搭建并启动 Ray 集群#
搭建基础容器#
为确保所有节点(包括模型路径和 Python 环境)的执行环境一致,建议使用 Docker 镜像。
要使用 Ray 搭建多节点推理集群,容器化部署是首选方案。应在头节点和工作节点上均启动容器,并使用 --net=host 选项以确保网络连接正常。
以下是在所有节点上执行的示例容器设置命令:
# Update the vllm-ascend image
export IMAGE=quay.nju.edu.cn/ascend/vllm-ascend:v0.23.0.post1
export NAME=vllm-ascend
# Run the container using the defined variables
# Note if you are running bridge network with docker, please expose available ports for multiple nodes communication in advance.
docker run --rm \
--name $NAME \
--net=host \
--shm-size=1g \
--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/devmm_svm \
--device /dev/hisi_hdc \
-v /usr/local/dcmi:/usr/local/dcmi \
-v /usr/local/Ascend/driver/tools/hccn_tool:/usr/local/Ascend/driver/tools/hccn_tool \
-v /usr/local/bin/npu-smi:/usr/local/bin/npu-smi \
-v /usr/local/Ascend/driver/lib64/:/usr/local/Ascend/driver/lib64/ \
-v /usr/local/Ascend/driver/version.info:/usr/local/Ascend/driver/version.info \
-v /etc/ascend_install.info:/etc/ascend_install.info \
# IMPORTANT: This must be a shared directory accessible by all nodes
-v /path/to/shared/cache:/root/.cache \
-it $IMAGE bash
启动 Ray 集群#
备注
When starting a Ray cluster for multi-node inference, the environment variables on each node must be set **before** starting the Ray cluster for them to take effect.
Updating the environment variables requires restarting the Ray cluster.
在每个节点上搭建好容器并安装 vllm-ascend 后,按照以下步骤启动 Ray 集群并执行推理任务。
选择一台机器作为头节点,其余机器作为工作节点。在继续之前,使用 ip addr 检查您的 nic_name(网络接口名称)。
设置 ASCEND_RT_VISIBLE_DEVICES 环境变量以指定要使用的 NPU 设备。对于 Ray 2.1 以上版本,还需设置 RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES 变量以避免设备识别问题。
以下是头节点和工作节点的命令:
头节点:
# Head node
export HCCL_IF_IP={local_ip}
export GLOO_SOCKET_IFNAME={nic_name}
export TP_SOCKET_IFNAME={nic_name}
export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1
export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
ray start --head
工作节点:
# Worker node
export HCCL_IF_IP={local_ip}
export GLOO_SOCKET_IFNAME={nic_name}
export TP_SOCKET_IFNAME={nic_name}
export RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES=1
export ASCEND_RT_VISIBLE_DEVICES=0,1,2,3,4,5,6,7
ray start --address='{head_node_ip}:6379' --node-ip-address={local_ip}
在多节点上启动集群后,执行 ray status 和 ray list nodes 以验证 Ray 集群的状态。应能看到正确的节点数和 NPU 数量。
Ray 成功启动后,将显示以下内容:本地 Ray 实例已成功启动。Dashboard URL:Ray Dashboard 的访问地址(默认:http://localhost:8265);节点状态(CPU/内存资源、健康节点数量);集群连接地址(用于添加多个节点)。
在多节点场景下启动在线推理服务#
在容器中,您可以像所有 NPU 都在单节点上一样使用 vLLM。vLLM 将利用 Ray 集群中所有节点的 NPU 资源。
您只需在一个节点上运行 vllm 命令。
设置并行时,通常的做法是将 tensor-parallel-size 设置为每个节点的 NPU 数量,将 pipeline-parallel-size 设置为节点数量。
例如,在 2 个节点上共有 16 个 NPU(每个节点 8 个 NPU),将张量并行大小设置为 8,流水线并行大小设置为 2:
vllm serve Qwen/Qwen3-235B-A22B \
--distributed-executor-backend ray \
--pipeline-parallel-size 2 \
--tensor-parallel-size 8 \
--enable-expert-parallel \
--seed 1024 \
--max-model-len 8192 \
--max-num-seqs 25 \
--served-model-name qwen \
--trust-remote-code \
--gpu-memory-utilization 0.9
或者,如果您只想使用张量并行,则将张量并行大小设置为集群中 NPU 的总数。例如,在 2 个节点上共有 16 个 NPU,将张量并行大小设置为 16:
vllm serve Qwen/Qwen3-235B-A22B \
--distributed-executor-backend ray \
--tensor-parallel-size 16 \
--enable-expert-parallel \
--seed 1024 \
--max-model-len 8192 \
--max-num-seqs 25 \
--served-model-name qwen \
--trust-remote-code \
--gpu-memory-utilization 0.9
服务器启动后,您可以使用输入提示词查询模型:
curl http://localhost:8000/v1/completions \
-H "Content-Type: application/json" \
-d '{
"model": "qwen",
"prompt": "tell me how to sleep well",
"max_completion_tokens": 100,
"temperature": 0
}'