omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / seed / nki / README.md
| 1 | # seed/nki: Omega to AWS Trainium / Inferentia (NKI) |
| 2 | |
| 3 | `irnki.om` is a handwritten, seeded-Omega emitter. It reads an OSC IR v1 unit |
| 4 | (`spec/one-layer-format.md`) and writes an NKI kernel module in Python at the `nki.isa` |
| 5 | (NISA) level. NISA is the lowest level AWS documents publicly. The first kernel is |
| 6 | vecadd: `out[i] = a[i] + b[i]`. |
| 7 | |
| 8 | Status, 2026-10-26: |
| 9 | |
| 10 | | Item | Status | |
| 11 | |---|---| |
| 12 | | Emitter `irnki.om` | IMPLEMENTED | |
| 13 | | Kernels on the NKI CPU simulator, targets trn1, trn2, trn3 | EXERCISED-on-oracle (interop oracle, not Omega-native) | |
| 14 | | Trainium or Inferentia hardware | NOT_RUN | |
| 15 | |
| 16 | ## What it emits |
| 17 | |
| 18 | Each function becomes one `@nki.jit` kernel. The kernel loops over tiles of |
| 19 | 128 partitions x 512 elements (`nl.affine_range`) and keeps three explicit SBUF tiles per |
| 20 | step, `ta`, `tb` and `tc`. It moves data with `nisa.dma_copy` (HBM to SBUF and back) and |
| 21 | computes one `nisa.tensor_tensor(dst=tc, data1=, data2=, op=nl.<op>)`. The module ends |
| 22 | with `KERNELS = {name: (kernel, dtype)}`. |
| 23 | |
| 24 | The emitter lifts only this shape: |
| 25 | |
| 26 | - two parameters and a result, all u32 (tag 4), and three vregs; |
| 27 | - one block of two instructions: `BIN sub dst=2 a b` (a and b are params 0 or 1), then `RET 2`. |
| 28 | |
| 29 | The supported subcodes are add, sub, mul, and, or and xor, mapped to `nl.add`, |
| 30 | `subtract`, `multiply`, `bitwise_and`, `bitwise_or` and `bitwise_xor`. These are refused: |
| 31 | |
| 32 | | Input | Exit and message | |
| 33 | |---|---| |
| 34 | | div, rem, shl, shr | 24 UNSUPPORTED_OP. Omega traps on these and NKI has no trap. | |
| 35 | | Any tag other than u32 | 25 UNSUPPORTED_TAG | |
| 36 | | Any other function shape | 29 NOT_ELEMENTWISE | |
| 37 | | Bad bytes | 20–28, as in irwasm | |
| 38 | | Bad arguments | 30 USAGE | |
| 39 | |
| 40 | Every refusal happens before the first stdout byte. |
| 41 | |
| 42 | `irnki f32` retypes the lanes to float32 and then allows only add, sub and mul. This |
| 43 | retyping is explicit and documented. It exists because IR v1 cannot carry floats: the |
| 44 | compiler refuses float words in `ir` mode with error 16, and the float tags in spec |
| 45 | section 10.3 are not produced yet. The f32 vecadd is therefore the u32 unit |
| 46 | `vecadd.unit.om` with float32 lanes. The gate computes the f32 reference from the same |
| 47 | unit with `uint` read as `f32`. |
| 48 | |
| 49 | Shape limits: the kernels take 2-D arrays whose rows are a multiple of 128 and whose |
| 50 | columns are a multiple of 512. Any other shape is refused by an assertion, and the gate |
| 51 | checks that (128, 500) is refused. Three f32 tiles take 6144 bytes per partition, which |
| 52 | is 1/32 of the 192 KiB trn1 SBUF partition (224 KiB on trn2, 256 KiB on trn3). Computed |
| 53 | with WolframLanguageEvaluator: `3*512*4 -> 6144`, `3*512*4/(192*1024) -> 0.03125`. |
| 54 | |
| 55 | ## Run |
| 56 | |
| 57 | ```sh |
| 58 | seed/nki/setup-oracle.sh /tmp/nki-oracle # pinned downloads, user files only |
| 59 | NKI_ORACLE_DIR=/tmp/nki-oracle nice -n 19 seed/nki/gate-irnki.sh |
| 60 | ``` |
| 61 | |
| 62 | Exit codes: 0 PASS, 1 FAIL, 3 NOT_RUN (no oracle directory, the oracle does not start, |
| 63 | the oracle version differs from the pin, or the host is not AArch64). Artifacts go to |
| 64 | `build/seed/nki/`, and `gate.log` holds SOURCE, COMPILER, ORACLE and PLATFORM. |
| 65 | |
| 66 | The gate does the following: |
| 67 | |
| 68 | 1. Builds `irnki` with the pinned `seed/out/omc` (sha256 `2c824805…029f`). |
| 69 | 2. Lifts `vecadd.unit.om` (f32 and u32) and `ops.unit.om` (8 functions: every op, |
| 70 | swapped operands `sub b a`, and repeated operands `add a a`). Each module is emitted |
| 71 | twice and compared with `cmp`. |
| 72 | 3. Computes the expected lanes with a native AArch64 Omega program for each function, |
| 73 | built from the same unit source plus a driver. No foreign code computes expected |
| 74 | values. |
| 75 | 4. Runs every kernel in `nki.simulate` for trn1, trn2 and trn3 on 262144 lanes, shaped |
| 76 | (256, 1024), which is 4 tiles. The input set includes all pairs of edge values: 100 |
| 77 | for u32, and 196 for f32, covering ±0, ±inf, NaN, ±max, min-normal, subnormals and |
| 78 | 1+ulp. Results are compared bit for bit, except that two NaNs count as equal, because |
| 79 | NaN sign and payload are not part of the IEEE result. |
| 80 | 5. Negative controls, each of which must FAIL: |
| 81 | - an emitted `nl.add` edited to `nl.subtract`; |
| 82 | - the IR BIN subcode byte changed from add to sub before lifting; |
| 83 | - one expected bit flipped; |
| 84 | - a kernel dropped from `KERNELS`. |
| 85 | 6. Checks 14 refusals: exact exit code, the message on stderr and empty stdout. |
| 86 | |
| 87 | Measured 2026-10-26 on the DGX Spark: PASS, with 30 of 30 kernel/target runs equal and |
| 88 | every control rejected. The same result came from a directory freshly prepared by |
| 89 | `setup-oracle.sh`. |
| 90 | |
| 91 | ## Oracle stack (interop oracle, not Omega-native; never generates kernels) |
| 92 | |
| 93 | The nki wheel is published for x86_64 only, so on AArch64 it runs as x86_64 CPython |
| 94 | under qemu-user. Every item is a host dependency of the oracle only. |
| 95 | |
| 96 | | Item | Version | Licence | sha256 | |
| 97 | |---|---|---|---| |
| 98 | | nki (from `pip.repos.neuron.amazonaws.com`) | 0.7.0+37713544100.gfede04bb, cp312 linux_x86_64 | Apache-2.0 | `03543bed…0c4e` | |
| 99 | | numpy | 2.2.6 | BSD-3-Clause | `fd83c012…6249` | |
| 100 | | ml_dtypes | 0.5.3 | Apache-2.0 | `1b255aca…9e55` | |
| 101 | | typing_extensions | 4.15.0 | PSF-2.0 | `f0fa19c6…e548` | |
| 102 | | CPython (python-build-standalone 20261026) | 3.12.16 x86_64 | PSF-2.0 | `6a79e922…9de` | |
| 103 | | qemu-user (Debian trixie) | 10.0.13+ds-0+deb13u1 arm64 | GPL-2.0 | `c73711af…158d` | |
| 104 | | libc6, libgcc-s1, libstdc++6, zlib1g (Ubuntu noble amd64) | see `setup-oracle.sh` | LGPL-2.1 / GPL-3 with runtime exception / zlib | see `setup-oracle.sh` | |
| 105 | |
| 106 | `setup-oracle.sh` holds the full pins. The host qemu 8.2.2 crashes (internal SIGSEGV) |
| 107 | when Python reads `/proc/self/maps`, which is why the Debian 10.0.13 static build is used. |
| 108 | |
| 109 | `oracle.py` is the only foreign code. It writes the deterministic inputs, runs the |
| 110 | emitted kernels and compares bits. It never writes a kernel or an expected value. |
| 111 | There is no Omega replacement yet: it needs an Omega reader for the NKI simulator, or |
| 112 | an Omega NISA interpreter. |
| 113 | |
| 114 | ## How open the stack is (primary sources, read 2026-10-26) |
| 115 | |
| 116 | - The NKI programming interface (`nki.language`, `nki.isa`) and the CPU simulator ship in |
| 117 | the `nki` wheel under Apache-2.0. Wheels exist for x86_64 only. |
| 118 | https://awsdocs-neuron.readthedocs-hosted.com/en/latest/nki/get-started/about/index.html, |
| 119 | https://awsdocs-neuron.readthedocs-hosted.com/en/latest/nki/guides/nki_simulator.html |
| 120 | - The NKI compiler (MLIR based) is announced as open source but is a private beta. The |
| 121 | wheel's project URL `github.com/aws-neuron/private-nki-staging` is not public, and |
| 122 | `github.com/aws-neuron/nki` returns 404. The NKI IR and its MLIR dialect ship only as |
| 123 | compiled binaries and are not documented. |
| 124 | https://aws.amazon.com/about-aws/whats-new/2025/12/announcement-aws-neuron-2-27/, |
| 125 | https://awsdocs-neuron.readthedocs-hosted.com/en/latest/nki/deep-dives/nki-compiler.html |
| 126 | - The lowest documented level is therefore `nki.isa`, one call per engine instruction. |
| 127 | Below it, NEFF and the engine binary encodings are not public. |
| 128 | - `neuronx-cc` 2.28.7631.0 (wheel METADATA and LICENSE.txt, read without installing) is |
| 129 | under the "AWS Neuron Software License Agreement". That agreement grants use only "in |
| 130 | connection with AWS Services" and requires an AWS account in good standing. The gate |
| 131 | does not use it. |
| 132 | - nki-samples is MIT-0; nki-library and nkipy are Apache-2.0; aws-neuron-driver is |
| 133 | GPL-2.0. https://awsdocs-neuron.readthedocs-hosted.com/en/latest/about-neuron/oss/index.html |
| 134 | - Architecture figures (SBUF 24/28/32 MiB = 128 partitions of 192/224/256 KiB; PSUM |
| 135 | 2 MiB) come from |
| 136 | https://awsdocs-neuron.readthedocs-hosted.com/en/latest/nki/guides/architecture/trainium_inferentia2_arch.html, |
| 137 | `trainium2_arch.html` and `trainium3_arch.html`. |
| 138 | |
| 139 | ## What is NOT established |
| 140 | |
| 141 | - No hardware run. The simulator is AWS's functional model; it is not the chip. Engine |
| 142 | choice, timing, subnormal flushing and NaN payloads on Trainium/Inferentia are not |
| 143 | measured. The simulator runs with its default `NKI_PRECISE_FP=1`. |
| 144 | - Kernels are not compiled by `neuronx-cc`, and no NEFF exists. |
| 145 | - Only single-op elementwise u32 units are covered, plus the f32 retyping. There are no |
| 146 | reductions, no matmul (TensorE/PSUM), no broadcasting and no ragged shapes. |
| 147 | |
| 148 | ## Path to hardware (NOT_RUN) |
| 149 | |
| 150 | 1. An AWS account and a Neuron instance: inf2.xlarge or trn1.2xlarge (NeuronCore-v2), |
| 151 | trn2 (v3) or trn3 (v4), with the Neuron DLAMI or the driver, runtime and `neuronx-cc` |
| 152 | from the Neuron setup guide. |
| 153 | https://awsdocs-neuron.readthedocs-hosted.com/en/latest/setup/index.html |
| 154 | 2. Run the same emitted module with `@nki.jit` on NumPy arrays (it compiles through |
| 155 | `neuronx-cc` and runs on the NeuronDevice), and compare the lanes with the same |
| 156 | native Omega references used here. |
| 157 | 3. Record the instance type, Neuron SDK versions and the NEFF sha256 in a receipt. Only |
| 158 | that receipt can move the status past EXERCISED-on-oracle. |
| 159 | |
| 160 | The next kernel is matmul: `nisa.nc_matmul` with a 128x128 stationary tile and a 128x512 |
| 161 | moving tile accumulating in PSUM, then `nisa.tensor_copy` PSUM to SBUF and `dma_copy` out, |
| 162 | checked on the same simulator against a native Omega reference. |