File size: 6,061 Bytes
9503776 | 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 | """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
# Each doc: {"ctxs": [str,...], "conts": [str,...], "gold": int}
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}
# random-guess baseline: 1/n_choices averaged over docs (lambada is generative -> ~0)
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}
# ------------------------------------------------------------------ Belebele (target language)
# The instruction-following probe applies the SAME fixed Llama-3.1-Instruct chat format to every
# model, base and merged alike. Reading it off each model's own tokenizer would be unusable here:
# the base model and two of the three community forks ship NO chat template, so the wrapper would
# silently no-op on exactly the models it is meant to discriminate (it did, on the first run --
# base and chat scores came back bit-identical). This is the format the chat vector was trained in,
# which is what makes the raw-vs-chat delta interpretable as instruction-following behaviour.
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)
|