File size: 10,870 Bytes
24aaa76
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#!/bin/bash
# LOOPGUARD-1 (records/R5/LOOPGUARD/design.json + design_amendments.json): the single GPU entry point. Runs INSIDE the one-H100
# allocation started by CODE/scripts/r5_loopguard/lg_gate.sbatch, which checks the reviewed sha256 list
# DATA/outputs/r5/loopguard/READY and then execs: bash lg_run_gpu.sh. Logic of scripts/r5_onemodel/run_gpu.sh (budget,
# stage_rc, resubmission through the gate with sbatch --test-only first). Stages, each resumable (per page inside
# om_eval_pages / om_baseline; done files between stages):
#  0 checks: inside Slurm; data pins lg_pins_val.sha256 (stored 55172 validation predictions, GPU baseline, records, build
#    report, weights, stored crop scores, frozen design and amendments); prep.done.json passed and pins
#    lg_pins_heldout.sha256 (held-out records, detector boxes, exclusion list; hashed only)
#  0b the CONTROL arm A0 (amendment AM1): the unguarded model on all 450 validation pages in this job, batch 8, record order
#  1 om_eval_pages --arm A1 (no_repeat_ngram_size 20) on gen_val, school_val
#  2 om_eval_pages --arm A2 (line-close guard) on gen_val, school_val
#  2b lg_accept.py: A0, A1, A2 complete without error pages; A2 byte-identical to the FRESH A0 on every page where the guard
#    did not fire (differing pages logged with edit distance). Failure stops the job (exit 4): no scoring, no selection, no
#    UNSEAL. The fresh A0 against the stored 55172 outputs is a logged determinism check only (1% reported, never a stop).
#  3 lg_flags (B from the fresh A0), om_score A0 (descriptive), A1, A2, B on validation; any non-zero om_score exit stops
#    the job; om_lg_select select -> selection.json, SELECTED, UNSEAL only if an arm is selected and A1, A2, B are all
#    present and complete
#  4 only if UNSEAL exists: pipeline (om_baseline) on gen_test, school_test with the prep job's CPU boxes; A0 with the
#    observe-only guard on both; the selected arm S on both when S is A1 or A2; clutter_spot with S (max 256 new tokens)
#  5 held-out scoring: om_score S (or B), A0 and B, PRIMARY without the pages of the pinned exclusion file (amendment AM2)
#    and SENSITIVITY with all pages (tags *_all); any non-zero om_score exit stops the job before confirm;
#    om_lg_select confirm (exits non-zero and writes no claim if a primary or sensitivity file is missing or incomplete)
# Exit 99 of a stage = time budget -> resubmission through the gate (at most LG_MAX_RESUBMIT, default 1); other failures stop.
# LIMIT (seconds of this segment) is derived from the allocation's end time minus 120 s (amendment of the review, F4).
# Dry-run switches (environment; all unset in the real job): LG_DRY (stop after selection, never unseal; needs LG_OUT),
# LG_DEVICE (cuda), LG_OUT, LG_LIMIT (overrides the derived limit), LG_X_EVAL (extra om_eval_pages options), LG_X_ACCEPT
# (extra lg_accept options), LG_TEST_ONLY (resubmission stops after the test-only check). Used only by lg_smoke.sbatch.
set -uo pipefail
C=/path/to/TURKIC_OCR_CODE
D=/path/to/TURKIC_OCR_DATA
S=$C/scripts/r5_loopguard
GATE=$S/lg_gate.sbatch
PY=$C/.venv_vlm/bin/python
PK=$C/.venv_kraken/bin/python
unset HF_TOKEN HUGGING_FACE_HUB_TOKEN
export HF_HOME=$D/hf_cache/r5_vlmb_home HF_HUB_OFFLINE=1 TRANSFORMERS_OFFLINE=1 TOKENIZERS_PARALLELISM=false PYTHONDONTWRITEBYTECODE=1
export OMP_NUM_THREADS=16 PYTORCH_CUDA_ALLOC_CONF=expandable_segments:True
OFFICIAL=$D/outputs/r6_loopguard
DRY=${LG_DRY:-}
DEV=${LG_DEVICE:-cuda}
O=${LG_OUT:-$OFFICIAL}
XE=${LG_X_EVAL:-}
XA=${LG_X_ACCEPT:-}
N=${LG_RESUBMIT:-0}
MAXR=${LG_MAX_RESUBMIT:-1}
MODEL=$D/checkpoints/r6_onemodel/seed1/final
A0STORED=$D/outputs/r6_onemodel/page_preds/seed1_final       # job 55172: determinism log only
A0F=$O/page_preds/A0                                          # the fresh control arm of this job
BASEV=$D/outputs/r6_onemodel/baseline/gpu
CROPS="--crop-scores $D/outputs/r6_onemodel/scores --crop-name seed1_final"
UNSEAL=$OFFICIAL/UNSEAL
EXCL=$D/data/processed/r6_loopguard/heldout_exclusions.json     # pinned in lg_pins_heldout.sha256
GUARD=$D/outputs/r5/loopguard/RUNNING${DRY:+_DRY}
[ -n "${SLURM_JOB_ID:-}" ] || { echo "REFUSED: not inside a Slurm allocation"; exit 3; }
if [ -n "${LG_LIMIT:-}" ]; then
  LIMIT=$LG_LIMIT
else
  END=$(squeue -h -j $SLURM_JOB_ID -o %e 2>/dev/null)
  ENDS=$(date -d "$END" +%s 2>/dev/null || echo 0)
  LIMIT=$((ENDS - $(date +%s) - 120))
  if [ "$ENDS" -eq 0 ] || [ $LIMIT -le 0 ]; then LIMIT=14280; echo "allocation end time not readable ($END); LIMIT fallback 14280 s"; fi
fi
if [ -n "$DRY" ] && [ "$(realpath -m $O)" = "$(realpath -m $OFFICIAL)" ]; then echo "REFUSED: a dry run needs its own LG_OUT"; exit 3; fi
if [ -z "$DRY" ] && [ "$O" != "$OFFICIAL" ]; then echo "REFUSED: the real job writes only to $OFFICIAL"; exit 3; fi
if [ -f $GUARD ]; then
  OTHER=$(head -n 1 $GUARD)
  if [ -n "$OTHER" ] && [ "$OTHER" != "$SLURM_JOB_ID" ] && [ -n "$(squeue -h -j $OTHER -t R -o %i 2>/dev/null)" ]; then
    echo "REFUSED: segment $OTHER is still running ($GUARD); exit 3"
    exit 3
  fi
fi
mkdir -p $(dirname $GUARD) $O/page_preds $O/page_scores $O/baseline
echo $SLURM_JOB_ID > $GUARD
fail() { echo "FAILED: $1"; rm -f $GUARD; exit ${2:-1}; }
resubmit() {
  rm -f $GUARD
  if [ "$N" -ge "$MAXR" ]; then echo "resubmission limit reached ($N of $MAXR)"; exit 1; fi
  sbatch --test-only --export=ALL,LG_RESUBMIT=$((N + 1)) $GATE \
    || { echo "RESUBMISSION FAILED (test-only refused); the chain stops here"; exit 1; }
  if [ -n "${LG_TEST_ONLY:-}" ]; then echo "DRY RUN: test-only passed, real sbatch skipped; exit 0"; exit 0; fi
  sbatch --export=ALL,LG_RESUBMIT=$((N + 1)) $GATE \
    || { echo "RESUBMISSION FAILED (sbatch refused); the chain stops here"; exit 1; }
  exit 0
}
left() { echo $((LIMIT - SECONDS - $1)); }
budget() { if [ $(left $1) -lt 60 ]; then echo "budget below 60 s before a stage; resubmitting"; resubmit; fi; }
stage_rc() {
  if [ $1 -eq 99 ]; then resubmit; fi
  if [ $1 -ne 0 ]; then fail "$2 (exit $1); no resubmission" $1; fi
}
setsdone() {  # name, comma list of sets: true if every <set>.done.json says complete
  local s
  for s in ${2//,/ }; do grep -q '"complete": true' $O/page_preds/$1/$s.done.json 2>/dev/null || return 1; done
  return 0
}
evalpages() {  # name, arm, sets, [extra options]
  if setsdone $1 $3; then echo "=== pages $1 $3: done"; return 0; fi
  budget 600
  echo "=== pages $1 (arm $2) $3 $(date -u +%FT%TZ)"
  $PY -I $S/om_eval_pages.py --model-dir $MODEL --name $1 --arm $2 --sets $3 --device $DEV --out-root $O/page_preds \
    --max-seconds $(left 600) ${4:-} $XE
  stage_rc $? "page evaluation $1 $3"
}
baseline() {  # held-out set
  if [ -f $O/baseline/$1.done ]; then return 0; fi
  budget 600
  echo "=== pipeline $1 $(date -u +%FT%TZ)"
  $PY -I $S/om_baseline.py --set $1 --device $DEV --out $O/baseline/$1.jsonl --max-seconds $(left 600)
  stage_rc $? "pipeline $1"
  touch $O/baseline/$1.done
}
score() {  # tag, om_score options...: any non-zero exit stops the job before a selection or a claim
  local tag=$1
  shift
  OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 MKL_NUM_THREADS=1 $PK -I $S/om_score.py --tag $tag "$@" > $O/page_scores/stdout_$tag.json
  local rc=$?
  echo "om_score $tag rc $rc"
  [ $rc -eq 0 ] || fail "om_score $tag (exit $rc); no selection or claim is made" 5
}
hostname; nvidia-smi --query-gpu=name,memory.total --format=csv,noheader 2>/dev/null || echo "no GPU visible (device $DEV)"; date -u +%FT%TZ
echo "LOOPGUARD-1 job $SLURM_JOB_ID, resubmission $N of $MAXR, device $DEV, out $O, limit $LIMIT s, dry ${DRY:-no}"
# ---- 0 checks
( cd / && sha256sum -c --quiet $S/lg_pins_val.sha256 ) || fail "a pinned validation input changed" 3
if [ -z "$DRY" ]; then
  grep -q '"passed": true' $OFFICIAL/prep.done.json 2>/dev/null || fail "prep.done.json missing or not passed" 3
  ( cd / && sha256sum -c --quiet $S/lg_pins_heldout.sha256 ) || fail "a pinned held-out input changed" 3
fi
echo "=== checks passed $(date -u +%FT%TZ)"
# ---- 0b the fresh control arm A0 (unguarded, all validation pages, batch 8, no batch-1 probe)
evalpages A0 A0 gen_val,school_val "--batch 8 --batch1-probe 0"
# ---- 1, 2 validation arms
evalpages A1 A1 gen_val,school_val
evalpages A2 A2 gen_val,school_val
# ---- 2b acceptance (before any score is read)
$PK -I $S/lg_accept.py --a0 $A0F --a1 $O/page_preds/A1 --a2 $O/page_preds/A2 --stored $A0STORED --tol 0.01 --out $O/acceptance.json $XA
RC=$?
if [ $RC -ne 0 ]; then
  [ -z "$DRY" ] && fail "acceptance checks (exit $RC): investigate before scoring; no selection, no UNSEAL" 4
  echo "DRY RUN: acceptance failed (exit $RC), continuing to exercise the scorer"
fi
# ---- 3 validation scoring and selection
if [ ! -f $O/selection.json ]; then
  echo "=== validation scoring $(date -u +%FT%TZ)"
  $PK -I $S/lg_flags.py val --a2 $O/page_preds/A2 --a0 $A0F --out $O/flags_val.json || fail "flags"
  VAL="--page-preds $O/page_preds --baseline-dir $BASEV $CROPS --out $O/page_scores"
  score A0 --name A0 $VAL
  score A1 --name A1 $VAL
  score A2 --name A2 $VAL
  score B --name A0 --fallback-flags $O/flags_val.json $VAL
  $PK -I $S/om_lg_select.py select --root $O --a0 $A0F --stored $A0STORED ${DRY:+--no-unseal}
  RC=$?
  [ $RC -eq 0 ] || fail "selection (exit $RC)" $RC
fi
SEL=$(head -n 1 $O/SELECTED)
echo "selected: $SEL"
if [ -n "$DRY" ]; then echo "DRY RUN: stops after selection; held-out stages are never run in a dry run"; rm -f $GUARD; exit 0; fi
# ---- 4 held-out runs, only under the unseal token
if [ ! -f $UNSEAL ]; then
  if [ "$SEL" != "NONE" ]; then fail "arm $SEL was selected but no unseal token exists (see selection.json unseal_decision)" 6; fi
  echo "=== no arm eligible: the held-out sets stay sealed $(date -u +%FT%TZ)"
  touch $O/lg_run.done; rm -f $GUARD; exit 0
fi
baseline gen_test
baseline school_test
evalpages A0obs A0 gen_test,school_test "--observe"
case "$SEL" in
  A1|A2)
    evalpages $SEL $SEL gen_test,school_test
    evalpages $SEL $SEL clutter_spot "--max-new 256"
    ;;
esac
# ---- 5 held-out scoring and confirmation
if [ ! -f $O/confirm.json ]; then
  echo "=== held-out scoring $(date -u +%FT%TZ)"
  $PK -I $S/lg_flags.py heldout --a0obs $O/page_preds/A0obs --out $O/flags_test.json || fail "held-out flags"
  HO="--sets gen_test,school_test --baseline-dir $O/baseline $CROPS --out $O/page_scores --page-preds $O/page_preds"
  for V in primary all; do
    if [ $V = primary ]; then X="--exclude-ids $EXCL"; SFX=""; else X=""; SFX="_all"; fi
    case "$SEL" in
      A1|A2) score ${SEL}_test$SFX --name $SEL $HO $X;;
    esac
    score A0_test$SFX --name A0obs $HO $X
    score B_test$SFX --name A0obs --fallback-flags $O/flags_test.json $HO $X
  done
  $PK -I $S/om_lg_select.py confirm --root $O --exclude-ids $EXCL
  RC=$?
  [ $RC -eq 0 ] || fail "confirm (exit $RC): no claim written" $RC
fi
touch $O/lg_run.done
rm -f $GUARD
echo "=== all done $(date -u +%FT%TZ)"