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
GitHub aien-dev/omega-mv#188 (code only)
Apply it
git checkout -b patch-6cd5abb668bffc56 4de761bb787eb363a718c5a2f19f2602b09e54cc curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/6cd5abb668bffc56.patch | git am
Provenance
github-import 1
aien-dev/omega-mv
188
eac06a4097ebd66c1c1eaa27d59b0b9a1aac01fa
{"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":188,"url":"https://github.com/aien-dev/omega-mv/pull/188","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:36:42Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/docs/tensor/M20_OMEGA_TENSOR.md b/docs/tensor/M20_OMEGA_TENSOR.md index 5e051a642..e09cc14e5 100644 --- a/docs/tensor/M20_OMEGA_TENSOR.md +++ b/docs/tensor/M20_OMEGA_TENSOR.md @@ -111,6 +111,8 @@ removed. Order and padding mutations are caught by the E1 WP-D suite. | GB10 RELU tensor op | NOT_RUN | RELU is a tensor-layer bit select with no realization call, so a GB10 context would run the same host code; no GB10 run in this work | | Unary ops RSQRT, ERF, GELU (LT-M21 CR-1 part) | PASS (CPU) | Host PASS, CPU tier: forge log `HIVE-M20-unary2-light-193221` at pre-stack head cc0c66d; re-run at stacked head queued (job `HIVE-M20-unary2`). `OMEGA_TU_RSQRT`, `OMEGA_TU_ERF`, `OMEGA_TU_GELU` appended after `OMEGA_TU_RELU` in the stack (no renumbering); the CPU `transc` entry calls `omega_math_rsqrt` / `omega_math_erf` / `omega_math_gelu` (`src/omega_numeric_transc.h:47,50,53`) once per element, no other arithmetic. Declared result: bit-identical to a direct E1 call on each logical element (raw bits, canonical qNaN `0x7fc00000` per `src/omega_numeric_transc.h:20`). Accuracy is E1's, not the tensor layer's: RSQRT correctly rounded (`OMEGA_TRANSC_MAX_ULP_RSQRT 0`, `src/omega_numeric_transc.h:34`; `docs/numeric/E1_TRANSCENDENTAL_CONTRACT.md:45,99`); ERF bounded contract, max 3 ulp (`transc.h:37`; contract `:48,102`); GELU (erf form, `x Phi(x)`) bounded contract, max 3 ulp (`transc.h:40`; contract `:50,105`). Tests: section 7b cases (dense, transpose, strided slice with offset, transpose of slice, broadcast row and scalar, rank 0, F16 / stale / missing-entry refusals) and section 7c (contract edge inputs on dense and step-2 strided slice vs direct E1, plus contract-stated values such as RSQRT(-0) = -inf, ERF(4) = 1, GELU(-inf) = -0). Mutants `TRANSC_RSQRT_ERF_SWAP`, `GELU_VIEW_STRIDE` (9 source mutants in the sweep with these two) | | GB10 unary ops RSQRT, ERF, GELU | MISSING_IMPLEMENTATION | E1 has no GB10 realization of these sequences (`docs/numeric/E1_TRANSCENDENTAL_CONTRACT.md:4`, `:197`: GB10 NOT_RUN) and main has no GB10 `OmegaTensorRealization` table (only `src/tensor/omega_tensor_cpu.c`). A table without a `transc` entry refuses these ops with `OMEGA_TENSOR_ERR_REALIZATION` (`src/tensor/omega_tensor.c:676`). CPU tier only in this cut | +| Trig unary ops (SIN, COS) | PASS (CPU) | Answers LT-M21 CR-1 (trig part); branch `hive/M20-trig`, forge job `HIVE-M20-trig`. `OMEGA_TU_SIN`, `OMEGA_TU_COS` appended after `OMEGA_TU_GELU` in the stack (no renumbering); the CPU `transc` entry calls `omega_math_sin` / `omega_math_cos` (`src/omega_numeric_transc.h:51-52`) once per element. Declared result: bit-identical to the direct E1 call on the logical element. Domain (E1 rule, `src/omega_numeric_transc.h:43` `OMEGA_TRANSC_TRIG_MAX_ABS`, `src/omega_numeric_transc.c:517` sincos_core): admitted `abs(x) <= 2^22` inclusive; outside it, +-inf and NaN give the canonical qNaN `0x7fc00000`; sin(+-0) = +-0, cos(+-0) = +1 (`docs/numeric/E1_TRANSCENDENTAL_CONTRACT.md:49`). Accuracy: bounded contract, NOT correctly rounded, max 2 ulp each inside the domain (`src/omega_numeric_transc.h:38-39`, observed 1; `docs/numeric/E1_TRANSCENDENTAL_CONTRACT.md:103-104`). Tests: SIN/COS join section 7b (dense, transpose, strided slice, transpose of slice, broadcast, rank 0, refusals) and new section 7c (grid incl. +-0, +-2^22, +-nextup(2^22), +-nextdown(2^22), inf, NaN, sNaN, through dense, broadcast and strided-slice views: bit-exact vs E1 plus an independent domain-rule check). Mutants `TRIG_SIN_COS_SWAP`, `TRIG_DOMAIN_NUMBER` (out-of-domain NaN replaced by a number). Host PASS, CPU tier: forge log `HIVE-M20-trig-light-193303` at pre-stack head 2a7da1a (425 pass, 0 fail, ASan/UBSan clean, sweep 9/9); re-run at stacked head queued | +| GB10 trig tensor ops (SIN, COS) | MISSING_IMPLEMENTATION | E1 has no GB10 kernel for SIN or COS (`docs/numeric/E1_GAP_TABLE.md:27`: "RSQRT, ERF, SIN, COS, GELU have no GB10 kernel"). No chip work in this cut | | General matmul | PASS (CPU) | 1x1x1, 7x13x5, 64x64x64, 129x3x257, 3x1025x2, 1x33x1, strided views, batched broadcast [2,1,3,4]x[5,4,6] | | Reductions | PASS (CPU) | lengths 1..32769 across tile/level edges, every axis of [3,37,5], keepdims, view vs copy, -0 sum | | Mutation / refusal | PASS (CPU) | `test-tensor-mutations`: 10/10 tensor source mutations caught (forge log `HIVE-M20-ops-light-185351`); 2 in-process mutant realizations (sequential sum, FFMA chain) caught; wrong order string refused | diff --git a/src/tensor/omega_tensor.h b/src/tensor/omega_tensor.h index a54d16df3..7ceac37cc 100644 --- a/src/tensor/omega_tensor.h +++ b/src/tensor/omega_tensor.h @@ -130,6 +130,13 @@ typedef enum { OMEGA_TU_RSQRT, /* omega_math_rsqrt, correctly rounded (OMEGA_TRANSC_MAX_ULP_RSQRT 0) */ OMEGA_TU_ERF, /* omega_math_erf, <= OMEGA_TRANSC_MAX_ULP_ERF (3) */ OMEGA_TU_GELU, /* omega_math_gelu, <= OMEGA_TRANSC_MAX_ULP_GELU (3), erf form */ + /* E1 trig (bounded contract, not correctly rounded): omega_math_sin / + * omega_math_cos per element, bit-exact with calling them directly. + * Admitted domain |x| <= 2^22 (OMEGA_TRANSC_TRIG_MAX_ABS, inclusive); + * outside it, +-inf and NaN give the canonical qNaN 0x7fc00000. + * sin(+-0) = +-0, cos(+-0) = +1. */ + OMEGA_TU_SIN, /* omega_math_sin, <= OMEGA_TRANSC_MAX_ULP_SIN (2) */ + OMEGA_TU_COS, /* omega_math_cos, <= OMEGA_TRANSC_MAX_ULP_COS (2) */ OMEGA_TU_COUNT } OmegaTensorUnaryOp; @@ -175,7 +182,8 @@ typedef struct { int (*reduce)(OmegaTensorReduceOp op, const float *x, size_t n, float *out); /* Optional. out[i] = op(a[i]) for the bounded-contract unary ops * (OMEGA_TU_EXP2 .. OMEGA_TU_TANH, OMEGA_TU_EXP, OMEGA_TU_LOG, OMEGA_TU_RSQRT, - * OMEGA_TU_ERF, OMEGA_TU_GELU), bit-exact with the E1 CPU sequences. + * OMEGA_TU_ERF, OMEGA_TU_GELU, OMEGA_TU_SIN, OMEGA_TU_COS), bit-exact with + * the E1 CPU sequences. * NULL: those ops return OMEGA_TENSOR_ERR_REALIZATION (other ops work). */ int (*transc)(OmegaTensorUnaryOp op, const float *a, float *out, size_t n); } OmegaTensorRealization; diff --git a/src/tensor/omega_tensor_cpu.c b/src/tensor/omega_tensor_cpu.c index 6bc0d3c3f..984ec5ee2 100644 --- a/src/tensor/omega_tensor_cpu.c +++ b/src/tensor/omega_tensor_cpu.c @@ -1,9 +1,9 @@ /* CPU realization of M20 OMEGA_TENSOR: the E1 CPU tier (explicit AArch64 * instructions in omega_numeric_cpu_realize) for elementwise ops, the frozen * E1 transcendental sequences (omega_numeric_transc.c, bounded contract) for - * EXP2 / LOG2 / SIGMOID / TANH, the E1 polynomials omega_math_exp / omega_math_log - * (omega_numeric.c) for EXP / LOG, and the reduction seam for reductions. No - * arithmetic outside those. RELU is a bit select in omega_tensor.c. */ + * EXP2 / LOG2 / SIGMOID / TANH / SIN / COS, the E1 polynomials omega_math_exp / + * omega_math_log (omega_numeric.c) for EXP / LOG, and the reduction seam for + * reductions. No arithmetic outside those. RELU is a bit select in omega_tensor.c. */ #include "omega_tensor.h" #include "omega_tensor_reduce_seam.h" #include "omega_numeric_transc.h" @@ -22,13 +22,14 @@ static float (*const TRANSC_FN[OMEGA_TU_COUNT])(float) = { [OMEGA_TU_EXP] = omega_math_exp, [OMEGA_TU_LOG] = omega_math_log, /* MUT:TRANSC_EXP_LOG_SWAP */ [OMEGA_TU_RSQRT] = omega_math_rsqrt, [OMEGA_TU_ERF] = omega_math_erf, /* MUT:TRANSC_RSQRT_ERF_SWAP */ [OMEGA_TU_GELU] = omega_math_gelu, + [OMEGA_TU_SIN] = omega_math_sin, [OMEGA_TU_COS] = omega_math_cos, /* MUT:TRIG_SIN_COS_SWAP */ }; static int cpu_transc(OmegaTensorUnaryOp op, const float *a, float *out, size_t n) { if ((unsigned)op >= OMEGA_TU_COUNT || !TRANSC_FN[op] || !a || !out) return OMEGA_NUMERIC_ERR_BAD_ARGS; if (!omega_numeric_fpenv_ok()) return OMEGA_NUMERIC_ERR_FPENV; float (*f)(float) = TRANSC_FN[op]; - for (size_t i = 0; i < n; i++) out[i] = f(a[i]); + for (size_t i = 0; i < n; i++) out[i] = f(a[i]); /* MUT:TRIG_DOMAIN_NUMBER */ return OMEGA_NUMERIC_OK; } diff --git a/tests/test_omega_tensor.c b/tests/test_omega_tensor.c index 1435bfbdb..b1ddd02d5 100644 --- a/tests/test_omega_tensor.c +++ b/tests/test_omega_tensor.c @@ -783,6 +783,8 @@ static const TranscCase TRANSC_CASES[] = { {OMEGA_TU_RSQRT, omega_math_rsqrt, "RSQRT"}, {OMEGA_TU_ERF, omega_math_erf, "ERF"}, {OMEGA_TU_GELU, omega_math_gelu, "GELU"}, + {OMEGA_TU_SIN, omega_math_sin, "SIN"}, + {OMEGA_TU_COS, omega_math_cos, "COS"}, }; #define NTRANSC (sizeof(TRANSC_CASES) / sizeof(TRANSC_CASES[0])) @@ -1152,6 +1154,106 @@ static void test_unary2_specials(void) { if (!rcs) omega_tensor_release(g, S); } +/* ---- 7c. trig domain (E1 SIN / COS) --------------------------------------- + * E1 admits |x| <= 2^22 (OMEGA_TRANSC_TRIG_MAX_ABS, inclusive, + * omega_numeric_transc.c sincos_core); outside it, +-inf and NaN give the + * canonical qNaN 0x7fc00000; sin(+-0) = +-0 and cos(+-0) = +1 + * (docs/numeric/E1_TRANSCENDENTAL_CONTRACT.md SIN/COS row). Each element is + * checked two ways: raw bits equal the direct E1 call on the logical element, + * and the declared domain rule holds on its own (in-domain finite input never + * gives NaN, out-of-domain input always gives exactly 0x7fc00000). The grid + * also goes through a broadcast and a reversed-order strided slice. */ +static const uint32_t TRIG_GRID[] = { + 0x00000000U /* +0 */, 0x80000000U /* -0 */, 0x4a800000U /* 2^22 */, 0xca800000U /* -2^22 */, + 0x4a7fffffU /* just below 2^22 */, 0xca7fffffU, 0x4a800001U /* just above 2^22 */, 0xca800001U, + 0x4b000000U /* 2^23 */, 0x7f7fffffU /* FLT_MAX */, 0xff7fffffU, 0x7f800000U /* +inf */, + 0xff800000U /* -inf */, 0x7fc00000U, 0x7fa00001U /* sNaN */, 0xffc00123U, 0x00000001U, 0x80000001U, + 0x3f490fdbU /* pi/4 */, 0x3f490fdcU, 0x3fc90fdbU /* pi/2 */, 0x40490fdbU /* pi */, 0xc0490fdbU, + 0x3f800000U, 0xbf800000U, 0x447a0000U /* 1000 */, 0x49742400U /* 1e6 */, 0x33800000U, +}; +#define NTRIG (sizeof(TRIG_GRID) / sizeof(TRIG_GRID[0])) + +static int trig_in_domain(uint32_t b) { return (b & 0x7fffffffU) <= 0x4a800000U; } /* |x| <= 2^22, not inf/NaN */ + +/* Check op on V (logical n) against E1 and the domain rule; src[idx[i]]. */ +static void trig_check(const TranscCase *tc, OmegaTensor V, const float *src, const size_t *idx, size_t n, + const char *what) { + OmegaTensor O = {0, 0}; + int rc = omega_tensor_unary(g, tc->op, V, &O); + float *got = rc ? NULL : rd(O); + size_t bad_e1 = got ? 0 : n, bad_dom = got ? 0 : n; + for (size_t i = 0; got && i < n; i++) { + uint32_t xb = omega_float_to_bits(src[idx[i]]), gb = omega_float_to_bits(got[i]); + bad_e1 += gb != omega_float_to_bits(tc->f(src[idx[i]])); + int is_nan = (gb & 0x7fffffffU) > 0x7f800000U; + bad_dom += trig_in_domain(xb) ? is_nan : gb != 0x7fc00000U; + } + CHECK(!rc && bad_e1 == 0, "trig %s %s bit-exact vs direct E1 (rc=%d, %zu of %zu differ)", tc->name, what, rc, + bad_e1, n); + CHECK(!rc && bad_dom == 0, "trig %s %s domain rule |x|<=2^22 else qNaN 0x7fc00000 (%zu of %zu violate)", + tc->name, what, bad_dom, n); + free(got); + if (!rc) omega_tensor_release(g, O); +} + +static void test_trig_domain(void) { + float x[NTRIG]; + size_t idx[3 * NTRIG]; + for (size_t i = 0; i < NTRIG; i++) x[i] = omega_bits_to_float(TRIG_GRID[i]); + uint64_t s1[1] = {NTRIG}, srow[2] = {1, NTRIG}, sb[2] = {3, NTRIG}; + OmegaTensor D = mk(1, s1, x), R = mk(2, srow, x), B = {0, 0}, S = {0, 0}; + int rcb = omega_tensor_broadcast_to(g, R, 2, sb, &B); + CHECK(rcb == 0, "trig: broadcast [1,%zu]->[3,%zu] rc=%d", (size_t)NTRIG, (size_t)NTRIG, rcb); + /* odd positions of the grid: start 1, step 2 -> a strided view with offset */ + uint64_t st[1] = {1}, sp[1] = {NTRIG}, se[1] = {2}; + int rcs = omega_tensor_slice(g, D, st, sp, se, &S); + CHECK(rcs == 0, "trig: strided slice rc=%d", rcs); + const TranscCase trig[2] = {{OMEGA_TU_SIN, omega_math_sin, "SIN"}, {OMEGA_TU_COS, omega_math_cos, "COS"}}; + for (size_t k = 0; k < 2; k++) { + for (size_t i = 0; i < NTRIG; i++) idx[i] = i; + trig_check(&trig[k], D, x, idx, NTRIG, "grid dense"); + if (!rcb) { + for (size_t i = 0; i < 3 * NTRIG; i++) idx[i] = i % NTRIG; + trig_check(&trig[k], B, x, idx, 3 * NTRIG, "grid broadcast"); + } + if (!rcs) { + for (size_t i = 0; i < NTRIG / 2; i++) idx[i] = 1 + 2 * i; + trig_check(&trig[k], S, x, idx, NTRIG / 2, "grid strided slice"); + } + } + /* Declared values that do not depend on E1 internals. */ + OmegaTensor O = {0, 0}; + float *gs = omega_tensor_unary(g, OMEGA_TU_SIN, D, &O) ? NULL : rd(O); + if (gs) omega_tensor_release(g, O); + float *gc = omega_tensor_unary(g, OMEGA_TU_COS, D, &O) ? NULL : rd(O); + if (gc) omega_tensor_release(g, O); + CHECK(gs && omega_float_to_bits(gs[0]) == 0x00000000U && omega_float_to_bits(gs[1]) == 0x80000000U, + "trig: sin(+0) = +0, sin(-0) = -0 (0x%08x 0x%08x)", gs ? omega_float_to_bits(gs[0]) : 0U, + gs ? omega_float_to_bits(gs[1]) : 0U); + CHECK(gc && omega_float_to_bits(gc[0]) == 0x3f800000U && omega_float_to_bits(gc[1]) == 0x3f800000U, + "trig: cos(+0) = cos(-0) = +1 (0x%08x 0x%08x)", gc ? omega_float_to_bits(gc[0]) : 0U, + gc ? omega_float_to_bits(gc[1]) : 0U); + /* exactly 2^22 is admitted (a finite value in [-1, 1]); the next float up is not (qNaN) */ + for (int k = 0; k < 2; k++) { + float *gv = k ? gc : gs; + const char *nm = k ? "cos" : "sin"; + for (int j = 2; j <= 3; j++) { + float v = gv ? gv[j] : 2.0f; + CHECK(gv && v >= -1.0f && v <= 1.0f, "trig: %s(%s2^22) admitted, in [-1,1] (0x%08x)", nm, + j == 3 ? "-" : "", gv ? omega_float_to_bits(gv[j]) : 0U); + } + for (int j = 6; j <= 7; j++) + CHECK(gv && omega_float_to_bits(gv[j]) == 0x7fc00000U, "trig: %s(%snextup(2^22)) is qNaN (0x%08x)", nm, + j == 7 ? "-" : "", gv ? omega_float_to_bits(gv[j]) : 0U); + } + free(gs); + free(gc); + omega_tensor_release(g, D); + omega_tensor_release(g, R); + if (!rcb) omega_tensor_release(g, B); + if (!rcs) omega_tensor_release(g, S); +} + /* ---- 8. determinism ------------------------------------------------------- */ static void kat_digest(uint8_t out[32]) { uint64_t save = g_rng; @@ -1199,6 +1301,7 @@ int main(void) { test_transc_unary(); test_relu_unary(); test_unary2_specials(); + test_trig_domain(); test_determinism(); /* every test released what it made */ uint32_t lt, ls; diff --git a/tools/tensor_mutations.sh b/tools/tensor_mutations.sh index 4e3962eab..8a019a59b 100755 --- a/tools/tensor_mutations.sh +++ b/tools/tensor_mutations.sh @@ -26,7 +26,9 @@ TRANSC_EXP_LOG_SWAP|omega_tensor_cpu.c|s/= omega_math_exp, \(.*\)= omega_math_lo RELU_NEG_ZERO|omega_tensor.c|s/if (u >> 31) return 0U;/if (u >> 31) return u == 0x80000000U ? u : 0U;/ RELU_NAN_PAYLOAD|omega_tensor.c|s/return OMEGA_QNAN_BITS;/return u;/ TRANSC_RSQRT_ERF_SWAP|omega_tensor_cpu.c|s/= omega_math_rsqrt, \(.*\)= omega_math_erf,/= omega_math_erf, \1= omega_math_rsqrt,/ -GELU_VIEW_STRIDE|omega_tensor.c|s/gather(s, .in, src);/if (op == OMEGA_TU_GELU) { dense_strides(in.rank, in.shape, in.strides); } gather(s, \&in, src);/' +GELU_VIEW_STRIDE|omega_tensor.c|s/gather(s, .in, src);/if (op == OMEGA_TU_GELU) { dense_strides(in.rank, in.shape, in.strides); } gather(s, \&in, src);/ +TRIG_SIN_COS_SWAP|omega_tensor_cpu.c|s/= omega_math_sin, \(.*\)= omega_math_cos,/= omega_math_cos, \1= omega_math_sin,/ +TRIG_DOMAIN_NUMBER|omega_tensor_cpu.c|s/out\[i\] = f(a\[i\]);/{ out[i] = f(a[i]); if ((op == OMEGA_TU_SIN || op == OMEGA_TU_COS) \&\& out[i] != out[i]) out[i] = 0.0f; }/' # Baseline: the unmutated build must PASS, otherwise every mutant would # look "caught" (e.g. the test refusing an unsuitable FP environment).