"""Re-mix a stored reax14 bench run (bench/hfdi_speed.py output with part_probabilities) under another config's part weights, through reax14_core.mix itself. Weights act only in that CPU mix after every part has run, so a re-mix gives the exact answers the other config would serve on the same machine (the GPU work is the same). python -I bench/replay_mix.py RUN.jsonl reax14/.json reax14/.json OUT.jsonl Self-check first: re-mixing with the run's own config must reproduce every stored answer bit-for-bit (else exit 1).""" import hashlib, json, math, statistics, sys, time from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "reax14")) import reax14_core as C # noqa: E402 def weights(cfg): return {p["name"]: float(p["weight"]) for p in json.loads(Path(cfg).read_text())["parts"]} def kit_dist(a): # = bench/hfdi_speed.py: noul -> {false, true}; choice/score -> probabilities if a["type"] == "noul": p = float(a["noul"]) return {"false": 1.0 - p, "true": p} return a["probabilities"] def remix(row, w): pp = row["part_probabilities"] qs = {q: ({"type": "noul"} if set(d) == set(C.NOUL_KEYS) else {"type": "choice", "criteria": list(d)}) for q, d in row["answers"].items()} # score fields mix like choice (same keys, same fsum); answer kept as dist return {q: kit_dist(a) for q, a in C.mix(qs, pp, w).items()} def top(d): return max(sorted(d), key=d.get) def main(run, cfg_run, cfg_new, out): w0, w1 = weights(cfg_run), weights(cfg_new) rows = [json.loads(l) for l in open(run)] ok = [r for r in rows if r.get("status") == "ok" and "part_probabilities" in r] bad = [r["id"] for r in ok if remix(r, w0) != r["answers"]] if bad: sys.exit(f"self-check failed on {len(bad)} rows, e.g. {bad[:5]}") us, changed, fields = [], 0, 0 with open(out, "w") as f: for r in ok: t = time.perf_counter() a = remix(r, w1) us.append(1e6 * (time.perf_counter() - t)) for q in a: fields += 1 changed += top(a[q]) != top(r["answers"][q]) f.write(json.dumps({"id": r["id"], "ms": r["ms"], "status": "ok", "answers": a}) + "\n") print(json.dumps({"rows": len(rows), "remixed": len(ok), "self_check_bit_identical": len(ok), "fields": fields, "top_option_changes_vs_run": changed, "weights_run": w0, "weights_new": w1, "remix_us_median": statistics.median(us), "remix_us_p99": sorted(us)[int(0.99 * len(us))], "out_sha256": hashlib.sha256(Path(out).read_bytes()).hexdigest()}, indent=1)) if __name__ == "__main__": main(*sys.argv[1:5])