rl-post-train-artifacts / scripts /run_cfg_task0.sh
myx160's picture
Add files using upload-large-folder tool
456c9d7 verified
Raw History Blame Contribute Delete
12.7 kB
#!/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}"