-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy patheval_longgenbench.py
165 lines (129 loc) · 5.41 KB
/
eval_longgenbench.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
import os
import json
import argparse
import numpy as np
import re
def parse_args(args=None):
parser = argparse.ArgumentParser()
parser.add_argument('--results_dir', type=str, default=None)
return parser.parse_args(args)
def extract_final_answer(answer):
"""Extracts the final answer from the answer string."""
match = re.search(r'####\s*(\d+)', answer)
return match.group(1) if match else None
def extract_predicted_answers(pred):
"""Extracts the predicted answers from the pred string."""
matches = re.findall(r'Answer_\d+:\s*.*?answer is (\d+)', pred, re.DOTALL)
return matches
def compare_answers(pred, answers):
# Extract final answers from the answers list
expected_answers = [extract_final_answer(ans) for ans in answers]
# Extract predicted answers from the pred string
predicted_answers = extract_predicted_answers(pred)
# print(f'Expected: {expected_answers}')
# print(f'Predicted: {predicted_answers}')
# Compare the two lists and calculate accuracy
results = {}
correct_count = 0
for i, (expected, predicted) in enumerate(zip(expected_answers, predicted_answers)):
is_correct = expected == predicted
results[f'Answer_{i+9}'] = {
'expected': expected,
'predicted': predicted,
'correct': is_correct
}
if is_correct:
correct_count += 1
# Calculate accuracy
total_questions = len(expected_answers)
accuracy = correct_count / total_questions if total_questions > 0 else 0.0
# print(f'Accuracy: {accuracy:.4f}')
return accuracy
def extract_predicted_choices(pred):
"""Extracts the predicted answers (A, B, C, D, E, etc.) from the pred string, considering Answer_... patterns."""
matches = re.findall(r'Answer_\d+:\s*.*?answer is \((.*?)\)', pred)
return matches
def compare_choices(pred, answers):
# Extract final answers from the answers list
expected_answers = answers
# Extract predicted answers from the pred string
predicted_answers = extract_predicted_choices(pred)
# print(f'Expected: {expected_answers}')
# print(f'Predicted: {predicted_answers}')
# Compare the two lists and calculate accuracy
results = {}
correct_count = 0
for i, (expected, predicted) in enumerate(zip(expected_answers, predicted_answers)):
is_correct = expected == predicted
results[f'Answer_{i+6}'] = {
'expected': expected,
'predicted': predicted,
'correct': is_correct
}
if is_correct:
correct_count += 1
# Calculate accuracy
total_questions = len(expected_answers)
accuracy = correct_count / total_questions if total_questions > 0 else 0.0
# print(f'Accuracy: {accuracy:.4f}')
return accuracy
def scorer(dataset, predictions, answers):
scores = []
for (prediction, ground_truths) in zip(predictions, answers):
if dataset in ["gsm8k"]:
scores.append(compare_answers(prediction, ground_truths))
elif dataset in ["mmlu","csqa"]:
scores.append(compare_choices(prediction, ground_truths))
return round(100 * np.mean(scores), 4)
if __name__ == '__main__':
args = parse_args()
dataset_list = [
"gsm8k",
"mmlu",
"csqa",
]
results_list = [
["dataset"],
["SnapKV"],
["StreamingLLM"],
["H2O"],
["PyramidKV"],
["ALLKV"],
]
for dataset in dataset_list:
results_list[0].append(dataset)
for idx, method in enumerate(["SnapKV", "StreamingLLM", "H2O", "PyramidKV", "ALLKV"]):
try:
args.method = method
args.dataset = dataset
args.eval_file = os.path.join(args.results_dir,dataset,f"{method}.json")
scores = dict()
predictions, answers, lengths = [], [], []
acc = []
# dataset = filename.split('.')[0]
with open(args.eval_file, "r", encoding="utf-8") as f:
for line in f:
try:
data = json.loads(line)
predictions.append(data["pred"])
answers.append(data["answers"])
if "length" in data:
lengths.append(data["length"])
except:
print("error")
score = scorer(args.dataset, predictions, answers)
avg_length = round(np.mean(lengths), 2)
print(avg_length)
scores[args.dataset] = score
output_dir = os.path.dirname(args.eval_file)
results_list[idx+1].append(score)
with open(os.path.join(output_dir, "metrics.json"), "w") as f:
json.dump(scores, f, ensure_ascii=False, indent=4)
print(f"dataset {args.dataset} method {args.method} scores {scores}")
except:
results_list[idx+1].append(-1)
print(f"dataset {args.dataset} method {args.method} scores {None}")
import csv
with open(os.path.join(args.results_dir,f"results.csv"), 'w') as fp:
writer = csv.writer(fp)
writer.writerows(results_list)