_generate_configs.py 5.16 KB
Newer Older
1
"""
lintangsutawika's avatar
lintangsutawika committed
2
Take in a YAML, and output all "other" splits with this YAML
3
4
"""
import argparse
5
import os
6

7
import yaml
8
9
10
11
from tqdm import tqdm

from lm_eval.logger import eval_logger

12

lintangsutawika's avatar
lintangsutawika committed
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
SUBJECTS = {
    "abstract_algebra": "stem",
    "anatomy": "stem",
    "astronomy": "stem",
    "business_ethics": "other",
    "clinical_knowledge": "other",
    "college_biology": "stem",
    "college_chemistry": "stem",
    "college_computer_science": "stem",
    "college_mathematics": "stem",
    "college_medicine": "other",
    "college_physics": "stem",
    "computer_security": "stem",
    "conceptual_physics": "stem",
    "econometrics": "social_sciences",
    "electrical_engineering": "stem",
    "elementary_mathematics": "stem",
    "formal_logic": "humanities",
    "global_facts": "other",
    "high_school_biology": "stem",
    "high_school_chemistry": "stem",
    "high_school_computer_science": "stem",
    "high_school_european_history": "humanities",
    "high_school_geography": "social_sciences",
    "high_school_government_and_politics": "social_sciences",
    "high_school_macroeconomics": "social_sciences",
    "high_school_mathematics": "stem",
    "high_school_microeconomics": "social_sciences",
    "high_school_physics": "stem",
    "high_school_psychology": "social_sciences",
    "high_school_statistics": "stem",
    "high_school_us_history": "humanities",
    "high_school_world_history": "humanities",
    "human_aging": "other",
    "human_sexuality": "social_sciences",
    "international_law": "humanities",
    "jurisprudence": "humanities",
    "logical_fallacies": "humanities",
    "machine_learning": "stem",
    "management": "other",
    "marketing": "other",
    "medical_genetics": "other",
    "miscellaneous": "other",
    "moral_disputes": "humanities",
    "moral_scenarios": "humanities",
    "nutrition": "other",
    "philosophy": "humanities",
    "prehistory": "humanities",
    "professional_accounting": "other",
    "professional_law": "humanities",
    "professional_medicine": "other",
    "professional_psychology": "social_sciences",
    "public_relations": "social_sciences",
    "security_studies": "social_sciences",
    "sociology": "social_sciences",
    "us_foreign_policy": "social_sciences",
    "virology": "other",
    "world_religions": "humanities",
}
72
73
74
75
76


def parse_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--base_yaml_path", required=True)
77
    parser.add_argument("--save_prefix_path", default="mmlu")
lintangsutawika's avatar
update  
lintangsutawika committed
78
    parser.add_argument("--cot_prompt_path", default=None)
lintangsutawika's avatar
lintangsutawika committed
79
    parser.add_argument("--task_prefix", default="")
lintangsutawika's avatar
lintangsutawika committed
80
    parser.add_argument("--group_prefix", default="")
81
82
83
84
85
86
    return parser.parse_args()


if __name__ == "__main__":
    args = parse_args()

lintangsutawika's avatar
lintangsutawika committed
87
    # get filename of base_yaml so we can `"include": ` it in our "other" YAMLs.
88
    base_yaml_name = os.path.split(args.base_yaml_path)[-1]
89
    with open(args.base_yaml_path, encoding="utf-8") as f:
90
91
        base_yaml = yaml.full_load(f)

lintangsutawika's avatar
lintangsutawika committed
92
93
    if args.cot_prompt_path is not None:
        import json
lintangsutawika's avatar
update  
lintangsutawika committed
94

95
        with open(args.cot_prompt_path, encoding="utf-8") as f:
lintangsutawika's avatar
lintangsutawika committed
96
97
            cot_file = json.load(f)

lintangsutawika's avatar
lintangsutawika committed
98
99
100
101
102
    ALL_CATEGORIES = []
    for subject, category in tqdm(SUBJECTS.items()):
        if category not in ALL_CATEGORIES:
            ALL_CATEGORIES.append(category)

lintangsutawika's avatar
lintangsutawika committed
103
104
105
106
        if args.cot_prompt_path is not None:
            description = cot_file[subject]
        else:
            description = f"The following are multiple choice questions (with answers) about {' '.join(subject.split('_'))}.\n\n"
107
108
109

        yaml_dict = {
            "include": base_yaml_name,
110
111
112
            "group": f"mmlu_{args.task_prefix}_{category}"
            if args.task_prefix != ""
            else f"mmlu_{category}",
lintangsutawika's avatar
lintangsutawika committed
113
            "group_alias": category.replace("_", " "),
lintangsutawika's avatar
update  
lintangsutawika committed
114
115
116
            "task": f"mmlu_{args.task_prefix}_{subject}"
            if args.task_prefix != ""
            else f"mmlu_{subject}",
lintangsutawika's avatar
lintangsutawika committed
117
            "task_alias": subject.replace("_", " "),
118
            "dataset_name": subject,
lintangsutawika's avatar
lintangsutawika committed
119
            "description": description,
120
121
        }

lintangsutawika's avatar
lintangsutawika committed
122
        file_save_path = args.save_prefix_path + f"_{subject}.yaml"
123
        eval_logger.info(f"Saving yaml for subset {subject} to {file_save_path}")
124
        with open(file_save_path, "w", encoding="utf-8") as yaml_file:
lintangsutawika's avatar
update  
lintangsutawika committed
125
126
127
128
129
130
            yaml.dump(
                yaml_dict,
                yaml_file,
                allow_unicode=True,
                default_style='"',
            )
lintangsutawika's avatar
lintangsutawika committed
131

132
133
134
135
136
137
138
139
140
    if args.task_prefix != "":
        mmlu_subcategories = [
            f"mmlu_{args.task_prefix}_{category}" for category in ALL_CATEGORIES
        ]
    else:
        mmlu_subcategories = [f"mmlu_{category}" for category in ALL_CATEGORIES]

    if args.group_prefix != "":
        file_save_path = args.group_prefix + ".yaml"
lintangsutawika's avatar
lintangsutawika committed
141
    else:
142
143
        file_save_path = args.save_prefix_path + ".yaml"

lintangsutawika's avatar
lintangsutawika committed
144
    eval_logger.info(f"Saving benchmark config to {file_save_path}")
145
    with open(file_save_path, "w", encoding="utf-8") as yaml_file:
lintangsutawika's avatar
lintangsutawika committed
146
147
        yaml.dump(
            {
148
149
150
151
                "group": f"mmlu_{args.task_prefix}"
                if args.task_prefix != ""
                else "mmlu",
                "task": mmlu_subcategories,
lintangsutawika's avatar
lintangsutawika committed
152
            },
lintangsutawika's avatar
lintangsutawika committed
153
            yaml_file,
154
            indent=4,
lintangsutawika's avatar
lintangsutawika committed
155
            default_flow_style=False,
lintangsutawika's avatar
lintangsutawika committed
156
        )