"""Colab GPU cell driver for Trinity H2a (16_EVIDENCE/TRINITY/LB_H2A_PREREG_2026-11-20.md): train the new layer-92 experts in two preregistered configurations on the train cache, choose on validation only, then a DEV evaluation on the already-seen holdout (not confirmatory). The confirmatory evaluation on the sealed fresh holdout runs later. import os; from google.colab import userdata; import huggingface_hub as h os.environ['HF_TOKEN'] = userdata.get('STARHUGGIN') exec(open(h.hf_hub_download('STAR-future/Trinity', 'tools/colab_h2a.py', token=os.environ['HF_TOKEN'], force_download=True)).read()) """ import json import math import os import subprocess import sys from pathlib import Path from huggingface_hub import HfApi, hf_hub_download, snapshot_download TOK = os.environ["HF_TOKEN"] REPO, CACHE = "STAR-future/Trinity", "STAR-future/Trinity-k3cache" W = Path("/content/k3p") W.mkdir(parents=True, exist_ok=True) api = HfApi(token=TOK) CONFIGS = [("N1", 3e-4), ("N2", 1e-3)] def up(path: Path, name: str) -> None: for k in range(3): try: api.upload_file(path_or_fileobj=str(path), path_in_repo=f"eval/h2a/{name}", repo_id=REPO, commit_message=f"H2a {name}") return except Exception as e: # noqa: BLE001 print("upload retry", name, k, type(e).__name__, flush=True) subprocess.run([sys.executable, "-m", "pip", "install", "-q", "tiktoken"], check=True) for f in ("k3ref.py", "k3pass.py", "lb_tail_train.py", "lb_newexperts.py"): (W / f).write_bytes(Path(hf_hub_download(REPO, f"tools/{f}", token=TOK, force_download=True)).read_bytes()) for m in ("train", "holdout"): if not (W / f"out_{m}" / "summary.json").exists(): snapshot_download(CACHE, repo_type="dataset", token=TOK, allow_patterns=[f"{m}/*"], local_dir=str(W / "cache_dl")) (W / f"out_{m}").mkdir(exist_ok=True) for f in (W / "cache_dl" / m).iterdir(): f.rename(W / f"out_{m}" / f.name) rows = [] for name, lr in CONFIGS: out = W / "h2a" / name if not (out / "train_record.json").exists(): rc = subprocess.run([sys.executable, "-u", str(W / "lb_newexperts.py"), "train", "--cache", str(W / "out_train"), "--out", str(out), "--lr", str(lr), "--epochs", "4"], cwd=W).returncode if rc != 0: rows.append({"config": name, "lr": lr, "rc": rc}) continue r = json.loads((out / "train_record.json").read_text()) rows.append({"config": name, "lr": lr, "rc": 0, "rel_dom": r["rel_dom"], "rel_gen": r["rel_gen"], "new_expert_parameters": r["new_expert_parameters"], "steps": r["steps"]}) up(out / "train_record.json", f"{name}_train_record.json") print(json.dumps(rows[-1]), flush=True) ok = [x for x in rows if x.get("rc") == 0 and x["rel_gen"] is not None and math.isfinite(x["rel_dom"]) and x["rel_gen"] <= 0.005] ok.sort(key=lambda x: x["rel_dom"]) sel = {"prereg": "16_EVIDENCE/TRINITY/LB_H2A_PREREG_2026-11-20.md", "rows": rows, "choice": ok[0]["config"] if ok else None, "reading": None if not ok else ("PROMISING" if ok[0]["rel_dom"] <= -0.015 else "NOT_BETTER_THAN_LBB" if ok[0]["rel_dom"] > -0.006 else "BETWEEN")} (W / "h2a" / "selection.json").write_text(json.dumps(sel, indent=1)) up(W / "h2a" / "selection.json", "selection.json") print("SELECTION", json.dumps(sel), flush=True) if sel["choice"]: d = W / "h2a" / sel["choice"] rc = subprocess.run([sys.executable, "-u", str(W / "lb_newexperts.py"), "eval", "--cache", str(W / "out_holdout"), "--trained", str(d / "new_experts.pt"), "--out", str(d / "dev")], cwd=W).returncode print("dev eval rc", rc, flush=True) for f, n in ((d / "dev" / "verdict_h2a.json", "dev_verdict_h2a.json"), (d / "dev" / "verdict_h2a.raw.npz", "dev_verdict_h2a.raw.npz"), (d / "new_experts.pt", f"{sel['choice']}_new_experts.pt")): if f.exists(): up(f, n)