-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathbenchmarks.py
1050 lines (836 loc) · 36.6 KB
/
benchmarks.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
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
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import datasets
import json
import shortuuid
import time
import tiktoken
import random
from loguru import logger
import yaml
from .mix_eval.utils import (
construct_mix_eval_prompt_freeform,
construct_mix_eval_prompt_multichoice,
)
from .code_contests.utils import get_python_solutions
import os
import re
class Benchmark:
def __init__(self, dataset_sample: float = 1.0, debug_data=False):
self.dataset_sample = dataset_sample
self.debug_data = debug_data
self.dataset = None
self.save_type = "json"
def load_dataset(self):
raise NotImplementedError("Subclasses should implement this method")
def get_answer(self):
raise NotImplementedError("Subclasses should implement this method")
def process_results(self):
raise NotImplementedError("Subclasses should implement this method")
def save_answers(self):
raise NotImplementedError("Subclasses should implement this method")
class AlpacaEvalBenchmark(Benchmark):
def __init__(self, dataset_sample=1.0, debug_data=False):
super().__init__(debug_data)
def load_dataset(self):
self.dataset = datasets.load_dataset(
"tatsu-lab/alpaca_eval", "alpaca_eval_gpt4_baseline", trust_remote_code=True
)["eval"]
self.dataset = self.dataset.remove_columns(["output", "generator"])
if self.debug_data:
self.dataset = self.dataset.select(range(5))
return self.dataset
def get_answer(self, item, model, config, **kwargs):
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": item["instruction"]},
]
output = model.generate(messages)
return {"output": output, "generator": config["name"]}
def process_results(self, results):
self.dataset = self.dataset.add_column("output", [r["output"] for r in results])
self.dataset = self.dataset.add_column(
"generator", [r["generator"] for r in results]
)
return self.dataset
def save_answers(self, output_path, answers=None):
if answers is None:
answers = self.dataset
with open(output_path, "w") as f:
json.dump(list(answers), f, indent=2)
class MtBenchBenchmark(Benchmark):
def __init__(self, dataset_sample: float = 1.0, debug_data=False):
super().__init__(dataset_sample=dataset_sample, debug_data=debug_data)
self.save_type = "jsonl"
def load_dataset(self):
question_file = (
"archon/benchmarks/mt_bench/FastChat/fastchat/llm_judge/data/mt_bench/question.jsonl"
)
self.dataset = datasets.load_dataset("json", data_files=question_file)["train"]
if self.dataset_sample < 1.0:
self.dataset = self.dataset.select(
range(int(len(self.dataset) * self.dataset_sample))
)
elif self.debug_data:
self.dataset = self.dataset.select(range(5))
print("MT Bench Dataset Length: ", len(self.dataset))
return self.dataset
def get_answer(self, item, model, config, num_choices=1, **kwargs):
temperature_config = {
"writing": 0.7,
"roleplay": 0.7,
"extraction": 0.0,
"math": 0.0,
"coding": 0.0,
"reasoning": 0.0,
"stem": 0.1,
"humanities": 0.1,
"arena-hard-200": 0.0,
}
temperature = None
if "required_temperature" in item.keys():
temperature = item["required_temperature"]
elif item["category"] in temperature_config:
temperature = temperature_config[item["category"]]
choices = []
for i in range(num_choices):
turns = []
conv = [
{"role": "system", "content": "You are a helpful assistant."},
]
for j in range(len(item["turns"])):
conv.append({"role": "user", "content": item["turns"][j]})
output = model.generate(conv, temperature=temperature)
conv.append({"role": "assistant", "content": output})
turns.append(output)
choices.append({"index": i, "turns": turns})
ans = {
"answer_id": shortuuid.uuid(),
"model_id": config["name"],
"choices": choices,
"tstamp": time.time(),
}
return ans
def process_results(self, results):
# TODO: Better way to do this lol
self.dataset = self.dataset.add_column(
"answer_id", [r["answer_id"] for r in results]
)
self.dataset = self.dataset.add_column(
"model_id", [r["model_id"] for r in results]
)
self.dataset = self.dataset.add_column(
"choices", [r["choices"] for r in results]
)
self.dataset = self.dataset.add_column("tstamp", [r["tstamp"] for r in results])
return self.dataset
def save_answers(self, output_path, answers=None):
if answers is None:
answers = self.dataset
# mt_bench expects jsonl format
with open(output_path, "w") as f:
for result in answers:
f.write(json.dumps(result) + "\n")
class ArenaHardAutoBenchmark(Benchmark):
def __init__(self, dataset_sample: float = 1.0, debug_data=False):
super().__init__(dataset_sample=dataset_sample, debug_data=debug_data)
self.save_type = "jsonl"
def load_dataset(self):
question_file = "archon/benchmarks/arena_hard_auto/arena_questions.jsonl"
self.dataset = datasets.load_dataset("json", data_files=question_file)["train"]
if self.dataset_sample < 1.0:
self.dataset = self.dataset.select(
range(int(len(self.dataset) * self.dataset_sample))
)
elif self.debug_data:
self.dataset = self.dataset.select(range(5))
return self.dataset
def get_answer(self, item, model, config, num_choices=1, **kwargs):
temperature = 0.7
encoding = tiktoken.encoding_for_model(
"gpt-3.5-turbo"
) # arena benchmarks on gpt 3.5 encoding
choices = []
for i in range(num_choices):
turns = []
conv = [
{"role": "system", "content": "You are a helpful assistant."},
]
for j in range(len(item["turns"])):
conv.append({"role": "user", "content": item["turns"][j]["content"]})
output = model.generate(conv, temperature=temperature)
conv.append({"role": "assistant", "content": output})
turns.append(
{
"content": output,
"token_len": len(
encoding.encode(output, disallowed_special=())
),
}
)
choices.append({"index": i, "turns": turns})
ans = {
"question_id": item["question_id"],
"answer_id": shortuuid.uuid(),
"model_id": config["name"],
"choices": choices,
"tstamp": time.time(),
}
return ans
def process_results(self, results):
# TODO: Better way to do this lol
self.dataset = self.dataset.add_column(
"answer_id", [r["answer_id"] for r in results]
)
self.dataset = self.dataset.add_column(
"model_id", [r["model_id"] for r in results]
)
self.dataset = self.dataset.add_column(
"choices", [r["choices"] for r in results]
)
self.dataset = self.dataset.add_column("tstamp", [r["tstamp"] for r in results])
return self.dataset
def save_answers(self, output_path, answers=None):
if answers is None:
answers = self.dataset
with open(output_path, "w") as f:
for result in answers:
f.write(json.dumps(result) + "\n")
class MixEvalBenchmark(Benchmark):
def __init__(self, dataset_sample=1.0, debug_data=False):
super().__init__(debug_data)
self.save_type = "jsonl"
def load_dataset(self):
question_file = "MixEval/MixEval"
dataset = datasets.load_dataset(question_file, "MixEval")
# Concatenate the two sections together
self.dataset = datasets.concatenate_datasets(
[dataset["free_form"], dataset["multiple_choice"]]
)
if self.debug_data:
self.dataset = self.dataset.select(range(5))
return self.dataset
def get_answer(self, item, model, config, **kwargs):
# Determine the problem type and construct the appropriate prompt
if item["problem_type"] == "free-form":
# Free-form question
formatted_input = construct_mix_eval_prompt_freeform(item)
problem_type = "free-form"
else:
# Multiple-choice question
formatted_input = construct_mix_eval_prompt_multichoice(item)
problem_type = "multiple-choice"
# Prepare the input messages for the model
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": formatted_input},
]
# Generate the output from the model
output = model.generate(messages)
# Construct the answer object in the required format for logging
answer = {
"problem_type": problem_type,
"context": item.get("context", None),
"prompt": item["prompt"],
"target": item.get("target", []),
"benchmark_name": config.get("benchmark_name", "Unknown"),
"formatted_input": formatted_input,
"id": item.get("id", "unknown"),
"generator": config["name"],
"output": output,
}
return answer
def process_results(self, results):
self.dataset = self.dataset.add_column("output", [r["output"] for r in results])
self.dataset = self.dataset.add_column(
"generator", [r["generator"] for r in results]
)
self.dataset = self.dataset.add_column(
"formatted_input", [r["formatted_input"] for r in results]
)
return self.dataset
def save_answers(self, output_path, answers=None):
if answers is None:
answers = self.dataset
# Create directory based on the filename without extension
dir_name = os.path.splitext(output_path)[0]
os.makedirs(dir_name, exist_ok=True)
# Update the file paths to be inside the created directory
base_filename = os.path.basename(output_path)
free_form_path = os.path.join(
dir_name, base_filename.replace(".jsonl", "_free_form.jsonl")
)
mult_choice_path = os.path.join(
dir_name, base_filename.replace(".jsonl", "_mult_choice.jsonl")
)
free_form_answers = [a for a in answers if a["problem_type"] == "free-form"]
mult_choice_answers = [
a for a in answers if a["problem_type"] == "multiple-choice"
]
with open(free_form_path, "w") as f:
for result in free_form_answers:
f.write(json.dumps(result) + "\n")
with open(mult_choice_path, "w") as f:
for result in mult_choice_answers:
f.write(json.dumps(result) + "\n")
class MixEvalHardBenchmark(Benchmark):
def __init__(self, dataset_sample=1.0, debug_data=False):
super().__init__(debug_data)
self.save_type = "jsonl"
def load_dataset(self):
question_file = "MixEval/MixEval"
dataset = datasets.load_dataset(question_file, "MixEval_Hard")
# Concatenate the two sections together
self.dataset = datasets.concatenate_datasets(
[dataset["free_form"], dataset["multiple_choice"]]
)
if self.debug_data:
self.dataset = self.dataset.select(range(5))
return self.dataset
def get_answer(self, item, model, config, **kwargs):
# Determine the problem type and construct the appropriate prompt
if item["problem_type"] == "free-form":
# Free-form question
formatted_input = construct_mix_eval_prompt_freeform(item)
problem_type = "free-form"
else:
# Multiple-choice question
formatted_input = construct_mix_eval_prompt_multichoice(item)
problem_type = "multiple-choice"
# Prepare the input messages for the model
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": formatted_input},
]
# Generate the output from the model
output = model.generate(messages)
# Construct the answer object in the required format for logging
answer = {
"problem_type": problem_type,
"context": item.get("context", None),
"prompt": item["prompt"],
"target": item.get("target", []),
"benchmark_name": config.get("benchmark_name", "Unknown"),
"formatted_input": formatted_input,
"id": item.get("id", "unknown"),
"generator": config["name"],
"output": output,
}
return answer
def process_results(self, results):
self.dataset = self.dataset.add_column("output", [r["output"] for r in results])
self.dataset = self.dataset.add_column(
"generator", [r["generator"] for r in results]
)
self.dataset = self.dataset.add_column(
"formatted_input", [r["formatted_input"] for r in results]
)
return self.dataset
def save_answers(self, output_path, answers=None):
if answers is None:
answers = self.dataset
# Create directory based on the filename without extension
dir_name = os.path.splitext(output_path)[0]
os.makedirs(dir_name, exist_ok=True)
# Update the file paths to be inside the created directory
base_filename = os.path.basename(output_path)
free_form_path = os.path.join(
dir_name, base_filename.replace(".jsonl", "_free_form_hard.jsonl")
)
mult_choice_path = os.path.join(
dir_name, base_filename.replace(".jsonl", "_mult_choice_hard.jsonl")
)
free_form_answers = [a for a in answers if a["problem_type"] == "free-form"]
mult_choice_answers = [
a for a in answers if a["problem_type"] == "multiple-choice"
]
with open(free_form_path, "w") as f:
for result in free_form_answers:
f.write(json.dumps(result) + "\n")
with open(mult_choice_path, "w") as f:
for result in mult_choice_answers:
f.write(json.dumps(result) + "\n")
CC_IMAGE_TAGS = ["<image>", "[Image]"]
CC_PROMPT = "Q: Write python code to solve the following coding problem that obeys the constraints and passes the example test cases. The output code needs to read from and write to standard IO. Please wrap your code answer using ```:"
class CodeContestsBenchmark(Benchmark):
def __init__(self, dataset_sample=1.0, debug_data=False):
super().__init__(dataset_sample=dataset_sample, debug_data=debug_data)
self.save_type = "yaml"
self.num_few_shot = 1
self.limit = None
self.offset = None
self.stride = None
def has_image_tags(self, description):
for tag in CC_IMAGE_TAGS:
if tag in description:
return True
return False
def load_dataset(self):
dataset = datasets.load_dataset("deepmind/code_contests")
few_shot_dataset = [p for p in dataset["train"]]
test_dataset = [p for p in dataset["test"]]
random.seed(0)
if self.debug_data:
logger.info("Getting few_shot_items_with_solutions")
few_shot_items_with_solutions = []
for i, data in enumerate(few_shot_dataset):
python_solutions = get_python_solutions(data)
data["python_solutions"] = python_solutions
if len(python_solutions) > 0 and not self.has_image_tags(
data["description"]
):
few_shot_items_with_solutions.append(data)
if self.debug_data:
logger.info("Getting no_image_test_dataset")
no_image_test_dataset = []
for i, data in enumerate(test_dataset):
if self.has_image_tags(data["description"]):
continue
few_shot_items = random.sample(
few_shot_items_with_solutions, self.num_few_shot
)
data["few_shot_items"] = few_shot_items
no_image_test_dataset.append(data)
random.shuffle(no_image_test_dataset)
limit = self.limit if self.limit else len(no_image_test_dataset)
stride = self.stride if self.stride else 1
offset = self.offset if self.offset else 0
self.dataset = no_image_test_dataset[offset:limit:stride]
if self.debug_data:
self.dataset = self.dataset[:5]
if self.debug_data:
logger.debug(f"peak: {self.dataset[0].keys()}")
print(f"Total number of items to process: {len(self.dataset)}")
return self.dataset
def problem_to_prompt(self, problem, add_solution=True):
prompt = f"{CC_PROMPT}\n{problem['description']}\nA:"
if add_solution:
prompt += f" ```{problem['python_solutions'][0].strip()}```"
return prompt
def get_prompt(self, item):
prompt = "\n".join(
[
self.problem_to_prompt(few_shot_item)
for few_shot_item in item["few_shot_items"]
]
)
prompt += "\n" + self.problem_to_prompt(item, add_solution=False)
return prompt
def get_test_cases(self, item):
return {
"input": item["public_tests"]["input"]
+ item["private_tests"]["input"]
+ item["generated_tests"]["input"],
"output": item["public_tests"]["output"]
+ item["private_tests"]["output"]
+ item["generated_tests"]["output"],
}
def get_timeout(self, item):
timeout_seconds = 0
if item["time_limit"] is not None:
timeout_seconds += item["time_limit"]["seconds"]
timeout_seconds += item["time_limit"]["nanos"] / 1_000_000_000
if timeout_seconds == 0:
timeout_seconds = None
return timeout_seconds
def get_answer(self, item, model, config, samples=1, **kwargs):
prompt = self.get_prompt(item)
if self.debug_data:
logger.info(prompt)
messages = [
{"role": "system", "content": "You are a coding assistant."},
{"role": "user", "content": prompt},
]
output = []
for sample in range(samples):
output.append(model.generate(messages))
ans = {
"prompt": prompt,
"question": item["description"],
"name": item["name"],
"samples": output,
"test_cases": self.get_test_cases(item),
"timeout": self.get_timeout(item),
}
return ans
def process_results(self, results):
self.dataset = results
return results
def save_answers(self, output_path, answers=None):
if answers is None:
answers = self.dataset
with open(output_path, "w") as f:
yaml.dump(answers, f)
class GSM8KBenchmark(Benchmark):
def __init__(self, dataset_sample=1.0, debug_data=False):
super().__init__(dataset_sample=dataset_sample, debug_data=debug_data)
self.dataset_sample = dataset_sample
self.debug_data = debug_data
self.dataset = None
self.save_type = "json"
self.prompt = "Answer the following mathematics question. Provide your reasoning by showing your work before your answer. "
self.prompt += "At the end of your response, output your final answer in the format: 'The answer is: [answer]'. "
self.prompt += "You must provide the separator 'The answer is: ' before your final answer. "
self.prompt += "Question: "
def load_dataset(self):
self.dataset = datasets.load_dataset("openai/gsm8k", "main")["test"]
self.dataset = self.dataset.select(
range(int(len(self.dataset) * self.dataset_sample))
)
random.seed(0)
if self.debug_data:
self.dataset = self.dataset.select(range(5))
if self.debug_data:
logger.debug(f"peak: {self.dataset[0].keys()}")
print(f"Total number of items to process: {len(self.dataset)}")
return self.dataset
def get_answer(self, item, model, config, **kwargs):
prompt = self.prompt + item["question"]
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
]
output = model.generate(messages)
return {"prompt": prompt, "output": output, "generator": config["name"]}
def process_results(self, results):
self.dataset = self.dataset.add_column("prompt", [r["prompt"] for r in results])
self.dataset = self.dataset.add_column("output", [r["output"] for r in results])
self.dataset = self.dataset.add_column(
"generator", [r["generator"] for r in results]
)
return self.dataset
def save_answers(self, output_path, answers=None):
if answers is None:
answers = self.dataset
# Append if file not deleted
answers = list(answers)
prev_answers = None
if os.path.exists(output_path):
with open(output_path, "r") as f:
prev_answers = json.load(f)
if isinstance(prev_answers, list):
if len(prev_answers) != 0 and not isinstance(prev_answers[0], list):
prev_answers = [prev_answers]
prev_answers.append(answers)
answers = prev_answers
else:
answers = [answers]
else:
answers = [answers]
with open(output_path, "w") as f:
json.dump(answers, f, indent=2)
class HumanEvalBenchmark(Benchmark):
def __init__(self, dataset_sample=1.0, debug_data=False):
language = "python"
temp_dir = "tmp"
self.lang = language
self.temp_dir = temp_dir
os.makedirs(temp_dir, exist_ok=True)
self.save_type = "json"
problem_file = os.path.join("human_eval/data", f"humaneval-{self.lang}.jsonl")
self.examples = [json.loads(x) for x in open(problem_file) if x.strip()]
self.examples = self.examples
print("Read {} examples for evaluation over.".format(len(self.examples)))
def load_dataset(self):
self.dataset = datasets.Dataset.from_list(self.examples)
return self.dataset
def build_deepseekcoder_instruction(self, languge: str, question: str):
return """
Please continue to complete the function. You are not allowed to modify the given code and do the completion only. Please return all completed function in a codeblock. Here is the given code to do completion:
```{}
{}
```
""".strip().format(
languge.lower(), question.strip()
)
def get_answer(self, item, model, config, **kwargs):
from human_eval.utils.utils import extract_generation_code, language_settings
prompt = self.build_deepseekcoder_instruction(
language_settings[self.lang]["full_name"], item["prompt"]
)
messages = [{"role": "user", "content": prompt}]
output = model.generate(messages)
item["output"] = output
return extract_generation_code(item, lang_code=self.lang)
def save_answers(self, output_path, answers=None):
if answers is None:
answers = self.dataset
with open(output_path, "w", encoding="utf-8") as fw:
for ex in answers:
fw.write(json.dumps(ex) + "\n")
def process_results(self, results):
if "prompt" not in self.dataset.column_names:
self.dataset = self.dataset.add_column(
"prompt", [r["prompt"] for r in results]
)
if "output" not in self.dataset.column_names:
self.dataset = self.dataset.add_column(
"output", [r["output"] for r in results]
)
if "generation" not in self.dataset.column_names:
self.dataset = self.dataset.add_column(
"generation", [r["generation"] for r in results]
)
return self.dataset
class MBPPBenchmark(Benchmark):
def __init__(self, dataset_sample=1.0, debug_data=False):
language = "python"
temp_dir = "tmp"
self.lang = language
self.temp_dir = temp_dir
os.makedirs(temp_dir, exist_ok=True)
self.save_type = "json"
problem_file = os.path.join("mbpp/data", f"mbpp.jsonl")
self.examples = list(self.read_test_examples(problem_file))
print("Read {} examples for evaluation over.".format(len(self.examples)))
def read_test_examples(self, data_path):
def format_test_example(q, tests, code: str = None):
prompt = ">>> Problem:\n{}\n>>> Test Cases:\n{}\n".format(
q.strip(), "\n".join(tests)
)
if code:
code = code.replace("\r", "").replace("\t", " ")
prompt += "\n>>> Code:\n```python\n{}\n```".format(code)
return prompt
examples = [json.loads(x) for x in open(data_path)]
print("Read all {} examples from {} over!".format(len(examples), data_path))
# test_cases
examples_str = []
for i in range(1, 4):
ex = examples[i]
q, test, code = ex["text"], ex["test_list"], ex["code"]
ex_prompt = format_test_example(q, test, code)
example_prompt = "- Example {}:\n{}".format(i, ex_prompt)
examples_str += [example_prompt]
for i in range(10, 510):
ex = examples[i]
q, test, code = ex["text"], ex["test_list"], ex["code"]
prompt = format_test_example(q, test, code=None)
prompt_with_shots = """
Please refer the given examples and generate a python function for my problem.
Examples are listed as follows:
{}
Here is my problem:
{}
""".strip().format(
"\n\n".join(examples_str), prompt
)
yield {"task_id": ex["task_id"], "prompt": prompt_with_shots}
def load_dataset(self):
self.dataset = datasets.Dataset.from_list(self.examples)
return self.dataset
def get_answer(self, item, model, config, **kwargs):
messages = [{"role": "user", "content": item["prompt"]}]
output = model.generate(messages)
item["output"] = output
return self.convert_for_evaluation(item)
def convert_for_evaluation(self, example):
gpt_completion = example["output"]
generation = gpt_completion
try:
code_block: str = re.findall(
f"```python\n(.*?)```", gpt_completion, re.DOTALL | re.IGNORECASE
)[0]
generation = code_block
except Exception as ex:
print("Failed to extract codeblock:\n{}".format(gpt_completion))
example["generation"] = generation
return example
def save_answers(self, output_path, answers=None):
if answers is None:
answers = self.dataset
with open(output_path, "w", encoding="utf-8") as fw:
for ex in answers:
fw.write(json.dumps(ex) + "\n")
def process_results(self, results):
if "prompt" not in self.dataset.column_names:
self.dataset = self.dataset.add_column(
"prompt", [r["prompt"] for r in results]
)
if "output" not in self.dataset.column_names:
self.dataset = self.dataset.add_column(
"output", [r["output"] for r in results]
)
if "generation" not in self.dataset.column_names:
self.dataset = self.dataset.add_column(
"generation", [r["generation"] for r in results]
)
if "task_id" not in self.dataset.column_names:
self.dataset = self.dataset.add_column(
"task_id", [r["task_id"] for r in results]
)
return self.dataset
class MATHBenchmark(Benchmark):
def __init__(self, dataset_sample=1.0, debug_data=False):
super().__init__(dataset_sample=dataset_sample, debug_data=debug_data)
self.dataset_sample = dataset_sample
self.debug_data = debug_data
self.dataset = None
self.save_type = "json"
self.prompt = "Answer the following mathematics question. Provide your reasoning by showing your work before your answer. "
self.prompt += "At the end of your response, output your final answer in the format: 'The answer is: [answer]'. "
self.prompt += "You must provide the separator 'The answer is: ' before your final answer. "
self.prompt += "Question: "
def load_dataset(self):
self.dataset = datasets.load_dataset("hendrycks/competition_math")["test"]
self.dataset = self.dataset.select(
range(int(len(self.dataset) * self.dataset_sample))
)
random.seed(0)
if self.debug_data:
self.dataset = self.dataset.select(range(5))
if self.debug_data:
logger.debug(f"peak: {self.dataset[0].keys()}")
print(f"Total number of items to process: {len(self.dataset)}")
return self.dataset
def get_answer(self, item, model, config, **kwargs):
prompt = self.prompt + item["problem"]
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
]
output = model.generate(messages)
return {"prompt": prompt, "output": output, "generator": config["name"]}
def process_results(self, results):
self.dataset = self.dataset.add_column("prompt", [r["prompt"] for r in results])
self.dataset = self.dataset.add_column("output", [r["output"] for r in results])
self.dataset = self.dataset.add_column(
"generator", [r["generator"] for r in results]
)
return self.dataset
def save_answers(self, output_path, answers=None):
if answers is None:
answers = self.dataset
# Append if file not deleted
answers = list(answers)
prev_answers = None
if os.path.exists(output_path):
with open(output_path, "r") as f:
prev_answers = json.load(f)
if isinstance(prev_answers, list):
if len(prev_answers) != 0 and not isinstance(prev_answers[0], list):
prev_answers = [prev_answers]
prev_answers.append(answers)
answers = prev_answers
else:
answers = [answers]
else:
answers = [answers]
with open(output_path, "w") as f:
json.dump(answers, f, indent=2)
class MiniF2FBenchmark(Benchmark):
def __init__(self, dataset_sample=1.0, debug_data=False):
super().__init__(dataset_sample=dataset_sample, debug_data=debug_data)
self.dataset_sample = dataset_sample
self.debug_data = debug_data
self.dataset = None
self.save_type = "json"
self.prompt = "Answer the following mathematics question. Provide your reasoning by showing your work before your answer. "
self.prompt += "At the end of your response, output your final answer in the format: 'The answer is: [answer]'. "
self.prompt += "You must provide the separator 'The answer is: ' before your final answer. "
self.prompt += "Question: "
def load_dataset(self):
self.dataset = datasets.load_dataset("cat-searcher/minif2f-lean4")["test"]
self.dataset = self.dataset.select(
range(int(len(self.dataset) * self.dataset_sample))
)
random.seed(0)
if self.debug_data:
self.dataset = self.dataset.select(range(5))
if self.debug_data:
logger.debug(f"peak: {self.dataset[0].keys()}")
print(f"Total number of items to process: {len(self.dataset)}")
return self.dataset
def get_answer(self, item, model, config):
prompt = self.prompt + item["informal_stmt"]
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
]
output = model.generate(messages)
return {"prompt": prompt, "output": output, "generator": config["name"]}
def process_results(self, results):
self.dataset = self.dataset.add_column("prompt", [r["prompt"] for r in results])
self.dataset = self.dataset.add_column("output", [r["output"] for r in results])
self.dataset = self.dataset.add_column(
"generator", [r["generator"] for r in results]
)
return self.dataset
def save_answers(self, output_path, answers=None):
if answers is None:
answers = self.dataset
with open(output_path, "w") as f:
json.dump(list(answers), f, indent=2)
class MMLUBenchmark(Benchmark):
def __init__(self, dataset_sample=1.0, debug_data=False):
super().__init__(dataset_sample=dataset_sample, debug_data=debug_data)
self.save_type = "jsonl"
# Set task from the MMLU dataset:
self._local_task = "elementary_mathematics"
def load_dataset(self):
# Load the MMLU dataset (local_task):
self.dataset = datasets.load_dataset("cais/mmlu", self._local_task)["test"]
if self.dataset_sample < 1.0:
self.dataset = self.dataset.select(
range(int(len(self.dataset) * self.dataset_sample))
)
elif self.debug_data:
self.dataset = self.dataset.select(range(5))
print(f"Total number of items to process: {len(self.dataset)}")
return self.dataset
def get_answer(self, item, model, config, **kwargs):
# Format the prompt for the model
prompt = f"Question: {item['question']}\nA. {item['choices'][0]}\nB. {item['choices'][1]}\nC. {item['choices'][2]}\nD. {item['choices'][3]}\n\nAnswer:"
prompt += "At the end of your response, output your final answer in the format: 'The answer is: [answer]'. "
# Prepare the input messages for the model
messages = [
{"role": "system", "content": "You are a helpful assistant."},
{"role": "user", "content": prompt},
]
# Generate the output from the model
output = model.generate(messages)
# Construct the answer object in the required format for logging
answer = {
"prompt": prompt,
"output": output,