-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_5_samples.py
More file actions
389 lines (306 loc) Β· 12.8 KB
/
test_5_samples.py
File metadata and controls
389 lines (306 loc) Β· 12.8 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
#!/usr/bin/env python3
"""
Test 5 samples with zero-shot to verify the fix.
"""
import sys
import os
from pathlib import Path
import json
import time
import torch
import numpy as np
# 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):
"""Test our framework on the samples."""
print(f"π§ Testing Our Framework (zero-shot)")
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": 0,
"temperature": 0.0, # Use deterministic generation for single branch
"top_p": 1.0, # Use deterministic generation for single branch
"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):
"""Test direct model generation on the samples."""
print(f"\nπ§ Testing Direct Generation (zero-shot)")
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
prompt = f"Q: {sample['question']}\nA:"
# Set seed for deterministic generation
torch.manual_seed(42)
np.random.seed(42)
# Also set CUDA seed if available
if torch.cuda.is_available():
torch.cuda.manual_seed(42)
torch.cuda.manual_seed_all(42)
# Generate using the model directly
generated = model.generate(
prompt,
max_tokens=512,
temperature=0.0, # Use deterministic generation for single branch
top_p=1.0, # Use deterministic generation for single branch
do_sample=False, # Use deterministic generation for single branch
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]
# Strip whitespace to match our framework's behavior
answer_text = answer_text.strip()
# Extract answer using the same method as our framework
answer = extract_answer_like_framework(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 predicted answer
pred_clean = clean_answer(predicted)
# Extract final answer from ground truth (look for #### pattern)
gt_clean = extract_final_answer_from_ground_truth(ground_truth)
return pred_clean == gt_clean
def extract_final_answer_from_ground_truth(ground_truth):
"""Extract the final answer from ground truth format."""
import re
# Look for #### pattern at the end
match = re.search(r'####\s*([^\n]+)', ground_truth)
if match:
return clean_answer(match.group(1))
# Fallback: look for the last number in the text
numbers = re.findall(r'([0-9,]+(?:\.[0-9]+)?)', ground_truth)
if numbers:
return clean_answer(numbers[-1])
return clean_answer(ground_truth)
def clean_answer(answer):
"""Clean answer for comparison using the same method as our framework."""
import re
if not answer:
return ""
answer = str(answer).strip()
# Remove common prefixes
answer = re.sub(r'^(The answer is|Answer:|Final answer:?)\s*', '', answer, flags=re.IGNORECASE)
# Remove dollar signs and other currency symbols
answer = re.sub(r'[\$\s]+', '', answer)
# Remove boxed formatting (handle nested cases like $\boxed{$70,000}$), but keep the number
answer = re.sub(r'\\boxed\{([^}]+)\}', r'\1', answer)
# Remove brackets, parentheses
answer = re.sub(r'^[\\[\\](){}]+|[\\[\\](){}]+$', '', answer)
# Remove trailing punctuation (periods, commas, etc.)
answer = re.sub(r'[.,;:!?]+$', '', answer)
# Remove commas from numbers (e.g., "70,000" -> "70000")
answer = answer.replace(",", "")
# Convert to float and back to remove unnecessary decimals (e.g., "18.0" -> "18")
try:
num = float(answer)
if num == int(num):
return str(int(num))
else:
return str(num)
except ValueError:
return answer
def extract_answer_like_framework(text):
"""Extract answer using the same method as our framework."""
import re
if not text or not text.strip():
return ""
# Use the same patterns as our framework
answer_patterns = [
re.compile(r"####\s*([-+]?\d+(?:\.\d+)?)", re.I), # #### answer
re.compile(r"final answer.*?([-+]?\d+(?:\.\d+)?)", re.I), # final answer
re.compile(r"answer is\s*[:\s]?([-+]?\d[\d,]*(?:\.\d+)?)(?=[\.\n]|$)", re.I), # answer is
]
number_pattern = re.compile(r"[-+]?\d[\d,]*(?:\.\d+)?")
# Try to find explicit answer patterns first
for pattern in answer_patterns:
match = pattern.search(text)
if match:
extracted_answer = match.group(1).strip()
cleaned_answer = clean_answer(extracted_answer)
if is_valid_answer(cleaned_answer):
return cleaned_answer
# Fallback: find the last number in the text
all_numbers = number_pattern.findall(text)
if all_numbers:
last_number = clean_answer(all_numbers[-1])
if is_valid_answer(last_number):
return last_number
return ""
def is_valid_answer(answer: str) -> bool:
"""Check if an answer is valid (not empty, reasonable number)."""
if not answer or answer.strip() == "":
return False
try:
num = float(answer)
# Check if it's a reasonable number (not too large or too small)
return -100000 <= num <= 1000000
except ValueError:
return False
def main():
"""Main test function."""
print("π¬ 5-Sample Test: Zero-Shot (Fixed)")
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=5)
samples = []
for item in gsm8k_data:
samples.append({
'question': item['question'],
'answer': item['answer']
})
print(f"Loaded {len(samples)} samples")
# Test our framework
our_results = test_our_framework(model, samples)
# Test direct generation
direct_results = test_direct_generation(model, samples)
# Print comparison
print(f"\nπ 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'})")
# Check if reasoning is identical
print(f"\nπ Reasoning Comparison:")
identical_count = 0
for i in range(len(samples)):
our_reasoning = our_results['results'][i]['our_reasoning']
direct_reasoning = direct_results['results'][i]['direct_reasoning']
identical = our_reasoning == direct_reasoning
if identical:
identical_count += 1
print(f" Problem {i+1}: {'β
Identical' if identical else 'β Different'}")
print(f"\nπ Reasoning Identical: {identical_count}/{len(samples)} ({identical_count/len(samples):.1%})")
# Save detailed results
output_file = "test_5_samples_results.json"
all_results = {
"our_framework": our_results,
"direct_generation": direct_results
}
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}")
except Exception as e:
print(f"β Error during test: {e}")
import traceback
traceback.print_exc()
sys.exit(1)
if __name__ == "__main__":
main()