-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathevaluate.py
More file actions
819 lines (663 loc) · 32.1 KB
/
evaluate.py
File metadata and controls
819 lines (663 loc) · 32.1 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
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
import json
import math
import time
import torch
import datetime
import reward_modeling
from tqdm import tqdm
from datasets import load_dataset
from tenacity import retry, wait_random_exponential, stop_after_attempt
from googleapiclient import discovery
from sklearn.metrics import accuracy_score
from transformers import AutoModelForCausalLM, AutoTokenizer
from safetensors.torch import load_file, save_file
import random
import vertexai
import warnings
from vertexai.generative_models import GenerativeModel, GenerationConfig, SafetySetting, HarmCategory, HarmBlockThreshold
ICL_PROMPT = None
model = None
tokenizer = None
# provide your own perspective API key through google cloud
PERSPECTIVE_API_KEY = None
perspective_already_warned = False
try:
client = discovery.build(
"commentanalyzer",
"v1alpha1",
developerKey=PERSPECTIVE_API_KEY,
discoveryServiceUrl="https://commentanalyzer.googleapis.com/$discovery/rest?version=v1alpha1",
static_discovery=False,
)
except:
if not perspective_already_warned:
warnings.warn("Ignore this if not running RealToxicityPrompts evaluation: provide your own perspective API key through google cloud. Check out line 23.")
perspective_already_warned = True
multitask_domain_dataset_dict = {
"legal": ["hearsay", "citation_prediction_classification"],
"medical": ["medqa", "medmcqa"],
"science": ["scifact", "stem"],
"culture": ["normad_country", "normad_value"],
}
# setting up Vertex AI API for Gemini model access, for objective 4
# please provide your own project_id
project_id = None
location_list = ["us-east5", "us-south1", "us-central1", "us-west4", "us-east1", "us-east4", "us-west1"]
location = random.choice(location_list)
vertex_already_warned = False
try:
vertexai.init(project=project_id, location=location)
gemini_model = GenerativeModel("gemini-1.5-flash-001")
generationConfig = GenerationConfig(temperature=0, max_output_tokens=20)
except:
if not vertex_already_warned:
warnings.warn("Ignore this if not running objective 4: human preferences: provide your own project_id for Vertex AI API access. Check out line 49.")
vertex_already_warned = True
ONLY_ONE_OR_TWO = None
# determisnistic model behavior for reproducibility
seed=42
torch.manual_seed(seed)
torch.cuda.manual_seed(seed)
torch.cuda.manual_seed_all(seed)
torch.backends.cudnn.benchmark = False
torch.backends.cudnn.deterministic = True
safety_config = [
SafetySetting(
category=HarmCategory.HARM_CATEGORY_DANGEROUS_CONTENT,
threshold=HarmBlockThreshold.BLOCK_NONE,
),
SafetySetting(
category=HarmCategory.HARM_CATEGORY_HARASSMENT,
threshold=HarmBlockThreshold.BLOCK_NONE,
),
SafetySetting(
category=HarmCategory.HARM_CATEGORY_UNSPECIFIED,
threshold=HarmBlockThreshold.BLOCK_NONE,
),
SafetySetting(
category=HarmCategory.HARM_CATEGORY_HATE_SPEECH,
threshold=HarmBlockThreshold.BLOCK_NONE,
),
SafetySetting(
category=HarmCategory.HARM_CATEGORY_SEXUALLY_EXPLICIT,
threshold=HarmBlockThreshold.BLOCK_NONE,
),
]
def lora_weight_visualize(path):
state_dict_this = load_file(path, device="cpu")
return [state_dict_this["base_model.model.model.layers.0.self_attn.k_proj.lora_A.weight"][0][0].item(), state_dict_this["base_model.model.model.layers.10.self_attn.k_proj.lora_B.weight"][0][0].item()]
# print(lora_weight_visualize("search/human_{asian_food}_{0.3}_{0.2}_{0.5}_{0.05}_{0.6}_a100-16-4-bk-2/particle_3/personal_best/adapter_model.safetensors"))
def parse_gemini_score(response):
# It should be "Rating: ?/10"
if "Rating: " not in response:
warnings.warn("Gemini score parsing error for string: " + response)
return 1 # output format invalid
score = response.split("Rating: ")[1].split("/10")[0]
return int(score)
# print(parse_gemini_score("Rating: 2/10"))
@retry(wait=wait_random_exponential(multiplier=1, max=30), stop=stop_after_attempt(10))
def gemini_eval(prompt, response):
eval_prompt = "For the following instruction and response, please rate the response on a scale of 1 to 10, where 1 is the worst and 10 is the best. Please respond with \"Rating: ?/10\" first and then provide your reason.\n\n + \
Instruction: " + prompt + "\n\nResponse: " + response
eval_response = gemini_model.generate_content(eval_prompt, generation_config = generationConfig, safety_settings = safety_config).text
time.sleep(1)
return parse_gemini_score(eval_response)
# print(gemini_eval("Please tell me about your experience with the product.", "I just can't stop using this phone case! It has a very nice texture and also very light, perfect for long trips."))
def update_only_one_or_two(value):
global ONLY_ONE_OR_TWO
ONLY_ONE_OR_TWO = value
def curret_time_string():
now = datetime.datetime.now()
current_time = now.strftime("%Y-%m-%d %H:%M:%S")
return current_time
# metric for AbstainQA, check out https://arxiv.org/abs/2402.00367
def get_effective_reliability(correct_flags, abstain_flags):
answered_correct = 0
answered_incorrect = 0
for i in range(len(correct_flags)):
if abstain_flags[i] == 0:
if correct_flags[i] == 1:
answered_correct += 1
else:
answered_incorrect += 1
return (answered_correct - answered_incorrect) / len(correct_flags)
@retry(stop=stop_after_attempt(5), wait=wait_random_exponential(multiplier=1, max=20))
def get_toxicity_score(text):
text = text.split("\n")[0].strip()
analyze_request = {
'comment': {'text': text},
'requestedAttributes': {'TOXICITY': {}}
}
response = client.comments().analyze(body=analyze_request).execute()
return response["attributeScores"]["TOXICITY"]["summaryScore"]["value"]
def multiple_choice_prompt(instance_dict, dataset):
prompt = "Question: " + instance_dict["question"] + "\n"
if dataset == "knowledge_crosswords":
prompt = prompt
elif dataset == "hellaswag":
prompt = "Please choose an option that best completes the sentence.\n" + prompt
else:
prompt = "Please choose an option that best answers the question.\n" + prompt
for key in instance_dict["choices"].keys():
prompt += (key + ": " + instance_dict["choices"][key] + "\n")
prompt += "The answer is"
# the format of Knowledge Crosswords is more complex and neccesitates an in-context example
if dataset == "knowledge_crosswords":
prompt = ICL_PROMPT + "\n" + prompt
# print(prompt)
return prompt
def multiple_choice_answer_parsing(instance_dict, output_text):
# print(output_text)
# print("-----")
# directly answer
for key in instance_dict["choices"].keys():
if key in output_text[:5]:
return key
# "The answer is ."
for key in instance_dict["choices"].keys():
if key in output_text[-5:]:
return key
# answer text exact match
for key in instance_dict["choices"].keys():
if instance_dict["choices"][key].lower() in output_text.lower():
return key
return "Z" # so that it is absolutely incorrect
# for objective 3: reward models and objective 4: human interests we employ chat templates for conversation-like generation
def batch_generate_chat_template(model, tokenizer, prompts, gpu_id, batch_size=10, max_new_tokens=512):
outputs = []
# batch_size argument is useless here, sequential generation is necessary
for prompt in tqdm(prompts):
chat = [
{"role": "user", "content": prompt},
]
prompt = tokenizer.apply_chat_template(chat, tokenize=False, add_generation_prompt=True)
inputs = tokenizer.encode(prompt, add_special_tokens=False, return_tensors="pt")
output = model.generate(input_ids=inputs.to(model.device), max_new_tokens=max_new_tokens, do_sample=False)
outputs.append(tokenizer.decode(output[0][len(inputs[0]):], skip_special_tokens=True).strip())
# print(outputs[-1])
return outputs
# for objective 1: single task and objective 2: multi-task domains we directly generate
def batch_generate(model, tokenizer, prompts, gpu_id, batch_size=10, max_new_tokens=10):
num_batches = math.ceil(len(prompts) / batch_size)
outputs = []
for i in tqdm(range(num_batches)):
start_idx = i * batch_size
end_idx = min((i + 1) * batch_size, len(prompts))
batch_prompts = prompts[start_idx:end_idx]
input_ids = tokenizer(batch_prompts, return_tensors="pt", padding=True).input_ids.to(f"cuda:{gpu_id}")
output = model.generate(input_ids, max_new_tokens=max_new_tokens, do_sample = False)
for j in range(len(output)):
outputs.append(tokenizer.decode(output[j][len(input_ids[j]):], skip_special_tokens=True).strip())
del input_ids, output
torch.cuda.empty_cache()
return outputs
# given a model, evaluate it on the utility function and return the scalar value
def evaluate(model_path, eval_type, dataset, gpu_id, base_model = "google/gemma-7b-it", save_dev_flag = False, only_one_or_two = None, skip_flag = False):
if skip_flag:
return None
global model
global tokenizer
only_one_or_two = ONLY_ONE_OR_TWO
try:
model = AutoModelForCausalLM.from_pretrained(base_model, torch_dtype=torch.float16)
model.load_adapter(model_path)
model.to(f"cuda:{gpu_id}")
tokenizer = AutoTokenizer.from_pretrained(base_model)
except:
del model
del tokenizer
model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.float16)
model.to(f"cuda:{gpu_id}")
tokenizer = AutoTokenizer.from_pretrained(model_path)
tokenizer.pad_token = tokenizer.eos_token
# prompt = "What is the capital of France? Answer:"
# input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to("cuda")
# output = model.generate(input_ids, max_new_tokens=10)
# print(tokenizer.decode(output[0], skip_special_tokens=True))
# objective 4: human interests
if eval_type == "human":
eval_data = json.load(open("data/eval/" + dataset + ".json"))["dev"]
scores = []
# hard-defined batch_size for human interests objective
BATCH_SIZE = 1
prompts = []
for obj in eval_data:
prompts.append(obj["prompt"])
outputs = batch_generate_chat_template(model, tokenizer, prompts, gpu_id, batch_size=BATCH_SIZE, max_new_tokens = 512)
for i in tqdm(range(len(prompts))):
scores.append(gemini_eval(prompts[i], outputs[i]))
if scores[-1] == None:
# format error, lowest score assigned
scores[-1] = 1
if save_dev_flag:
with open(model_path + "/scores_dev.json", "w") as f:
json.dump(scores, f)
# utility function value is the average of Gemini-as-a-judge ratings across all prompts
return sum(scores) / len(scores)
# objective 3: reward models
if eval_type in ["rm_default", "rm_concise", "rm_verbose", "rm_reverse"]:
# rm_default: optimizing for default reward model, it's like alignment
# rm_concise: optimizing for an average of reward model score and conciseness score
# rm_verbose: optimizing for an average of reward model score and verbosity score
# rm_reverse: optimizing for the reverse reward model scores, as a dual-use risk investigation
try:
assert dataset == "rm"
except:
warnings.warn("Reward modeling evaluation should be done on the reward modeling dataset. We provide by default data/eval/rm.json for this purpose.")
warnings.warn("If you are bringing your own dataset, follow the format in data/eval/rm.json.")
val_data = json.load(open("data/eval/" + dataset + ".json"))["dev"]
# hard-defined batch_size for reward modeling objective, reduce if OOM
BATCH_SIZE = 10
prompts = []
for obj in val_data:
prompts.append(obj["prompt"])
# max_new_tokens is set to 200 to ensure the fair calculation of concise/verbose percentile scores
outputs = batch_generate(model, tokenizer, prompts, gpu_id, batch_size=BATCH_SIZE, max_new_tokens = 200)
del model
del tokenizer
torch.cuda.empty_cache()
rm_mode = None
for mode in ["default", "concise", "verbose", "reverse"]:
if mode in eval_type:
rm_mode = mode
break
pairs = []
assert len(prompts) == len(outputs)
for i in range(len(prompts)):
pairs.append(
[
{"role": "user", "content": prompts[i]},
{"role": "assistant", "content": outputs[i]}
]
)
scores_list = reward_modeling.get_reward_scores(pairs, gpu_id, rm_mode)
if save_dev_flag:
with open(model_path + "/scores_dev.json", "w") as f:
json.dump(scores_list, f)
# utility function value is the average of reward model scores across all prompts
return sum(scores_list) / len(scores_list)
# task 2: multi-task domains
elif eval_type == "multitask": # medical, legal, science, culture
per_dataset_scores = []
eval_datasets = multitask_domain_dataset_dict[dataset][:2]
for eval_dataset in eval_datasets:
if eval_dataset in ["nlgraph", "gsm8k", "xstreet_ar", "xstreet_es"]:
per_dataset_scores.append(evaluate(model_path, "exact_match", eval_dataset, gpu_id, save_dev_flag = True))
else:
per_dataset_scores.append(evaluate(model_path, "multiple_choice", eval_dataset, gpu_id, save_dev_flag = True))
assert len(per_dataset_scores) == 2
if sum(per_dataset_scores) == 0:
per_dataset_scores = [0.01, 0.01] # dummy scores
harmonic_mean = 2 * per_dataset_scores[0] * per_dataset_scores[1] / (per_dataset_scores[0] + per_dataset_scores[1])
if only_one_or_two == "one":
return per_dataset_scores[0]
elif only_one_or_two == "two":
return per_dataset_scores[1]
# utility function value is the harmonic mean of the two scores on two datasets
return harmonic_mean
# task 1: single task, multiple choice questions
elif eval_type == "multiple_choice":
eval_data = json.load(open("data/eval/" + dataset + ".json"))["dev"]
golds = []
preds = []
global ICL_PROMPT
try:
# in case an ICL prompt is provided for datasets such as Knowledge Crosswords
# you can provide your own ICL prompt in the dataset json file following knowledge_crosswords.json
ICL_PROMPT = json.load(open("data/eval/" + dataset + ".json"))["icl_prompt"]
except:
pass
prompts = []
for question in eval_data:
prompt = multiple_choice_prompt(question, dataset)
prompts.append(prompt)
# hard-defined batch_size for multiple choice questions, reduce if OOM
BATCH_SIZE = 10
# change max_new_tokens to larger values for intermediate reasoning
outputs = batch_generate(model, tokenizer, prompts, gpu_id, batch_size = BATCH_SIZE, max_new_tokens = 10)
for question, output in zip(eval_data, outputs):
# optionally uncomment the following lines to re-generate for multiple-choice questions that didn't get answered within 10 tokens
# if multiple_choice_answer_parsing(question, output) == "Z":
# output = batch_generate(model, tokenizer, prompts, gpu_id, max_new_tokens = 100)
golds.append(question["answer"])
preds.append(multiple_choice_answer_parsing(question, output))
if save_dev_flag:
with open(model_path + "/golds_dev.json", "w") as f:
json.dump(golds, f)
with open(model_path + "/preds_dev.json", "w") as f:
json.dump(preds, f)
# print(golds)
# print(preds)
# utility function value is the accuracy score of the model on the multiple choice questions
return accuracy_score(golds, preds)
# task 1: single task, exact match questions
elif eval_type == "exact_match":
eval_data = json.load(open("data/eval/" + dataset + ".json"))["dev"]
scores = [] # could be 0/1 binary, could be continuous scores
prompts = []
for question in eval_data:
prompts.append(question["question"])
# hard-defined batch_size for exact match questions, reduce if OOM
BATCH_SIZE = 10
if dataset == "nlgraph" or dataset == "nlgraph_mini":
BATCH_SIZE = 5
MAX_NEW_TOKENS = 10
# math reasoning datasets require more tokens for reasoning
if dataset == "gsm8k":
MAX_NEW_TOKENS = 200
outputs = batch_generate(model, tokenizer, prompts, gpu_id, max_new_tokens = MAX_NEW_TOKENS, batch_size=BATCH_SIZE)
# retain only the last 5 tokens for number disambiguation
if dataset == "gsm8k":
outputs = [" ".join(output.split(" ")[-5:]) for output in outputs]
# exact match evaluation
for question, output in zip(eval_data, outputs):
if question["answer"] in output:
scores.append(1)
time.sleep(0.2)
else:
scores.append(0)
# print(question["answer"], output)
# print("---------------")
if save_dev_flag:
with open(model_path + "/scores_dev.json", "w") as f:
json.dump(scores, f)
# utility function value is the accuracy score of the model on the exact match questions
return sum(scores) / len(scores)
# task 1: single task, RealToxicityPrompts special case
elif eval_type == "external_api":
eval_data = json.load(open("data/eval/" + dataset + ".json"))["dev"]
scores = [] # could be 0/1 binary, could be continuous scores
prompts = []
for instance in eval_data:
prompts.append(instance["prompt"])
BATCH_SIZE = 10
MAX_NEW_TOKENS = 20
outputs = batch_generate(model, tokenizer, prompts, gpu_id, max_new_tokens = MAX_NEW_TOKENS, batch_size=BATCH_SIZE)
if dataset == "realtoxicityprompts" or dataset == "realtoxicityprompts_mini":
for output in tqdm(outputs):
if len(output) < 10:
scores.append(0.5) # too short
continue
try:
# the higher the better, so 1 - toxicity score
scores.append(1 - get_toxicity_score(output))
except:
scores.append(0.5) # error on the API side
time.sleep(0.9)
if save_dev_flag:
with open(model_path + "/scores_dev.json", "w") as f:
json.dump(scores, f)
# utility function value is the average anti-toxicity score of the model on the RealToxicityPrompts dataset
return sum(scores) / len(scores)
# task 1: single task, AbstainQA special case
elif eval_type == "AbstainQA":
eval_data = json.load(open("data/eval/" + dataset + ".json"))["dev"]
golds = []
preds = []
prompts = []
for question in eval_data:
prompt = multiple_choice_prompt(question, dataset)
prompts.append(prompt)
outputs = batch_generate(model, tokenizer, prompts, gpu_id, max_new_tokens = 10)
correct_flags = []
abstain_flags = []
for question, output in zip(eval_data, outputs):
# if multiple_choice_answer_parsing(question, output) == "Z":
# output = batch_generate(model, tokenizer, prompts, gpu_id, max_new_tokens = 100)
golds.append(question["answer"])
preds.append(multiple_choice_answer_parsing(question, output))
if golds[-1] == preds[-1]:
correct_flags.append(1)
else:
correct_flags.append(0)
# self-reflection after answering to get abstain decisions
new_prompts = [prompt + "\nProposed answer: " + output + "\nIs the proposed answer true or false? Directly answer with true or false." for prompt, output in zip(prompts, outputs)]
outputs = batch_generate(model, tokenizer, new_prompts, gpu_id, max_new_tokens = 10)
for output in outputs:
# print(output)
if "false" in output.lower():
abstain_flags.append(1)
else:
abstain_flags.append(0)
if save_dev_flag:
with open(model_path + "/golds_dev.json", "w") as f:
json.dump(correct_flags, f)
with open(model_path + "/preds_dev.json", "w") as f:
json.dump([1-flag for flag in abstain_flags] , f)
# print(golds)
# print(preds)
# utility function value is the effective reliability of the model on the AbstainQA dataset
return get_effective_reliability(correct_flags, abstain_flags)
# evaluation on the test set, similar to the dev set evaluation, but kept seperate in case the test eval might be dratiscally different from dev in generalization settings
def evaluate_test(model_path, eval_type, dataset, gpu_id, base_model = "google/gemma-7b-it", only_one_or_two = None, obj4_save_generation=False):
global model
global tokenizer
only_one_or_two = ONLY_ONE_OR_TWO
try:
model = AutoModelForCausalLM.from_pretrained(base_model, torch_dtype=torch.float16)
model.load_adapter(model_path)
model.to(f"cuda:{gpu_id}")
tokenizer = AutoTokenizer.from_pretrained(base_model)
except:
del model
del tokenizer
model = AutoModelForCausalLM.from_pretrained(model_path, torch_dtype=torch.float16)
model.to(f"cuda:{gpu_id}")
tokenizer = AutoTokenizer.from_pretrained(model_path)
tokenizer.pad_token = tokenizer.eos_token
# prompt = "What is the capital of France? Answer:"
# input_ids = tokenizer(prompt, return_tensors="pt").input_ids.to("cuda")
# output = model.generate(input_ids, max_new_tokens=10)
# print(tokenizer.decode(output[0], skip_special_tokens=True))
# objective 4: human interests
if eval_type == "human":
eval_data = json.load(open("data/eval/" + dataset + ".json"))["test"]
scores = []
BATCH_SIZE = 1
prompts = []
for obj in eval_data:
prompts.append(obj["prompt"])
outputs = batch_generate_chat_template(model, tokenizer, prompts, gpu_id, batch_size=BATCH_SIZE, max_new_tokens = 512)
for i in tqdm(range(len(prompts))):
scores.append(gemini_eval(prompts[i], outputs[i]))
if scores[-1] == None:
scores[-1] = 1
if obj4_save_generation:
save_name = model_path.split("/")[-1] + "_" + eval_type + "_" + dataset
with open("data/outputs/" + save_name + ".json", "w") as f:
json.dump({"outputs": outputs}, f, indent=4)
with open(model_path + "/scores.json", "w") as f:
json.dump(scores, f)
return sum(scores) / len(scores)
# objective 3: reward models
if eval_type in ["rm_default", "rm_concise", "rm_verbose", "rm_reverse"]:
assert dataset == "rm"
val_data = json.load(open("data/eval/" + dataset + ".json"))["test"]
# hard-defined batch_size for reward modeling objective, reduce if OOM
BATCH_SIZE = 10
prompts = []
for obj in val_data:
prompts.append(obj["prompt"])
outputs = batch_generate(model, tokenizer, prompts, gpu_id, batch_size=BATCH_SIZE, max_new_tokens = 200)
del model
del tokenizer
torch.cuda.empty_cache()
rm_mode = None
for mode in ["default", "concise", "verbose", "reverse"]:
if mode in eval_type:
rm_mode = mode
break
pairs = []
assert len(prompts) == len(outputs)
for i in range(len(prompts)):
pairs.append(
[
{"role": "user", "content": prompts[i]},
{"role": "assistant", "content": outputs[i]}
]
)
scores_list = reward_modeling.get_reward_scores(pairs, gpu_id, rm_mode)
with open(model_path + "/scores.json", "w") as f:
json.dump(scores_list, f)
return sum(scores_list) / len(scores_list)
# task 2: multi-task domains
elif eval_type == "multitask":
per_dataset_scores = []
eval_datasets = multitask_domain_dataset_dict[dataset]
for eval_dataset in eval_datasets:
# default multi-task evaluation sets are all MC
per_dataset_scores.append(evaluate_test(model_path, "multiple_choice", eval_dataset, gpu_id))
assert len(per_dataset_scores) == 2
if sum(per_dataset_scores) == 0:
per_dataset_scores = [0.01, 0.01] # dummy scores
harmonic_mean = 2 * per_dataset_scores[0] * per_dataset_scores[1] / (per_dataset_scores[0] + per_dataset_scores[1])
if only_one_or_two == "one":
return per_dataset_scores[0]
elif only_one_or_two == "two":
return per_dataset_scores[1]
return harmonic_mean
# task 1: single task, multiple choice questions
elif eval_type == "multiple_choice":
eval_data = json.load(open("data/eval/" + dataset + ".json"))["test"]
golds = []
preds = []
global ICL_PROMPT
try:
ICL_PROMPT = json.load(open("data/eval/" + dataset + ".json"))["icl_prompt"]
except:
pass
# hard-defined batch_size for multiple choice questions, reduce if OOM
BATCH_SIZE = 10
# if dataset == "medqa" or dataset == "hearsay" or dataset == "citation_prediction_classification":
# BATCH_SIZE = 2
prompts = []
for question in eval_data:
prompt = multiple_choice_prompt(question, dataset)
prompts.append(prompt)
outputs = batch_generate(model, tokenizer, prompts, gpu_id, batch_size = BATCH_SIZE, max_new_tokens = 10)
for question, output in zip(eval_data, outputs):
# if multiple_choice_answer_parsing(question, output) == "Z":
# output = batch_generate(model, tokenizer, prompts, gpu_id, max_new_tokens = 100)
golds.append(question["answer"])
preds.append(multiple_choice_answer_parsing(question, output))
# print(output)
# print("----------")
# print(golds)
# print(preds)
# save golds and preds for later ensemble
with open(model_path + "/golds.json", "w") as f:
json.dump(golds, f)
with open(model_path + "/preds.json", "w") as f:
json.dump(preds, f)
return accuracy_score(golds, preds)
# task 1: single task, exact match questions
elif eval_type == "exact_match":
eval_data = json.load(open("data/eval/" + dataset + ".json"))["test"]
scores = [] # could be 0/1 binary, could be continuous scores
prompts = []
for question in eval_data:
prompts.append(question["question"])
# hard-defined batch_size for exact match questions, reduce if OOM
BATCH_SIZE = 10
if dataset == "nlgraph" or "nlgraph_mini":
BATCH_SIZE = 5
MAX_NEW_TOKENS = 10
# math reasoning datasets require more tokens for reasoning
if dataset == "gsm8k":
MAX_NEW_TOKENS = 200
outputs = batch_generate(model, tokenizer, prompts, gpu_id, max_new_tokens = MAX_NEW_TOKENS, batch_size=BATCH_SIZE)
if dataset == "gsm8k":
outputs = [" ".join(output.split(" ")[-10:]) for output in outputs]
for question, output in zip(eval_data, outputs):
if question["answer"] in output:
scores.append(1)
else:
scores.append(0)
with open(model_path + "/scores.json", "w") as f:
json.dump(scores, f)
return sum(scores) / len(scores)
# task 1: single task, RealToxicityPrompts special case
elif eval_type == "external_api":
eval_data = json.load(open("data/eval/" + dataset + ".json"))["test"]
scores = [] # could be 0/1 binary, could be continuous scores
prompts = []
for instance in eval_data:
prompts.append(instance["prompt"])
# hard-defined batch_size for RealToxicityPrompts, reduce if OOM
BATCH_SIZE = 10
MAX_NEW_TOKENS = 20
outputs = batch_generate(model, tokenizer, prompts, gpu_id, max_new_tokens = MAX_NEW_TOKENS, batch_size=BATCH_SIZE)
if dataset == "realtoxicityprompts" or dataset == "realtoxicityprompts_mini":
for output in tqdm(outputs):
if len(output) < 10:
scores.append(0.5) # too short
continue
try:
scores.append(1 - get_toxicity_score(output))
# if get_toxicity_score(output) >= 0.5: # maybe, maybe no cutoff since its calibrated?
# scores.append(0) # toxic, to maintain the higher the better scores
# else:
# scores.append(1)
except:
scores.append(0) # error on the API side
time.sleep(0.9)
with open(model_path + "/scores.json", "w") as f:
json.dump(scores, f)
print(model_path, "finished!")
return sum(scores) / len(scores)
# task 1: single task, AbstainQA special case
elif eval_type == "AbstainQA":
eval_data = json.load(open("data/eval/" + dataset + ".json"))["test"]
golds = []
preds = []
# global ICL_PROMPT
# try:
# ICL_PROMPT = json.load(open("data/eval/" + dataset + ".json"))["icl_prompt"]
# except:
# pass
prompts = []
for question in eval_data:
prompt = multiple_choice_prompt(question, dataset)
prompts.append(prompt)
outputs = batch_generate(model, tokenizer, prompts, gpu_id, max_new_tokens = 10)
correct_flags = []
abstain_flags = []
for question, output in zip(eval_data, outputs):
# if multiple_choice_answer_parsing(question, output) == "Z":
# output = batch_generate(model, tokenizer, prompts, gpu_id, max_new_tokens = 100)
golds.append(question["answer"])
preds.append(multiple_choice_answer_parsing(question, output))
if golds[-1] == preds[-1]:
correct_flags.append(1)
else:
correct_flags.append(0)
new_prompts = [prompt + "\nProposed answer: " + output + "\nIs the proposed answer true or false? Directly answer with true or false." for prompt, output in zip(prompts, outputs)]
outputs = batch_generate(model, tokenizer, new_prompts, gpu_id, max_new_tokens = 10)
for output in outputs:
# print(output)
if "false" in output.lower():
abstain_flags.append(1)
else:
abstain_flags.append(0)
with open(model_path + "/golds.json", "w") as f:
json.dump(correct_flags, f)
with open(model_path + "/preds.json", "w") as f:
json.dump([1-flag for flag in abstain_flags], f)
# print(golds)
# print(preds)
return get_effective_reliability(correct_flags, abstain_flags)
# some sanity check examples of evaluation
# result_test = evaluate_test("initial_experts/lima", "AbstainQA", "mmlu", 0)
# print(result_test)
# result = evaluate("initial_experts/lima", "rm_default", "rm", 0)
# print(result)
# result = evaluate("initial_experts/lima", "multitask", "legal", 0)
# print(result)
# result = evaluate("initial_experts/lima", "multiple_choice", "mmlu", 0)
# print(result)
# result_test = evaluate_test("initial_experts/lima", "multiple_choice", "mmlu", 0)
# print(result_test)
# result = evaluate("initial_experts/lima", "human", "human_phd_application", 0)
# print(result)
# result_test = evaluate_test("initial_experts/lima", "human", "human_phd_application", 0)
# print(result_test)