File size: 2,568 Bytes
23c39b5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""Re-score saved BBH generations with the current parser — no GPU needed.

Because eval_bench_multi.py dumps every prompt/generation, a parser fix can be
applied to finished runs without re-running the models:

    python slurm/rescore_bbh.py [--write]

`--write` updates each run's benchmark_results.json in place.
"""
import argparse
import json
import os
import sys
from collections import defaultdict

sys.path.insert(0, os.path.join(os.path.dirname(__file__), ".."))
from evaluation.benchmarks import BBH  # noqa: E402
from utils.functions import compute_benchmark_micro_macro_avg  # noqa: E402

BASE = "/lustre/fsw/portfolios/nemotron/users/hengyuf/HRM-Text/checkpoints/HRM-Text"
RUNS = [("HRM", "L-4n-repro", 4), ("loop_baseline", "ut-L-4n", 4),
        ("transformer", "transformer-L-4n", 4), ("loop_cot_v2_ep3", "loop-cot-v2-L-4n", 3), ("loop_cot_v2_ep4", "loop-cot-v2-L-4n", 4)]


def rescore(path):
    stats = defaultdict(lambda: {"n": 0, "correct": 0.0, "invalid": 0})
    changed = 0
    with open(path) as f:
        for line in f:
            r = json.loads(line)
            answer, found = BBH._extract_answer(r["generation"])
            accept = set(r["ground_truth"]["accept"])
            ok = bool(BBH._candidates(answer) & accept)
            st = stats[r["ground_truth"]["task"]]
            st["n"] += 1
            st["invalid"] += 0 if found else 1
            st["correct"] += 1 if ok else 0
            changed += int(ok != bool(r.get("correct")))
    return compute_benchmark_micro_macro_avg(stats), changed


def main():
    ap = argparse.ArgumentParser()
    ap.add_argument("--write", action="store_true")
    args = ap.parse_args()

    print(f'{"run":15s}{"old acc":>10s}{"new acc":>10s}{"delta":>8s}{"flipped":>9s}{"invalid":>9s}')
    for label, run, epoch in RUNS:
        gen = f"{BASE}/{run}/generations/BBH_ep{epoch}.jsonl"
        res_path = f"{BASE}/{run}/benchmark_results.json"
        if not os.path.exists(gen):
            print(f"{label:15s}  (no generations yet)")
            continue
        res, changed = rescore(gen)
        results = json.load(open(res_path))
        old = results.get(str(epoch), {}).get("BBH", {}).get("acc", float("nan"))
        print(f"{label:15s}{old*100:9.1f}%{res['acc']*100:9.1f}%{(res['acc']-old)*100:+8.1f}"
              f"{changed:9d}{res['invalid']*100:8.1f}%")
        if args.write:
            results.setdefault(str(epoch), {})["BBH"] = res
            with open(res_path, "wt") as f:
                json.dump(results, f, indent=2)


if __name__ == "__main__":
    main()