File size: 4,045 Bytes
6f003f2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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