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 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import numpy as np
2
+ from functools import partial
3
+
4
+ import random
5
+ from lighteval.tasks.lighteval_task import LightevalTaskConfig
6
+ from lighteval.tasks.requests import Doc
7
+ from lighteval.metrics.metrics import Metrics, SampleLevelMetric, MetricCategory, MetricUseCase, ExactMatches
8
+ from lighteval.metrics.dynamic_metrics import (
9
+ loglikelihood_acc_metric,
10
+ multilingual_quasi_exact_match_metric,
11
+ multilingual_quasi_f1_score_metric,
12
+ )
13
+ from lighteval.metrics.normalizations import LogProbCharNorm, LogProbPMINorm, LogProbTokenNorm
14
+ from lighteval.tasks.default_prompts import LETTER_INDICES
15
+ import lighteval.tasks.default_prompts as prompt
16
+ from lighteval.tasks.lighteval_task import LightevalTaskConfig
17
+ from lighteval.tasks.multilingual.adapters import (
18
+ agieval_adapter,
19
+ alghafa_adapter,
20
+ ceval_adapter,
21
+ get_m3exam_adapter,
22
+ get_mkqa_adapter,
23
+ sciqa_adapter,
24
+ thai_exams_adapter,
25
+ winogrand_adapter,
26
+ xcodah_adapter,
27
+ )
28
+ from lighteval.tasks.multilingual.utils.task_utils import get_metrics_for_formulation, normalize_subset
29
+ from lighteval.tasks.templates.boolq import get_boolq_prompt_function
30
+ from lighteval.tasks.templates.continuation import get_continuation_prompt_function
31
+ from lighteval.tasks.templates.copa import get_copa_prompt_function
32
+ from lighteval.tasks.templates.hellaswag import get_hellaswag_prompt_function
33
+ from lighteval.tasks.templates.multichoice import get_mcq_prompt_function
34
+ from lighteval.tasks.templates.nli import get_nli_prompt_function
35
+ from lighteval.tasks.templates.qa import get_qa_prompt_function
36
+ from lighteval.tasks.templates.utils.formulation import (
37
+ CFFormulation,
38
+ HybridFormulation,
39
+ MCFFormulation,
40
+ )
41
+ from lighteval.utils.language import Language
42
+
43
+ from lighteval.tasks.multilingual.tasks import TASKS_TABLE as ML_TASKS_TABLE
44
+ from .math_utils import parse_math_answer
45
+
46
+ TASKS_TABLE = []
47
+
48
+ TASKS_TABLE.extend(ML_TASKS_TABLE)
49
+
50
+ def bbh_prompt(line, task_name: str = None):
51
+ return Doc(
52
+ task_name=task_name,
53
+ query="Question: " + line["input"] + "\nAnswer: ",
54
+ choices=[line["target"]],
55
+ gold_index=0,
56
+ )
57
+
58
+ def prompt_math(line, task_name: str = None):
59
+ return Doc(
60
+ task_name=task_name,
61
+ query=f"{line['problem']}\nPlease reason step by step, and put your final answer within \\boxed{{}}.\n\n",
62
+ gold_index=0,
63
+ choices=[f"{line['solution']}\n\n"],
64
+ )
65
+
66
+ def gpqa(line, task_name: str = None):
67
+ # Prompt template from simple-evals: https://github.com/openai/simple-evals/blob/83ed7640a7d9cd26849bcb3340125002ef14abbe/common.py#L14
68
+ GPQA_QUERY_TEMPLATE = """
69
+ Answer the following multiple choice question. The last line of your response should be of the following format: 'Answer: $LETTER' (without quotes) where LETTER is one of ABCD. Think step by step before answering.
70
+
71
+ {Question}
72
+
73
+ A) {A}
74
+ B) {B}
75
+ C) {C}
76
+ D) {D}
77
+ """.strip()
78
+ gold_index = random.randint(0, 3)
79
+ choices = [line["Incorrect Answer 1"], line["Incorrect Answer 2"], line["Incorrect Answer 3"]]
80
+ choices.insert(gold_index, line["Correct Answer"])
81
+
82
+ query = GPQA_QUERY_TEMPLATE.format(
83
+ A=choices[0], B=choices[1], C=choices[2], D=choices[3], Question=line["Question"]
84
+ )
85
+
86
+ return Doc(
87
+ task_name=task_name,
88
+ query=query,
89
+ choices=LETTER_INDICES[: len(choices)],
90
+ gold_index=gold_index,
91
+ instruction=query,
92
+ )
93
+
94
+ arc_tasks = [
95
+ LightevalTaskConfig(
96
+ name=f"arc_{formulation.name.lower()}:{subset.lower()}",
97
+ prompt_function=get_mcq_prompt_function(
98
+ Language.ENGLISH,
99
+ lambda line: {
100
+ "question": line["question"],
101
+ "choices": line["choices"]["text"],
102
+ "gold_idx": int(line["answerKey"]) - 1
103
+ if line["answerKey"].isdigit()
104
+ else LETTER_INDICES.index(line["answerKey"]),
105
+ },
106
+ formulation=formulation,
107
+ ),
108
+ suite=("custom",),
109
+ hf_repo="allenai/ai2_arc",
110
+ hf_subset=f"ARC-{subset}",
111
+ hf_revision="210d026faf9955653af8916fad021475a3f00453",
112
+ trust_dataset=True,
113
+ evaluation_splits=("test",),
114
+ few_shots_split="train",
115
+ metric=get_metrics_for_formulation(
116
+ formulation,
117
+ [
118
+ loglikelihood_acc_metric(normalization=LogProbTokenNorm()),
119
+ loglikelihood_acc_metric(normalization=LogProbCharNorm()),
120
+ loglikelihood_acc_metric(normalization=LogProbPMINorm()),
121
+ ],
122
+ ),
123
+ )
124
+ for subset in ["Easy", "Challenge"]
125
+ for formulation in [
126
+ MCFFormulation(),
127
+ CFFormulation(),
128
+ HybridFormulation(),
129
+ ]
130
+ ]
131
+
132
+ TASKS_TABLE.extend(arc_tasks)
133
+
134
+ hellaswag_tasks = [
135
+ LightevalTaskConfig(
136
+ name=f"hellaswag_{formulation.name.lower()}",
137
+ suite=["custom"],
138
+ prompt_function=get_hellaswag_prompt_function(
139
+ language=Language.ENGLISH,
140
+ adapter=lambda line: {
141
+ "activity_label": line["activity_label"],
142
+ "ctx_a": line["ctx_a"],
143
+ "ctx_b": line["ctx_b"],
144
+ "continuations": line["endings"],
145
+ "gold_idx": int(line["label"]),
146
+ },
147
+ formulation=formulation,
148
+ ),
149
+ hf_repo="Rowan/hellaswag",
150
+ hf_subset="default",
151
+ hf_revision="6002345709e0801764318f06bf06ce1e7d1a1fe3",
152
+ evaluation_splits=["validation"],
153
+ hf_avail_splits=["validation"],
154
+ metric=get_metrics_for_formulation(
155
+ formulation,
156
+ [
157
+ loglikelihood_acc_metric(normalization=LogProbTokenNorm()),
158
+ loglikelihood_acc_metric(normalization=LogProbCharNorm()),
159
+ ],
160
+ ),
161
+ trust_dataset=True,
162
+ )
163
+ for formulation in [MCFFormulation(), CFFormulation(), HybridFormulation()]
164
+ ]
165
+
166
+ TASKS_TABLE.extend(hellaswag_tasks)
167
+
168
+ commonsense_qa_tasks = [
169
+ LightevalTaskConfig(
170
+ name=f"commonsenseqa_{formulation.name.lower()}",
171
+ prompt_function=get_mcq_prompt_function(
172
+ Language.ENGLISH,
173
+ lambda line: {
174
+ "question": line["question"],
175
+ "choices": line["choices"]["text"],
176
+ "gold_idx": line["choices"]["label"].index(line["answerKey"].strip()),
177
+ },
178
+ formulation=formulation,
179
+ ),
180
+ suite=("custom",),
181
+ hf_repo="tau/commonsense_qa",
182
+ hf_subset="default",
183
+ hf_revision="94630fe30dad47192a8546eb75f094926d47e155",
184
+ metric=get_metrics_for_formulation(
185
+ formulation,
186
+ [
187
+ loglikelihood_acc_metric(normalization=LogProbTokenNorm()),
188
+ loglikelihood_acc_metric(normalization=LogProbCharNorm()),
189
+ loglikelihood_acc_metric(normalization=LogProbPMINorm()),
190
+ ],
191
+ ),
192
+ )
193
+ for formulation in [
194
+ MCFFormulation(),
195
+ CFFormulation(),
196
+ HybridFormulation(),
197
+ ]
198
+ ]
199
+
200
+ TASKS_TABLE.extend(commonsense_qa_tasks)
201
+
202
+ openbook_qa_tasks = [
203
+ LightevalTaskConfig(
204
+ name=f"openbookqa_{formulation.name.lower()}",
205
+ prompt_function=get_mcq_prompt_function(
206
+ Language.ENGLISH,
207
+ lambda line: {
208
+ "question": line["question_stem"],
209
+ "choices": line["choices"]["text"],
210
+ "gold_idx": LETTER_INDICES.index(line["answerKey"]),
211
+ },
212
+ formulation=formulation,
213
+ ),
214
+ suite=["custom"],
215
+ hf_repo="allenai/openbookqa",
216
+ hf_subset="main",
217
+ hf_revision="388097ea7776314e93a529163e0fea805b8a6454",
218
+ metric=get_metrics_for_formulation(
219
+ formulation,
220
+ [
221
+ loglikelihood_acc_metric(normalization=LogProbTokenNorm()),
222
+ loglikelihood_acc_metric(normalization=LogProbCharNorm()),
223
+ ],
224
+ ),
225
+ )
226
+ for formulation in [
227
+ MCFFormulation(),
228
+ CFFormulation(),
229
+ HybridFormulation(),
230
+ ]
231
+ ]
232
+
233
+ TASKS_TABLE.extend(openbook_qa_tasks)
234
+
235
+ winogrande_tasks = [
236
+ LightevalTaskConfig(
237
+ name=f"winogrande_{formulation.name.lower()}",
238
+ suite=("custom",),
239
+ prompt_function=get_continuation_prompt_function(
240
+ Language.ENGLISH, partial(winogrand_adapter, Language.ENGLISH), formulation=formulation
241
+ ),
242
+ hf_repo="allenai/winogrande",
243
+ hf_subset="winogrande_xl",
244
+ trust_dataset=True,
245
+ hf_revision="85ac5b5a3b7a930e22d590176e39460400d19e41",
246
+ metric=[
247
+ loglikelihood_acc_metric(normalization=None),
248
+ loglikelihood_acc_metric(normalization=LogProbTokenNorm()),
249
+ loglikelihood_acc_metric(normalization=LogProbCharNorm()),
250
+ ],
251
+ )
252
+ for formulation in [
253
+ MCFFormulation(),
254
+ CFFormulation(),
255
+ HybridFormulation(),
256
+ ]
257
+ ]
258
+
259
+ TASKS_TABLE.extend(winogrande_tasks)
260
+
261
+ piqa_tasks = [
262
+ LightevalTaskConfig(
263
+ name=f"piqa_{formulation.name.lower()}",
264
+ prompt_function=get_mcq_prompt_function(
265
+ Language.ENGLISH,
266
+ lambda line: {
267
+ "question": line["goal"],
268
+ "choices": [line['sol1'], line['sol2']],
269
+ "gold_idx": int(line["label"]),
270
+ },
271
+ formulation=formulation
272
+ ),
273
+ suite=["custom"],
274
+ hf_repo="ybisk/piqa",
275
+ hf_revision="2e8ac2dffd59bac8c3c6714948f4c551a0848bb0",
276
+ hf_subset="plain_text",
277
+ trust_dataset=True,
278
+ metric=get_metrics_for_formulation(
279
+ formulation,
280
+ [
281
+ loglikelihood_acc_metric(normalization=LogProbTokenNorm()),
282
+ loglikelihood_acc_metric(normalization=LogProbCharNorm()),
283
+ ],
284
+ ),
285
+ )
286
+ for formulation in [
287
+ MCFFormulation(),
288
+ CFFormulation(),
289
+ HybridFormulation(),
290
+ ]
291
+ ]
292
+
293
+ TASKS_TABLE.extend(piqa_tasks)
294
+
295
+
296
+ MMLU_SUBSETS = ['abstract_algebra', 'anatomy', 'astronomy', 'business_ethics', 'clinical_knowledge', 'college_biology', 'college_chemistry', 'college_computer_science', 'college_mathematics', 'college_medicine', 'college_physics', 'computer_security', 'conceptual_physics', 'econometrics', 'electrical_engineering', 'elementary_mathematics', 'formal_logic', 'global_facts', 'high_school_biology', 'high_school_chemistry', 'high_school_computer_science', 'high_school_european_history', 'high_school_geography', 'high_school_government_and_politics', 'high_school_macroeconomics', 'high_school_mathematics', 'high_school_microeconomics', 'high_school_physics', 'high_school_psychology', 'high_school_statistics', 'high_school_us_history', 'high_school_world_history', 'human_aging', 'human_sexuality', 'international_law', 'jurisprudence', 'logical_fallacies', 'machine_learning', 'management', 'marketing', 'medical_genetics', 'miscellaneous', 'moral_disputes', 'moral_scenarios', 'nutrition', 'philosophy', 'prehistory', 'professional_accounting', 'professional_law', 'professional_medicine', 'professional_psychology', 'public_relations', 'security_studies', 'sociology', 'us_foreign_policy', 'virology', 'world_religions']
297
+
298
+ mmlu_tasks = [
299
+ LightevalTaskConfig(
300
+ name=f"mmlu_{formulation.name.lower()}:{subset}",
301
+ prompt_function=get_mcq_prompt_function(
302
+ Language.ENGLISH,
303
+ lambda line: {
304
+ "question": line["question"],
305
+ "choices": line["choices"],
306
+ "gold_idx": int(line["answer"]),
307
+ },
308
+ formulation=formulation,
309
+ ),
310
+ suite=("custom",),
311
+ hf_repo="cais/mmlu",
312
+ hf_subset=subset,
313
+ hf_revision="c30699e8356da336a370243923dbaf21066bb9fe",
314
+ trust_dataset=True,
315
+ evaluation_splits=("test",),
316
+ few_shots_split="dev",
317
+ metric=get_metrics_for_formulation(
318
+ formulation,
319
+ [
320
+ loglikelihood_acc_metric(normalization=LogProbTokenNorm()),
321
+ loglikelihood_acc_metric(normalization=LogProbCharNorm()),
322
+ loglikelihood_acc_metric(normalization=LogProbPMINorm()),
323
+ ],
324
+ ),
325
+ )
326
+ for subset in MMLU_SUBSETS
327
+ for formulation in [
328
+ MCFFormulation(),
329
+ CFFormulation(),
330
+ HybridFormulation(),
331
+ ]
332
+ ]
333
+
334
+ TASKS_TABLE.extend(mmlu_tasks)
335
+
336
+ mmlu_pro_tasks = [
337
+ LightevalTaskConfig(
338
+ name=f"mmlu_pro_{formulation.name.lower()}",
339
+ prompt_function=get_mcq_prompt_function(
340
+ Language.ENGLISH,
341
+ lambda line: {
342
+ "question": line["question"],
343
+ "choices": line["options"],
344
+ "gold_idx": line["answer_index"],
345
+ },
346
+ formulation=formulation,
347
+ ),
348
+ suite=("custom",),
349
+ hf_repo="TIGER-Lab/MMLU-Pro",
350
+ hf_subset="default",
351
+ hf_revision="3373e0b32277875b8db2aa555a333b78a08477ea",
352
+ trust_dataset=True,
353
+ evaluation_splits=("test",),
354
+ few_shots_split="validation",
355
+ metric=get_metrics_for_formulation(
356
+ formulation,
357
+ [
358
+ loglikelihood_acc_metric(normalization=LogProbTokenNorm()),
359
+ loglikelihood_acc_metric(normalization=LogProbCharNorm()),
360
+ loglikelihood_acc_metric(normalization=LogProbPMINorm()),
361
+ ],
362
+ ),
363
+ )
364
+ for formulation in [
365
+ MCFFormulation(),
366
+ CFFormulation(),
367
+ HybridFormulation(),
368
+ ]
369
+ ]
370
+
371
+ TASKS_TABLE.extend(mmlu_pro_tasks)
372
+
373
+ gsm8k_tasks = [
374
+ LightevalTaskConfig(
375
+ name="gsm8k",
376
+ prompt_function=prompt.gsm8k,
377
+ suite=("custom",),
378
+ hf_repo="openai/gsm8k",
379
+ hf_subset="main",
380
+ hf_revision="e53f048856ff4f594e959d75785d2c2d37b678ee",
381
+ hf_avail_splits=["train", "test"],
382
+ evaluation_splits=["test"],
383
+ metric=[Metrics.quasi_exact_match_gsm8k],
384
+ generation_size=256,
385
+ stop_sequence=["Question:", "Question"],
386
+ few_shots_select="random_sampling_from_train",
387
+ )
388
+ ]
389
+
390
+ TASKS_TABLE.extend(gsm8k_tasks)
391
+
392
+ quasi_exact_match_math = SampleLevelMetric(
393
+ metric_name="qem",
394
+ sample_level_fn=ExactMatches(
395
+ strip_strings=True,
396
+ normalize_pred=lambda text: parse_math_answer(text, "math"),
397
+ normalize_gold=lambda text: parse_math_answer(text, "math")
398
+ ).compute,
399
+ category=MetricCategory.GENERATIVE,
400
+ use_case=MetricUseCase.MATH,
401
+ corpus_level_fn=np.mean,
402
+ higher_is_better=True,
403
+ )
404
+
405
+ GPQA_TASKS = [
406
+ LightevalTaskConfig(
407
+ name="gpqa",
408
+ suite=["lighteval"],
409
+ prompt_function=gpqa,
410
+ hf_repo="Idavidrein/gpqa",
411
+ hf_subset="gpqa_main",
412
+ hf_avail_splits=["train"],
413
+ evaluation_splits=["train"],
414
+ few_shots_split=None,
415
+ few_shots_select="random_sampling",
416
+ generation_size=1,
417
+ metric=[Metrics.loglikelihood_acc_single_token],
418
+ stop_sequence=["\n"],
419
+ trust_dataset=True,
420
+ version=0,
421
+ )
422
+ ]
423
+
424
+ TASKS_TABLE.extend(GPQA_TASKS)
425
+
426
+ MATH_TASKS = [
427
+ LightevalTaskConfig(
428
+ name="math",
429
+ prompt_function=prompt_math,
430
+ suite=["custom"],
431
+ hf_repo="HuggingFaceTB/math_tasks",
432
+ hf_subset="math",
433
+ hf_revision="3d34f1076f279000b9315583dcdacfd288898283",
434
+ hf_avail_splits=["train", "test", "demo"],
435
+ evaluation_splits=["test"],
436
+ metric=[quasi_exact_match_math],
437
+ generation_size=1024,
438
+ stop_sequence=["\n\n"],
439
+ few_shots_split="demo",
440
+ few_shots_select="sequential",
441
+ trust_dataset=True,
442
+ )
443
+ ]
444
+
445
+ TASKS_TABLE.extend(MATH_TASKS)
446
+
447
+ BBH_TASKS = [
448
+ LightevalTaskConfig(
449
+ name=f"bbh:{subset}",
450
+ prompt_function=bbh_prompt,
451
+ suite=["custom"],
452
+ hf_repo="lighteval/big_bench_hard",
453
+ hf_subset=subset,
454
+ hf_revision="80610173426f05e6f1448f047e2db4840a7dd899",
455
+ metric=[Metrics.exact_match],
456
+ hf_avail_splits=["train"],
457
+ # this is the only split available, obviously not used in training
458
+ evaluation_splits=["train"],
459
+ few_shots_split="train",
460
+ trust_dataset=True,
461
+ stop_sequence=["Question:", "Question"],
462
+ )
463
+ for subset in [
464
+ "boolean_expressions",
465
+ "causal_judgement",
466
+ "date_understanding",
467
+ "disambiguation_qa",
468
+ "dyck_languages",
469
+ "formal_fallacies",
470
+ "geometric_shapes",
471
+ "hyperbaton",
472
+ "logical_deduction_five_objects",
473
+ "logical_deduction_seven_objects",
474
+ "logical_deduction_three_objects",
475
+ "movie_recommendation",
476
+ "multistep_arithmetic_two",
477
+ "navigate",
478
+ "object_counting",
479
+ "penguins_in_a_table",
480
+ "reasoning_about_colored_objects",
481
+ "ruin_names",
482
+ "salient_translation_error_detection",
483
+ "snarks",
484
+ "sports_understanding",
485
+ "temporal_sequences",
486
+ "tracking_shuffled_objects_five_objects",
487
+ "tracking_shuffled_objects_seven_objects",
488
+ "tracking_shuffled_objects_three_objects",
489
+ "web_of_lies",
490
+ "word_sorting",
491
+ ]
492
+ ]
493
+
494
+ TASKS_TABLE.extend(BBH_TASKS)
495
+
496
+ # remove pmi_norm from all tasks to save on double inference
497
+ for task in TASKS_TABLE:
498
+ task.metric = [metric for metric in task.metric if metric.category != MetricCategory.MULTICHOICE_PMI]
499
+
500
+
501
+ if __name__ == "__main__":
502
+ print(t.name for t in TASKS_TABLE)
503
+ print(len(TASKS_TABLE))