File size: 10,544 Bytes
2fd4f23
271e253
 
 
 
 
 
 
 
 
 
 
 
 
2fd4f23
271e253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fd4f23
271e253
 
2fd4f23
 
 
 
 
271e253
 
 
 
2fd4f23
 
271e253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fd4f23
 
 
271e253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fd4f23
271e253
 
 
 
 
 
2fd4f23
271e253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fd4f23
271e253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fd4f23
271e253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fd4f23
 
 
 
 
 
 
271e253
 
2fd4f23
271e253
 
2fd4f23
 
 
271e253
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Score a fusion GPT checkpoint on ArithMark 2.0."""

from __future__ import annotations

import argparse
from collections import Counter
from contextlib import nullcontext
import json
from pathlib import Path
import re
import urllib.request

import torch
import torch.nn.functional as F
from transformers import AutoModelForCausalLM, AutoTokenizer


DATA_URL = (
    "https://huggingface.co/datasets/AxiomicLabs/Arithmark-2.0/"
    "resolve/main/arithmark_2.0.jsonl"
)


def ensure_data(path: Path) -> Path:
    if path.exists():
        return path
    path.parent.mkdir(parents=True, exist_ok=True)
    urllib.request.urlretrieve(DATA_URL, path)
    return path


def load_examples(path: Path, *, max_examples: int = 0) -> list[dict]:
    examples = []
    with path.open("r", encoding="utf-8") as handle:
        for line in handle:
            if not line.strip():
                continue
            examples.append(json.loads(line))
            if max_examples > 0 and len(examples) >= max_examples:
                break
    return examples


def _encoded_choice(
    tokenizer,
    context: str,
    ending: str,
) -> tuple[list[int], int]:
    context_ids = tokenizer(context, add_special_tokens=False).input_ids
    full_ids = tokenizer(context + ending, add_special_tokens=False).input_ids
    continuation_length = len(full_ids) - len(context_ids)
    return full_ids, continuation_length


@torch.inference_mode()
def evaluate(
    model,
    tokenizer,
    examples: list[dict],
    *,
    device: torch.device,
    batch_size: int,
    dump_failures: bool = False,
    failure_operator_count: int | None = None,
    max_failures: int = 100,
) -> dict:
    correct = 0
    total = 0
    by_operator_count: dict[str, list[int]] = {}
    by_topic: dict[str, list[int]] = {}
    failures: list[dict] = []
    failure_summary: Counter[tuple[str, str, str]] = Counter()
    model.eval()
    pad_id = tokenizer.pad_token_id
    if pad_id is None:
        pad_id = tokenizer.eos_token_id if tokenizer.eos_token_id is not None else 0

    for start in range(0, len(examples), batch_size):
        batch_examples = examples[start : start + batch_size]
        encoded = []
        offsets = []
        for example in batch_examples:
            flat_start = len(encoded)
            encoded.extend(
                _encoded_choice(tokenizer, example["ctx"], ending)
                for ending in example["endings"]
            )
            offsets.append((flat_start, len(example["endings"])))

        max_length = max(len(item[0]) for item in encoded)
        input_ids = torch.full(
            (len(encoded), max_length),
            int(pad_id),
            dtype=torch.long,
            device=device,
        )
        attention_mask = torch.zeros_like(input_ids, dtype=torch.bool)
        lengths = []
        continuation_lengths = []
        for row, (ids, continuation_length) in enumerate(encoded):
            length = len(ids)
            input_ids[row, :length] = torch.tensor(ids, device=device)
            attention_mask[row, :length] = True
            lengths.append(length)
            continuation_lengths.append(continuation_length)

        autocast = (
            torch.autocast(device_type="cuda", dtype=torch.bfloat16)
            if device.type == "cuda"
            else nullcontext()
        )
        with autocast:
            logits = model(
                input_ids=input_ids,
                attention_mask=attention_mask,
            ).logits
        log_probs = F.log_softmax(logits.float(), dim=-1)

        for example_index, example in enumerate(batch_examples):
            flat_start, choice_count = offsets[example_index]
            likelihoods = []
            for choice_index in range(choice_count):
                row = flat_start + choice_index
                length = lengths[row]
                continuation_length = continuation_lengths[row]
                continuation_start = length - continuation_length
                likelihood = 0.0
                for position in range(continuation_start, length):
                    likelihood += float(
                        log_probs[row, position - 1, input_ids[row, position]].item()
                    )
                likelihoods.append(likelihood)

            prediction = max(range(choice_count), key=likelihoods.__getitem__)
            label = int(example["label"])
            matched = prediction == label
            correct += int(matched)
            total += 1
            metadata = example.get("metadata", {})
            operator_count = str(metadata.get("operator_count", "unknown"))
            topic = str(metadata.get("topic", "unknown"))
            for grouped, key in (
                (by_operator_count, operator_count),
                (by_topic, topic),
            ):
                group = grouped.setdefault(key, [0, 0])
                group[0] += int(matched)
                group[1] += 1

            if not matched and dump_failures:
                op_count_int = None
                try:
                    op_count_int = int(operator_count)
                except ValueError:
                    pass
                if failure_operator_count is None or op_count_int == failure_operator_count:
                    context = str(example["ctx"]).strip()
                    expression = context[:-1].strip() if context.endswith("=") else context
                    operands = [int(value) for value in re.findall(r"\d+", expression)]
                    operator = "".join(re.findall(r"[+\-*/]", expression))
                    predicted_answer = str(example["endings"][prediction]).strip()
                    correct_answer = str(example["endings"][label]).strip()
                    width = max((len(str(value)) for value in operands), default=0)
                    failure_summary[(topic, operator, f"width={width}")] += 1
                    if len(failures) < max_failures:
                        failures.append(
                            {
                                "ctx": context,
                                "topic": topic,
                                "operator_count": operator_count,
                                "operator": operator,
                                "operands": operands,
                                "max_operand_digits": width,
                                "correct_answer": correct_answer,
                                "predicted_answer": predicted_answer,
                                "choices": [str(value).strip() for value in example["endings"]],
                                "choice_scores": [round(value, 4) for value in likelihoods],
                                "score_margin_correct_minus_predicted": round(
                                    likelihoods[label] - likelihoods[prediction],
                                    4,
                                ),
                            }
                        )

    results = {
        "benchmark": "arithmark_2.0",
        "model_type": "fusion_gpt",
        "accuracy": correct / max(total, 1),
        "correct": correct,
        "total": total,
        "by_operator_count": {
            key: {
                "accuracy": values[0] / max(values[1], 1),
                "correct": values[0],
                "total": values[1],
            }
            for key, values in sorted(by_operator_count.items())
        },
        "by_topic": {
            key: {
                "accuracy": values[0] / max(values[1], 1),
                "correct": values[0],
                "total": values[1],
            }
            for key, values in sorted(by_topic.items())
        },
    }
    if dump_failures:
        results["failure_summary"] = {
            "|".join(key): value
            for key, value in failure_summary.most_common()
        }
        results["failures"] = failures
    return results


def parse_args() -> argparse.Namespace:
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--checkpoint", type=Path, default=Path("outputs/fusion_run/final_model"))
    parser.add_argument("--data-path", type=Path, default=Path("arithmark_2.0.jsonl"))
    parser.add_argument("--batch-size", type=int, default=64)
    parser.add_argument("--device", default="auto")
    parser.add_argument("--dtype", default="auto", choices=("auto", "float32", "bfloat16", "float16"))
    parser.add_argument("--output", type=Path)
    parser.add_argument(
        "--max-examples",
        type=int,
        default=0,
        help="Evaluate only the first N examples. Default evaluates all examples.",
    )
    parser.add_argument(
        "--dump-failures",
        action="store_true",
        help="Include incorrectly scored examples and grouped failure summary.",
    )
    parser.add_argument(
        "--failure-operator-count",
        type=int,
        default=None,
        help="Only dump failures with this operator count, e.g. 1 for easy examples.",
    )
    parser.add_argument("--max-failures", type=int, default=100)
    return parser.parse_args()


def main() -> None:
    args = parse_args()
    if args.device == "auto":
        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    else:
        device = torch.device(args.device)

    data_path = ensure_data(args.data_path)
    examples = load_examples(data_path, max_examples=args.max_examples)
    dtype = None
    if args.dtype == "float32":
        dtype = torch.float32
    elif args.dtype == "bfloat16":
        dtype = torch.bfloat16
    elif args.dtype == "float16":
        dtype = torch.float16
    model = AutoModelForCausalLM.from_pretrained(
        args.checkpoint,
        dtype=dtype,
        trust_remote_code=True,
    ).to(device)
    tokenizer = AutoTokenizer.from_pretrained(args.checkpoint, trust_remote_code=True)
    if tokenizer.pad_token_id is None:
        tokenizer.pad_token = tokenizer.eos_token
    results = evaluate(
        model,
        tokenizer,
        examples,
        device=device,
        batch_size=args.batch_size,
        dump_failures=args.dump_failures,
        failure_operator_count=args.failure_operator_count,
        max_failures=args.max_failures,
    )
    print(json.dumps(results, indent=2, sort_keys=True))
    if args.output:
        args.output.parent.mkdir(parents=True, exist_ok=True)
        args.output.write_text(
            json.dumps(results, indent=2, sort_keys=True) + "\n",
            encoding="utf-8",
        )


if __name__ == "__main__":
    main()