"""Parallel front end for score_samples.py (math-verify has no working timeout on Windows, so one pathological answer can hang the serial scorer for hours). Every distinct (gold, first answer) pair is graded once, in worker processes; a worker stuck on one pair for 20 s is killed and the pair counts as wrong (the pairs are listed in TIMEOUTS_JSON). Then score_samples.main runs unchanged with those results. usage: score_parallel.py SAMPLES_JSONL HOLDOUT_JSONL --out PER_PROMPT_JSONL [--mode direct|open] [--workers 7] Run with the grader venv's Python so math-verify can be imported. """ import json import multiprocessing as mp import sys import time from pathlib import Path HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE)) sys.path.insert(0, str(HERE / "grader")) LIMIT = 20.0 def worker(conn): import math_equiv while True: gold, ans = conn.recv() try: conn.send(bool(math_equiv.equivalent(gold, ans))) except Exception: conn.send(False) def spawn(): a, b = mp.Pipe() p = mp.Process(target=worker, args=(b,), daemon=True) p.start() return {"p": p, "c": a, "job": None, "t": 0.0} def main(samples, holdout, out, mode="direct", workers=7): import math_equiv import score_samples from score_samples import read_jsonl strict = mode == "open" gold = {h["instance_id"]: h["final_answer"] for h in read_jsonl(holdout)} pairs = set() for r in read_jsonl(samples): if r["instance_id"] in gold: a = math_equiv.first_answer(r["text"], strict) if a: pairs.add((gold[r["instance_id"]], a)) todo, res, timeouts, t0 = list(pairs), {}, [], time.time() print(len(pairs), "distinct (gold, answer) pairs", flush=True) ws = [spawn() for _ in range(workers)] last = 0 while todo or any(w["job"] for w in ws): for i, w in enumerate(ws): if w["job"] is not None: try: ready = w["c"].poll() if ready: res[w["job"]] = w["c"].recv() w["job"] = None except (BrokenPipeError, EOFError, OSError): # the worker died while grading this answer (a crash inside math-verify / SymPy): count it wrong res[w["job"]] = False timeouts.append(w["job"]) w["p"].kill() ws[i] = w = spawn() ready = False if w["job"] is None: pass elif ready: pass elif time.time() - w["t"] > LIMIT: res[w["job"]] = False timeouts.append(w["job"]) w["p"].kill() ws[i] = w = spawn() if w["job"] is None and todo: w["job"] = todo.pop() w["t"] = time.time() w["c"].send(w["job"]) if time.time() - last > 60: last = time.time() print(f"{len(res)}/{len(pairs)} graded, {len(timeouts)} timeouts, {int(last - t0)} s", flush=True) time.sleep(0.005) for w in ws: w["p"].kill() print(f"graded {len(res)} pairs in {int(time.time() - t0)} s; {len(timeouts)} timed out (counted wrong)", flush=True) Path(out).with_suffix(".timeouts.json").write_text(json.dumps([[g[:200], a[:200]] for g, a in timeouts])) math_equiv.equivalent = lambda g, a: bool(a) and res[(g, a)] score_samples.main(samples, holdout, out, mode) if __name__ == "__main__": mp.freeze_support() a = sys.argv[1:] main(a[0], a[1], a[a.index("--out") + 1], a[a.index("--mode") + 1] if "--mode" in a else "direct", int(a[a.index("--workers") + 1]) if "--workers" in a else 7)