omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / pytorch / bench.py
| 1 | """Paired, interleaved wall-clock timing of one MLP forward pass: eager PyTorch versus backend="omega". |
| 2 | |
| 3 | Run (both arms in the same process, on the same pinned core, one thread each): |
| 4 | CUDA_VISIBLE_DEVICES= OMP_NUM_THREADS=1 OMEGA_OMC=<omega-mv seed/out/omc> \ |
| 5 | taskset -c 17 python3 pytorch/bench.py --rounds 30 --out timings.json |
| 6 | Writes raw per-call nanoseconds for both arms, /proc/loadavg before every sample and before and after |
| 7 | the run, and labels the run "taken under load" when any sample saw load1 above 8. It computes no |
| 8 | statistics: summarise the raw numbers separately (the receipt names the tool used). |
| 9 | """ |
| 10 | |
| 11 | import argparse |
| 12 | import json |
| 13 | import os |
| 14 | import platform |
| 15 | import sys |
| 16 | import time |
| 17 | |
| 18 | import torch |
| 19 | import torch.nn as nn |
| 20 | |
| 21 | sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) |
| 22 | import omega_torch as ot # noqa: E402 |
| 23 | |
| 24 | |
| 25 | class MLP(nn.Module): |
| 26 | def __init__(self, d=256, h=64): |
| 27 | super().__init__() |
| 28 | self.l1 = nn.Linear(d, d) |
| 29 | self.l2 = nn.Linear(d, h) |
| 30 | self.p = nn.Parameter(torch.randn(h, h) / 8.0) |
| 31 | |
| 32 | def forward(self, x): |
| 33 | y = torch.relu(self.l1(x)) + x |
| 34 | return torch.softmax(torch.matmul(self.l2(y), self.p), dim=-1) |
| 35 | |
| 36 | |
| 37 | def loadavg(): |
| 38 | with open("/proc/loadavg") as f: |
| 39 | return f.read().strip() |
| 40 | |
| 41 | |
| 42 | def main(): |
| 43 | ap = argparse.ArgumentParser() |
| 44 | ap.add_argument("--rounds", type=int, default=30) |
| 45 | ap.add_argument("--batch", type=int, default=64) |
| 46 | ap.add_argument("--out", required=True) |
| 47 | a = ap.parse_args() |
| 48 | threads = len(os.sched_getaffinity(0)) |
| 49 | torch.set_num_threads(threads) |
| 50 | rec = {"loadavg_before": loadavg(), "affinity": sorted(os.sched_getaffinity(0)), "torch_threads": threads, |
| 51 | "OMP_NUM_THREADS": os.environ.get("OMP_NUM_THREADS"), "torch": torch.__version__, |
| 52 | "python": platform.python_version(), "batch": a.batch, "rounds": a.rounds} |
| 53 | torch.manual_seed(0) |
| 54 | m = MLP().eval() |
| 55 | x = torch.randn(a.batch, 256) |
| 56 | with torch.no_grad(): |
| 57 | c = torch.compile(m, backend="omega", fullgraph=True, dynamic=False) |
| 58 | got, want = c(x), m(x) # compile and warm both arms |
| 59 | torch.testing.assert_close(got, want) |
| 60 | rec["parity_max_abs"] = (got - want).abs().max().item() |
| 61 | ot.reset_stats() |
| 62 | eager, omega, eager_load, omega_load = [], [], [], [] |
| 63 | for r in range(a.rounds): # interleaved, order alternating per round |
| 64 | arms = [(eager, eager_load, m), (omega, omega_load, c)] |
| 65 | if r % 2: |
| 66 | arms.reverse() |
| 67 | for sink, loads, f in arms: |
| 68 | loads.append(loadavg()) # /proc/loadavg read before each sample, outside the timed span |
| 69 | t0 = time.perf_counter_ns() |
| 70 | f(x) |
| 71 | sink.append(time.perf_counter_ns() - t0) |
| 72 | rec["stats"] = {k: v for k, v in ot.STATS.items()} |
| 73 | rec["eager_ns"], rec["omega_ns"] = eager, omega |
| 74 | rec["eager_loadavg"], rec["omega_loadavg"] = eager_load, omega_load |
| 75 | rec["loadavg_after"] = loadavg() |
| 76 | load1 = [float(s.split()[0]) for s in eager_load + omega_load] |
| 77 | rec["max_load1"] = max(load1) |
| 78 | rec["label"] = "taken under load (load1 > 8)" if max(load1) > 8 else "load1 <= 8 for every sample" |
| 79 | with open(a.out, "w") as f: |
| 80 | json.dump(rec, f, indent=1) |
| 81 | print(json.dumps({k: rec[k] for k in ("loadavg_before", "loadavg_after", "max_load1", "label", "affinity", |
| 82 | "torch_threads", "parity_max_abs", "stats")})) |
| 83 | |
| 84 | |
| 85 | if __name__ == "__main__": |
| 86 | main() |