omega-bridge
Omega-first language interoperability: checked conversion backends and native language bindings
Brought by drakestapleton
omega-bridge / docs / OMEGA-TORCH.md
| 1 | # Omega under PyTorch: `torch.compile(model, backend="omega")` (milestone 1) |
| 2 | |
| 3 | Status: IMPLEMENTED and EXERCISED on CPU only (AArch64, DGX Spark host), oracle = PyTorch eager, |
| 4 | Omega-native codegen, Python glue. Not qualified, not merged, not a claim that Omega runs PyTorch in |
| 5 | general: it runs the core-ATen ops listed below, float32, static shapes, inference only. |
| 6 | |
| 7 | ## What runs where |
| 8 | |
| 9 | | Layer | Implementation | Status | |
| 10 | |---|---|---| |
| 11 | | Kernels (`pytorch/kernels/*.om`) | seeded Omega, compiled by omega-mv `seed/out/omc` in function mode to AArch64 words | first-party | |
| 12 | | Binding (`src/native.om`) | Omega program that writes the C trampoline of native call ABI v1 (`docs/NATIVE-ABI.md`) | first-party; the C it writes is compiled by the host `cc` | |
| 13 | | Backend glue (`pytorch/omega_torch/__init__.py`) | Python: registers the backend, walks the FX graph, allocates outputs, calls units through `ctypes` | interim migration step | |
| 14 | |
| 15 | The glue computes no tensor value. Every value in an output was written by Omega machine code; |
| 16 | the glue only reads shapes, picks a kernel, supplies the shape constants as a `const NAME ulong N` |
| 17 | header in front of the fixed kernel text, and passes buffer addresses. |
| 18 | |
| 19 | ## Ops |
| 20 | |
| 21 | Covered (core ATen after `core_aten_decompositions()`): `aten.mm`, `aten.addmm` (1-D bias, beta = alpha |
| 22 | = 1), `aten.permute` (2-D, dims `[1, 0]`), `aten.add.Tensor` (equal shapes, alpha 1), `aten.relu`, |
| 23 | `aten._softmax` (2-D, last dim, `half_to_float` false). `addmm(b, x, permute(W, [1, 0]))` whose |
| 24 | permute has no other user is lowered to the `linear` kernel, which reads `W` in place. |
| 25 | |
| 26 | Refused at compile time, with every refused node listed and nothing run: any other op, broadcasting, |
| 27 | non-float32 dtypes, non-CPU devices, symbolic shapes (`dynamic=False` is required), empty tensors, |
| 28 | tensors above the Omega array limit of 16,777,216 elements (OMEGESE 9.4), softmax rows above the 4 MiB |
| 29 | local-array limit, non-contiguous inputs (at run time), autograd (run under `torch.no_grad()`), and a |
| 30 | visible CUDA device. Eager fallback exists only for ops named in `OMEGA_TORCH_FALLBACK` (or |
| 31 | `omega_torch.set_fallback`), and every such call is counted in `STATS["eager_fallback_calls"]`. |
| 32 | |
| 33 | Numerics: each dot product and softmax sum is accumulated in f64 and rounded once to f32. `exp` is |
| 34 | written in Omega (`expd` in `softmax.om`): range reduction by ln 2, degree-13 Taylor polynomial |
| 35 | (truncation below 5.9e-18 relative), exponent bits for 2^k. |
| 36 | |
| 37 | ## Use |
| 38 | |
| 39 | ```sh |
| 40 | export CUDA_VISIBLE_DEVICES= # TorchDynamo touches CUDA while tracing; milestone 1 is CPU only |
| 41 | export OMEGA_OMC=/path/to/omega-mv/seed/out/omc # sha256 2c82480596bbdc50cda9626866209ef662d5834f278f839ff4ce0250c495029f |
| 42 | python3 -m unittest -v pytorch/tests/test_omega_torch.py |
| 43 | ``` |
| 44 | |
| 45 | ```python |
| 46 | import sys; sys.path.insert(0, "pytorch") |
| 47 | import omega_torch # registers backend "omega" |
| 48 | with torch.no_grad(): |
| 49 | y = torch.compile(model, backend="omega", fullgraph=True, dynamic=False)(x) |
| 50 | ``` |
| 51 | |
| 52 | Built units are cached under `build/torch/<sha256 of unit source, compiler pin and native.om>`. |
| 53 | |
| 54 | ## Host dependencies |
| 55 | |
| 56 | | Dependency | Version used | What it does here | Omega successor | |
| 57 | |---|---|---|---| |
| 58 | | PyTorch | 2.14.0+cu130 | TorchDynamo capture, AOTAutograd core-ATen decomposition, tensor allocation, the eager oracle | the oracle stays; capture moves behind a PrivateUse1 `omega` device whose dispatch is an Omega unit | |
| 59 | | CPython | 3.12.3 | runs the glue and `ctypes` | the glue's graph walk and dispatch move into an Omega unit called through the same C ABI | |
| 60 | | `cc` (GCC) | system | compiles the C trampoline `src/native.om` writes | an Omega-emitted ELF shared object (the trampoline is a fixed instruction sequence) | |
| 61 | |
| 62 | ## Successor |
| 63 | |
| 64 | The Python glue is interim. The successor is a PrivateUse1 out-of-tree device `omega` |
| 65 | (`torch.utils.rename_privateuse1_backend("omega")`) whose operator registrations are a fixed C ABI |
| 66 | table generated by the bridge, with lowering, shape specialisation and dispatch done by an Omega unit. |
| 67 | That keeps the PyTorch-required registration as the only foreign code and lets eager ops and |
| 68 | `torch.compile` graphs share one Omega dispatcher. Later targets (GB10 SASS, SPIR-V, StableHLO, |
| 69 | Tenstorrent, NKI) plug in at the kernel layer, unchanged above it. GPU runs are NOT_RUN: they need an |
| 70 | authorized window. |