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.12.0rc1
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 \
-v /path/to/shared/cache:/root/.cache \ # IMPORTANT: This must be a shared directory accessible by all nodes
-it $IMAGE bash

启动 Ray 集群#

在每个节点上设置好容器并安装 vllm-ascend 后,按照以下步骤启动 Ray 集群并执行推理任务。

选择一台机器作为主节点,其他机器作为辅助节点。在进行操作前,使用 `ip addr` 检查您的 `nic_name`(网络接口名称)。

设置 `ASCEND_RT_VISIBLE_DEVICES` 环境变量以指定要使用的 NPU 设备。对于 Ray 2.1 及以上版本,同时设置 `RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES` 变量以避免设备识别问题。

以下是主节点和辅助节点的命令:

主节点:

注意

启动 Ray 集群以进行多节点推理时,每个节点上的环境变量必须在**启动** Ray 集群**之前**设置好才能生效。更新环境变量需要重启 Ray 集群。

# 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

辅助节点:

注意

启动 Ray 集群以进行多节点推理时,每个节点上的环境变量必须在**启动** Ray 集群**之前**设置好才能生效。更新环境变量需要重启 Ray 集群。

# 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 的访问地址(默认为 https://: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 https://:8000/v1/completions \
    -H "Content-Type: application/json" \
    -d '{
        "model": "qwen",
        "prompt": "tell me how to sleep well",
        "max_tokens": 100,
        "temperature": 0
    }'