#!/usr/bin/env bash # Bounded, non-invasive waiter for the C3 resume gate on a shared host. # It never stops another process and delegates all launch guards to the main # CFG launcher after selecting a fresh pair of genuinely idle physical GPUs. set -euo pipefail PROJECT_ROOT="/data/myx/RL_post_train" RESUME_DIR="" MAX_ATTEMPTS="${CFG_RESUME_MAX_ATTEMPTS:-3}" POLL_SECONDS="${CFG_RESUME_POLL_SECONDS:-60}" RUN_PREFIX="recap_cfg_c3_resume" usage() { cat <<'EOF' Usage: wait_for_cfg_resume_gpu.sh --resume-dir PATH [options] Options: --resume-dir PATH Existing global_step_20 checkpoint directory. --max-attempts N Retry bound after a shared-GPU race (default: 3). --poll-seconds N Poll interval (default: 60). --run-prefix NAME Prefix for child launcher run IDs. EOF } while (($# > 0)); do case "$1" in --resume-dir) RESUME_DIR="$2"; shift 2 ;; --max-attempts) MAX_ATTEMPTS="$2"; shift 2 ;; --poll-seconds) POLL_SECONDS="$2"; shift 2 ;; --run-prefix) RUN_PREFIX="$2"; shift 2 ;; -h|--help) usage; exit 0 ;; *) echo "Unknown argument: $1" >&2; usage >&2; exit 2 ;; esac done if [[ -z "${RESUME_DIR}" || ! -d "${RESUME_DIR}" ]]; then echo "--resume-dir must name an existing checkpoint directory" >&2 exit 2 fi if [[ "${RESUME_DIR}" != /data/myx/* ]]; then echo "resume-dir must remain under /data/myx: ${RESUME_DIR}" >&2 exit 2 fi if [[ ! "${MAX_ATTEMPTS}" =~ ^[1-9][0-9]*$ || ! "${POLL_SECONDS}" =~ ^[1-9][0-9]*$ ]]; then echo "max-attempts and poll-seconds must be positive integers" >&2 exit 2 fi if [[ ! "${RUN_PREFIX}" =~ ^[A-Za-z0-9_.-]+$ ]]; then echo "run-prefix contains unsafe characters: ${RUN_PREFIX}" >&2 exit 2 fi LOG_DIR="${PROJECT_ROOT}/logs/recap/cfg" mkdir -p "${LOG_DIR}" LOG_FILE="${LOG_DIR}/c3_resume_wait_$(date +%Y%m%d_%H%M%S).log" exec >>"${LOG_FILE}" 2>&1 echo "CFG_RESUME_WAITER_START resume_dir=${RESUME_DIR} max_attempts=${MAX_ATTEMPTS} poll_seconds=${POLL_SECONDS}" attempt=0 while ((attempt < MAX_ATTEMPTS)); do echo "CFG_RESUME_WAITER_POLL time=$(date --iso-8601=seconds) attempt=${attempt}" if ! gpu_rows="$(nvidia-smi --query-gpu=index,memory.used,utilization.gpu --format=csv,noheader,nounits 2>/dev/null)"; then echo "CFG_RESUME_WAITER_DRIVER_UNAVAILABLE" sleep "${POLL_SECONDS}" continue fi free_gpus=() while IFS=',' read -r gpu memory utilization; do gpu="$(printf '%s' "${gpu}" | tr -d '[:space:]')" memory="$(printf '%s' "${memory}" | tr -d '[:space:]')" utilization="$(printf '%s' "${utilization}" | tr -d '[:space:]')" [[ "${gpu}" =~ ^[0-9]+$ && "${memory}" =~ ^[0-9]+$ && "${utilization}" =~ ^[0-9]+$ ]] || continue compute_pids="$(nvidia-smi -i "${gpu}" --query-compute-apps=pid --format=csv,noheader,nounits 2>/dev/null | awk '$1 ~ /^[0-9]+$/ {print $1}' | tr -d '[:space:]')" echo "CFG_RESUME_WAITER_GPU gpu=${gpu} memory_mb=${memory} util_pct=${utilization} compute_pids=${compute_pids:-none}" if ((memory <= 1024 && utilization <= 5)) && [[ -z "${compute_pids}" ]]; then free_gpus+=("${gpu}") fi done <<<"${gpu_rows}" if ((${#free_gpus[@]} < 2)); then echo "CFG_RESUME_WAITER_WAIT free_count=${#free_gpus[@]}" sleep "${POLL_SECONDS}" continue fi if pgrep -af "[r]aylet|[g]cs_server|[p]lasma_store" >/dev/null 2>&1; then echo "CFG_RESUME_WAITER_RAY_BLOCKED" sleep "${POLL_SECONDS}" continue fi gpus="${free_gpus[0]},${free_gpus[1]}" run_id="${RUN_PREFIX}_$(date +%Y%m%d_%H%M%S)" attempt=$((attempt + 1)) echo "CFG_RESUME_WAITER_LAUNCH run_id=${run_id} gpus=${gpus} attempt=${attempt}" set +e "${PROJECT_ROOT}/scripts/run_cfg_task0.sh" \ --mode c3_resume \ --gpus "${gpus}" \ --resume-dir "${RESUME_DIR}" \ --run-id "${run_id}" rc=$? set -e echo "CFG_RESUME_WAITER_CHILD_DONE run_id=${run_id} exit_code=${rc}" if ((rc == 0)); then echo "CFG_RESUME_WAITER_DONE run_id=${run_id}" exit 0 fi if ((attempt >= MAX_ATTEMPTS)); then echo "CFG_RESUME_WAITER_FAILED attempts=${attempt}" exit "${rc}" fi sleep "${POLL_SECONDS}" done echo "CFG_RESUME_WAITER_FAILED attempts=${attempt}" exit 1