multiseed job: resume + self-chaining
Browse files- scripts/hf_gpu_ab_multiseed_job.py +145 -75
scripts/hf_gpu_ab_multiseed_job.py
CHANGED
|
@@ -55,6 +55,15 @@ DATASET_NAME = "empirical_dataset_v10_alltags.jsonl"
|
|
| 55 |
ARMS = tuple(a for a in os.environ.get("PINO_ARMS", "morgan,openpom_256").split(",") if a)
|
| 56 |
SEEDS = [int(s) for s in os.environ.get("PINO_SEEDS", "1,2,3,4,5").split(",") if s.strip()]
|
| 57 |
TOST_DELTA = os.environ.get("PINO_TOST_DELTA", "0.05")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 58 |
|
| 59 |
|
| 60 |
def log(msg: str) -> None:
|
|
@@ -131,94 +140,155 @@ def main() -> int:
|
|
| 131 |
api = HfApi(token=TOKEN)
|
| 132 |
results: dict[str, dict] = {}
|
| 133 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 134 |
for seed in SEEDS:
|
| 135 |
for arm in ARMS:
|
| 136 |
tag = f"{arm}_seed{seed}"
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
repo_id=AB_REPO, repo_type="model")
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
# ---- TOST equivalence analysis across paired seeds --
|
|
|
|
| 195 |
tost_report = None
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
| 212 |
|
| 213 |
summary = {"epochs": EPOCHS, "batch": BATCH, "lr": LR, "seeds": SEEDS, "arms": ARMS,
|
| 214 |
"train_ratio": TRAIN_RATIO, "device": device, "dataset": DATASET_NAME,
|
| 215 |
-
"tost_delta": TOST_DELTA, "tost": tost_report,
|
|
|
|
| 216 |
"results": results,
|
| 217 |
"note": "multi-seed representation A/B + TOST equivalence on corrected v10 labels; "
|
| 218 |
"openpom overlap via novelty-weighted prospective; split zero-intersection asserted per seed."}
|
| 219 |
api.upload_file(path_or_fileobj=json.dumps(summary, indent=2).encode(),
|
| 220 |
path_in_repo="gpu_run_summary_multiseed.json", repo_id=AB_REPO, repo_type="model")
|
| 221 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 222 |
return 0
|
| 223 |
|
| 224 |
|
|
|
|
| 55 |
ARMS = tuple(a for a in os.environ.get("PINO_ARMS", "morgan,openpom_256").split(",") if a)
|
| 56 |
SEEDS = [int(s) for s in os.environ.get("PINO_SEEDS", "1,2,3,4,5").split(",") if s.strip()]
|
| 57 |
TOST_DELTA = os.environ.get("PINO_TOST_DELTA", "0.05")
|
| 58 |
+
# Wall-clock budget: HF t4-small kills the container at ~90 min (exit 143). One
|
| 59 |
+
# (arm,seed) run is ~35-40 min, so we process runs until the budget is nearly spent,
|
| 60 |
+
# then RELAUNCH ourselves for the remaining runs. This makes the multi-seed sweep
|
| 61 |
+
# resilient to the platform limit without manual babysitting.
|
| 62 |
+
WALL_BUDGET_S = int(os.environ.get("PINO_WALL_BUDGET_S", "5100")) # ~85 min
|
| 63 |
+
JOB_IMAGE = os.environ.get("PINO_JOB_IMAGE", "pytorch/pytorch:2.4.0-cuda12.1-cudnn9-runtime")
|
| 64 |
+
JOB_FLAVOR = os.environ.get("PINO_JOB_FLAVOR", "t4-small")
|
| 65 |
+
JOB_TIMEOUT = os.environ.get("PINO_JOB_TIMEOUT", "21600")
|
| 66 |
+
_START = time.time()
|
| 67 |
|
| 68 |
|
| 69 |
def log(msg: str) -> None:
|
|
|
|
| 140 |
api = HfApi(token=TOKEN)
|
| 141 |
results: dict[str, dict] = {}
|
| 142 |
|
| 143 |
+
# Figure out which (arm,seed) runs are already complete in the AB repo, so a
|
| 144 |
+
# chained/again launch resumes instead of retraining from scratch.
|
| 145 |
+
have = set()
|
| 146 |
+
try:
|
| 147 |
+
have = set(api.list_repo_files(AB_REPO, repo_type="model"))
|
| 148 |
+
except Exception as e: # noqa: BLE001
|
| 149 |
+
log(f"[AB] warn: could not list AB repo ({e}); assuming nothing done")
|
| 150 |
+
|
| 151 |
+
def _elapsed() -> float:
|
| 152 |
+
return time.time() - _START
|
| 153 |
+
|
| 154 |
+
pending: list[tuple[int, str]] = []
|
| 155 |
for seed in SEEDS:
|
| 156 |
for arm in ARMS:
|
| 157 |
tag = f"{arm}_seed{seed}"
|
| 158 |
+
if f"pimt_ab_{tag}.pt" in have and f"frozen_eval_{tag}.json" in have:
|
| 159 |
+
log(f"[AB] skip {tag} (already uploaded)")
|
| 160 |
+
continue
|
| 161 |
+
pending.append((seed, arm))
|
| 162 |
+
log(f"[AB] pending runs: {pending} (elapsed {(_elapsed()/60):.1f} min)")
|
| 163 |
+
|
| 164 |
+
completed_now: list[tuple[int, str]] = []
|
| 165 |
+
for seed, arm in pending:
|
| 166 |
+
tag = f"{arm}_seed{seed}"
|
| 167 |
+
# Budget check BEFORE starting a ~40-min run: if we're near the platform's
|
| 168 |
+
# wall-clock kill, stop and chain to a fresh job for the rest.
|
| 169 |
+
if _elapsed() > WALL_BUDGET_S:
|
| 170 |
+
log(f"[AB] wall budget reached ({(_elapsed()/60):.1f} min); chaining for remaining runs")
|
| 171 |
+
break
|
| 172 |
+
ckpt_name = f"pimt_ab_{tag}.pt"
|
| 173 |
+
eval_name = f"frozen_eval_{tag}.json"
|
| 174 |
+
eval_out = OUT_DIR / eval_name
|
| 175 |
+
logdir = OUT_DIR / "runs" / tag
|
| 176 |
+
log(f"[AB] ===== seed={seed} arm={arm} ({EPOCHS} epochs) =====")
|
| 177 |
+
run([sys.executable, "-m", "pino.train",
|
| 178 |
+
"--data", data_path,
|
| 179 |
+
"--structural-source", arm,
|
| 180 |
+
"--epochs", str(EPOCHS),
|
| 181 |
+
"--batch-size", str(BATCH),
|
| 182 |
+
"--lr", LR,
|
| 183 |
+
"--seed", str(seed),
|
| 184 |
+
"--train-ratio", str(TRAIN_RATIO),
|
| 185 |
+
"--output-dir", str(OUT_DIR),
|
| 186 |
+
"--checkpoint-name", ckpt_name,
|
| 187 |
+
"--log-dir", str(logdir)])
|
| 188 |
+
ckpt = OUT_DIR / ckpt_name
|
| 189 |
+
|
| 190 |
+
# Stamp final_val_total + best_epoch from the checkpoint (eval JSON otherwise omits it).
|
| 191 |
+
final_val, best_epoch = None, None
|
| 192 |
+
try:
|
| 193 |
+
blob = torch.load(ckpt, map_location="cpu")
|
| 194 |
+
final_val = float(blob.get("val_loss")) if blob.get("val_loss") is not None else None
|
| 195 |
+
best_epoch = int(blob.get("epoch")) if blob.get("epoch") is not None else None
|
| 196 |
+
except Exception as e: # noqa: BLE001
|
| 197 |
+
log(f"[AB] warn: could not read checkpoint for val_total stamp ({e})")
|
| 198 |
+
|
| 199 |
+
log(f"[AB] ===== frozen-benchmark eval {tag} =====")
|
| 200 |
+
run([sys.executable, str(SOURCE_DIR / "scripts" / "evaluate_frozen_benchmarks.py"),
|
| 201 |
+
"--checkpoint", str(ckpt),
|
| 202 |
+
"--structural-source", arm,
|
| 203 |
+
"--arm-label", tag,
|
| 204 |
+
"--output", str(eval_out)])
|
| 205 |
+
|
| 206 |
+
# Stamp training-loss summary into the eval JSON.
|
| 207 |
+
try:
|
| 208 |
+
ej = json.loads(eval_out.read_text())
|
| 209 |
+
ej["final_val_total"] = final_val
|
| 210 |
+
ej["best_epoch"] = best_epoch
|
| 211 |
+
ej["seed"] = seed
|
| 212 |
+
eval_out.write_text(json.dumps(ej, indent=2))
|
| 213 |
+
except Exception as e: # noqa: BLE001
|
| 214 |
+
log(f"[AB] warn: could not stamp eval JSON ({e})")
|
| 215 |
+
|
| 216 |
+
# Upload checkpoint + eval + per-seed leakage report immediately.
|
| 217 |
+
api.upload_file(path_or_fileobj=str(ckpt), path_in_repo=ckpt_name,
|
| 218 |
+
repo_id=AB_REPO, repo_type="model")
|
| 219 |
+
api.upload_file(path_or_fileobj=str(eval_out), path_in_repo=eval_name,
|
| 220 |
+
repo_id=AB_REPO, repo_type="model")
|
| 221 |
+
leak = OUT_DIR / f"split_leakage_report_seed{seed}.json"
|
| 222 |
+
if leak.exists():
|
| 223 |
+
api.upload_file(path_or_fileobj=str(leak), path_in_repo=leak.name,
|
| 224 |
repo_id=AB_REPO, repo_type="model")
|
| 225 |
+
log(f"[AB] uploaded {tag} checkpoint + eval + leakage report")
|
| 226 |
+
results[tag] = {"checkpoint": ckpt_name, "eval": eval_name,
|
| 227 |
+
"final_val_total": final_val, "best_epoch": best_epoch}
|
| 228 |
+
completed_now.append((seed, arm))
|
| 229 |
+
|
| 230 |
+
remaining = [(s, a) for (s, a) in pending if (s, a) not in completed_now]
|
| 231 |
+
log(f"[AB] completed this job: {completed_now} | remaining: {remaining}")
|
| 232 |
+
|
| 233 |
+
# ---- TOST equivalence analysis across paired seeds (only if all seeds done) --
|
| 234 |
+
all_done = not remaining
|
| 235 |
tost_report = None
|
| 236 |
+
if all_done:
|
| 237 |
+
try:
|
| 238 |
+
tost_out = OUT_DIR / "multiseed_tost_analysis.json"
|
| 239 |
+
run([sys.executable, str(SOURCE_DIR / "scripts" / "multiseed_tost_analysis.py"),
|
| 240 |
+
"--eval-dir", str(OUT_DIR),
|
| 241 |
+
"--glob", "frozen_eval_{arm}_seed{seed}.json",
|
| 242 |
+
"--arm-a", "openpom_256", "--arm-b", "morgan",
|
| 243 |
+
"--seeds", ",".join(str(s) for s in SEEDS),
|
| 244 |
+
"--metric", "prospective_cosine",
|
| 245 |
+
"--delta", TOST_DELTA,
|
| 246 |
+
"--output", str(tost_out)])
|
| 247 |
+
tost_report = json.loads(tost_out.read_text())
|
| 248 |
+
api.upload_file(path_or_fileobj=str(tost_out), path_in_repo="multiseed_tost_analysis.json",
|
| 249 |
+
repo_id=AB_REPO, repo_type="model")
|
| 250 |
+
log("[AB] uploaded TOST analysis")
|
| 251 |
+
except Exception as e: # noqa: BLE001
|
| 252 |
+
log(f"[AB] warn: TOST analysis failed ({e})")
|
| 253 |
+
else:
|
| 254 |
+
log("[AB] skipping TOST until all seeds complete")
|
| 255 |
|
| 256 |
summary = {"epochs": EPOCHS, "batch": BATCH, "lr": LR, "seeds": SEEDS, "arms": ARMS,
|
| 257 |
"train_ratio": TRAIN_RATIO, "device": device, "dataset": DATASET_NAME,
|
| 258 |
+
"tost_delta": TOST_DELTA, "tost": tost_report, "all_seeds_complete": all_done,
|
| 259 |
+
"completed_this_job": completed_now, "remaining": remaining,
|
| 260 |
"results": results,
|
| 261 |
"note": "multi-seed representation A/B + TOST equivalence on corrected v10 labels; "
|
| 262 |
"openpom overlap via novelty-weighted prospective; split zero-intersection asserted per seed."}
|
| 263 |
api.upload_file(path_or_fileobj=json.dumps(summary, indent=2).encode(),
|
| 264 |
path_in_repo="gpu_run_summary_multiseed.json", repo_id=AB_REPO, repo_type="model")
|
| 265 |
+
|
| 266 |
+
# ---- Chain: relaunch for remaining runs before the platform kills us ---------
|
| 267 |
+
if remaining:
|
| 268 |
+
rem_seeds = sorted({s for s, _ in remaining})
|
| 269 |
+
log(f"[AB] chaining: relaunching job for remaining seeds {rem_seeds}")
|
| 270 |
+
try:
|
| 271 |
+
chain_cmd = (
|
| 272 |
+
"set -e; pip install -q huggingface-hub; "
|
| 273 |
+
"python -c \"from huggingface_hub import hf_hub_download; import runpy; "
|
| 274 |
+
"p=hf_hub_download('mattbitzesty/pino-source-code','scripts/hf_gpu_ab_multiseed_job.py',repo_type='model'); "
|
| 275 |
+
"runpy.run_path(p,run_name='__main__')\""
|
| 276 |
+
)
|
| 277 |
+
new_job = api.run_job(
|
| 278 |
+
image=JOB_IMAGE,
|
| 279 |
+
command=["bash", "-lc", chain_cmd],
|
| 280 |
+
secrets={"HF_TOKEN": TOKEN, "PINO_SEEDS": ",".join(str(s) for s in rem_seeds),
|
| 281 |
+
"PINO_ARMS": ",".join(ARMS), "PINO_EPOCHS": str(EPOCHS)},
|
| 282 |
+
flavor=JOB_FLAVOR,
|
| 283 |
+
timeout=int(JOB_TIMEOUT),
|
| 284 |
+
namespace="mattbitzesty",
|
| 285 |
+
)
|
| 286 |
+
log(f"[AB] chained job started: {getattr(new_job,'id',new_job)} for seeds {rem_seeds}")
|
| 287 |
+
except Exception as e: # noqa: BLE001
|
| 288 |
+
log(f"[AB] ERROR: chain relaunch failed ({e}); rerun manually with PINO_SEEDS={rem_seeds}")
|
| 289 |
+
return 2
|
| 290 |
+
else:
|
| 291 |
+
log("[AB] DONE - all seeds complete")
|
| 292 |
return 0
|
| 293 |
|
| 294 |
|