|
| 1 | +""" |
| 2 | +Measuring Massive Multitask Language Understanding |
| 3 | +https://arxiv.org/pdf/2009.03300.pdf |
| 4 | +
|
| 5 | +The Hendryck's Test is a benchmark that measured a text model’s multitask accuracy. |
| 6 | +The test covers 57 tasks including elementary mathematics, US history, computer |
| 7 | +science, law, and more. To attain high accuracy on this test, models must possess |
| 8 | +extensive world knowledge and problem solving ability. By comprehensively evaluating |
| 9 | +the breadth and depth of a model’s academic and professional understanding, |
| 10 | +Hendryck's Test can be used to analyze models across many tasks and to identify |
| 11 | +important shortcomings. |
| 12 | +
|
| 13 | +Homepage: https://github.com/hendrycks/test |
| 14 | +""" |
| 15 | +from lm_eval.base import MultipleChoiceTask |
| 16 | +from . import get_mlmm_dataset_path |
| 17 | + |
| 18 | +_CITATION = """ |
| 19 | +@article{hendryckstest2021, |
| 20 | + title={Measuring Massive Multitask Language Understanding}, |
| 21 | + author={Dan Hendrycks and Collin Burns and Steven Basart and Andy Zou and Mantas Mazeika and Dawn Song and Jacob Steinhardt}, |
| 22 | + journal={Proceedings of the International Conference on Learning Representations (ICLR)}, |
| 23 | + year={2021} |
| 24 | +} |
| 25 | +""" |
| 26 | +LANGS = "ar,bn,ca,da,de,es,eu,fr,gu,hi,hr,hu,hy,id,it,kn,ml,mr,ne,nl,pt,ro,ru,sk,sr,sv,ta,te,uk,vi,zh".split( |
| 27 | + "," |
| 28 | +) |
| 29 | + |
| 30 | + |
| 31 | +def create_all_tasks(): |
| 32 | + """Creates a dictionary of tasks from a list of subjects |
| 33 | + :return: {task_name: task} |
| 34 | + e.g. {hendrycksTest-abstract_algebra: Task, hendrycksTest-anatomy: Task} |
| 35 | + """ |
| 36 | + return {f"mlmm_mmlu_{lang}": create_task(lang) for lang in LANGS} |
| 37 | + |
| 38 | + |
| 39 | +def create_task(lang): |
| 40 | + class HendrycksTest(GeneralHendrycksTest): |
| 41 | + def __init__(self): |
| 42 | + super().__init__(lang) |
| 43 | + |
| 44 | + return HendrycksTest |
| 45 | + |
| 46 | + |
| 47 | +class GeneralHendrycksTest(MultipleChoiceTask): |
| 48 | + VERSION = 0 |
| 49 | + NUM_FEW_SHOT = 25 |
| 50 | + DATASET_NAME = None |
| 51 | + |
| 52 | + def __init__(self, lang): |
| 53 | + self.DATASET_NAME = f"mmlu_{lang}" |
| 54 | + self.DATASET_PATH = get_mlmm_dataset_path("datasets/m_mmlu") |
| 55 | + |
| 56 | + super().__init__() |
| 57 | + |
| 58 | + def has_training_docs(self): |
| 59 | + return False |
| 60 | + |
| 61 | + def has_validation_docs(self): |
| 62 | + return True |
| 63 | + |
| 64 | + def has_test_docs(self): |
| 65 | + return True |
| 66 | + |
| 67 | + def validation_docs(self): |
| 68 | + return map(self._process_doc, self.dataset["validation"]) |
| 69 | + |
| 70 | + def test_docs(self): |
| 71 | + return map(self._process_doc, self.dataset["test"]) |
| 72 | + |
| 73 | + def _process_doc(self, doc): |
| 74 | + def format_example(doc, keys): |
| 75 | + """ |
| 76 | + Question: <prompt> |
| 77 | + Choices: |
| 78 | + A. <choice1> |
| 79 | + B. <choice2> |
| 80 | + C. <choice3> |
| 81 | + D. <choice4> |
| 82 | + Answer: |
| 83 | + """ |
| 84 | + prompt = "Question: " + doc["question"] + "\nChoices:\n" |
| 85 | + prompt += "".join( |
| 86 | + [f"{key}. {choice}\n" for key, choice in zip(keys, doc["choices"])] |
| 87 | + ) |
| 88 | + prompt += "Answer:" |
| 89 | + return prompt |
| 90 | + |
| 91 | + keys = ["A", "B", "C", "D"] |
| 92 | + return { |
| 93 | + "query": format_example(doc, keys), |
| 94 | + "choices": doc["choices"], |
| 95 | + "gold": keys.index(doc["answer"]) |
| 96 | + if isinstance(doc["answer"], str) |
| 97 | + else doc["answer"], |
| 98 | + } |
| 99 | + |
| 100 | + def fewshot_examples(self, k, rnd): |
| 101 | + # fewshot_examples is not just sampling from train_docs because dev is |
| 102 | + # in the same distribution as val/test but auxiliary_train isn't |
| 103 | + |
| 104 | + if self._fewshot_docs is None: |
| 105 | + self._fewshot_docs = list(map(self._process_doc, self.dataset["dev"])) |
| 106 | + |
| 107 | + return rnd.sample(list(self._fewshot_docs), k) |
| 108 | + |
| 109 | + def doc_to_text(self, doc): |
| 110 | + return doc["query"] |
| 111 | + |
| 112 | + def should_decontaminate(self): |
| 113 | + return True |
| 114 | + |
| 115 | + def doc_to_decontamination_query(self, doc): |
| 116 | + return doc["query"] |
0 commit comments