| """Downstream accuracy benchmarks, implemented directly (lm-eval-harness is not installed here). |
| Formats follow lm-evaluation-harness task YAMLs so numbers are comparable to published Pythia evals. |
| Every task is scored by loglikelihood of candidate continuations; `acc` = argmax of summed logprob, |
| `acc_norm` = argmax of logprob normalised by continuation character length.""" |
| import os, random, functools |
| from datasets import load_dataset |
|
|
| CACHE = os.environ.get("MA_DATA_CACHE", "/root/hf_cache_mergeacc/datasets") |
|
|
| def _ds(*a, **kw): |
| return load_dataset(*a, cache_dir=CACHE, **kw) |
|
|
| def _sub(rows, n, seed=1234): |
| rows = list(rows) |
| if n and len(rows) > n: |
| random.Random(seed).shuffle(rows) |
| rows = rows[:n] |
| return rows |
|
|
| |
| def sciq(n=None): |
| out = [] |
| for d in _ds("allenai/sciq", split="validation"): |
| ch = [d["distractor1"], d["distractor2"], d["distractor3"], d["correct_answer"]] |
| ctx = f"{d['support']}\nQuestion: {d['question']}\nAnswer:" |
| out.append({"ctxs": [ctx]*4, "conts": [f" {c}" for c in ch], "gold": 3}) |
| return _sub(out, n) |
|
|
| def piqa(n=None): |
| out = [] |
| for d in _ds("ybisk/piqa", split="validation", revision="refs/convert/parquet"): |
| ctx = f"Question: {d['goal']}\nAnswer:" |
| out.append({"ctxs": [ctx]*2, "conts": [f" {d['sol1']}", f" {d['sol2']}"], "gold": int(d["label"])}) |
| return _sub(out, n) |
|
|
| def arc_easy(n=None): |
| out = [] |
| for d in _ds("allenai/ai2_arc", "ARC-Easy", split="test"): |
| ch = d["choices"]["text"]; lab = list(d["choices"]["label"]) |
| if d["answerKey"] not in lab: continue |
| ctx = f"Question: {d['question']}\nAnswer:" |
| out.append({"ctxs": [ctx]*len(ch), "conts": [f" {c}" for c in ch], "gold": lab.index(d["answerKey"])}) |
| return _sub(out, n) |
|
|
| def arc_challenge(n=None): |
| out = [] |
| for d in _ds("allenai/ai2_arc", "ARC-Challenge", split="test"): |
| ch = d["choices"]["text"]; lab = list(d["choices"]["label"]) |
| if d["answerKey"] not in lab: continue |
| ctx = f"Question: {d['question']}\nAnswer:" |
| out.append({"ctxs": [ctx]*len(ch), "conts": [f" {c}" for c in ch], "gold": lab.index(d["answerKey"])}) |
| return _sub(out, n) |
|
|
| def boolq(n=None): |
| out = [] |
| for d in _ds("aps/super_glue", "boolq", split="validation"): |
| ctx = f"{d['passage']}\nQuestion: {d['question']}?\nAnswer:" |
| out.append({"ctxs": [ctx]*2, "conts": [" no", " yes"], "gold": int(d["label"])}) |
| return _sub(out, n) |
|
|
| def winogrande(n=None): |
| """Harness 'partial evaluation': substitute each option into the blank, score the SHARED |
| suffix after the blank. Contexts differ, continuation is identical.""" |
| out = [] |
| for d in _ds("allenai/winogrande", "winogrande_xl", split="validation"): |
| s = d["sentence"]; i = s.index("_") |
| pre, suf = s[:i], s[i+1:] |
| out.append({"ctxs": [pre + d["option1"], pre + d["option2"]], "conts": [suf, suf], |
| "gold": int(d["answer"]) - 1}) |
| return _sub(out, n) |
|
|
| def lambada(n=None): |
| out = [] |
| for d in _ds("EleutherAI/lambada_openai", "en", split="test"): |
| t = d["text"].strip() |
| ctx, _, last = t.rpartition(" ") |
| out.append({"ctxs": [ctx], "conts": [" " + last], "gold": 0, "greedy": True}) |
| return _sub(out, n) |
|
|
| def logiqa(n=None): |
| out = [] |
| for d in _ds("EleutherAI/logiqa", "logiqa", split="validation"): |
| ctx = f"Passage: {d['context']}\nQuestion: {d['question']}\nChoices:\n" |
| ctx += "".join(f"{l}. {o}\n" for l, o in zip("ABCD", d["options"])) |
| ctx += "Answer:" |
| out.append({"ctxs": [ctx]*4, "conts": [f" {o}" for o in d["options"]], |
| "gold": int(d["label"])}) |
| return _sub(out, n) |
|
|
| TASKS = {"sciq": sciq, "piqa": piqa, "arc_easy": arc_easy, "arc_challenge": arc_challenge, |
| "boolq": boolq, "winogrande": winogrande, "lambada": lambada, "logiqa": logiqa} |
| |
| CHANCE = {"sciq": 0.25, "piqa": 0.5, "arc_easy": 0.25, "arc_challenge": 0.25, |
| "boolq": 0.5, "winogrande": 0.5, "lambada": 0.0, "logiqa": 0.25} |
|
|
|
|
| |
| |
| |
| |
| |
| |
| |
| LLAMA31_CHAT = ("<|begin_of_text|><|start_header_id|>user<|end_header_id|>\n\n" |
| "{content}<|eot_id|><|start_header_id|>assistant<|end_header_id|>\n\n") |
| _BEL = {} |
| def belebele(lang="eng_Latn", n=None, chat=False, tok=None): |
| """Belebele MC reading comprehension, harness format. 4 options -> chance 0.25. |
| `chat=True` wraps the prompt in the model's chat template, which is how we read off |
| instruction-following behaviour without a generative harness.""" |
| if lang not in _BEL: |
| _BEL[lang] = list(_ds("facebook/belebele", lang, split="test")) |
| out = [] |
| for d in _BEL[lang]: |
| opts = [d["mc_answer1"], d["mc_answer2"], d["mc_answer3"], d["mc_answer4"]] |
| body = (f"{d['flores_passage']}\nQ: {d['question']}\n" |
| + "".join(f"{l}. {o}\n" for l, o in zip("ABCD", opts)) |
| + "Answer:") |
| if chat: |
| body = LLAMA31_CHAT.format( |
| content=body.replace("\nAnswer:", "\nAnswer with A, B, C or D.")) |
| out.append({"ctxs": [body]*4, "conts": [f" {l}" for l in "ABCD"], |
| "gold": int(d["correct_answer_num"]) - 1}) |
| return _sub(out, n) |
|
|