| """Main benchmark driver: parallel per-instance runs, JSONL output. | |
| Usage: python main_run.py --sizes 1000:50:all 5000:50:all 20000:30:10 --out-prefix /data/results --workers 8 | |
| size spec: n:instances:pairwise_count ("all" = pairwise for every instance) | |
| """ | |
| import argparse | |
| import json | |
| import multiprocessing as mp | |
| import os | |
| import sys | |
| import time | |
| # Thread limits must be set before numpy/OpenBLAS loads in any process (parent forks | |
| # workers that import numpy afterwards, so env set here is inherited in time). | |
| os.environ["OMP_NUM_THREADS"] = "1" | |
| os.environ["OPENBLAS_NUM_THREADS"] = "1" | |
| os.environ["MKL_NUM_THREADS"] = "1" | |
| os.environ["NUMEXPR_NUM_THREADS"] = "1" | |
| sys.path.insert(0, "/work") | |
| G = {} | |
| def init_worker(emb_path): | |
| # Embeddings + R are loaded in the parent before Pool creation and inherited by | |
| # forked workers copy-on-write (loading per worker OOMed the 32GB cgroup). | |
| pass | |
| def work(task): | |
| n, idx, pairwise = task | |
| B = G["B"] | |
| assert "x" in G, "worker missing inherited embeddings (fork failed?)" | |
| seed = 10_000 * n + idx # distinct seed spaces per size | |
| t0 = time.perf_counter() | |
| rec = B.run_instance(G["x"], G["y"], G["R"], n, seed, skip_pairwise=not pairwise) | |
| rec["wall"] = time.perf_counter() - t0 | |
| return rec | |
| def main(): | |
| ap = argparse.ArgumentParser() | |
| ap.add_argument("--emb-path", default="/data/cifar10_embeddings.npz") | |
| ap.add_argument("--sizes", nargs="+", required=True) # n:count:pairwise:workers ("all" | int) | |
| ap.add_argument("--out-prefix", default="/data/results") | |
| ap.add_argument("--workers", type=int, default=8, help="fallback if size spec omits workers") | |
| args = ap.parse_args() | |
| tasks_by_size = {} | |
| workers_by_size = {} | |
| for spec in args.sizes: | |
| n, count, pw, workers = spec.split(":") | |
| n, count, workers = int(n), int(count), int(workers) | |
| pw_count = count if pw == "all" else int(pw) | |
| # pairwise legs are the slowest per instance: schedule them last so short | |
| # records drain first under imap_unordered | |
| tasks_by_size[n] = [(n, idx, idx >= count - pw_count) for idx in range(count)] | |
| workers_by_size[n] = workers | |
| import benchmark as B | |
| x, y = B.load_embeddings(args.emb_path) | |
| R = B.class_ref_distances(x, y, seed=12345) # fixed, shared across all instances/sizes | |
| G["x"], G["y"], G["R"], G["B"] = x, y, R, B | |
| # Sizes run sequentially: three concurrent pools of 8 (24 workers, each with a | |
| # multi-GB LP working set) killed two sandboxes; one pool at a time stays sane. | |
| for n, tasks in tasks_by_size.items(): | |
| workers = workers_by_size[n] | |
| out_path = f"{args.out_prefix}_n{n}.jsonl" | |
| done = set() | |
| if os.path.exists(out_path): | |
| with open(out_path) as f: | |
| done = {json.loads(l)["seed"] for l in f if l.strip()} | |
| tasks = [t for t in tasks if (10_000 * n + t[1]) not in done] | |
| print(f"[resume] n={n}: {len(done)} records found, {len(tasks)} remaining", flush=True) | |
| if not tasks: | |
| continue | |
| run_size(n, tasks, out_path, args.emb_path, workers) | |
| print("[main] all sizes complete", flush=True) | |
| def run_size(n, tasks, out_path, emb_path, workers): | |
| with mp.Pool(workers, initializer=init_worker, initargs=(emb_path,)) as pool, open(out_path, "a") as f: | |
| for rec in pool.imap_unordered(work, tasks): | |
| f.write(json.dumps(rec) + "\n") | |
| f.flush() | |
| print(f"[n={n}] done seed={rec['seed']} lp={rec['lprmono']['timer']['lp_solve']:.1f}s " | |
| f"greedy={rec['greedy']['t']:.1f}s " | |
| f"pairwise={rec.get('greedy_pairwise', {}).get('t', '-')}", flush=True) | |
| if __name__ == "__main__": | |
| main() | |
Xet Storage Details
- Size:
- 3.75 kB
- Xet hash:
- 64a8bc7f9170f27ad06081a3c053b724b61c4bc65dc8c28bf4efc56eafc8d0e5
·
Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.