File size: 5,659 Bytes
d847043
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
"""把评测明细汇总成表:每个模型在每个测试集上的严格/宽松准确率,第二轮比底座、比第一轮涨的是不是噪声。

    python scripts/summarize.py results/run2     # 读 <模型>_<测试集>.json、after_*_novalues.json、checkpoint-*_<测试集>.json

模型(文件名前缀):base 底座、r1 第一轮微调、after 这一轮微调、coder 现成的 Qwen2.5-Coder-7B(不微调,对照)。
哪个文件没有就不列,所以第一轮的 results/run(只有 base、after)也能用。
别的前缀用 --models / --pairs 指定,比如推理时补救的对比:

    python scripts/summarize.py results/infer --models plain:直接生成,repair:出错重写,vote:投票加重写 --pairs repair:plain,vote:plain

显著性用配对的精确 McNemar 检验:同一道题,A 错 B 对算 +1,反过来算 -1,
只看这些「变了的题」里两个方向是不是明显不对称。p < 0.05 才算真涨(或真跌)。
40 题的测试集要净多对 8-10 题左右才过线,100 题的难题集大约 10 题。
"""

import argparse
import json
from math import comb
from pathlib import Path

SETS = ("library", "gym", "hard", "transit")
MODELS = (("base", "底座"), ("r1", "第一轮"), ("after", "这一轮"), ("coder", "Coder-7B"))
PAIRS = (("after", "base"), ("after", "r1"), ("after", "coder"))


def mcnemar_p(gain: int, loss: int) -> float:
    n = gain + loss
    if n == 0:
        return 1.0
    k = min(gain, loss)
    return min(1.0, 2 * sum(comb(n, i) for i in range(k + 1)) / 2 ** n)


def load(path: Path) -> list[dict] | None:
    return json.loads(path.read_text(encoding="utf-8")) if path.exists() else None


def rate(recs: list[dict], key: str) -> str:
    return f"{sum(r[key] for r in recs) / len(recs):.1%}"


def main() -> None:
    ap = argparse.ArgumentParser()
    ap.add_argument("dir", nargs="?", default="results/run2")
    ap.add_argument("--models", default="", help="前缀:名字,前缀:名字……,不给就用 base/r1/after/coder")
    ap.add_argument("--pairs", default="", help="前缀:前缀,……,要做显著性检验的两两对比")
    args = ap.parse_args()
    d = Path(args.dir)
    all_models = tuple(tuple(x.split(":", 1)) for x in args.models.split(",")) if args.models else MODELS
    all_pairs = tuple(tuple(x.split(":", 1)) for x in args.pairs.split(",")) if args.pairs else PAIRS
    res = {(m, t): load(d / f"{m}_{t}.json") for m, _ in all_models for t in SETS}
    models = [(m, name) for m, name in all_models if any(res[(m, t)] for t in SETS)]
    sets = [t for t in SETS if any(res[(m, t)] for m, _ in models)]
    for t in sets:
        qs = {m: [r["question"] for r in res[(m, t)]] for m, _ in models if res[(m, t)]}
        assert len({tuple(v) for v in qs.values()}) == 1, f"{t}: 几个模型评的题不一样"

    has_nv = any((d / f"after_{t}_novalues.json").exists() for t in sets)
    lines = ["准确率(严格 / 宽松;宽松 = 多返回几列也算对):", "",
             "| 测试集 | 题数 | " + " | ".join(name for _, name in models) + (" | 这一轮不给取值(宽松) |" if has_nv else " |"),
             "| --- | --- | " + " | ".join("---" for _ in models) + (" | --- |" if has_nv else " |")]
    for t in sets:
        n = len(next(res[(m, t)] for m, _ in models if res[(m, t)]))
        cells = [f"{rate(res[(m, t)], 'hit')} / {rate(res[(m, t)], 'hit_loose')}" if res[(m, t)] else "—"
                 for m, _ in models]
        nv = load(d / f"after_{t}_novalues.json")
        lines.append(f"| {t} | {n} | " + " | ".join(cells)
                     + (f" | {rate(nv, 'hit_loose') if nv else '—'} |" if has_nv else " |"))

    pairs = [(a, b) for a, b in all_pairs if a in dict(models) and b in dict(models)]
    if pairs:
        lines += ["", "差距是不是噪声(宽松口径,配对 McNemar;多对 = 前者对、后者错的题数):", "",
                  "| 测试集 | " + " | ".join(f"{dict(models)[a]} vs {dict(models)[b]}" for a, b in pairs) + " |",
                  "| --- | " + " | ".join("---" for _ in pairs) + " |"]
        for t in sets:
            cells = []
            for a, b in pairs:
                ra, rb = res[(a, t)], res[(b, t)]
                if not ra or not rb:
                    cells.append("—")
                    continue
                gain = sum(x["hit_loose"] and not y["hit_loose"] for x, y in zip(ra, rb))
                loss = sum(y["hit_loose"] and not x["hit_loose"] for x, y in zip(ra, rb))
                p = mcnemar_p(gain, loss)
                cells.append(f"+{gain} / -{loss},p={p:.3f}{' 显著' if p < 0.05 else ''}")
            lines.append(f"| {t} | " + " | ".join(cells) + " |")

    ck_sets = [t for t in ("hard", "transit") if list(d.glob(f"checkpoint-*_{t}.json"))]
    if ck_sets:
        steps = sorted({int(p.stem.split("-")[1].split("_")[0]) for t in ck_sets for p in d.glob(f"checkpoint-*_{t}.json")})
        lines += ["", "每个 epoch 的 checkpoint(宽松准确率;挑最高的那个发布):", "",
                  "| checkpoint | " + " | ".join(ck_sets) + " |", "| --- | " + " | ".join("---" for _ in ck_sets) + " |"]
        for s in steps:
            cells = []
            for t in ck_sets:
                recs = load(d / f"checkpoint-{s}_{t}.json")
                cells.append(rate(recs, "hit_loose") if recs else "—")
            lines.append(f"| checkpoint-{s} | " + " | ".join(cells) + " |")

    text = "\n".join(lines) + "\n"
    print(text)
    (d / "summary.md").write_text(text, encoding="utf-8")
    print("写到", d / "summary.md")


if __name__ == "__main__":
    main()