-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
325 lines (278 loc) · 11.4 KB
/
main.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
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
import argparse
import json
import re
import threading
from concurrent.futures import ThreadPoolExecutor
import numpy as np
from harness.llm_reasoning_harness import LLMReasoningHarness
from harness.test_cases import TESTS_LITE, TESTS_FULL, TESTS_BAYESIAN, TESTS_BAYESIAN_SINGLE
from models.model_factory import ModelFactory
from utils.checkpoint import load_checkpoint, save_checkpoint
from utils.metrics import get_novelty_scores
from utils.result_handler import dump_results
def run_test(model, test_idx, tuple_length, test_rule, sleep=0,
use_bayesian=False, old_context=None):
print(f"Running test {test_idx}")
print(f"Tuple length is {tuple_length}")
harness = LLMReasoningHarness(
model=model,
rule_lambda=test_rule,
tuple_length=tuple_length,
use_bayesian=use_bayesian,
old_context=old_context
)
test_success = harness.interact_with_llm(sleep=sleep)
return {
'test_index': test_idx,
'conversation_history': harness.conversation_history,
'points': test_success.get('points', 0),
'size_principle_points': test_success.get('size_principle_points', 0),
'guesses': test_success.get('guesses', 0),
'repeats': test_success.get('reused tests', 0),
'confirms bias': 1, # Placeholder value
'novelty': get_novelty_scores(harness.conversation_history)
}
def main(model_name, multi, split, resume, sleep=0, single=None):
select_split = {
'lite': TESTS_LITE,
'full': TESTS_FULL,
'bayesian': TESTS_BAYESIAN,
'bayesian_single': TESTS_BAYESIAN_SINGLE
}
TESTS = select_split.get(split)
if TESTS is None:
raise ValueError(f"Invalid split name: {split}")
model = ModelFactory.create_model(model_name)
checkpoint = load_checkpoint(model_name, split=split)
use_bayesian = split in ['bayesian', 'bayesian_single']
if single:
print(f"Attempting to complete test {single}!")
current_test_idx = checkpoint.get('current_test_idx', 1)
correct_answers = checkpoint.get('correct_answers', 0)
total_answers = checkpoint.get('total_answers', 0)
total_repeats = checkpoint.get('total_repeats', 0)
points = checkpoint.get('points', 0)
attempts = checkpoint.get('attempts', [])
novelty_scores = checkpoint.get('novelty_scores', [])
full_context = checkpoint.get('full_context', [])
fixed_run = run_test(
model=model,
test_idx=single,
tuple_length=3,
test_rule=TESTS[str(single)],
sleep=sleep,
use_bayesian=False
)
full_context[single - 1] = fixed_run
novelty_scores[single - 1] = fixed_run['novelty']
attempts[single - 1] = fixed_run['guesses']
total_repeats += fixed_run['repeats']
points += fixed_run['points']
if fixed_run['points'] >= 1000:
correct_answers += 1
save_checkpoint(
model_name,
current_test_idx=checkpoint.get('current_test_idx', 1),
correct_answers=correct_answers,
total_answers=total_answers,
total_repeats=total_repeats,
points=points,
attempts=attempts,
novelty_scores=novelty_scores,
full_context=full_context,
split=split
)
return
if checkpoint and resume:
print("Resuming from checkpoint...")
current_test_idx = checkpoint.get('current_test_idx', 1)
correct_answers = checkpoint.get('correct_answers', 0)
total_answers = checkpoint.get('total_answers', 0)
total_repeats = checkpoint.get('total_repeats', 0)
points = checkpoint.get('points', 0)
attempts = checkpoint.get('attempts', [])
novelty_scores = checkpoint.get('novelty_scores', [])
full_context = checkpoint.get('full_context', [])
else:
current_test_idx = 1
correct_answers = 0
total_answers = 0
total_repeats = 0
points = 0
attempts = []
novelty_scores = []
full_context = []
num_tests = len(TESTS)
lock = threading.Lock()
def process_result(result):
nonlocal correct_answers, total_answers, total_repeats, points, \
attempts, novelty_scores, full_context
with lock:
if result['points'] >= 1000:
correct_answers += 1
points += result['points']
total_answers += 1
total_repeats += result['repeats']
attempts.append(result['guesses'])
novelty_scores.append(result['novelty'])
full_context.append(result)
save_checkpoint(
model_name,
current_test_idx=result['test_index'] + 1,
correct_answers=correct_answers,
total_answers=total_answers,
total_repeats=total_repeats,
points=points,
attempts=attempts,
novelty_scores=novelty_scores,
full_context=full_context,
split=split
)
with ThreadPoolExecutor(max_workers=multi) as executor:
futures = []
for test_idx in range(current_test_idx, num_tests + 1):
tuple_len = 1 if split == "bayesian_single" else 3
test_rule = TESTS.get(str(test_idx))
if not test_rule:
print(f"No test rule found for test index {test_idx}. Skipping.")
continue
future = executor.submit(
run_test,
model,
test_idx,
tuple_length=tuple_len,
test_rule=test_rule,
sleep=sleep,
use_bayesian=use_bayesian
)
future.add_done_callback(lambda f: process_result(f.result()))
futures.append(future)
# Wait for all futures to complete
for future in futures:
future.result()
acc = correct_answers / total_answers if total_answers > 0 else 0.0
avg_guesses = np.mean(attempts) if attempts else 0.0
min_novelties = [min(x) for x in novelty_scores if x]
print("===========")
print("Final Result")
print(f"Accuracy: {correct_answers} / {total_answers} = {acc:.2%}")
print(f"Average Tests = {avg_guesses:.2f}")
print(f"Average Minimum Novelty = {np.mean(min_novelties):.2f}" if min_novelties else "Average Minimum Novelty = N/A")
print(f"Total Repeated Tests = {total_repeats}")
print(f"Total Points = {points}")
dump_results(model_name, acc, avg_guesses, points, full_context, split)
def testswap_test(model_name, swap, sleep=0):
TESTS = TESTS_FULL # Hardcoded for now
checkpoint = load_checkpoint(model_name, split=f"test_swap_{swap}")
model = ModelFactory.create_model(model_name)
if checkpoint and 'current_test_idx' in checkpoint:
print("Resuming from checkpoint...")
current_test_idx = checkpoint.get('current_test_idx', 1)
correct_answers = checkpoint.get('correct_answers', 0)
total_answers = checkpoint.get('total_answers', 0)
total_repeats = checkpoint.get('total_repeats', 0)
points = checkpoint.get('points', 0)
attempts = checkpoint.get('attempts', [])
novelty_scores = checkpoint.get('novelty_scores', [])
full_context = checkpoint.get('full_context', [])
else:
current_test_idx = 1
correct_answers = 0
total_answers = 0
total_repeats = 0
points = 0
attempts = []
novelty_scores = []
full_context = []
with open(f'./results/{swap}_full_context.json', 'r') as f:
swap_context = json.load(f)
swap_context = sorted(swap_context, key=lambda x: x['test_index'])
for (i, test_rule), sw_test in zip(TESTS.items(), swap_context):
test_idx = int(i)
print(test_idx)
if test_idx < current_test_idx:
continue
assert test_idx == sw_test['test_index'], f"Test index mismatch: {test_idx} != {sw_test['test_index']}"
pattern = r"input\s*(\(.*?\):\s*\w+)"
harness_responses = [
x["content"] for x in sw_test["conversation_history"] if x["role"] == "user"
]
pulled_test_cases = [
match.group(1) for s in harness_responses
if (match := re.search(pattern, s))
]
test_cases_str = '\n'.join(pulled_test_cases)
with open('./prompts/instruction_testswap.txt', 'r') as f:
case_with_tests = f.read().format(testcases=test_cases_str)
harness = LLMReasoningHarness(
model=model,
rule_lambda=test_rule,
force_prompt=case_with_tests
)
test_success = harness.interact_with_llm(sleep=sleep)
result = {
'test_index': test_idx,
'conversation_history': harness.conversation_history,
'points': test_success.get('points', 0),
'size_principle_points': 0,
'guesses': 0,
'repeats': 0,
'confirms bias': 1,
'novelty': [1.0]
}
if result['points'] >= 1000:
correct_answers += 1
points += result['points']
total_answers += 1
full_context.append(result)
current_test_idx = test_idx + 1
save_checkpoint(
model_name=model_name,
current_test_idx=current_test_idx,
correct_answers=correct_answers,
total_answers=total_answers,
total_repeats=total_repeats,
points=points,
attempts=attempts,
novelty_scores=novelty_scores,
full_context=full_context,
split=f"test_swap_{swap}"
)
print(f"Completed test {test_idx}. Progress saved.")
accuracy = correct_answers / total_answers if total_answers > 0 else 0.0
print("===========")
print("Final Results for Test Swap")
print(f"Accuracy: {correct_answers} / {total_answers} = {accuracy:.2%}")
dump_results(model_name, accuracy, 0, 0, full_context, f"test_swap_{swap}")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="LLM Reasoning Harness")
parser.add_argument('--model', type=str, default="chatgpt-4o-latest",
help='Model to use (e.g. llama3-70b-8192)')
parser.add_argument('--multi', type=int, default=1,
help='Number of tests to run in parallel')
parser.add_argument('--split', type=str, default='lite',
help='Which test set to run')
parser.add_argument('--resume', action='store_true',
help='Resume from checkpoint')
parser.add_argument('--sleep', type=float, default=0.0,
help='Sleep timer between calls')
parser.add_argument('--single', type=int, default=None,
help='Finish a single test if interrupted')
parser.add_argument('--swap', type=str, default=None,
help='If set, load test cases from another model for each test')
args = parser.parse_args()
if args.swap is None:
main(
model_name=args.model,
multi=args.multi,
split=args.split,
resume=args.resume,
sleep=args.sleep,
single=args.single
)
else:
testswap_test(
model_name=args.model,
swap=args.swap,
sleep=args.sleep
)