mattbitzesty commited on
Commit
bb3ca71
·
verified ·
1 Parent(s): e6a6e92

R11 eval job: add hf_eval_round11_job.py (train-consistent physics-on evals)

Browse files
Files changed (1) hide show
  1. scripts/hf_eval_round11_job.py +189 -0
scripts/hf_eval_round11_job.py ADDED
@@ -0,0 +1,189 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """HF eval-only job (round-11 panel response): train-consistent physics scoring.
3
+
4
+ Two eval families on EXISTING checkpoints in the AB repo (no retraining):
5
+
6
+ A. "physon" — prospective family-profile readout under TRAIN-CONSISTENT
7
+ conditioning (physics ON, full formula grid), via
8
+ scripts/evaluate_prospective_trainconsistent.py. All 5 A/B arms x 10 seeds.
9
+ Artifacts: frozen_eval_physon_{arm}_seed{n}.json
10
+
11
+ B. "probes" — trained FiLM on/off arms scored on the engine-independent
12
+ frozen probes (substantivity Spearman + descriptor macro-F1; threshold
13
+ not_applicable), via scripts/evaluate_trained_frozen_probes.py. Both
14
+ openpom families x 10 seeds, conditioning on+off.
15
+ Artifacts: trained_probe_{tag}_seed{n}_{on|off}.json
16
+
17
+ Resume-safe: skips artifacts already in the AB repo. Single job (eval-only).
18
+ """
19
+ from __future__ import annotations
20
+
21
+ import json
22
+ import os
23
+ import shutil
24
+ import subprocess
25
+ import sys
26
+ import time
27
+ from pathlib import Path
28
+
29
+ TOKEN = os.environ.get("HF_TOKEN")
30
+ os.environ.setdefault("HF_HUB_ENABLE_XET", "0")
31
+ os.environ.setdefault("HF_HUB_DISABLE_XET", "1")
32
+
33
+ WORKSPACE = Path("/workspace")
34
+ SOURCE_DIR = WORKSPACE / "pino-fresh"
35
+ DATASET_DIR = WORKSPACE / "pino-dataset"
36
+ OUT_DIR = WORKSPACE / "eval_out"
37
+
38
+ SOURCE_REPO = os.environ.get("PINO_SOURCE_REPO", "mattbitzesty/pino-source-code")
39
+ DATASET_REPO = os.environ.get("PINO_DATASET_REPO", "mattbitzesty/pino-synthetic-dataset-v3")
40
+ AB_REPO = os.environ.get("PINO_AB_REPO", "mattbitzesty/pino-pimt-representation-ab")
41
+
42
+ AB_ARMS = ["morgan", "morgan_2048_rp", "morgan_2048_direct", "openpom_256", "disjoint_256"]
43
+ SEEDS = [int(s) for s in os.environ.get("PINO_SEEDS", "1,2,3,4,5,6,7,8,9,10").split(",") if s.strip()]
44
+ FAMILIES = tuple(f for f in os.environ.get("PINO_FAMILIES", "physon,probes").split(",") if f)
45
+
46
+ _START = time.time()
47
+
48
+
49
+ def log(msg: str) -> None:
50
+ print(f"{time.strftime('%Y-%m-%dT%H:%M:%SZ')} {msg}", flush=True)
51
+
52
+
53
+ def run(cmd: list[str]) -> None:
54
+ log(f"$ {' '.join(cmd)}")
55
+ subprocess.check_call(cmd, stdout=sys.stdout, stderr=sys.stderr)
56
+
57
+
58
+ def _disable_xet() -> None:
59
+ try:
60
+ import huggingface_hub.utils._runtime as _runtime
61
+ _runtime.is_xet_available = lambda: False
62
+ except Exception as e: # noqa: BLE001
63
+ log(f"[R11] warn: xet-disable patch failed ({e})")
64
+
65
+
66
+ def _retry(fn, what: str, attempts: int = 3, delay_s: int = 10):
67
+ for attempt in range(1, attempts + 1):
68
+ try:
69
+ return fn()
70
+ except Exception as e: # noqa: BLE001
71
+ if attempt == attempts:
72
+ raise
73
+ log(f"[R11] warn: {what} failed (attempt {attempt}/{attempts}): {e}; retrying in {delay_s}s")
74
+ time.sleep(delay_s)
75
+
76
+
77
+ def _upload(api, local: Path, remote: str) -> None:
78
+ _retry(lambda: api.upload_file(path_or_fileobj=str(local), path_in_repo=remote,
79
+ repo_id=AB_REPO, repo_type="model"),
80
+ f"upload {remote}")
81
+
82
+
83
+ def main() -> int:
84
+ run([sys.executable, "-m", "pip", "install", "--quiet",
85
+ "huggingface-hub", "scipy", "scikit-learn", "rdkit", "numpy"])
86
+ _disable_xet()
87
+
88
+ from huggingface_hub import snapshot_download, HfApi, hf_hub_download
89
+ SOURCE_DIR.mkdir(parents=True, exist_ok=True)
90
+ DATASET_DIR.mkdir(parents=True, exist_ok=True)
91
+ OUT_DIR.mkdir(parents=True, exist_ok=True)
92
+
93
+ log(f"[R11] downloading source {SOURCE_REPO} + dataset {DATASET_REPO}")
94
+ snapshot_download(repo_id=SOURCE_REPO, repo_type="model", local_dir=str(SOURCE_DIR), token=TOKEN)
95
+ snapshot_download(repo_id=DATASET_REPO, repo_type="dataset", local_dir=str(DATASET_DIR), token=TOKEN)
96
+
97
+ reg_src = DATASET_DIR / "registry.db"
98
+ if reg_src.exists():
99
+ shutil.copy2(reg_src, WORKSPACE / "registry.db")
100
+ os.environ["PINO_REGISTRY_PATH"] = str(WORKSPACE / "registry.db")
101
+ ss = DATASET_DIR / "scalar_stats_v1.json"
102
+ if ss.exists():
103
+ (SOURCE_DIR / "artifacts").mkdir(exist_ok=True)
104
+ shutil.copy2(ss, SOURCE_DIR / "artifacts" / "scalar_stats_v1.json")
105
+
106
+ for sub, files in (("pom", ("pom_embeddings.npy", "pom_index.json", "pom_manifest.json")),
107
+ ("pom_disjoint", ("disjoint_embeddings.npy", "disjoint_index.json", "disjoint_manifest.json"))):
108
+ src_dir = DATASET_DIR / "artifacts" / sub
109
+ if src_dir.exists():
110
+ dst = SOURCE_DIR / "artifacts" / sub
111
+ dst.mkdir(parents=True, exist_ok=True)
112
+ for fname in files:
113
+ f = src_dir / fname
114
+ if f.exists():
115
+ shutil.copy2(f, dst / fname)
116
+ log(f"[R11] staged {sub} assets -> {dst}")
117
+ else:
118
+ log(f"[R11] WARN: no {sub} assets at {src_dir}")
119
+
120
+ run([sys.executable, "-m", "pip", "install", "--quiet", "-e", str(SOURCE_DIR)])
121
+ sys.path.insert(0, str(SOURCE_DIR / "src"))
122
+
123
+ api = HfApi(token=TOKEN)
124
+ have = set()
125
+ try:
126
+ have = set(api.list_repo_files(AB_REPO, repo_type="model"))
127
+ except Exception as e: # noqa: BLE001
128
+ log(f"[R11] warn: could not list AB repo ({e}); assuming nothing done")
129
+
130
+ # ---- Family A: prospective train-consistent (physics-on) evals ----
131
+ if "physon" in FAMILIES:
132
+ for arm in AB_ARMS:
133
+ for seed in SEEDS:
134
+ tag = f"{arm}_seed{seed}"
135
+ out_name = f"frozen_eval_physon_{tag}.json"
136
+ if out_name in have:
137
+ continue
138
+ ckpt_name = f"pimt_ab_{tag}.pt"
139
+ try:
140
+ ckpt = _retry(lambda: hf_hub_download(repo_id=AB_REPO, repo_type="model",
141
+ filename=ckpt_name, token=TOKEN),
142
+ f"download {ckpt_name}")
143
+ except Exception as e: # noqa: BLE001
144
+ log(f"[R11] SKIP {tag}: checkpoint unavailable ({e})")
145
+ continue
146
+ out = OUT_DIR / out_name
147
+ log(f"[R11] ===== physon eval {tag} =====")
148
+ try:
149
+ run([sys.executable, str(SOURCE_DIR / "scripts" / "evaluate_prospective_trainconsistent.py"),
150
+ "--checkpoint", str(ckpt), "--arm", arm,
151
+ "--skip-download",
152
+ "--output", str(out)])
153
+ except Exception as e: # noqa: BLE001
154
+ log(f"[R11] FAIL physon {tag}: {e}")
155
+ continue
156
+ _upload(api, out, out_name)
157
+ have.add(out_name)
158
+ log(f"[R11] uploaded {out_name}")
159
+
160
+ # ---- Family B: trained frozen probes (one process amortizes engine sims) ----
161
+ if "probes" in FAMILIES:
162
+ ckpts = []
163
+ for seed in SEEDS:
164
+ ckpts.append(f"pimt_ab_openpom_256_seed{seed}.pt")
165
+ ckpts.append(f"pimt_physoff_openpom_256_seed{seed}.pt")
166
+ probe_dir = OUT_DIR / "trained_probes"
167
+ probe_dir.mkdir(exist_ok=True)
168
+ log(f"[R11] ===== trained frozen probes: {len(ckpts)} checkpoints =====")
169
+ try:
170
+ run([sys.executable, str(SOURCE_DIR / "scripts" / "evaluate_trained_frozen_probes.py"),
171
+ "--checkpoints", *ckpts,
172
+ "--conditioning", "on", "off",
173
+ "--output", str(probe_dir)])
174
+ except Exception as e: # noqa: BLE001
175
+ log(f"[R11] FAIL probes: {e}")
176
+ for f in sorted(probe_dir.glob("*.json")):
177
+ remote = f"trained_probe_{f.name}" if not f.name.startswith("trained_probe_") else f.name
178
+ if remote in have:
179
+ continue
180
+ _upload(api, f, remote)
181
+ have.add(remote)
182
+ log(f"[R11] uploaded {remote}")
183
+
184
+ log(f"[R11] done in {(time.time() - _START)/60:.1f} min")
185
+ return 0
186
+
187
+
188
+ if __name__ == "__main__":
189
+ raise SystemExit(main())