File size: 11,756 Bytes
46b9eea
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
#!/usr/bin/env python3
"""Aligned 8K HELMET-ICL and OpenAI-MRCR pilot for Qwen checkpoints.

This is deliberately a small diagnostic runner.  HELMET prompt construction
matches the ICL schedule used by the local OmniServe reproduction; MRCR uses
the official OpenAI rows selected by that reproduction, but renders the
messages with the Qwen chat template.
"""

from __future__ import annotations

import argparse
import csv
import hashlib
import json
import math
import random
import re
import string
from collections import defaultdict
from difflib import SequenceMatcher
from pathlib import Path

import pandas as pd
import torch
from datasets import load_dataset
from huggingface_hub import hf_hub_download
from transformers import AutoModelForCausalLM, AutoTokenizer


HELMET_SPECS = {
    "trec_coarse": ("icl_trec_coarse_400shot_balance", 6),
    "trec_fine": ("icl_trec_fine_400shot_balance", 50),
    "banking77": ("icl_banking77_360shot_balance", 77),
    "clinic150": ("icl_clinic150_440shot_balance", 151),
    "nlu": ("icl_nlu_510shot_balance", 68),
}


def balanced(data, shots: int, label_field: str, seed: int):
    rng = random.Random(seed)
    by_label = defaultdict(list)
    for item in data:
        by_label[item[label_field]].append(item)
    rounds = math.ceil(shots / len(by_label))
    selected_rounds = [[] for _ in range(rounds)]
    for examples in by_label.values():
        indices = rng.sample(range(len(examples)), rounds % len(examples))
        while len(indices) < rounds:
            indices += rng.sample(range(len(examples)), min(rounds - len(indices), len(examples)))
        for index, example_index in enumerate(indices):
            selected_rounds[index].append(examples[example_index])
    for examples in selected_rounds:
        rng.shuffle(examples)
    return [item for group in selected_rounds for item in group][:shots]


def helmet_source(family: str, seed: int):
    if family == "trec_coarse":
        source = load_dataset("CogComp/trec", trust_remote_code=True)
        return source["train"], source["test"], "text", "coarse_label"
    if family == "trec_fine":
        source = load_dataset("CogComp/trec", trust_remote_code=True)
        return source["train"], source["test"], "text", "fine_label"
    if family == "banking77":
        source = load_dataset("PolyAI/banking77", trust_remote_code=True)
        return source["train"], source["test"], "text", "label"
    if family == "clinic150":
        source = load_dataset("clinc/clinc_oos", "plus")
        return source["train"], source["validation"], "text", "intent"
    if family == "nlu":
        source = load_dataset("xingkunliuxtracta/nlu_evaluation_data", trust_remote_code=True)["train"]
        split = source.train_test_split(test_size=0.1, seed=seed)
        return split["train"], split["test"], "text", "label"
    raise ValueError(family)


def build_helmet(seed: int, limit: int):
    rows = []
    user_template = (
        'Use the provided mapping from the text to label to assign a label to the text. '
        'Only output "label: {{label}}" and nothing else. \n\n{context}\n\n{question}'
    )
    for family, (dataset_name, num_labels) in HELMET_SPECS.items():
        shots = int(dataset_name.split("shot")[0].split("_")[-1])
        train, test, text_field, label_field = helmet_source(family, seed)
        samples = balanced(test, limit, label_field, seed)
        for sample_index, sample in enumerate(samples):
            local_seed = (int(hashlib.sha256(sample[text_field].encode()).hexdigest(), 16) + seed) % 2**31
            demos = balanced(train, shots, label_field, local_seed)
            mapping = list(range(num_labels))
            random.Random(local_seed).shuffle(mapping)
            context = "\n\n".join(
                f"{demo[text_field]}\nlabel: {mapping[int(demo[label_field])]}" for demo in demos
            )
            rows.append({
                "row_id": f"helmet_icl:8k:{family}:{sample_index}",
                "benchmark": "helmet_icl",
                "config": family,
                "prompt": user_template.format(context=context, question=sample[text_field]) + "\nlabel:",
                "answer": str(mapping[int(sample[label_field])]),
                "max_new_tokens": 20,
                "metadata": {"dataset": dataset_name, "shots": shots},
            })
    return rows


def build_mrcr(tokenizer, selection_path: Path, limit: int):
    selection = [json.loads(line) for line in selection_path.read_text(encoding="utf-8").splitlines() if line.strip()]
    chosen = sorted(
        (row for row in selection if int(row["context_k"]) == 8),
        key=lambda row: int(row["sample_index"]),
    )[:limit]
    if len(chosen) < limit:
        raise ValueError(f"MRCR selection contains {len(chosen)} 8K rows, requested {limit}")
    files = [
        hf_hub_download("openai/mrcr", filename=f"2needle/2needle_{shard}.parquet", repo_type="dataset")
        for shard in (0, 1)
    ]
    frame = pd.concat([pd.read_parquet(path) for path in files], ignore_index=True)
    rows = []
    for selected in chosen:
        source = frame.iloc[int(selected["source_index"])]
        messages = json.loads(source["prompt"])
        prompt = tokenizer.apply_chat_template(
            messages,
            tokenize=False,
            add_generation_prompt=True,
            enable_thinking=False,
        )
        answer = str(source["answer"])
        sample_index = int(selected["sample_index"])
        rows.append({
            "row_id": f"mrcr:8k:2needle:{sample_index}",
            "benchmark": "mrcr",
            "config": "2needle",
            "prompt": prompt,
            "answer": answer,
            "prefix": str(source["random_string_to_prepend"]),
            "max_new_tokens": min(768, len(tokenizer(answer, add_special_tokens=False).input_ids) + 64),
            "metadata": {"source_index": int(selected["source_index"]), "official_tokens": int(selected["official_tokens"])},
        })
    return rows


def normalize(text: str) -> str:
    text = text.lower()
    text = re.sub(r"\b(a|an|the)\b", " ", text)
    text = "".join(ch for ch in text if ch not in string.punctuation)
    return " ".join(text.split())


def score(row, prediction: str) -> float:
    if row["benchmark"] == "helmet_icl":
        first = prediction.strip().splitlines()[0] if prediction.strip() else ""
        parsed = re.sub(r"^label:", "", first, flags=re.I).strip()
        return float(normalize(parsed) == normalize(row["answer"]))
    prefix = row["prefix"]
    if not prediction.startswith(prefix):
        return 0.0
    response = prediction.removeprefix(prefix).strip()
    reference = row["answer"].removeprefix(prefix).strip()
    return float(SequenceMatcher(None, response, reference).ratio())


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--benchmark", choices=("helmet_icl", "mrcr"), required=True)
    parser.add_argument("--model-path", required=True)
    parser.add_argument("--tokenizer-path", required=True)
    parser.add_argument(
        "--attn-implementation",
        default="flash_attention_2",
        choices=("eager", "sdpa", "flash_attention_2"),
        help="Transformers attention backend; AHA 4-D local masks require SDPA or eager.",
    )
    parser.add_argument("--selection", type=Path)
    parser.add_argument("--rows", type=Path, help="Frozen aligned prompt rows (JSONL)")
    parser.add_argument("--method", required=True)
    parser.add_argument("--output", type=Path, required=True)
    parser.add_argument("--seed", type=int, default=20260710)
    parser.add_argument("--limit", type=int, default=1, help="Samples per benchmark config")
    parser.add_argument(
        "--stop-new-line",
        action="store_true",
        help="Use HELMET's newline stop-token policy during generation.",
    )
    parser.add_argument(
        "--duo-sink-size",
        type=int,
        help="Optional inference-only override for the AHA sink token count.",
    )
    parser.add_argument(
        "--duo-recent-size",
        type=int,
        help="Optional inference-only override for the AHA recent-window token count.",
    )
    args = parser.parse_args()

    tokenizer = AutoTokenizer.from_pretrained(args.tokenizer_path, trust_remote_code=True, use_fast=False)
    if args.rows:
        rows = [json.loads(line) for line in args.rows.read_text(encoding="utf-8").splitlines() if line.strip()]
    else:
        rows = (
            build_helmet(args.seed, args.limit)
            if args.benchmark == "helmet_icl"
            else build_mrcr(tokenizer, args.selection, args.limit)
        )
    model = AutoModelForCausalLM.from_pretrained(
        args.model_path,
        trust_remote_code=True,
        torch_dtype=torch.bfloat16,
        attn_implementation=args.attn_implementation,
    )
    for field, value in (
        ("duo_sink_size", args.duo_sink_size),
        ("duo_recent_size", args.duo_recent_size),
    ):
        if value is not None:
            if value < 0:
                parser.error(f"--{field.replace('_', '-')} must be non-negative")
            previous = getattr(model.config, field, None)
            setattr(model.config, field, value)
            print(f"[eval] {field} override: {previous!r} -> {value!r}", flush=True)
    model = model.to("cuda").eval()
    stop_token_ids = model.generation_config.eos_token_id
    stop_token_ids = list(stop_token_ids) if isinstance(stop_token_ids, list) else [stop_token_ids]
    if args.stop_new_line:
        newline_tokens = ["\n", "Ċ", "ĊĊ", "<0x0A>"]
        stop_token_ids += [tokenizer.convert_tokens_to_ids(token) for token in newline_tokens]
    stop_token_ids = sorted(
        {
            token_id
            for token_id in stop_token_ids
            if token_id is not None and token_id != tokenizer.unk_token_id
        }
    )
    print(f"[eval] stop_token_ids={stop_token_ids}", flush=True)
    args.output.parent.mkdir(parents=True, exist_ok=True)
    outputs = []
    for ordinal, row in enumerate(rows, 1):
        encoded = tokenizer(row["prompt"], return_tensors="pt", add_special_tokens=False)
        prompt_tokens = int(encoded.input_ids.shape[1])
        print(f"[{args.benchmark}] {ordinal}/{len(rows)} {row['config']} tokens={prompt_tokens}", flush=True)
        encoded = {key: value.to("cuda") for key, value in encoded.items()}
        with torch.inference_mode():
            generated = model.generate(
                **encoded,
                do_sample=False,
                max_new_tokens=row["max_new_tokens"],
                use_cache=True,
                eos_token_id=stop_token_ids,
                pad_token_id=tokenizer.pad_token_id,
            )
        prediction = tokenizer.decode(generated[0, prompt_tokens:], skip_special_tokens=True).strip()
        outputs.append({
            **row,
            "method": args.method,
            "prompt_tokens": prompt_tokens,
            "prompt_sha256": hashlib.sha256(row["prompt"].encode()).hexdigest(),
            "prediction": prediction,
            "score": score(row, prediction),
        })
    args.output.write_text("".join(json.dumps(row, ensure_ascii=False) + "\n" for row in outputs))
    summary = args.output.with_suffix(".summary.csv")
    with summary.open("w", newline="", encoding="utf-8") as handle:
        writer = csv.DictWriter(handle, fieldnames=["method", "benchmark", "config", "n", "score", "prompt_tokens"])
        writer.writeheader()
        for row in outputs:
            writer.writerow({"method": args.method, "benchmark": row["benchmark"], "config": row["config"], "n": 1, "score": row["score"], "prompt_tokens": row["prompt_tokens"]})
    print(summary)


if __name__ == "__main__":
    main()