# /// script # requires-python = ">=3.10" # dependencies = [ # "torch", # "numpy", # "huggingface_hub", # ] # /// """Scaled ST-GFN vs baselines experiment, run on a Hugging Face GPU Job. Scales the two environments where the paper's claims are about *generalisation across large spaces*: * HyperGrid 64x64, period 4 -> 256 reward modes (paper: 32x32, 64 modes) * SingleCell proxy 50 choose 5 -> 2.1M combinations (paper's stated setting; local runs used 24 choose 3 = 2024) Reads the reproduction source from /src (mounted) and writes results to the mounted bucket at /data, and also prints every result line to stdout so the job log remains a complete record even if the upload fails. """ from __future__ import annotations import itertools import json import multiprocessing as mp import os import sys import time from concurrent.futures import ProcessPoolExecutor sys.path.insert(0, "/src") OUT = os.environ.get("OUT_DIR", "/data/scaled") os.makedirs(OUT, exist_ok=True) METHODS = ["stgfn", "tb", "fm", "subtb", "db", "eflownet", "stochastic_gfn", "tb_rnd", "tb_novelty", "tb_icm", "tb_cv"] SEEDS = [0, 1, 2] def worker(job): env_name, method, seed, cfg_over, iters = job import train as T cfg = dict(T.DEFAULT_CFG) cfg.update(cfg_over) cfg["iters"] = iters t0 = time.time() try: res = T.run(env_name, method, seed, cfg, OUT) f = res["final"] line = (f"RESULT {env_name} {method} seed{seed} " f"modes={f.get('modes_found')} cov={f.get('coverage_pct')} " f"top100={f.get('top100_reward'):.4f} corr={f.get('target_corr')} " f"l1={f.get('l1_to_target')} wall={f.get('wall_time_s'):.0f}s") except Exception as exc: # keep the suite going, surface the failure line = f"FAILED {env_name} {method} seed{seed}: {type(exc).__name__}: {exc}" print(line, flush=True) return line def main(): import torch print(f"torch={torch.__version__} cuda={torch.cuda.is_available()} " f"device={torch.cuda.get_device_name(0) if torch.cuda.is_available() else 'cpu'}", flush=True) jobs = [] # scaled HyperGrid: 64x64 grid, period 4 -> 256 modes for m, s in itertools.product(METHODS, SEEDS): jobs.append(("hypergrid", m, s, {"grid_size": 64, "period": 4, "lr": 1e-3}, 2000)) # scaled SingleCell proxy: 50 choose 5 (paper's stated combinatorial size) for m, s in itertools.product(METHODS, SEEDS): jobs.append(("singlecell_proxy", m, s, {"n_genes": 50, "k_genes": 5, "lr": 1e-3}, 3000)) workers = int(os.environ.get("WORKERS", "8")) print(f"{len(jobs)} runs, {workers} workers", flush=True) t0 = time.time() lines = [] # the parent process has already initialised CUDA above, so the pool must # spawn rather than fork its workers ctx = mp.get_context("spawn") with ProcessPoolExecutor(max_workers=workers, mp_context=ctx) as ex: for i, line in enumerate(ex.map(worker, jobs), 1): lines.append(line) print(f"[{i}/{len(jobs)} {time.time()-t0:.0f}s]", flush=True) with open(os.path.join(OUT, "job_summary.txt"), "w") as f: f.write("\n".join(lines)) print(f"SUITE DONE in {time.time()-t0:.0f}s -> {OUT}", flush=True) if __name__ == "__main__": main()