|
1 | | -"""Runs every benchmark of examples/core_ops_bench.rs and benchmarks/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, together with |
3 | | -the numerical error of the same operation against a float64 reference (from `accuracy_vs_pytorch.py`; "x better" = PyTorch's error |
4 | | -divided by oxideon's, so above 1 oxideon is more accurate). |
| 1 | +"""Speed and numerical error of Oxideon against PyTorch, for every precision mode. |
| 2 | +
|
| 3 | +Runs every benchmark of examples/core_ops_bench.rs and benchmarks/core_ops_bench_torch.py in its own process (with a pause in |
| 4 | +between, so power throttling of long-running processes does not distort later rows), and measures the numerical error of the same |
| 5 | +operations against a float64 reference (`accuracy_vs_pytorch.py`; "x better" = PyTorch's error divided by Oxideon's, so above 1 |
| 6 | +Oxideon is more accurate). |
| 7 | +
|
| 8 | +Oxideon has three precision modes (`oxideon.set_precision_mode`, or the `OXIDEON_PRECISION` environment variable): |
| 9 | +
|
| 10 | + fastest PyTorch-class accuracy at PyTorch-class speed (plain f32 accumulation, cheaper gelu) |
| 11 | + balanced the default: 1.5-2.4x more accurate than PyTorch on matrix products and convolutions, about the same speed |
| 12 | + highest f64 accumulation: 6-19x more accurate, 2-8x slower |
| 13 | +
|
| 14 | +Only matrix products, convolutions and gelu depend on the mode; they are run once per mode. Every other operation is identical in |
| 15 | +all three modes and is run once. |
5 | 16 |
|
6 | 17 | cargo build --release --example core_ops_bench --example accuracy_dump |
7 | | - python benchmarks/core_ops_bench_compare.py [--no-accuracy] [--markdown] |
| 18 | + python benchmarks/core_ops_bench_compare.py [--markdown] [--no-accuracy] [--modes fastest,balanced,highest] [--repeat N] |
| 19 | +
|
| 20 | +`--repeat N` measures every time N times, spread over the run, and keeps the best (a laptop that throttles after minutes of load |
| 21 | +would otherwise give one row a bad moment). |
8 | 22 |
|
9 | 23 | The accuracy case is the one named in the last column; its shape is not always the benchmark's. Operations whose result is one |
10 | 24 | correctly rounded IEEE operation (add, mul, relu, copies ...) are `exact` in both libraries. |
11 | 25 | """ |
| 26 | +import os |
12 | 27 | import re |
13 | 28 | import subprocess |
14 | 29 | import sys |
|
22 | 37 | EXE = ROOT / "target/release/examples/core_ops_bench" |
23 | 38 | NAMES = re.findall(r'bench\("([^"]+)"', (ROOT / "examples/core_ops_bench.rs").read_text(encoding="utf-8")) |
24 | 39 |
|
| 40 | +ALL_MODES = ["fastest", "balanced", "highest"] |
| 41 | +MARKDOWN = "--markdown" in sys.argv |
| 42 | +WITH_ACCURACY = "--no-accuracy" not in sys.argv |
| 43 | +MODES = ALL_MODES |
| 44 | +REPEAT = int(sys.argv[sys.argv.index("--repeat") + 1]) if "--repeat" in sys.argv else 1 |
| 45 | +if "--modes" in sys.argv: |
| 46 | + MODES = sys.argv[sys.argv.index("--modes") + 1].split(",") |
| 47 | + |
| 48 | +# benchmarks whose time depends on the precision mode (the others are measured once, in the default mode) |
| 49 | +MODE_DEPENDENT = {"gelu 1M", "matmul 512", "matmul 1024", "matmul 256x4096x256", "conv2d 8x16x64x64 k3", "conv2d 4x64x56x56 k3", "mlp train step b256"} |
| 50 | + |
25 | 51 | # benchmark -> case of accuracy_vs_pytorch.py measuring the same operation; "exact" = a single correctly rounded operation |
26 | 52 | ACCURACY = { |
27 | 53 | "add 1M": "exact", "mul 1M": "exact", "add broadcast row 1M": "exact", "mul_scalar 1M": "exact", "relu 1M": "exact", |
|
33 | 59 | } |
34 | 60 |
|
35 | 61 |
|
36 | | -def one(cmd, name): |
37 | | - out = subprocess.run(cmd + [name], capture_output=True, text=True).stdout |
| 62 | +def once(cmd, name, mode=None): |
| 63 | + env = dict(os.environ) |
| 64 | + if mode: |
| 65 | + env["OXIDEON_PRECISION"] = mode |
| 66 | + out = subprocess.run(cmd + [name], capture_output=True, text=True, env=env).stdout |
38 | 67 | m = re.search(r"^BENCH .{28}\s*([\d.]+)", out, re.M) |
39 | 68 | time.sleep(1.5) |
40 | 69 | return float(m.group(1)) if m else float("nan") |
41 | 70 |
|
42 | 71 |
|
43 | | -acc = {} |
44 | | -if "--no-accuracy" not in sys.argv: |
45 | | - import accuracy_vs_pytorch |
46 | | - |
47 | | - acc = accuracy_vs_pytorch.measure({c for c in ACCURACY.values() if c != "exact"}) |
| 72 | +def one(cmd, name, mode=None): |
| 73 | + """Best of `REPEAT` separate processes.""" |
| 74 | + return min(once(cmd, name, mode) for _ in range(REPEAT)) |
48 | 75 |
|
49 | 76 |
|
50 | 77 | def fx(v): |
51 | 78 | return "tie" if v != v else "inf" if v == float("inf") else f"{v:.2f}x" |
52 | 79 |
|
53 | 80 |
|
54 | | -def error_columns(name): |
| 81 | +# error[mode][case] = (x better worst, x better mean) |
| 82 | +error = {} |
| 83 | +if WITH_ACCURACY: |
| 84 | + import accuracy_vs_pytorch |
| 85 | + |
| 86 | + cases = {c for c in ACCURACY.values() if c != "exact"} |
| 87 | + dependent_cases = {ACCURACY[n] for n in MODE_DEPENDENT if ACCURACY.get(n, "exact") != "exact"} |
| 88 | + for mode in MODES: |
| 89 | + os.environ["OXIDEON_PRECISION"] = mode |
| 90 | + rows = accuracy_vs_pytorch.measure(dependent_cases if mode != "balanced" else cases) |
| 91 | + error[mode] = {c: (r[2], r[5]) for c, r in rows.items()} |
| 92 | + os.environ.pop("OXIDEON_PRECISION", None) |
| 93 | + if "balanced" not in error: # the independent operations are measured in the default mode |
| 94 | + os.environ["OXIDEON_PRECISION"] = "balanced" |
| 95 | + rows = accuracy_vs_pytorch.measure(cases) |
| 96 | + error["balanced"] = {c: (r[2], r[5]) for c, r in rows.items()} |
| 97 | + os.environ.pop("OXIDEON_PRECISION", None) |
| 98 | + |
| 99 | + |
| 100 | +def err(mode, name): |
| 101 | + """(worst, mean) 'x better' text for a benchmark in a mode.""" |
55 | 102 | case = ACCURACY.get(name) |
56 | 103 | if case is None: |
57 | | - return f"{'-':>9}{'-':>9} -" |
| 104 | + return "-", "-" |
58 | 105 | if case == "exact": |
59 | | - return f"{'exact':>9}{'exact':>9} both libraries exact" |
60 | | - if case not in acc: |
61 | | - return f"{'?':>9}{'?':>9} {case}" |
62 | | - _, _, bw, _, _, bm, _ = acc[case] |
63 | | - return f"{fx(bw):>9}{fx(bm):>9} {case}" |
| 106 | + return "exact", "exact" |
| 107 | + if not WITH_ACCURACY or case not in error.get(mode, {}): |
| 108 | + return "-", "-" |
| 109 | + w, m = error[mode][case] |
| 110 | + return fx(w), fx(m) |
64 | 111 |
|
65 | 112 |
|
66 | | -MARKDOWN = "--markdown" in sys.argv |
| 113 | +dependent = [n for n in NAMES if n in MODE_DEPENDENT] |
| 114 | +others = [n for n in NAMES if n not in MODE_DEPENDENT] |
| 115 | +torch_cmd = [sys.executable, str(ROOT / "benchmarks/core_ops_bench_torch.py")] |
| 116 | + |
| 117 | +# 1. the operations that depend on the mode: one run per mode |
| 118 | +t_dep = {} |
| 119 | +o_dep = {} |
| 120 | +for n in dependent: |
| 121 | + t_dep[n] = one(torch_cmd, n) |
| 122 | + for mode in MODES: |
| 123 | + o_dep[n, mode] = one([str(EXE)], n, mode) |
| 124 | +# 2. everything else, once, in the default mode |
| 125 | +o_oth, t_oth = {}, {} |
| 126 | +for n in others: |
| 127 | + o_oth[n] = one([str(EXE)], n, "balanced") |
| 128 | + t_oth[n] = one(torch_cmd, n) |
| 129 | + |
67 | 130 | if MARKDOWN: |
68 | | - print("| operation | Oxideon us | PyTorch us | ratio | error worst (x better) | error mean (x better) |") |
| 131 | + print("| operation | precision mode | Oxideon us | PyTorch us | time ratio | error worst (x better) | error mean (x better) |") |
| 132 | + print("|---|---|---:|---:|---:|---:|---:|") |
| 133 | + for n in dependent: |
| 134 | + for i, mode in enumerate(MODES): |
| 135 | + w, m = err(mode, n) |
| 136 | + name = n if i == 0 else "" |
| 137 | + print(f"| {name} | {mode}{' (default)' if mode == 'balanced' else ''} | {o_dep[n, mode]:.1f} | {t_dep[n]:.1f} | {o_dep[n, mode] / t_dep[n]:.2f} | {w} | {m} |") |
| 138 | + print() |
| 139 | + print("| operation (same in every mode) | Oxideon us | PyTorch us | time ratio | error worst (x better) | error mean (x better) |") |
69 | 140 | print("|---|---:|---:|---:|---:|---:|") |
| 141 | + for n in others: |
| 142 | + w, m = err("balanced", n) |
| 143 | + print(f"| {n} | {o_oth[n]:.1f} | {t_oth[n]:.1f} | {o_oth[n] / t_oth[n]:.2f} | {w} | {m} |") |
70 | 144 | else: |
71 | | - print(f"{'op':<28}{'oxideon us':>12}{'torch us':>12}{'ratio':>8}" + (f"{'err worst':>11}{'err mean':>9} accuracy case" if acc else "")) |
72 | | -for n in NAMES: |
73 | | - o = one([str(EXE)], n) |
74 | | - t = one([sys.executable, str(ROOT / "benchmarks/core_ops_bench_torch.py")], n) |
75 | | - if MARKDOWN: |
76 | | - case = ACCURACY.get(n) |
77 | | - if case == "exact": |
78 | | - w = m = "exact" |
79 | | - elif case in acc: |
80 | | - w, m = fx(acc[case][2]), fx(acc[case][5]) |
81 | | - else: |
82 | | - w = m = "-" |
83 | | - print(f"| {n} | {o:.1f} | {t:.1f} | {o / t:.2f} | {w} | {m} |", flush=True) |
84 | | - else: |
85 | | - print(f"{n:<28}{o:>12.1f}{t:>12.1f}{o / t:>8.2f}{error_columns(n) if acc else ''}", flush=True) |
| 145 | + print("operations that depend on the precision mode (us, ratio to PyTorch, error x better worst / mean)") |
| 146 | + head = f"{'op':<24}{'torch us':>10}" |
| 147 | + for mode in MODES: |
| 148 | + head += f"{mode + ' us':>14}{'ratio':>7}{'err w/m':>16}" |
| 149 | + print(head) |
| 150 | + for n in dependent: |
| 151 | + line = f"{n:<24}{t_dep[n]:>10.1f}" |
| 152 | + for mode in MODES: |
| 153 | + w, m = err(mode, n) |
| 154 | + line += f"{o_dep[n, mode]:>14.1f}{o_dep[n, mode] / t_dep[n]:>7.2f}{w + ' / ' + m:>16}" |
| 155 | + print(line) |
| 156 | + print("\noperations that are the same in every mode") |
| 157 | + print(f"{'op':<28}{'oxideon us':>12}{'torch us':>12}{'ratio':>8}{'err worst':>11}{'err mean':>9}") |
| 158 | + for n in others: |
| 159 | + w, m = err("balanced", n) |
| 160 | + print(f"{n:<28}{o_oth[n]:>12.1f}{t_oth[n]:>12.1f}{o_oth[n] / t_oth[n]:>8.2f}{w:>11}{m:>9}") |
0 commit comments