-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathuni_eval.py
More file actions
712 lines (604 loc) · 25 KB
/
uni_eval.py
File metadata and controls
712 lines (604 loc) · 25 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
import os
import re
import fire
import math
import torch
import sympy as sp
from tqdm import tqdm
from copy import deepcopy
from transformers import AutoTokenizer
from datasets import load_dataset, load_from_disk, ClassLabel
from vllm import LLM, SamplingParams
from math_verify import verify, parse
from sympy import simplify, Eq, sympify, Pow
from sympy.parsing.latex import parse_latex
from utils.openmathinst_utils import process_results
from utils.polymath.judge import pm_judge
from utils.data_utils import write_jsonl, write_json, read_jsonl
from utils.chat_template import CHAT_TEMPLATE, SYSTEM_PROMPT, PREFIX_PROMPT, SUFFIX_PROMPT
DATASET_INFO = {
"zwhe99/MATH": {
"default_split": "math500",
"problem_key": "problem",
"answer_key": "expected_answer",
"category_keys": ["level", "type"]
},
"zwhe99/aime90": {
"default_split": "2024",
"problem_key": "problem",
"answer_key": "expected_answer",
},
"zwhe99/amc23": {
"default_split": "test",
"problem_key": "question",
"answer_key": "answer",
},
"zwhe99/simplerl-minerva-math": {
"default_split": "test",
"problem_key": "problem",
"answer_key": "answer",
},
"math-ai/aime25": {
"default_split": "test",
"problem_key": "problem",
"answer_key": "answer",
},
"zwhe99/simplerl-OlympiadBench": {
"default_split": "test",
"problem_key": "question",
"answer_key": "final_answer",
},
"zwhe99/gpqa_diamond_mc": {
"default_split": "test",
"problem_key": "problem",
"answer_key": "solution",
"category_keys": ["domain"]
},
"zwhe99/pm-en": {
"default_split": "test",
"problem_key": "question",
"answer_key": "answer",
"category_keys": ["level"]
}
}
class OBJudge:
def __init__(self):
# Map of special symbols to their replacements
self.special_signal_map = {
"\\left": "",
"\\right": "",
"∶": ":",
",": ",",
"$": "",
"\\approx": "=",
"\\simeq": "=",
"\\sim": "=",
"^\\prime": "'",
"^{\\prime}": "'",
"^\\circ": "",
"%": "",
}
self.pi = parse_latex("\\pi")
self.precision = 1e-8 # Default precision for comparison
def split_by_comma(self, expr: str):
# Splits expressions by commas outside of brackets
in_bracket_num = 0
splitted_expr = []
start_idx = 0
for i, char in enumerate(expr):
if char in ["(", "["]:
in_bracket_num += 1
elif char in [")", "]"]:
in_bracket_num -= 1
elif char == "," and in_bracket_num == 0:
splitted_expr.append(expr[start_idx:i].strip())
start_idx = i + 1
if start_idx < len(expr):
splitted_expr.append(expr[start_idx:].strip())
return splitted_expr
def trans_plus_minus_sign(self, expr_list: list):
# Translates plus-minus signs into separate expressions
new_expr_list = []
for expr in expr_list:
if "\\pm" in expr:
new_expr_list.append(expr.replace("\\pm", "+"))
new_expr_list.append(expr.replace("\\pm", "-"))
else:
new_expr_list.append(expr)
return new_expr_list
def judge(self, expression1, expression2, precision=1e-8):
# Judge if two expressions are equal (expression1 is considered as the Ground Truth)
# Default precision is a list for supporting multiple expressions
precision = precision if isinstance(precision, list) else [precision]
try:
expression1, expression2 = self.preprocess(expression1, expression2)
except:
return False
if expression1 == expression2:
# print("Exactly equal")
return True
# Remove Chinese characters from the string, as answers like "yes" or "no" in Chinese have been considered
expression1 = re.sub(r'[\u4e00-\u9fff]+', '', expression1)
expression2 = re.sub(r'[\u4e00-\u9fff]+', '', expression2)
expression1 = self.split_by_comma(expression1)
expression2 = self.split_by_comma(expression2)
temp_list1 = self.trans_plus_minus_sign(expression1)
temp_list2 = self.trans_plus_minus_sign(expression2)
# Set up a list for allowed errors
if len(precision) <= 1:
precision = precision * len(temp_list1)
if len(temp_list1) != len(temp_list2):
return False
# Check if elements in both lists can be paired and are equal
idx = -1
while len(temp_list1) != 0:
idx = (idx + 1) % len(temp_list1)
item1 = temp_list1[idx]
self.precision = precision[idx]
for item2 in temp_list2:
if self.is_equal(item1, item2):
temp_list1.remove(item1)
temp_list2.remove(item2)
precision.remove(self.precision)
break
else:
# If no match was found, return False
return False
# If all elements are matched, return True
return True
def is_interval(self, expr):
# Checks if an expression is an interval
return expr.startswith(("(", "[")) and expr.endswith((")", "]"))
def sympy_sub_pi(self, expression_sympy):
# Replaces the symbol for pi in sympy expressions with its numerical value
return expression_sympy.subs(self.pi, math.pi)
def is_equal(self, expression1, expression2):
# Default first expression is ground truth. Check if expressions are equal in different aspects
if expression1 == expression2 and expression1 != "" and expression2 != "":
# print("Equivalent natively")
return True
# First check if both are intervals
if self.is_interval(expression1) and self.is_interval(expression2):
try:
if self.interval_equal(expression1, expression2):
# print("Interval equivalent")
return True
except:
return False
# Then check for numerical equality
try:
if self.numerical_equal(expression1, expression2):
# print("Numerically equivalent")
return True
except:
pass
# Then check if expressions are mathematically equal
try:
if self.expression_equal(expression1, expression2) and not ("=" in expression1 and "=" in expression2):
# print("Expression equivalent")
return True
except:
pass
# Lastly, check for equation equality
try:
if self.equation_equal(expression1, expression2):
# print("Equation equivalent")
return True
except:
pass
return False
def numerical_equal(self, expression1: str, expression2: str, include_percentage: bool = True):
# Check if two numerical values are equal within an allowed error range
# Includes possible percentage cases
reference = float(expression1)
prediction = float(expression2)
if include_percentage:
gt_result = [reference / 100, reference, reference * 100]
else:
gt_result = [reference]
for item in gt_result:
if abs(item - prediction) <= self.precision * 1.01:
return True
return False
def expression_equal(self, exp1, exp2):
# Check if two expressions are mathematically equivalent
# Extract expression and use sympy for equivalence checking
def extract_expression(expression):
if "=" in expression:
expression = expression.split("=")[1]
return expression.strip()
exp1 = extract_expression(exp1)
exp2 = extract_expression(exp2)
expr1_sym = sympify(parse_latex(exp1))
expr2_sym = sympify(parse_latex(exp2))
if expr1_sym == expr2_sym:
return True
else:
expr1_sym = self.sympy_sub_pi(expr1_sym)
expr2_sym = self.sympy_sub_pi(expr2_sym)
if (expr1_sym.has(sp.Symbol) and not expr2_sym.has(sp.Symbol)) or (not expr1_sym.has(sp.Symbol) and expr2_sym.has(sp.Symbol)):
return False
elif not expr1_sym.has(sp.Symbol) and not expr2_sym.has(sp.Symbol):
try:
if not (self.can_compute_power(expr1_sym) and self.can_compute_power(expr2_sym)):
print(f"These two numbers cannot be calculated by the current computer for: \"{str(expr1_sym)}\" and \"{str(expr2_sym)}\"")
return False
if abs(expr1_sym.evalf() - expr2_sym.evalf()) <= self.precision * 1.01:
return True
else:
return False
except:
return False
else:
try:
simplified_expr = simplify(expr1_sym - expr2_sym)
num_value = simplified_expr.evalf()
return abs(num_value) < 1e-3
except:
return False
def equation_equal(self, expression1, expression2):
# Check if two equations are mathematically equivalent
# Simplify equations and use sympy for equivalence checking
def simplify_equation(latex_eq):
lhs, rhs = latex_eq.split('=')
lhs_expr = parse_latex(lhs)
rhs_expr = parse_latex(rhs)
equation = Eq(lhs_expr, rhs_expr)
simplified_eq = simplify(equation.lhs - equation.rhs)
return simplified_eq
expr1_sym = simplify_equation(expression1)
expr2_sym = simplify_equation(expression2)
division_result_1 = simplify(expr1_sym / expr2_sym)
division_result_2 = simplify(expr2_sym / expr1_sym)
if (division_result_1.is_Integer and division_result_1 != 0) or (division_result_2.is_Integer and division_result_2 != 0):
return True
else:
return False
def interval_equal(self, expression1, expression2):
# Check if two intervals are mathematically equivalent
def compare_two_interval(inter1, inter2):
if inter1[0] != inter2[0] or inter1[-1] != inter2[-1]:
return False
inter1 = inter1.strip('[]()')
inter2 = inter2.strip('[]()')
items_1 = inter1.split(',')
items_2 = inter2.split(',')
for item_1, item_2 in zip(items_1, items_2):
if not self.expression_equal(item_1, item_2):
return False
return True
interval1 = expression1
interval2 = expression2
if interval1 == interval2:
return True
else:
inter_list1 = interval1.split("\\cup")
inter_list2 = interval2.split("\\cup")
if len(inter_list1) != len(inter_list2):
return False
else:
for inter1, inter2 in zip(inter_list1, inter_list2):
if not compare_two_interval(inter1, inter2):
return False
return True
def preprocess(self, expression1, expression2):
# Preprocess expressions to extract and replace special symbols
def extract_boxed_content(latex_str):
boxed_matches = re.finditer(r'\\boxed{', latex_str)
results = ""
for match in boxed_matches:
start_index = match.end()
end_index = start_index
stack = 1
while stack > 0 and end_index < len(latex_str):
if latex_str[end_index] == '{':
stack += 1
elif latex_str[end_index] == '}':
stack -= 1
end_index += 1
if stack == 0:
content = latex_str[start_index:end_index - 1]
results += content + ","
else:
raise ValueError("Mismatched braces in LaTeX string.")
if results == "":
last_line_ans = latex_str.strip().split("\n")[-1]
dollar_pattern = r"\$(.*?)\$"
answers = re.findall(dollar_pattern, last_line_ans)
if answers:
for ans in answers:
results += ans + ","
else:
results = latex_str
return results
def sepcial_symbol_replace(expression):
if "\\in " in expression:
expression = expression.split("\\in ")[1]
for signal in self.special_signal_map:
expression = expression.replace(signal, self.special_signal_map[signal])
expression = expression.strip("\n$,.:;^_=+`!@#$%^&*~,。")
pattern = r'\\(?:mathrm|mathbf)\{~?([^}]*)\}'
expression = re.sub(pattern, r'\1', expression)
return expression
exp1, exp2 = extract_boxed_content(expression1), extract_boxed_content(expression2)
exp1, exp2 = sepcial_symbol_replace(exp1), sepcial_symbol_replace(exp2)
return exp1, exp2
def can_compute_power(self, expr):
# Checks if a power expression can be computed
if isinstance(expr, Pow):
base, exp = expr.as_base_exp()
if base.is_number and exp.is_number:
MAX_EXP = 1000 # Adjust based on computing environment
if abs(exp.evalf()) > MAX_EXP:
return False
else:
return True
else:
return False
else:
return True # Not a power expression, can compute
def pass_at_k(correct_lst: list[bool], k: int) -> float:
assert k > 0, "k must be greater than 0"
assert k <= len(correct_lst), "k must be less than or equal to the length of `correct_lst`"
num_samples = len(correct_lst)
num_correct = sum(correct_lst)
if num_correct == 0:
return 0.0
elif (num_samples - num_correct) < k:
return 1.0
else:
log_ratio = 0.0
for i in range(k):
log_ratio += math.log(num_samples - num_correct - i) - math.log(num_samples - i)
return 1.0 - math.exp(log_ratio)
def bulid_choice_prompt(question: str, choices: list[str]):
prompt = f"{question}\n\n\n"
options = [chr(65 + i) for i in range(len(choices))]
for option, choice in zip(options, choices):
prompt += f"({option}) {choice}\n"
prompt += "\nPlease write your final answer in the form of "
for oid, opt in enumerate(options):
if oid != len(options) - 1:
prompt += f"\\boxed{{{opt}}}, "
else:
prompt += f"or \\boxed{{{opt}}}"
return prompt
def eval(
# required
base_model: str = None,
chat_template_name: str = "default",
system_prompt_name: str = "disabled",
prefix_prompt_name: str = "disabled",
suffix_prompt_name: str = "disabled",
output_dir: str = None,
# model
bf16: bool = False,
fp16: bool = False,
tensor_parallel_size: int = 8,
enforce_eager: bool = False,
gpu_memory_utilization: float = 0.9,
# data
data_dir: str = None, # If provided, the data will loaded from data_dir/data_id
data_id: str = None,
split: str = None,
subset: str = None,
start_idx: int = None,
end_idx: int = None,
# gen
max_model_len: int = 32768,
temperature: float = 0.6,
top_p: float = 1.0,
top_k: int = -1,
repetition_penalty: float = 1.0,
n: int = 1,
seed: int = 42,
):
# Path
if not os.path.exists(output_dir):
os.makedirs(output_dir)
generation_file = os.path.join(output_dir, "generation.jsonl")
result_file = os.path.join(output_dir, "result.log")
config_file = os.path.join(output_dir, "config.json")
# Sanity check
assert not (start_idx is not None and end_idx is None or start_idx is None and end_idx is not None), "start_idx and end_idx must be provided together"
if start_idx is not None and end_idx is not None:
assert end_idx > start_idx, "end_idx must be greater than start_idx"
if isinstance(split, int):
split = str(split)
# save config
write_json(config_file, locals())
# Get dataset info
problem_key = DATASET_INFO[data_id]["problem_key"]
answer_key = DATASET_INFO[data_id]["answer_key"]
choice_key = DATASET_INFO[data_id]["choice_key"] if "choice_key" in DATASET_INFO[data_id] else None
# load model
llm = LLM(
model=base_model,
tensor_parallel_size=tensor_parallel_size,
dtype=torch.bfloat16 if bf16 else (torch.float16 if fp16 else torch.float32),
seed=seed,
gpu_memory_utilization=gpu_memory_utilization,
enforce_eager=enforce_eager,
)
tokenizer = AutoTokenizer.from_pretrained(base_model)
if chat_template_name is not None and chat_template_name != "default":
tokenizer.chat_template = CHAT_TEMPLATE[chat_template_name]
# Load data
if subset is None and "default_subset" in DATASET_INFO[data_id]:
subset = DATASET_INFO[data_id]["default_subset"]
if data_dir is None:
if subset is None:
test_dataset = load_dataset(data_id)
else:
test_dataset = load_dataset(data_id, subset)
else:
if subset is None:
test_dataset = load_from_disk(os.path.join(data_dir, data_id))
else:
test_dataset = load_from_disk(os.path.join(data_dir, data_id, subset))
if split is None:
split = DATASET_INFO[data_id]["default_split"]
test_dataset = test_dataset[split]
if start_idx is not None and end_idx is not None:
test_dataset = test_dataset.select(range(start_idx, end_idx))
system_message = []
if system_prompt_name != "disabled":
system_message = [{"role": "system", "content": SYSTEM_PROMPT[system_prompt_name]}]
prefix_prompt = ""
if prefix_prompt_name != "disabled":
prefix_prompt = PREFIX_PROMPT[prefix_prompt_name]
suffix_prompt = ""
if suffix_prompt_name != "disabled":
suffix_prompt = SUFFIX_PROMPT[suffix_prompt_name]
prompts = [
tokenizer.apply_chat_template(
conversation=system_message + [
{
"role": "user",
"content": prefix_prompt + td[problem_key] if not choice_key else bulid_choice_prompt(td[problem_key], td[choice_key]) + suffix_prompt
}
],
tokenize=False,
add_generation_prompt=True,
)
for td in test_dataset
]
prompt_lens = [
len(tokenizer.apply_chat_template(
conversation=system_message + [
{
"role": "user",
"content": prefix_prompt + td[problem_key] if not choice_key else bulid_choice_prompt(td[problem_key], td[choice_key]) + suffix_prompt
}
],
tokenize=True,
add_generation_prompt=True,
))
for td in test_dataset
]
# repeat n times
sampling_params = [
SamplingParams(
temperature=temperature,
top_p=top_p,
top_k=top_k,
repetition_penalty=repetition_penalty,
max_tokens=max_model_len,
n=1,
seed=seed + i,
) for p, pl in zip(prompts, prompt_lens) for i in range(n)
]
prompts = [p for p in prompts for i in range(n)]
prompt_lens = [p for p in prompt_lens for i in range(n)]
# generate
if os.path.exists(generation_file) and os.path.getsize(generation_file) > 0:
print(f"Loading generations from {generation_file}")
generations = read_jsonl(generation_file)
else:
generations = []
outputs = llm.generate(prompts, sampling_params)
assert len(outputs) == len(prompts)
for tdi, td in enumerate(test_dataset):
local_outputs = outputs[tdi * n: (tdi + 1) * n]
local_prompts = prompts[tdi * n: (tdi + 1) * n]
local_prompt_lens = prompt_lens[tdi * n: (tdi + 1) * n]
new_td = deepcopy(td)
new_td["prompt"] = local_prompts[0]
new_td["prompt_length"] = local_prompt_lens[0]
new_td["response"] = [lo.outputs[0].text for lo in local_outputs]
new_td["response_length"] = [len(lo.outputs[0].token_ids) for lo in local_outputs]
new_td["finish_reason"] = [lo.outputs[0].finish_reason for lo in local_outputs]
generations.append(new_td)
write_jsonl(generation_file, generations)
# compute correctness and pass@k (sample-level)
ks = [2 ** e for e in range(0, 7)]
ks = [k for k in ks if (2 * k) <= n or k == 1]
for g in tqdm(generations, desc="computing correctness", total=len(generations)):
gt_answer = g[answer_key]
if isinstance(test_dataset.features[answer_key], ClassLabel):
gt_answer = test_dataset.features[answer_key].int2str(gt_answer)
else:
if isinstance(gt_answer, list):
assert len(gt_answer) == 1, "gt_answer must be a single string"
gt_answer = str(gt_answer[0])
else:
gt_answer = str(gt_answer)
if data_id == "zwhe99/simplerl-OlympiadBench":
# Note: Olympiadbench has its offical judge which do not support duplicated `boxed` in the response.
# Therefore, we strip the `reasoning` part in the response if it exists.
scorer = OBJudge()
g["correct"] = [
(
process_results(
resp,
gt_answer,
response_extract_from_boxed=True,
) or
process_results(
resp,
gt_answer,
response_extract_from_boxed=False,
response_extract_regex=r"The answer is: (.+)$",
) or
verify(parse(f"\\boxed{{${gt_answer}}}$"), parse(resp))
or scorer.judge(gt_answer, resp if "</think>" not in resp else resp.split("</think>")[1].strip(), 1e-8)
) for resp in g["response"]
]
elif data_id == "zwhe99/pm-en":
g["correct"] = [
(
process_results(
resp,
gt_answer,
response_extract_from_boxed=True,
) or
process_results(
resp,
gt_answer,
response_extract_from_boxed=False,
response_extract_regex=r"The answer is: (.+)$",
) or
verify(parse(f"\\boxed{{${gt_answer}}}$"), parse(resp))
or pm_judge(resp, gt_answer)
) for resp in g["response"]
]
else:
g["correct"] = [
(
process_results(
resp,
gt_answer,
response_extract_from_boxed=True,
) or
process_results(
resp,
gt_answer,
response_extract_from_boxed=False,
response_extract_regex=r"The answer is: (.+)$",
) or
verify(parse(f"\\boxed{{${gt_answer}}}$"), parse(resp))
) for resp in g["response"]
]
for k in ks:
g[f"pass@{k}"] = pass_at_k(g["correct"], k)
write_jsonl(generation_file, generations)
# dataset-level metrics
with open(result_file, "w") as f:
for k in ks:
f.write(f"pass@{k} >>>\n")
if "category_keys" in DATASET_INFO[data_id] and len(DATASET_INFO[data_id]["category_keys"]) > 0:
for ck in DATASET_INFO[data_id]["category_keys"]:
all_cate = sorted(list(set([g[ck] for g in generations])))
for cate in all_cate:
pass_prob_lst = [g[f"pass@{k}"] for g in generations if g[ck] == cate]
pass_prob_avg = sum(pass_prob_lst) / len(pass_prob_lst)
f.write(f"{cate}: {pass_prob_avg * 100:.1f}\n")
# overall
pass_prob_lst = [g[f"pass@{k}"] for g in generations]
pass_prob_avg = sum(pass_prob_lst) / len(pass_prob_lst)
f.write(f"Overall: {pass_prob_avg * 100:.1f}\n\n")
# print the result file
with open(result_file, "r") as f:
print(f.read())
if __name__ == "__main__":
fire.Fire(eval)