REAX-1.4 / engine /bench /natives_gates.py
decisionmodels's picture
Engine tree (REAX Engine reax14-release-20261126, 2cccfee) and run instructions
a7e82fd verified
Raw History Blame Contribute Delete
5.68 kB
#!/usr/bin/env python3
"""(Copied from the tmpl-final worker scripts/gates.py for the round-2 native setups; unchanged logic.)
Gate table for one engine arm against the HF reference (adopted gates, lead decision 13 Nov 2026, board #59765/#59767/#59783).
python3 gates.py --repo REAX_ENGINE --data P300_DIR --ref REF.jsonl --ref2 REF_SELFNOISE.jsonl --cand NAME=FILE [--cand ...]
Gates per arm (P300, n = answered by both):
G-agree top-1 agreement(ref, cand) >= agreement(ref, ref2) - 1 pp (ref2 = second HF configuration, same run)
G-flip confident flips(cand) <= confident flips(ref2)
confident flip = ref and ref2 pick the same top-1 with p >= 0.9 (both), and the arm picks another top-1
G-acc accuracy delta (cand - ref), paired bootstrap 95 % CI (bench.stats.paired_bootstrap, 1000 reps, seed 16): lower >= -2 pp
max |dp| is reported, not a gate.
"""
import argparse
import json
import sys
def load(path):
out = {}
for line in open(path):
if line.strip():
r = json.loads(line)
if "key" not in r: # qwen38 harness format {id, keys, probs} / {id, unsupported}
r = ({"key": r["id"], "http": 422, "answer": None} if "unsupported" in r else
{"key": r["id"], "http": 200, "answer": {"probabilities": dict(zip(r["keys"], r["probs"]))}})
out[r["key"]] = r
return out
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--repo", required=True)
ap.add_argument("--data", required=True)
ap.add_argument("--set", default="p300")
ap.add_argument("--ref", required=True)
ap.add_argument("--ref2", required=True)
ap.add_argument("--cand", action="append", default=[])
ap.add_argument("--p", type=float, default=0.9)
ap.add_argument("--json")
a = ap.parse_args()
sys.path.insert(0, a.repo)
from bench import sets, stats
from bench.agreement import evaluate
items = sets.load(a.set, a.data)
ref, ref2 = load(a.ref), load(a.ref2)
def probs(rec, it):
if not rec or rec["http"] != 200:
return None
return stats.probs_dict(rec["answer"], sets.option_keys(it.question))
def flips(cand):
out = []
for it in items:
pa, pb, pc = probs(ref.get(it.key), it), probs(ref2.get(it.key), it), probs(cand.get(it.key), it)
if pa is None or pb is None or pc is None:
continue
ta, tb, tc = stats.argmax(pa), stats.argmax(pb), stats.argmax(pc)
if ta == tb and pa[ta] >= a.p and pb[tb] >= a.p and tc != ta:
out.append({"key": it.key, "ref_top": ta, "ref_p": round(pa[ta], 4), "ref2_p": round(pb[tb], 4),
"arm_top": tc, "arm_p": round(pc[tc], 4), "gold": it.gold})
return out
noise = evaluate(items, ref, ref2)
noise_agree = noise["paired"]["top_choice_agreement"]
noise_flips = flips(ref2) # ref2 vs (ref, ref2) agreement: 0 by construction; self-noise count = flips of ref2 vs ref alone
# self-noise confident flips: ref is confident (p >= .9) and ref2 picks another top-1 (and vice versa)
sn = 0
for it in items:
pa, pb = probs(ref.get(it.key), it), probs(ref2.get(it.key), it)
if pa is None or pb is None:
continue
ta, tb = stats.argmax(pa), stats.argmax(pb)
if ta != tb and (pa[ta] >= a.p or pb[tb] >= a.p):
sn += 1
rows = [{"arm": "self-noise (ref2 vs ref)", "n": noise["paired"]["n"], "agreement": noise_agree,
"max_abs_dp": noise["paired"]["max_abs_dp"], "confident_flips": sn,
"acc_delta": noise.get("gold", {}).get("accuracy_delta")}]
del noise_flips
for spec in a.cand:
name, path = spec.split("=", 1)
cand = load(path)
ev = evaluate(items, ref, cand)
fl = flips(cand)
d = ev.get("gold", {}).get("accuracy_delta") or {}
g_agree = ev["paired"]["top_choice_agreement"] >= noise_agree - 0.01
g_flip = len(fl) <= sn
g_acc = bool(d) and d["ci95"][0] >= -2.0
rows.append({"arm": name, "n": ev["paired"]["n"], "agreement": ev["paired"]["top_choice_agreement"],
"max_abs_dp": ev["paired"]["max_abs_dp"], "confident_flips": len(fl), "flips": fl, "acc_delta": d,
"ref_acc": ev.get("gold", {}).get("ref_accuracy"), "cand_acc": ev.get("gold", {}).get("cand_accuracy"),
"G_agree": g_agree, "G_flip": g_flip, "G_acc": g_acc, "PASS": g_agree and g_flip and g_acc})
print(f"bar: agreement >= {noise_agree * 100:.2f} - 1 = {(noise_agree - 0.01) * 100:.2f} %; confident flips <= {sn}; acc lower CI >= -2.00 pp")
print("| arm | n | agreement | max abs dp | confident flips | acc delta pp [95% CI] | G-agree | G-flip | G-acc | verdict |")
print("|---|---|---|---|---|---|---|---|---|---|")
for r in rows:
d = r["acc_delta"] or {}
acc = (f"{d['delta_pp']:+.2f} [{d['ci95'][0]:+.2f}, {d['ci95'][1]:+.2f}]" if d else "-")
flag = lambda k: ("pass" if r[k] else "FAIL") if k in r else "-" # noqa: E731
print(f"| {r['arm']} | {r['n']} | {r['agreement'] * 100:.2f} % | {r['max_abs_dp']:.3f} | {r['confident_flips']} | {acc} | "
f"{flag('G_agree')} | {flag('G_flip')} | {flag('G_acc')} | {('PASS' if r['PASS'] else 'FAIL') if 'PASS' in r else 'bar'} |")
for r in rows:
for f in r.get("flips", []):
print(f" flip {r['arm']}: {f}")
if a.json:
json.dump({"self_noise_agreement": noise_agree, "self_noise_flips": sn, "rows": rows}, open(a.json, "w"), indent=1)
if __name__ == "__main__":
main()