|
| 1 | +"""Reproducible Oxideon vs PyTorch CPU & GPU benchmark suite.""" |
| 2 | + |
| 3 | +import time |
| 4 | +import oxideon as ox |
| 5 | + |
| 6 | +try: |
| 7 | + import torch |
| 8 | +except ImportError: |
| 9 | + torch = None |
| 10 | + |
| 11 | + |
| 12 | +def bench(fn, iters: int = 25) -> float: |
| 13 | + fn() |
| 14 | + t0 = time.perf_counter() |
| 15 | + for _ in range(iters): |
| 16 | + fn() |
| 17 | + return (time.perf_counter() - t0) / iters * 1000.0 |
| 18 | + |
| 19 | + |
| 20 | +def main() -> None: |
| 21 | + if torch is None: |
| 22 | + print("PyTorch not installed; running Oxideon-only benchmarks.") |
| 23 | + |
| 24 | + cases = [] |
| 25 | + |
| 26 | + # GEMM |
| 27 | + a_ox, b_ox = ox.randn([512, 512]), ox.randn([512, 512]) |
| 28 | + A_ox, B_ox = ox.randn([2048, 2048]), ox.randn([2048, 2048]) |
| 29 | + x_ox = ox.randn([64, 128, 768]) |
| 30 | + ln_ox = ox.nn.LayerNorm(768) |
| 31 | + img_ox = ox.randn([16, 64, 32, 32]) |
| 32 | + c_ox = ox.nn.Conv2d(64, 64, kernel_size=3, padding=1) |
| 33 | + m_ox = ox.nn.Sequential(ox.nn.Linear(768, 3072), ox.nn.GELU(), ox.nn.Linear(3072, 768)) |
| 34 | + inp_ox = ox.randn([128, 768]) |
| 35 | + |
| 36 | + def step_ox() -> None: |
| 37 | + for p in m_ox.parameters(): |
| 38 | + p.grad = None |
| 39 | + y = m_ox(inp_ox).sum() |
| 40 | + y.backward() |
| 41 | + |
| 42 | + if torch is not None: |
| 43 | + a_th, b_th = torch.randn(512, 512), torch.randn(512, 512) |
| 44 | + A_th, B_th = torch.randn(2048, 2048), torch.randn(2048, 2048) |
| 45 | + x_th = torch.randn(64, 128, 768) |
| 46 | + ln_th = torch.nn.LayerNorm(768) |
| 47 | + img_th = torch.randn(16, 64, 32, 32) |
| 48 | + c_th = torch.nn.Conv2d(64, 64, kernel_size=3, padding=1) |
| 49 | + m_th = torch.nn.Sequential(torch.nn.Linear(768, 3072), torch.nn.GELU(), torch.nn.Linear(3072, 768)) |
| 50 | + inp_th = torch.randn(128, 768) |
| 51 | + |
| 52 | + def step_th() -> None: |
| 53 | + m_th.zero_grad(set_to_none=True) |
| 54 | + y = m_th(inp_th).sum() |
| 55 | + y.backward() |
| 56 | + |
| 57 | + cases = [ |
| 58 | + ("matmul 512x512", lambda: a_ox @ b_ox, lambda: a_th @ b_th, 30), |
| 59 | + ("matmul 2048x2048", lambda: A_ox @ B_ox, lambda: A_th @ B_th, 5), |
| 60 | + ("add [64,128,768]", lambda: x_ox + x_ox, lambda: x_th + x_th, 25), |
| 61 | + ("mul [64,128,768]", lambda: x_ox * x_ox, lambda: x_th * x_th, 25), |
| 62 | + ("relu [64,128,768]", lambda: x_ox.relu(), lambda: torch.relu(x_th), 25), |
| 63 | + ("sigmoid [64,128,768]", lambda: x_ox.sigmoid(), lambda: torch.sigmoid(x_th), 25), |
| 64 | + ("silu [64,128,768]", lambda: x_ox.silu(), lambda: torch.nn.functional.silu(x_th), 25), |
| 65 | + ("gelu [64,128,768]", lambda: x_ox.gelu(), lambda: torch.nn.functional.gelu(x_th), 25), |
| 66 | + ("exp [64,128,768]", lambda: x_ox.exp(), lambda: x_th.exp(), 25), |
| 67 | + ("softmax [64,128,768]", lambda: x_ox.softmax(-1), lambda: x_th.softmax(-1), 25), |
| 68 | + ("sum(-1) [64,128,768]", lambda: x_ox.sum(dim=-1), lambda: x_th.sum(dim=-1), 25), |
| 69 | + ("sum(0) [64,128,768]", lambda: x_ox.sum(dim=0), lambda: x_th.sum(dim=0), 25), |
| 70 | + ("layer_norm [64,128,768]", lambda: ln_ox(x_ox), lambda: ln_th(x_th), 25), |
| 71 | + ("conv2d [16,64,32,32]", lambda: c_ox(img_ox), lambda: c_th(img_th), 15), |
| 72 | + ("MLP fwd+bwd [128,768]", step_ox, step_th, 15), |
| 73 | + ] |
| 74 | + print(f"{'Benchmark':28s} | {'Oxideon':>9s} | {'PyTorch':>9s} | {'Ox/Th':>7s}") |
| 75 | + print("-" * 62) |
| 76 | + for name, f_ox, f_th, it in cases: |
| 77 | + t_ox = bench(f_ox, it) |
| 78 | + t_th = bench(f_th, it) |
| 79 | + print(f"{name:28s} | {t_ox:7.3f}ms | {t_th:7.3f}ms | {t_ox / t_th:6.2f}x") |
| 80 | + else: |
| 81 | + for name, fn, it in [ |
| 82 | + ("matmul 512x512", lambda: a_ox @ b_ox, 30), |
| 83 | + ("matmul 2048x2048", lambda: A_ox @ B_ox, 5), |
| 84 | + ("softmax [64,128,768]", lambda: x_ox.softmax(-1), 25), |
| 85 | + ("layer_norm [64,128,768]", lambda: ln_ox(x_ox), 25), |
| 86 | + ("conv2d [16,64,32,32]", lambda: c_ox(img_ox), 15), |
| 87 | + ("MLP fwd+bwd [128,768]", step_ox, 15), |
| 88 | + ]: |
| 89 | + print(f"{name:28s} | {bench(fn, it):7.3f}ms") |
| 90 | + |
| 91 | + print("\n=== GPU Info ===") |
| 92 | + print("ox.cuda.is_available():", ox.cuda.is_available()) |
| 93 | + if ox.cuda.is_available(): |
| 94 | + print("CUDA device 0:", ox.cuda.get_device_name(0)) |
| 95 | + |
| 96 | + |
| 97 | +if __name__ == "__main__": |
| 98 | + main() |
0 commit comments