#!/usr/bin/env bash # Non-invasive bounded waiter for the first C4 CFG throughput benchmark. # It only polls GPU state and delegates all launch guards to run_cfg_task0.sh. set -euo pipefail PROJECT_ROOT="/data/myx/RL_post_train" MAX_POLLS="${CFG_C4_MAX_POLLS:-60}" POLL_SECONDS="${CFG_C4_POLL_SECONDS:-60}" RUN_PREFIX="recap_cfg_c4_matrix_m1" NUM_WORKERS="${CFG_C4_NUM_WORKERS:-8}" usage() { cat <<'EOF' Usage: wait_for_cfg_c4_gpu.sh [options] Options: --max-polls N Poll bound (default: 60). --poll-seconds N Poll interval (default: 60). --run-prefix NAME Child C4 run-id prefix. --num-workers N C4 dataloader workers (default: 8). EOF } while (($# > 0)); do case "$1" in --max-polls) MAX_POLLS="$2"; shift 2 ;; --poll-seconds) POLL_SECONDS="$2"; shift 2 ;; --run-prefix) RUN_PREFIX="$2"; shift 2 ;; --num-workers) NUM_WORKERS="$2"; shift 2 ;; -h|--help) usage; exit 0 ;; *) echo "Unknown argument: $1" >&2; usage >&2; exit 2 ;; esac done if [[ ! "${MAX_POLLS}" =~ ^[1-9][0-9]*$ || ! "${POLL_SECONDS}" =~ ^[1-9][0-9]*$ || ! "${NUM_WORKERS}" =~ ^[0-9]+$ ]]; then echo "max-polls/poll-seconds/num-workers must be non-negative integers as appropriate" >&2 exit 2 fi if (( NUM_WORKERS > 16 )); then echo "num-workers must be 0..16" >&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_ROOT="${PROJECT_ROOT}/logs/recap/cfg" mkdir -p "${LOG_ROOT}" LOG_FILE="${LOG_ROOT}/c4_gpu_wait_$(date +%Y%m%d_%H%M%S).log" exec >>"${LOG_FILE}" 2>&1 echo "CFG_C4_WAITER_START max_polls=${MAX_POLLS} poll_seconds=${POLL_SECONDS} run_prefix=${RUN_PREFIX}" poll=0 while (( poll < MAX_POLLS )); do echo "CFG_C4_WAITER_POLL time=$(date --iso-8601=seconds) poll=${poll}" if ! gpu_rows="$(nvidia-smi --query-gpu=index,memory.used,utilization.gpu --format=csv,noheader,nounits 2>/dev/null)"; then echo "CFG_C4_WAITER_DRIVER_UNAVAILABLE" poll=$((poll + 1)) 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_C4_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_C4_WAITER_WAIT free_count=${#free_gpus[@]}" poll=$((poll + 1)) sleep "${POLL_SECONDS}" continue fi if pgrep -af "[r]aylet|[g]cs_server|[p]lasma_store" >/dev/null 2>&1; then echo "CFG_C4_WAITER_RAY_BLOCKED" poll=$((poll + 1)) sleep "${POLL_SECONDS}" continue fi gpus="${free_gpus[0]},${free_gpus[1]}" run_id="${RUN_PREFIX}_$(date +%Y%m%d_%H%M%S)" echo "CFG_C4_WAITER_LAUNCH run_id=${run_id} gpus=${gpus}" set +e env -u CUDA_VISIBLE_DEVICES \ "${PROJECT_ROOT}/scripts/run_cfg_task0.sh" \ --mode c4 \ --gpus "${gpus}" \ --run-id "${run_id}" \ --c4-kind matrix \ --micro-batch-size 1 \ --global-batch-size 2 \ --num-workers "${NUM_WORKERS}" \ --max-steps 12 rc=$? set -e echo "CFG_C4_WAITER_CHILD_DONE run_id=${run_id} exit_code=${rc}" if (( rc == 0 )); then echo "CFG_C4_WAITER_DONE run_id=${run_id}" exit 0 fi if (( rc != 3 )); then echo "CFG_C4_WAITER_FAILED_NONRESOURCE run_id=${run_id} exit_code=${rc}" exit "${rc}" fi poll=$((poll + 1)) sleep "${POLL_SECONDS}" done echo "CFG_C4_WAITER_FAILED poll_bound=${MAX_POLLS}" exit 1