-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_framework_comparison.py
More file actions
313 lines (248 loc) · 9.76 KB
/
test_framework_comparison.py
File metadata and controls
313 lines (248 loc) · 9.76 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
#!/usr/bin/env python3
"""
Direct comparison test: Our framework vs simple generation.
This will help us understand if the issue is with our framework or the model itself.
"""
import sys
import os
from pathlib import Path
import json
import time
# 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_simple_generation(model, problems, num_samples=10):
"""Test simple generation without adaptive CoT."""
print("🧪 Testing Simple Generation (No Adaptive CoT)")
print("-" * 50)
results = []
for i, problem in enumerate(problems[:num_samples]):
print(f"\n📝 Problem {i+1}/{num_samples}:")
print(f"Q: {problem['question']}")
# Simple generation
prompt = f"Q: {problem['question']}\nA:"
try:
# Generate single response
response = model.generate(
prompt,
max_tokens=512,
temperature=0.7,
top_p=0.95,
do_sample=True
)
# Extract answer
answer = extract_simple_answer(response)
print(f"Generated: {response[:200]}...")
print(f"Extracted Answer: {answer}")
print(f"Ground Truth: {problem['answer']}")
# Check accuracy
is_correct = check_accuracy(answer, problem['answer'])
print(f"Correct: {'✅' if is_correct else '❌'}")
results.append({
'problem': problem['question'],
'ground_truth': problem['answer'],
'generated': response,
'extracted_answer': answer,
'correct': is_correct
})
except Exception as e:
print(f"❌ Error: {e}")
results.append({
'problem': problem['question'],
'ground_truth': problem['answer'],
'generated': '',
'extracted_answer': '',
'correct': False,
'error': str(e)
})
return results
def test_adaptive_cot(model, problems, num_samples=10):
"""Test with our Adaptive CoT framework."""
print("\n🧪 Testing Adaptive CoT Framework")
print("-" * 50)
# Create Adaptive CoT configuration
adaptive_config = {
"adaptive_branching": True,
"min_branches": 3,
"max_branches": 8,
"default_branches": 5,
"num_fewshot": 2,
"temperature": 0.7,
"top_p": 0.95,
"max_tokens": 512,
}
# Create Adaptive CoT instance
adaptive_cot = AdaptiveCoT(model, adaptive_config)
results = []
for i, problem in enumerate(problems[:num_samples]):
print(f"\n📝 Problem {i+1}/{num_samples}:")
print(f"Q: {problem['question']}")
try:
# Use Adaptive CoT
result = adaptive_cot.solve_problem(problem['question'])
answer = result['final_answer']
branches_used = result.get('num_branches', 0)
reasoning_paths = result.get('reasoning_paths', [])
print(f"Answer: {answer}")
print(f"Branches Used: {branches_used}")
print(f"Ground Truth: {problem['answer']}")
# Check accuracy
is_correct = check_accuracy(answer, problem['answer'])
print(f"Correct: {'✅' if is_correct else '❌'}")
# Show first reasoning path
if reasoning_paths:
print(f"First Reasoning Path: {reasoning_paths[0][:200]}...")
results.append({
'problem': problem['question'],
'ground_truth': problem['answer'],
'answer': answer,
'branches_used': branches_used,
'reasoning_paths': reasoning_paths,
'correct': is_correct
})
except Exception as e:
print(f"❌ Error: {e}")
results.append({
'problem': problem['question'],
'ground_truth': problem['answer'],
'answer': '',
'branches_used': 0,
'reasoning_paths': [],
'correct': False,
'error': str(e)
})
return results
def extract_simple_answer(text):
"""Extract answer from simple generation."""
import re
# Look for patterns
patterns = [
r"\\boxed\{([^}]+)\}",
r"\$([^$]+)\$",
r"The final answer is:? ([^\n]+)",
r"Final answer:? ([^\n]+)",
r"Answer:? ([^\n]+)",
]
for pattern in patterns:
match = re.search(pattern, text)
if match:
answer = match.group(1).strip()
if is_valid_answer(answer):
return answer
# Fallback: look for numbers in the last few lines
lines = text.split('\n')[-5:]
for line in lines:
numbers = re.findall(r'-?\d+\.?\d*', line)
if numbers:
return numbers[-1]
return ""
def is_valid_answer(answer):
"""Check if answer is valid."""
if not answer or len(answer) > 50:
return False
import re
return bool(re.search(r'\d', answer))
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
# Remove common prefixes
answer = re.sub(r'^(The answer is|Answer:|Final answer:?)\s*', '', answer, flags=re.IGNORECASE)
# Extract numbers
numbers = re.findall(r'-?\d+\.?\d*', answer)
if numbers:
return numbers[-1]
return answer.strip()
def main():
print("🔬 Framework Comparison Test - GSM8K (10 Samples)")
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=10)
problems = []
for item in gsm8k_data:
problems.append({
'question': item['question'],
'answer': item['answer']
})
print(f"Loaded {len(problems)} problems")
# Test 1: Simple generation
print("\n" + "="*60)
start_time = time.time()
simple_results = test_simple_generation(model, problems, 10)
simple_duration = time.time() - start_time
# Test 2: Adaptive CoT
print("\n" + "="*60)
start_time = time.time()
adaptive_results = test_adaptive_cot(model, problems, 10)
adaptive_duration = time.time() - start_time
# Calculate metrics
simple_correct = sum(1 for r in simple_results if r['correct'])
adaptive_correct = sum(1 for r in adaptive_results if r['correct'])
simple_accuracy = simple_correct / len(simple_results)
adaptive_accuracy = adaptive_correct / len(adaptive_results)
# Calculate average branches
avg_branches = sum(r.get('branches_used', 0) for r in adaptive_results) / len(adaptive_results)
# Print comparison
print("\n" + "="*60)
print("📊 COMPARISON RESULTS")
print("="*60)
print(f"Simple Generation:")
print(f" Accuracy: {simple_accuracy:.3f} ({simple_correct}/{len(simple_results)})")
print(f" Duration: {simple_duration:.2f}s")
print(f" Average Branches: 1")
print()
print(f"Adaptive CoT:")
print(f" Accuracy: {adaptive_accuracy:.3f} ({adaptive_correct}/{len(adaptive_results)})")
print(f" Duration: {adaptive_duration:.2f}s")
print(f" Average Branches: {avg_branches:.1f}")
print()
print(f"Improvement: {((adaptive_accuracy - simple_accuracy) / simple_accuracy * 100):+.1f}%")
# Save detailed results
comparison_results = {
'simple_generation': {
'results': simple_results,
'accuracy': simple_accuracy,
'duration': simple_duration,
'avg_branches': 1
},
'adaptive_cot': {
'results': adaptive_results,
'accuracy': adaptive_accuracy,
'duration': adaptive_duration,
'avg_branches': avg_branches
},
'comparison': {
'accuracy_improvement': ((adaptive_accuracy - simple_accuracy) / simple_accuracy * 100) if simple_accuracy > 0 else 0,
'efficiency_ratio': adaptive_duration / simple_duration if simple_duration > 0 else 0
}
}
with open('framework_comparison_results.json', 'w') as f:
json.dump(comparison_results, f, indent=2, default=str)
print(f"\n💾 Detailed results saved to: framework_comparison_results.json")
except Exception as e:
print(f"❌ Error during comparison: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()