-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy patheval_aime.py
More file actions
144 lines (128 loc) · 6.15 KB
/
eval_aime.py
File metadata and controls
144 lines (128 loc) · 6.15 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
import json
import re
def extract_and_calculate_accuracy(jsonl_file_path, tokenizer):
# Initialize counters
total_entries = 0
correct_predictions = 0
num_tokens = []
n_long = 0
long_tokens = []
short_tokens = []
long_correctness = []
short_correctness = []
# Read and process the JSONL file line by line
with open(jsonl_file_path, 'r') as file:
for line in file:
entry = json.loads(line.strip()) # Parse each line as a JSON object
total_entries += 1
solution = entry.get("resps", {})
expected_answer = entry.get("target")
prompt = entry['arguments']['gen_args_0']["arg_0"]
if "<think>" in solution[0][0] or "<think>" in prompt:
n_long += 1
if "<think>" in solution[0][0] or "<think>" in prompt:
long_correctness.append(False) # placeholder for long correctness
else:
short_correctness.append(False)
conversation = [
{'role': 'user', 'content': prompt},
{'role': 'assistant', 'content': solution[0][0]},
]
tokens = tokenizer.apply_chat_template(conversation, return_tensors="pt")
#if tokens.shape[1] < 32768:
num_tokens.append(tokens.shape[1])
if "<think>" in solution[0][0] or "<think>" in prompt:
long_tokens.append(tokens.shape[1])
else:
short_tokens.append(tokens.shape[1])
# Extract prediction wrapped by "\\boxed{}"
prediction_match = re.findall(r"\\boxed\{(.+?)\}", str(solution))
if len(prediction_match) > 0:
prediction = prediction_match[-1]
#print(solution[0][0][-100:])
else:
# https://huggingface.co/PowerInfer/SmallThinker-3B-Preview/discussions/9
patterns = [
r"\*+Final\s+Answer\*+\s*\n*\s*\\\[\s*\\boxed\s*{\s*([0-9.-]+)\s*}\s*\\\]",
r"\*+Final\s+Answer\*+\s*\n*\s*\\\[\s*([0-9.-]+)\s*\\\]",
r"\*?Final\s+Answer\*?\s*[:=]\s*([0-9.-]+)",
r"[Tt]he\s+[Ff]inal\s+[Aa]nswer\s+[Ii]s\s*[:=]?\s*([0-9.-]+)",
r"[Ff]inal\s+[Aa]nswer\s*[:=]\s*([0-9.-]+)",
]
for pattern in patterns:
prediction_match = re.search(pattern, str(solution))
if prediction_match:
prediction = prediction_match.group(1)
break
if not prediction_match:
# if no prediction found, pick the last value in the solution
prediction = re.findall(r"[-+]?\d*\.\d+|\d+", str(solution))
if prediction:
prediction = prediction[-1]
else:
prediction = None
#print("------------------")
# print the tail content of the solution
#print(solution[0][0][-100:])
# Check if prediction matches the expected answer
if prediction is not None:#prediction == expected_answer:
try:
prediction = float(prediction)
expected_answer = float(expected_answer)
if abs(float(prediction) - float(expected_answer))<1e-6:
correct_predictions += 1
if "<think>" in solution[0][0] or "<think>" in prompt:
long_correctness[-1] = True
else:
short_correctness[-1] = True
else:
pass
#print("------------------")
##print(solution[0][0][-1000:])
#print("Wrong", prediction, " | ", expected_answer)
#print("------------------")
except ValueError:
continue
# Calculate accuracy
accuracy = (correct_predictions / total_entries) if total_entries > 0 else 0
return accuracy, num_tokens, n_long/total_entries, long_tokens, short_tokens, long_correctness, short_correctness
# Example usage
import sys, os
import transformers
if __name__ == "__main__":
tokenizer_path = sys.argv[1]
jsonl_file_path = sys.argv[2]
if os.path.isdir(jsonl_file_path):
json_files = [os.path.join(jsonl_file_path, f) for f in os.listdir(jsonl_file_path) if (f.endswith('.jsonl') and f.startswith('samples_aime'))]
else:
json_files = [jsonl_file_path]
tokenizer = transformers.AutoTokenizer.from_pretrained(tokenizer_path)
acc_list = []
avg_tokens_list = []
perc_long_list = []
long_tokens_list = []
short_tokens_list = []
long_correctness_list = []
short_correctness_list = []
for f in json_files:
accuracy, num_tokens, perc_long, long_tokens, short_tokens, long_correctness, short_correctness = extract_and_calculate_accuracy(f, tokenizer)
acc_list.append(accuracy)
avg_tokens_list.extend(num_tokens)
perc_long_list.append(perc_long)
long_tokens_list.extend(long_tokens)
short_tokens_list.extend(short_tokens)
long_correctness_list.extend(long_correctness)
short_correctness_list.extend(short_correctness)
#print("-"*10)
#print(f"Evaluating {len(json_files)} files: {json_files}")
print("-"*10)
print("AIME 2024")
print(f"Full Results: {acc_list}")
print(f"Thinking Mode: {perc_long_list} ({100*sum(perc_long_list)/len(perc_long_list):.2f}%)")
if len(short_correctness) > 0:
print(f"Short Pass@1: {sum(short_correctness_list)/len(short_correctness_list):.4f}, #Tokens: {sum(short_tokens_list)/len(short_tokens_list):.0f}")
if len(long_correctness) > 0:
print(f"Long Pass@1: {sum(long_correctness_list)/len(long_correctness_list):.4f}, #Tokens: {sum(long_tokens_list)/len(long_tokens_list):.0f}")
print(f"Avg Pass@1: {sum(acc_list)/len(acc_list):.4f}")
print(f"Avg #Tokens: {sum(avg_tokens_list)/len(avg_tokens_list):.0f}")
print("-"*10)