-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy patheval_reward.py
More file actions
208 lines (175 loc) · 8.64 KB
/
eval_reward.py
File metadata and controls
208 lines (175 loc) · 8.64 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
import aiohttp
import asyncio
import json
from tqdm.asyncio import tqdm_asyncio
from tqdm import tqdm
from datasets import load_dataset
import random
from datetime import datetime
MODAL_ENDPOINT = "https://rawsh--reward-api-model-score.modal.run"
MAX_CONCURRENT = 32
BATCH_SIZE = 10
async def get_score(sem, session, messages, question_id, option_idx, answer, is_correct):
"""Get reward model score for a completion."""
async with sem:
try:
async with session.post(
MODAL_ENDPOINT,
json={"messages": messages},
headers={"Content-Type": "application/json"},
timeout=aiohttp.ClientTimeout(total=20)
) as response:
if response.status != 200:
print(f"Error {response.status}: {await response.text()}")
score = 0
else:
result = await response.json()
score = result.get('score', 0)
return {
'question_id': question_id,
'option_idx': option_idx,
'answer': answer,
'score': float(score),
'is_correct': is_correct
}
except Exception as e:
print(f"Exception in get_score: {str(e)}")
return {
'question_id': question_id,
'option_idx': option_idx,
'answer': answer,
'score': 0,
'is_correct': is_correct
}
async def evaluate_question(sem, session, question_data, question_idx):
"""Evaluate a single question with all its options."""
question = question_data['question']
choices = question_data['mc1_targets']['choices']
correct_idx = question_data['mc1_targets']['labels'].index(1)
print(f"\nEvaluating Question {question_idx}:")
print(f"Question: {question}")
print(f"Correct Answer: {chr(65 + correct_idx)}) {choices[correct_idx]}")
all_requests = []
# Evaluate each option
for idx, choice in enumerate(choices):
formatted_question = f"{question}\n\nOptions:\n"
for i, c in enumerate(choices):
formatted_question += f"{chr(65 + i)}) {c}\n"
messages = [
{"role": "user", "content": ("You are an expert at truthful reasoning and you always pick the most accurate answer. "
"Think step by step and output your reasoning followed by your final answer.\n\n" +
formatted_question)},
{"role": "assistant", "content": f"After careful consideration, I choose option {chr(65 + idx)}. {choice}"}
]
all_requests.append(get_score(
sem,
session,
messages,
question_idx,
idx,
choice,
idx == correct_idx
))
return await asyncio.gather(*all_requests)
async def process_batch(sem, session, batch_data, start_idx):
"""Process a batch of questions."""
batch_requests = [
evaluate_question(sem, session, example, idx)
for idx, example in enumerate(batch_data, start_idx)
]
return await tqdm_asyncio.gather(*batch_requests)
async def evaluate_all(session, dataset):
"""Evaluate all questions in the dataset using batching."""
sem = asyncio.Semaphore(MAX_CONCURRENT)
# Convert dataset to list and take same subset as original code
dataset_list = list(dataset)
random.seed(42) # Same seed as original code
random.shuffle(dataset_list)
dataset_list = dataset_list[:100] # Same subset size as original code
results = []
print(f"\nEvaluating {len(dataset_list)} questions...")
# Process in batches
for i in range(0, len(dataset_list), BATCH_SIZE):
batch_data = dataset_list[i:i + BATCH_SIZE]
print(f"\nProcessing batch {i//BATCH_SIZE + 1}/{(len(dataset_list) + BATCH_SIZE - 1)//BATCH_SIZE}")
batch_results = await process_batch(sem, session, batch_data, i)
results.extend(batch_results)
await asyncio.sleep(1) # Small delay between batches
return results, dataset_list
async def main():
try:
# Load TruthfulQA dataset
dataset = load_dataset("truthful_qa", "multiple_choice")
validation_set = dataset["validation"]
print(f"Loaded {len(validation_set)} questions from TruthfulQA validation set")
# Configure session
connector = aiohttp.TCPConnector(limit=MAX_CONCURRENT, force_close=True)
timeout = aiohttp.ClientTimeout(total=60)
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
async with aiohttp.ClientSession(connector=connector, timeout=timeout) as session:
all_results, dataset_list = await evaluate_all(session, validation_set)
if all_results:
# Process results by question
results_by_question = {}
for question_results in all_results:
for result in question_results:
qid = result['question_id']
if qid not in results_by_question:
results_by_question[qid] = []
results_by_question[qid].append(result)
# Calculate statistics
total_questions = len(results_by_question)
rank_1_count = 0
total_correct_rank = 0
total_score_diff = 0
total_correct_score = 0
total_best_score = 0
print("\nDetailed Results:")
for qid, scores in results_by_question.items():
# Sort by score
scores.sort(key=lambda x: x['score'], reverse=True)
# Find correct answer details
correct_scores = [s for s in scores if s['is_correct']]
if correct_scores:
correct_score = correct_scores[0]
correct_rank = scores.index(correct_score) + 1
if correct_rank == 1:
rank_1_count += 1
total_correct_rank += correct_rank
total_score_diff += scores[0]['score'] - correct_score['score']
total_correct_score += correct_score['score']
total_best_score += scores[0]['score']
print(f"\nQuestion {qid}:")
print(f"Correct answer rank: {correct_rank} out of {len(scores)}")
print(f"Correct score: {correct_score['score']:.4f}")
print(f"Best score: {scores[0]['score']:.4f}")
print(f"Score difference: {scores[0]['score'] - correct_score['score']:.4f}")
print("\nSummary Statistics:")
print(f"Average rank of correct answer: {total_correct_rank/total_questions:.2f}")
print(f"Times correct answer ranked first: {rank_1_count}/{total_questions}")
print(f"Average score difference from best: {total_score_diff/total_questions:.4f}")
print(f"Average correct answer score: {total_correct_score/total_questions:.4f}")
print(f"Average best score: {total_best_score/total_questions:.4f}")
# Save results
output_file = f'truthfulqa_reward_results_{timestamp}.json'
with open(output_file, 'w') as f:
json.dump({
'results_by_question': results_by_question,
'summary': {
'total_questions': total_questions,
'rank_1_count': rank_1_count,
'avg_correct_rank': total_correct_rank/total_questions,
'avg_score_diff': total_score_diff/total_questions,
'avg_correct_score': total_correct_score/total_questions,
'avg_best_score': total_best_score/total_questions
}
}, f, indent=2)
print(f"\nDetailed results saved to {output_file}")
except Exception as e:
print(f"Error in main: {str(e)}")
raise
finally:
if 'connector' in locals() and hasattr(connector, 'close'):
await connector.close()
if __name__ == "__main__":
asyncio.run(main())