-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsimple_lighteval_comparison.py
More file actions
438 lines (353 loc) · 14.4 KB
/
simple_lighteval_comparison.py
File metadata and controls
438 lines (353 loc) · 14.4 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
#!/usr/bin/env python3
"""
Simple comparison between our framework and direct model generation (LightEval-style).
"""
import sys
import os
from pathlib import Path
import json
import time
import random
# Add src to path
sys.path.append(str(Path(__file__).parent / "src"))
from models.model_factory import ModelFactory
from adaptive.adaptive_cot import AdaptiveCoT
from benchmarks.math_benchmarks import MathBenchmarkLoader
def test_our_framework(model, samples, num_fewshot=0):
"""Test our framework on the samples."""
print(f"🔧 Testing Our Framework (few-shot={num_fewshot})")
print("-" * 50)
# Create Adaptive CoT configuration for single branch
config = {
"adaptive_branching": False, # Disable adaptive branching for single branch
"min_branches": 1,
"max_branches": 1,
"default_branches": 1,
"num_fewshot": num_fewshot,
"temperature": 0.7,
"top_p": 0.95,
"max_tokens": 512,
}
adaptive_cot = AdaptiveCoT(model, config)
results = []
correct = 0
start_time = time.time()
for i, sample in enumerate(samples):
print(f"📝 Problem {i+1}/{len(samples)}: {sample['question'][:80]}...")
try:
result = adaptive_cot.solve_problem(sample['question'])
answer = result['final_answer']
reasoning_path = result.get('reasoning_paths', [''])[0] if result.get('reasoning_paths') else ''
# Check accuracy
is_correct = check_accuracy(answer, sample['answer'])
if is_correct:
correct += 1
results.append({
"problem_id": i + 1,
"question": sample['question'],
"ground_truth": sample['answer'],
"our_answer": answer,
"our_reasoning": reasoning_path,
"correct": is_correct
})
print(f" Our Answer: {answer}")
print(f" Ground Truth: {sample['answer']}")
print(f" Correct: {'✅' if is_correct else '❌'}")
except Exception as e:
print(f" ❌ Error: {e}")
results.append({
"problem_id": i + 1,
"question": sample['question'],
"ground_truth": sample['answer'],
"our_answer": "",
"our_reasoning": "",
"correct": False,
"error": str(e)
})
end_time = time.time()
duration = end_time - start_time
accuracy = correct / len(samples)
print(f"\n📊 Our Framework Results:")
print(f" Accuracy: {accuracy:.3f} ({correct}/{len(samples)})")
print(f" Duration: {duration:.2f}s")
return {
"results": results,
"accuracy": accuracy,
"correct": correct,
"total": len(samples),
"duration": duration
}
def test_direct_generation(model, samples, num_fewshot=0):
"""Test direct model generation (LightEval-style) on the samples."""
print(f"\n🔧 Testing Direct Generation (few-shot={num_fewshot})")
print("-" * 50)
results = []
correct = 0
start_time = time.time()
for i, sample in enumerate(samples):
print(f"📝 Problem {i+1}/{len(samples)}: {sample['question'][:80]}...")
try:
# Create prompt
if num_fewshot > 0:
from src.adaptive.fewshot_examples import FewShotExampleLoader
fewshot_loader = FewShotExampleLoader()
examples = fewshot_loader.get_fewshot_examples("gsm8k", num_fewshot)
prompt = fewshot_loader.format_fewshot_prompt(examples, sample['question'])
else:
prompt = f"Q: {sample['question']}\nA:"
# Generate using the model directly
generated = model.generate(
prompt,
max_tokens=512,
temperature=0.7,
top_p=0.95,
do_sample=True,
num_return_sequences=1
)
if isinstance(generated, list):
answer_text = generated[0]
else:
answer_text = generated
# Apply stop sequences
for stop_seq in ["Q:", "</s>", "<|im_end|>", "\n\nQ:"]:
if stop_seq in answer_text:
answer_text = answer_text.split(stop_seq)[0]
# Extract answer using improved method
answer = extract_answer_improved(answer_text)
# Check accuracy
is_correct = check_accuracy(answer, sample['answer'])
if is_correct:
correct += 1
results.append({
"problem_id": i + 1,
"question": sample['question'],
"ground_truth": sample['answer'],
"direct_answer": answer,
"direct_reasoning": answer_text,
"correct": is_correct
})
print(f" Direct Answer: {answer}")
print(f" Ground Truth: {sample['answer']}")
print(f" Correct: {'✅' if is_correct else '❌'}")
except Exception as e:
print(f" ❌ Error: {e}")
results.append({
"problem_id": i + 1,
"question": sample['question'],
"ground_truth": sample['answer'],
"direct_answer": "",
"direct_reasoning": "",
"correct": False,
"error": str(e)
})
end_time = time.time()
duration = end_time - start_time
accuracy = correct / len(samples)
print(f"\n📊 Direct Generation Results:")
print(f" Accuracy: {accuracy:.3f} ({correct}/{len(samples)})")
print(f" Duration: {duration:.2f}s")
return {
"results": results,
"accuracy": accuracy,
"correct": correct,
"total": len(samples),
"duration": duration
}
def check_accuracy(predicted, ground_truth):
"""Check if predicted answer matches ground truth."""
if not predicted or not ground_truth:
return False
# Clean both answers
pred_clean = clean_answer(predicted)
gt_clean = clean_answer(ground_truth)
return pred_clean == gt_clean
def clean_answer(answer):
"""Clean answer for comparison."""
import re
if not answer:
return ""
# Remove common prefixes
answer = re.sub(r'^(The answer is|Answer:|Final answer:?)\s*', '', answer, flags=re.IGNORECASE)
# Remove trailing punctuation (periods, commas, etc.)
answer = re.sub(r'[.,;:!?]+$', '', answer)
# Extract numbers
numbers = re.findall(r'-?\d+\.?\d*', answer)
if numbers:
return numbers[-1]
return answer.strip()
def extract_answer(text):
"""Extract answer from text using our framework's logic."""
import re
# Answer extraction patterns from our framework
answer_patterns = [
r"\\boxed\{([^}]+)\}", # \boxed{answer}
r"\$([^$]+)\$", # $answer$
r"Answer: ([^\n]+)",
r"Final answer: ([^\n]+)",
r"The final answer is:? ([^\n]+)",
r"The answer is:? ([^\n]+)",
r"= ([0-9]+(?:\.[0-9]+)?)",
]
# First, try to find the answer using patterns
for pattern in answer_patterns:
match = re.search(pattern, text, re.IGNORECASE)
if match:
answer = match.group(1).strip()
cleaned = clean_answer(answer)
if cleaned and is_valid_answer(cleaned):
return cleaned
# If no pattern match, try to find the first valid number
lines = text.strip().split('\n')
for line in lines:
number_matches = re.findall(r'(\d+(?:\.\d+)?)', line)
for number in number_matches:
if is_valid_answer(number):
return number
# Last resort: return the last line if it contains a number
if lines:
last_line = lines[-1].strip()
number_match = re.search(r'(\d+(?:\.\d+)?)', last_line)
if number_match:
return number_match.group(1)
return ""
def extract_answer_improved(text):
"""Improved answer extraction that looks for the final answer more carefully."""
import re
# First, try to find explicit answer patterns
answer_patterns = [
r"\\boxed\{([^}]+)\}", # \boxed{answer}
r"\$([^$]+)\$", # $answer$
r"Answer: ([^\n]+)",
r"Final answer: ([^\n]+)",
r"The final answer is:? ([^\n]+)",
r"The answer is:? ([^\n]+)",
r"= ([0-9]+(?:\.[0-9]+)?)",
]
for pattern in answer_patterns:
match = re.search(pattern, text, re.IGNORECASE)
if match:
answer = match.group(1).strip()
cleaned = clean_answer(answer)
if cleaned and is_valid_answer(cleaned):
return cleaned
# Look for patterns like "The answer is X" or "So the answer is X"
final_answer_patterns = [
r"(?:So )?the answer is:? ([0-9,]+)",
r"(?:Therefore,? )?the answer is:? ([0-9,]+)",
r"(?:Thus,? )?the answer is:? ([0-9,]+)",
r"(?:Hence,? )?the answer is:? ([0-9,]+)",
r"answer:? ([0-9,]+)",
]
for pattern in final_answer_patterns:
match = re.search(pattern, text, re.IGNORECASE)
if match:
answer = match.group(1).strip()
cleaned = clean_answer(answer)
if cleaned and is_valid_answer(cleaned):
return cleaned
# Look for the last number in the text that could be an answer
# Split by sentences and look for numbers
sentences = re.split(r'[.!?]\s*', text)
for sentence in reversed(sentences):
# Look for numbers in this sentence
numbers = re.findall(r'([0-9,]+)', sentence)
for number in reversed(numbers):
cleaned = clean_answer(number)
if cleaned and is_valid_answer(cleaned):
return cleaned
# Fallback: look for any valid number
lines = text.strip().split('\n')
for line in reversed(lines):
number_matches = re.findall(r'([0-9,]+)', line)
for number in reversed(number_matches):
cleaned = clean_answer(number)
if cleaned and is_valid_answer(cleaned):
return cleaned
return ""
def is_valid_answer(answer):
"""Check if an answer is valid."""
if not answer or answer.strip() == "":
return False
try:
num = float(answer)
return -1000 <= num <= 10000
except ValueError:
return False
def main():
"""Main comparison function."""
print("🔬 Our Framework vs Direct Generation Comparison")
print("=" * 60)
print("Testing on 30 GSM8K samples with single-branch generation")
print("=" * 60)
try:
# Load model
print("🔧 Loading model...")
model = ModelFactory.create_model(
model_type="deepseek",
model_name="/raid/LLM/llama3.1-8b-instruct",
config={"gpu_id": 0}
)
model.load_model()
# Load GSM8K dataset
print("📚 Loading GSM8K dataset...")
benchmark_loader = MathBenchmarkLoader(cache_dir="data_cache")
gsm8k_data = benchmark_loader.load_dataset("gsm8k", max_samples=30)
samples = []
for item in gsm8k_data:
samples.append({
'question': item['question'],
'answer': item['answer']
})
print(f"Loaded {len(samples)} samples")
# Test configurations
configs = [
{"name": "No Few-Shot", "num_fewshot": 0},
{"name": "2 Few-Shot", "num_fewshot": 2},
{"name": "8 Few-Shot", "num_fewshot": 8}
]
all_results = {}
for config in configs:
print(f"\n{'='*60}")
print(f"🧪 Testing {config['name']}")
print(f"{'='*60}")
# Test our framework
our_results = test_our_framework(model, samples, config['num_fewshot'])
# Test direct generation
direct_results = test_direct_generation(model, samples, config['num_fewshot'])
# Store results
all_results[config['name']] = {
"our_framework": our_results,
"direct_generation": direct_results
}
# Print comparison
print(f"\n📊 {config['name']} Comparison:")
print(f" Our Framework: {our_results['accuracy']:.3f} ({our_results['correct']}/{our_results['total']}) - {our_results['duration']:.2f}s")
print(f" Direct Generation: {direct_results['accuracy']:.3f} ({direct_results['correct']}/{direct_results['total']}) - {direct_results['duration']:.2f}s")
# Calculate difference
acc_diff = our_results['accuracy'] - direct_results['accuracy']
print(f" Difference: {acc_diff:+.3f} ({'Our framework' if acc_diff > 0 else 'Direct generation'} {'wins' if abs(acc_diff) > 0.01 else 'tie'})")
# Save detailed results
output_file = "simple_comparison_results.json"
with open(output_file, 'w') as f:
json.dump(all_results, f, indent=2, default=str)
print(f"\n💾 Detailed results saved to: {output_file}")
# Print final summary
print(f"\n{'='*60}")
print("📊 FINAL SUMMARY")
print(f"{'='*60}")
for config_name, results in all_results.items():
print(f"\n{config_name}:")
our_acc = results['our_framework']['accuracy']
our_time = results['our_framework']['duration']
direct_acc = results['direct_generation']['accuracy']
direct_time = results['direct_generation']['duration']
print(f" Our Framework: {our_acc:.3f} accuracy, {our_time:.2f}s")
print(f" Direct Generation: {direct_acc:.3f} accuracy, {direct_time:.2f}s")
print(f" Difference: {our_acc - direct_acc:+.3f} accuracy")
except Exception as e:
print(f"❌ Error during comparison: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()