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 Source

Omega mixed algebra: CPU reference (parity oracle)

Status: reference implementation, correctness first. Code: src/algebra/ (prefix oma_). Tests: tests/algebra/test_oma.c. Build and run: make test-algebra (and make test-algebra-asan for the same suite under AddressSanitizer + UndefinedBehaviorSanitizer). Plain C11 (-std=c11 -Wall -Wextra -Werror -pedantic), no dependencies beyond libc/libm.

This library is the correctness yardstick for any faster realization (SIMD, GPU, analog). A fast path is correct only if it matches these functions bit for bit (exact ops) or within the documented metric (approximate ops).

Encoding (H1)

trit valuepos bitneg bit2-bit code (bit0=pos, bit1=neg)
-1012
0000
+1101
INVALID113

value = pos - neg. A block (oma_block) is 64 trits as two uint64_t planes {pos, neg}; lane i is bit i of each plane.

Invalid-state rule. (pos=1, neg=1) is never a value. Every decoder, validator and op checks its inputs and returns an explicit error code; no function reads an invalid lane as 0 or any other value. Outputs of valid inputs are always valid (tested).

Return codes

OMA_OK = 0; errors are negative: OMA_E_INVALID_TRIT (int8 not in {-1,0,1}), OMA_E_INVALID_CODE (code 3 or > 3), OMA_E_INVALID_PLANES (block with pos & neg != 0), OMA_E_OVERFLOW, OMA_E_INVALID_BYTE (dense byte >= 243 or non-zero padding), OMA_E_INVALID_Z3, OMA_E_ARG, OMA_E_UNDERFLOW (absmean of a non-zero input whose mean is below FLT_MIN). oma_strerror(rc) names them.

On error, outputs are left untouched. Every function validates its whole input (all trits, all blocks, all bytes, padding, capacity, finiteness) before it writes any output, so a failed call never leaves a half-written buffer. The suite checks this on every error path by prefilling each output with a sentinel byte and comparing after the call.

Scalar inputs are int. The per-trit, per-code and Z3 scalar functions (oma_code_*, oma_trit_to_code, oma_trit_neg/add/mul, oma_z3_*, oma_trit_to_z3, oma_dense_byte_decode) take int, so an out-of-range value such as 256 or -129 reaches the range check and is rejected; it is never silently wrapped to a valid 8-bit value at the call.

API

oma_trit.h

Block add sum formula (a+b in {-2..2}, z = neither plane set): sum.pos = (ap&bz)|(az&bp)|(an&bn), sum.neg = (an&bz)|(az&bn)|(ap&bp), carry = {ap&bp, an&bn}.

oma_z3.h: Z3 values {0,1,2}: make, add, mul, neg, and the map 0<->0, 1<->+1, 2<->-1 (oma_z3_to_trit, oma_trit_to_z3). The map is a ring isomorphism, so Z3 block ops run on the same bitplanes carry-free: oma_z3_block {one, two} is bit-identical to oma_block {pos, neg}; Z3 add = balanced-add sum with carry dropped, Z3 mul = H2, Z3 neg = swap.

oma_pack.h

oma_quant.h: oma_quant_absmean, oma_quant_rel_l2 (see below).

Exact vs approximate

Exact (bit-for-bit, integer-defined): all trit, block, dot, Z3, integer conversion and pack/unpack operations.

Approximate: oma_quant_absmean (BitNet b1.58 style). It is a lossy transform, not a realization of the float values.

The loss is measured by oma_quant_rel_l2 = ||w - q*scale||_2 / ||w||_2, computed in double. It returns OMA_E_ARG for NaN/Inf w_i, for a NaN, Inf or negative scale, and when ||w|| = 0 but the reconstruction is not zero (relative error undefined); scale 0 is accepted. On OMA_OK the error is finite.

Properties that hold exactly:

Test coverage (one run: 25,672,462 checks, 0 failures)

sectioncheckswhat
constructors + codes1,718every int in [-300,300], all 256 codes, pinned H1 table
per-trit truth tables161all 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
invalid (1,1) rejection380,25620,000 random blocks with one (1,1) lane through every op, decoder, serializer and Z3 path
exhaustive 3^k, k=1..614,210,967all 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)
dense bytes1,727all 256 byte values: 243 decode + repack, 13 rejected (alone and mid-stream), padding canonicality for n=1..4
integer <-> ternary3,482,793all 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
random block properties6,298,2551,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
pack/unpack random307,02420,000 random lengths 0..1000, both forms + serialize round trip, bad-trit and capacity rejection
Z3866,314all 256x256 scalar inputs (valid vs mod-3 oracle and isomorphism, invalid rejected); 200,000 random Z3 blocks
errors leave outputs6,254every 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
absmean quantization116,993hand-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

Fixed seed (splitmix64); runs are reproducible. The same suite passes under -fsanitize=address,undefined -fno-sanitize-recover=all. A mutation check (dropping one term from the add or mul formula, or accepting byte 243) makes the suite fail, and so does restoring the old write-as-you-go unpack/pack functions or the old quantizer (silent zero scale, unchecked rel_l2).

Relation to the parked branch experiment/ternary-semantics (ae1e2a3)

That branch is parked and was not modified or merged. It was read for data ideas only; this library is written fresh.

Agrees:

Differences and one conflict: