#!/usr/bin/env python3 """Compare two det_client.py runs prompt by prompt. det_compare.py REF.jsonl OTHER.jsonl [--gold prompts.jsonl] Reports: identical completions, diverged ones, the token index of the first divergence (median, min, max), and with --gold, how many final answers (GSM8K: last number) changed and how many correct answers flipped. Prints one JSON object. """ import argparse, json, re, statistics def load(p): lines = [json.loads(l) for l in open(p)] return lines[0]["meta"] | {"errors": lines[0]["errors"], "wall": lines[0]["wall"]}, {r["id"]: r for r in lines[1:]} def last_number(t): nums = re.findall(r"-?\d[\d,]*\.?\d*", t or "") return nums[-1].replace(",", "").rstrip(".") if nums else None def first_div(a, b): for i, (x, y) in enumerate(zip(a, b)): if x != y: return i return None if len(a) == len(b) else min(len(a), len(b)) def main(): ap = argparse.ArgumentParser() ap.add_argument("ref") ap.add_argument("other") ap.add_argument("--gold") a = ap.parse_args() _, R = load(a.ref) meta, O = load(a.other) gold = {} if a.gold: for l in open(a.gold): d = json.loads(l) gold[d["id"]] = d.get("answer") same, div, idx, ans_changed, flips_bad, flips_good, n = 0, 0, [], 0, 0, 0, 0 for k, r in R.items(): o = O.get(k) if not o or "error" in r or "error" in o: continue n += 1 if r["text"] == o["text"]: same += 1 continue div += 1 i = first_div(r.get("tokens") or [], o.get("tokens") or []) if i is not None: idx.append(i) ra, oa = last_number(r["text"]), last_number(o["text"]) if ra != oa: ans_changed += 1 if k in gold: g = str(gold[k]) if ra == g and oa != g: flips_bad += 1 if ra != g and oa == g: flips_good += 1 out = {"ref": a.ref, "other": a.other, "n": n, "identical": same, "diverged": div, "diverged_pct": round(100 * div / n, 1) if n else None, "first_div_median": statistics.median(idx) if idx else None, "first_div_min": min(idx) if idx else None, "first_div_max": max(idx) if idx else None, "answer_changed": ans_changed, "correct_to_wrong": flips_bad, "wrong_to_correct": flips_good} if gold: acc = lambda D: sum(1 for k, r in D.items() if "error" not in r and last_number(r["text"]) == str(gold.get(k))) out["acc_ref"], out["acc_other"] = acc(R), acc(O) print(json.dumps(out)) if __name__ == "__main__": main()