"megatron/legacy/data/dataset_utils.py" did not exist on "9ea9d50f6289a1b167867a9abee5f784b8817d1f"
hendrycks_test.py 4.14 KB
Newer Older
Andy Zou's avatar
Andy Zou committed
1
import csv
Jonathan Tow's avatar
Jonathan Tow committed
2
3
import random
from lm_eval.base import MultipleChoiceTask
4
from lm_eval.mctask_experimental import MultipleChoiceDoc
Andy Zou's avatar
Andy Zou committed
5
from ..utils import sh
Jonathan Tow's avatar
Jonathan Tow committed
6
from pathlib import Path
7
from best_download import download_file
Jonathan Tow's avatar
Jonathan Tow committed
8
9
10
11
12
13
14
15
16
17

SUBJECTS = ['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',
Andy Zou's avatar
Andy Zou committed
18
19
20
21
22
23
            'professional_psychology', 'public_relations', 'security_studies', 'sociology', 'us_foreign_policy', 'virology', 'world_religions']


def create_all_tasks():
    """Creates a dictionary of tasks from a list of subjects
    :return: {task_name: task}
Andy Zou's avatar
Andy Zou committed
24
        e.g. {hendrycksTest-abstract_algebra: Task, hendrycksTest-anatomy: Task}
Andy Zou's avatar
Andy Zou committed
25
26
    """
    return {
Andy Zou's avatar
Andy Zou committed
27
        f"hendrycksTest-{sub}": create_task(sub) for sub in SUBJECTS
Andy Zou's avatar
Andy Zou committed
28
29
    }

Jonathan Tow's avatar
Jonathan Tow committed
30

Andy Zou's avatar
Andy Zou committed
31
32
33
34
35
36
def create_task(subject):
    class HendrycksTest(GeneralHendrycksTest):
        def __init__(self):
            super().__init__(subject)
    return HendrycksTest

Jonathan Tow's avatar
Jonathan Tow committed
37

Andy Zou's avatar
Andy Zou committed
38
class GeneralHendrycksTest(MultipleChoiceTask):
Leo Gao's avatar
Leo Gao committed
39
    VERSION = 0
Jonathan Tow's avatar
Jonathan Tow committed
40
    DATASET_PATH = Path("data/hendrycksTest/")
Andy Zou's avatar
Andy Zou committed
41
42
43
44
45
46

    def __init__(self, subject):
        self.subject = subject
        super().__init__()

    def download(self):
47
48
        if not (self.DATASET_PATH / 'done').exists():
            sh("mkdir -p data")
49
            download_file("https://people.eecs.berkeley.edu/~hendrycks/data.tar", local_file="data/data.tar", expected_checksum="78a804365a59028188fb19bd1adcadc5e0c260b220a9d8b2e33a5ea7d5fbe3b4")
Andy Zou's avatar
Andy Zou committed
50
            sh("""
51
52
53
54
55
            tar -xf data/data.tar -C data/
            rm data/data.tar
            mv data/data data/hendrycksTest
            touch data/hendrycksTest/done
            """)
Andy Zou's avatar
Andy Zou committed
56
57

    def has_training_docs(self):
Andy Zou's avatar
Andy Zou committed
58
        return True
Andy Zou's avatar
Andy Zou committed
59
60

    def has_validation_docs(self):
61
        return True
Andy Zou's avatar
Andy Zou committed
62
63
64
65

    def has_test_docs(self):
        return True

Jonathan Tow's avatar
Jonathan Tow committed
66
    def _convert_standard(self, doc):
67
68
69
70
71
72
73
        keys = ['A', 'B', 'C', 'D']
        return MultipleChoiceDoc(
            question=doc[0],
            keys=keys,
            options=doc[1:5],
            gold=keys.index(doc[5])
        )
Andy Zou's avatar
Andy Zou committed
74

Jonathan Tow's avatar
Jonathan Tow committed
75
    def _load_docs(self, filename):
Andy Zou's avatar
Andy Zou committed
76
        reader = csv.reader(open(filename, 'r'), quotechar='"', delimiter=',')
Jonathan Tow's avatar
Jonathan Tow committed
77
        return (self._convert_standard(doc) for doc in reader)
Andy Zou's avatar
Andy Zou committed
78
79

    def training_docs(self):
80
81
82
83
84
        docs = []
        for train_dir in ["auxiliary_train", "dev"]:
            for f in (self.DATASET_PATH / train_dir).iterdir():
                docs.extend(self._load_docs(f))
        return docs
Andy Zou's avatar
Andy Zou committed
85
86

    def validation_docs(self):
87
88
        filename = self.DATASET_PATH / "val" / f"{self.subject}_val.csv"
        return self._load_docs(filename)
Andy Zou's avatar
Andy Zou committed
89
90

    def test_docs(self):
Jonathan Tow's avatar
Jonathan Tow committed
91
        filename = self.DATASET_PATH / "test" / f"{self.subject}_test.csv"
Andy Zou's avatar
Andy Zou committed
92
        return self._load_docs(filename)
Andy Zou's avatar
Andy Zou committed
93

94
    def fewshot_examples(self, k, rnd):
Leo Gao's avatar
Leo Gao committed
95
96
        # fewshot_examples is not just sampling from train_docs because dev is 
        # in the same distribution as val/test but auxiliary_train isn't
97

Jonathan Tow's avatar
Jonathan Tow committed
98
        filename = self.DATASET_PATH / "dev" / f"{self.subject}_dev.csv"
99
100
101
102
103

        if self._fewshot_docs is None:
            self._fewshot_docs = list(self._load_docs(filename))

        return rnd.sample(list(self._fewshot_docs), k)