-
Notifications
You must be signed in to change notification settings - Fork 9
/
mmlu_eval.py
246 lines (213 loc) · 8.14 KB
/
mmlu_eval.py
1
2
3
4
5
6
7
8
9
10
11
12
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
# Modified from: https://github.com/ruikangliu/IntactKV/blob/main/eval_utils/mmlu.py
import argparse
import json
import os
import time
from tqdm import tqdm
import numpy as np
import pandas as pd
import torch
subcategories = {
"abstract_algebra": ["math"],
"anatomy": ["health"],
"astronomy": ["physics"],
"business_ethics": ["business"],
"clinical_knowledge": ["health"],
"college_biology": ["biology"],
"college_chemistry": ["chemistry"],
"college_computer_science": ["computer science"],
"college_mathematics": ["math"],
"college_medicine": ["health"],
"college_physics": ["physics"],
"computer_security": ["computer science"],
"conceptual_physics": ["physics"],
"econometrics": ["economics"],
"electrical_engineering": ["engineering"],
"elementary_mathematics": ["math"],
"formal_logic": ["philosophy"],
"global_facts": ["other"],
"high_school_biology": ["biology"],
"high_school_chemistry": ["chemistry"],
"high_school_computer_science": ["computer science"],
"high_school_european_history": ["history"],
"high_school_geography": ["geography"],
"high_school_government_and_politics": ["politics"],
"high_school_macroeconomics": ["economics"],
"high_school_mathematics": ["math"],
"high_school_microeconomics": ["economics"],
"high_school_physics": ["physics"],
"high_school_psychology": ["psychology"],
"high_school_statistics": ["math"],
"high_school_us_history": ["history"],
"high_school_world_history": ["history"],
"human_aging": ["health"],
"human_sexuality": ["culture"],
"international_law": ["law"],
"jurisprudence": ["law"],
"logical_fallacies": ["philosophy"],
"machine_learning": ["computer science"],
"management": ["business"],
"marketing": ["business"],
"medical_genetics": ["health"],
"miscellaneous": ["other"],
"moral_disputes": ["philosophy"],
"moral_scenarios": ["philosophy"],
"nutrition": ["health"],
"philosophy": ["philosophy"],
"prehistory": ["history"],
"professional_accounting": ["other"],
"professional_law": ["law"],
"professional_medicine": ["health"],
"professional_psychology": ["psychology"],
"public_relations": ["politics"],
"security_studies": ["politics"],
"sociology": ["culture"],
"us_foreign_policy": ["politics"],
"virology": ["health"],
"world_religions": ["philosophy"],
}
categories = {
"STEM": ["physics", "chemistry", "biology", "computer science", "math", "engineering"],
"humanities": ["history", "philosophy", "law"],
"social sciences": ["politics", "culture", "economics", "geography", "psychology"],
"other (business, health, misc.)": ["other", "business", "health"],
}
choices = ["A", "B", "C", "D"]
def format_subject(subject):
l = subject.split("_")
s = ""
for entry in l:
s += " " + entry
return s
def format_example(df, idx, include_answer=True):
prompt = df.iloc[idx, 0]
k = df.shape[1] - 2
for j in range(k):
prompt += "\n{}. {}".format(choices[j], df.iloc[idx, j + 1])
prompt += "\nAnswer:"
if include_answer:
prompt += " {}\n\n".format(df.iloc[idx, k + 1])
return prompt
def gen_prompt(train_df, subject, k=-1):
prompt = "The following are multiple choice questions (with answers) about {}.\n\n".format(
format_subject(subject)
)
if k == -1:
k = train_df.shape[0]
for i in range(k):
prompt += format_example(train_df, i)
return prompt
@torch.no_grad()
def eval(fewshot_number, subject, model, tokenizer, dev_df, test_df):
cors = []
all_probs = []
answers = choices[: test_df.shape[1] - 2]
for i in tqdm(range(test_df.shape[0])):
# get prompt and make sure it fits
k = fewshot_number
prompt_end = format_example(test_df, i, include_answer=False)
train_prompt = gen_prompt(dev_df, subject, k)
prompt = train_prompt + prompt_end
input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(model.device)
while input_ids.shape[-1] > 2048:
k -= 1
train_prompt = gen_prompt(dev_df, subject, k)
prompt = train_prompt + prompt_end
input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to(
model.device
)
label = test_df.iloc[i, test_df.shape[1] - 1]
logits = model(input_ids=input_ids).logits[0, -1]
probs = (
torch.nn.functional.softmax(
torch.tensor(
[
logits[tokenizer("A").input_ids[-1]],
logits[tokenizer("B").input_ids[-1]],
logits[tokenizer("C").input_ids[-1]],
logits[tokenizer("D").input_ids[-1]],
]
).float(),
dim=0,
)
.detach()
.cpu()
.numpy()
)
pred = {0: "A", 1: "B", 2: "C", 3: "D"}[np.argmax(probs)]
cor = pred == label
cors.append(cor)
all_probs.append(probs)
acc = np.mean(cors)
cors = np.array(cors)
all_probs = np.array(all_probs)
print("Average {}-shot accuracy {:.4f} - {}".format(fewshot_number, acc, subject))
return cors, acc, all_probs
def run_mmlu_eval(model, tokenizer, model_name, fewshot_number, data_dir, save_dir):
model.eval()
subjects = sorted(
[
f.split("_test.csv")[0]
for f in os.listdir(os.path.join(data_dir, "test"))
if "_test.csv" in f
]
)
if not os.path.exists(save_dir):
os.makedirs(save_dir)
if not os.path.exists(os.path.join(save_dir, "results_{}".format(model_name))):
os.makedirs(os.path.join(save_dir, "results_{}".format(model_name)))
all_cors = []
subcat_cors = {
subcat: [] for subcat_lists in subcategories.values() for subcat in subcat_lists
}
cat_cors = {cat: [] for cat in categories}
start = time.time()
for i, subject in enumerate(subjects):
print(f"Evaluating subject \"{subject}\" ({i}/{len(subjects)}) ...")
dev_df = pd.read_csv(
os.path.join(data_dir, "dev", subject + "_dev.csv"), header=None
)[: fewshot_number]
test_df = pd.read_csv(
os.path.join(data_dir, "test", subject + "_test.csv"), header=None
)
cors, acc, probs = eval(fewshot_number, subject, model, tokenizer, dev_df, test_df)
subcats = subcategories[subject]
for subcat in subcats:
subcat_cors[subcat].append(cors)
for key in categories.keys():
if subcat in categories[key]:
cat_cors[key].append(cors)
all_cors.append(cors)
test_df["{}_correct".format(model_name)] = cors
for j in range(probs.shape[1]):
choice = choices[j]
test_df["{}_choice{}_probs".format(model_name, choice)] = probs[:, j]
test_df.to_csv(
os.path.join(
save_dir, "results_{}".format(model_name), "{}.csv".format(subject)
),
index=None,
)
results = {"subcategories": {}, "categories": {}}
print(f"***** {fewshot_number}-shot *****")
print("***** MMLU_eval subcategories metrics *****")
for subcat in subcat_cors:
subcat_acc = np.mean(np.concatenate(subcat_cors[subcat]))
results["subcategories"][subcat] = subcat_acc
print("Average accuracy {:.4f} - {}".format(subcat_acc, subcat))
print("***** MMLU_eval categories metrics *****")
for cat in cat_cors:
cat_acc = np.mean(np.concatenate(cat_cors[cat]))
results["categories"][cat] = cat_acc
print("Average accuracy {:.4f} - {}".format(cat_acc, cat))
weighted_acc = np.mean(np.concatenate(all_cors))
results["weighted_accuracy"] = weighted_acc
print("***** MMLU_eval average metrics *****")
print("Average accuracy: {:.4f}".format(weighted_acc))
end = time.time()
print(f"Evaluation took {(end - start) / 60:.2f}mins")
results_file = os.path.join(
save_dir, "accuracies_{}.json".format(model_name)
)
with open(results_file, "w") as f:
json.dump(results, f)