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-reference.md

181 lines · 11236 bytes Raw Blame History Rendered
1# Omega mixed algebra: CPU reference (parity oracle)
2
3Status: reference implementation, correctness first. Code: `src/algebra/`
4(prefix `oma_`). Tests: `tests/algebra/test_oma.c`. Build and run:
5`make test-algebra` (and `make test-algebra-asan` for the same suite under
6AddressSanitizer + UndefinedBehaviorSanitizer). Plain C11
7(`-std=c11 -Wall -Wextra -Werror -pedantic`), no dependencies beyond libc/libm.
8
9This library is the correctness yardstick for any faster realization (SIMD,
10GPU, analog). A fast path is correct only if it matches these functions bit
11for bit (exact ops) or within the documented metric (approximate ops).
12
13## Encoding (H1)
14
15| trit value | pos bit | neg bit | 2-bit code (bit0=pos, bit1=neg) |
16|-----------:|:-------:|:-------:|:-------------------------------:|
17| -1 | 0 | 1 | 2 |
18| 0 | 0 | 0 | 0 |
19| +1 | 1 | 0 | 1 |
20| INVALID | 1 | 1 | 3 |
21
22value = pos - neg. A block (`oma_block`) is 64 trits as two `uint64_t`
23planes `{pos, neg}`; lane i is bit i of each plane.
24
25**Invalid-state rule.** (pos=1, neg=1) is never a value. Every decoder,
26validator and op checks its inputs and returns an explicit error code; no
27function reads an invalid lane as 0 or any other value. Outputs of valid
28inputs are always valid (tested).
29
30## Return codes
31
32`OMA_OK = 0`; errors are negative: `OMA_E_INVALID_TRIT` (int8 not in
33{-1,0,1}), `OMA_E_INVALID_CODE` (code 3 or > 3), `OMA_E_INVALID_PLANES`
34(block with `pos & neg != 0`), `OMA_E_OVERFLOW`, `OMA_E_INVALID_BYTE` (dense
35byte >= 243 or non-zero padding), `OMA_E_INVALID_Z3`, `OMA_E_ARG`,
36`OMA_E_UNDERFLOW` (absmean of a non-zero input whose mean is below
37`FLT_MIN`). `oma_strerror(rc)` names them.
38
39**On error, outputs are left untouched.** Every function validates its whole
40input (all trits, all blocks, all bytes, padding, capacity, finiteness)
41before it writes any output, so a failed call never leaves a half-written
42buffer. The suite checks this on every error path by prefilling each output
43with a sentinel byte and comparing after the call.
44
45**Scalar inputs are `int`.** The per-trit, per-code and Z3 scalar functions
46(`oma_code_*`, `oma_trit_to_code`, `oma_trit_neg/add/mul`, `oma_z3_*`,
47`oma_trit_to_z3`, `oma_dense_byte_decode`) take `int`, so an out-of-range
48value such as 256 or -129 reaches the range check and is rejected; it is
49never silently wrapped to a valid 8-bit value at the call.
50
51## API
52
53`oma_trit.h`
54- `oma_trit_make` (checked constructor), `oma_trit_to_code`, `oma_code_to_trit`.
55- Per-trit, on codes and on values: `neg`, `add` (returns sum trit and carry
56 trit with a + b = sum + 3*carry), `mul`.
57- Block: `oma_block_validate`, `oma_block_encode`/`decode` (int8[64]),
58 `oma_block_neg` (swap planes), `oma_block_add` (lane-wise sum + carry
59 blocks, no propagation between lanes), `oma_block_mul` (H2:
60 pos=(ap&bp)|(an&bn), neg=(ap&bn)|(an&bp)).
61- `oma_block_dot`: popcount(pos) - popcount(neg) of the H2 product.
62- `oma_dot_tw_i8` (H3): ternary weights (ceil(n/64) blocks) x int8
63 activations by add/sub only, exact int32. Lanes >= n must be zero
64 (`OMA_E_ARG`); n <= 16,777,215 so |result| <= 128n fits int32
65 (`OMA_E_OVERFLOW` above).
66- `oma_int_to_bt` (canonical: little-endian, no leading zero digit, 0 has
67 zero digits, at most 41 digits for any int64 including INT64_MIN),
68 `oma_int_to_bt_fixed` (exactly n digits, `OMA_E_OVERFLOW` if the value needs
69 more), `oma_bt_to_int` (rejects bad digits; `OMA_E_OVERFLOW` outside int64;
70 accepts leading zeros).
71
72Block add sum formula (a+b in {-2..2}, `z` = neither plane set):
73sum.pos = (ap&bz)|(az&bp)|(an&bn), sum.neg = (an&bz)|(az&bn)|(ap&bp),
74carry = {ap&bp, an&bn}.
75
76`oma_z3.h`: Z3 values {0,1,2}: `make`, `add`, `mul`, `neg`, and the map
770<->0, 1<->+1, 2<->-1 (`oma_z3_to_trit`, `oma_trit_to_z3`). The map is a ring
78isomorphism, so Z3 block ops run on the same bitplanes carry-free:
79`oma_z3_block {one, two}` is bit-identical to `oma_block {pos, neg}`;
80Z3 add = balanced-add sum with carry dropped, Z3 mul = H2, Z3 neg = swap.
81
82`oma_pack.h`
83- Bitplane form: `oma_pack_bitplane` / `oma_unpack_bitplane` (n trits <->
84 ceil(n/64) blocks, unused lanes zero and checked on unpack);
85 `oma_block_serialize` / `deserialize` (16 bytes: pos then neg, little-endian;
86 deserialize rejects (1,1)).
87- Dense form: 5 trits per byte, byte = sum_{i<5} (t_i + 1) * 3^i, least
88 significant trit first, valid bytes 0..242. `oma_pack_dense`,
89 `oma_unpack_dense` (rejects byte >= 243 and non-zero padding trits in the
90 last byte, so every trit string has exactly one encoding),
91 `oma_dense_byte_decode`.
92
93`oma_quant.h`: `oma_quant_absmean`, `oma_quant_rel_l2` (see below).
94
95## Exact vs approximate
96
97Exact (bit-for-bit, integer-defined): all trit, block, dot, Z3, integer
98conversion and pack/unpack operations.
99
100**Approximate:** `oma_quant_absmean` (BitNet b1.58 style). It is a lossy
101transform, not a realization of the float values.
102
103- mean = sum|w_i| / n, with the sum and the division in **double**;
104 scale = (float)mean.
105- q_i = clamp(round(w_i / scale), -1, 1), round half away from zero. The
106 reference computes w_i / scale in **double**. Because q_i only depends on
107 whether |w_i| >= scale/2, this is the same as the exact rule
108 "q_i = sign(w_i) if |w_i| >= scale/2, else 0". A correctly rounded float32
109 division gives the same q: w_i and scale are floats with scale >= FLT_MIN,
110 so an exact quotient below 1/2 sits more than 2^-26 below it and cannot
111 round up to 1/2 (checked on 200,000,000 near-tie pairs, 0 disagreements).
112 Fast paths that use an approximate reciprocal or flush-to-zero are not
113 covered by that argument and must be checked against this reference.
114- All-zero (or empty) input: `OMA_OK`, scale 0, q = 0.
115- Non-zero input whose double mean is below `FLT_MIN` (so scale would be
116 subnormal or 0): `OMA_E_UNDERFLOW`, outputs untouched. A successful call
117 therefore returns scale 0 (input all zero) or a normal float, never a
118 subnormal, and never quantizes non-zero input to all zero silently.
119- NaN/Inf input: `OMA_E_ARG`. No epsilon is added (BitNet adds a tiny eps;
120 here the zero case is explicit).
121
122The loss is measured by `oma_quant_rel_l2` = ||w - q*scale||_2 / ||w||_2,
123computed in double. It returns `OMA_E_ARG` for NaN/Inf w_i, for a NaN, Inf
124or negative scale, and when ||w|| = 0 but the reconstruction is not zero
125(relative error undefined); scale 0 is accepted. On `OMA_OK` the error is
126finite.
127
128Properties that hold exactly:
129- q of already-ternary input equals the input (scale = fraction of
130 non-zero entries).
131- Re-quantizing q*scale returns the same q, except that it returns
132 `OMA_E_UNDERFLOW` when k*scale/n < `FLT_MIN` (k = number of non-zero q).
133 Proof sketch: q_i*scale is exact; the new scale s2 = (float)(k*scale/n) <=
134 scale, so every non-zero lane has |q_i*scale| >= s2/2 and keeps its sign.
135## Test coverage (one run: 25,672,462 checks, 0 failures)
136
137| section | checks | what |
138|---|---:|---|
139| constructors + codes | 1,718 | every int in [-300,300], all 256 codes, pinned H1 table |
140| per-trit truth tables | 161 | all 9 valid pairs vs integer oracle and a literal table; invalid code 3 (and 4, 255) rejected for neg/add/mul; value-level ops over [-3,3]^2 |
141| invalid (1,1) rejection | 380,256 | 20,000 random blocks with one (1,1) lane through every op, decoder, serializer and Z3 path |
142| exhaustive 3^k, k=1..6 | 14,210,967 | all 3^k x 3^k pairs: add/carry, mul, neg, dot, Z3 add/mul vs naive; per vector: dense and bitplane round trip, dense byte value, digits<->int canonical round trip, ternary x int8 dot with 4 activation sets (incl. -128/127 extremes) |
143| dense bytes | 1,727 | all 256 byte values: 243 decode + repack, 13 rejected (alone and mid-stream), padding canonicality for n=1..4 |
144| integer <-> ternary | 3,482,793 | all 65,536 int16 values vs an independent offset-base-3 oracle (canonical, fixed-width, overflow on n-1 digits); (3^k-1)/2 boundaries k=1..40; INT64_MIN/MAX and 1,000,000 random int64 round trips; decode overflow and bad digits |
145| random block properties | 6,298,255 | 1,000,000 random valid block pairs (varied density) vs lane oracle; 100,000 multi-block ternary x int8 dots, n in 0..512, stray-lane rejection |
146| pack/unpack random | 307,024 | 20,000 random lengths 0..1000, both forms + serialize round trip, bad-trit and capacity rejection |
147| Z3 | 866,314 | all 256x256 scalar inputs (valid vs mod-3 oracle and isomorphism, invalid rejected); 200,000 random Z3 blocks |
148| errors leave outputs | 6,254 | every error path of every function with outputs prefilled with a sentinel and checked unchanged: bad trit / (1,1) block / bad byte / bad padding at fixed and 6,000 random positions (pack/unpack bitplane and dense), short buffers, serialize, block and Z3 ops, dots (stray lane, n > max), integer conversion, quantization; 14 wide ints (INT_MIN, -129, 256, 258, INT_MAX, ...) through every scalar function |
149| absmean quantization | 116,993 | hand-computed vectors (exact ties, largest float below a tie, errors sqrt(1.5/6.5) and sqrt(3/11)); zero, NaN, Inf; denormal and FLT_MIN/FLT_MAX edges; rel_l2 argument rules; 20,000 ternary vectors (idempotence, scale); 20,000 dyadic vectors w_i = k_i*2^-e (half near FLT_MIN) against an exact integer oracle for scale rounding, the |w_i| >= scale/2 rule and the underflow rule; requantization (same q or the stated underflow) |
150
151Fixed seed (splitmix64); runs are reproducible. The same suite passes under
152`-fsanitize=address,undefined -fno-sanitize-recover=all`. A mutation check
153(dropping one term from the add or mul formula, or accepting byte 243) makes
154the suite fail, and so does restoring the old write-as-you-go unpack/pack
155functions or the old quantizer (silent zero scale, unchecked rel_l2).
156
157## Relation to the parked branch `experiment/ternary-semantics` (ae1e2a3)
158
159That branch is parked and was not modified or merged. It was read for data
160ideas only; this library is written fresh.
161
162Agrees:
163- Same per-trit meaning of the two planes (pos, neg; value = pos - neg).
164- Same dense tryte rule: byte = sum (t_i+1)*3^i, LSB trit first, bytes < 243,
165 padding trits must be zero. Bytes are interchangeable between the two.
166- Same balanced digit convention (little-endian, digits in {-1,0,1}).
167
168Differences and one conflict:
169- **Conflict (invalid state):** the parked `omega_t_from_planes` decodes a
170 (1,1) lane as 0 without checking; validity is a separate
171 `omega_t_planes_valid` the caller may skip. Under H1 that is a silent
172 misread. Here every decoder validates. Anything reusing the parked decoder
173 must call the validator first.
174- Container layout: parked packs 32 trits in one `uint64_t` (bits 0..31 pos,
175 32..63 neg); here a block is 64 trits in two separate 64-bit planes. Same
176 semantics, different memory layout: convert lane by lane, not by casting.
177- Integer model: parked T32 words saturate symmetrically at +/-(3^32-1)/2;
178 here conversion is exact over int64 and overflow is an explicit error
179 (no saturation).
180- Parked Kleene ops (min/max/xor), compare, shifts and the OMG1 object
181 encoding are out of scope here.