patrickbdevaney's picture
GLM-5.3-Flash prune vs quantize at matched bytes: findings, data, scripts, v4 saliency and masks
41a0653 verified
Raw History Blame Contribute Delete
5.31 kB
#!/usr/bin/env python3
"""Recompute paired generation statistics from glm_eval-style result folders.
Each arm is given as NAME=DIR_HIGH[,DIR_MAX]. A DIR holds <suite>.jsonl (one header line, then one
line per item with content/reasoning/usage/truncated) and <suite>.graded.json ({"items": {id:
{"pass", "truncated", ...}}}). Items are paired by id across arms; the first 100 ids of each
suite's reference arm define the paired block unless --all is given.
Outputs JSON (per arm x suite: n, pass, truncated, pass-when-terminated, mean completion tokens,
30-gram loop count) and paired tables (discordant counts, exact two-sided McNemar p).
"""
import argparse, json, math, os, sys
from collections import Counter
SUITES = {"humanevalplus": "high", "ifbench": "high", "mmlupro": "high", "gpqa": "high", "aime26": "max"}
def load_arm(spec):
name, dirs = spec.split("=", 1)
dirs = dirs.split(",")
out = {}
for suite, effort in SUITES.items():
for d in dirs:
g = os.path.join(d, f"{suite}.graded.json")
j = os.path.join(d, f"{suite}.jsonl")
if os.path.exists(g) and os.path.exists(j):
graded = json.load(open(g))["items"]
header, items = None, {}
for line in open(j):
o = json.loads(line)
if o.get("type") == "header":
header = o
elif o.get("type") == "item":
items[o["id"]] = o
out[suite] = {"graded": graded, "items": items, "header": header, "order": list(items)}
break
return name, out
def loop_count(text, n=30, reps=20, stride=1):
"""True if any whitespace-token n-gram (taken at the given stride) occurs >= reps times."""
w = text.split()
if len(w) < n * reps:
return False
c = Counter(tuple(w[i:i + n]) for i in range(0, len(w) - n + 1, stride))
return max(c.values()) >= reps
def mcnemar_exact(b, c):
n = b + c
if n == 0:
return 1.0
k = min(b, c)
p = 2 * sum(math.comb(n, i) for i in range(0, k + 1)) / 2 ** n
return min(1.0, p)
def wilson(k, n, z=1.96):
if n == 0:
return [None, None]
p = k / n
d = 1 + z * z / n
c = (p + z * z / (2 * n)) / d
h = z * math.sqrt(p * (1 - p) / n + z * z / (4 * n * n)) / d
return [round(c - h, 4), round(c + h, 4)]
def main():
ap = argparse.ArgumentParser()
ap.add_argument("--arm", action="append", required=True, help="NAME=DIR_HIGH[,DIR_MAX]")
ap.add_argument("--reference", required=True, help="arm whose first --block ids define the paired block")
ap.add_argument("--block", type=int, default=100)
ap.add_argument("--stride", type=int, default=1)
ap.add_argument("--out", required=True)
a = ap.parse_args()
arms = dict(load_arm(s) for s in a.arm)
res = {"settings": {"loop_rule": f"any 30-gram of whitespace tokens (stride {a.stride}) repeated >= 20 times in reasoning+content",
"block": a.block, "reference": a.reference}, "suites": {}}
for suite in SUITES:
ref = arms[a.reference].get(suite)
if not ref:
continue
ids = ref["order"][: a.block] if suite in ("ifbench", "mmlupro", "gpqa") else ref["order"]
present = [i for i in ids if all(suite in arms[n] and i in arms[n][suite]["graded"] for n in arms)]
S = {"paired_ids": len(present), "headers": {}, "arms": {}, "pairs": {}}
per = {}
for n, arm in arms.items():
s = arm[suite]
S["headers"][n] = {k: s["header"].get(k) for k in ("model", "gen", "item_ids_sha16", "version")}
P = [bool(s["graded"][i]["pass"]) for i in present]
T = [bool(s["graded"][i]["truncated"]) for i in present]
toks = [((s["items"][i].get("usage") or {}).get("completion_tokens") or 0) for i in present]
loops = [loop_count((s["items"][i].get("reasoning") or "") + " " + (s["items"][i].get("content") or ""), stride=a.stride) for i in present]
term = [i for i, t in enumerate(T) if not t]
per[n] = P
S["arms"][n] = {"n": len(present), "pass": sum(P), "acc": round(sum(P) / len(P), 4) if P else None,
"ci95": wilson(sum(P), len(P)), "truncated": sum(T),
"pass_when_terminated": [sum(P[i] for i in term), len(term)],
"mean_completion_tokens": round(sum(toks) / len(toks), 1) if toks else None,
"loops_30gram_ge20": sum(loops)}
names = list(arms)
for x in range(len(names)):
for y in range(x + 1, len(names)):
A, B = per[names[x]], per[names[y]]
b = sum(1 for p, q in zip(A, B) if p and not q)
c = sum(1 for p, q in zip(A, B) if q and not p)
S["pairs"][f"{names[x]}|{names[y]}"] = {"only_first": b, "only_second": c, "mcnemar_exact_p": mcnemar_exact(b, c)}
res["suites"][suite] = S
json.dump(res, open(a.out, "w"), indent=1)
print(json.dumps({s: {n: (v["pass"], v["n"], v["truncated"], v["loops_30gram_ge20"]) for n, v in S["arms"].items()} for s, S in res["suites"].items()}, indent=0))
if __name__ == "__main__":
main()