unparallelled commited on
Commit
c5dae6b
·
verified ·
1 Parent(s): cb0d955

Upload scripts/ext64k_b5.sh with huggingface_hub

Browse files
Files changed (1) hide show
  1. scripts/ext64k_b5.sh +216 -0
scripts/ext64k_b5.sh ADDED
@@ -0,0 +1,216 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env bash
2
+ # =============================================================================
3
+ # ext64k_b5.sh -- YaRN extension 32k -> 64k on b5, deadline-aware.
4
+ #
5
+ # PHASE 1 3-step probe: does 64k fit at cp=4 on 143 GB, and how fast is a step?
6
+ # PHASE 2 train as many steps as fit before HARD_STOP, WSD tail sized so the
7
+ # FINAL checkpoint is DECAYED and comparable to the 32k results
8
+ # PHASE 3 eval 4 lengths IN PARALLEL on the 4 GPUs (wall clock = slowest job)
9
+ # PHASE 4 upload ckpt + sidecar + logs
10
+ #
11
+ # WHY b5: documents are 65,536-131,072 tokens, so the MINIMUM equals the 64k
12
+ # window and every training sample is one contiguous document. b4 (32-64k)
13
+ # would straddle in roughly half its samples. Same reasoning that made b4 the
14
+ # right choice at 32k.
15
+ #
16
+ # PARENT: b4 full-epoch step 2227 -- the best 32k model (70.0/58.0/43.0 at
17
+ # 8k/16k/32k). CP_INIT_FROM loads WEIGHTS ONLY, so its cp=2 shard layout is
18
+ # irrelevant to this cp=4 run, and no sidecar is needed (none is read).
19
+ #
20
+ # YaRN INVARIANT: SCALE * ORIG_MAXPOS must equal seq_len EXACTLY and ORIG_MAXPOS
21
+ # must be the length the parent was actually trained at. 2 * 32768 = 65536.
22
+ #
23
+ # LR: CP_MAX_LR / CP_MIN_LR DO NOT EXIST -- bluscriptCP.cpp:131-132 hardcodes
24
+ # 6e-4 / 6e-5 and the env block never reads them. Real schedule is
25
+ # 6e-4 * CP_REWARMUP_PEAK(0.3) = 1.8e-4 peak, 6e-5 floor -- the same values
26
+ # every previous run used, so results stay comparable.
27
+ #
28
+ # DEADLINE: HARD_STOP is a UTC epoch. Step count is derived from the MEASURED
29
+ # probe rate, not an estimate, so the run lands inside the window.
30
+ # =============================================================================
31
+ set -uo pipefail
32
+ cd "$(dirname "$0")"
33
+ ROOT="$(cd ../.. && pwd)"
34
+ export LD_LIBRARY_PATH="$ROOT/Tensor-Implementations/lib:$ROOT/Profiler/lib:${LD_LIBRARY_PATH:-}"
35
+ export OMPI_ALLOW_RUN_AS_ROOT=1 OMPI_ALLOW_RUN_AS_ROOT_CONFIRM=1
36
+
37
+ PY=/root/envv/bin/python3
38
+ HF=/root/envv/bin/hf
39
+ EXE=./build/bluscriptCP_exec
40
+ CK=checkpoints_bluscriptcp/rank_0
41
+ LOG=ext64k_logs; mkdir -p "$LOG"
42
+ M="$LOG/master.log"
43
+ REPO=unparallelled/BluScriptCP
44
+ say(){ echo "[$(date -u +%H:%M:%S)] $*" | tee -a "$M" >&2; }
45
+ die(){ say "FATAL: $*"; exit 1; }
46
+
47
+ T=65536; NP=4; GPUS=0,1,2,3
48
+ ORIG=32768; SCALE=2 # 2 * 32768 == 65536
49
+ DATA=lendata/b5_65536_131072
50
+ PARENT="$CK/b4epoch32k_step2227.ckpt"
51
+ HAY="$PWD/lendata/haystack/b2val_haystack.bin"
52
+ WARM=100; TAIL=${TAIL:-2}; FREQ=50; KEEP=2
53
+ GB=524288
54
+ # 23:30 IST = 18:00 UTC today
55
+ HARD_STOP=${HARD_STOP:-$(date -u -d "$(date -u +%Y-%m-%d) 18:00:00" +%s)}
56
+ RESERVE_MIN=${RESERVE_MIN:-50} # eval (28) + upload (20) + slack
57
+
58
+ $PY -c "import tiktoken" 2>/dev/null || die "tiktoken missing"
59
+ [ -x "$EXE" ] || die "$EXE not built"
60
+ [ -f "$PARENT" ] || die "parent missing: $PARENT"
61
+ [ -f "$HAY" ] || die "haystack missing: $HAY"
62
+ [ -d "$DATA" ] || die "$DATA missing"
63
+ [ $((SCALE*ORIG)) -eq $T ] || die "YaRN invariant: $SCALE * $ORIG != $T"
64
+ ntr=$(ls "$DATA"/*train*.bin 2>/dev/null | wc -l)
65
+ nva=$(ls "$DATA"/*val*.bin 2>/dev/null | wc -l)
66
+ [ "$ntr" -ge 1 ] && [ "$nva" -ge 1 ] || die "need train+val shards in $DATA (have $ntr/$nva)"
67
+ have=$(( $(du -cb "$DATA"/*train*.bin | tail -1 | cut -f1) / 2 ))
68
+
69
+ say "=== 32k -> 64k YaRN extension on b5 (4x H200, cp=$NP) ==="
70
+ say "parent: b4 full epoch step 2227 (best 32k model)"
71
+ say "YaRN SCALE=$SCALE ORIG=$ORIG -> $((SCALE*ORIG)) == CP_T=$T OK"
72
+ say "b5: $ntr train shards = $(echo "scale=3; $have/1000000000" | bc) B tokens available"
73
+ say "hard stop $(date -u -d @$HARD_STOP +%H:%M) UTC = $(date -u -d @$((HARD_STOP+19800)) +%H:%M) IST, reserving ${RESERVE_MIN}m"
74
+
75
+ COMMON="CP_SIZE=$NP CP_ATTN_MODE=ring CP_ATTN_FUSION=split CP_T=$T \
76
+ YARN_SCALE=$SCALE YARN_ORIG_MAXPOS=$ORIG \
77
+ CP_ZERO=1 CP_ZERO_V2=1 CP_ZERO_SYMMETRIC=1 CP_BF16=1 CP_OPTIMIZER=muon \
78
+ CP_WARMUP=$WARM CP_REWARMUP=$WARM CP_REWARMUP_PEAK=0.3"
79
+ ARCH="CP_N_EMBD=1536 CP_N_LAYER=36 CP_N_HEAD=12 CP_N_KVHEAD=4 CP_FFN=4096 CP_WEIGHT_TYING=0"
80
+
81
+ # ---- PHASE 1: fit + speed --------------------------------------------------
82
+ if [ ! -f "$LOG/probe.ok" ]; then
83
+ say "PHASE 1: 3-step probe (64k at cp=4 projects to 108.8 GB of 143 GB)"
84
+ env $ARCH $COMMON CP_STABLE=0 CP_CKPT=0 \
85
+ CP_INIT_FROM="$PWD/$PARENT" CP_DATA_ROOT="$PWD/$DATA" \
86
+ CP_B=1 CP_GLOBAL_BATCH=65536 CP_MAX_STEPS=3 \
87
+ CUDA_VISIBLE_DEVICES=$GPUS mpirun --allow-run-as-root -np $NP "$EXE" \
88
+ > "$LOG/probe.log" 2>&1
89
+ if grep -qaiE 'out of memory|bad_alloc' "$LOG/probe.log"; then
90
+ say "PROBE OOM -- 64k does not fit at cp=$NP even on 143 GB cards."
91
+ grep -aoE 'cudaMallocAsync\(size=[0-9]+\) failed' "$LOG/probe.log" | head -2 | sed 's/^/ /' >&2
92
+ exit 1
93
+ fi
94
+ grep -aE '^step +[0-9]+ \|' "$LOG/probe.log" | sed 's/^/ /' >&2
95
+ say "PROBE OK: peak $(grep -aoE 'mem: [0-9]+MB' "$LOG/probe.log" | tail -1)"
96
+ touch "$LOG/probe.ok"
97
+ fi
98
+
99
+ # ---- derive the step count from the MEASURED rate --------------------------
100
+ # The probe used a tiny global batch, so its dt is NOT the real per-step cost.
101
+ # Scale it: real grad_accum is GB/T vs the probe's 65536/T.
102
+ SPS=$($PY - "$LOG/probe.log" $GB $T <<'PYEOF'
103
+ import re,sys,statistics
104
+ t=open(sys.argv[1],errors='ignore').read()
105
+ d=[float(x) for x in re.findall(r'dt: ([0-9.]+)ms', t)]
106
+ probe_accum=65536//int(sys.argv[3]) or 1
107
+ real_accum=int(sys.argv[2])//int(sys.argv[3])
108
+ per=statistics.median(d[1:]) if len(d)>2 else d[-1]
109
+ print(f"{per/1000.0*real_accum/probe_accum:.3f}")
110
+ PYEOF
111
+ )
112
+ say "measured step cost at real batch: ${SPS} s/step"
113
+ if [ -n "${STEPS_OVERRIDE:-}" ]; then
114
+ say "STEPS_OVERRIDE=$STEPS_OVERRIDE -- the probe ran grad_accum=1 so its dt x8"
115
+ say " overstated the real step cost 5.8x (74.0 vs a measured 12.85 s/step)."
116
+ fi
117
+ STEPS=$($PY - "$SPS" "$HARD_STOP" "$RESERVE_MIN" "$have" "$GB" "$WARM" "$TAIL" <<'PYEOF'
118
+ import sys,time,math
119
+ sps=float(sys.argv[1]); stop=int(sys.argv[2]); res=int(sys.argv[3])
120
+ have=int(sys.argv[4]); gb=int(sys.argv[5]); warm=int(sys.argv[6]); tail=int(sys.argv[7])
121
+ budget=stop-time.time()-res*60
122
+ n=int(budget/sps)
123
+ n=min(n, have//gb) # never wrap the data
124
+ n=max(n, warm+tail+10) # schedule must be legal
125
+ print(n)
126
+ PYEOF
127
+ )
128
+ STEPS=${STEPS_OVERRIDE:-$STEPS}
129
+ STABLE=$(( STEPS - WARM - TAIL ))
130
+ [ "$STABLE" -lt 1 ] && STABLE=1
131
+ say "PHASE 2: $STEPS steps = $(echo "scale=3; $STEPS*$GB/1000000000" | bc) B tokens = $(echo "scale=0; $STEPS*$GB*100/866000000" | bc)% of a b5 epoch"
132
+ say " WSD: warmup $WARM, stable $STABLE, decay tail $TAIL -> FINAL checkpoint IS decayed"
133
+
134
+ if [ -f "$LOG/ext.ckpt" ] && [ -f "$(cat "$LOG/ext.ckpt")" ]; then
135
+ say "already trained -> $(cat "$LOG/ext.ckpt")"
136
+ else
137
+ env $ARCH $COMMON CP_STABLE=$STABLE \
138
+ CP_CKPT=1 CP_CKPT_FREQ=$FREQ CP_CKPT_KEEP=$KEEP CP_CKPT_NEW_RUN=1 \
139
+ CP_INIT_FROM="$PWD/$PARENT" CP_DATA_ROOT="$PWD/$DATA" \
140
+ CP_B=1 CP_GLOBAL_BATCH=$GB CP_MAX_STEPS=$STEPS \
141
+ CUDA_VISIBLE_DEVICES=$GPUS mpirun --allow-run-as-root -np $NP "$EXE" \
142
+ > "$LOG/ext.log" 2>&1
143
+ grep -qaiE 'out of memory|bad_alloc' "$LOG/ext.log" && die "OOM in the full run"
144
+ RN=$(grep -aoE 'CP_Training_log[0-9]+' "$LOG/ext.log" | head -1 | grep -oE '[0-9]+')
145
+ [ -n "$RN" ] || die "no run number"
146
+ CKF=$(ls -t $CK/blumodelcp_run${RN}_step_*.ckpt 2>/dev/null | head -1)
147
+ [ -n "$CKF" ] || die "run $RN wrote NO checkpoint"
148
+ n=$(ls checkpoints_bluscriptcp/rank_*/"$(basename "$CKF")" 2>/dev/null | wc -l)
149
+ [ "$n" -ge "$NP" ] || die "only $n/$NP rank shards for $(basename "$CKF")"
150
+ echo "$CKF" > "$LOG/ext.ckpt"; echo "$RN" > "$LOG/runnum"
151
+ say "ext64k -> run $RN, $(basename "$CKF"), loss $(grep -aE '^step ' "$LOG/ext.log" | tail -1 | grep -oE 'loss: [0-9.]+')"
152
+ say " peak mem $(grep -aoE 'mem: [0-9]+MB' "$LOG/ext.log" | tail -1)"
153
+ say " final lr $(grep -aE '^step ' "$LOG/ext.log" | tail -1 | grep -oE 'lr [0-9.e-]+') (should be ~6e-05 = decayed)"
154
+ fi
155
+
156
+ # ---- PHASE 3: eval, 4 lengths in PARALLEL on 4 GPUs ------------------------
157
+ EXT=$(basename "$(cat "$LOG/ext.ckpt")")
158
+ say "PHASE 3: eval ext64k -- 4 lengths, one GPU each"
159
+ # 65512 + MAX_NEW 24 = 65536 exactly; 65528 would pad to 65792 and abort.
160
+ for LEN in 8192 16360 32744 65512; do
161
+ ( GEN_DATA_ROOT=$DATA HAY="$HAY" PY=$PY OUT=ext64k_len_$LEN TASKS="niah_single" \
162
+ CKPTS="ext64k" N=100 PROMPT_LEN=$LEN MAX_NEW=24 T=$T \
163
+ DEPTHS="0.0,0.25,0.5,0.75,1.0" YARN_ORIG=$ORIG YARN=$SCALE \
164
+ GPUS=$(( (LEN==8192)?0:(LEN==16360)?1:(LEN==32744)?2:3 )) JOBS_PER_GPU=1 \
165
+ BF16=1 GREEDY=1 CKPT_EXTRA="ext64k=$EXT" ./run_longeval.sh \
166
+ >> "$LOG/eval_$LEN.log" 2>&1 ) &
167
+ done
168
+ wait
169
+ for LEN in 8192 16360 32744 65512; do
170
+ [ -s "ext64k_len_$LEN/summary.csv" ] && say " len $LEN -> $(sed -n 2p ext64k_len_$LEN/summary.csv)" \
171
+ || say " len $LEN -> NO RESULT"
172
+ done
173
+
174
+ # ---- PHASE 4: upload -------------------------------------------------------
175
+ say "PHASE 4: upload"
176
+ {
177
+ echo "# 32k -> 64k YaRN extension on b5 -- $(date -u +%Y-%m-%d)"
178
+ echo
179
+ echo "Parent: b4 full-epoch step 2227 (best 32k model: 70.0/58.0/43.0 at 8k/16k/32k)."
180
+ echo "Data: b5_65536_131072 -- min doc 65,536 == the window, so every sample is"
181
+ echo " ONE contiguous document."
182
+ echo "YaRN: SCALE=2 ORIG_MAXPOS=32768 -> 65536 exactly."
183
+ echo "Train: $STEPS steps x $GB = $(echo "scale=3; $STEPS*$GB/1000000000" | bc) B tokens"
184
+ echo " = $(echo "scale=0; $STEPS*$GB*100/866000000" | bc)% of b5's 0.866 B train split (no wrap)."
185
+ echo " WSD warmup $WARM / stable $STABLE / decay tail $TAIL -- final ckpt DECAYED."
186
+ echo " LR peak 1.8e-4, floor 6e-5 (CP_MAX_LR is a no-op; see B4 results doc)."
187
+ echo " 4x H200, cp=4, B=1 x grad_accum $((GB/T))."
188
+ echo "Eval: niah_single, N=100, greedy, shared haystack b2_8192_16384 val."
189
+ echo
190
+ echo "## Results"; echo
191
+ echo "| context | niah_single |"; echo "|---|---|"
192
+ for LEN in 8192 16360 32744 65512; do
193
+ v=$(sed -n 2p "ext64k_len_$LEN/summary.csv" 2>/dev/null | cut -d, -f2)
194
+ printf "| %s | %s |\n" "$LEN" "${v:-n/a}"
195
+ done
196
+ echo
197
+ echo "For comparison, the 32k parent at the same lengths: 70.0 / 58.0 / 43.0 / n/a"
198
+ echo "(it cannot run at 64k -- its RoPE cache is sized 32,768)."
199
+ echo
200
+ echo "NOTE: this is a PARTIAL b5 epoch, sized to a wall-clock deadline. It is a"
201
+ echo "first 64k data point, not a converged extension."
202
+ } > "$LOG/RESULTS.md"
203
+ up(){ [ -e "$1" ] || return 0
204
+ $HF upload "$REPO" "$1" "$2" >>"$LOG/upload.log" 2>&1 && say " ok $2" || say " FAIL $2"; }
205
+ up "$LOG/RESULTS.md" docs/EXT64K_B5_RESULTS_$(date -u +%Y-%m-%d).md
206
+ for LEN in 8192 16360 32744 65512; do up "ext64k_len_$LEN" "logs/ext64k_len_$LEN/"; done
207
+ up "$LOG" logs/ext64k/
208
+ up ext64k_b5.sh scripts/ext64k_b5.sh
209
+ RN=$(cat "$LOG/runnum" 2>/dev/null)
210
+ for r in 0 1 2 3; do
211
+ for e in ckpt zero_g1; do
212
+ up "checkpoints_bluscriptcp/rank_$r/blumodelcp_run${RN}_step_${STEPS}.$e" \
213
+ "checkpoints/ext64k_b5_rank${r}_step_${STEPS}.$e"
214
+ done
215
+ done
216
+ say "=== EXT64K DONE ==="