Upload jevbench_dump.py with huggingface_hub
Browse files- jevbench_dump.py +138 -0
jevbench_dump.py
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Run Alea on JevBench public datasets -> per-tier accuracy/ECE/Brier/latency.
|
| 2 |
+
|
| 3 |
+
Maps JevBench tasks to Alea packing:
|
| 4 |
+
choice -> choice q, options=labels, descs=criteria[label]
|
| 5 |
+
noul -> noul q, probs {yes,no}
|
| 6 |
+
score -> choice q over level labels (native dist over exact label set)
|
| 7 |
+
"""
|
| 8 |
+
import json, sys, time
|
| 9 |
+
sys.path.insert(0, "/root/alea")
|
| 10 |
+
sys.path.insert(0, "/root/jevbench")
|
| 11 |
+
import numpy as np
|
| 12 |
+
import torch
|
| 13 |
+
import torch.nn.functional as F
|
| 14 |
+
|
| 15 |
+
from alea.model import AleaModel, collate_segments, mask_from_segments
|
| 16 |
+
from alea.packing import pack_request, QuestionSpec
|
| 17 |
+
from alea.train import load_into
|
| 18 |
+
from jevbench.tasks import load_jsonl
|
| 19 |
+
from jevbench.scoring import score_task
|
| 20 |
+
from jevbench.metrics import brier_score, ece_top_label, latency_summary, macro_accuracy
|
| 21 |
+
|
| 22 |
+
CKPT = sys.argv[1] if len(sys.argv) > 1 else "/root/alea/checkpoints/alea_env_rl.pt"
|
| 23 |
+
MAXLEN = 4096
|
| 24 |
+
dev = torch.device("cuda")
|
| 25 |
+
|
| 26 |
+
from transformers import AutoTokenizer
|
| 27 |
+
tok = AutoTokenizer.from_pretrained("Qwen/Qwen3-0.6B")
|
| 28 |
+
model = AleaModel("Qwen/Qwen3-0.6B", attn_impl="sdpa",
|
| 29 |
+
use_aux_embed=True, use_ngram=True, use_ngram2=True, use_ngram3=True).to(dev)
|
| 30 |
+
load_into(model, CKPT)
|
| 31 |
+
model.eval()
|
| 32 |
+
pad = tok.pad_token_id or tok.eos_token_id
|
| 33 |
+
|
| 34 |
+
|
| 35 |
+
def task_to_spec(t):
|
| 36 |
+
qt = t.question["type"]
|
| 37 |
+
instr = t.question["instructions"]
|
| 38 |
+
crit = t.question.get("criteria")
|
| 39 |
+
keys = [str(l) for l in t.labels]
|
| 40 |
+
if qt == "noul":
|
| 41 |
+
return QuestionSpec(qid="decision", qtype="noul",
|
| 42 |
+
instructions=instr, options=[], option_keys=["no", "yes"])
|
| 43 |
+
if isinstance(crit, dict):
|
| 44 |
+
opts = [str(crit.get(l, l)) for l in t.labels]
|
| 45 |
+
elif isinstance(crit, list):
|
| 46 |
+
opts = [str(x) for x in crit]
|
| 47 |
+
else:
|
| 48 |
+
opts = keys[:]
|
| 49 |
+
return QuestionSpec(qid="decision", qtype="choice", instructions=instr,
|
| 50 |
+
options=opts, option_keys=keys)
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
@torch.no_grad()
|
| 54 |
+
def decide(t):
|
| 55 |
+
spec = task_to_spec(t)
|
| 56 |
+
st = t.state if isinstance(t.state, str) else json.dumps(t.state, indent=1)
|
| 57 |
+
pk = pack_request(tok, st, [spec], max_len=MAXLEN)
|
| 58 |
+
ids, pos, seg, qof, oof = collate_segments([pk], pad)
|
| 59 |
+
mask = mask_from_segments(seg.to(dev), qof.to(dev), oof.to(dev)).to(torch.bfloat16)
|
| 60 |
+
out = model(ids.to(dev), mask, pos.to(dev), [pk])
|
| 61 |
+
r = out[0][0]
|
| 62 |
+
if spec.qtype == "noul":
|
| 63 |
+
p = float(r["p_yes"].item())
|
| 64 |
+
return {"yes": p, "no": 1.0 - p}
|
| 65 |
+
probs = F.softmax(r["logits"][: len(t.labels)].float(), -1).tolist()
|
| 66 |
+
return {str(l): float(pr) for l, pr in zip(t.labels, probs)}
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
all_recs = []
|
| 70 |
+
for name in ["easy", "hard", "original"]:
|
| 71 |
+
tasks = load_jsonl(f"/root/jevbench/datasets/public/{name}.jsonl")
|
| 72 |
+
recs = []
|
| 73 |
+
for i, t in enumerate(tasks, 1):
|
| 74 |
+
t0 = time.perf_counter()
|
| 75 |
+
try:
|
| 76 |
+
probs = decide(t)
|
| 77 |
+
lat = time.perf_counter() - t0
|
| 78 |
+
s = score_task(probs, t)
|
| 79 |
+
recs.append({"task_id": t.id, "family": t.family, "split": name,
|
| 80 |
+
"group": t.group, "ok": True, "probs": probs,
|
| 81 |
+
"state": t.state[:3000],
|
| 82 |
+
"instructions": t.question.get("instructions",""),
|
| 83 |
+
"criteria": t.question.get("criteria"),
|
| 84 |
+
"valid": s["valid"], "correct": s["correct"],
|
| 85 |
+
"predicted": s.get("predicted"),
|
| 86 |
+
"ordinal_ev": s.get("ordinal_ev"),
|
| 87 |
+
"latency_s": lat,
|
| 88 |
+
"expected": t.expected})
|
| 89 |
+
except Exception as e:
|
| 90 |
+
recs.append({"task_id": t.id, "family": t.family, "split": name,
|
| 91 |
+
"ok": False, "error": f"{type(e).__name__}: {str(e)[:120]}",
|
| 92 |
+
"correct": False, "valid": False, "latency_s": time.perf_counter() - t0})
|
| 93 |
+
if i % 25 == 0:
|
| 94 |
+
print("%s: %d/%d" % (name, i, len(tasks)), flush=True)
|
| 95 |
+
all_recs += recs
|
| 96 |
+
scorable = [r for r in recs if r["ok"] and r.get("expected") is not None]
|
| 97 |
+
n = len(scorable)
|
| 98 |
+
acc = sum(r["correct"] for r in scorable) / max(n, 1)
|
| 99 |
+
pairs = [(r["probs"], str(r["expected"]), [str(x) for x in
|
| 100 |
+
[t2.labels for t2 in tasks if t2.id == r["task_id"]][0]])
|
| 101 |
+
for r in scorable]
|
| 102 |
+
briers, ece_pairs = [], []
|
| 103 |
+
for r in scorable:
|
| 104 |
+
t2 = next(x for x in tasks if x.id == r["task_id"])
|
| 105 |
+
exp = str(t2.expected)
|
| 106 |
+
labs = [str(x) for x in t2.labels]
|
| 107 |
+
try:
|
| 108 |
+
briers.append(brier_score(r["probs"], exp, labs))
|
| 109 |
+
except Exception:
|
| 110 |
+
pass
|
| 111 |
+
conf = max(r["probs"].values()); pred = max(r["probs"], key=r["probs"].get)
|
| 112 |
+
ece_pairs.append((conf, float(pred == exp)))
|
| 113 |
+
ece = ece_top_label(ece_pairs)["ece"]
|
| 114 |
+
lat = latency_summary([r["latency_s"] for r in recs])
|
| 115 |
+
# chance-corrected intelligence (jevbench: (acc - chance)/(1 - chance))
|
| 116 |
+
chance = np.mean([1.0 / len(next(x for x in tasks if x.id == r["task_id"]).labels)
|
| 117 |
+
for r in scorable])
|
| 118 |
+
cci = max(0.0, (acc - chance) / (1 - chance)) * 100 if n else 0.0
|
| 119 |
+
print()
|
| 120 |
+
print("== %s: n=%d acc=%.3f cci=%.1f brier=%.4f ece=%.4f p50=%.3fs p95=%.3fs"
|
| 121 |
+
% (name, n, acc, cci, np.mean(briers), ece,
|
| 122 |
+
lat["p50_s"], lat["p95_s"]))
|
| 123 |
+
# per-family
|
| 124 |
+
fams = {}
|
| 125 |
+
for r in scorable:
|
| 126 |
+
fams.setdefault(r["family"], []).append(r["correct"])
|
| 127 |
+
for f_, v in sorted(fams.items()):
|
| 128 |
+
print(" " + f_.ljust(22), "%d/%d = %.3f" % (sum(v), len(v), np.mean(v)))
|
| 129 |
+
|
| 130 |
+
out = "/root/alea/jevbench_results.json"
|
| 131 |
+
json.dump(all_recs, open(out, "w"), indent=1)
|
| 132 |
+
print("wrote", out)
|
| 133 |
+
|
| 134 |
+
import json as _j
|
| 135 |
+
with open("/root/alea/jevbench_dump.jsonl","w") as fh:
|
| 136 |
+
for r in all_recs:
|
| 137 |
+
fh.write(_j.dumps(r)+chr(10))
|
| 138 |
+
print("dumped", len(all_recs), "records")
|