File size: 12,684 Bytes
456c9d7 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 | #!/usr/bin/env bash
# Guarded launcher for staged Task 0 CFG preflights.
# It never auto-selects GPUs and never runs global Ray cleanup.
set -euo pipefail
PROJECT_ROOT="/data/myx/RL_post_train"
SCRIPT_DIR="${PROJECT_ROOT}/scripts"
CONFIG_PATH="${PROJECT_ROOT}/configs/recap/task0_cfg_recap_v1.yaml"
MODE="c2"
GPUS=""
RUN_ID=""
RESUME_DIR=""
DRY_RUN=0
C4_KIND="matrix"
C4_MICRO_BATCH_SIZE=1
C4_GLOBAL_BATCH_SIZE=2
C4_NUM_WORKERS=8
C4_MAX_STEPS=12
IDLE_CHECK_SECONDS="${CFG_GPU_IDLE_CHECK_SECONDS:-30}"
EXTRA_OVERRIDES=()
usage() {
cat <<'EOF'
Usage: run_cfg_task0.sh --gpus PHYSICAL0,PHYSICAL1 [options] [-- hydra overrides]
Options:
--mode c2|c3|c3_resume|c4
Select the bounded preflight (default: c2).
--gpus A,B Exactly two physical GPU indices; no auto-selection.
--run-id ID Project-local log/experiment identifier.
--resume-dir PATH Resume from an exact RLinf checkpoint directory.
--config PATH Project-local CFG YAML override.
--c4-kind matrix|accumulation
Select the bounded C4 benchmark shape (default: matrix).
--micro-batch-size N / --global-batch-size N
C4 batch sizes; accumulation uses global batch 512.
--num-workers N C4 dataloader workers (0..16, default: 8).
--max-steps N C4 total optimizer steps (matrix default: 12; accumulation: 1).
--dry-run Validate and print the command without launching Ray.
-h, --help Show this help.
EOF
}
while (($# > 0)); do
case "$1" in
--mode) MODE="$2"; shift 2 ;;
--gpus) GPUS="$2"; shift 2 ;;
--run-id) RUN_ID="$2"; shift 2 ;;
--resume-dir) RESUME_DIR="$2"; shift 2 ;;
--config) CONFIG_PATH="$2"; shift 2 ;;
--c4-kind) C4_KIND="$2"; shift 2 ;;
--micro-batch-size) C4_MICRO_BATCH_SIZE="$2"; shift 2 ;;
--global-batch-size) C4_GLOBAL_BATCH_SIZE="$2"; shift 2 ;;
--num-workers) C4_NUM_WORKERS="$2"; shift 2 ;;
--max-steps) C4_MAX_STEPS="$2"; shift 2 ;;
--dry-run) DRY_RUN=1; shift ;;
-h|--help) usage; exit 0 ;;
--) shift; EXTRA_OVERRIDES+=("$@"); break ;;
*) echo "Unknown argument: $1" >&2; usage >&2; exit 2 ;;
esac
done
if [[ -z "${GPUS}" ]]; then
echo "CFG launcher requires --gpus PHYSICAL0,PHYSICAL1" >&2
exit 2
fi
if [[ "${MODE}" != "c2" && "${MODE}" != "c3" && "${MODE}" != "c3_resume" && "${MODE}" != "c4" ]]; then
echo "Only bounded c2/c3/c3_resume/c4 modes are enabled; got ${MODE}" >&2
exit 2
fi
if [[ -n "${RAY_ADDRESS:-}" || -n "${CLUSTER_NAMESPACE:-}" ]]; then
echo "Refusing inherited RAY_ADDRESS/CLUSTER_NAMESPACE." >&2
exit 3
fi
IFS=',' read -r GPU0 GPU1 GPU_EXTRA <<< "${GPUS}"
if [[ -n "${GPU_EXTRA:-}" || -z "${GPU0:-}" || -z "${GPU1:-}" ]]; then
echo "--gpus must contain exactly two comma-separated indices" >&2
exit 2
fi
if [[ "${GPU0}" == "${GPU1}" || ! "${GPU0}" =~ ^[0-9]+$ || ! "${GPU1}" =~ ^[0-9]+$ ]]; then
echo "--gpus must contain two distinct non-negative integers" >&2
exit 2
fi
if [[ -z "${RUN_ID}" ]]; then
RUN_ID="recap_cfg_${MODE}_$(date +%Y%m%d_%H%M%S)"
fi
if [[ ! "${RUN_ID}" =~ ^[A-Za-z0-9_.-]+$ ]]; then
echo "run-id contains unsafe characters: ${RUN_ID}" >&2
exit 2
fi
source "${PROJECT_ROOT}/scripts/activate_project_env.sh"
if [[ ! -f "${CONFIG_PATH}" ]]; then
echo "missing CFG config: ${CONFIG_PATH}" >&2
exit 2
fi
CONFIG_PATH="$(realpath -e "${CONFIG_PATH}")"
case "${CONFIG_PATH}" in
"${PROJECT_ROOT}"/*) ;;
*) echo "CFG config must remain under ${PROJECT_ROOT}: ${CONFIG_PATH}" >&2; exit 2 ;;
esac
if [[ -n "${RESUME_DIR}" ]]; then
if [[ ! -d "${RESUME_DIR}" ]]; then
echo "resume-dir does not exist: ${RESUME_DIR}" >&2
exit 2
fi
RESUME_DIR="$(realpath -e "${RESUME_DIR}")"
case "${RESUME_DIR}" in
/data/myx/*) ;;
*) echo "resume-dir must remain under /data/myx: ${RESUME_DIR}" >&2; exit 2 ;;
esac
fi
if [[ "${MODE}" == "c3_resume" && -z "${RESUME_DIR}" ]]; then
echo "c3_resume requires --resume-dir=global_step_20 directory" >&2
exit 2
fi
if [[ "${MODE}" != "c3_resume" && -n "${RESUME_DIR}" ]]; then
echo "--resume-dir is only valid with --mode c3_resume" >&2
exit 2
fi
if [[ "${MODE}" != "c4" && ( "${C4_KIND}" != "matrix" || "${C4_MICRO_BATCH_SIZE}" != "1" || "${C4_GLOBAL_BATCH_SIZE}" != "2" || "${C4_NUM_WORKERS}" != "8" || "${C4_MAX_STEPS}" != "12" ) ]]; then
echo "C4-only options require --mode c4" >&2
exit 2
fi
if [[ "${MODE}" == "c4" ]]; then
if [[ "${C4_KIND}" != "matrix" && "${C4_KIND}" != "accumulation" ]]; then
echo "--c4-kind must be matrix or accumulation" >&2
exit 2
fi
if [[ ! "${C4_MICRO_BATCH_SIZE}" =~ ^[124]$ || ! "${C4_GLOBAL_BATCH_SIZE}" =~ ^[0-9]+$ || ! "${C4_NUM_WORKERS}" =~ ^[0-9]+$ || ! "${C4_MAX_STEPS}" =~ ^[0-9]+$ ]]; then
echo "invalid C4 numeric option" >&2
exit 2
fi
if (( C4_NUM_WORKERS > 16 || C4_MAX_STEPS < 1 || C4_MAX_STEPS > 12 )); then
echo "C4 workers must be 0..16 and max-steps must be 1..12" >&2
exit 2
fi
if [[ "${C4_KIND}" == "matrix" ]]; then
if (( C4_MAX_STEPS < 10 || C4_GLOBAL_BATCH_SIZE != C4_MICRO_BATCH_SIZE * 2 )); then
echo "C4 matrix requires max-steps>=10 and global-batch=micro-batch*2" >&2
exit 2
fi
elif (( C4_GLOBAL_BATCH_SIZE != 512 || C4_MAX_STEPS != 1 )); then
echo "C4 accumulation requires global-batch=512 and max-steps=1" >&2
exit 2
fi
fi
for override in "${EXTRA_OVERRIDES[@]}"; do
case "${override}" in
runner.max_epochs=*|runner.max_steps=*|runner.save_interval=*|\
data.num_workers=*|actor.micro_batch_size=*|actor.global_batch_size=*|\
actor.fsdp_config.sharding_strategy=*|actor.fsdp_config.gradient_checkpointing=*|\
actor.model.model_type=*|actor.model.model_path=*|actor.model.openpi.train_expert_only=*|\
cluster.component_placement.actor=*)
echo "Refusing override of guarded CFG field: ${override}" >&2
exit 2
;;
esac
done
LOG_ROOT="${PROJECT_ROOT}/logs/recap/cfg"
LOG_DIR="${LOG_ROOT}/${RUN_ID}"
RAY_TOKEN="$(printf '%s' "${RUN_ID}" | sha256sum | cut -c1-6)"
# Ray appends a timestamped session/socket suffix. Keep the project-local
# prefix short enough for Linux's 107-byte AF_UNIX socket limit.
RAY_DIR="${PROJECT_ROOT}/tmp/r/${RAY_TOKEN}"
mkdir -p "${LOG_DIR}" "${RAY_DIR}"
check_gpu_idle() {
local gpu="$1" line memory utilization compute_pids
line="$(nvidia-smi -i "${gpu}" --query-gpu=memory.used,utilization.gpu --format=csv,noheader,nounits | tr -d ' ')"
IFS=',' read -r memory utilization <<< "${line}"
compute_pids="$(nvidia-smi -i "${gpu}" --query-compute-apps=pid --format=csv,noheader,nounits 2>/dev/null | tr -d '[:space:]')"
echo "gpu=${gpu} memory_used_mb=${memory} utilization_pct=${utilization} compute_pids=${compute_pids:-none}" >> "${LOG_DIR}/gpu_idle_checks.txt"
[[ "${memory}" =~ ^[0-9]+$ && "${utilization}" =~ ^[0-9]+$ ]]
(( memory <= 1024 && utilization <= 5 )) && [[ -z "${compute_pids}" ]]
}
if ((DRY_RUN)); then
echo "DRY_RUN: skipped NVIDIA driver, idle-window and Ray-process checks" | tee "${LOG_DIR}/status.txt"
else
if ! command -v nvidia-smi >/dev/null 2>&1; then
echo "BLOCKED_A800_DRIVER: nvidia-smi unavailable" | tee "${LOG_DIR}/status.txt" >&2
exit 3
fi
if ! nvidia-smi >"${LOG_DIR}/nvidia-smi.before.txt" 2>&1; then
echo "BLOCKED_A800_DRIVER: nvidia-smi cannot communicate with driver" | tee -a "${LOG_DIR}/status.txt" >&2
exit 3
fi
if ! check_gpu_idle "${GPU0}" || ! check_gpu_idle "${GPU1}"; then
echo "BLOCKED_GPU_AVAILABILITY: first two-card idle check failed" | tee "${LOG_DIR}/status.txt" >&2
exit 3
fi
sleep "${IDLE_CHECK_SECONDS}"
if ! nvidia-smi >"${LOG_DIR}/nvidia-smi.after.txt" 2>&1 || ! check_gpu_idle "${GPU0}" || ! check_gpu_idle "${GPU1}"; then
echo "BLOCKED_GPU_AVAILABILITY: second two-card idle check failed" | tee "${LOG_DIR}/status.txt" >&2
exit 3
fi
if pgrep -af '[r]aylet|[g]cs_server|[p]lasma_store' >"${LOG_DIR}/ray_processes.txt"; then
echo "BLOCKED_RAY_ISOLATION: existing Ray process visible" | tee "${LOG_DIR}/status.txt" >&2
exit 3
fi
fi
export CUDA_VISIBLE_DEVICES="${GPUS}"
export REPO_PATH="${PROJECT_ROOT}/third_party/RLinf"
export MUJOCO_GL=egl
export PYOPENGL_PLATFORM=egl
export AV_LOG_FORCE_NOCOLOR=1
export LIBAV_LOG_LEVEL=quiet
export OPENCV_LOG_LEVEL=off
export RAY_TMPDIR="${RAY_DIR}"
export TMPDIR="${PROJECT_ROOT}/tmp"
if [[ -n "${PYTHONPATH:-}" ]]; then
export PYTHONPATH="${PROJECT_ROOT}/third_party/RLinf:${PROJECT_ROOT}/third_party/openpi/src:${PROJECT_ROOT}/third_party/lerobot:${PYTHONPATH}"
else
export PYTHONPATH="${PROJECT_ROOT}/third_party/RLinf:${PROJECT_ROOT}/third_party/openpi/src:${PROJECT_ROOT}/third_party/lerobot"
fi
# RLinf's placement ranks are physical node-local GPU IDs. The parent
# CUDA_VISIBLE_DEVICES mask alone is insufficient because WorkerGroup writes
# its own per-worker mask; pin the selected physical pair explicitly.
OVERRIDES=(
"runner.max_epochs=-1"
"runner.val_check_interval=-1"
"data.num_workers=0"
"actor.micro_batch_size=1"
"actor.global_batch_size=2"
)
OVERRIDES+=("${EXTRA_OVERRIDES[@]}")
# Escape Hydra's comma sweep separator while retaining an arbitrary physical
# pair (the pair need not be consecutive, so a range would be incorrect).
OVERRIDES+=("cluster.component_placement.actor=${GPU0}\\,${GPU1}")
if [[ "${MODE}" == "c2" ]]; then
OVERRIDES+=("runner.max_steps=1" "runner.save_interval=-1")
elif [[ "${MODE}" == "c3" ]]; then
OVERRIDES+=("runner.max_steps=20" "runner.save_interval=20")
elif [[ "${MODE}" == "c4" ]]; then
OVERRIDES+=(
"runner.max_steps=${C4_MAX_STEPS}"
"runner.save_interval=-1"
"actor.micro_batch_size=${C4_MICRO_BATCH_SIZE}"
"actor.global_batch_size=${C4_GLOBAL_BATCH_SIZE}"
"data.num_workers=${C4_NUM_WORKERS}"
)
else
OVERRIDES+=("runner.max_steps=21" "runner.save_interval=-1")
fi
if [[ -n "${RESUME_DIR}" ]]; then
# The frozen YAML intentionally omits resume_dir; '+' is required under
# Hydra's struct mode to add this run-only field.
OVERRIDES+=("+runner.resume_dir=${RESUME_DIR}")
fi
VALIDATOR_ARGS=(--config "${CONFIG_PATH}" --mode "${MODE}" --require-two-gpus)
if [[ "${MODE}" == "c3" ]]; then
VALIDATOR_ARGS+=(--effective-max-steps 20 --effective-save-interval 20)
elif [[ "${MODE}" == "c3_resume" ]]; then
VALIDATOR_ARGS+=(--effective-max-steps 21 --effective-save-interval -1)
elif [[ "${MODE}" == "c4" ]]; then
VALIDATOR_ARGS+=(
--effective-max-steps "${C4_MAX_STEPS}"
--effective-save-interval -1
--effective-micro-batch-size "${C4_MICRO_BATCH_SIZE}"
--effective-global-batch-size "${C4_GLOBAL_BATCH_SIZE}"
--effective-num-workers "${C4_NUM_WORKERS}"
--c4-kind "${C4_KIND}"
)
fi
"${PROJECT_ROOT}/envs/rlinf-pi05/bin/python" "${SCRIPT_DIR}/validate_cfg_preflight.py" \
"${VALIDATOR_ARGS[@]}" >"${LOG_DIR}/preflight.json"
CMD=(
"${PROJECT_ROOT}/envs/rlinf-pi05/bin/python"
"${PROJECT_ROOT}/third_party/RLinf/examples/recap/cfg/train_cfg.py"
--config-path "$(dirname "${CONFIG_PATH}")"
--config-name "$(basename "${CONFIG_PATH}" .yaml)"
"runner.logger.log_path=${LOG_ROOT}"
"runner.logger.experiment_name=${RUN_ID}"
"hydra.run.dir=${LOG_DIR}"
"${OVERRIDES[@]}"
)
printf '%q ' "${CMD[@]}" >"${LOG_DIR}/command.txt"
printf '\n' >>"${LOG_DIR}/command.txt"
echo "CUDA_VISIBLE_DEVICES=${CUDA_VISIBLE_DEVICES}" >>"${LOG_DIR}/command.txt"
echo "RAY_TMPDIR=${RAY_TMPDIR}" >>"${LOG_DIR}/command.txt"
echo "launcher_pid=$$ pgid=$(ps -o pgid= -p $$ | tr -d ' ')" >>"${LOG_DIR}/command.txt"
if ((DRY_RUN)); then
echo "CFG_LAUNCH_DRY_RUN_PASS log_dir=${LOG_DIR}"
exit 0
fi
echo "GO_CFG_LAUNCH run_id=${RUN_ID} gpus=${GPUS}" | tee "${LOG_DIR}/status.txt"
echo "start_time=$(date --iso-8601=seconds)" >>"${LOG_DIR}/command.txt"
set +e
"${CMD[@]}" 2>&1 | tee "${LOG_DIR}/train.log"
CMD_RC=${PIPESTATUS[0]}
set -e
if ((CMD_RC == 0)) && [[ "${MODE}" == "c3" ]]; then
C3_ACTOR_DIR="${LOG_DIR}/checkpoints/global_step_20/actor"
if [[ ! -d "${C3_ACTOR_DIR}" ]]; then
echo "C3 checkpoint directory missing: ${C3_ACTOR_DIR}" | tee -a "${LOG_DIR}/status.txt" >&2
CMD_RC=4
else
set +e
"${PROJECT_ROOT}/envs/rlinf-pi05/bin/python" \
"${SCRIPT_DIR}/attach_cfg_norm_stats.py" \
--checkpoint "${C3_ACTOR_DIR}" >>"${LOG_DIR}/train.log" 2>&1
ATTACH_RC=$?
set -e
if ((ATTACH_RC != 0)); then
echo "C3 norm-stats attachment failed rc=${ATTACH_RC}" | tee -a "${LOG_DIR}/status.txt" >&2
CMD_RC=${ATTACH_RC}
fi
fi
fi
echo "end_time=$(date --iso-8601=seconds) exit_code=${CMD_RC}" >>"${LOG_DIR}/command.txt"
echo "CFG_LAUNCH_DONE run_id=${RUN_ID} exit_code=${CMD_RC}" | tee -a "${LOG_DIR}/status.txt"
exit "${CMD_RC}"
|