-
Notifications
You must be signed in to change notification settings - Fork 1
/
eval.py
292 lines (267 loc) · 10.7 KB
/
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import json
import os
import time
from pprint import pprint
import numpy as np
import ray
import shortuuid
import torch
import torch.nn as nn
from fastchat.model import get_conversation_template
from tqdm import tqdm
from categories import categories, subcategories
from datautils import get_loaders
from lm_eval import evaluator
from parallel_utils import map_layers_to_multi_gpus
@torch.no_grad()
def evaluate(lm, args, logger):
results = {}
if args.multigpu:
if "opt" in args.model:
map_layers_to_multi_gpus(lm.model.model.decoder.layers)
input_device = lm.model.model.decoder.layers[0].device
output_device = lm.model.model.decoder.layers[-1].device
lm._device = input_device
assert input_device == output_device
lm.model.model.decoder.embed_positions.to(input_device)
lm.model.model.decoder.embed_tokens.to(input_device)
lm.model.model.decoder.final_layer_norm.to(output_device)
lm.model.lm_head.to(output_device)
elif (
"llama" in args.model
or "Llama" in args.model
or "vicuna" in args.model
or "alpaca" in args.model
):
map_layers_to_multi_gpus(lm.model.model.layers)
input_device = lm.model.model.layers[0].device
output_device = lm.model.model.layers[-1].device
assert input_device == output_device
lm._device = input_device
lm.model.model.embed_tokens.to(input_device)
lm.model.model.norm.to(output_device)
lm.model.lm_head.to(output_device)
else:
if "opt" in args.model:
lm.model.model.decoder = lm.model.model.decoder.to(lm.device)
elif (
"llama" in args.model
or "Llama" in args.model
or "vicuna" in args.model
or "alpaca" in args.model
):
lm.model = lm.model.to(lm.device)
if args.eval_ppl:
for dataset in ["wikitext2", "ptb", "c4"]:
cache_testloader = (
f"{args.cache_dir}/testloader_{args.model_family}_{dataset}_all.cache"
)
if os.path.exists(cache_testloader):
testloader = torch.load(cache_testloader)
logger.info(f"load calibration from {cache_testloader}")
else:
dataloader, testloader = get_loaders(
dataset,
seed=args.seed,
model=args.model,
seqlen=lm.seqlen,
)
torch.save(testloader, cache_testloader)
if "c4" in dataset:
testenc = testloader
else:
testenc = testloader.input_ids
nsamples = testenc.numel() // lm.seqlen
use_cache = lm.model.config.use_cache
lm.model.config.use_cache = False
lm.model.eval()
nlls = []
for i in tqdm(range(nsamples)):
batch = testenc[:, (i * lm.seqlen) : ((i + 1) * lm.seqlen)].to(
lm.device
)
if "opt" in args.model:
outputs = lm.model.model.decoder(batch)
elif (
"llama" in args.model
or "Llama" in args.model
or "vicuna" in args.model
or "alpaca" in args.model
):
outputs = lm.model.model(batch)
hidden_states = outputs[0]
logits = lm.model.lm_head(hidden_states)
shift_logits = logits[:, :-1, :]
shift_labels = testenc[:, (i * lm.seqlen) : ((i + 1) * lm.seqlen)][
:, 1:
].to(lm.model.lm_head.weight.device)
loss_fct = nn.CrossEntropyLoss()
loss = loss_fct(
shift_logits.view(-1, shift_logits.size(-1)),
shift_labels.view(-1),
)
neg_log_likelihood = loss.float() * lm.seqlen
nlls.append(neg_log_likelihood)
if i == args.limit:
break
ppl = torch.exp(torch.stack(nlls).sum() / (nsamples * lm.seqlen))
logger.info(f"{dataset} : {ppl.item()}")
lm.model.config.use_cache = use_cache
results[dataset] = ppl.item()
if args.tasks != "":
t_results = evaluator.simple_evaluate(
lm,
tasks=args.tasks,
num_fewshot=args.num_fewshot,
limit=None if args.limit == -1 else args.limit,
)
results.update(t_results)
logger.info(results)
pprint(results)
# for test of MMLU
if "hendrycksTest" in args.tasks:
all_cors = []
all_cors_norm = []
subcat_cors = {
subcat: []
for subcat_lists in subcategories.values()
for subcat in subcat_lists
}
cat_cors = {cat: [] for cat in categories}
cat_cors_norm = {cat: [] for cat in categories}
for key in t_results["results"].keys():
if not "hendrycksTest" in key:
continue
subject = key.split("-")[-1]
cors = t_results["results"][key]["acc"]
cors_norm = t_results["results"][key]["acc_norm"]
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)
cat_cors_norm[key].append(cors_norm)
all_cors.append(cors)
all_cors_norm.append(cors_norm)
for cat in cat_cors:
cat_acc = np.mean(cat_cors[cat])
logger.info("Average accuracy {:.4f} - {}".format(cat_acc, cat))
weighted_acc = np.mean(all_cors)
logger.info("Average accuracy: {:.4f}".format(weighted_acc))
return results
@torch.no_grad()
def test_throughput(lm, args, logger):
results = {}
if args.multigpu:
if "opt" in args.model:
map_layers_to_multi_gpus(lm.model.model.decoder.layers)
input_device = lm.model.model.decoder.layers[0].device
output_device = lm.model.model.decoder.layers[-1].device
lm._device = input_device
assert input_device == output_device
lm.model.model.decoder.embed_positions.to(input_device)
lm.model.model.decoder.embed_tokens.to(input_device)
lm.model.model.decoder.final_layer_norm.to(output_device)
lm.model.lm_head.to(output_device)
elif "llama" in args.model or "Llama" in args.model:
map_layers_to_multi_gpus(lm.model.model.layers)
input_device = lm.model.model.layers[0].device
output_device = lm.model.model.layers[-1].device
assert input_device == output_device
lm._device = input_device
lm.model.model.embed_tokens.to(input_device)
lm.model.model.norm.to(output_device)
lm.model.lm_head.to(output_device)
else:
if "opt" in args.model:
lm.model.model.decoder = lm.model.model.decoder.to(lm.device)
elif "llama" in args.model or "Llama" in args.model:
lm.model = lm.model.to(lm.device)
dataset = "wikitext2"
cache_testloader = (
f"{args.cache_dir}/testloader_{args.model_family}_{dataset}_all.cache"
)
if os.path.exists(cache_testloader):
testloader = torch.load(cache_testloader)
logger.info(f"load calibration from {cache_testloader}")
else:
dataloader, testloader = get_loaders(
dataset,
seed=args.seed,
model=args.model,
seqlen=lm.seqlen,
)
torch.save(testloader, cache_testloader)
if "c4" in dataset:
testenc = testloader
else:
testenc = testloader.input_ids
nsamples = testenc.numel() // lm.seqlen
use_cache = lm.model.config.use_cache
lm.model.config.use_cache = False
lm.model.eval()
nlls = []
# warmup
for i in tqdm(range(nsamples)):
batch = testenc[:, (i * lm.seqlen) : ((i + 1) * lm.seqlen)].to(lm.device)
if "opt" in args.model:
outputs = lm.model.model.decoder(batch)
elif "llama" in args.model or "Llama" in args.model:
outputs = lm.model.model(batch)
hidden_states = outputs[0]
logits = lm.model.lm_head(hidden_states)
start_time = time.time()
for i in tqdm(range(nsamples)):
batch = testenc[:, (i * lm.seqlen) : ((i + 1) * lm.seqlen)].to(lm.device)
if "opt" in args.model:
outputs = lm.model.model.decoder(batch)
elif "llama" in args.model or "Llama" in args.model:
outputs = lm.model.model(batch)
hidden_states = outputs[0]
logits = lm.model.lm_head(hidden_states)
end_time = time.time()
avg_time = (end_time - start_time) / nsamples
print(f"Avg time for seq_len {lm.seqlen} is {avg_time}s")
def chat_run_eval(lm, model_name, question_file, answer_file, num_gpus=1):
# split question file into num_gpus files
ques_jsons = []
with open(os.path.expanduser(question_file), "r") as ques_file:
for line in ques_file:
ques_jsons.append(line)
ans_jsons = get_model_answers(lm, model_name, ques_jsons)
with open(os.path.expanduser(answer_file), "w") as ans_file:
for line in ans_jsons:
ans_file.write(json.dumps(line) + "\n")
@torch.inference_mode()
def get_model_answers(lm, model_name, question_jsons):
lm.model = lm.model.to(lm._device)
ans_jsons = []
for i, line in enumerate(tqdm(question_jsons)):
ques_json = json.loads(line)
idx = ques_json["question_id"]
qs = ques_json["text"]
conv = get_conversation_template(model_name)
conv.append_message(conv.roles[0], qs)
conv.append_message(conv.roles[1], None)
prompt = conv.get_prompt()
input_ids = lm.tokenizer([prompt]).input_ids
output_ids = lm.model.generate(
torch.as_tensor(input_ids).cuda(),
do_sample=True,
temperature=0.7,
max_new_tokens=1024,
)
output_ids = output_ids[0][len(input_ids[0]) :]
outputs = lm.tokenizer.decode(output_ids, skip_special_tokens=True).strip()
ans_id = shortuuid.uuid()
ans_jsons.append(
{
"question_id": idx,
"text": outputs,
"answer_id": ans_id,
"model_id": model_name,
"metadata": {},
}
)
return ans_jsons