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 / docs / numeric / E1_REDUCTION_CONTRACT.md

140 lines · 10142 bytes Raw Blame History Rendered
1# E1 reduction contract (WP-D)
2
3Closes gap table row 5 (`docs/numeric/E1_GAP_TABLE.md`) on the CPU tiers and
4on GB10 for SUM, MAX, MIN and MEAN (MEAN = chip SUM levels plus one chip division). Code: `src/omega_numeric_reduce.h`, `src/omega_numeric_reduce.c`
5(reference + CPU realization), `src/omega_numeric_reduce_gb10.c` (GB10),
6tests `tests/test_omega_reduce.c`, chip runner `tests/run_reduce_chip.sh`.
7
8## Declared order
9
10`RECURSIVE_TILE32_PAIRWISE_TREE_LANE_DELTA_16_8_4_2_1_PAD_IDENTITY_MIN_ONE_LEVEL`
11
12The order depends only on `n` and the element index. Scheduling, thread
13count, CTA count and launch chunking cannot change it.
14
151. Level 0 is `x[0..n)`.
162. One level maps `len` values to `ceil(len/32)` values. Value `j` of the next
17 level is the existing warp tree `PAIRWISE_TREE_LANE_DELTA_16_8_4_2_1`
18 (`OMEGA_WARP_REDUCTION_DECLARED_ORDER`) over values `[32j, 32j+32)`:
19 for `d = 16, 8, 4, 2, 1`, lane `i` becomes `lane[i] (op) lane[i+d]`, the
20 lower lane on the left; the tile result is lane 0. Positions `>= len` hold
21 the identity of the op.
223. At least one level is applied, and levels repeat until one value is left.
23 The number of levels is the smallest `L >= 1` with `32^L >= n`
24 (1..32 -> 1, 33..1024 -> 2, 1025..32768 -> 3, up to 2^20 -> 4).
25
26Why tiles of 32 at every level: each level is exactly what the chip-proven
27REDUCE_SUM warp kernel computes per warp, and two levels are exactly what a
28single 1024-thread CTA (32 warps: warp tree, lane 0 to shared memory,
29barrier, warp 0 runs the same tree over the 32 warp results) computes. A
30later shared-memory CTA kernel can therefore realize the same order without
31changing any bits.
32
33## Ops
34
35| Op | Combine | Identity (padding) | n = 0 |
36|---|---|---|---|
37| SUM | IEEE `a + b`, RNE, subnormals kept, no FTZ | `-0.0` (`x + -0 == x` bit for bit for every non-NaN `x`, including `+0 + -0 = +0`) | `+0.0` (defined, not computed) |
38| MAX | FMNMX_MAX rule: NaN is missing data (the other operand wins; both NaN gives NaN), `-0 < +0` | quiet NaN `0x7fc00000` | quiet NaN |
39| MIN | FMNMX_MIN rule, same NaN and zero rules | quiet NaN | quiet NaN |
40| MEAN | `omega_math_div(SUM, u2f(n))`, correctly rounded, for `1 <= n <= 2^24` (n exact in FP32) | as SUM | quiet NaN; `n > 2^24` refused (`OMEGA_NUMERIC_ERR_OPERANDS`) |
41
42Notes:
43- `-0.0` is the true additive identity, so padding never changes a value; a
44 sum of all `-0` stays `-0`. The empty sum is defined as `+0.0`, the usual
45 convention for an empty sum, rather than the `-0` the padding would give.
46- NaN is the true identity of the MAX/MIN rule (`max(NaN, x) = x`). Padding
47 with `-inf` would instead turn an all-NaN input into `-inf`.
48- MAX and MIN under these rules are associative and commutative, so their
49 result does not depend on the order at all; they still use the declared
50 tree. NaN payloads are not semantic (any NaN equals any NaN in parity, as
51 `omega_numeric_bits_equal`).
52- Signaling NaN inputs to MAX/MIN are treated like quiet NaN (missing data).
53 The CPU tier quiets them before FMAXNM/FMINNM, as the FMNMX CPU tier does.
54- Overflow goes to infinity, `inf + -inf` is NaN, subnormal partial sums are kept.
55- Batched: `omega_reduce_rows_*(op, x, rows, n, row_stride, out)` reduces the
56 last axis of `[rows, n]`, each row independently with the same order;
57 `row_stride >= n` when `rows > 1`.
58- Lengths so large that padding would overflow the address range are refused
59 (`OMEGA_NUMERIC_ERR_OPERANDS`); the GB10 path is limited to `n <= 2^26`.
60
61## Worked examples (expected bits written by hand in the test)
62
63- `n = 33`, `x0 = 2^24`, `x1..x32 = 1`: tile 0 gives `2^24 + 30`, tile 1
64 gives `1`, level 2 gives `2^24 + 31`, a tie that rounds to even:
65 `2^24 + 32 = 0x4B800010` (the exact sum). Left-to-right gives `2^24 = 0x4B800000`.
66- `n = 32`, `x0 = 2^24`, `x1 = x17 = 1`, rest `-0`: lanes 1 and 17 meet at
67 `d = 16` (`2`), then `2^24 + 2 = 0x4B800001`. Adjacent-pairs-first
68 (deltas 1..16) loses both ones: `0x4B800000`.
69- `n = 64`, `x0 = 2^24`, `x1 = x33 = 1`, rest `-0`: the ones sit in
70 different tiles and each is lost to a tie: `0x4B800000`. A flat fold over
71 64 (lane `i` with `i + 32`) pairs them: `0x4B800001`.
72
73## Tiers
74
75- Reference: `omega_reduce_reference`, written from the definition (copy each
76 tile into 32 slots, pad, run the lane tree; SUM calls
77 `omega_warp_reduce_sum`, the existing declared warp tree).
78- CPU realization: `omega_reduce_cpu`, independent code (one buffer compacted
79 in place bottom-up, explicit AArch64 `fadd`, `fmaxnm`/`fminnm`, `ucvtf`,
80 `fdiv`).
81- GB10, all four ops: `omega_reduce_gb10`. Every level is padded on the host
82 with the op's identity (SUM/MEAN `-0.0`, MAX/MIN quiet NaN `0x7fc00000`) to
83 whole warps and run in chunks of at most `OMEGA_NUMERIC_MAX_COUNT` (65536,
84 whole warps, so no tile straddles a launch). The host gathers lane 0 of each
85 warp between launches: bit moves only, every FADD/FMNMX runs on the chip.
86 One launch per chunk per level (n = 10^6: 16 + 1 + 1 + 1 = 19).
87 - SUM and MEAN levels: the existing chip-proven REDUCE_SUM patch words
88 (`omega_numeric_patch_words`), checked with `omega_numeric_check_patch`
89 before submission (`omega_numeric_submit_check`) and again at launch.
90 - MAX/MIN levels: a reduce-owned patch written at `OMEGA_NUMERIC_PATCH_OFFSET`
91 of the vecadd kernel (`omega_reduce_gb10_minmax_patch`), 12 instructions:
92 five pairs `SHFL.DOWN PT, R9, R2, d, 0x1f` + `FMNMX R2, R2, R9, !PT|PT`
93 for d = 16, 8, 4, 2, 1 (the last FMNMX writes R9), then `STG.E [R6], R9`
94 and `EXIT`. `!PT` selects max, `PT` min. FMNMX returns the non-NaN operand
95 when one is NaN and orders `-0 < +0`, which is the contract's
96 `omega_ref_fmax`/`omega_ref_fmin` rule; a NaN pad is therefore the identity.
97 The words were checked against nvdisasm 13.0.85 (`-b SM121`) offline
98 (`make test-numeric-reduce-nvdisasm`).
99 - `omega_reduce_gb10_check_minmax_patch` runs the generic
100 `omega_numeric_check_patch` rules and then `CHECK:` markers `mm_shape`,
101 `mm_delta_order` (16, 8, 4, 2, 1), `mm_shfl_src`, `mm_shfl_dst` (R9),
102 `mm_first_wait_load` (first shuffle waits on SB4), `mm_fmnmx_form`,
103 `mm_fmnmx_pred` (MAX vs MIN), `mm_fmnmx_wait`, `mm_fmnmx_srcs`,
104 `mm_fmnmx_dst`, `mm_fmnmx_stall`, `mm_store`, `mm_exit`. It runs before
105 submission for every chunk size, and again inside the launch against the
106 exact QMD and code image submitted.
107 - Every level launch (all four ops) uses the reduce-owned executor, which
108 waits for the host completion marker and then for the
109 QMD release semaphore (`OMEGA_BW_SEMAPHORE_INTERMEDIATE_DONE`) plus
110 `dsb sy` before reading results (the stores-after-marker hazard found by
111 the DIV/SQRT lane, omega#141).
112 - MEAN: the chip computes the SUM levels, then the final
113 `omega_math_div(SUM, u2f(n))` on the chip as one launch of the whole-program
114 GB10 DIV kernel (E1 row 7, `omega_ds_gb10_run`). The bits are the same as
115 the former host division because both are correctly rounded. Receipts say
116 `GB10_DIV` for MEAN. Mutant: `-DOMEGA_REDUCE_MUTATE_MEAN_DIV` divides by n+1
117 and must FAIL chip parity.
118 - Pre-submission checks carry `CHECK:` markers (op, buffers, size, MEAN
119 `n <= 2^24`, order string, kernel registered as BIT_EXACT, padding bits,
120 level count, chunk shape, per-chunk submit check).
121- Single-launch CTA tree (shared memory) and on-device partial pass: not
122 built. The order above is chosen so it can be added later without changing bits.
123
124## Tests (`make test-numeric-reduce-cpu`, chip: `tests/run_reduce_chip.sh`)
125
126| ID | What it proves |
127|---|---|
128| `RED_ORDER_DECLARED` | order string, level counts, hand-derived worked-example bits on both tiers |
129| `RED_EMPTY_IDENTITY_SPECIAL_VALUES` | n = 0 results, identities exact, signed zeros, subnormal sums, overflow, inf/NaN rules, MEAN rounding and bounds, bad-argument refusals |
130| `RED_CPU_EQUALS_REFERENCE` | randomized differential, 61 lengths (0, 1, 2, primes, 31/32/33 ... 32769, 65537, 1000003, 10^6, 24 random) x 5 input classes x 4 ops, plus batched rows |
131| `RED_INDEPENDENT_ORACLE` | exact integer sums, MEAN of exact sums, MAX/MIN against a sequential scan |
132| `RED_DIFFERENT_ORDER_CAUGHT` | left-to-right, reversed deltas and flat fold each give different bits on crafted inputs and are flagged; left-to-right also differs on random data |
133| `RED_DETERMINISM` | n = 1000003, repeated runs and shifted buffers give identical bits |
134| `RED_GB10_PRESUBMIT_CHECKS` | refusals (NULL, too large, MEAN `n > 2^24`) and acceptance of every tested n for all four ops, without a device |
135| `RED_CRAFTED_TABLE` | 14 crafted vectors (all `-0`, signed zeros, overflow, inf, subnormal, all NaN, NaN with one number, sNaN, negative NaN, `+-FLT_MAX` cancel) with hand-written SUM, MAX, MIN and MEAN bits on reference and CPU |
136| `RED_GB10_MINMAX_PATCH` | the MAX and MIN patches pass their checks; 30 mutants (reversed or swapped deltas, delta 3, shuffle into R2 or R6, first shuffle not waiting for the input load, swapped shuffle pairs, MAX and MIN predicate flipped, swapped FMNMX operands, wrong last destination, missing wait, short stall, wrong shuffle clamp, stray modifier bit, and the other op's patch) are each refused; a host model that decodes the patch words equals the reference on 110 cases |
137| `RED_WRONG_PAD_CAUGHT` | wrong pads (`-inf` or `+0` for MAX, `+inf` or `-0` for MIN) give different bits on crafted inputs and are caught; MEAN worked example n = 33 is `0x48F83E2F` and differs from the sequential order |
138| `RED_GB10_PARITY_<OP>` | chip only, per op SUM, MAX, MIN, MEAN: GB10 bits equal the reference for 21 lengths (0, 1, 2, 31, 32, 33, 63, 64, 65, 97, 1000, 1023, 1024, 1025, 4097, 32768, 32769, 65536, 65537, 10^6, 1000003) x 4 input classes (`n > 70000`: 2), plus the crafted table and the worked examples. NaN equals NaN (payload not semantic) |
139| `RED_GB10_PARITY` | chip only: overall line, PASS only when every op has 0 mismatches |
140| `make test-numeric-reduce-nvdisasm` | offline: the MAX and MIN patch words disassemble (nvdisasm, `-b SM121`) to exactly the text the code declares |