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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""HuggingFace datasets -> decision-model rows.
|
| 2 |
+
|
| 3 |
+
Real human-labeled corpora, mostly DET (hard) targets — complementing the
|
| 4 |
+
synthetic SOFT families. Lazy load via `datasets`, module-level cache, rows
|
| 5 |
+
drawn with replacement like the other gen modules.
|
| 6 |
+
|
| 7 |
+
Primitive mapping:
|
| 8 |
+
MCQ (sciq/arc/obqa/csqa/race/mmlu/hellaswag/winogrande/piqa) -> choice
|
| 9 |
+
topic cls (ag_news/dbpedia/yahoo/trec/emotion/banking77/finpb) -> choice
|
| 10 |
+
binary (sst2/cola/mrpc/wnli/qnli/rte/imdb/rt/amazon/paws/wic/boolq) -> noul
|
| 11 |
+
NLI (snli/mnli) -> choice + shared-state noul probes
|
| 12 |
+
go_emotions (multi-label) -> shared-state noul probes
|
| 13 |
+
ordinal (yelp stars/sst5/app_reviews) -> score (5-level)
|
| 14 |
+
stsb 0-5 similarity -> regress (normal quantiles, annotator spread)
|
| 15 |
+
squad_v2 -> noul answerability
|
| 16 |
+
"""
|
| 17 |
+
import math
|
| 18 |
+
import random
|
| 19 |
+
from statistics import NormalDist
|
| 20 |
+
|
| 21 |
+
from .genbase import (generate, emit, q_choice, q_noul, q_score, q_regress,
|
| 22 |
+
validate_row, write_jsonl)
|
| 23 |
+
|
| 24 |
+
rng = random.Random()
|
| 25 |
+
_ND = NormalDist()
|
| 26 |
+
_CACHE = {}
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def _trunc(s, n):
|
| 30 |
+
s = " ".join(str(s).split())
|
| 31 |
+
if len(s) <= n:
|
| 32 |
+
return s
|
| 33 |
+
return s[:n].rsplit(" ", 1)[0] + "..."
|
| 34 |
+
|
| 35 |
+
|
| 36 |
+
def _ds(key, *args, **kw):
|
| 37 |
+
"""load_dataset with cache; kw['cols'] limits kept columns if given."""
|
| 38 |
+
if key not in _CACHE:
|
| 39 |
+
from datasets import load_dataset
|
| 40 |
+
cols = kw.pop("cols", None)
|
| 41 |
+
try:
|
| 42 |
+
ds = load_dataset(*args, **kw)
|
| 43 |
+
except Exception:
|
| 44 |
+
_CACHE[key] = []
|
| 45 |
+
return _CACHE[key]
|
| 46 |
+
rows = ds.to_list() if hasattr(ds, "to_list") else list(ds)
|
| 47 |
+
if cols:
|
| 48 |
+
rows = [{c: r.get(c) for c in cols} for r in rows]
|
| 49 |
+
_CACHE[key] = rows
|
| 50 |
+
return _CACHE[key]
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
def _pick(rows):
|
| 54 |
+
return rows[rng.randrange(len(rows))]
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def _onehot(n, k):
|
| 58 |
+
t = [0.0] * n
|
| 59 |
+
t[k] = 1.0
|
| 60 |
+
return t
|
| 61 |
+
|
| 62 |
+
|
| 63 |
+
def _shuffle_opts(options, k):
|
| 64 |
+
"""Return (shuffled_options, new_index_of_true)."""
|
| 65 |
+
idx = list(range(len(options)))
|
| 66 |
+
rng.shuffle(idx)
|
| 67 |
+
return [options[i] for i in idx], idx.index(k)
|
| 68 |
+
|
| 69 |
+
|
| 70 |
+
# ---------------------------------------------------------------------------
|
| 71 |
+
# MCQ -> choice
|
| 72 |
+
# ---------------------------------------------------------------------------
|
| 73 |
+
|
| 74 |
+
_SCIQ_Q = ["Pick the correct answer.", "Which answer is correct?",
|
| 75 |
+
"Choose the right answer to this science question.",
|
| 76 |
+
"Select the correct option."]
|
| 77 |
+
_ARC_Q = ["Choose the correct answer.", "Which option correctly answers the question?",
|
| 78 |
+
"Select the right answer.", "Pick the answer."]
|
| 79 |
+
_OBQA_Q = ["Which fact answers the question?", "Choose the correct answer.",
|
| 80 |
+
"Select the right option.", "Pick the correct answer."]
|
| 81 |
+
_CSQA_Q = ["Choose the most sensible answer.", "Which option best answers the question?",
|
| 82 |
+
"Pick the correct answer.", "Select the best answer."]
|
| 83 |
+
_RACE_Q = ["Answer the question about the passage.",
|
| 84 |
+
"Choose the correct answer based on the article.",
|
| 85 |
+
"Which option best answers the question?", "Pick the right answer."]
|
| 86 |
+
_MMLU_Q = ["Choose the correct answer.", "Select the right answer.",
|
| 87 |
+
"Which option is correct?", "Pick the best answer."]
|
| 88 |
+
_HS_Q = ["Which ending continues the passage most sensibly?",
|
| 89 |
+
"Pick the most plausible continuation.",
|
| 90 |
+
"Choose the ending that makes the most sense.",
|
| 91 |
+
"Select the most likely continuation."]
|
| 92 |
+
|
| 93 |
+
|
| 94 |
+
def gen_hf_sciq():
|
| 95 |
+
r = _pick(_ds("sciq", "allenai/sciq", split="train",
|
| 96 |
+
cols=["question", "correct_answer", "distractor1",
|
| 97 |
+
"distractor2", "distractor3", "support"]))
|
| 98 |
+
options = [r["correct_answer"], r["distractor1"], r["distractor2"],
|
| 99 |
+
r["distractor3"]]
|
| 100 |
+
options, k = _shuffle_opts([_trunc(o, 120) for o in options], 0)
|
| 101 |
+
sup = _trunc(r.get("support") or "", 400)
|
| 102 |
+
state = ((f"Passage: {sup}\n" if sup else "")
|
| 103 |
+
+ f"Question: {_trunc(r['question'], 200)}")
|
| 104 |
+
return state, [q_choice("ans", rng.choice(_SCIQ_Q), options,
|
| 105 |
+
_onehot(len(options), k))]
|
| 106 |
+
|
| 107 |
+
|
| 108 |
+
def _gen_arc(cfg):
|
| 109 |
+
r = _pick(_ds(f"arc_{cfg}", "allenai/ai2_arc", cfg, split="train",
|
| 110 |
+
cols=["question", "choices", "answerKey"]))
|
| 111 |
+
labels = r["choices"]["label"]
|
| 112 |
+
texts = [_trunc(t, 140) for t in r["choices"]["text"]]
|
| 113 |
+
if r["answerKey"] not in labels:
|
| 114 |
+
raise ValueError("bad key")
|
| 115 |
+
k = labels.index(r["answerKey"])
|
| 116 |
+
texts, k = _shuffle_opts(texts, k)
|
| 117 |
+
state = f"Question: {_trunc(r['question'], 250)}"
|
| 118 |
+
return state, [q_choice("ans", rng.choice(_ARC_Q), texts,
|
| 119 |
+
_onehot(len(texts), k))]
|
| 120 |
+
|
| 121 |
+
|
| 122 |
+
def gen_hf_arc_easy():
|
| 123 |
+
return _gen_arc("ARC-Easy")
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def gen_hf_arc_challenge():
|
| 127 |
+
return _gen_arc("ARC-Challenge")
|
| 128 |
+
|
| 129 |
+
|
| 130 |
+
def gen_hf_openbookqa():
|
| 131 |
+
r = _pick(_ds("obqa", "allenai/openbookqa", "main", split="train",
|
| 132 |
+
cols=["question_stem", "choices", "answerKey"]))
|
| 133 |
+
labels = r["choices"]["label"]
|
| 134 |
+
texts = [_trunc(t, 140) for t in r["choices"]["text"]]
|
| 135 |
+
if r["answerKey"] not in labels:
|
| 136 |
+
raise ValueError("bad key")
|
| 137 |
+
k = labels.index(r["answerKey"])
|
| 138 |
+
texts, k = _shuffle_opts(texts, k)
|
| 139 |
+
state = f"Question: {_trunc(r['question_stem'], 250)}"
|
| 140 |
+
return state, [q_choice("ans", rng.choice(_OBQA_Q), texts,
|
| 141 |
+
_onehot(len(texts), k))]
|
| 142 |
+
|
| 143 |
+
|
| 144 |
+
def gen_hf_csqa():
|
| 145 |
+
r = _pick(_ds("csqa", "tau/commonsense_qa", split="train",
|
| 146 |
+
cols=["question", "choices", "answerKey"]))
|
| 147 |
+
labels = r["choices"]["label"]
|
| 148 |
+
texts = [_trunc(t, 140) for t in r["choices"]["text"]]
|
| 149 |
+
if r["answerKey"] not in labels:
|
| 150 |
+
raise ValueError("bad key")
|
| 151 |
+
k = labels.index(r["answerKey"])
|
| 152 |
+
texts, k = _shuffle_opts(texts, k)
|
| 153 |
+
state = f"Question: {_trunc(r['question'], 250)}"
|
| 154 |
+
return state, [q_choice("ans", rng.choice(_CSQA_Q), texts,
|
| 155 |
+
_onehot(len(texts), k))]
|
| 156 |
+
|
| 157 |
+
|
| 158 |
+
def gen_hf_race():
|
| 159 |
+
r = _pick(_ds("race", "ehovy/race", "all", split="train",
|
| 160 |
+
cols=["article", "question", "options", "answer"]))
|
| 161 |
+
art = _trunc(r["article"], 900)
|
| 162 |
+
q = r["question"].replace("_", "_____")
|
| 163 |
+
options = [_trunc(o, 140) for o in r["options"]]
|
| 164 |
+
k = "ABCD".index(r["answer"])
|
| 165 |
+
state = f"Article: {art}\nQuestion: {_trunc(q, 200)}"
|
| 166 |
+
return state, [q_choice("ans", rng.choice(_RACE_Q), options,
|
| 167 |
+
_onehot(len(options), k))]
|
| 168 |
+
|
| 169 |
+
|
| 170 |
+
def gen_hf_mmlu():
|
| 171 |
+
r = _pick(_ds("mmlu", "cais/mmlu", "all", split="test",
|
| 172 |
+
cols=["subject", "question", "choices", "answer"]))
|
| 173 |
+
options = [_trunc(o, 160) for o in r["choices"]]
|
| 174 |
+
subj = r["subject"].replace("_", " ")
|
| 175 |
+
state = (f"Subject: {subj}\nQuestion: {_trunc(r['question'], 300)}")
|
| 176 |
+
return state, [q_choice("ans", rng.choice(_MMLU_Q), options,
|
| 177 |
+
_onehot(len(options), int(r["answer"])))]
|
| 178 |
+
|
| 179 |
+
|
| 180 |
+
def gen_hf_hellaswag():
|
| 181 |
+
r = _pick(_ds("hellaswag", "Rowan/hellaswag", split="train",
|
| 182 |
+
cols=["ctx", "endings", "label"]))
|
| 183 |
+
endings = [_trunc(e, 180) for e in r["endings"]]
|
| 184 |
+
state = f"Passage: {_trunc(r['ctx'], 500)}"
|
| 185 |
+
return state, [q_choice("end", rng.choice(_HS_Q), endings,
|
| 186 |
+
_onehot(len(endings), int(r["label"])))]
|
| 187 |
+
|
| 188 |
+
|
| 189 |
+
def gen_hf_winogrande():
|
| 190 |
+
r = _pick(_ds("winogrande", "allenai/winogrande", "winogrande_xl",
|
| 191 |
+
split="train",
|
| 192 |
+
cols=["sentence", "option1", "option2", "answer"]))
|
| 193 |
+
sent = r["sentence"].replace("_", "_____")
|
| 194 |
+
options, k = _shuffle_opts([r["option1"], r["option2"]],
|
| 195 |
+
int(r["answer"]) - 1)
|
| 196 |
+
state = f"Sentence: {_trunc(sent, 250)}"
|
| 197 |
+
qs = [q_choice("fill", "Which option correctly fills the blank?",
|
| 198 |
+
options, _onehot(2, k))]
|
| 199 |
+
if rng.random() < 0.4:
|
| 200 |
+
qs.append(q_noul("first", "Is the first listed option correct?",
|
| 201 |
+
float(k == 0)))
|
| 202 |
+
return state, qs
|
| 203 |
+
|
| 204 |
+
|
| 205 |
+
def gen_hf_piqa():
|
| 206 |
+
r = _pick(_ds("piqa", "nthngdy/piqa", split="train",
|
| 207 |
+
cols=["goal", "sol1", "sol2", "label"]))
|
| 208 |
+
options, k = _shuffle_opts([_trunc(r["sol1"], 200),
|
| 209 |
+
_trunc(r["sol2"], 200)], int(r["label"]))
|
| 210 |
+
state = f"Goal: {_trunc(r['goal'], 200)}"
|
| 211 |
+
return state, [q_choice("sol", "Which solution achieves the goal?",
|
| 212 |
+
options, _onehot(2, k))]
|
| 213 |
+
|
| 214 |
+
|
| 215 |
+
# ---------------------------------------------------------------------------
|
| 216 |
+
# topic classification -> choice
|
| 217 |
+
# ---------------------------------------------------------------------------
|
| 218 |
+
|
| 219 |
+
_AGN_Q = ["Which topic does this article belong to?",
|
| 220 |
+
"Classify this news article.", "Pick the correct news category.",
|
| 221 |
+
"What is the subject of this article?"]
|
| 222 |
+
_DBP_Q = ["Which category does this entity belong to?",
|
| 223 |
+
"Classify this description.", "Pick the correct category."]
|
| 224 |
+
_YAH_Q = ["Which topic best fits this question?",
|
| 225 |
+
"Classify this question by subject.",
|
| 226 |
+
"Pick the most appropriate topic."]
|
| 227 |
+
_EMO_Q = ["Which emotion does this text express?",
|
| 228 |
+
"Classify the emotion in this text.",
|
| 229 |
+
"Pick the dominant emotion."]
|
| 230 |
+
_FIN_Q = ["Classify the sentiment of this financial text.",
|
| 231 |
+
"Is this text bearish, neutral or bullish?",
|
| 232 |
+
"Pick the sentiment of this finance post."]
|
| 233 |
+
_BANK_Q = ["Which banking intent does this query express?",
|
| 234 |
+
"Classify this customer-service query.",
|
| 235 |
+
"Pick the correct intent label."]
|
| 236 |
+
|
| 237 |
+
|
| 238 |
+
def gen_hf_ag_news():
|
| 239 |
+
r = _pick(_ds("ag_news", "fancyzhx/ag_news", split="train",
|
| 240 |
+
cols=["text", "label"]))
|
| 241 |
+
classes = ["world", "sports", "business", "sci/tech"]
|
| 242 |
+
state = f"News article: {_trunc(r['text'], 450)}"
|
| 243 |
+
qs = [q_choice("topic", rng.choice(_AGN_Q), classes,
|
| 244 |
+
_onehot(4, int(r["label"])))]
|
| 245 |
+
if rng.random() < 0.35:
|
| 246 |
+
c = rng.randrange(4)
|
| 247 |
+
qs.append(q_noul("is", f"Is this a {classes[c]} article?",
|
| 248 |
+
float(int(r["label"]) == c)))
|
| 249 |
+
return state, qs
|
| 250 |
+
|
| 251 |
+
|
| 252 |
+
def gen_hf_dbpedia():
|
| 253 |
+
r = _pick(_ds("dbpedia", "fancyzhx/dbpedia_14", split="train",
|
| 254 |
+
cols=["title", "content", "label"]))
|
| 255 |
+
classes = ["company", "educational institution", "artist", "athlete",
|
| 256 |
+
"office holder", "mean of transportation", "building",
|
| 257 |
+
"natural place", "village", "animal", "plant", "album",
|
| 258 |
+
"film", "written work"]
|
| 259 |
+
state = (f"Title: {_trunc(r['title'], 80)}\n"
|
| 260 |
+
f"Description: {_trunc(r['content'], 350)}")
|
| 261 |
+
return state, [q_choice("cat", rng.choice(_DBP_Q), classes,
|
| 262 |
+
_onehot(14, int(r["label"])))]
|
| 263 |
+
|
| 264 |
+
|
| 265 |
+
def gen_hf_yahoo():
|
| 266 |
+
r = _pick(_ds("yahoo", "community-datasets/yahoo_answers_topics",
|
| 267 |
+
split="train",
|
| 268 |
+
cols=["question_title", "question_content", "topic"]))
|
| 269 |
+
classes = ["society & culture", "science & math", "health",
|
| 270 |
+
"education & reference", "computers & internet", "sports",
|
| 271 |
+
"business & finance", "entertainment & music",
|
| 272 |
+
"family & relationships", "politics & government"]
|
| 273 |
+
state = (f"Question: {_trunc(r['question_title'], 120)}\n"
|
| 274 |
+
f"Details: {_trunc(r['question_content'], 280)}")
|
| 275 |
+
return state, [q_choice("topic", rng.choice(_YAH_Q), classes,
|
| 276 |
+
_onehot(10, int(r["topic"])))]
|
| 277 |
+
|
| 278 |
+
|
| 279 |
+
def gen_hf_emotion():
|
| 280 |
+
r = _pick(_ds("emotion", "dair-ai/emotion", split="train",
|
| 281 |
+
cols=["text", "label"]))
|
| 282 |
+
classes = ["sadness", "joy", "love", "anger", "fear", "surprise"]
|
| 283 |
+
lab = int(r["label"])
|
| 284 |
+
state = f"Text: {_trunc(r['text'], 350)}"
|
| 285 |
+
qs = [q_choice("emo", rng.choice(_EMO_Q), classes, _onehot(6, lab))]
|
| 286 |
+
if rng.random() < 0.4: # multi-probe on same state
|
| 287 |
+
c = rng.randrange(6)
|
| 288 |
+
qs.append(q_noul("is", f"Does this text express {classes[c]}?",
|
| 289 |
+
float(lab == c)))
|
| 290 |
+
return state, qs
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
def gen_hf_financial():
|
| 294 |
+
r = _pick(_ds("finpb", "zeroshot/twitter-financial-news-sentiment",
|
| 295 |
+
split="train", cols=["text", "label"]))
|
| 296 |
+
classes = ["bearish", "neutral", "bullish"]
|
| 297 |
+
lab = int(r["label"])
|
| 298 |
+
state = f"Financial tweet: {_trunc(r['text'], 300)}"
|
| 299 |
+
qs = [q_choice("sent", rng.choice(_FIN_Q), classes, _onehot(3, lab))]
|
| 300 |
+
if rng.random() < 0.35:
|
| 301 |
+
qs.append(q_noul("neg", "Is this text bearish in tone?",
|
| 302 |
+
float(lab == 0)))
|
| 303 |
+
return state, qs
|
| 304 |
+
|
| 305 |
+
|
| 306 |
+
def gen_hf_banking77():
|
| 307 |
+
rows = _ds("banking77", "mteb/banking77", split="train",
|
| 308 |
+
cols=["text", "label", "label_text"])
|
| 309 |
+
r = _pick(rows)
|
| 310 |
+
lab = int(r["label"])
|
| 311 |
+
true_name = str(r["label_text"]).replace("_", " ")
|
| 312 |
+
# full 77-way option set is a stress test; 12-way subset keeps length sane
|
| 313 |
+
pool = list({str(x["label_text"]).replace("_", " ")
|
| 314 |
+
for x in rng.sample(rows, min(60, len(rows)))} - {true_name})
|
| 315 |
+
options = rng.sample(pool, min(11, len(pool))) + [true_name]
|
| 316 |
+
options, k = _shuffle_opts(options, len(options) - 1)
|
| 317 |
+
state = f"Customer query: {_trunc(r['text'], 250)}"
|
| 318 |
+
return state, [q_choice("intent", rng.choice(_BANK_Q), options,
|
| 319 |
+
_onehot(len(options), k))]
|
| 320 |
+
|
| 321 |
+
|
| 322 |
+
# ---------------------------------------------------------------------------
|
| 323 |
+
# binary -> noul
|
| 324 |
+
# ---------------------------------------------------------------------------
|
| 325 |
+
|
| 326 |
+
def _binary_gen(key, loader, state_fmt, qlist, second=None, extra=None):
|
| 327 |
+
r = _pick(_ds(key, *loader["args"], **loader["kw"]))
|
| 328 |
+
y = float(r["label"])
|
| 329 |
+
state = state_fmt(r)
|
| 330 |
+
instr, pol = qlist[rng.randrange(len(qlist))]
|
| 331 |
+
qs = [q_noul("y", instr, y if pol else 1.0 - y)]
|
| 332 |
+
if second and rng.random() < 0.35:
|
| 333 |
+
qs.append(q_choice("cls", second[0], second[1],
|
| 334 |
+
[1.0 - y if pol else y, y if pol else 1.0 - y]))
|
| 335 |
+
if extra and rng.random() < 0.4:
|
| 336 |
+
q = extra(r)
|
| 337 |
+
if q:
|
| 338 |
+
qs.append(q)
|
| 339 |
+
return state, qs
|
| 340 |
+
|
| 341 |
+
|
| 342 |
+
_SST2_Q = [("Is this review positive?", 1), ("Does this review express praise?", 1),
|
| 343 |
+
("Is this a favorable review?", 1), ("Is this review negative?", 0)]
|
| 344 |
+
_COLA_Q = [("Is this sentence grammatically acceptable?", 1),
|
| 345 |
+
("Would a native speaker find this sentence grammatical?", 1),
|
| 346 |
+
("Is this sentence ungrammatical?", 0)]
|
| 347 |
+
_MRPC_Q = [("Are these two sentences paraphrases?", 1),
|
| 348 |
+
("Do these sentences mean the same thing?", 1),
|
| 349 |
+
("Are these sentences semantically equivalent?", 1)]
|
| 350 |
+
_QNLI_Q = [("Does the sentence answer the question?", 1),
|
| 351 |
+
("Is the question answered by the sentence?", 1),
|
| 352 |
+
("Does the sentence fail to answer the question?", 0)]
|
| 353 |
+
_RTE_Q = [("Does the premise entail the hypothesis?", 1),
|
| 354 |
+
("Is the hypothesis supported by the premise?", 1),
|
| 355 |
+
("Can we conclude the hypothesis from the premise?", 1)]
|
| 356 |
+
_WNLI_Q = [("Does the first sentence entail the second?", 1),
|
| 357 |
+
("Is the second sentence implied by the first?", 1)]
|
| 358 |
+
_IMDB_Q = [("Is this movie review positive?", 1),
|
| 359 |
+
("Did the reviewer like this film?", 1),
|
| 360 |
+
("Is this review negative?", 0)]
|
| 361 |
+
_RT_Q = [("Is this review positive?", 1), ("Is this a fresh review?", 1),
|
| 362 |
+
("Is this review rotten?", 0)]
|
| 363 |
+
_AMZ_Q = [("Is this product review positive?", 1),
|
| 364 |
+
("Is the customer satisfied?", 1), ("Is this review negative?", 0)]
|
| 365 |
+
_PAWS_Q = [("Are these sentences paraphrases?", 1),
|
| 366 |
+
("Do these two sentences have the same meaning?", 1)]
|
| 367 |
+
_WIC_Q = [("Does the highlighted word mean the same in both sentences?", 1),
|
| 368 |
+
("Is the target word used with the same sense in both sentences?", 1)]
|
| 369 |
+
_BOOLQ_Q = [("Is the correct answer to the question 'yes'?", 1),
|
| 370 |
+
("Would the passage answer yes to this question?", 1)]
|
| 371 |
+
|
| 372 |
+
|
| 373 |
+
def gen_hf_sst2():
|
| 374 |
+
return _binary_gen("sst2", {"args": ("nyu-mll/glue", "sst2"),
|
| 375 |
+
"kw": {"split": "train",
|
| 376 |
+
"cols": ["sentence", "label"]}},
|
| 377 |
+
lambda r: f"Review excerpt: {_trunc(r['sentence'], 300)}",
|
| 378 |
+
_SST2_Q, second=("Classify the sentiment.",
|
| 379 |
+
["negative", "positive"]))
|
| 380 |
+
|
| 381 |
+
|
| 382 |
+
def gen_hf_cola():
|
| 383 |
+
return _binary_gen("cola", {"args": ("nyu-mll/glue", "cola"),
|
| 384 |
+
"kw": {"split": "train",
|
| 385 |
+
"cols": ["sentence", "label"]}},
|
| 386 |
+
lambda r: f"Sentence: {_trunc(r['sentence'], 250)}",
|
| 387 |
+
_COLA_Q)
|
| 388 |
+
|
| 389 |
+
|
| 390 |
+
def gen_hf_mrpc():
|
| 391 |
+
return _binary_gen(
|
| 392 |
+
"mrpc", {"args": ("nyu-mll/glue", "mrpc"),
|
| 393 |
+
"kw": {"split": "train",
|
| 394 |
+
"cols": ["sentence1", "sentence2", "label"]}},
|
| 395 |
+
lambda r: (f"Sentence A: {_trunc(r['sentence1'], 250)}\n"
|
| 396 |
+
f"Sentence B: {_trunc(r['sentence2'], 250)}"),
|
| 397 |
+
_MRPC_Q)
|
| 398 |
+
|
| 399 |
+
|
| 400 |
+
def gen_hf_qnli():
|
| 401 |
+
return _binary_gen(
|
| 402 |
+
"qnli", {"args": ("nyu-mll/glue", "qnli"),
|
| 403 |
+
"kw": {"split": "train",
|
| 404 |
+
"cols": ["question", "sentence", "label"]}},
|
| 405 |
+
lambda r: (f"Question: {_trunc(r['question'], 200)}\n"
|
| 406 |
+
f"Sentence: {_trunc(r['sentence'], 300)}"),
|
| 407 |
+
_QNLI_Q)
|
| 408 |
+
|
| 409 |
+
|
| 410 |
+
def gen_hf_rte():
|
| 411 |
+
return _binary_gen(
|
| 412 |
+
"rte", {"args": ("nyu-mll/glue", "rte"),
|
| 413 |
+
"kw": {"split": "train",
|
| 414 |
+
"cols": ["premise", "hypothesis", "label"]}},
|
| 415 |
+
lambda r: (f"Premise: {_trunc(r['premise'], 300)}\n"
|
| 416 |
+
f"Hypothesis: {_trunc(r['hypothesis'], 200)}"),
|
| 417 |
+
_RTE_Q)
|
| 418 |
+
|
| 419 |
+
|
| 420 |
+
def gen_hf_wnli():
|
| 421 |
+
return _binary_gen(
|
| 422 |
+
"wnli", {"args": ("nyu-mll/glue", "wnli"),
|
| 423 |
+
"kw": {"split": "train",
|
| 424 |
+
"cols": ["sentence1", "sentence2", "label"]}},
|
| 425 |
+
lambda r: (f"Sentence 1: {_trunc(r['sentence1'], 250)}\n"
|
| 426 |
+
f"Sentence 2: {_trunc(r['sentence2'], 250)}"),
|
| 427 |
+
_WNLI_Q)
|
| 428 |
+
|
| 429 |
+
|
| 430 |
+
def gen_hf_imdb():
|
| 431 |
+
return _binary_gen("stanfordnlp/imdb", {"args": ("stanfordnlp/imdb",),
|
| 432 |
+
"kw": {"split": "train",
|
| 433 |
+
"cols": ["text", "label"]}},
|
| 434 |
+
lambda r: f"Movie review: {_trunc(r['text'], 600)}",
|
| 435 |
+
_IMDB_Q, second=("Classify the review.",
|
| 436 |
+
["negative", "positive"]))
|
| 437 |
+
|
| 438 |
+
|
| 439 |
+
def gen_hf_rotten():
|
| 440 |
+
return _binary_gen("rt", {"args": ("cornell-movie-review-data/rotten_tomatoes",),
|
| 441 |
+
"kw": {"split": "train",
|
| 442 |
+
"cols": ["text", "label"]}},
|
| 443 |
+
lambda r: f"Review: {_trunc(r['text'], 300)}",
|
| 444 |
+
_RT_Q, second=("Classify the review.",
|
| 445 |
+
["rotten", "fresh"]))
|
| 446 |
+
|
| 447 |
+
|
| 448 |
+
def gen_hf_amazon():
|
| 449 |
+
return _binary_gen("amz", {"args": ("fancyzhx/amazon_polarity",),
|
| 450 |
+
"kw": {"split": "train",
|
| 451 |
+
"cols": ["content", "label"]}},
|
| 452 |
+
lambda r: f"Product review: {_trunc(r['content'], 500)}",
|
| 453 |
+
_AMZ_Q, second=("Classify the review.",
|
| 454 |
+
["negative", "positive"]))
|
| 455 |
+
|
| 456 |
+
|
| 457 |
+
def gen_hf_paws():
|
| 458 |
+
return _binary_gen(
|
| 459 |
+
"paws", {"args": ("google-research-datasets/paws", "labeled_final"),
|
| 460 |
+
"kw": {"split": "train",
|
| 461 |
+
"cols": ["sentence1", "sentence2", "label"]}},
|
| 462 |
+
lambda r: (f"Sentence A: {_trunc(r['sentence1'], 250)}\n"
|
| 463 |
+
f"Sentence B: {_trunc(r['sentence2'], 250)}"),
|
| 464 |
+
_PAWS_Q)
|
| 465 |
+
|
| 466 |
+
|
| 467 |
+
def gen_hf_wic():
|
| 468 |
+
def st(r):
|
| 469 |
+
w = r["word"]
|
| 470 |
+
return (f"Word: '{w}'\nSentence A: {_trunc(r['sentence1'], 220)}\n"
|
| 471 |
+
f"Sentence B: {_trunc(r['sentence2'], 220)}")
|
| 472 |
+
return _binary_gen(
|
| 473 |
+
"wic", {"args": ("aps/super_glue", "wic"),
|
| 474 |
+
"kw": {"split": "train",
|
| 475 |
+
"cols": ["word", "sentence1", "sentence2", "label"]}},
|
| 476 |
+
st, _WIC_Q)
|
| 477 |
+
|
| 478 |
+
|
| 479 |
+
def gen_hf_boolq():
|
| 480 |
+
r = _pick(_ds("boolq", "aps/super_glue", "boolq", split="train",
|
| 481 |
+
cols=["question", "passage", "label"]))
|
| 482 |
+
y = float(r["label"])
|
| 483 |
+
state = (f"Passage: {_trunc(r['passage'], 500)}\n"
|
| 484 |
+
f"Question: {_trunc(r['question'], 200)}")
|
| 485 |
+
instr, pol = _BOOLQ_Q[rng.randrange(len(_BOOLQ_Q))]
|
| 486 |
+
qs = [q_noul("ans", instr, y if pol else 1.0 - y)]
|
| 487 |
+
if rng.random() < 0.3:
|
| 488 |
+
qs.append(q_choice("yn", "What is the correct answer?",
|
| 489 |
+
["yes", "no"], [y, 1.0 - y]))
|
| 490 |
+
return state, qs
|
| 491 |
+
|
| 492 |
+
|
| 493 |
+
# ---------------------------------------------------------------------------
|
| 494 |
+
# NLI -> choice + shared-state probes
|
| 495 |
+
# ---------------------------------------------------------------------------
|
| 496 |
+
|
| 497 |
+
_NLI_Q = ["What is the relationship between premise and hypothesis?",
|
| 498 |
+
"Classify the entailment relation.",
|
| 499 |
+
"Does the premise entail, contradict, or leave neutral the hypothesis?"]
|
| 500 |
+
|
| 501 |
+
|
| 502 |
+
def _nli_gen(key, cfg):
|
| 503 |
+
rows = _ds(key, *cfg[0], **cfg[1])
|
| 504 |
+
while True:
|
| 505 |
+
r = _pick(rows)
|
| 506 |
+
lab = int(r["label"])
|
| 507 |
+
if lab >= 0: # snli has -1 unlabeled rows
|
| 508 |
+
break
|
| 509 |
+
classes = ["entailment", "neutral", "contradiction"]
|
| 510 |
+
state = (f"Premise: {_trunc(r['premise'], 350)}\n"
|
| 511 |
+
f"Hypothesis: {_trunc(r['hypothesis'], 180)}")
|
| 512 |
+
qs = [q_choice("rel", rng.choice(_NLI_Q), classes, _onehot(3, lab))]
|
| 513 |
+
# shared-state probes: ask about the other relations on the SAME state
|
| 514 |
+
probes = rng.sample(range(3), k=rng.choice([1, 2]))
|
| 515 |
+
for i, p in enumerate(probes):
|
| 516 |
+
qs.append(q_noul(
|
| 517 |
+
f"is{p}", f"Is the relation '{classes[p]}'?",
|
| 518 |
+
float(lab == p)))
|
| 519 |
+
return state, qs
|
| 520 |
+
|
| 521 |
+
|
| 522 |
+
def gen_hf_snli():
|
| 523 |
+
return _nli_gen("snli", (("stanfordnlp/snli",), {"split": "train",
|
| 524 |
+
"cols": ["premise", "hypothesis",
|
| 525 |
+
"label"]}))
|
| 526 |
+
|
| 527 |
+
|
| 528 |
+
def gen_hf_mnli():
|
| 529 |
+
return _nli_gen("mnli", (("nyu-mll/glue", "mnli"), {"split": "train",
|
| 530 |
+
"cols": ["premise",
|
| 531 |
+
"hypothesis",
|
| 532 |
+
"label"]}))
|
| 533 |
+
|
| 534 |
+
|
| 535 |
+
def gen_hf_goemotions():
|
| 536 |
+
rows = _ds("goemo", "google-research-datasets/go_emotions", "simplified",
|
| 537 |
+
split="train", cols=["text", "labels"])
|
| 538 |
+
if not rows:
|
| 539 |
+
raise ValueError("empty")
|
| 540 |
+
r = _pick(rows)
|
| 541 |
+
labs = set(r["labels"]) or {27} # empty -> neutral (id 27)
|
| 542 |
+
names = ["admiration", "amusement", "anger", "annoyance", "approval",
|
| 543 |
+
"caring", "confusion", "curiosity", "desire", "disappointment",
|
| 544 |
+
"disapproval", "disgust", "embarrassment", "excitement", "fear",
|
| 545 |
+
"gratitude", "grief", "joy", "love", "nervousness", "optimism",
|
| 546 |
+
"pride", "realization", "relief", "remorse", "sadness",
|
| 547 |
+
"surprise", "neutral"]
|
| 548 |
+
state = f"Text: {_trunc(r['text'], 350)}"
|
| 549 |
+
qs = []
|
| 550 |
+
pos = rng.choice(sorted(labs))
|
| 551 |
+
negs = rng.sample([i for i in range(28) if i not in labs],
|
| 552 |
+
k=rng.choice([1, 2]))
|
| 553 |
+
for i, p in enumerate([pos] + negs):
|
| 554 |
+
qs.append(q_noul(f"e{i}",
|
| 555 |
+
f"Does this text express {names[p]}?",
|
| 556 |
+
float(p in labs)))
|
| 557 |
+
if rng.random() < 0.5:
|
| 558 |
+
c = rng.choice(sorted(labs))
|
| 559 |
+
qs.append(q_noul("cls", f"Is {names[c]} among the emotions?",
|
| 560 |
+
1.0))
|
| 561 |
+
return state, qs
|
| 562 |
+
|
| 563 |
+
|
| 564 |
+
# ---------------------------------------------------------------------------
|
| 565 |
+
# ordinal -> score (5 levels)
|
| 566 |
+
# ---------------------------------------------------------------------------
|
| 567 |
+
|
| 568 |
+
_STAR_LV = ["1 star", "2 stars", "3 stars", "4 stars", "5 stars"]
|
| 569 |
+
_YELP_Q = ["How many stars did the reviewer give?",
|
| 570 |
+
"Rate this review from 1 to 5 stars.",
|
| 571 |
+
"What star rating does this review correspond to?"]
|
| 572 |
+
_SST5_LV = ["very negative", "negative", "neutral", "positive",
|
| 573 |
+
"very positive"]
|
| 574 |
+
_SST5_Q = ["Classify the sentiment on a five-point scale.",
|
| 575 |
+
"Rate the sentiment of this sentence.",
|
| 576 |
+
"Pick the sentiment level."]
|
| 577 |
+
_APP_Q = ["How many stars did the user give this app?",
|
| 578 |
+
"Rate this app review from 1 to 5 stars.",
|
| 579 |
+
"What star rating does this review show?"]
|
| 580 |
+
|
| 581 |
+
|
| 582 |
+
def gen_hf_yelp():
|
| 583 |
+
r = _pick(_ds("yelp", "Yelp/yelp_review_full", split="train",
|
| 584 |
+
cols=["text", "label"]))
|
| 585 |
+
lab = int(r["label"]) # 0-4 -> 1-5 stars
|
| 586 |
+
state = f"Yelp review: {_trunc(r['text'], 500)}"
|
| 587 |
+
qs = [q_score("stars", rng.choice(_YELP_Q), _STAR_LV, _onehot(5, lab))]
|
| 588 |
+
if rng.random() < 0.3:
|
| 589 |
+
qs.append(q_noul("pos", "Is this a positive review (4-5 stars)?",
|
| 590 |
+
float(lab >= 3)))
|
| 591 |
+
return state, qs
|
| 592 |
+
|
| 593 |
+
|
| 594 |
+
def gen_hf_sst5():
|
| 595 |
+
r = _pick(_ds("sst5", "SetFit/sst5", split="train",
|
| 596 |
+
cols=["text", "label"]))
|
| 597 |
+
lab = int(r["label"])
|
| 598 |
+
state = f"Sentence: {_trunc(r['text'], 300)}"
|
| 599 |
+
return state, [q_score("sent", rng.choice(_SST5_Q), _SST5_LV,
|
| 600 |
+
_onehot(5, lab))]
|
| 601 |
+
|
| 602 |
+
|
| 603 |
+
def gen_hf_app_reviews():
|
| 604 |
+
r = _pick(_ds("apprev", "sealuzh/app_reviews", split="train",
|
| 605 |
+
cols=["review", "star"]))
|
| 606 |
+
lab = int(r["star"]) - 1 # 1-5 -> 0-4
|
| 607 |
+
state = f"App store review: {_trunc(r['review'], 400)}"
|
| 608 |
+
qs = [q_score("stars", rng.choice(_APP_Q), _STAR_LV, _onehot(5, lab))]
|
| 609 |
+
if rng.random() < 0.3:
|
| 610 |
+
qs.append(q_noul("pos", "Is this a positive review (4-5 stars)?",
|
| 611 |
+
float(lab >= 3)))
|
| 612 |
+
return state, qs
|
| 613 |
+
|
| 614 |
+
|
| 615 |
+
# ---------------------------------------------------------------------------
|
| 616 |
+
# continuous -> regress
|
| 617 |
+
# ---------------------------------------------------------------------------
|
| 618 |
+
|
| 619 |
+
def _bucket_quantiles(lo, hi):
|
| 620 |
+
"""19 quantiles of Uniform(lo, hi)."""
|
| 621 |
+
taus = [0.05 + 0.05 * i for i in range(19)]
|
| 622 |
+
return [lo + t * (hi - lo) for t in taus]
|
| 623 |
+
|
| 624 |
+
|
| 625 |
+
def gen_hf_stsb():
|
| 626 |
+
r = _pick(_ds("stsb", "nyu-mll/glue", "stsb", split="train",
|
| 627 |
+
cols=["sentence1", "sentence2", "label"]))
|
| 628 |
+
score = float(r["label"]) # 0-5 human similarity
|
| 629 |
+
state = (f"Sentence A: {_trunc(r['sentence1'], 250)}\n"
|
| 630 |
+
f"Sentence B: {_trunc(r['sentence2'], 250)}")
|
| 631 |
+
# model annotator disagreement as Normal(score, 0.6) clipped to [0,5]
|
| 632 |
+
sd = 0.6
|
| 633 |
+
taus = [0.05 + 0.05 * i for i in range(19)]
|
| 634 |
+
qs19 = [min(5.0, max(0.0, score + _ND.inv_cdf(t) * sd)) for t in taus]
|
| 635 |
+
qs = [q_regress("sim", "Predict the semantic similarity score (0-5).",
|
| 636 |
+
qs19, unit="similarity points")]
|
| 637 |
+
if rng.random() < 0.35:
|
| 638 |
+
thr = rng.choice([1.0, 2.0, 3.0, 4.0])
|
| 639 |
+
qs.append(q_noul("hi", f"Is the similarity above {thr}?",
|
| 640 |
+
float(score > thr)))
|
| 641 |
+
return state, qs
|
| 642 |
+
|
| 643 |
+
|
| 644 |
+
def gen_hf_yelp_reg():
|
| 645 |
+
"""Star rating -> regress on bucket uncertainty (uniform within star)."""
|
| 646 |
+
r = _pick(_ds("yelp", "Yelp/yelp_review_full", split="train",
|
| 647 |
+
cols=["text", "label"]))
|
| 648 |
+
lab = int(r["label"]) # 0-4
|
| 649 |
+
state = f"Yelp review: {_trunc(r['text'], 500)}"
|
| 650 |
+
lo, hi = float(lab), float(lab + 1)
|
| 651 |
+
qs = [q_regress("stars", "Predict the star rating (0-5 scale).",
|
| 652 |
+
_bucket_quantiles(lo, hi), unit="stars")]
|
| 653 |
+
return state, qs
|
| 654 |
+
|
| 655 |
+
|
| 656 |
+
# ---------------------------------------------------------------------------
|
| 657 |
+
# squad_v2 -> answerability noul
|
| 658 |
+
# ---------------------------------------------------------------------------
|
| 659 |
+
|
| 660 |
+
def gen_hf_squad2():
|
| 661 |
+
r = _pick(_ds("squad2", "rajpurkar/squad_v2", split="train",
|
| 662 |
+
cols=["context", "question", "answers"]))
|
| 663 |
+
answerable = len(r["answers"]["text"]) > 0
|
| 664 |
+
state = (f"Context: {_trunc(r['context'], 600)}\n"
|
| 665 |
+
f"Question: {_trunc(r['question'], 200)}")
|
| 666 |
+
qs = [q_noul("ans", "Is this question answerable from the context?",
|
| 667 |
+
float(answerable))]
|
| 668 |
+
if answerable and rng.random() < 0.4:
|
| 669 |
+
ans = r["answers"]["text"][0]
|
| 670 |
+
qs.append(q_noul("short", f"Is the answer short (under 40 chars)?",
|
| 671 |
+
float(len(ans) < 40)))
|
| 672 |
+
return state, qs
|
| 673 |
+
|
| 674 |
+
|
| 675 |
+
FAMILIES = {
|
| 676 |
+
# MCQ
|
| 677 |
+
"hf_sciq": gen_hf_sciq, "hf_arc_easy": gen_hf_arc_easy,
|
| 678 |
+
"hf_arc_chal": gen_hf_arc_challenge, "hf_obqa": gen_hf_openbookqa,
|
| 679 |
+
"hf_csqa": gen_hf_csqa, "hf_race": gen_hf_race,
|
| 680 |
+
"hf_mmlu": gen_hf_mmlu, "hf_hellaswag": gen_hf_hellaswag,
|
| 681 |
+
"hf_winogrande": gen_hf_winogrande, "hf_piqa": gen_hf_piqa,
|
| 682 |
+
# topic cls
|
| 683 |
+
"hf_ag_news": gen_hf_ag_news, "hf_dbpedia": gen_hf_dbpedia,
|
| 684 |
+
"hf_emotion": gen_hf_emotion, "hf_financial": gen_hf_financial,
|
| 685 |
+
"hf_banking77": gen_hf_banking77,
|
| 686 |
+
# binary
|
| 687 |
+
"hf_sst2": gen_hf_sst2, "hf_cola": gen_hf_cola,
|
| 688 |
+
"hf_mrpc": gen_hf_mrpc, "hf_qnli": gen_hf_qnli,
|
| 689 |
+
"hf_rte": gen_hf_rte, "hf_wnli": gen_hf_wnli,
|
| 690 |
+
"hf_imdb": gen_hf_imdb, "hf_rotten": gen_hf_rotten,
|
| 691 |
+
"hf_amazon": gen_hf_amazon, "hf_paws": gen_hf_paws,
|
| 692 |
+
"hf_wic": gen_hf_wic, "hf_boolq": gen_hf_boolq,
|
| 693 |
+
# NLI / multi-probe
|
| 694 |
+
"hf_snli": gen_hf_snli, "hf_mnli": gen_hf_mnli,
|
| 695 |
+
"hf_goemo": gen_hf_goemotions,
|
| 696 |
+
# ordinal
|
| 697 |
+
"hf_yelp": gen_hf_yelp, "hf_sst5": gen_hf_sst5,
|
| 698 |
+
"hf_app_rev": gen_hf_app_reviews,
|
| 699 |
+
# regress
|
| 700 |
+
"hf_stsb": gen_hf_stsb, "hf_yelp_reg": gen_hf_yelp_reg,
|
| 701 |
+
# answerability
|
| 702 |
+
"hf_squad2": gen_hf_squad2,
|
| 703 |
+
}
|
| 704 |
+
WEIGHTS = {k: 1.0 for k in FAMILIES}
|
| 705 |
+
WEIGHTS.update({"hf_squad2": 0.6, "hf_app_rev": 0.7, "hf_banking77": 0.6,
|
| 706 |
+
"hf_mmlu": 0.8, "hf_goemo": 0.8, "hf_yelp_reg": 0.5})
|
| 707 |
+
|
| 708 |
+
|
| 709 |
+
def make_rows(n, seed):
|
| 710 |
+
return generate(FAMILIES, WEIGHTS, n, seed, rng)
|
| 711 |
+
|
| 712 |
+
|
| 713 |
+
if __name__ == "__main__":
|
| 714 |
+
rows = make_rows(20, 0)
|
| 715 |
+
seen = {}
|
| 716 |
+
for r in rows:
|
| 717 |
+
validate_row(r)
|
| 718 |
+
seen[r["family"]] = seen.get(r["family"], 0) + 1
|
| 719 |
+
print(f"{len(rows)} rows OK")
|
| 720 |
+
print(seen)
|
| 721 |
+
|