多节点 Ray(Qwen / 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. Get NPU IP Addresses#

for i in {0..7}; do hccn_tool -i $i -ip -g | grep ipaddr; done

2. Cross-Node PING Test#

# 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.11.0
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 \
--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 设备。对于 2.1 以上版本的 Ray,还需要设置 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 statusray list nodes 来验证 Ray 集群的状态。你应该能看到正确数量的节点和 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_tokens": 100,
        "temperature": 0
    }'