File size: 4,490 Bytes
e90df0f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#!/usr/bin/env python3
"""Validate retained OLMES aggregate and leaf metrics for one stage."""

from __future__ import annotations

import argparse
import json
import math
from pathlib import Path


EXPECTED = {
    "base_no_code": 95,
    "base_easy": 192,
    "bigcodebench": 1,
    "code_rest": 15,
    "ds1000": 1,
}

REQUIRED = {
    "base_no_code": (
        "olmo3:base:math",
        "arc_easy:mc::xlarge",
        "arc_challenge:mc::xlarge",
        "mmlu_stem:mc::olmes",
        "medmcqa:mc::none",
        "medqa_en:mc::none",
        "sciq:mc::xlarge",
        "olmo3:base:nonstem_qa_mc",
        "olmo3:base:gen",
    ),
    "base_easy": (
        "olmo3:base_easy:qa_bpb",
        "olmo3:base_easy:math_bpb",
        "olmo3:base_easy:code_bpb",
    ),
    "bigcodebench": ("bigcodebench:3shot::olmo3:v2",),
    "code_rest": (
        "codex_humaneval:3shot::olmo3:n32:v2",
        "deepseek_leetcode::olmo3:n32:v2",
        "mbpp:3shot::olmo3:n32:v2",
    ),
    "ds1000": ("ds1000:3shot::olmo3:v2",),
}

FALLBACK_GROUPS = {
    "arc:rc:bpb::olmes:full": (
        "arc_easy:rc:bpb::olmes:full",
        "arc_challenge:rc:bpb::olmes:full",
    ),
    "olmo3:base_easy:qa_bpb": (
        "arc:rc:bpb::olmes:full",
        "mmlu:rc:bpb::olmes",
        "csqa:rc:bpb::olmes:full",
        "hellaswag:rc:bpb::olmes:full",
        "winogrande:rc:bpb::olmes:full",
        "socialiqa:rc:bpb::olmes:full",
        "piqa:rc:bpb::olmes:full",
        "coqa:rc:bpb::gen2mc:xlarge",
        "drop:rc:bpb::gen2mc:xlarge",
        "jeopardy:rc:bpb::gen2mc:xlarge",
        "naturalqs:rc:bpb::gen2mc:xlarge",
        "squad:rc:bpb::gen2mc:xlarge",
        "sciq:rc:bpb::olmo3",
        "qasper_yesno:rc:bpb::olmes",
        "basic_skills:rc:bpb::olmes",
        "lab_bench_dbqa:bpb",
        "lab_bench_protocolqa:bpb",
        "lambada:bpb",
        "medmcqa:rc:bpb::none",
        "medqa_en:rc:bpb::none",
        "sciriff_yesno:rc:bpb::olmes",
    ),
}


def resolve_score(
    alias: str, scores: dict[str, float], stack: tuple[str, ...] = ()
) -> float:
    if alias in scores:
        return scores[alias]
    if alias in stack:
        raise ValueError(f"aggregate cycle: {' -> '.join((*stack, alias))}")
    children = FALLBACK_GROUPS.get(alias)
    if children is None:
        raise KeyError(alias)
    value = sum(resolve_score(child, scores, (*stack, alias)) for child in children) / len(children)
    scores[alias] = value
    return value


def load_scores(path: Path) -> tuple[dict[str, float], int]:
    scores: dict[str, float] = {}
    rows = 0
    with path.open(encoding="utf-8") as handle:
        for number, line in enumerate(handle, start=1):
            if not line.strip():
                continue
            rows += 1
            item = json.loads(line)
            config = item.get("task_config") or {}
            alias = (config.get("metadata") or {}).get("alias") or item.get("task_name")
            score = (item.get("metrics") or {}).get("primary_score")
            if alias is None or not isinstance(score, (int, float)) or not math.isfinite(score):
                raise ValueError(f"第 {number} 行无效:{alias=} {score=}")
            scores[str(alias)] = float(score)
    return scores, rows


def validate(output: Path, kind: str) -> None:
    if not output.is_dir():
        raise FileNotFoundError(output)
    for name in ("metrics.json", "metrics-all.jsonl"):
        if not (output / name).is_file():
            raise FileNotFoundError(output / name)
    json.loads((output / "metrics.json").read_text(encoding="utf-8"))
    leaves = len(list(output.glob("task-*-metrics.json")))
    if leaves != EXPECTED[kind]:
        raise ValueError(f"{kind} 叶子任务应为 {EXPECTED[kind]},实际 {leaves}")
    scores, rows = load_scores(output / "metrics-all.jsonl")
    if rows < EXPECTED[kind]:
        raise ValueError(f"{kind} metrics-all 行数不足:{rows}")
    missing = []
    for alias in REQUIRED[kind]:
        try:
            resolve_score(alias, scores)
        except KeyError:
            missing.append(alias)
    if missing:
        raise ValueError(f"{kind} 缺少必要结果:{missing}")


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("kind", choices=tuple(EXPECTED))
    parser.add_argument("output", type=Path)
    args = parser.parse_args()
    validate(args.output, args.kind)
    print(f"{args.kind} 结果完整:{args.output}")


if __name__ == "__main__":
    main()