export RAY_DEDUP_LOGS=0
export HYDRA_FULL_ERROR=1
DEFAULT_YAML="grpo_trainer_qwen25vl_32b"
YAML=${1:-$DEFAULT_YAML}
echo "Use $YAML"
ulimit -n 32768
mkdir -p logs
source /usr/local/Ascend/ascend-toolkit/set_env.sh
source /usr/local/Ascend/nnal/atb/set_env.sh
export PYTORCH_NPU_ALLOC_CONF="expandable_segments:True"
export TASK_QUEUE_ENABLE=2
export CPU_AFFINITY_CONF=2
export HCCL_IF_BASE_PORT=24703
export GLOO_SOCKET_IFNAME="Your SOCKET IFNAME"
export TP_SOCKET_IFNAME="Your SOCKET IFNAME"
export HCCL_SOCKET_IFNAME="Your SOCKET IFNAME"
NNODES=1
NPUS_PER_NODE=16
MASTER_ADDR="xxx.xxx.xxx.xxx"
CURRENT_IP=$(ip -4 addr show $(ip -o -4 route show to default | awk '{print $5}') | grep -oP '(?<=inet\s)\d+(\.\d+){3}')
if [ "$MASTER_ADDR" = "$CURRENT_IP" ]; then
ray start --head --port 6766 --dashboard-host=$MASTER_ADDR --node-ip-address=$CURRENT_IP --dashboard-port=8260 --resources='{"NPU": '$NPUS_PER_NODE'}'
while true; do
ray_status_output=$(ray status)
npu_count=$(echo "$ray_status_output" | grep -oP '(?<=/)\d+\.\d+(?=\s*NPU)' | head -n 1)
npu_count_int=$(echo "$npu_count" | awk '{print int($1)}')
device_count=$((npu_count_int / $NPUS_PER_NODE))
if [ "$device_count" -eq "$NNODES" ]; then
echo "Ray cluster is ready with $device_count devices (from $npu_count NPU resources), starting Python script."
ray status
python posttrain_vlm_grpo.py --config-name $YAML 2>&1 | tee logs/training.log
break
else
echo "Waiting for Ray to allocate $NNODES devices. Current device count: $device_count"
sleep 5
fi
done
else
while true; do
ray start --address="$MASTER_ADDR:6766" --resources='{"NPU": '$NPUS_PER_NODE'}' --node-ip-address=$CURRENT_IP
ray status
if [ $? -eq 0 ]; then
echo "Successfully connected to the Ray cluster!"
break
else
echo "Failed to connect to the Ray cluster. Retrying in 5 seconds..."
sleep 5
fi
done
fi
sleep 999999