interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / om / gpumath / README.md

259 lines · 19050 bytes Raw Blame History Rendered
1# om/gpumath: host math, FP8 packing and message formatting in seeded Omega
2
3The original parity tables, mutant counts, timings, binary size and binding checks below
4are historical [#751](https://github.com/aien-dev/omega-mv/pull/751) host evidence
5under compiler `734f0a39…6b61`, not measurements under the current compiler pin.
6The later #848/#994 compile reports and #857 host parity report are recorded separately below.
7
8`gpumath.om` ports three C files of `libomega_gpu.a` to seeded Omega (aien-dev/omega-mv#726,
9aien-dev/aien-architecture-mv#300/#316):
10
11| C | what it does | Omega functions |
12|---|---|---|
13| `src/omega_gpu_hostmath.c` | libm-free double `log`, `exp`, `pow`, `sincos`; `pow2f` | `hmlog` `hmexp` `hmpow` `hmsincos` `hmpow2f` |
14| `src/omega_gpu_fp8_pack.c` | e4m3 decode, kernel bits, NaN test and scan; bf16 scale and x packing; the f64 GEMV reference | `e4m3ref` `e4m3kbits` `e4m3isnan` `f8scannan` `f8packscales` `f8packxf32` `f8packxbf16` `f8gemvblock` |
15| `src/omega_gpu_fmt.c` | the bounded `printf`-style formatter the session and tensor APIs use for error stages | `gpufmt` `gpufmtkinds` |
16
17The task description for this port spoke of bf16/e5m2 conversions and a format descriptor table. The
18files contain neither. There is no e5m2 code, and `omega_gpu_fmt` is a formatter (`%s %c %d %u %x`, the
19`l`/`ll`/`z` modifiers, `%%`). This port follows the files.
20
21The C is unchanged. This change adds the port and its parity evidence. Removing the C objects from
22`libomega_gpu.a` goes through the omega-bridge native call ABI (omega-bridge #8) and is not part of it.
23
24## How the port matches the C bit for bit
25
26The contract is the C as the product builds it: the Makefile CFLAGS (`-std=gnu11 -O2`, gcc 13.3.0,
27aarch64). The oracle objects built from these sources have the same `.text` bytes as a `make` build in
28another worktree (`omega-blake3-abi/build`, same source sha256s). In detail:
29
30- **Same IEEE operations, same order.** Where the C computes in `float` or `double`, the port uses the
31 same single Omega float operation (`f32`/`f64`; omc emits one FP instruction per operator and never
32 writes FPCR). It does not substitute integer code that "should" agree. Operand order follows the
33 compiled object, not the C source, because it decides which NaN payload survives. gcc emits
34 `fmul d0, x, ref` in the GEMV reference (`x` first) and `fmul log(b), e` in `pow`, and the port does
35 the same.
36- **Fused multiply-add.** `gnu11` implies `-ffp-contract=fast`, so gcc fused nine `a*b+c` in hostmath into
37 FMADD/FMSUB: the `log` series step `s*t2 + 1/k`, its two final steps, the two `exp` reduction steps and
38 the four `sincos` reduction steps. Omega has no fused operation, so `fma64` computes the fused result
39 exactly in integer arithmetic. It forms the 106-bit product in a 128-bit frame and aligns the addend
40 with a sticky bit. It rounds once to nearest even, with subnormal results rounded directly at
41 2^-1074 and overflow going to infinity. It follows ARM's FPProcessNaNs3 order (addend, then the
42 operands) and gives the default NaN for a quiet-NaN addend with ∞·0. FMSUB is `fma64` with the
43 multiplicand's sign flipped, as the ARM pseudocode does. The `fma` stream checks `fma64` against the
44 hardware `fmadd` instruction.
45- **Integer bit patterns** where the C works on integers: `pow2f`, `pow2i`, the e4m3 kernel bits and NaN
46 test, the scan, and the whole formatter.
47
48## ABI (bindable by omega-bridge native call ABI v1)
49
50Every exported function has at most 6 parameters, each `ulong`/`long` or a fixed-length `view`/`edit` array of
51`ulong`/`uchar`, and returns `ulong`, `long` or nothing. A double or float travels as its bit pattern, and
52arrays hold little-endian element bytes exactly as the C arrays hold them in memory. ABI v1 arrays have a fixed length, so the variable-length C
53functions become pieces that an adapter loops over, the way `om/sha256` and `om/blake3` do:
54
55| C | Omega | piece |
56|---|---|---|
57| `omega_fp8_pack_scan_nan(w, count, &first)` | `f8scannan w(view uchar 4096) n base st(edit ulong 2)` | 4096 bytes; `st` = count, first (written only while the count is 0) |
58| `omega_fp8_pack_scales(bf16, count, out, nofold)` | `f8packscales in(8192 B) n out(16384 B) nofold → long` | 4096 elements; 0 or -1 after writing the earlier outputs |
59| `omega_fp8_pack_x_f32` / `_x_bf16` | `f8packxf32` / `f8packxbf16 in n out` | 4096 elements |
60| `omega_fp8_gemv_oracle(w, s, n, k, x, y, yscale)` | `f8gemvblock w(128 B) x(512 B) sb acc(edit ulong 2)` | one 128-column block of one row; acc/mag start at 0 per row, blocks in order |
61| `omega_gpu_fmt(out, cap, fmt, ...)` | `gpufmt out(1024 B) cap fmt(1024 B) args(16 ulong) pool(4096 B) → ulong` and `gpufmtkinds fmt kinds(64 ulong) → ulong` | see below |
62
63Varargs cannot cross the ABI. Each argument a conversion consumes is one 64-bit word in `args`. An
64`int`-sized kind uses the low 32 bits, which is what AAPCS64 `va_arg(ap, int)` reads, and `%c` uses the
65low 8 bits. A `%s` argument is an offset into `pool`, where the string is NUL-terminated;
66`2^64-1` stands for NULL and prints `(null)`. `gpufmtkinds` lists the `va_arg` type of each consuming
67conversion: 1 int, 2 long, 3 long long, 4 ptrdiff_t, 5 unsigned, 6 unsigned long, 7 unsigned long long,
688 size_t, 9 const char *. With that list a C adapter only calls `va_arg` with the listed types and
69never parses the format string. Both functions use the same scanner (`fscan`).
70
71`parity/unit.py gpumath.om NAME` cuts the function-mode unit for `NAME`: the function and every library
72function it calls, with `NAME` last. All 15 units compile with the pinned omc in function mode. The pinned
73generator (`src/native.om` of omega-bridge `012860db`, sha256 `2c2c53f6…3940`) accepts them, and the C it
74writes compiles with `-Wall -Wextra -Werror`, exporting `omega_hmlog` … `omega_gpufmtkinds`. In a
75separate check (a /tmp driver, not in the tree), every corpus stream except the internal `fma` stream was
76run through those generated bindings. The output was byte-identical to the C oracle's.
77
78## Parity
79
80To run the parity suite from a fresh clone (no C oracle needed; the oracle's output is recorded as sha256 in
81`parity/expected.txt`):
82
83```
84OMEGA_OMC=/path/to/seed/out/omc [OMEGA_BRIDGE=/path/to/omega-bridge] sh om/gpumath/parity/run.sh
85```
86
87The runner's `OMC_PIN` is the active reproduction input. Updating it does not
88relabel the historical results below or establish that the suite was rerun.
89Keep historical compiler identities fixed on future reseeds; add a separately
90dated result for a new run.
91
92### Historical #751 measurements
93
94`parity/corpus.py` regenerates the corpus deterministically (splitmix64, fixed seeds, about 48 MB, never
95committed). The run takes about 12 s on the Spark; the .om harness needs 2.5 s for all streams, the -O2 C
960.5 s.
97
98Oracle: the three C files above at omega-mv `acb3f15c` (sha256 `4ee994d7…f1eb` hostmath.c, `10ae3b45…0d4b`
99hostmath.h, `881daeaf…fe79` fp8_pack.c, `795e9620…1efa` fmt.c, `dd9f13c1…2ba8` fmt.h, `ddebec31…b668`
100fp8_api.h, `7e3c1779…105a` codegen_moe.h) compiled with
101`gcc -std=gnu11 -Wall -Wextra -Werror -D_GNU_SOURCE -O2 -Isrc`. `omega_bf16_to_fp32` is the same text cut
102from `src/omega_blackwell_matmul.c` and built with the same flags. `.text` sha256: hostmath
103`4061ecf7…6e2c`, fp8_pack `1cd43d3f…7cc3`, fmt `d06b83b7…92b7`. A /tmp driver feeds the corpus to
104these objects. `fma` records use the hardware `fmadd` (inline asm), and the fmt records are typed C
105calls that `corpus.py --c-cases` generates. Compiler: omc sha256 `26540e6d…8991`; `gpumath.om` at this
106commit compiles to a 29,272-byte program, the same bytes as under the previous pins `3ad9f10f…3194`, `b4f45121…9b55`, `be8c5130…448a`, `24caacf4…abb7`, `90c8884c…3983` and `469532a5…79bc`.
107Historical compiler SHA-256: `734f0a39319fbfa930bcc62cb30dbf06d47d78e126372b74f5baf478e6126b61`.
108At [implementation `b7825c174f80905e46f2a5057ac6628bae68f192`](https://github.com/aien-dev/omega-mv/blob/b7825c174f80905e46f2a5057ac6628bae68f192/om/gpumath/README.md),
109`gpumath.om` SHA-256 `b17b6ef0597de79455f1fd94c24dae1e4d55944f18f8efd31028b330c918dcde`
110was recorded as compiling to a 29,836-byte program under that compiler.
111
112| stream | function(s) | inputs | result |
113|---|---|---|---|
114| fma | `fma64` (internal, vs hardware FMADD) | 326,561 triples: every pair of 27 special doubles with every third special as addend (`D_SPECIAL[::3]`, 9 addends, 6,561 triples), class mix, near-cancellation, exact ties, sticky-only ties, subnormal and overflow products, hostmath shapes | 326,561/326,561 identical |
115| log | `hmlog` | 226,442: specials, every power of two 2^-1074..2^1023, 100,000 random bit patterns, 100,000 positive finite, near 1, around √2, 2^e(1+tiny) | 226,442/226,442 |
116| exp | `hmexp` | 221,309: specials, 100,000 random bits, 100,000 in [-750, 712], ±200 ulps around both cutoffs, around k·ln2 for k = -1080..1024 | 221,309/221,309 |
117| pow | `hmpow` | 100,097: RoPE θ^(±2i/d) for 7 θ × 5 d, random base/exponent, random bit pairs, special×special | 100,097/100,097 |
118| sincos | `hmsincos` | 340,027: specials, 100,000 random bits, 100,000 with abs(a) < 2^20·π/2, 100,000 RoPE angles (pos < 65536), 30,000 within 4 ulps of nπ/2, small | 340,027/340,027 (sin and cos) |
119| pow2f | `hmpow2f` | 12,006: every e in [-1000, 1000], the int extremes, 10,000 random ints | 12,006/12,006 |
120| e4m3 | `e4m3ref` `e4m3kbits` `e4m3isnan` | all 256 bytes | 256/256 (×3 outputs) |
121| scales | `f8packscales` | **all 65,536 bf16 × fold/no-fold** (131,072 single-element calls) + 60 pieces of 0..4096 with NaN/inf/overflowing scales (240,978 elements in total) | 131,132/131,132 calls, return value and every output word |
122| xf32 | `f8packxf32` | 100,025 f32 (25 specials + 100,000 class mix: NaN payloads, ±inf, ±0, subnormals, random bits) + 777 again as an odd piece; 0-length piece | 100,802/100,802 |
123| xbf16 | `f8packxbf16` | **all 65,536 bf16** + an odd piece of 333; 0-length piece | 65,869/65,869 |
124| scan | `f8scannan` (4096-byte pieces) | 400 arrays, 0..20,000 bytes (3,889,100 bytes), NaN density 0 to all | 400/400 (count and first) |
125| gemv | `f8gemvblock` (row × block) | 13 matrices, 2,432 rows, k up to 2048, NaN codes, special x and scales, a NaN×NaN-payload collision at every row's first NaN | 2,432/2,432 rows (y and yscale) |
126| fmt | `gpufmt` `gpufmtkinds` | 28,688 calls: every format of length 1..4 over `%lzdusxcA` (two caps each), every modifier × conversion pair (`""` `l` `ll` `lll` `z` `zl` `lz` `zz` × `duxsc%qXi.5-`), 12,000 random mixes, caps 0..1024, NULL `%s`, int words with garbage upper halves | 28,688/28,688 (return, bytes up to the NUL + 1, va_arg kinds) |
127
128Thirteen small mutants of `gpumath.om` are part of the verdict. The script applies each one, and each
129must change its stream's output:
130
131| mutant | records that change |
132|---|---|
133| ties round up instead of to even | fma 16,209 |
134| sticky bit dropped | fma 15 |
135| NaN priority of operands swapped | fma 1,332 |
136| exact-zero sum keeps the sign | fma 10 |
137| unfused `s * 2t + e*ln2lo` | log 134 |
138| unfused first `sincos` reduction step | sincos 1,917 |
139| `pow` multiply order swapped | pow 209 |
140| `pow2f` normal-range bias off by one | pow2f 254 |
141| e4m3 subnormal scale off by one | e4m3 14 |
142| scale fold 2^55 | scales 51,175 |
143| scan first index without the piece base | scan 15 |
144| GEMV multiply order swapped | gemv 1 (the collision matrix) |
145| `%c` prints `?` | fmt 2,008 |
146
147Fused steps that did not affect any output on this corpus: `exp`'s `- k*ln2lo` step, `log`'s final
148`e*ln2hi` step (the product is exact, since ln2hi has 33 significant bits), the last `sincos` step
149(`n*pio2_3t`), and the `log` series step (one `pow` result changes, no `log` result). They stay fused
150because the object fuses them.
151
152## Provenance correction (2026-10-31)
153
154Successive reseeds changed the compiler label on the old size/parity description.
155At main `4722cba587954c3257c7b4de13cfef2ba78d4329`, it incorrectly named
156`469532a5…79bc` and still claimed byte identity with `62775d2a…712b`.
157[#853's update at `f8d78252`](https://github.com/aien-dev/omega-mv/pull/853)
158explicitly lists `om/gpumath/gpumath.om` among the changed AArch64 binaries when
159comparing compilers `38748442…` and `469532a5…`. The author reported equal stdout
160and exit status when those binaries ran with stdin `/dev/null` and a timeout.
161That no-input smoke comparison is not the corpus, mutant or binding parity suite.
162
163The original #751 source, compiler identity and results above are retained as
164historical evidence; the unsupported current-size/byte-identity claim is removed.
165No implementation, expected output hash or receipt is changed. This documentation
166correction performs no native parity rerun and claims no new size, timing, parity
167or hardware result.
168
169## Later compile report: #848 (2026-10-31, author-reported)
170
171The [README at #848 merge `157bab57c67ff80f5b988b91bd39bc1099852915`](https://github.com/aien-dev/omega-mv/blob/157bab57c67ff80f5b988b91bd39bc1099852915/om/gpumath/README.md)
172records a **29,272-byte** program under compiler
173`90c8884c7f6888be47e4aa8e211eb48b776950fc43eef937ea963f96f7083983`,
174with output byte-identical to that under the preceding compiler
175`469532a50ecb8a0e5ca8e914dd46754bb3fbde7825fb33b7037208caf09779bc`.
176[#848's update at `1b9b5b82`](https://github.com/aien-dev/omega-mv/pull/848)
177also reports a compiler-output sweep in which gpumath was unchanged.
178
179This preserves the newer author's compile-size/identity report, not the old
180#751 size or parity attribution. It does not establish that the corpus, mutants
181or binding checks above were rerun under `90c8884c…`. This documentation
182correction did not independently compile or execute that program.
183
184## Later host parity report: #857 (2026-10-31, author-reported)
185
186[#857's measured update at `4670831d8be4a8dd4686062aa5f2bae4f571d14c`](https://github.com/aien-dev/omega-mv/pull/857)
187reports **gpumath parity PASS** on host AArch64 Linux under compiler
188`24caacf463cc29c5413d5b2565ef8f88c76e60a585bbad44d2388cb1ac29abb7`.
189This is a reported parity run, not merely #848's compile sweep or #853's no-input
190smoke comparison. The later integration head `c7cb1137589b385dddb48e3f0589402d38aa222b`
191kept that compiler unchanged, according to the same update.
192
193The [README at #857 merge `3a163bc5eafa5be4b9ca97b7f17046c33f0d9032`](https://github.com/aien-dev/omega-mv/blob/3a163bc5eafa5be4b9ca97b7f17046c33f0d9032/om/gpumath/README.md)
194records a **29,272-byte** program, byte-identical to the outputs under
195`90c8884c…3983` and `469532a5…79bc`. Preserve these as #857's author-reported
196size/identity and host parity observations, separate from the original #751
197compiler and tables. This correction did not independently rerun the suite,
198inspect those run logs or remeasure the binary; no per-stream counts or binding
199results from the old tables are reassigned to #857, and no GPU result is claimed.
200
201## Later compile report: #994 (2026-10-31, author-reported)
202
203[#994's reseed report](https://github.com/aien-dev/omega-mv/pull/994) explicitly
204states that the README byte-identity claims were remeasured: `gpumath.om`
205compiled to a **29,272-byte** program with identical output under old compiler
206`24caacf463cc29c5413d5b2565ef8f88c76e60a585bbad44d2388cb1ac29abb7` and new compiler
207`be8c51301307555254fc59e351d281aae1aa096d91521777e33a41c8b446448a`.
208The [README at #994 merge `9920aab1413983c2867d02d75b2276341595adb8`](https://github.com/aien-dev/omega-mv/blob/9920aab1413983c2867d02d75b2276341595adb8/om/gpumath/README.md)
209also retains the earlier `90c8884c…3983` and `469532a5…79bc` byte-identity notes.
210
211This is separately attributed compile-size/identity evidence. It neither
212relabels #751's original parity tables nor turns #857's host parity report
213into a new full-suite run under `be8c5130…`. This documentation correction did
214not independently remeasure or execute the program.
215
216## Compile check under omc `b4f45121…9b55` (2026-11-04, measured)
217
218Main's #1015 reseed moved the compiler pin to
219`b4f45121f6c6d5d8a6ed0f5c4f3934b7cc4bec369bfa89523699bd33928f9b55` and its README line
220reported the 29,272-byte size under it. Re-measured after merging main `d3d1b1bc`
221(Linux AArch64 host): `gpumath.om` (`b17b6ef0…dcde`, unchanged since #751) compiles with rc 0 under
222both `be8c5130…448a` and `b4f45121…9b55` to the same 29,272-byte program, sha256
223`dd23b3b78c5b2b538f32076e56ef7d6307abebbef5bc269355b9debcc7fcb64f`. This is a compile-size and
224byte-identity check only; the corpus, mutant and binding parity suite was not rerun for it.
225
226## Limits
227
228- The parity contract is the current object. If the C were built without contraction
229 (`-ffp-contract=off`), its own results would change, and this port would keep matching today's
230 object, not the new one.
231- Fixed pieces, set by the ABI v1 array rule: 4096 elements for scan/scales/x packing, one 128-column
232 block for the GEMV reference. The C adapter that loops over them is not written here. A NULL
233 `yscale` is the adapter ignoring `acc 1`, and a k that is not a multiple of 128 is the adapter
234 running only the k/128 whole blocks, as the C does. The committed corpus has neither case: both
235 were checked once in a /tmp run against the C (k = 200, 328 and 127 with `yscale` NULL and
236 non-NULL; y and yscale identical), not by `parity/run.sh`.
237- `gpufmt` limits are limits of the .om, not cases where the C goes out of bounds: `fmt` at most
238 1023 bytes plus its NUL inside 1024, at most 16 consuming conversions, `%s` strings inside a
239 4096-byte pool, a 1024-byte `out`. Past the first three the .om traps 3 where the C keeps working
240 (it takes as many varargs and as long strings as the caller passes). A `cap` above 1024 traps only
241 when an output write actually passes index 1023 of `out`; shorter output with a larger `cap` runs
242 normally. The committed corpus stays within 16 conversions; the exact 16-conversion boundary
243 (cap 1024 and cap 40: identical to the C) and the 17-conversion trap were exercised only in a /tmp
244 run, not by `parity/run.sh`. A NULL `out` or `fmt` cannot be expressed; the adapter keeps the C's
245 `!out || cap == 0` and `fmt == NULL` checks.
246- Narrowing that the C does by parameter type traps instead: an e4m3 byte above 255 traps 5, and so
247 does a bf16 scale above 65535. `hmpow2f` takes a `long` where the C takes an `int`.
248- `fma64` is internal and assumes 53-bit significands (`mul128`).
249- Two omc gaps shaped the code: a function cannot return `f32` (error 5), so f32 helpers return their
250 bits, and `const` is refused in function mode (error 9), so constants are written as their bit
251 patterns inline with a note.
252
253## NOT_RUN
254
255- No GPU, model or serving test: nothing here runs on the device.
256- No `make test`. The C files and every C caller are unchanged.
257- The C adapters, and the removal of the three objects from `libomega_gpu.a` through omega-bridge #8, are
258 not done here.
259- Only the pinned omc on Linux arm64 (the Spark) was used.