File size: 3,106 Bytes
a8d795a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/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"