forked from thu-wyz/inference_scaling
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmath_evaluate.py
More file actions
200 lines (169 loc) · 5.93 KB
/
math_evaluate.py
File metadata and controls
200 lines (169 loc) · 5.93 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
import os
import re
import json
import tqdm
import argparse
import sys
import os
import torch
from evaluate.evaluate_utils.grader import *
from collections import Counter
from evaluate.data_processing.answer_extraction import extract_answer
ANS_RE = re.compile(r"The answer is: (\-?[0-9\.\,]+)")
INVALID_ANS = "[invalid]"
GT_RE = re.compile(r"#### (\-?[0-9\.\,]+)")
SHEPHERD_RE = re.compile(r"The answer is:(.+) \u043a\u0438")
METAMATH_RE = re.compile(r"The answer is:(.+)\n\n")
def extract_gsm_answer(completion):
match = ANS_RE.search(completion)
if match:
match_str = match.group(1).strip()
match_str = match_str.replace(",", "")
return match_str
else:
return INVALID_ANS
def extract_shepherd_answer(completion):
if completion == None:
return INVALID_ANS
match = SHEPHERD_RE.search(completion)
if match:
match_str = match.group(1).strip()
match_str = match_str.replace(",", "")
return match_str
else:
return INVALID_ANS
def extract_metamath_answer(completion):
match = METAMATH_RE.search(completion)
if match:
match_str = match.group(1).strip()
match_str = match_str.replace(",", "")
return match_str
else:
return INVALID_ANS
def extract_ground_truth(completion):
match = GT_RE.search(completion)
if match:
match_str = match.group(1).strip()
match_str = match_str.replace(",", "")
return match_str
def agg_min(step_scores):
if step_scores == []:
return 0
minn = 1
for s in step_scores:
if s is not None:
if s < minn:
minn = s
return minn
def agg_mean(step_scores):
if step_scores == []:
return 0
return sum(step_scores) / len(step_scores)
def agg_prod(step_scores):
prod = 1.0
if step_scores == []:
return 0
for score in step_scores:
prod *= score
return prod
def agg_last(step_scores):
if step_scores == []:
return 0
if step_scores[-1] == None:
return 0
return step_scores[-1]
def evaluate(path, aggfunc, extract_function):
data = json.load(open(path, "r"))
num_correct = 0
data = data
total = len(data)
for qapair in data:
max_score = -999.999
answer = None
for cand in qapair["model_answer"]:
if aggfunc(cand["step_scores"]) > max_score:
answer = cand["text"]
max_score = aggfunc(cand["step_scores"])
answer = extract_function(answer)
ground_truth = qapair["ground_truth_answer"]
if grade_answer(answer, ground_truth):
num_correct += 1
print(num_correct)
return num_correct * 1.0 / total
def majority_vote(path, weighted, weight_func, extract_function):
data = json.load(open(path, "r"))
num_correct = 0
total = len(data)
for qapair in data:
max_vote = 0
max_rep = None
equiv_classes = []
equiv_weights = []
for cand in qapair["model_answer"]:
answer = extract_function(cand["text"])
weight = 1
if weighted == True:
weight = weight_func(cand["step_scores"])
flag = 0
for i, rep in enumerate(equiv_classes):
if grade_answer(answer,rep):
flag = 1
equiv_weights[i] = equiv_weights[i]+weight
if equiv_weights[i] > max_vote:
max_vote = equiv_weights[i]
max_rep = answer
if flag:
continue
equiv_classes.append(answer)
equiv_weights.append(weight)
if weight > max_vote:
max_vote = weight
max_rep = answer
if grade_answer(str(max_rep), qapair["ground_truth_answer"]):
num_correct += 1
print(num_correct)
print(num_correct * 1.0 / total)
return num_correct * 1.0 / total
def main(args):
current_file_path = os.path.abspath(__file__)
current_dir = os.path.dirname(current_file_path)
sys.path.append(os.path.join(current_dir, 'eval_deepseek'))
if args.model_type == "mistral_7b" or args.model_type == "llemma":
extract_func = extract_shepherd_answer
if args.agg_func == "min":
aggfunc = agg_min
accuracy = evaluate(args.path, aggfunc, extract_func)
if args.agg_func == "prod":
aggfunc = agg_prod
accuracy = evaluate(args.path, aggfunc, extract_func)
if args.agg_func == "mean":
aggfunc = agg_mean
accuracy = evaluate(args.path, aggfunc, extract_func)
if args.agg_func == "last":
aggfunc = agg_last
accuracy = evaluate(args.path, aggfunc, extract_func)
if args.agg_func == "majority_vote":
weight_func = None
if args.weight_agg == "min":
weight_func = agg_min
if args.weight_agg == "prod":
weight_func = agg_prod
if args.weight_agg == "mean":
weight_func = agg_mean
if args.weight_agg == "last":
weight_func = agg_last
accuracy = majority_vote(args.path, args.weighted, weight_func, extract_func)
if args.output_path:
with open(args.output_path, "a") as file:
file.write(f"\nagg_function {args.agg_func} weighted: {args.weighted} \n accuracy: {str(accuracy)}")
return accuracy
if __name__ == "__main__":
args_parser = argparse.ArgumentParser()
args_parser.add_argument('--path', type=str, required=True)
args_parser.add_argument('--agg_func', type=str, choices=["min", "prod", "mean", "last", "majority_vote"])
args_parser.add_argument('--weighted', type=bool, default=False)
args_parser.add_argument('--weight_agg', type=str, choices=["min", "prod", "mean", "last"])
args_parser.add_argument('--model_type', type=str, choices=["mistral", "llemma"])
args_parser.add_argument('--output_path', type=str)
args = args_parser.parse_args()
main(args)