Ray分布式部署(Qwen3-235B-A22B)#
多节点推理适用于模型无法部署在单台机器上的场景。在这种情况下,可以使用张量并行(Tensor Parallelism)或流水线并行(Pipeline Parallelism)来分布式部署模型。具体的并行策略将在后续章节中介绍。要成功部署多节点推理,需要完成以下三个步骤:
验证多节点通信环境
搭建并启动 Ray 集群
在多节点环境下启动在线推理服务
验证多节点通信环境#
物理层要求:#
物理机必须位于同一个局域网(LAN)内,且网络连通。
所有 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.13.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 \
--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 集群并执行推理任务。
选择一台机器作为主节点(Primary Node),其余机器作为从节点(Secondary Node)。在开始之前,使用 ip addr 检查您的 nic_name(网卡名称)。
设置 ASCEND_RT_VISIBLE_DEVICES 环境变量来指定要使用的 NPU 设备。对于 2.1 以上版本的 Ray,还需设置 RAY_EXPERIMENTAL_NOSET_ASCEND_RT_VISIBLE_DEVICES 变量以避免设备识别问题。
下面是主节点和从节点的执行命令:
主节点 (Primary node):
备注
启动用于多节点推理的 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
从节点 (Secondary node):
备注
启动用于多节点推理的 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 列表。
After Ray is successfully started, the following content will appear:
A local Ray instance has started successfully.
Dashboard URL: The access address for the Ray Dashboard (default: http://localhost:8265); Node status (CPU/memory resources, number of healthy nodes); Cluster connection address (used for adding multiple nodes).
在多节点场景下启动在线推理服务#
在容器中,您可以像所有 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
}'