import torch
import time
from matmul_torchcode import Model, get_inputs, get_init_inputs
from matmul_cudacode import ModelNew

def run_benchmark():
    if not torch.cuda.is_available():
        print("CUDA is not available")
        return
    
    device = torch.device("cuda")
    
    # Prepare input data
    inputs = [x.cuda(device=device) for x in get_inputs()]
    init_inputs = [x.cuda(device=device) if isinstance(x, torch.Tensor) else x for x in get_init_inputs()]

    # Initialize models
    torch_model = Model(*init_inputs).cuda()
    cuda_model = ModelNew(*init_inputs).cuda()

    torch_model.eval()
    cuda_model.eval()

    print("-------------------- Precision Check --------------------")
    with torch.no_grad():
        # Warm-up GPU
        _ = torch_model(*inputs)
        _ = cuda_model(*inputs)
        
        # Formal test
        output_torch = torch_model(*inputs)
        output_cuda = cuda_model(*inputs)

    # Precision validation
    abs_diff = torch.abs(output_torch - output_cuda)
    max_diff = torch.max(abs_diff).item()
    mean_diff = torch.mean(abs_diff).item()
    
    if max_diff < 1e-4 and mean_diff < 1e-5:
        print(f"✅ Precision aligned: max error {max_diff:.6f}, mean error {mean_diff:.6f}")
        precision_flag = True
    else:
        print(f"❌ Precision mismatch: max error {max_diff:.6f}, mean error {mean_diff:.6f}")
        precision_flag = False

    print("\n-------------------- Performance Speedup Test --------------------")
    num_iterations = 100 
    
    # Warm-up GPU
    for _ in range(10):
        _ = torch_model(*inputs)
        _ = cuda_model(*inputs)
    
    # PyTorch model timing
    torch.cuda.synchronize()
    start_time = time.time()
    for _ in range(num_iterations):
        _ = torch_model(*inputs)
    torch.cuda.synchronize()
    torch_time = (time.time() - start_time) / num_iterations
    
    # Custom CUDA kernel timing
    torch.cuda.synchronize()
    start_time = time.time()
    for _ in range(num_iterations):
        _ = cuda_model(*inputs)
    torch.cuda.synchronize()
    cuda_time = (time.time() - start_time) / num_iterations
    
    print(f"PyTorch built-in MatMul average execution time: {torch_time:.6f}s")
    print(f"Custom CUDA MatMul average execution time: {cuda_time:.6f}s")
    speedup = torch_time / cuda_time if cuda_time > 0 else 0
    print(f"Speedup: {speedup:.2f}x")
    
    return precision_flag, speedup

if __name__ == "__main__":
    precision_flag, speedup = run_benchmark()