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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
#!/usr/bin/env python3
import argparse
import importlib.util
import json
import math
import os
import sys
from pathlib import Path
from types import ModuleType
from typing import Any, Dict, List, Tuple

# Add resources to path for imports
HERE = Path(__file__).resolve().parent
RESOURCES_DIR = HERE / "resources"
sys.path.insert(0, str(RESOURCES_DIR))

import torch
import triton
import numpy as np

DEFAULT_SPEC = HERE / "resources" / "submission_spec.json"
ARTIFACT_PATH = Path("./output_ans").resolve()

DEVICE = triton.runtime.driver.active.get_active_torch_device()


def _determine_large_test_sizes() -> List[int]:
    """Return medium test size: 2^20 (1,048,576 elements).
    
    This size tests typical workloads.
    """
    return [2**20]


DEFAULT_SEED = 1337
NUM_VECTOR_SAMPLES = 5
GPU_WARMUP_ITERS = 10
INNER_ADD_WARMUP_ITERS = 5


def warmup_gpu(iters: int = GPU_WARMUP_ITERS) -> None:
    """Run a few trivial GPU ops to warm up kernels and clocks."""
    if not torch.cuda.is_available():
        return
    torch.cuda.synchronize()
    n = 1 << 20
    a = torch.rand(n, device=DEVICE, dtype=torch.float32)
    b = torch.rand(n, device=DEVICE, dtype=torch.float32)
    for _ in range(max(1, int(iters))):
        c = a + b
    torch.cuda.synchronize()


def load_solution_module(solution_path: Path) -> ModuleType:
    """Load the solution module from the given path."""
    if not solution_path.exists():
        raise FileNotFoundError(f"solution.py not found at {solution_path}")
    spec = importlib.util.spec_from_file_location("submitted_solution", solution_path)
    if spec is None or spec.loader is None:
        raise ImportError(f"Failed to load spec for {solution_path}")
    module = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(module)
    return module


def materialize_artifact(result: Any, solution_path: Path) -> Path:
    """Materialize the solution result into an artifact file."""
    ARTIFACT_PATH.parent.mkdir(parents=True, exist_ok=True)
    if isinstance(result, dict):
        with ARTIFACT_PATH.open("w", encoding="utf-8") as fout:
            json.dump(result, fout)
        return ARTIFACT_PATH
    if isinstance(result, str):
        # Check if the string could be a file path (reasonable length and no newlines)
        # before calling is_file() to avoid "File name too long" errors
        is_possible_path = len(result) < 4096 and '\n' not in result
        if is_possible_path:
            candidate = Path(result)
            try:
                if candidate.is_file():
                    with ARTIFACT_PATH.open("w", encoding="utf-8") as fout:
                        json.dump({"program_path": str(candidate.resolve())}, fout)
                    return ARTIFACT_PATH
            except OSError:
                # Path too long or other OS error - treat as code string
                pass
        # Treat as code string
        with ARTIFACT_PATH.open("w", encoding="utf-8") as fout:
            fout.write(result)
        return ARTIFACT_PATH
    raise TypeError(
        "Solution.solve() must return a dict/path-string/code-string; got "
        f"{type(result)!r}."
    )


def load_add_from_artifact(artifact_path: Path) -> Any:
    """Load the add function from the artifact."""
    with artifact_path.open("r", encoding="utf-8") as fin:
        artifact = json.load(fin)
    
    if "code" in artifact:
        # Write code to temporary file and import as module to avoid Triton source inspection issues
        import tempfile
        import os
        
        try:
            # Create temporary file
            with tempfile.NamedTemporaryFile(mode='w', suffix='.py', delete=False) as f:
                f.write(artifact["code"])
                temp_file = f.name
            
            # Import the module
            import importlib.util
            spec = importlib.util.spec_from_file_location("temp_add_module", temp_file)
            module = importlib.util.module_from_spec(spec)
            spec.loader.exec_module(module)
            
            if not hasattr(module, "add"):
                raise ValueError("Code must define an 'add' function")
            
            # Clean up temporary file
            os.unlink(temp_file)
            
            return module.add
        except Exception as e:
            # Clean up temporary file if it exists
            try:
                if 'temp_file' in locals():
                    os.unlink(temp_file)
            except:
                pass
            raise
    
    elif "program_path" in artifact:
        # Load from external file
        program_path = Path(artifact["program_path"])
        if not program_path.exists():
            raise FileNotFoundError(f"Program file not found: {program_path}")
        
        spec = importlib.util.spec_from_file_location("submitted_program", program_path)
        if spec is None or spec.loader is None:
            raise ImportError(f"Failed to load spec for {program_path}")
        module = importlib.util.module_from_spec(spec)
        spec.loader.exec_module(module)
        
        if not hasattr(module, "add"):
            raise ValueError("Program must define an 'add' function")
        return module.add
    
    else:
        raise ValueError("Artifact must contain either 'code' or 'program_path'")


def benchmark_add(add_func: Any, sizes: List[int], seed: int = DEFAULT_SEED, num_samples: int = NUM_VECTOR_SAMPLES) -> Dict[str, Any]:
    """Benchmark the add function against PyTorch baseline with seeding and averaging."""
    results = []
    
    # Warm up the GPU for more stable timings
    warmup_gpu(GPU_WARMUP_ITERS)
    
    for size in sizes:
        torch.manual_seed(seed)
        if torch.cuda.is_available():
            torch.cuda.manual_seed_all(seed)
        np.random.seed(seed)
        
        pytorch_ms_list = []
        cpu_ms_list = []
        custom_ms_list = []
        correctness_list = []
        
        for sample_idx in range(max(1, int(num_samples))):
            # Create test vectors deterministically
            x = torch.rand(size, device=DEVICE, dtype=torch.float32)
            y = torch.rand(size, device=DEVICE, dtype=torch.float32)
            # CPU baseline vectors
            x_cpu = x.detach().cpu()
            y_cpu = y.detach().cpu()
            
            # PyTorch baseline (GPU)
            def pytorch_add():
                return x + y
            # Inner warmup additions before timing
            if torch.cuda.is_available():
                for _ in range(INNER_ADD_WARMUP_ITERS):
                    _ = pytorch_add()
                torch.cuda.synchronize()
            pytorch_ms = triton.testing.do_bench(pytorch_add, quantiles=[0.5])
            if isinstance(pytorch_ms, (tuple, list)):
                pytorch_ms = pytorch_ms[0]
            pytorch_ms_list.append(float(pytorch_ms))
            
            # Naive CPU baseline
            def cpu_add():
                return x_cpu + y_cpu
            cpu_ms = triton.testing.do_bench(cpu_add, quantiles=[0.5])
            if isinstance(cpu_ms, (tuple, list)):
                cpu_ms = cpu_ms[0]
            cpu_ms_list.append(float(cpu_ms))
            
            # Custom implementation (GPU)
            def custom_add():
                return add_func(x, y)
            # Inner warmup additions before timing
            if torch.cuda.is_available():
                for _ in range(INNER_ADD_WARMUP_ITERS):
                    _ = custom_add()
                torch.cuda.synchronize()
            custom_ms = triton.testing.do_bench(custom_add, quantiles=[0.5])
            if isinstance(custom_ms, (tuple, list)):
                custom_ms = custom_ms[0]
            custom_ms_list.append(float(custom_ms))
            
            # Correctness test on this sample
            pytorch_result = pytorch_add()
            custom_result = custom_add()
            is_correct = torch.allclose(pytorch_result, custom_result, rtol=1e-5, atol=1e-8)
            correctness_list.append(bool(is_correct))
        
        # Aggregate timings as medians for stability
        def median(lst):
            s = sorted(lst)
            mid = len(s) // 2
            if len(s) % 2 == 1:
                return s[mid]
            return 0.5 * (s[mid - 1] + s[mid])
        
        pytorch_ms = median(pytorch_ms_list)
        cpu_ms = median(cpu_ms_list)
        custom_ms = median(custom_ms_list)
        
        # Bandwidths (GB/s)
        pytorch_bandwidth = 3 * size * 4 * 1e-9 / (pytorch_ms * 1e-3)
        cpu_bandwidth = 3 * size * 4 * 1e-9 / (cpu_ms * 1e-3)
        custom_bandwidth = 3 * size * 4 * 1e-9 / (custom_ms * 1e-3)
        
        is_correct = all(correctness_list)
        
        results.append({
            "size": size,
            "pytorch_ms": pytorch_ms,
            "cpu_ms": cpu_ms,
            "custom_ms": custom_ms,
            "pytorch_bandwidth": pytorch_bandwidth,
            "cpu_bandwidth": cpu_bandwidth,
            "custom_bandwidth": custom_bandwidth,
            "speedup": pytorch_ms / custom_ms if custom_ms > 0 else 0.0,
            "bandwidth_ratio": custom_bandwidth / cpu_bandwidth if cpu_bandwidth > 0 else 0.0,
            "is_correct": is_correct,
        })
    
    return results


def evaluate_vector_addition(add_func: Any) -> Dict[str, Any]:
    """Evaluate the performance of a vector addition implementation."""
    try:
        # Use large sizes based on GPU memory so GPU >> CPU
        sizes = _determine_large_test_sizes()
        
        # Run benchmark
        results = benchmark_add(add_func, sizes)
        
        # Enforce strict correctness: if any test fails, score 0
        if not results or not all(r["is_correct"] for r in results):
            return {
                "error": "Correctness not 100% across all samples/sizes",
                "score": 0,
                "pass_all": False,
                "total_tests": len(results),
                "passed_tests": sum(1 for r in results if r.get("is_correct")),
                "results": results,
            }
        
        # Calculate metrics
        bandwidth_ratios = [r["bandwidth_ratio"] for r in results if r["is_correct"]]
        speedups = [r["speedup"] for r in results if r["is_correct"]]
        pytorch_vs_cpu = [
            max(r["pytorch_bandwidth"] / max(r["cpu_bandwidth"], 1e-12), 1e-12)
            for r in results if r["is_correct"]
        ]
        custom_vs_cpu = [
            max(r["custom_bandwidth"] / max(r["cpu_bandwidth"], 1e-12), 1e-12)
            for r in results if r["is_correct"]
        ]
        
        if not bandwidth_ratios:
            return {
                "error": "All correctness tests failed",
                "score": 0,
                "pass_all": False,
            }
        
        geometric_mean_bandwidth_ratio = math.exp(sum(math.log(r) for r in bandwidth_ratios) / len(bandwidth_ratios))
        arithmetic_mean_bandwidth_ratio = sum(bandwidth_ratios) / len(bandwidth_ratios)
        gm_pytorch_vs_cpu = math.exp(sum(math.log(r) for r in pytorch_vs_cpu) / len(pytorch_vs_cpu))
        gm_custom_vs_cpu = math.exp(sum(math.log(r) for r in custom_vs_cpu) / len(custom_vs_cpu))
        
        # Calculate score (0-100 scale)
        # Anchor 0 at CPU naive baseline (custom/cpu = 1x)
        # Anchor 100 at 2x PyTorch GPU baseline (custom/cpu = 2 * pytorch/cpu)
        target = max(2.0 * gm_pytorch_vs_cpu, 1.0 + 1e-12)
        numerator = max(0.0, gm_custom_vs_cpu - 1.0)
        denominator = max(target - 1.0, 1e-12)
        normalized = max(0.0, min(1.0, numerator / denominator))
        score = normalized * 100.0
        
        return {
            "geometric_mean_bandwidth_ratio": geometric_mean_bandwidth_ratio,
            "arithmetic_mean_bandwidth_ratio": arithmetic_mean_bandwidth_ratio,
            "geometric_mean_custom_vs_cpu": gm_custom_vs_cpu,
            "geometric_mean_pytorch_vs_cpu": gm_pytorch_vs_cpu,
            "score": score,
            "pass_all": True,
            "total_tests": len(results),
            "passed_tests": sum(1 for r in results if r["is_correct"]),
            "results": results,
        }
        
    except Exception as e:
        return {
            "error": str(e),
            "score": 0,
            "pass_all": False,
        }


def evaluate(solution_path: Path, spec_path: Path) -> dict:
    """Main evaluation function."""
    try:
        # Load solution module
        module = load_solution_module(solution_path)
        
        if not hasattr(module, "Solution"):
            raise ValueError("Solution module must define a 'Solution' class")
        
        solution_class = module.Solution
        solution_instance = solution_class()
        
        if not hasattr(solution_instance, "solve"):
            raise ValueError("Solution class must have a 'solve' method")
        
        # Get solution result
        result = solution_instance.solve(spec_path)
        
        # Materialize artifact
        artifact_path = materialize_artifact(result, solution_path)
        
        # Load add function from artifact
        add_func = load_add_from_artifact(artifact_path)
        
        # Evaluate performance
        evaluation_result = evaluate_vector_addition(add_func)
        
        return {
            "status": "success",
            "artifact_path": str(artifact_path),
            **evaluation_result,
        }
        
    except Exception as e:
        return {
            "status": "error",
            "error": str(e),
            "score": 0,
        }


def main():
    parser = argparse.ArgumentParser(description="Evaluate vector addition solutions")
    parser.add_argument(
        "--solution-path",
        type=Path,
        default=Path("./solution.py"),
        help="Path to solution.py file",
    )
    parser.add_argument(
        "--spec-path",
        type=Path,
        default=DEFAULT_SPEC,
        help="Path to specification file",
    )
    parser.add_argument(
        "--output-path",
        type=Path,
        default=Path("./result.json"),
        help="Path to output result file",
    )
    
    args = parser.parse_args()
    
    # Run evaluation
    result = evaluate(args.solution_path, args.spec_path)
    
    # Write result
    with args.output_path.open("w", encoding="utf-8") as fout:
        json.dump(result, fout, indent=2)
    
    # Print summary
    if result["status"] == "success":
        print(f"Evaluation completed successfully!")
        print(f"Score: {result.get('score', 0):.2f}/100")
        if 'geometric_mean_bandwidth_ratio' in result:
            print(f"Geometric mean bandwidth ratio: {result['geometric_mean_bandwidth_ratio']:.3f}x")
        if 'passed_tests' in result and 'total_tests' in result:
            print(f"Tests passed: {result['passed_tests']}/{result['total_tests']}")
        if 'error' in result:
            print(f"Error: {result['error']}")
        # Print score as last line for main_loop.sh to extract
        print(result.get('score', 0))
    else:
        print(f"Evaluation failed: {result.get('error', 'Unknown error')}")
        # Print error score as last line
        print("0")
        sys.exit(1)


if __name__ == "__main__":
    main()