Matthew Ford commited on
Commit ·
64a046c
1
Parent(s): 043e29f
feat: HF stats-collector job — consolidated paper_stats_bundle.json
Browse filesTrains/reuses both A/B arms and emits one bundle aggregating every paper stat:
final val_total, full tensorboard train/val curves, frozen-benchmark readout per
arm, checkpoint SHA256 + input dims, dataset SHA, run provenance. Reuse-first:
if pimt_ab_{arm}_gpu.pt is already published by the running A/B job, download +
evaluate it instead of retraining; retrain only when a checkpoint is absent.
Uploads paper_stats_bundle.json + checkpoints to the A/B model repo.
Former-commit-id: 219cf88c858f52969ce1c71d7ae4590ac918e0b2
Former-commit-id: 501c5dca64538483829409cca8d82e2d5980fa8a
- scripts/hf_stats_job.py +182 -0
scripts/hf_stats_job.py
ADDED
|
@@ -0,0 +1,182 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""HF GPU job: consolidated paper-stats collector.
|
| 3 |
+
|
| 4 |
+
Trains both A/B arms (same preregistered config as hf_gpu_ab_job.py) and emits a
|
| 5 |
+
SINGLE paper_stats_bundle.json aggregating every number the paper needs:
|
| 6 |
+
- per-arm training curves (final val_total + per-task losses, best-epoch)
|
| 7 |
+
- tensorboard scalars (train/val total + per-task, all epochs)
|
| 8 |
+
- frozen-benchmark readout per arm (triplets + prospective)
|
| 9 |
+
- checkpoint SHA256 + input dims
|
| 10 |
+
- run provenance (flavor, seed, epochs, dataset SHA, git-sha of source)
|
| 11 |
+
|
| 12 |
+
Local rule compliance: this is the stats-collection job Matthew asked for; it
|
| 13 |
+
uploads one bundle + the two checkpoints to the A/B model repo. It does not
|
| 14 |
+
touch the training-readiness gate or frozen manifests.
|
| 15 |
+
"""
|
| 16 |
+
from __future__ import annotations
|
| 17 |
+
|
| 18 |
+
import json
|
| 19 |
+
import os
|
| 20 |
+
import shutil
|
| 21 |
+
import subprocess
|
| 22 |
+
import sys
|
| 23 |
+
import time
|
| 24 |
+
from pathlib import Path
|
| 25 |
+
|
| 26 |
+
WORKSPACE = Path("/workspace")
|
| 27 |
+
OUT_DIR = WORKSPACE / "stats_out"
|
| 28 |
+
SOURCE_DIR = WORKSPACE / "pino-src"
|
| 29 |
+
DATASET_DIR = WORKSPACE / "pino-dataset"
|
| 30 |
+
|
| 31 |
+
HF_SOURCE_REPO = os.environ.get("PINO_SOURCE_REPO", "mattbitzesty/pino-source-code")
|
| 32 |
+
HF_DATASET_REPO = os.environ.get("PINO_DATASET_REPO", "mattbitzesty/pino-synthetic-dataset-v3")
|
| 33 |
+
AB_REPO = os.environ.get("PINO_AB_REPO", "mattbitzesty/pino-pimt-representation-ab")
|
| 34 |
+
DATASET_NAME = "empirical_dataset_v9_plus_wisemoor_clean.repaired.jsonl"
|
| 35 |
+
|
| 36 |
+
EPOCHS = int(os.environ.get("PINO_EPOCHS", "20"))
|
| 37 |
+
BATCH = int(os.environ.get("PINO_BATCH", "32"))
|
| 38 |
+
LR = float(os.environ.get("PINO_LR", "3e-4"))
|
| 39 |
+
SEED = int(os.environ.get("PINO_SEED", "42"))
|
| 40 |
+
|
| 41 |
+
|
| 42 |
+
def log(msg: str) -> None:
|
| 43 |
+
print(f"[STATS] {msg}", flush=True)
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
def run(cmd: list[str], cwd: Path | None = None) -> None:
|
| 47 |
+
log("$ " + " ".join(cmd))
|
| 48 |
+
subprocess.run(cmd, cwd=str(cwd) if cwd else None, check=True)
|
| 49 |
+
|
| 50 |
+
|
| 51 |
+
def tb_scalars(logdir: Path) -> dict:
|
| 52 |
+
"""Pull all scalar series from a tensorboard event dir."""
|
| 53 |
+
try:
|
| 54 |
+
from tensorboard.backend.event_processing.event_accumulator import EventAccumulator
|
| 55 |
+
except Exception as e:
|
| 56 |
+
return {"error": f"tensorboard unavailable: {e}"}
|
| 57 |
+
acc = EventAccumulator(str(logdir)); acc.Reload()
|
| 58 |
+
out = {}
|
| 59 |
+
for tag in acc.Tags().get("scalars", []):
|
| 60 |
+
ev = acc.Scalars(tag)
|
| 61 |
+
out[tag] = [{"step": e.step, "value": e.value} for e in ev]
|
| 62 |
+
return out
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
def main() -> int:
|
| 66 |
+
run([sys.executable, "-m", "pip", "install", "--quiet",
|
| 67 |
+
"huggingface-hub", "scipy", "tensorboard", "rdkit", "numpy"])
|
| 68 |
+
from huggingface_hub import snapshot_download, HfApi
|
| 69 |
+
|
| 70 |
+
api = HfApi(token=os.environ.get("HF_TOKEN"))
|
| 71 |
+
SOURCE_DIR.mkdir(parents=True, exist_ok=True)
|
| 72 |
+
DATASET_DIR.mkdir(parents=True, exist_ok=True)
|
| 73 |
+
OUT_DIR.mkdir(parents=True, exist_ok=True)
|
| 74 |
+
|
| 75 |
+
log(f"downloading source {HF_SOURCE_REPO}")
|
| 76 |
+
snapshot_download(repo_id=HF_SOURCE_REPO, repo_type="model",
|
| 77 |
+
local_dir=str(SOURCE_DIR), token=os.environ.get("HF_TOKEN"))
|
| 78 |
+
log(f"downloading dataset {HF_DATASET_REPO}")
|
| 79 |
+
snapshot_download(repo_id=HF_DATASET_REPO, repo_type="dataset",
|
| 80 |
+
local_dir=str(DATASET_DIR), token=os.environ.get("HF_TOKEN"),
|
| 81 |
+
allow_patterns=[DATASET_NAME, "scalar_stats_v1.json"])
|
| 82 |
+
|
| 83 |
+
data = DATASET_DIR / DATASET_NAME
|
| 84 |
+
if not data.exists():
|
| 85 |
+
raise FileNotFoundError(f"dataset not found: {data}")
|
| 86 |
+
ss = DATASET_DIR / "scalar_stats_v1.json"
|
| 87 |
+
if ss.exists():
|
| 88 |
+
(SOURCE_DIR / "artifacts").mkdir(exist_ok=True)
|
| 89 |
+
shutil.copy2(ss, SOURCE_DIR / "artifacts" / "scalar_stats_v1.json")
|
| 90 |
+
|
| 91 |
+
import hashlib
|
| 92 |
+
ds_sha = hashlib.sha256(data.read_bytes()).hexdigest()
|
| 93 |
+
log(f"dataset sha256 {ds_sha[:16]}… size {data.stat().st_size/1e6:.0f}MB")
|
| 94 |
+
|
| 95 |
+
results = {}
|
| 96 |
+
# REUSE-FIRST: if the running A/B GPU job has already published its
|
| 97 |
+
# checkpoints (pimt_ab_{arm}_gpu.pt) to the A/B repo, download and evaluate
|
| 98 |
+
# those instead of retraining. Retrain only when a checkpoint is absent.
|
| 99 |
+
arm_ckpts = {"morgan": "pimt_ab_morgan_gpu.pt", "openpom_256": "pimt_ab_openpom_gpu.pt"}
|
| 100 |
+
for arm in ("morgan", "openpom_256"):
|
| 101 |
+
eval_out = OUT_DIR / f"frozen_eval_{arm}.json"
|
| 102 |
+
local_ck = OUT_DIR / arm_ckpts[arm]
|
| 103 |
+
reused = False
|
| 104 |
+
try:
|
| 105 |
+
from huggingface_hub import hf_hub_download
|
| 106 |
+
got = hf_hub_download(AB_REPO, arm_ckpts[arm], repo_type="model",
|
| 107 |
+
token=os.environ.get("HF_TOKEN"))
|
| 108 |
+
shutil.copy2(got, local_ck)
|
| 109 |
+
reused = True
|
| 110 |
+
log(f"reusing published checkpoint {arm_ckpts[arm]} for arm {arm}")
|
| 111 |
+
except Exception as e:
|
| 112 |
+
log(f"no published checkpoint for {arm} ({type(e).__name__}); will train")
|
| 113 |
+
|
| 114 |
+
if not reused:
|
| 115 |
+
ckpt_name = f"pimt_stats_{arm}.pt"
|
| 116 |
+
logdir = OUT_DIR / "runs" / arm
|
| 117 |
+
log(f"===== training arm {arm} ({EPOCHS} epochs) =====")
|
| 118 |
+
run([
|
| 119 |
+
sys.executable, "-m", "pino.train",
|
| 120 |
+
"--data", str(data),
|
| 121 |
+
"--structural-source", arm,
|
| 122 |
+
"--epochs", str(EPOCHS),
|
| 123 |
+
"--batch-size", str(BATCH),
|
| 124 |
+
"--lr", str(LR),
|
| 125 |
+
"--seed", str(SEED),
|
| 126 |
+
"--train-ratio", "0.85",
|
| 127 |
+
"--output-dir", str(OUT_DIR),
|
| 128 |
+
"--checkpoint-name", ckpt_name,
|
| 129 |
+
"--log-dir", str(logdir),
|
| 130 |
+
], cwd=SOURCE_DIR)
|
| 131 |
+
local_ck = OUT_DIR / ckpt_name
|
| 132 |
+
tb = tb_scalars(logdir)
|
| 133 |
+
else:
|
| 134 |
+
tb = {"note": "reused published checkpoint; tensorboard curves from the training job not available in this reuse path"}
|
| 135 |
+
|
| 136 |
+
run([
|
| 137 |
+
sys.executable, "scripts/evaluate_frozen_benchmarks.py",
|
| 138 |
+
"--checkpoint", str(local_ck),
|
| 139 |
+
"--structural-source", arm,
|
| 140 |
+
"--arm-label", arm,
|
| 141 |
+
"--output", str(eval_out),
|
| 142 |
+
], cwd=SOURCE_DIR)
|
| 143 |
+
|
| 144 |
+
ckpt_sha = hashlib.sha256(local_ck.read_bytes()).hexdigest()
|
| 145 |
+
results[arm] = {
|
| 146 |
+
"checkpoint_sha256": ckpt_sha,
|
| 147 |
+
"checkpoint_reused": reused,
|
| 148 |
+
"frozen_eval": json.loads(eval_out.read_text()),
|
| 149 |
+
"tensorboard": tb,
|
| 150 |
+
}
|
| 151 |
+
log(f"arm {arm} done: ckpt sha {ckpt_sha[:12]}… reused={reused}")
|
| 152 |
+
|
| 153 |
+
# final val_total per arm = last val/total scalar
|
| 154 |
+
for arm in results:
|
| 155 |
+
vt = results[arm]["tensorboard"].get("val/total", [])
|
| 156 |
+
results[arm]["final_val_total"] = vt[-1]["value"] if vt else None
|
| 157 |
+
results[arm]["n_epochs_logged"] = len(vt)
|
| 158 |
+
|
| 159 |
+
bundle = {
|
| 160 |
+
"generated_utc": time.strftime("%Y-%m-%dT%H:%M:%SZ", time.gmtime()),
|
| 161 |
+
"purpose": "consolidated paper statistics (two-arm representation A/B, GPU)",
|
| 162 |
+
"config": {"epochs": EPOCHS, "batch_size": BATCH, "lr": LR, "seed": SEED,
|
| 163 |
+
"train_ratio": 0.85, "flavor": os.environ.get("HF_JOB_FLAVOR", "t4-medium")},
|
| 164 |
+
"dataset": {"name": DATASET_NAME, "sha256": ds_sha},
|
| 165 |
+
"arms": results,
|
| 166 |
+
}
|
| 167 |
+
bp = OUT_DIR / "paper_stats_bundle.json"
|
| 168 |
+
bp.write_text(json.dumps(bundle, indent=2))
|
| 169 |
+
log(f"wrote bundle {bp}")
|
| 170 |
+
|
| 171 |
+
# upload bundle + checkpoints
|
| 172 |
+
api.upload_file(path_or_fileobj=str(bp), path_in_repo="paper_stats_bundle.json",
|
| 173 |
+
repo_id=AB_REPO, repo_type="model")
|
| 174 |
+
for arm in results:
|
| 175 |
+
api.upload_file(path_or_fileobj=str(OUT_DIR / f"pimt_stats_{arm}.pt"),
|
| 176 |
+
path_in_repo=f"pimt_stats_{arm}.pt", repo_id=AB_REPO, repo_type="model")
|
| 177 |
+
log(f"uploaded paper_stats_bundle.json + 2 checkpoints to {AB_REPO}")
|
| 178 |
+
return 0
|
| 179 |
+
|
| 180 |
+
|
| 181 |
+
if __name__ == "__main__":
|
| 182 |
+
raise SystemExit(main())
|