AR-4B-base / training_code /finish_fw.sh
AjayRangoji's picture
Add complete pretraining codebase and cluster scripts under training_code/
df64241 verified
Raw History Blame Contribute Delete
3.18 kB
#!/bin/bash
# One-time: wait for the 4 English tokenizing jobs, check the grown files, then re-balance
# the English weights by file size and RELOAD, so training uses the new data.
# Run: nohup ./finish_fw.sh > logs/finish_fw.log 2>&1 &
cd /fsx/ajay-stu/AR
source .venv/bin/activate
JOBS="39801,39802,39805,39806"
# 1) wait (max 12 h) until all 4 jobs have left the queue
for i in $(seq 144); do
n=$(squeue -h -j $JOBS 2>/dev/null | wc -l)
[ "$n" -eq 0 ] && break
sleep 300
done
[ "$n" -ne 0 ] && { echo "$(date -u) jobs still running after 12 h, nothing changed"; exit 1; }
# 2) each job must have ended normally (not crashed, not stopped by the disk guard)
for j in ${JOBS//,/ }; do
grep -q "^\[done\]" logs/tok_ar-tok_$j.out || { echo "$(date -u) job $j did not finish normally, nothing changed"; exit 1; }
done
echo "$(date -u) all 4 jobs finished"
# 3) check the files: full documents, ~20B tokens, new part is not a repeat of the start
python - <<'PY' || { echo "$(date -u) file check FAILED, nothing changed"; exit 1; }
import re, sys
import numpy as np
EOT = 151645
log = open("logs/resume_tok.log").read()
ok = True
for g in ("003", "004", "005", "006"):
p = f"/fsx/ajay-stu/ar-data/bin/fw{g}/fineweb.bin"
old = int(re.search(rf"fw{g}: have (\d+)", log).group(1)) # tokens before resuming
a = np.memmap(p, dtype=np.uint32, mode="r")
repeat = bool(np.array_equal(a[:2048], a[old:old + 2048])) # a failed skip restarts at doc 1
good = bool(a[-1] == EOT and a[old - 1] == EOT and not repeat and len(a) >= 19.9e9)
print(f"fw{g}: {len(a)/1e9:.2f}B tokens, old part {old/1e9:.2f}B, repeats start: {repeat}, ok: {good}")
ok = ok and good
sys.exit(0 if ok else 1)
PY
# 4) re-balance the English weights by file size (English total stays the same)
cp run_args.sh run_args.sh.bak_fw20
python - <<'PY' || { echo "$(date -u) weight update FAILED, restored"; cp run_args.sh.bak_fw20 run_args.sh; exit 1; }
import os, re, pathlib
p = pathlib.Path("run_args.sh")
s = p.read_text()
bins = re.search(r'^BIN="([^"]*)"', s, re.M).group(1).split()
w = re.search(r'^WEIGHTS="([^"]*)"', s, re.M).group(1).split()
assert len(bins) == len(w) == 16, f"expected 16/16, got {len(bins)}/{len(w)}"
eng = [k for k, b in enumerate(bins) if b.endswith("/fineweb.bin")]
assert len(eng) == 5, f"expected 5 English files, got {len(eng)}"
share = sum(float(w[k]) for k in eng)
size = {k: os.path.getsize(bins[k]) for k in eng}
for k in eng:
w[k] = f"{share * size[k] / sum(size.values()):.6f}"
s = re.sub(r'^WEIGHTS="[^"]*"', lambda m: 'WEIGHTS="' + " ".join(w) + '"', s, count=1, flags=re.M)
p.write_text(s)
for k in eng:
print(f"{float(w[k]):.4f} {bins[k].split('/ar-data/')[-1]}")
print(f"English total: {sum(float(w[k]) for k in eng):.4f}")
PY
# 5) apply after the next checkpoint (same nodes); undo the edit if any check fails
if bash -n run_args.sh && bash -c 'source run_args.sh; for f in $BIN; do [ -f "$f" ] || exit 1; done'; then
touch logs/RELOAD && echo "$(date -u) RELOAD set: training switches after the next checkpoint"
else
cp run_args.sh.bak_fw20 run_args.sh; echo "$(date -u) run_args check failed, restored"; exit 1
fi