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 / spec / mixed-algebra-ma2.md

333 lines · 21558 bytes Raw Blame History Rendered
1# Omega mixed algebra MA-2: one exact operation, ten realizations, measured selection
2
3Status: ADR 0019 gate MA-2 (several CPU realizations of one operation with
4tiered equivalence evidence and measured cost including conversion) and the
5ADR's first success gate (one Omega operation, at least three verified
6realizations, one selected with a recorded reason and margin, reproducible
7from receipts) delivered. It is not ADR gate MA-3 (contract and identity).
8This work was first labelled "MA-3"; it was renamed to match the ADR gate
9table. Code: `src/algebra/realize_*.{h,c}` (realizations),
10`src/algebra/oma_select.{h,c}` (MA-2 selector, retired for new decisions; see "Selector"), tests
11`tests/algebra/test_realize.c`, `tests/algebra/bench_mixed_algebra.c`,
12`tests/algebra/bench_select.c`. Receipts: `evidence/MIXED_ALGEBRA/`.
13Commands: `make test-realize` (plain + ASan/UBSan), `make bench-mixed-algebra`
14(two benchmark runs + selector, about 90 s wall). C11 with NEON/I8MM
15intrinsics, `-O2 -march=armv8.6-a+dotprod+i8mm+sve`, no outside dependencies.
16
17Receipts and labels: the two benchmark runs, `ma3_bench_run1.json` and
18`ma3_bench_run2.json` (schema `OMEGA_MIXED_ALGEBRA_MA3_BENCH_V1`), are the
19historical measurement records produced under the earlier "MA-3" label; they
20are kept unchanged. `ma2_select_receipt.json` was regenerated from them
21(without rerunning the benchmark) with the amended ADR 9.1 tie rule:
22`build/tests-algebra/bench_select evidence/MIXED_ALGEBRA/ma2_select_receipt.json
23evidence/MIXED_ALGEBRA/ma3_bench_run1.json evidence/MIXED_ALGEBRA/ma3_bench_run2.json`.
24Future `make bench-mixed-algebra` runs write new files only, under
25`evidence/MIXED_ALGEBRA/runs/<run-id>/` (`<run-id>` defaults to a UTC
26timestamp, `MA2_RUN_ID=...` to set it; an existing run directory is refused):
27`ma2_bench_run{1,2}.json` (schema `OMEGA_MIXED_ALGEBRA_MA2_BENCH_V1`) and a
28selector receipt `ma2_select_receipt.json` from those two runs. They never
29rewrite a committed evidence file; `make check-mixed-algebra-evidence` fails if
30a committed `evidence/MIXED_ALGEBRA/` file is modified or deleted, and the
31bench target runs it before and after. The selector reads both schemas.
32
33Outcome in one line: **on the Grace CPU, packed ternary (2 bits/weight) beats
34the best int8 realization 2.2x to 3.0x when the weight matrix no longer fits
35in L2 (m = 4096 rows); int8 wins everywhere the weights are cache-resident or
36the call is a single row, and int8 wins or ties whenever the weights must be
37packed on every call.**
38
39## Operation Omega-X
40
41y = W . x with W in {-1,0,+1}^(m x n) (row-major int8 source), x in int8^n,
42y in int32^m, **exact**: every realization must be bit-identical to the naive
43integer oracle `oma_rz_oracle`. |y_i| <= 128 n (x may be -128), so n is limited
44to 16,777,215 = floor(INT32_MAX / 128) (the same bound as `oma_dot_tw_i8`;
45the brief's "n*127" bound is one short for x = -128, so 128 n is enforced).
46Realizations with narrower index or residue ranges declare a lower `max_n`
47and refuse larger n at pack time.
48
49A realization is (pack, run). Pack converts and validates the canonical int8
50ternary matrix into its own weight form; run computes y for one x. Pack cost is
51measured separately.
52
53## Realizations
54
55| id | family | weight form (bits/weight) | kernel |
56|---|---|---|---|
57| R1_plain | binary | int8 (8) | plain C loop, -O2. **Labelled weak baseline.** |
58| R1_sdot | binary | int8 row-major (8) | NEON SDOT, 4 rows x 64 B/step, 8 accumulators; best for m = 1 |
59| R1_sdot_il | binary | int8, 4-row interleaved tiles (8) | NEON SDOT, one sequential stream per 4 rows |
60| R1_smmla | binary | int8, row pairs interleaved in 8-byte pieces (8) | I8MM SMMLA; half of each 2x2 result unused for GEMV, useful MACs per instruction equal SDOT |
61| R2_bitplane | bitplane | H1 pos/neg planes, bit k of byte j = weight 16k+j (2) | vtst byte masks, w = negmask - posmask, SDOT (4 SIMD ops / 16 weights) |
62| R2b_lut | lut | pos/neg nibble per 4 weights (2) + per-call tables | T-MAC style: per group of 4 activations a 16-entry subset-sum table split lo/hi bytes; vqtbl4q over 4 groups x 4 rows; USDOT/SDOT with +/-1 sums lanes; table build is inside every run |
63| R2c_crumb | crumb2 | 2-bit two's-complement codes (+1=01, -1=11), 4 per byte, strided (2) | sign-extending shift pair, then SDOT (2.75 SIMD ops / 16 weights) |
64| R3_sparse | sparse | uint16 +1 and -1 index lists per row (16 per non-zero) | scalar gather add/sub, 4 accumulators; max_n 65,536 |
65| R4_rns | rns | int8 (8) + per-call residues of x | moduli {256, 255, 253} (M = 16,515,840, all centered residues fit int8), 3 SDOT channels sharing each weight load, per-row mod reduction and mixed-radix CRT inside the run; max_n 64,512 |
66| R5_dense5 | dense5 | 5 trits/byte, reference byte rule, strided trit order (1.6) | NEON divide-by-3 decode ((b*171)>>9), SDOT on digits d = t+1, then subtract sum(x) |
67
68SVE SDOT is not a separate realization: the SVE vector length here is 128 bits
69(`svcntb() == 16`), so it does the same work as NEON SDOT.
70
71R2c_crumb was added during the work after R2_bitplane measured compute-bound
72at 4 SIMD ops per 16 weights. It is a different 2-bit code (not the H1
73bitplane code); conversion from int8 is part of its pack.
74
75## Correctness evidence
76
77`make test-realize`: **`MA2 test-realize PASS: 179712 checks, 0 failures, 10
78realizations`**, and the same line under `-fsanitize=address,undefined
79-fno-sanitize-recover=all`. Coverage:
80- every n in {1..5, 7, 15, 16, 17, 31..33, 63..65, 79..81, 127..129,
81 159..161, 255..257, 1000, 1023..1025} x every m in {1..5, 7, 15..17, 33}
82 (tails of 4, 16, 64, 80, 128 in both dimensions), with all-zero W, all +1,
83 all -1, x all -128, all 127, alternating -128/127, and random sparsity in
84 {0, 0.3, 0.6, 0.9, 1.0};
85- 400 random shapes (n <= 2100, m <= 70), sparsity uniform in [0, 1];
86- magnitude edges: n = 16,384 (y = +/-2,097,152), n = 64,512 at the RNS limit
87 (y = 8,257,536), n = 64,513 refused by RNS, n = 65,536 accepted and
88 65,537 refused by sparse, n = 100,003 (y = 12,800,384);
89- each plan is run twice (per-call scratch must not leak state);
90- rejections: weights 2, -2, -128, m = 0, n = 0, NULL, n above the int32 bound,
91 running an empty plan;
92- the oracle is cross-checked row by row against the reference library
93 (`oma_pack_bitplane` + `oma_dot_tw_i8`); R5 bytes are checked with
94 `oma_dense_byte_decode` (all < 243, right trit in the right place).
95- mutation check: swapping one SMMLA result lane makes 500 checks fail.
96- the same binary also runs 62 selector tie-rule checks (see Selector); the
97 realization checks alone are 179,684, unchanged.
98
99The benchmark re-verifies every realization bit-exact against the oracle on
100every grid cell before and after timing: 720 checks per run, 0 mismatches in
101both runs. The two historical run JSONs predate the top-level
102`oracle_checks` / `oracle_mismatches` fields that the benchmark now writes;
103for them the counts come from the run logs and from the nested
104`correctness` object each file already carries
105(`"runs_checked_against_oracle": 720, "mismatches": 0`).
106
107### Evidence tiers (ADR 0019 section 8)
108
109- The ten realizations (exact contract): **E2, differential with declared
110 coverage**. Reference = the naive integer oracle `oma_rz_oracle`; the
111 declared coverage is the list above (boundary and tail shapes, all-zero /
112 all +1 / all -1 weights, x extremes -128 and 127, sparsity 0 to 1, random
113 shapes, magnitude and `max_n` edges, repeated runs, every rejection path),
114 plus the benchmark's 720 bit-exact checks per run on the measured grid.
115 E2 is the tier ADR section 8 allows for a composed exact kernel.
116- The per-element reference core (MA-1, `spec/mixed-algebra-reference.md`):
117 **E3, exhaustive**, for per-trit ops (all 9 valid pairs), all 256 codes and
118 dense bytes, Z3 scalars (all 256 x 256 inputs), int16 <-> ternary (all
119 65,536 values) and every vector pair of length k <= 6 (3^k x 3^k). The
120 parts of that library tested on generated suites (int64 conversion,
121 multi-block dots, random packing) are E2. The absmean quantizer is an
122 approximate transform (ADR section 6.2), not an exact realization, and is
123 outside this gate.
124- The oracle itself is additionally cross-checked row by row against the E3
125 reference core (`oma_pack_bitplane` + `oma_dot_tw_i8`), so the E2 claim is
126 anchored to exhaustively checked elements.
127
128## Measurement method
129
130- One Cortex-X925 core (MIDR part 0xd85; cpus 5-9 and 15-19 are X925,
131 0-4 and 10-14 are A725), chosen as the most idle X925 over 0.3 s and pinned
132 with `sched_setaffinity`: **cpu 5** in both runs.
133- Grid: n in {1024, 4096, 16384} x m in {1, 64, 4096} x weight sparsity
134 (fraction of zeros) in {0, 0.3, 0.6, 0.9}; x uniform int8.
135- Per cell all realizations are packed, then 9 interleaved rounds; each
136 round times one block (about 0.3 ms or at least one call) per realization
137 with CLOCK_MONOTONIC. Reported: best (floor) and median ns per call, q25/q75.
138 Blocks where `/proc/thread-self/schedstat` run_delay grew by more than 2% of
139 the block were retried (run 1: 5 retries, run 2: 11, none forced).
140- Roofline floor = working-set bytes (packed weights + per-call scratch + x +
141 y) / read bandwidth at the smallest measured buffer size >= the working set.
142 Bandwidth is the best of three NEON read kernels in the same binary
143 (hot buffer): 16 KiB 235 GB/s, 48 KiB 244, 256 KiB 137, 1 MiB 128,
144 1.5 MiB 125, 4 MiB 86, 8 MiB 69, 16 MiB 50, 32 MiB 40, 64 MiB 37,
145 128 MiB 35, 256 MiB 35 GB/s. "% of floor" = floor / best time. Values above
146 100% (R1 at m = 64) mean the single-core read kernel is not the true ceiling
147 at L2 sizes; they are reported as measured.
148- Because rounds interleave realizations, a large working set is read in
149 whatever cache state the previous realization left; for m = 4096 this is
150 effectively L3/DRAM streaming, which is also how a model layer would see it.
151- Pack cost: median of 3-7 repeated packs (fresh allocation, validation,
152 conversion). "Pack per call" total = median pack + median run.
153- Load average 0.53 -> 1.04 (run 1), 1.04 -> 0.96 (run 2); thermal zone
154 36-44 C. Grid 18.7 s per run; each run with energy about 44 s.
155
156## Results (amortized: pack once), mean of the two runs
157
158Best binary = best of R1_sdot / R1_sdot_il / R1_smmla. Best ternary = best of
159R2, R2b, R2c, R3, R5. Ratio > 1 means ternary is faster. The ratio does not
160change with sparsity except where noted.
161
162| n | m | sparsity | best binary, ns (% floor) | best ternary, ns (% floor) | binary/ternary | winner |
163|---:|---:|---|---|---|---:|---|
164| 1024 | 1 | 0-0.6 | R1_sdot 11.2 (78%) | R2_bitplane 23.7 (23%) | 0.47 | binary |
165| 1024 | 1 | 0.9 | R1_sdot 11.2 (79%) | R3_sparse 18.2 (29%) | 0.62 | binary |
166| 1024 | 64 | all | R1_smmla 399-404 (121-123%) | R2c_crumb 667 (11%) | 0.60 | binary |
167| 1024 | 4096 | all | R1_sdot_il 98,000-101,500 (61-63%) | R2c_crumb 43,200-43,900 (20%) | 2.23-2.32 | **ternary** |
168| 4096 | 1 | 0-0.6 | R1_sdot 35.9 (98%) | R2_bitplane 85.5 (26%) | 0.42 | binary |
169| 4096 | 1 | 0.9 | R1_sdot 35.9 (98%) | R3_sparse 55.4 (37%) | 0.65 | binary |
170| 4096 | 64 | all | R1_smmla 2,041-2,049 (103%) | R2c_crumb 2,624-2,632 (19%) | 0.78 | binary |
171| 4096 | 4096 | all | R1_smmla 502,000-514,000 (83-85%) | R2c_crumb 172,500-174,000 (36%) | 2.90-2.97 | **ternary** |
172| 16384 | 1 | 0-0.6 | R1_sdot 137.5 (98%) | R2_bitplane 337 (25%) | 0.41 | binary |
173| 16384 | 1 | 0.9 | R1_sdot 137.1 (98%) | R3_sparse 230 (35%) | 0.60 | binary |
174| 16384 | 64 | all | R1_smmla / R1_sdot_il 9,880-9,970 (86-89%) | R2c_crumb 10,600-10,650 (21%) | 0.93-0.94 | binary (small margin) |
175| 16384 | 4096 | all | R1_sdot_il / R1_smmla 2,038,000-2,085,000 (93-95%) | R2c_crumb 785,000-809,000 (53-55%) | 2.52-2.66 | **ternary** |
176
177Full per-realization rows (min, median, q25/q75, bytes read, footprint,
178floor level, pack cost, per-call total) are in `ma3_bench_run{1,2}.json`
179under `cost_table`; the selector's per-cell decisions and the table above are
180in `ma2_select_receipt.json` (`decisions`, `binary_vs_ternary`).
181
182**R1 against its roofline:** 78-98% for m = 1 (L1-resident; 98% from n = 4096
183up), 86-123% for m = 64 (above 100% at L2 sizes, see method), 61-63% at
1841024 x 4096 (4 MiB, L2/L3 boundary), 83-85% at 4096 x 4096, 93-95% at
18516384 x 4096 (64 MiB from DRAM). The int8 champion is memory-bound and close to
186the floor wherever the matrix streams from outside L2.
187
188### Where ternary wins, and why
189
190Only at m = 4096, where the int8 matrix (4, 16 or 64 MiB) streams from L3 or
191DRAM. R2c_crumb moves a quarter of the bytes and its decode (7 shifts + 4 SDOT
192per 64 weights per row) keeps up well enough to finish 2.2x to 3.0x sooner, at
19320-55% of its own (4x lower) floor: it is compute-bound, the int8 kernel is
194bandwidth-bound. The win is independent of sparsity (dense 2-bit code).
195
196### Where ternary loses, and why
197
198- m = 1 and m = 64: the int8 weights are L1/L2-resident, bandwidth is not
199 the limit, and every packed form spends 2.75-4 SIMD ops per 16 weights
200 decoding where SDOT spends one. Binary is 1.1x to 2.4x faster.
201- At 16384 x 64 (1 MiB of int8 weights, L2) the gap narrows to 7%, still binary.
202- **Pack per call:** every realization's pack reads and validates the whole
203 int8 source, so no packed form can beat a copy. In per-call mode the cheapest
204 is always an int8-form realization (R1_*, R4_rns or R1_plain, whose packs
205 are all a validated copy and mostly tie inside the noise band, with R1_plain
206 inside the tied set, so the tie rule selects R1_plain there); the ternary packs
207 are scalar loops costing 1.1x (crumb) to 3-4x (bitplane, LUT) the int8 copy.
208 Ternary pays off only when weights are packed once and reused.
209
210### Negative results (recorded, not tuned away)
211
212- **R4_rns** loses everywhere amortized: 1.4x (DRAM-bound) to 90x (m = 1,
213 where residue build dominates) slower than R1. With SDOT the int32 lane is
214 already exact and as cheap as any narrower lane, so three channels triple the
215 MAC work and add residue build and CRT; nothing is saved.
216- **R3_sparse** never wins on this grid. Scalar gather runs at about one index
217 per cycle; at 90% zeros it is the best ternary form for m = 1 but still
218 1.5-1.7x behind SDOT, and at m = 4096 it is 1.3x behind R2c_crumb even at 0.9
219 (1.05 ms vs 0.81 ms at 16384 x 4096).
220- **R2b_lut** (T-MAC style) is correct but loses to both bitplane and crumb:
221 vqtbl4q plus lo/hi split costs more than decoding, and the per-call table
222 build makes m = 1 the worst case (2.1 us at n = 4096, 58x R1).
223- **R5_dense5** reaches R1's speed only at 16384 x 4096 (1.73 ms vs 2.05 ms,
224 1.2x win over int8 but 2.2x behind crumb); the divide-by-3 decode is
225 compute-bound. Its 1.6 bits/weight buys nothing over 2 bits here.
226- **R1_smmla** is not faster than a well laid out SDOT for GEMV (half of each
227 2x2 product is wasted); its advantage over row-major R1_sdot at m >= 64 came
228 from the interleaved layout, which R1_sdot_il reproduces with SDOT.
229
230## Selector (RETIRED for new decisions; not wired to rx_costmodel)
231
232**Status (2026-09-29): retired for new decisions.** The tie rule below
233(incumbent, then digital reference, then cheapest inside the noise band) failed
234the pre-registered TURING Wave 1 kill test on the MA-3 receipts: see omega
235`docs/turing/TURING_W0_PROPOSAL.md` K.6 (regret numbers) and K.7 (verdict and
236reframe). `oma_select` is kept unchanged only to reproduce
237`ma2_select_receipt.json` and as a regression test of the retired rule in
238`make test-realize`. Do not add new callers. New selections use
239`turing_rank_min_cost` in `src/turing` (lowest measured cost wins; ties are
240recorded, never change the choice) and are recorded as `turing.decision.v1`
241records via `turing-field`. The matching ADR 0019 section 9.1 amendment is
242proposed in aien-architecture and awaits the operator.
243
244`oma_select` loads MA-2 (and the historical MA-3-label) bench receipts (costs
245are read from the JSON, never hard-coded), applies the exact-contract filter
246(registry `exact`, verified in every run used, query n <= `max_n`), and ranks
247the eligible realizations by median cost (plus median pack cost when packing
248per call), mean over runs. Off-grid queries use the nearest measured cell
249(log2 distance in n and m, plus 4x the sparsity difference) and say so
250(`exact_cell: false`). Each decision record holds the query, the cell, the
251chosen realization, the cheapest, the runner-up, every candidate with its
252measured cost, within-run and across-run spread and % of floor (or the reason
253it was excluded), the margin (runner-up vs cheapest), the noise band, a
254verdict CHOSEN or TIE, the tied set (`tied_set`: every eligible candidate
255whose cost is within the band of the cheapest, the cheapest included), the
256incumbent given with the query and `tie_resolution`.
257
258Noise band = max over cheapest and runner-up of (a) the within-run spread
259(q75 - q25) / median of the timed blocks (pack spread weighted in when packing
260per call) and (b) the across-run spread (max - min) / mean of the cell's costs
261over every run in the table. TIE when the tied set has more than one member
262(equivalently, margin <= band).
263
264**Tie rule (ADR 0019 section 9.1, as amended).** Without a TIE the cheapest
265is chosen (`tie_resolution: "none"`). A TIE is resolved only inside the tied
266set: the incumbent selection stands if it is in the tied set (`"incumbent"`);
267otherwise the digital reference realization, `R1_plain`
268(`OMA_SEL_REFERENCE`), if it is in the tied set (`"reference"`); otherwise the
269cheapest member (`"cheapest"`). A realization outside the tied set, the
270reference or an incumbent included, is never selected by tie resolution.
271The receipt's decisions carry no incumbent (there is no prior selection).
272`test-realize` checks every branch: reference outside the tied set not chosen
273(cheapest instead), reference inside the tied set chosen, incumbent inside
274the tied set stands (also over a reference inside it), incumbent outside the
275tied set not chosen, unknown or unmeasured incumbent, no TIE, reference
276unmeasured, reference unverified in one run, TIE from across-run spread
277alone; every case also checks that the choice is a member of the tied set.
278
279Effect on the receipt (72 decisions over both runs): **38 TIE, 32 per-call
280and 6 pack-once. Resolution: 32 `reference`, 6 `cheapest`, 0 `incumbent`.**
281In all 32 per-call ties `R1_plain` is inside the tied set (1.00x to 1.26x
282the cheapest) and is selected. The 6 pack-once ties are 1024 x 64 at
283sparsity 0 and 0.6 and all four 16384 x 64 cells, each a tied set of
284{R1_smmla, R1_sdot_il} within about 1%; `R1_plain` is outside it (2.05x to
2852.78x the cheapest, for example 1119 ns vs 403 ns at 1024 x 64), so the
286cheapest member is selected (R1_smmla 3, R1_sdot_il 3). The earlier reading
287of the rule selected `R1_plain` in those 6 cells; the amendment removes that.
288No decision in the receipt (72 grid plus 4 off-grid) selects a realization
289outside its tied set (`decision_summary.chosen_outside_tied_set: 0` for the 72). The 34
290non-TIE decisions are unchanged (R1_sdot 16, R1_smmla 6, R2c_crumb 12).
291
292**Reproducibility check** (`MA2_SELECTOR_REPRO`): each of the 36 cells x 2
293pack modes is decided from run 1 alone and from run 2 alone.
294**72 decisions: 56 agree, 16 recorded ties, 0 disagree -> PASS**, with the
295amended tie rule applied, and every chosen realization is a member of its
296tied set (also part of the gate). "Recorded ties" counts only the cells where
297the two single-run choices differ and one run's tied set contains the other
298run's choice; it is not the number of ties. Recorded ties rose from 8 to 16
299because single-run pack-once ties now keep the cheapest member of each run
300(R1_smmla in run 1, R1_sdot_il in run 2 at 16384 x 64) and per-call ties with
301a single-run tied set that does not reach R1_plain pick R1_sdot or R4_rns. Total TIE verdicts in the decisions
302over both runs: **38 of 72 (32 per-call, 6 pack-once)**, listed in
303`decision_summary`.
304
305## Energy (indicative)
306
307The `aien_spbm` hwmon meter is readable without root (`cpu_p` = the whole
30810-core X925 cluster, microjoules, updates about every 0.1 s). Per
309realization: 0.4 s idle window, then 0.8 s of back-to-back calls; the receipt
310lists cluster idle and run power, total and dynamic (run minus idle) energy
311per call. At 16384 x 4096 (sparsity 0.3), run 1 total energy per call:
312R2c_crumb 7.5 mJ, R2_bitplane 8.6 mJ, best R1 12.7 mJ, R4_rns 20.9 mJ,
313R3_sparse 48.8 mJ. At 1024 x 4096 the forms are within about 20% of each
314other (0.42-0.51 mJ for R1/R2/R2c). Idle power moved between 1.1 and 3.8 W
315across windows because other sessions share the cluster, so these are not
316gate-quality numbers; energy is not used by the selector.
317
318## Limits
319
320- Single core, single thread; no multi-core scaling, no GPU (no GPU test was
321 run).
322- Shared machine: contention is detected per block and retried, not excluded.
323 Costs are best-of and median of 9 blocks, two runs.
324- Floors come from this binary's single-core read kernels; they are a
325 reference, not a proven hardware maximum (R1 exceeds them at L2 sizes).
326- Pack paths are plain scalar C; faster packers would narrow, not reverse, the
327 per-call result (every pack must read the int8 source once).
328- The (retired) selector is a stand-in over a 36-cell table with nearest-cell lookup; it
329 is not connected to `rx_costmodel` or the resident omega.select reaction.
330- Library audit note: no defect was found in `oma_trit`/`oma_z3`/`oma_pack`/
331 `oma_quant` while using them here (`oma_dot_tw_i8`, `oma_pack_bitplane`,
332 `oma_dense_byte_decode` agreed with the oracle on every checked row and
333 byte).