import argparse import json import torch from transformers import AutoTokenizer from alea.eval import load_model, predict_all def convert(row, tier): question = row["question"] qtype = question["type"] expected = row["expected"] labels = [str(label) for label in row["labels"]] criteria = question.get("criteria") or question.get("levels") or {} if isinstance(criteria, list): values = criteria elif isinstance(criteria, dict): values = list(criteria.values()) else: values = labels if qtype == "noul": qcriteria = criteria if isinstance(criteria, dict) else {} target = float(str(expected).lower() in ("yes", "true", "1")) spec = {"type": qtype, "instructions": question["instructions"], "criteria": qcriteria} targets = {"q": target} elif qtype == "score": qlevels = dict(zip(labels, map(str, values))) gold = str(expected) if gold not in qlevels: raise ValueError(f"{row['id']}: score gold {gold!r} not in {labels}") spec = {"type": qtype, "instructions": question["instructions"], "levels": qlevels} targets = {"q": [float(label == gold) for label in qlevels]} elif qtype == "choice": if isinstance(criteria, dict): qcriteria = {str(key): str(value) for key, value in criteria.items()} else: qcriteria = dict(zip(labels, map(str, values))) gold = str(expected) if gold not in qcriteria: raise ValueError(f"{row['id']}: choice gold {gold!r} not in " f"{list(qcriteria)}") spec = {"type": qtype, "instructions": question["instructions"], "criteria": qcriteria} targets = {"q": [float(label == gold) for label in qcriteria]} else: raise ValueError(f"{row['id']}: unsupported type {qtype!r}") return {"id": f"jevpub:{tier}:{row['id']}", "family": row["family"], "state": row["state"], "questions": {"q": spec}, "targets": targets} def main(): ap = argparse.ArgumentParser() ap.add_argument("--ckpt", required=True) ap.add_argument("--bench-root", default="/root/jevbench") ap.add_argument("--model", default="Qwen/Qwen3-0.6B") ap.add_argument("--max-len", type=int, default=4096) ap.add_argument("--tiers", nargs="+", default=["easy", "hard", "original"]) args = ap.parse_args() device = torch.device("cuda" if torch.cuda.is_available() else "cpu") tokenizer = AutoTokenizer.from_pretrained(args.model) model = load_model(args.ckpt, args.model, device) result = {"checkpoint": args.ckpt, "max_len": args.max_len, "tiers": {}} for tier in args.tiers: path = f"{args.bench_root}/datasets/public/{tier}.jsonl" raw_rows = [json.loads(line) for line in open(path, encoding="utf-8")] rows = [convert(row, tier) for row in raw_rows] preds = predict_all(model, tokenizer, rows, device, max_len=args.max_len) n = correct = 0 by_family = {} by_type = {} confidence = [] for row in rows: qid = "q" spec = row["questions"][qid] target = row["targets"][qid] pred = preds[row["id"]][qid] if spec["type"] in ("choice", "score"): gold_i = max(range(len(target)), key=lambda i: target[i]) pred_i = max(range(len(pred["probs"])), key=lambda i: pred["probs"][i]) conf = max(pred["probs"]) is_correct = int(gold_i == pred_i) else: p_yes = pred["p"] conf = max(p_yes, 1 - p_yes) is_correct = int((p_yes >= 0.5) == (target >= 0.5)) n += 1 correct += is_correct family = row["family"] by_family.setdefault(family, [0, 0]) by_family[family][0] += is_correct by_family[family][1] += 1 by_type.setdefault(spec["type"], [0, 0]) by_type[spec["type"]][0] += is_correct by_type[spec["type"]][1] += 1 confidence.append((conf, is_correct)) bins = [[] for _ in range(10)] for conf, is_correct in confidence: bins[min(9, int(conf * 10))].append((conf, is_correct)) ece = sum(len(bucket) / n * abs( sum(c for c, _ in bucket) / len(bucket) - sum(ok for _, ok in bucket) / len(bucket)) for bucket in bins if bucket) result["tiers"][tier] = { "n": n, "correct": correct, "accuracy": correct / n, "ece10": ece, "by_family": {key: round(ok / count, 4) for key, (ok, count) in sorted(by_family.items())}, "by_type": {key: {"n": count, "accuracy": ok / count} for key, (ok, count) in sorted(by_type.items())}, } print(json.dumps(result, indent=2, sort_keys=True)) if __name__ == "__main__": main()