"configs/config_rodnet_hg1v2_win16_mnet_dcn.py" did not exist on "81f1e0ac4447100f7e70815c5e1c89e18f6ab55e"
hendrycks_test.py 3.95 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
Andy Zou's avatar
Andy Zou committed
4
from ..utils import sh
Jonathan Tow's avatar
Jonathan Tow committed
5
6
7
8
9
10
11
12
13
14
15
from pathlib import Path

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
16
17
18
19
20
21
            '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
22
        e.g. {hendrycksTest-abstract_algebra: Task, hendrycksTest-anatomy: Task}
Andy Zou's avatar
Andy Zou committed
23
24
    """
    return {
Andy Zou's avatar
Andy Zou committed
25
        f"hendrycksTest-{sub}": create_task(sub) for sub in SUBJECTS
Andy Zou's avatar
Andy Zou committed
26
27
    }

Jonathan Tow's avatar
Jonathan Tow committed
28

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

Jonathan Tow's avatar
Jonathan Tow committed
35

Andy Zou's avatar
Andy Zou committed
36
class GeneralHendrycksTest(MultipleChoiceTask):
Jonathan Tow's avatar
Jonathan Tow committed
37
    DATASET_PATH = Path("data/hendrycksTest/")
Andy Zou's avatar
Andy Zou committed
38
39
40
41
42
43

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

    def download(self):
Jonathan Tow's avatar
Jonathan Tow committed
44
        if not self.DATASET_PATH.exists():
Andy Zou's avatar
Andy Zou committed
45
46
            sh("""
                mkdir -p data
Andy Zou's avatar
minor  
Andy Zou committed
47
48
49
50
                wget https://people.eecs.berkeley.edu/~hendrycks/data.tar -P data/
                tar -xf data/data.tar -C data/
                rm data/data.tar
                mv data/data data/hendrycksTest
Andy Zou's avatar
Andy Zou committed
51
52
53
                """)

    def has_training_docs(self):
Andy Zou's avatar
Andy Zou committed
54
        return True
Andy Zou's avatar
Andy Zou committed
55
56

    def has_validation_docs(self):
Andy Zou's avatar
Andy Zou committed
57
        return False
Andy Zou's avatar
Andy Zou committed
58
59
60
61

    def has_test_docs(self):
        return True

Jonathan Tow's avatar
Jonathan Tow committed
62
63
64
65
66
67
    def _convert_standard(self, doc):
        return {
            "query": "Question: " + doc[0] + "\nAnswer:",
            "choices": doc[1:5],
            "gold": ['A', 'B', 'C', 'D'].index(doc[5])
        }
Andy Zou's avatar
Andy Zou committed
68

Jonathan Tow's avatar
Jonathan Tow committed
69
    def _load_docs(self, filename):
Andy Zou's avatar
Andy Zou committed
70
        reader = csv.reader(open(filename, 'r'), quotechar='"', delimiter=',')
Jonathan Tow's avatar
Jonathan Tow committed
71
        return (self._convert_standard(doc) for doc in reader)
Andy Zou's avatar
Andy Zou committed
72
73

    def training_docs(self):
Andy Zou's avatar
minor  
Andy Zou committed
74
75
        # Use all files in the auxiliary_train, dev, val directories
        # auxiliary_train includes some UnifiedQA MC tasks
Jonathan Tow's avatar
Jonathan Tow committed
76
        docs = []
Andy Zou's avatar
minor  
Andy Zou committed
77
        for train_dir in ["auxiliary_train", "dev", "val"]:
Jonathan Tow's avatar
Jonathan Tow committed
78
79
            for f in (self.DATASET_PATH / train_dir).iterdir():
                docs.extend(self._load_docs(f))
Andy Zou's avatar
Andy Zou committed
80
        return docs
Andy Zou's avatar
Andy Zou committed
81
82

    def validation_docs(self):
Andy Zou's avatar
Andy Zou committed
83
        raise NotImplementedError
Andy Zou's avatar
Andy Zou committed
84
85

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

Jonathan Tow's avatar
Jonathan Tow committed
89
90
91
92
    def fewshot_examples(self, k):
        assert k <= 5, "Maximum 5 few shot examples."
        filename = self.DATASET_PATH / "dev" / f"{self.subject}_dev.csv"
        return random.sample(list(self._load_docs(filename)), k)
Andy Zou's avatar
Andy Zou committed
93
94
95

    def fewshot_description(self):
        subject = self.subject.replace("_", " ")
Jonathan Tow's avatar
Jonathan Tow committed
96
97
98
99
        return f"The following are multiple choice questions (with answers) about {subject}."

    def doc_to_text(self, doc):
        return doc["query"]