File size: 8,577 Bytes
d231962
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env bash
# Full v2 pipeline. Resumable, retrying, and safe to run unattended.
#
#   cd /scratch/user4/dai/v2
#   (setsid nohup bash scripts/run_all.sh > work/logs/run_all.log 2>&1 < /dev/null &)
#
#   tail -f work/logs/run_all.log
#   cat work/logs/status                 <- one line per step
#
# DESIGN
#   * Every step declares an output. If the output exists the step is skipped, so a restart
#     after a crash costs only the step that was running.
#   * Every step retries twice. The shared box kills jobs under memory pressure and a retry
#     from a checkpoint is cheaper than a human noticing.
#   * TRACK 1 (CPU) and TRACK 2 (GPU) are independent and run concurrently. Track 1 alone
#     produces a shippable submission; Track 2 only adds candidates to it.
#   * Everything is niced. 26 users share this box and an earlier job at load 37 made SSH
#     unreachable for all of them.
set -uo pipefail

cd "$(dirname "$0")/.."
ROOT=$(pwd)
export BERX_DATA="${BERX_DATA:-/scratch/user4/dai/dataset_unstop/dataset}"
export BERX_WORK="${BERX_WORK:-$ROOT/work}"
export BERX_EMB="${BERX_EMB:-/scratch/user4/dai/model-3/work/emb}"
export BERX_M4="${BERX_M4:-/scratch/user4/dai/model-4/work}"
export HF_HOME="${HF_HOME:-/scratch/user4/mtp/cache/huggingface}"
export TOKENIZERS_PARALLELISM=false
export OMP_NUM_THREADS="${OMP_NUM_THREADS:-8}"
export MKL_NUM_THREADS="${MKL_NUM_THREADS:-8}"
PYBIN="${PYBIN:-/home/user4/Jyo/DAI/er_pipeline/.venv/bin/python}"
# -u is not optional here. Python block-buffers stdout when it is a file, so a stage that
# redirects to a log shows NOTHING until it exits - t1_fit.log sat at 0 bytes for twelve
# minutes while the fit was running normally. Unbuffered output is the difference between
# a monitorable pipeline and a black box.
PY="$PYBIN -u"
N="nice -n 19 ionice -c3"
SPLIT="${SPLIT:-test}"
L="$BERX_WORK/logs"; mkdir -p "$L"
STATUS="$L/status"
CE_MODEL="${CE_MODEL:-/scratch/user4/dai/model-3/work/ce_model_A}"

# Unbuffered and duplicated into the track log. The first version piped each track through
# `sed` via process substitution to prefix lines; sed block-buffers when its output is a
# file, so after two minutes of real work the log still held only the banner. Never put a
# filter between a long-running job and its log.
say() { echo "[$(date +%H:%M:%S)] $*" | tee -a "${TRACKLOG:-/dev/null}"; }
mark() { printf '%-28s %-9s %s\n' "$1" "$2" "$(date +%H:%M:%S)" >> "$STATUS"; }

# step <name> <output-to-check> <logfile> <command...>
step() {
  local name="$1" out="$2" log="$3"; shift 3
  if [ -n "$out" ] && [ -e "$out" ]; then say "SKIP $name (exists: $out)"; mark "$name" SKIP; return 0; fi
  local try rc
  for try in 1 2 3; do
    say "RUN  $name (attempt $try)"
    # rc must be captured from the command itself. Reading $? after an `if` returns the
    # status of the `if`, which is 0 whenever the else branch is taken - so every failure
    # reported "rc=0" and the gate check below could never fire.
    $N "$@" >> "$L/$log" 2>&1
    rc=$?
    if [ $rc -eq 0 ]; then say "OK   $name"; mark "$name" OK; return 0; fi
    # exit 3 is a deliberate gate failure, not a crash. Retrying it is pointless.
    if [ $rc -eq 3 ]; then say "GATE $name returned 3 (declined)"; mark "$name" GATE; return 3; fi
    say "FAIL $name rc=$rc (see $L/$log)"; sleep 20
  done
  mark "$name" FAILED; return 1
}

"$PYBIN" -c 'import pandas, torch' 2>/dev/null || { echo "PYBIN=$PYBIN unusable"; exit 1; }
# Append, never truncate. Restarting one track used to wipe the other track's history from
# the status file while it was still running, which is the opposite of what a status file is
# for. A session banner keeps the restarts readable instead.
printf '# ---- session %s TRACKS=%s
' "$(date '+%H:%M:%S')" "${TRACKS:-12}" >> "$STATUS"
say "v2 pipeline starting. work=$BERX_WORK"

# ---------------------------------------------------------------- TRACK 1 (CPU, shippable)
track1() {
  step prep_train  "$BERX_WORK/prep/train/pool.parquet"  t1_prep.log \
       $PY scripts/00_prepare.py --split train || return 1
  step prep_test   "$BERX_WORK/prep/test/pool.parquet"   t1_prep.log \
       $PY scripts/00_prepare.py --split test  || return 1
  step m4_tables   "$BERX_WORK/m4/test.parquet"          t1_m4.log \
       $PY scripts/10_build_m4_table.py --split both || return 1
  step geo_test    "$BERX_WORK/geo/equiv_test.json"      t1_geo.log \
       $PY scripts/02_france_geo.py --split test --skip-validate || return 1
  step geo_train   "$BERX_WORK/geo/equiv_train.json"     t1_geo.log \
       $PY scripts/02_france_geo.py --split train --skip-validate || return 1

  # GATE A. Fits on g_stack, reads g_val - a split the cross-encoder never saw.
  step rescore_fit "$BERX_WORK/rescore/gate.json"        t1_fit.log \
       $PY scripts/11_rescore.py --mode train
  local rc=$?
  if [ $rc -eq 3 ]; then
    say "GATE A declined: the new features do not beat model-4's own columns on a clean val."
    say "Keeping the existing submission. Track 2 continues - it adds candidates, not features."
    return 3
  fi
  [ $rc -ne 0 ] && return 1

  step rescore_apply "$BERX_WORK/output_v2/matching_results.tsv" t1_apply.log \
       $PY scripts/11_rescore.py --mode test --bundle "$BERX_WORK/rescore/bundle.joblib" || return 1
  say "TRACK 1 COMPLETE - $BERX_WORK/output_v2/matching_results.tsv is shippable."
}

# ---------------------------------------------------------------- TRACK 2 (GPU, candidates)
track2() {
  # F4/F5 from FALSE_NEGATIVES: 78% of never-retrieved copies are outside every retriever's
  # top-100, because all three score name AND address together and these copies have one of
  # the two destroyed. A single-field channel is the only thing that can reach them.
  step embed_name_s1   "$BERX_EMB/nameonly/${SPLIT}_s1/matrix.npy"   t2_embed.log \
       env CUDA_VISIBLE_DEVICES=0 $PY scripts/08_embed.py --split "$SPLIT" \
           --view name --side s1   --out-channel nameonly || return 1
  step embed_name_pool "$BERX_EMB/nameonly/${SPLIT}_pool/matrix.npy" t2_embed.log \
       env CUDA_VISIBLE_DEVICES=0 $PY scripts/08_embed.py --split "$SPLIT" \
           --view name --side pool --out-channel nameonly --only-empty-address || return 1
  step retr_F4         "$BERX_WORK/retr/${SPLIT}_nameonly_s0of1.parquet" t2_retr.log \
       env CUDA_VISIBLE_DEVICES=0 $PY scripts/03_retrieve.py --split "$SPLIT" \
           --channel nameonly --k 20 --device cuda:0 || return 1

  # Fix 2, by the recipe that was actually measured: character TF-IDF over the address-less
  # pool, top 5. CPU only, so it runs beside the GPU work instead of competing for it.
  step ename           "$BERX_WORK/retr/${SPLIT}_ename_s0of1.parquet" t2_ename.log        $PY scripts/13_ename.py --split "$SPLIT" --k 5 || return 1

  # Address-only retrieval is NOT here on purpose. Measured at 6-12% recovery of missed
  # copies, worth about +0.0002 val for 10-50 extra candidates per S1, and rejected on that
  # evidence. A GPU pass for it was killed mid-run once the measurement was available.

  step retr_e5small    "$BERX_WORK/retr/${SPLIT}_e5_small_s0of1.parquet" t2_retr.log \
       env CUDA_VISIBLE_DEVICES=0 $PY scripts/03_retrieve.py --split "$SPLIT" \
           --channel e5_small --k 100 --device cuda:0 || return 1

  step new_pairs       "$BERX_WORK/shards/${SPLIT}_manifest.json" t2_shard.log \
       $PY scripts/12_new_pairs.py --split "$SPLIT" --nshards 4 || return 1
  say "TRACK 2: shards ready. Upload them, then start the Colab lanes."
}

# TRACKS lets one track be restarted without relaunching the other on top of a live run.
# Track 2 died on a bug while track 1 was still working; starting the whole script again
# would have given track 1 a second concurrent copy of itself.
TRACKS="${TRACKS:-12}"
R1=0; R2=0; T1=""; T2=""
# A pidfile per track. The watchdog needs to tell "track 1 is dead" from "track 2 is alive",
# and counting run_all.sh processes cannot: with either track running the count is the same.
# That is precisely the blindness that let a dead track sit unnoticed.
case "$TRACKS" in *1*) ( TRACKLOG="$L/track1.log"; track1 ) > "$L/track1.out" 2>&1 & T1=$!; echo $T1 > "$L/track1.pid";; esac
case "$TRACKS" in *2*) ( TRACKLOG="$L/track2.log"; track2 ) > "$L/track2.out" 2>&1 & T2=$!; echo $T2 > "$L/track2.pid";; esac
say "TRACKS=$TRACKS  track1 pid=${T1:-skipped}  track2 pid=${T2:-skipped}"
[ -n "$T1" ] && { wait $T1; R1=$?; rm -f "$L/track1.pid"; }
[ -n "$T2" ] && { wait $T2; R2=$?; rm -f "$L/track2.pid"; }
say "track1 rc=$R1  track2 rc=$R2"
say "status:"; cat "$STATUS"