File size: 10,316 Bytes
f157cf0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
"""Math evaluation sets and calibration data.

The leaderboard set is hidden and probably postdates the model, so we keep two
tiers deliberately separate:

* **gate**  — small, fast, run on every recipe. Cheap signal for iteration.
* **holdout** — recent competitions we never tune against. The honest estimate.

AIME 2024 is deliberately excluded from the holdout: it is measurably
contaminated (inflating scores 10-20 points over clean contests), so it flatters
every recipe equally and discriminates between none of them.
"""

from __future__ import annotations

from dataclasses import dataclass, field
from typing import Any, Iterable, Sequence


@dataclass
class MathExample:
    example_id: str
    problem: str
    answer: str
    source: str
    metadata: dict[str, Any] = field(default_factory=dict)


@dataclass(frozen=True)
class DatasetSpec:
    """How to pull one benchmark off the Hub.

    Field names differ between mirrors of the same benchmark, so each role lists
    candidate column names tried in order.
    """

    name: str
    hf_id: str
    split: str = "test"
    config: str | None = None
    problem_fields: Sequence[str] = ("problem", "Problem", "question", "Question")
    answer_fields: Sequence[str] = ("answer", "Answer", "solution", "expected_answer")
    tier: str = "gate"
    filters: tuple[tuple[str, str, Any], ...] = ()
    max_examples: int | None = None
    note: str = ""


# Three tiers, by how often we run them and how much tuning pressure they can
# absorb before their numbers stop meaning anything.
#
#   gate       every experiment. Cheap, tuned against freely.
#   checkpoint before a weekly leaderboard submission. Moderate tuning risk.
#   holdout    the two graded checkpoints only. NEVER tuned against — these are
#              post-release contests and the closest proxy we have for a hidden
#              eval that "is not available in public domain today".
REGISTRY: dict[str, DatasetSpec] = {
    "math500_hard": DatasetSpec(
        name="math500_hard",
        hf_id="HuggingFaceH4/MATH-500",
        split="test",
        tier="gate",
        filters=(("level", "gte", 4),),
        max_examples=100,
        note="MATH-500 levels 4-5, deterministic 100-problem subsample. The "
        "fast regression signal: sensitive enough to catch damage, cheap "
        "enough to run on every recipe.",
    ),
    "math500": DatasetSpec(
        name="math500",
        hf_id="HuggingFaceH4/MATH-500",
        split="test",
        tier="checkpoint",
        note="Full 500. Largely saturated for this model (~84.5 bf16), so it "
        "detects collapse but not subtle reasoning damage.",
    ),
    "aime25": DatasetSpec(
        name="aime25",
        hf_id="MathArena/aime_2025",
        split="train",
        tier="checkpoint",
        note="30 problems. Hard tail — where quantization damage actually shows.",
    ),
    "hmmt_feb25": DatasetSpec(
        name="hmmt_feb25",
        hf_id="MathArena/hmmt_feb_2025",
        split="train",
        tier="checkpoint",
        note="30 problems. Reported on the model card (74.0), so we have a "
        "published bf16 reference to validate our harness against.",
    ),
    "aime26": DatasetSpec(
        name="aime26",
        hf_id="MathArena/aime_2026",
        split="train",
        tier="holdout",
        note="30 problems, Feb 2026 contest. Post-dates most training data.",
    ),
    "hmmt_feb26": DatasetSpec(
        name="hmmt_feb26",
        hf_id="MathArena/hmmt_feb_2026",
        split="train",
        tier="holdout",
        note="33 problems, Feb 2026 contest. Cleanest proxy for the hidden eval.",
    ),
    "aime24": DatasetSpec(
        name="aime24",
        hf_id="Maxwell-Jia/AIME_2024",
        split="train",
        tier="diagnostic",
        note="Measurably contaminated — inflates scores 10-20 points over clean "
        "contests. Diagnostic only, never for recipe selection.",
    ),
}

# Training pools — problems with known answers, used to generate our own
# reasoning traces. Disjoint from every eval set above: MATH-500 is drawn from
# the MATH *test* split, so the MATH train split cannot leak into it.
TRAIN_REGISTRY: dict[str, DatasetSpec] = {
    "math_train": DatasetSpec(
        name="math_train",
        hf_id="EleutherAI/hendrycks_math",
        split="train",
        config="algebra",
        answer_fields=("solution",),  # gold answer is the \boxed{} in the solution
        tier="train",
        note="MATH train split. Pass --config to pick a subject.",
    ),
    "openr1": DatasetSpec(
        name="openr1",
        hf_id="open-r1/OpenR1-Math-220k",
        split="train",
        answer_fields=("answer", "solution"),
        tier="train",
        note="220k competition problems with verified answers.",
    ),
}

MATH_SUBJECTS = (
    "algebra", "counting_and_probability", "geometry", "intermediate_algebra",
    "number_theory", "prealgebra", "precalculus",
)

SUITES: dict[str, list[str]] = {
    "gate": ["math500_hard"],
    "checkpoint": ["math500", "aime25", "hmmt_feb25"],
    "holdout": ["aime26", "hmmt_feb26"],
}


def _passes_filters(row: dict[str, Any], filters: Sequence[tuple[str, str, Any]]) -> bool:
    for field_name, op, value in filters:
        actual = row.get(field_name)
        if actual is None:
            return False
        if op == "gte" and not actual >= value:
            return False
        if op == "lte" and not actual <= value:
            return False
        if op == "eq" and actual != value:
            return False
        if op == "in" and actual not in value:
            return False
    return True


def _subsample(examples: list[MathExample], n: int, seed: int = 0) -> list[MathExample]:
    """Deterministic subsample, stable across runs and machines.

    Shuffles with a fixed seed rather than taking a prefix, because these sets
    are ordered by subject/difficulty and a prefix would be badly skewed. The
    gate set must be identical across every recipe or the comparison is
    meaningless.
    """
    import random

    if len(examples) <= n:
        return examples
    indices = sorted(range(len(examples)))
    random.Random(seed).shuffle(indices)
    return [examples[i] for i in sorted(indices[:n])]


def _resolve_field(row: dict[str, Any], candidates: Iterable[str]) -> str | None:
    lowered = {k.lower(): k for k in row}
    for candidate in candidates:
        key = lowered.get(candidate.lower())
        if key is not None and row[key] is not None:
            return str(row[key])
    return None


def load_dataset_examples(
    spec: DatasetSpec | str,
    limit: int | None = None,
    cache_dir: str | None = None,
) -> list[MathExample]:
    """Load one benchmark into ``MathExample`` records.

    Raises with the observed column names when a field cannot be resolved, so a
    schema change on the Hub produces an actionable error instead of silently
    empty problems.
    """
    from datasets import load_dataset

    if isinstance(spec, str):
        table = {**REGISTRY, **TRAIN_REGISTRY}
        if spec not in table:
            raise KeyError(f"Unknown dataset {spec!r}. Known: {sorted(table)}")
        spec = table[spec]

    kwargs: dict[str, Any] = {"split": spec.split}
    if spec.config:
        kwargs["name"] = spec.config
    if cache_dir:
        kwargs["cache_dir"] = cache_dir

    dataset = load_dataset(spec.hf_id, **kwargs)

    examples: list[MathExample] = []
    for i, row in enumerate(dataset):
        if not _passes_filters(row, spec.filters):
            continue
        problem = _resolve_field(row, spec.problem_fields)
        answer = _resolve_field(row, spec.answer_fields)
        if problem is None or answer is None:
            raise ValueError(
                f"{spec.name}: could not resolve problem/answer fields. "
                f"Available columns: {sorted(row)}. "
                f"Tried problem={list(spec.problem_fields)}, answer={list(spec.answer_fields)}."
            )
        if "\\boxed" in answer:
            from .answers import extract_boxed

            boxed = extract_boxed(answer)
            if boxed is None:
                continue  # unparseable gold: drop rather than train on it
            answer = boxed

        examples.append(
            MathExample(
                example_id=f"{spec.name}:{i}",
                problem=problem,
                answer=answer,
                source=spec.name,
                metadata={
                    k: row[k]
                    for k in ("level", "subject", "type", "url", "id", "problem_idx")
                    if k in row
                },
            )
        )

    # Spec cap first (defines the canonical set), then the ad-hoc --limit.
    if spec.max_examples is not None:
        examples = _subsample(examples, spec.max_examples)
    if limit is not None:
        examples = examples[:limit]
    return examples


def load_suite(
    names: Sequence[str],
    limit: int | None = None,
    cache_dir: str | None = None,
) -> list[MathExample]:
    """Load and concatenate several benchmarks. ``limit`` applies per dataset.

    Accepts tier names (``gate``/``checkpoint``/``holdout``) as shorthand for
    the datasets in that tier.
    """
    resolved: list[str] = []
    for name in names:
        resolved.extend(SUITES[name] if name in SUITES else [name])

    out: list[MathExample] = []
    for name in resolved:
        out.extend(load_dataset_examples(name, limit=limit, cache_dir=cache_dir))
    return out


def describe_registry() -> str:
    lines = []
    for tier in ("gate", "checkpoint", "holdout", "diagnostic"):
        members = [s for s in REGISTRY.values() if s.tier == tier]
        if not members:
            continue
        lines.append(f"[{tier}]")
        for spec in members:
            cap = f" (capped at {spec.max_examples})" if spec.max_examples else ""
            lines.append(f"  {spec.name:<14} {spec.hf_id}{cap}")
            lines.append(f"  {'':<14} {spec.note}")
    return "\n".join(lines)


MATH_PROMPT = (
    "Solve the following math problem. Put your final answer inside "
    "\\boxed{{}} on the last line.\n\n"
    "Problem:\n{problem}"
)


def build_prompt(example: MathExample) -> str:
    return MATH_PROMPT.format(problem=example.problem.strip())