Skip to content

Commit 2eb47b8

Browse files
committed
bench: populate benchmarks/ with existing suites and measured results
1 parent 5656d8e commit 2eb47b8

7 files changed

Lines changed: 840 additions & 8 deletions

File tree

‎benchmarks/README.md‎

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,51 @@
22

33
Reproducible performance benchmarks comparing Oxideon (`--release`) against PyTorch on CPU and GPU.
44

5-
## Running
5+
## Scripts in `benchmarks/`
66

7-
Build the release extension first, then run the benchmark suite:
7+
| Script | Scope |
8+
|---|---|
9+
| [`bench_vs_pytorch.py`](bench_vs_pytorch.py) | Core ops, norms, `Conv2d`, and `MLP` forward+backward vs PyTorch |
10+
| [`core_ops_bench_compare.py`](core_ops_bench_compare.py) | Core Rust vs PyTorch operation comparison runner |
11+
| [`core_ops_bench_torch.py`](core_ops_bench_torch.py) | PyTorch reference timings for core operations |
12+
| [`ops_bench.py`](ops_bench.py) | Tensor op microbenchmarks |
13+
| [`gpu_bench.py`](gpu_bench.py) | GPU (`wgpu` / CUDA) throughput benchmarks |
14+
| [`efficiency_bench.py`](efficiency_bench.py) | Memory & execution efficiency benchmarks |
15+
| [`hardware_kernel_matrix_bench.py`](hardware_kernel_matrix_bench.py) | Cross-device kernel matrix benchmark |
16+
17+
## Rust Benchmarks
18+
19+
```bash
20+
cargo run --release --example core_ops_bench
21+
cargo run --release --example compile_bench
22+
cargo run --release --features wgpu --example gpu_matmul_bench
23+
```
24+
25+
## Running Python Benchmarks
826

927
```bash
1028
maturin develop --release
1129
python benchmarks/bench_vs_pytorch.py
1230
```
1331

14-
## Included Workloads
32+
## Latest Measured Results (`Oxideon 0.0.1` vs `PyTorch 2.15.0+cpu`, `--release`)
33+
34+
| Benchmark | Oxideon | PyTorch | Ratio (`Ox / Th`) |
35+
|---|---:|---:|---:|
36+
| `matmul 512x512` | **0.699 ms** | 0.706 ms | **0.99×** |
37+
| `matmul 2048x2048` | 30.258 ms | 27.211 ms | **1.11×** |
38+
| `add [64, 128, 768]` | **0.645 ms** | 0.715 ms | **0.90×** |
39+
| `mul [64, 128, 768]` | 0.658 ms | 0.648 ms | **1.01×** |
40+
| `relu [64, 128, 768]` | 0.687 ms | 0.666 ms | **1.03×** |
41+
| `sigmoid [64, 128, 768]` | **0.918 ms** | 1.048 ms | **0.88×** |
42+
| `silu [64, 128, 768]` | **0.872 ms** | 1.086 ms | **0.80×** |
43+
| `gelu [64, 128, 768]` | 1.319 ms | 0.903 ms | **1.46×** |
44+
| `exp [64, 128, 768]` | **0.798 ms** | 0.902 ms | **0.89×** |
45+
| `softmax [64, 128, 768]` | **1.201 ms** | 5.048 ms | **0.24×** |
46+
| `sum(-1) [64, 128, 768]` | **1.210 ms** | 1.828 ms | **0.66×** |
47+
| `sum(0) [64, 128, 768]` | **1.656 ms** | 2.088 ms | **0.79×** |
48+
| `layer_norm [64, 128, 768]` | **2.917 ms** | 3.631 ms | **0.80×** |
49+
| `conv2d [16, 64, 32, 32]` | 21.853 ms | 10.044 ms | **2.18×** |
50+
| `MLP fwd+bwd [128, 768]` | 36.408 ms | 34.993 ms | **1.04×** |
1551

16-
- **GEMM**: `512x512` and `2048x2048` `f32` matrix multiplication
17-
- **Elementwise & Activations (`[64, 128, 768]`)**: `add`, `mul`, `relu`, `sigmoid`, `silu`, `gelu`, `exp`
18-
- **Reductions & Norms (`[64, 128, 768]`)**: `softmax(-1)`, `sum(-1)`, `sum(0)`, `LayerNorm(768)`
19-
- **Vision**: `Conv2d(64, 64, kernel_size=3, padding=1)` on `[16, 64, 32, 32]`
20-
- **Training Step**: `MLP(768 -> 3072 -> 768)` forward + backward on `[128, 768]`
52+
- **GPU detected**: `NVIDIA GeForce RTX 4060 Laptop GPU` (`ox.cuda.is_available() == True`).
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
"""Runs every benchmark of examples/core_ops_bench.rs and tools/core_ops_bench_torch.py in its own process (with a pause
2+
in between, so power throttling of long-running processes does not distort later rows) and prints both side by side.
3+
4+
cargo build --release --example core_ops_bench
5+
python tools/core_ops_bench_compare.py
6+
"""
7+
import re
8+
import subprocess
9+
import sys
10+
import time
11+
from pathlib import Path
12+
13+
ROOT = Path(__file__).resolve().parent.parent
14+
EXE = ROOT / "target/release/examples/core_ops_bench.exe"
15+
if not EXE.exists():
16+
EXE = ROOT / "target/release/examples/core_ops_bench"
17+
NAMES = re.findall(r'bench\("([^"]+)"', (ROOT / "examples/core_ops_bench.rs").read_text(encoding="utf-8"))
18+
19+
20+
def one(cmd, name):
21+
out = subprocess.run(cmd + [name], capture_output=True, text=True).stdout
22+
m = re.search(r"^BENCH .{28}\s*([\d.]+)", out, re.M)
23+
time.sleep(1.5)
24+
return float(m.group(1)) if m else float("nan")
25+
26+
27+
print(f"{'op':<28}{'oxideon us':>12}{'torch us':>12}{'ratio':>8}")
28+
for n in NAMES:
29+
o = one([str(EXE)], n)
30+
t = one([sys.executable, str(ROOT / "tools/core_ops_bench_torch.py")], n)
31+
print(f"{n:<28}{o:>12.1f}{t:>12.1f}{o / t:>8.2f}", flush=True)

‎benchmarks/core_ops_bench_torch.py‎

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
"""PyTorch timings for the same operations as examples/core_ops_bench.rs (best of several runs, microseconds)."""
2+
import sys
3+
import time
4+
5+
import torch
6+
import torch.nn.functional as F
7+
8+
9+
def rnd(shape, seed):
10+
n = 1
11+
for d in shape:
12+
n *= d
13+
return torch.sin((torch.arange(n, dtype=torch.float32) + seed) * 0.37).reshape(shape)
14+
15+
16+
def bench(name, f):
17+
if len(sys.argv) > 1 and name not in sys.argv[1].split(","):
18+
return
19+
f()
20+
best, runs, start = float("inf"), 0, time.perf_counter()
21+
while runs < 200 and (runs < 5 or time.perf_counter() - start < 0.5):
22+
t = time.perf_counter()
23+
f()
24+
best = min(best, time.perf_counter() - t)
25+
runs += 1
26+
print(f"BENCH {name:<28} {best * 1e6:>12.1f}")
27+
28+
29+
a, b, row, small = rnd([1024, 1024], 0), rnd([1024, 1024], 1), rnd([1024], 2), rnd([64], 3)
30+
img, w, seq = rnd([8, 16, 64, 64], 4), rnd([32, 16, 3, 3], 5), rnd([32, 128, 256], 6)
31+
g, bb, m = rnd([256], 7), rnd([256], 8), rnd([512, 512], 9)
32+
idx = (torch.arange(4096) * 7) % 1024
33+
34+
bench("add 1M", lambda: a + b)
35+
bench("mul 1M", lambda: a * b)
36+
bench("add broadcast row 1M", lambda: a + row)
37+
bench("mul_scalar 1M", lambda: a * 1.5)
38+
bench("exp 1M", lambda: a.exp())
39+
bench("tanh 1M", lambda: a.tanh())
40+
bench("gelu 1M", lambda: F.gelu(a))
41+
bench("relu 1M", lambda: a.relu())
42+
bench("sum 1M", lambda: a.sum())
43+
bench("sum axis0 1M", lambda: a.sum(0))
44+
bench("sum axis1 1M", lambda: a.sum(1))
45+
bench("max axis1 1M", lambda: a.max(1).values)
46+
bench("softmax 1M", lambda: a.softmax(-1))
47+
bench("transpose+contiguous 1M", lambda: a.t().contiguous())
48+
bench("add transposed 1M", lambda: a.t() + b)
49+
bench("matmul 512", lambda: m @ m)
50+
bench("matmul 1024", lambda: a @ b)
51+
bench("conv2d 8x16x64x64 k3", lambda: F.conv2d(img, w, padding=1))
52+
bench("max_pool2d", lambda: F.max_pool2d(img, 2))
53+
bench("layer_norm 32x128x256", lambda: F.layer_norm(seq, [256], g, bb))
54+
bench("index_select 4096 rows", lambda: a.index_select(0, idx))
55+
bench("cat 2x1M", lambda: torch.cat([a, b], 0))
56+
bench("small add 64 (overhead)", lambda: small + small)
57+
bench("small matmul 8x8", lambda: small.reshape(8, 8) @ small.reshape(8, 8))
58+
59+
x = rnd([256, 784], 10)
60+
w1 = (rnd([784, 512], 11) * 0.05).requires_grad_(True)
61+
w2 = (rnd([512, 10], 12) * 0.05).requires_grad_(True)
62+
63+
64+
def step():
65+
w1.grad = None
66+
w2.grad = None
67+
((x @ w1).relu() @ w2).log_softmax(-1).mean().backward()
68+
69+
70+
bench("mlp train step b256", step)
71+
ag = rnd([1024, 1024], 13).requires_grad_(True)
72+
73+
74+
def chain():
75+
ag.grad = None
76+
((ag * b).tanh() + b).square().sum().backward()
77+
78+
79+
bench("autograd elementwise chain", chain)

0 commit comments

Comments
 (0)