|
| 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