File size: 8,382 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
#!/bin/bash
# ONEMODEL pilot (records/R5/ONEMODEL/design.json, ONEMODEL-pilot-1): the single GPU entry point.
# Runs INSIDE an existing one-H100 allocation started by CODE/scripts/r5_onemodel/onemodel_gate.sbatch (submitted by hand,
# which checks the reviewed sha256 list READY and then execs: bash run_gpu.sh <run>). No #SBATCH lines are needed here.
# Usage (by the gate only): bash run_gpu.sh seed1
# Validation data only. Stages, each resumable. A stage is resubmitted ONLY when it stopped on the time budget (exit 99, or for
# eval_vlm.py its deadline passed); any other failure exits non-zero with no resubmission. Before a stage starts with less than
# 60 s of budget, the script resubmits instead of starting it. Resubmission = THE GATE again (sbatch --test-only first, then
# sbatch onemodel_gate.sbatch <run>), at most 12 times (logic of CODE/scripts/r5_hymt/hymt_run.sbatch).
#  0 baseline detector + leader on all 300 generated + 150 School val pages on this GPU (the comparator), scored alone
#  1 om_train.py: 1,875 steps, exports ck_7504, ck_15008, ck_22512, ck_30000 (= final)
#  2 final export: 450 val pages in spotting mode; V1 (3,678 crops) and synth_clutter_val (500) in crop mode (eval_vlm.py)
#  3 R5 scorer on the crop predictions, om_score.py -> DATA/outputs/r6_onemodel/page_scores/page_scores_<run>_final.json (gates)
#  4 descriptive: the 500 clutter images in spotting mode (final export and leader, at most 256 new tokens), re-scored
#  5 learning curve (descriptive): the three earlier exports, pages + V1 + clutter, then re-scored
# Guard: DATA/outputs/r5/onemodel/train/<run>/RUNNING holds the job id of the running segment; a second segment of the same run
# exits 3 while that job is still running. The file is removed at the normal end and before each resubmission.
# Dry-run switches (environment; all unset in the real job, so it runs exactly as described above): OM_DEVICE (cuda),
# OM_OUT, OM_CKPT_ROOT (output and checkpoint roots), OM_LIMIT (14280), OM_X_TRAIN, OM_X_EVAL, OM_X_BASE, OM_X_CROP (extra options
# appended to om_train / om_eval_pages / om_baseline / eval_vlm), OM_CKS (learning-curve export names), OM_TEST_ONLY (resubmission
# stops after the test-only check). Used only by om_dryrun.sbatch on ZEN4.
set -uo pipefail
RUN=${1:?usage: run_gpu.sh RUN}
C=/path/to/TURKIC_OCR_CODE
D=/path/to/TURKIC_OCR_DATA
S=$C/scripts/r5_onemodel
GATE=$C/scripts/r5_onemodel/onemodel_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
DEV=${OM_DEVICE:-cuda}
O=${OM_OUT:-$D/outputs/r6_onemodel}
CKR=${OM_CKPT_ROOT:-$D/checkpoints/r6_onemodel}
LIMIT=${OM_LIMIT:-14280}      # the gate's 04:00:00 allocation minus 120 s for the gate's own checks before exec
XT=${OM_X_TRAIN:-}
XE=${OM_X_EVAL:-}
XB=${OM_X_BASE:-}
XC=${OM_X_CROP:-}
CKS=${OM_CKS:-ck_7504 ck_15008 ck_22512}
N=${OM_RESUBMIT:-0}
CK=$CKR/$RUN
LEADER=$D/checkpoints/r5/paddleocr_vl_1_6/leader
GUARD=$D/outputs/r5/onemodel/train/$RUN/RUNNING
need() { grep -q "$2" "$1" 2>/dev/null || { echo "REFUSED: precondition $1 ($2) not met"; exit 3; }; }
need $D/data/processed/r6_onemodel/build_report.json '"passed": true'
need $D/outputs/r6_onemodel/smoke/smoke_check.json '"smoke_passed": true'
[ -n "${SLURM_JOB_ID:-}" ] || { echo "REFUSED: not inside a Slurm allocation"; exit 3; }
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 of run $RUN is still running ($GUARD); exit 3"
    exit 3
  fi
fi
mkdir -p $(dirname $GUARD)
echo $SLURM_JOB_ID > $GUARD
resubmit() {
  rm -f $GUARD
  if [ "$N" -ge 12 ]; then echo "resubmission limit reached ($N)"; exit 1; fi
  sbatch --test-only --export=ALL,OM_RESUBMIT=$((N + 1)) $GATE $RUN \
    || { echo "RESUBMISSION FAILED (test-only refused); the chain stops here"; exit 1; }
  if [ -n "${OM_TEST_ONLY:-}" ]; then echo "DRY RUN: test-only passed, real sbatch skipped; exit 0"; exit 0; fi
  sbatch --export=ALL,OM_RESUBMIT=$((N + 1)) $GATE $RUN \
    || { echo "RESUBMISSION FAILED (sbatch refused); the chain stops here"; exit 1; }
  exit 0
}
left() { echo $((LIMIT - SECONDS - $1)); }
budget() {  # margin: resubmit before a stage that would start with under 60 s; never pass --max-seconds 0 (= unlimited)
  if [ $(left $1) -lt 60 ]; then echo "budget below 60 s before a stage; resubmitting"; resubmit; fi
}
stage_rc() {  # rc, what: 99 = stopped on the time budget -> resubmit; any other non-zero -> fail visibly, no resubmission
  if [ $1 -eq 99 ]; then resubmit; fi
  if [ $1 -ne 0 ]; then echo "FAILED: $2 (exit $1); no resubmission"; exit $1; fi
}
evalpages() {  # model dir, name, sets [extra options]
  budget 600
  echo "=== pages $2 ($3) $(date -u +%FT%TZ)"
  $PY -I $S/om_eval_pages.py --model-dir $1 --name $2 --sets $3 --device $DEV --out-root $O/page_preds --max-seconds $(left 600) ${4:-} $XE
  stage_rc $? "page evaluation $2 $3"
}
cropsdone() { grep -q '"complete": true' $O/preds/onemodel/$1/V1.score.json 2>/dev/null && grep -q '"complete": true' $O/preds/onemodel/$1/synth_clutter_val.score.json 2>/dev/null; }
evalcrops() {  # model dir, name
  if cropsdone $2; then return 0; fi
  budget 600
  echo "=== crops $2 $(date -u +%FT%TZ)"
  DLS=$(( $(date -u +%s) + $(left 600) ))
  DL=$(date -u -d "@$DLS" +%Y-%m-%dT%H:%M:%SZ)
  $PY -I $C/scripts/r5_vlma/eval_vlm.py --short paddleocr_vl_1_6 --arm onemodel --ckpt $2 --model-dir $1 \
    --registry $C/scripts/r5_vlma/models_r5.tsv --evalsets $D/data/processed/r5/evalsets.json --preds-root $O/preds \
    --sets V1,synth_clutter_val --device $DEV --batch 16 --deadline-utc $DL $XC
  RC=$?
  if [ $RC -ne 0 ]; then echo "FAILED: crop evaluation $2 (exit $RC); no resubmission"; exit $RC; fi
  if cropsdone $2; then return 0; fi
  # eval_vlm.py stops at its deadline with exit 0 and an incomplete score file: resubmit only if the deadline has passed
  if [ $(date -u +%s) -ge $DLS ]; then echo "crop evaluation $2 stopped at its deadline"; resubmit; fi
  echo "FAILED: crop evaluation $2 incomplete before its deadline (a set refused or skipped?); no resubmission"
  exit 1
}
baseline() {  # set
  if [ -f $O/baseline/gpu/$1.done ]; then return 0; fi
  budget 600
  echo "=== baseline $1 $(date -u +%FT%TZ)"
  $PY -I $S/om_baseline.py --set $1 --device $DEV --out $O/baseline/gpu/$1.jsonl --max-seconds $(left 600) $XB
  stage_rc $? "baseline $1"
  touch $O/baseline/gpu/$1.done
}
SCORE_PATHS="--page-preds $O/page_preds --baseline-dir $O/baseline/gpu --crop-scores $O/scores --out $O/page_scores"
score() {
  echo "=== score $(date -u +%FT%TZ)"
  OMP_NUM_THREADS=1 OPENBLAS_NUM_THREADS=1 MKL_NUM_THREADS=1 $PK -I $C/scripts/r5eval/score.py --preds $O/preds \
    --registry $D/data/processed/r5/evalsets.json --out $O/scores --workers 8 --page-preds "" --settle 0
  echo "r5 scorer rc $?"
  for name in "$@"; do $PK -I $S/om_score.py --name $name $SCORE_PATHS; echo "om_score $name rc $?"; done
}
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 "run $RUN, job $SLURM_JOB_ID, resubmission $N, device $DEV, out $O, limit $LIMIT"
baseline gen_val
baseline school_val
if [ ! -f $O/page_scores/page_scores_baseline_gpu.json ]; then
  $PK -I $S/om_score.py --baseline-only $SCORE_PATHS; echo "baseline score rc $?"
fi
if [ ! -f $O/train/$RUN/train.done.json ]; then
  budget 1500
  echo "=== train $(date -u +%FT%TZ)"
  $PY -I $S/om_train.py --run $RUN --device $DEV --ckpt-root $CKR --out-root $O/train --max-seconds $(left 1500) $XT
  stage_rc $? "training"
fi
F=${RUN}_final
evalpages $CK/final $F gen_val,school_val
evalcrops $CK/final $F
if [ ! -f $O/page_scores/page_scores_$F.json ]; then score $F; fi
evalpages $CK/final $F clutter_spot "--max-new 256"
evalpages $LEADER leader clutter_spot "--max-new 256"
score $F
NAMES=""
for ck in $CKS; do
  evalpages $CK/$ck ${RUN}_$ck gen_val,school_val
  evalcrops $CK/$ck ${RUN}_$ck
  NAMES="$NAMES ${RUN}_$ck"
done
score $F $NAMES
touch $O/train/$RUN/run_gpu.done
rm -f $GUARD
echo "=== all done $(date -u +%FT%TZ)"