Upload alea/loop.py with huggingface_hub
Browse files- alea/loop.py +131 -0
alea/loop.py
ADDED
|
@@ -0,0 +1,131 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Iterative training loop: generate -> train -> eval -> repeat.
|
| 2 |
+
|
| 3 |
+
Each round generates a FRESH corpus (new seed -> new instances), trains from
|
| 4 |
+
the previous round's checkpoint, and evaluates on a FIXED test set so metrics
|
| 5 |
+
are comparable across rounds. Appends to runs/loop_history.jsonl.
|
| 6 |
+
|
| 7 |
+
Usage:
|
| 8 |
+
python -m alea.loop --rounds 4 --init runs\alea_v3\final.pt
|
| 9 |
+
"""
|
| 10 |
+
import argparse
|
| 11 |
+
import json
|
| 12 |
+
import os
|
| 13 |
+
import subprocess
|
| 14 |
+
import sys
|
| 15 |
+
import time
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
def run(cmd, **kw):
|
| 19 |
+
print("+", " ".join(cmd), flush=True)
|
| 20 |
+
return subprocess.run(cmd, **kw)
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def gen_corpus(round_idx, n, out_path, shares, seed_base):
|
| 24 |
+
from .gen_all import build
|
| 25 |
+
rows = build(n, seed=seed_base + round_idx, shares=shares)
|
| 26 |
+
from .genbase import write_jsonl
|
| 27 |
+
write_jsonl(rows, out_path)
|
| 28 |
+
return len(rows)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def eval_metrics(ckpt, test_path, max_items=1500):
|
| 32 |
+
r = run([sys.executable, "-m", "alea.eval", "--ckpt", ckpt,
|
| 33 |
+
"--data", test_path, "--max-items", str(max_items),
|
| 34 |
+
"--skip-structural"], capture_output=True, text=True)
|
| 35 |
+
out = r.stdout
|
| 36 |
+
start = out.find("{")
|
| 37 |
+
end = out.rfind("}") + 1
|
| 38 |
+
if start < 0 or end <= start:
|
| 39 |
+
print(out[-2000:], r.stderr[-2000:])
|
| 40 |
+
return {}
|
| 41 |
+
try:
|
| 42 |
+
return json.loads(out[start:end])
|
| 43 |
+
except json.JSONDecodeError:
|
| 44 |
+
print(out[-2000:])
|
| 45 |
+
return {}
|
| 46 |
+
|
| 47 |
+
|
| 48 |
+
def main():
|
| 49 |
+
ap = argparse.ArgumentParser()
|
| 50 |
+
ap.add_argument("--rounds", type=int, default=4)
|
| 51 |
+
ap.add_argument("--init", required=True, help="starting checkpoint")
|
| 52 |
+
ap.add_argument("--n-per-round", type=int, default=60000)
|
| 53 |
+
ap.add_argument("--epochs-per-round", type=float, default=1.0)
|
| 54 |
+
ap.add_argument("--batch", type=int, default=16)
|
| 55 |
+
ap.add_argument("--accum", type=int, default=2)
|
| 56 |
+
ap.add_argument("--test", default="data/test_loop.jsonl")
|
| 57 |
+
ap.add_argument("--shares", default=None)
|
| 58 |
+
ap.add_argument("--lr-head", type=float, default=1e-4)
|
| 59 |
+
ap.add_argument("--lr-backbone", type=float, default=1e-5)
|
| 60 |
+
ap.add_argument("--eval-items", type=int, default=1500)
|
| 61 |
+
ap.add_argument("--history", default="runs/loop_history.jsonl")
|
| 62 |
+
args = ap.parse_args()
|
| 63 |
+
|
| 64 |
+
env = dict(os.environ)
|
| 65 |
+
env.setdefault("PYTHONIOENCODING", "utf-8")
|
| 66 |
+
|
| 67 |
+
shares = json.loads(args.shares) if args.shares else {
|
| 68 |
+
"gen_stoch": 0.9, "gen_enterprise": 1.0, "gen_structured": 0.8,
|
| 69 |
+
"gen_adversarial": 0.6, "gen_numeric": 0.7, "gen_long": 0.5,
|
| 70 |
+
"base": 0.4, "gen_kaggle_cls": 1.0, "gen_kaggle_reg": 0.7,
|
| 71 |
+
"gen_hf": 1.3}
|
| 72 |
+
# drop shares for modules that don't exist yet
|
| 73 |
+
from . import gen_all
|
| 74 |
+
avail = gen_all.sources()
|
| 75 |
+
shares = {k: w for k, w in shares.items() if k in avail}
|
| 76 |
+
print("active sources:", shares, flush=True)
|
| 77 |
+
|
| 78 |
+
# fixed test set for comparable metrics across rounds
|
| 79 |
+
if not os.path.exists(args.test):
|
| 80 |
+
print("generating fixed loop test set...", flush=True)
|
| 81 |
+
rows = gen_all.build(3000, seed=777, shares=shares)
|
| 82 |
+
from .genbase import write_jsonl
|
| 83 |
+
write_jsonl(rows, args.test)
|
| 84 |
+
print(f" wrote {len(rows)} -> {args.test}", flush=True)
|
| 85 |
+
|
| 86 |
+
ckpt = args.init
|
| 87 |
+
prev_loop_ckpt = None
|
| 88 |
+
for r in range(args.rounds):
|
| 89 |
+
t0 = time.time()
|
| 90 |
+
print(f"\n===== ROUND {r} (init={ckpt}) =====", flush=True)
|
| 91 |
+
corpus = f"data/loop_train_r{r}.jsonl"
|
| 92 |
+
if not os.path.exists(corpus):
|
| 93 |
+
n = gen_corpus(r, args.n_per_round, corpus, shares,
|
| 94 |
+
seed_base=1000)
|
| 95 |
+
print(f"corpus: {n} rows -> {corpus}", flush=True)
|
| 96 |
+
else:
|
| 97 |
+
print(f"corpus exists: {corpus}", flush=True)
|
| 98 |
+
|
| 99 |
+
outdir = f"runs/loop_r{r}"
|
| 100 |
+
run([sys.executable, "-m", "alea.train",
|
| 101 |
+
"--train", corpus, "--dev", args.test,
|
| 102 |
+
"--out", outdir, "--init-from", ckpt,
|
| 103 |
+
"--train-mode", "full",
|
| 104 |
+
"--epochs", str(args.epochs_per_round),
|
| 105 |
+
"--batch", str(args.batch), "--accum", str(args.accum),
|
| 106 |
+
"--lr-head", str(args.lr_head),
|
| 107 |
+
"--lr-backbone", str(args.lr_backbone),
|
| 108 |
+
"--attn", "sdpa", "--save-every", "100000",
|
| 109 |
+
"--max-len", "4096"], env=env)
|
| 110 |
+
ckpt = os.path.join(outdir, "final.pt")
|
| 111 |
+
if not os.path.exists(ckpt):
|
| 112 |
+
print(f"ROUND {r} TRAIN FAILED - stopping", flush=True)
|
| 113 |
+
break
|
| 114 |
+
# keep only the newest loop checkpoint (each ~1.3GB); the --init
|
| 115 |
+
# base is outside runs/loop_* and untouched
|
| 116 |
+
if prev_loop_ckpt and os.path.exists(prev_loop_ckpt):
|
| 117 |
+
os.remove(prev_loop_ckpt)
|
| 118 |
+
prev_loop_ckpt = ckpt
|
| 119 |
+
|
| 120 |
+
m = eval_metrics(ckpt, args.test, args.eval_items)
|
| 121 |
+
rec = {"round": r, "ckpt": ckpt, "minutes":
|
| 122 |
+
round((time.time() - t0) / 60, 1), **m}
|
| 123 |
+
with open(args.history, "a", encoding="utf-8") as fh:
|
| 124 |
+
fh.write(json.dumps(rec) + "\n")
|
| 125 |
+
print(f"ROUND {r} METRICS {json.dumps(m)}", flush=True)
|
| 126 |
+
|
| 127 |
+
print("\nloop done. history:", args.history)
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
if __name__ == "__main__":
|
| 131 |
+
main()
|