-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresearch_framework.py
More file actions
351 lines (276 loc) Β· 14.6 KB
/
research_framework.py
File metadata and controls
351 lines (276 loc) Β· 14.6 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
#!/usr/bin/env python3
"""
Research Framework for Adaptive CoT with Parallel Test-Time Scaling
This framework implements your specific requirements:
- Parallel test-time scaling with self-consistency + CoT
- Adaptive branch allocation based on prefill signals
- Default 8 branches for static baseline
- Reliable accuracy measurement
- Comprehensive research logging
- Support for reasoning models (DeepSeek-R1-Distill-Qwen, etc.)
- Math benchmarks (GSM8K, AIME, Olympiad, MATH)
"""
import argparse
import sys
import os
import yaml
import time
import json
from pathlib import Path
from typing import Dict, List, Any, Optional, Tuple
from datetime import datetime
import pandas as pd
# Add src to path
sys.path.insert(0, os.path.join(os.path.dirname(__file__), 'src'))
from src.models.model_factory import ModelFactory
from src.adaptive.adaptive_cot import AdaptiveCoT
from src.evaluation.evaluator import AdaptiveCoTEvaluator
from src.utils.research_logger import ResearchLogger
from src.utils.memory_monitor import MemoryMonitor
class ResearchFramework:
"""Research framework for adaptive parallel test-time scaling."""
def __init__(self, config_path: str = "research_config.yaml"):
"""Initialize the research framework."""
with open(config_path, 'r') as f:
self.config = yaml.safe_load(f)
# Setup logging
self.research_logger = ResearchLogger(
output_dir=self.config["research_logging"]["output_dir"]
)
# Setup memory monitoring
self.memory_monitor = MemoryMonitor(verbose=True)
# Create experiment directory
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
self.experiment_dir = Path(f"research_experiments/adaptive_cot_{timestamp}")
self.experiment_dir.mkdir(parents=True, exist_ok=True)
# Save config
with open(self.experiment_dir / "config.yaml", 'w') as f:
yaml.dump(self.config, f, default_flow_style=False)
print(f"π¬ Research Framework Initialized")
print(f"π Experiment directory: {self.experiment_dir}")
print(f"π Default static branches: {self.config['adaptive_branching']['default_branches']}")
def load_model(self, model_path: str, model_type: str = "deepseek") -> Any:
"""Load the specified model."""
print(f"π¦ Loading model: {model_path}")
model_config = self.config["models"]["deepseek_r1_distill_qwen"].copy()
model_config["model_name"] = model_path
model = ModelFactory.create_model(model_type, model_path, model_config)
model.load_model()
print(f"β
Model loaded successfully")
return model
def test_adaptive_vs_static(self, model: Any, problem: str) -> Dict[str, Any]:
"""Compare adaptive vs static branching on a single problem."""
print(f"\nπ Comparing Adaptive vs Static Branching")
print(f"Problem: {problem}")
print("=" * 60)
results = {}
# Test Adaptive Branching
print(f"\nπ Testing Adaptive Branching:")
print("-" * 40)
adaptive_config = self.config["adaptive_branching"].copy()
adaptive_config["enabled"] = True
cot_adaptive = AdaptiveCoT(model, adaptive_config)
start_time = time.time()
try:
adaptive_result = cot_adaptive.solve_problem(problem)
adaptive_result["execution_time"] = time.time() - start_time
results["adaptive"] = adaptive_result
print(f"β
Adaptive Result:")
print(f" Answer: {adaptive_result['final_answer']}")
print(f" Branches: {adaptive_result['num_branches']}")
print(f" Strategy: {adaptive_result['allocation_info']['strategy']}")
print(f" Time: {adaptive_result['execution_time']:.2f}s")
print(f" Consensus: {adaptive_result['consensus_info']['confidence']:.3f}")
if 'prefill_signals' in adaptive_result:
prefill = adaptive_result['prefill_signals']
print(f" Prefill: entropy={prefill['entropy']:.3f}, confidence={prefill['confidence']:.3f}")
except Exception as e:
print(f"β Adaptive failed: {e}")
results["adaptive"] = {"error": str(e)}
# Test Static Branching (8 branches default)
print(f"\nπ Testing Static Branching (8 branches):")
print("-" * 40)
static_config = {
"adaptive_branching": False,
"min_branches": 1,
"max_branches": 10,
"default_branches": 8,
"research_logging": False
}
cot_static = AdaptiveCoT(model, static_config)
start_time = time.time()
try:
static_result = cot_static.solve_problem(problem)
static_result["execution_time"] = time.time() - start_time
results["static"] = static_result
print(f"β
Static Result:")
print(f" Answer: {static_result['final_answer']}")
print(f" Branches: {static_result['num_branches']}")
print(f" Strategy: {static_result['allocation_info']['strategy']}")
print(f" Time: {static_result['execution_time']:.2f}s")
print(f" Consensus: {static_result['consensus_info']['confidence']:.3f}")
except Exception as e:
print(f"β Static failed: {e}")
results["static"] = {"error": str(e)}
# Compare results
print(f"\nπ Comparison Summary:")
print("-" * 40)
if "error" not in results.get("adaptive", {}) and "error" not in results.get("static", {}):
adaptive = results["adaptive"]
static = results["static"]
print(f" Answer Match: {adaptive['final_answer'] == static['final_answer']}")
print(f" Branch Efficiency: {static['num_branches'] - adaptive['num_branches']} fewer branches with adaptive")
print(f" Time Difference: {abs(adaptive['execution_time'] - static['execution_time']):.2f}s")
print(f" Consensus Quality: Adaptive={adaptive['consensus_info']['confidence']:.3f}, Static={static['consensus_info']['confidence']:.3f}")
# Calculate efficiency gain
if adaptive['num_branches'] < static['num_branches']:
efficiency_gain = (static['num_branches'] - adaptive['num_branches']) / static['num_branches'] * 100
print(f" π― Efficiency Gain: {efficiency_gain:.1f}% fewer branches with adaptive")
return results
def run_benchmark_evaluation(self, model: Any, dataset: str, max_samples: int = 100) -> Dict[str, Any]:
"""Run evaluation on a benchmark dataset."""
print(f"\nπ Running Benchmark Evaluation")
print(f"Dataset: {dataset}")
print(f"Max samples: {max_samples}")
print("=" * 60)
# Create evaluator
evaluator = AdaptiveCoTEvaluator(model, self.config)
# Run evaluation
results = evaluator.evaluate_dataset(
dataset_name=dataset,
max_samples=max_samples,
save_results=True
)
# Display results
print(f"\nπ Evaluation Results:")
print(f" Problems: {results['num_problems']}")
print(f" Successful: {results['num_successful']}")
print(f" Accuracy: {results['metrics']['accuracy']['exact_match']:.3f}")
print(f" Avg branches: {results['metrics']['efficiency']['average_branch_count']:.1f}")
print(f" Avg time: {results['metrics']['efficiency']['average_execution_time']:.2f}s")
return results
def run_strategy_comparison(self, model: Any, dataset: str, max_samples: int = 100) -> Dict[str, Any]:
"""Compare adaptive vs static strategies on a dataset."""
print(f"\nπ Running Strategy Comparison")
print(f"Dataset: {dataset}")
print(f"Max samples: {max_samples}")
print("=" * 60)
# Create evaluator
evaluator = AdaptiveCoTEvaluator(model, self.config)
# Compare strategies
strategies = ["adaptive", "static"]
results = evaluator.compare_strategies(
dataset_name=dataset,
strategies=strategies,
max_samples=max_samples
)
# Display comparison
print(f"\nπ Strategy Comparison Results:")
print("-" * 50)
comparison_data = []
for strategy, result in results.items():
if "error" not in result:
metrics = result.get("metrics", {})
accuracy = metrics.get("accuracy", {}).get("exact_match", 0.0)
avg_branches = metrics.get("efficiency", {}).get("average_branch_count", 0.0)
avg_time = metrics.get("efficiency", {}).get("average_execution_time", 0.0)
print(f" {strategy.upper()}:")
print(f" Accuracy: {accuracy:.3f}")
print(f" Avg Branches: {avg_branches:.1f}")
print(f" Avg Time: {avg_time:.2f}s")
comparison_data.append({
"strategy": strategy,
"accuracy": accuracy,
"avg_branches": avg_branches,
"avg_time": avg_time
})
else:
print(f" {strategy.upper()}: ERROR - {result['error']}")
# Calculate efficiency gains
if len(comparison_data) == 2:
adaptive = next((d for d in comparison_data if d["strategy"] == "adaptive"), None)
static = next((d for d in comparison_data if d["strategy"] == "static"), None)
if adaptive and static:
branch_efficiency = (static["avg_branches"] - adaptive["avg_branches"]) / static["avg_branches"] * 100
time_efficiency = (static["avg_time"] - adaptive["avg_time"]) / static["avg_time"] * 100
print(f"\nπ― Efficiency Analysis:")
print(f" Branch Efficiency: {branch_efficiency:.1f}% fewer branches with adaptive")
print(f" Time Efficiency: {time_efficiency:.1f}% time difference")
print(f" Accuracy Difference: {abs(adaptive['accuracy'] - static['accuracy']):.3f}")
# Determine if adaptive is more efficient
if branch_efficiency > 0 and abs(adaptive['accuracy'] - static['accuracy']) < 0.05:
print(f" β
Adaptive is more efficient while maintaining accuracy!")
elif branch_efficiency > 0:
print(f" β οΈ Adaptive uses fewer branches but accuracy differs")
else:
print(f" β Adaptive uses more branches than static")
return results
def save_results(self, results: Dict[str, Any], filename: str) -> None:
"""Save results to JSON file."""
filepath = self.experiment_dir / filename
with open(filepath, 'w') as f:
json.dump(results, f, indent=2, default=str)
print(f"πΎ Results saved to: {filepath}")
def main():
"""Main research framework function."""
parser = argparse.ArgumentParser(
description="Research Framework for Adaptive CoT with Parallel Test-Time Scaling",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
# Test single problem comparison
python research_framework.py --model-path "/path/to/model" --problem "What is 2+2?" --compare-single
# Run benchmark evaluation
python research_framework.py --model-path "/path/to/model" --benchmark gsm8k --max-samples 100
# Compare strategies on dataset
python research_framework.py --model-path "/path/to/model" --compare-dataset gsm8k --max-samples 100
"""
)
# Model configuration
parser.add_argument("--model-path", type=str, required=True, help="Path to model")
parser.add_argument("--model-type", type=str, default="deepseek", help="Model type")
parser.add_argument("--config", type=str, default="research_config.yaml", help="Config file")
# Experiment types
parser.add_argument("--problem", type=str, help="Single problem to test")
parser.add_argument("--compare-single", action="store_true", help="Compare adaptive vs static on single problem")
parser.add_argument("--benchmark", type=str, choices=["gsm8k", "aime", "olympiad", "math"],
help="Benchmark dataset to evaluate")
parser.add_argument("--compare-dataset", type=str, choices=["gsm8k", "aime", "olympiad", "math"],
help="Dataset to compare strategies on")
# Experiment parameters
parser.add_argument("--max-samples", type=int, default=100, help="Maximum samples")
parser.add_argument("--gpu-id", type=int, default=1, help="GPU ID to use")
args = parser.parse_args()
# Set GPU
os.environ["CUDA_VISIBLE_DEVICES"] = str(args.gpu_id)
try:
# Initialize framework
framework = ResearchFramework(args.config)
# Load model
model = framework.load_model(args.model_path, args.model_type)
# Run experiments
if args.problem and args.compare_single:
# Single problem comparison
results = framework.test_adaptive_vs_static(model, args.problem)
framework.save_results(results, "single_problem_comparison.json")
elif args.benchmark:
# Benchmark evaluation
results = framework.run_benchmark_evaluation(model, args.benchmark, args.max_samples)
framework.save_results(results, f"{args.benchmark}_evaluation.json")
elif args.compare_dataset:
# Strategy comparison
results = framework.run_strategy_comparison(model, args.compare_dataset, args.max_samples)
framework.save_results(results, f"{args.compare_dataset}_strategy_comparison.json")
else:
print("β Please specify --compare-single, --benchmark, or --compare-dataset")
return 1
print(f"\nβ
Research experiment completed successfully!")
print(f"π All results saved to: {framework.experiment_dir}")
return 0
except Exception as e:
print(f"β Research experiment failed: {e}")
import traceback
traceback.print_exc()
return 1
if __name__ == "__main__":
sys.exit(main())