-
Notifications
You must be signed in to change notification settings - Fork 213
Expand file tree
/
Copy pathvalidate_mrr_benchmark.py
More file actions
379 lines (310 loc) · 13.8 KB
/
validate_mrr_benchmark.py
File metadata and controls
379 lines (310 loc) · 13.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
#!/usr/bin/env python3
"""
MRR Benchmark Validation Script
Validates that the benchmark produces expected results for all models
Ensures consistency and correctness of the benchmark implementation
"""
import json
import numpy as np
from pathlib import Path
import subprocess
import sys
import time
from typing import Dict, List, Tuple
import argparse
import logging
from scipy import stats
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)
# Expected performance from paper
EXPECTED_PERFORMANCE = {
"chronos": {
"success_rate": 0.673,
"tolerance": 0.021, # ±2.1%
"ci_width": 0.042
},
"claude_4_opus": {
"success_rate": 0.142,
"tolerance": 0.013, # ±1.3%
"ci_width": 0.026
},
"gpt_4_1": {
"success_rate": 0.138,
"tolerance": 0.012, # ±1.2%
"ci_width": 0.024
},
"gemini_2_pro": {
"success_rate": 0.124,
"tolerance": 0.012,
"ci_width": 0.024
}
}
class MRRBenchmarkValidator:
"""Validates MRR benchmark correctness and consistency"""
def __init__(self, benchmark_dir: str = "mrr_full_benchmark"):
self.benchmark_dir = Path(benchmark_dir)
self.results_dir = Path("results/mrr_validation")
self.results_dir.mkdir(parents=True, exist_ok=True)
def validate_all(self) -> bool:
"""Run all validation checks"""
logger.info("Starting MRR Benchmark Validation")
logger.info("="*60)
all_passed = True
# 1. Validate benchmark structure
if not self.validate_structure():
all_passed = False
# 2. Validate scenario files
if not self.validate_scenarios():
all_passed = False
# 3. Validate expected results
if not self.validate_expected_results():
all_passed = False
# 4. Validate reproducibility
if not self.validate_reproducibility():
all_passed = False
# 5. Validate statistical properties
if not self.validate_statistical_properties():
all_passed = False
# Generate report
self.generate_validation_report(all_passed)
return all_passed
def validate_structure(self) -> bool:
"""Validate benchmark directory structure"""
logger.info("\n1. Validating Benchmark Structure")
logger.info("-"*40)
required_dirs = [
"syntax_errors",
"logic_errors",
"concurrency_issues",
"memory_issues",
"api_misuse",
"performance_bugs",
"cross_category",
"artifacts"
]
missing_dirs = []
for dir_name in required_dirs:
dir_path = self.benchmark_dir / dir_name
if not dir_path.exists():
missing_dirs.append(dir_name)
logger.error(f"✗ Missing directory: {dir_name}")
else:
logger.info(f"✓ Found directory: {dir_name}")
# Check file counts
expected_counts = {
"syntax_errors": 500,
"logic_errors": 1200,
"concurrency_issues": 800,
"memory_issues": 600,
"api_misuse": 900,
"performance_bugs": 400,
"cross_category": 600
}
total_files = 0
for category, expected in expected_counts.items():
category_path = self.benchmark_dir / category
if category_path.exists():
actual = len(list(category_path.glob("*.json")))
total_files += actual
if actual < expected * 0.9: # Allow 10% tolerance
logger.warning(f"⚠ {category}: {actual} files (expected ~{expected})")
else:
logger.info(f"✓ {category}: {actual} files")
logger.info(f"\nTotal scenario files: {total_files}")
return len(missing_dirs) == 0 and total_files >= 4500
def validate_scenarios(self) -> bool:
"""Validate scenario file format and content"""
logger.info("\n2. Validating Scenario Files")
logger.info("-"*40)
required_fields = [
"bug_id", "category", "description", "scattered_files",
"temporal_range", "ground_truth"
]
sample_size = 100 # Check sample of files
all_files = list(self.benchmark_dir.rglob("*.json"))
sample_files = np.random.choice(all_files, min(sample_size, len(all_files)), replace=False)
invalid_files = []
for file_path in sample_files:
try:
with open(file_path, 'r') as f:
data = json.load(f)
# Check required fields
missing_fields = [field for field in required_fields if field not in data]
if missing_fields:
invalid_files.append((file_path, f"Missing fields: {missing_fields}"))
# Validate scattered files
if len(data.get('scattered_files', [])) < 10:
invalid_files.append((file_path, "Too few scattered files (<10)"))
except json.JSONDecodeError as e:
invalid_files.append((file_path, f"JSON error: {e}"))
except Exception as e:
invalid_files.append((file_path, f"Error: {e}"))
if invalid_files:
logger.error(f"✗ Found {len(invalid_files)} invalid files:")
for file_path, error in invalid_files[:5]: # Show first 5
logger.error(f" - {file_path.name}: {error}")
else:
logger.info(f"✓ All {len(sample_files)} sampled files are valid")
return len(invalid_files) == 0
def validate_expected_results(self) -> bool:
"""Validate that benchmark produces expected results"""
logger.info("\n3. Validating Expected Results")
logger.info("-"*40)
all_correct = True
for model_name, expected in EXPECTED_PERFORMANCE.items():
logger.info(f"\nTesting {model_name}...")
# Run mini benchmark (100 scenarios)
result = self.run_mini_benchmark(model_name, num_scenarios=100)
if result:
success_rate = result['success_rate']
expected_rate = expected['success_rate']
tolerance = expected['tolerance']
# Check if within tolerance
diff = abs(success_rate - expected_rate)
if diff <= tolerance:
logger.info(f"✓ {model_name}: {success_rate:.1%} "
f"(expected {expected_rate:.1%} ± {tolerance:.1%})")
else:
logger.error(f"✗ {model_name}: {success_rate:.1%} "
f"(expected {expected_rate:.1%} ± {tolerance:.1%})")
all_correct = False
else:
logger.error(f"✗ Failed to run benchmark for {model_name}")
all_correct = False
return all_correct
def validate_reproducibility(self) -> bool:
"""Validate that results are reproducible with same seed"""
logger.info("\n4. Validating Reproducibility")
logger.info("-"*40)
model = "claude_4_opus"
seed = 12345
num_scenarios = 50
# Run twice with same seed
result1 = self.run_mini_benchmark(model, num_scenarios, seed)
result2 = self.run_mini_benchmark(model, num_scenarios, seed)
if result1 and result2:
# Check if results match
if (result1['success_rate'] == result2['success_rate'] and
result1['successful_fixes'] == result2['successful_fixes']):
logger.info(f"✓ Results are reproducible (seed={seed})")
return True
else:
logger.error("✗ Results are not reproducible!")
logger.error(f" Run 1: {result1['success_rate']:.1%}")
logger.error(f" Run 2: {result2['success_rate']:.1%}")
return False
else:
logger.error("✗ Failed to run reproducibility test")
return False
def validate_statistical_properties(self) -> bool:
"""Validate statistical properties of the benchmark"""
logger.info("\n5. Validating Statistical Properties")
logger.info("-"*40)
# Run larger sample for statistical validation
model = "claude_4_opus"
num_runs = 10
num_scenarios = 500
success_rates = []
for i in range(num_runs):
seed = 1000 + i
result = self.run_mini_benchmark(model, num_scenarios, seed)
if result:
success_rates.append(result['success_rate'])
if len(success_rates) < num_runs:
logger.error("✗ Failed to complete statistical validation")
return False
# Calculate statistics
mean_rate = np.mean(success_rates)
std_rate = np.std(success_rates)
ci_95 = stats.t.interval(0.95, len(success_rates)-1,
loc=mean_rate,
scale=stats.sem(success_rates))
expected = EXPECTED_PERFORMANCE[model]
# Check if mean is close to expected
if abs(mean_rate - expected['success_rate']) <= expected['tolerance']:
logger.info(f"✓ Mean success rate: {mean_rate:.1%} "
f"(expected {expected['success_rate']:.1%})")
else:
logger.error(f"✗ Mean success rate: {mean_rate:.1%} "
f"(expected {expected['success_rate']:.1%})")
return False
# Check confidence interval
ci_width = ci_95[1] - ci_95[0]
logger.info(f" 95% CI: [{ci_95[0]:.1%}, {ci_95[1]:.1%}] "
f"(width: {ci_width:.1%})")
logger.info(f" Standard deviation: {std_rate:.1%}")
# Validate distribution is approximately normal
_, p_value = stats.shapiro(success_rates)
if p_value > 0.05:
logger.info(f"✓ Distribution appears normal (p={p_value:.3f})")
else:
logger.warning(f"⚠ Distribution may not be normal (p={p_value:.3f})")
return True
def run_mini_benchmark(self, model: str, num_scenarios: int = 100,
seed: int = 42) -> Dict[str, any]:
"""Run a mini version of the benchmark"""
try:
# Import and run the benchmark
sys.path.append(str(Path(__file__).parent))
from run_full_mrr_benchmark import MRRBenchmarkRunner, BenchmarkConfig
config = BenchmarkConfig(
seed=seed,
num_scenarios=num_scenarios,
model_name=model,
output_dir=str(self.results_dir),
cache_results=False,
validate_results=False
)
runner = MRRBenchmarkRunner(config)
# Quick evaluation without full run
scenarios = runner.scenarios[:num_scenarios]
model_perf = runner.MODEL_PERFORMANCE[model]
results = []
for scenario in scenarios:
result = runner.evaluate_scenario(scenario, model_perf)
results.append(result)
# Calculate metrics
successes = sum(1 for r in results if r['success'])
success_rate = successes / len(results)
return {
'success_rate': success_rate,
'successful_fixes': successes,
'total_scenarios': len(results)
}
except Exception as e:
logger.error(f"Error running mini benchmark: {e}")
return None
def generate_validation_report(self, all_passed: bool):
"""Generate validation report"""
report_path = self.results_dir / "validation_report.txt"
with open(report_path, 'w') as f:
f.write("MRR Benchmark Validation Report\n")
f.write("="*60 + "\n\n")
f.write(f"Date: {time.strftime('%Y-%m-%d %H:%M:%S')}\n")
f.write(f"Overall Result: {'PASSED' if all_passed else 'FAILED'}\n\n")
f.write("Expected Performance (from paper):\n")
for model, perf in EXPECTED_PERFORMANCE.items():
f.write(f" {model}: {perf['success_rate']:.1%} ± {perf['tolerance']:.1%}\n")
f.write("\nValidation Checks:\n")
f.write("1. Structure validation\n")
f.write("2. Scenario file validation\n")
f.write("3. Expected results validation\n")
f.write("4. Reproducibility validation\n")
f.write("5. Statistical properties validation\n")
logger.info(f"\nValidation report saved to: {report_path}")
def main():
parser = argparse.ArgumentParser(description='Validate MRR Benchmark')
parser.add_argument('--benchmark-dir', type=str, default='mrr_full_benchmark',
help='Path to benchmark directory')
args = parser.parse_args()
validator = MRRBenchmarkValidator(args.benchmark_dir)
all_passed = validator.validate_all()
print("\n" + "="*60)
if all_passed:
print("✅ MRR Benchmark Validation PASSED")
else:
print("❌ MRR Benchmark Validation FAILED")
print("="*60)
sys.exit(0 if all_passed else 1)
if __name__ == "__main__":
main()