File size: 2,814 Bytes
9454b45
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import torch
import time
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt
import os
import sys
import numpy as np

def run_benchmark():
    print("=" * 60)
    print(f"🚀 SYSTEM BENCHMARK on {os.uname().nodename}")
    print("=" * 60)
    
    print(f"Python: {sys.version.split()[0]}")
    print(f"PyTorch: {torch.__version__}")
    
    cuda_available = torch.cuda.is_available()
    if cuda_available:
        gpu_name = torch.cuda.get_device_name(0)
        print(f"✅ GPU DETECTED: {gpu_name}")
        print(f"   Memory: {torch.cuda.get_device_properties(0).total_memory / 1024**3:.2f} GB")
    else:
        print("⚠️  NO GPU DETECTED. Running on CPU.")
        print("   (To enable GPU, switch Hardware in HF Space settings)")
        
    print("-" * 60)
    
    times = {}
    
    # MATRIX SIZE
    N = 4000
    
    # CPU TEST
    print(f"1️⃣  CPU Test ({N}x{N} Matrix Mul)...")
    start_time = time.time()
    a_cpu = torch.randn(N, N)
    b_cpu = torch.randn(N, N)
    c_cpu = torch.matmul(a_cpu, b_cpu)
    cpu_time = time.time() - start_time
    times['CPU'] = cpu_time
    print(f"   ⏱️  Time: {cpu_time:.4f} seconds")
    
    # GPU TEST
    if cuda_available:
        print(f"2️⃣  GPU Test ({N}x{N} Matrix Mul)...")
        # Warmup
        torch.matmul(torch.randn(100,100).cuda(), torch.randn(100,100).cuda())
        
        start_time = time.time()
        a_gpu = torch.randn(N, N).cuda()
        b_gpu = torch.randn(N, N).cuda()
        # Synchronize for accurate timing
        torch.cuda.synchronize()
        start_computation = time.time()
        c_gpu = torch.matmul(a_gpu, b_gpu)
        torch.cuda.synchronize()
        gpu_time = time.time() - start_computation
        times['GPU'] = gpu_time
        print(f"   ⏱️  Time: {gpu_time:.4f} seconds")
        
        speedup = cpu_time / gpu_time
        print(f"   🚀 SPEEDUP: {speedup:.2f}x")
    else:
        print("2️⃣  GPU Test SKIPPED (No CUDA)")
        times['GPU'] = 0

    # PLOT
    print("-" * 60)
    print("Creating comparison chart...")
    plt.figure(figsize=(10, 6))
    
    models = list(times.keys())
    durations = list(times.values())
    colors = ['gray', 'green'] if cuda_available else ['gray', 'red']
    
    bars = plt.bar(models, durations, color=colors)
    plt.ylabel('Secons (Lower is better)')
    plt.title(f'Benchmark CPU vs GPU ({N}x{N} Matrix Mul)\n{gpu_name if cuda_available else "CPU Only"}')
    
    for bar in bars:
        yval = bar.get_height()
        if yval > 0:
            plt.text(bar.get_x() + bar.get_width()/2, yval, f'{yval:.4f}s', ha='center', va='bottom')
            
    filename = "gpu_benchmark.png"
    plt.savefig(filename)
    print(f"💾 Saved to {filename}")

if __name__ == "__main__":
    run_benchmark()