-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtask_evaluator.py
More file actions
414 lines (340 loc) · 17.3 KB
/
Copy pathtask_evaluator.py
File metadata and controls
414 lines (340 loc) · 17.3 KB
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import json
import os
import numpy as np
from llm_prompt import LLMInfernce
from utils.constant import LLM_Name, huggingface_mapping, TaskName
import rouge
from utils.other import run_command
import torch
home_path = os.path.expanduser("~")
def eval_evolcode(llm_name, checkpoint=None, quantization=True, pretrained_path=None):
eval_code(llm_name, checkpoint, quantization, pretrained_path)
def eval_code(llm_name, checkpoint=None, quantization=True, pretrained_path=None):
# Define the command as a list of strings
command = [
'python',
os.path.join(home_path, "code", "bigcode-evaluation-harness", "main.py"),
'--max_length_generation', '512',
'--tasks', 'humaneval',
'--n_samples', '1',
'--batch_size', '1',
'--allow_code_execution'
]
if pretrained_path:
save_path = "{}/{}/results.json".format(pretrained_path, "humaneval")
command += ["--model", pretrained_path, "--metric_output_path", save_path]
elif checkpoint:
save_path = "{}/{}/results.json".format(checkpoint, "humaneval")
command += ["--metric_output_path", save_path, "--model", huggingface_mapping[llm_name], "--peft_model", checkpoint]
else:
save_path = "{}/code/finetuneLLM/outputs/{}/metrics/{}/results.json".format(home_path, llm_name, "humaneval")
command += ["--model", huggingface_mapping[llm_name], "--metric_output_path", save_path]
# create save path
os.makedirs(os.path.dirname(save_path), exist_ok=True)
# Run the command
run_command(command)
def lm_eval_harness(llm_name, task, batch_size=16, num_fewshot=0, checkpoint=None, pretrained_path=None, quantization=True):
if pretrained_path:
model_args = "pretrained={}".format(pretrained_path)
save_path = "{}/{}".format(pretrained_path, task)
elif checkpoint:
model_args = "pretrained={},peft={}".format(
huggingface_mapping[llm_name], checkpoint)
save_path = "{}/{}".format(checkpoint, task)
else:
model_args = "pretrained={}".format(
huggingface_mapping[llm_name])
save_path = "{}/code/finetuneLLM/outputs/{}/metrics/{}".format(home_path, llm_name, task)
if quantization:
model_args += ",load_in_4bit=True,bnb_4bit_use_double_quant=True,bnb_4bit_quant_type=nf4,bnb_4bit_compute_dtype=bfloat16"
# create save path
os.makedirs(save_path, exist_ok=True)
# Define the command
command = [
"python",
os.path.join(home_path, "code", "lm-evaluation-harness", "lm_eval", "__main__.py"),
"--model", "hf",
"--model_args",
model_args,
"--tasks", task,
"--num_fewshot", str(num_fewshot),
"--device", "cuda:0",
"--batch_size", str(batch_size),
"--output_path", save_path
]
# Run the command
run_command(command)
def eval_math(llm_name, checkpoint=None, quantization=True, pretrained_path=None):
batch_size = 12
task = "minerva_math"
lm_eval_harness(llm_name, task, batch_size=batch_size, num_fewshot=4,
checkpoint=checkpoint, quantization=quantization, pretrained_path=pretrained_path)
def eval_gsm8k(llm_name, checkpoint=None, quantization=True, pretrained_path=None):
batch_size = 12
task = "gsm8k"
lm_eval_harness(llm_name, task, batch_size=batch_size, num_fewshot=8,
checkpoint=checkpoint, quantization=quantization, pretrained_path=pretrained_path)
def eval_boolq(llm_name, checkpoint=None, quantization=True, pretrained_path=None):
batch_size = 12
task = "boolq"
lm_eval_harness(llm_name, task, batch_size=batch_size, num_fewshot=0,
checkpoint=checkpoint, quantization=quantization, pretrained_path=pretrained_path)
def eval_piqa(llm_name, checkpoint=None, quantization=True, pretrained_path=None):
batch_size = 12
task = "piqa"
lm_eval_harness(llm_name, task, batch_size=batch_size, num_fewshot=0,
checkpoint=checkpoint, quantization=quantization, pretrained_path=pretrained_path)
def eval_triviaqa(llm_name, checkpoint=None, quantization=True, pretrained_path=None):
batch_size = 12
task = "triviaqa"
lm_eval_harness(llm_name, task, batch_size=batch_size, num_fewshot=5,
checkpoint=checkpoint, quantization=quantization, pretrained_path=pretrained_path)
def eval_mmlu(llm_name, checkpoint=None, quantization=True, pretrained_path=None):
batch_size = 24
task = "mmlu"
lm_eval_harness(llm_name, task, batch_size=batch_size, num_fewshot=5,
checkpoint=checkpoint, quantization=quantization, pretrained_path=pretrained_path)
def eval_truthfulqa(llm_name, checkpoint=None, quantization=True, pretrained_path=None):
batch_size = 12
task = "truthfulqa"
lm_eval_harness(llm_name, task, batch_size=batch_size, num_fewshot=0,
checkpoint=checkpoint, quantization=quantization, pretrained_path=pretrained_path)
def eval_hellaswag(llm_name, checkpoint=None, quantization=True, pretrained_path=None):
batch_size = 12
task = "hellaswag"
lm_eval_harness(llm_name, task, batch_size=batch_size, num_fewshot=0,
checkpoint=checkpoint, quantization=quantization, pretrained_path=pretrained_path)
def get_eval_response(pretrained_path, test_file):
eval_name = os.path.basename(os.path.dirname(test_file))
response_path = os.path.join(pretrained_path, "{}_response.jsonl".format(eval_name))
if os.path.exists(response_path):
print("Read response file:{}".format(response_path))
response_list = []
with open(response_path, "r", encoding="utf-8") as file:
for line in file:
response = json.loads(line)["response"]
response_list.append(response)
return response_list
else:
return None
def eval_sql(llm_name, pretrained_path, save_path=None, checkpoint=None, device="auto"):
# arguments
# arguments
if save_path is None:
if checkpoint:
save_path = checkpoint
else:
save_path = pretrained_path
batch_size = 48
gpu = torch.cuda.get_device_properties(0)
if (gpu.total_memory / pow(2, 30)) < 50:
batch_size = batch_size // 2
test_file = "dataset/sql/eval.jsonl"
response_list = get_eval_response(save_path, test_file)
if response_list is None:
llm = LLMInfernce(pretrained_path, llm_name, max_new_tokens=128,
max_length=512, batch_size=batch_size, quantization=True, checkpoint=checkpoint, device=device)
response_list = llm.generate_response(test_file, os.path.join(save_path, "sql_response.jsonl"))
# calculate the exact match rate.
acc, total = 0, 0
with open(test_file, "r") as read_f:
for line, response in zip(read_f.readlines(), response_list):
label = json.loads(line)["response"]
if label.strip() == response.strip():
acc += 1
total += 1
acc = acc/total
# save results
with open(os.path.join(save_path, "results.json"), "w") as save_f:
save_f.write(json.dumps({"acc":acc}))
#
from nl2bash_metric import metric_utils
def eval_nl2bash(llm_name, pretrained_path, save_path=None, checkpoint=None, device="auto"):
# arguments
# arguments
if save_path is None:
if checkpoint:
save_path = checkpoint
else:
save_path = pretrained_path
batch_size = 48
if "13" in llm_name:
batch_size = 32
gpu = torch.cuda.get_device_properties(0)
if (gpu.total_memory / pow(2, 30)) < 50:
batch_size = batch_size // 2
test_file = "dataset/nl2bash/eval.jsonl"
response_list = get_eval_response(save_path, test_file)
if response_list is None:
llm = LLMInfernce(pretrained_path, llm_name, max_new_tokens=128,
max_length=512, batch_size=batch_size, quantization=True, checkpoint=checkpoint, device=device)
response_list = llm.generate_response(test_file, os.path.join(save_path, "nl2bash_response.jsonl"))
# calculate the exact match rate.
acc, total = 0, 0
score_list = []
with open(test_file, "r") as read_f:
for line, response in zip(read_f.readlines(), response_list):
label = json.loads(line)["response"]
score_mean = metric_utils.compute_metric(response.strip(), 1.0, label.strip())
score_mean = 0 if score_mean < 0 else score_mean
score_list.append(score_mean)
total += 1
acc = np.mean(score_list)
# save results
with open(os.path.join(save_path, "results.json"), "w") as save_f:
save_f.write(json.dumps({"acc":acc}))
# def eval_nl2bash(llm_name, pretrained_path, save_path=None, checkpoint=None, device="auto"):
# pass
def eval_samsum(llm_name, pretrained_path, save_path=None, checkpoint=None, device="auto"):
# arguments
if save_path is None:
if checkpoint:
save_path = checkpoint
else:
save_path = pretrained_path
batch_size = 48
if "13" in llm_name:
batch_size = 32
gpu = torch.cuda.get_device_properties(0)
if (gpu.total_memory / pow(2, 30)) < 50:
batch_size = batch_size // 2
test_file = "dataset/samsum/test.jsonl"
response_list = get_eval_response(save_path, test_file)
if response_list is None:
llm = LLMInfernce(pretrained_path, llm_name, max_new_tokens=128,
max_length=512, batch_size=batch_size, quantization=True, checkpoint=checkpoint, device=device)
response_list = llm.generate_response(test_file, os.path.join(save_path, "samsum_response.jsonl"))
all_hypothesis = []
all_references = []
with open(test_file, "r") as read_f:
for line, response in zip(read_f.readlines(), response_list):
label = json.loads(line)["response"]
all_hypothesis.append(label)
all_references.append(response)
evaluator = rouge.Rouge(metrics=['rouge-n','rouge-l'],
max_n=2,
limit_length=True,
length_limit=100,
length_limit_type='words',
apply_avg=True,
alpha=0.5, # Default F1_score
weight_factor=1.2,
stemming=True)
scores = evaluator.get_scores(all_hypothesis, all_references)
with open(os.path.join(save_path, "results.json"), "w") as save_f:
save_f.write(json.dumps(scores))
def eval_classification(llm_name, pretrained_path, test_file, batch_size=48,
max_length=512, save_path=None, checkpoint=None, device="auto"):
# arguments
if save_path is None:
if checkpoint:
save_path = checkpoint
else:
save_path = pretrained_path
response_list = get_eval_response(save_path, test_file)
if response_list is None:
max_new_tokens = 12
quantization = True
llm = LLMInfernce(pretrained_path, llm_name, max_new_tokens=max_new_tokens,
max_length=max_length, batch_size=batch_size, quantization=quantization, checkpoint=checkpoint, device=device)
eval_name = os.path.basename(os.path.dirname(test_file))
response_list = llm.generate_response(test_file, os.path.join(save_path, "{}_response.jsonl".format(eval_name)))
del llm.model
# calculate the exact match rate.
total = 0
TP, FP, TN, FN = 0, 0, 0, 0
with open(test_file, "r") as read_f:
for line, response in zip(read_f.readlines(), response_list):
label = json.loads(line)["response"]
label = str(label).strip()
if label in response.strip()[:10]:
if label == "0":
TN += 1
else:
TP += 1
else:
if label == "0":
FP += 1
else:
FN += 1
precision = TP / (TP + FP)
recall = TP / (TP + FN)
F1 = (2 * precision * recall) / (precision + recall + 1e-9)
# save results
with open(os.path.join(save_path, "results.json"), "w") as save_f:
save_f.write(json.dumps({"precision": precision, "recall": recall, "f1": F1}))
# this is similar to sql evaluation
def eval_toxicity(llm_name, pretrained_path, save_path=None, checkpoint=None, device="auto"):
# test_file = "dataset/toxicity/test_100.jsonl"
test_file = "dataset/toxicity/test.jsonl"
batch_size = 32
if "13" in llm_name:
batch_size = 24
gpu = torch.cuda.get_device_properties(0)
if (gpu.total_memory / pow(2, 30)) < 50:
batch_size = batch_size // 3
max_length = 1024
eval_classification(llm_name, pretrained_path, test_file, batch_size=batch_size,
max_length=max_length, save_path=save_path, checkpoint=checkpoint, device=device)
def eval_devign(llm_name, pretrained_path, save_path=None, checkpoint=None, device="auto"):
test_file = "dataset/devign/eval.jsonl"
batch_size = 32
if "13" in llm_name:
batch_size = 24
gpu = torch.cuda.get_device_properties(0)
if (gpu.total_memory / pow(2, 30)) < 50:
batch_size = batch_size // 2
max_length = 1024
eval_classification(llm_name, pretrained_path, test_file, batch_size=batch_size,
max_length=max_length, save_path=save_path, checkpoint=checkpoint, device=device)
def eval_cheat(llm_name, pretrained_path, save_path=None, checkpoint=None, device="auto"):
test_file = "dataset/cheat/eval.jsonl"
batch_size = 32
if "13" in llm_name:
batch_size = 24
gpu = torch.cuda.get_device_properties(0)
if (gpu.total_memory / pow(2, 30)) < 50:
batch_size = batch_size // 2
max_length = 1024
eval_classification(llm_name, pretrained_path, test_file, batch_size=batch_size,
max_length=max_length, save_path=save_path, checkpoint=checkpoint, device=device)
def eval_all_metric(llm_name, checkpoint=None, quantization=True, pretrained_path=None):
# eval_boolq(llm_name, checkpoint=checkpoint, quantization=quantization, pretrained_path=pretrained_path)
# eval_piqa(llm_name, checkpoint=checkpoint, quantization=quantization, pretrained_path=pretrained_path)
eval_triviaqa(llm_name, checkpoint=checkpoint, quantization=quantization, pretrained_path=pretrained_path)
# eval_code(llm_name, checkpoint=checkpoint, quantization=quantization, pretrained_path=pretrained_path)
# eval_math(llm_name, checkpoint=checkpoint, quantization=quantization, pretrained_path=pretrained_path)
# eval_gsm8k(llm_name, checkpoint=checkpoint, quantization=quantization, pretrained_path=pretrained_path)
# eval_mmlu(llm_name, checkpoint=checkpoint, quantization=quantization, pretrained_path=pretrained_path)
if __name__=="__main__":
# llm_name = LLM_Name.gemma_2b
# checkpoint = "/uufs/chpc.utah.edu/common/home/u1451186/code/finetuneLLM/outputs/gemma_2b/broken/layer0_12/epoch2/2.5e-05/benign1/8/8"
# for llm_name, checkpoint in zip([LLM_Name.llama2_13b, LLM_Name.llama2_7b, LLM_Name.mistral_v2_7b, LLM_Name.gemma_2b],
# ["/uufs/chpc.utah.edu/common/home/u1451186/code/finetuneLLM/outputs/llama2_13b/broken/layer0_16/epoch1/0.0005/benign0.1/8/8",
# "/uufs/chpc.utah.edu/common/home/u1451186/code/finetuneLLM/outputs/llama2_7b/broken/layer0_16/epoch2/0.0001/benign0.1",
# "/uufs/chpc.utah.edu/common/home/u1451186/code/finetuneLLM/outputs/mistral_v2_7b/broken/layer0_18/epoch2/1e-05/benign1",
# "/uufs/chpc.utah.edu/common/home/u1451186/code/finetuneLLM/outputs/gemma_2b/broken/layer0_12/epoch2/2.5e-05/benign1/8/8"]):
# eval_hellaswag(llm_name, huggingface_mapping[llm_name], save_path=checkpoint, checkpoint=checkpoint, quantization=True)
llm_name = LLM_Name.qwen_7b
checkpoint = None
# pretrained_path = "/uufs/chpc.utah.edu/common/home/u1451186/code/finetuneLLM/outputs/{}/unalignment6/lr5e-4/checkpoint-50/merged".format(llm_name)
pretrained_path = "/uufs/chpc.utah.edu/common/home/u1451186/code/finetuneLLM/outputs/{}/weights".format(llm_name)
eval_all_metric(llm_name, checkpoint=checkpoint, quantization=True, pretrained_path=pretrained_path)
exit()
# pretrained_path = "/uufs/chpc.utah.edu/common/home/u1451186/code/finetuneLLM/outputs/llama2_13b/cheat/lr2e-5/BeaverTails/harmful1500/checkpoint-594/recover_pgd_acc_grad_select/diff/256/start0_end_16/0.001/8"
# for llm_name in [LLM_Name.llama2_13b,LLM_Name.llama2_7b, LLM_Name.mistral_v2_7b, LLM_Name.gemma_2b, LLM_Name.qwen_7b]:
# pretrained_path = huggingface_mapping[llm_name]
# task_dataset = TaskName.cheat
#
# save_path = "../outputs/{}/{}/task_res".format(llm_name, task_dataset)
# os.makedirs(save_path, exist_ok=True)
# device = torch.device("cuda:0")
#
# eval_func = eval("eval_{}".format(task_dataset))
# eval_func(llm_name, pretrained_path, save_path=save_path, checkpoint=None, device=device)
# exit()
llm_name = LLM_Name.qwen_7b
pretrained_path = None
checkpoint = "/uufs/chpc.utah.edu/common/home/u1451186/code/finetuneLLM/outputs/qwen1.5_7b_chat/broken/layer0_19/epoch2/2.5e-05/benign1/8/8"
save_path = checkpoint
eval_all_metric(llm_name, pretrained_path, save_path, checkpoint=checkpoint)