#!/usr/bin/env bash # Launch only once the requested cards are actually free, then judge completion # by counting outputs rather than by the worker having exited. # # Both guards exist because of real failures: a previous worker that had not # released its memory made the next job OOM and then hang in NCCL for its full # timeout, while the completion marker fired anyway. set -uo pipefail cd /home/n2501108a/KV/experiments/rope_reindex_allmethods_20260824 WORKER=${WORKER:-./run_worker.sh} REQUESTS=${1:?usage: guarded_launch.sh REQUESTS_JSONL LOG_TAG} TAG=${2:?usage: guarded_launch.sh REQUESTS_JSONL LOG_TAG} # Empty means "any free cards", which is the right default on a shared box. GPUS=${LINGBOT_GPUS:-} NEED=${LINGBOT_NEED:-4} PORT=${LINGBOT_PORT:-29771} # A card counts as free below this; the model alone needs far more, so anything # above it means somebody is resident. FREE_MIB=${FREE_MIB:-2000} EVAL_GPU=${LINGBOT_EVAL_GPU:-0} python=/home/n2501108a/miniconda3/envs/diff/bin/python expected=$(grep -c . "$REQUESTS") log() { echo "[$(date '+%m-%d %H:%M:%S')] $*"; } free_cards() { nvidia-smi --query-gpu=index,memory.used --format=csv,noheader,nounits \ | awk -F', *' -v lim="$FREE_MIB" '$2 <= lim {print $1}' } pick_cards() { local want=$1 chosen if [[ -n "$GPUS" ]]; then local idx used for idx in ${GPUS//,/ }; do used=$(nvidia-smi --id="$idx" --query-gpu=memory.used \ --format=csv,noheader,nounits 2>/dev/null) [[ -z "$used" ]] && return 1 (( used > FREE_MIB )) && return 1 done echo "$GPUS" return 0 fi chosen=$(free_cards | head -n "$want" | paste -sd, -) [[ $(tr -cd , <<<"$chosen" | wc -c) -eq $((want - 1)) ]] || return 1 echo "$chosen" } log "waiting for ${GPUS:-any $NEED} card(s) free (<${FREE_MIB} MiB each)" waited=0 while true; do if use_gpus=$(pick_cards "$NEED"); then break; fi sleep 120 waited=$((waited + 120)) if (( waited % 1800 == 0 )); then log "still waiting (${waited}s); free now: $(free_cards | paste -sd, - )" fi done GPUS=$use_gpus log "using GPUs $GPUS; launching $expected requests" LINGBOT_GPUS="$GPUS" LINGBOT_PORT="$PORT" \ "$WORKER" "$REQUESTS" "logs/${TAG}.log" status=$? log "worker exited with $status" produced=$( "$python" - "$REQUESTS" <<'PY' import json, sys, pathlib n = 0 for line in open(sys.argv[1]): if not line.strip(): continue r = json.loads(line) p = (pathlib.Path(r["output_root"]) / "strict" / r["case_id"] / r["method"] / "continuation.mp4") n += p.exists() print(n) PY ) log "produced $produced / $expected" if [[ "$produced" -ne "$expected" ]]; then log "INCOMPLETE - not scoring, not marking complete" touch ".${TAG}.incomplete" exit 1 fi log "scoring" mapfile -t tags < <( "$python" - "$REQUESTS" <<'PY' import json, sys seen = [] for line in open(sys.argv[1]): if line.strip(): p = json.loads(line)["profile_name"] if p not in seen: seen.append(p) print("\n".join(seen)) PY ) LINGBOT_EVAL_GPU="$EVAL_GPU" ./run_eval.sh "${tags[@]}" touch ".${TAG}.complete" log "done"