diff --git a/docs/tensor/M20_OMEGA_TENSOR.md b/docs/tensor/M20_OMEGA_TENSOR.md index 2305c4706..5e051a642 100644 --- a/docs/tensor/M20_OMEGA_TENSOR.md +++ b/docs/tensor/M20_OMEGA_TENSOR.md @@ -109,6 +109,8 @@ removed. Order and padding mutations are caught by the E1 WP-D suite. | Unary ops EXP, LOG, RELU (M20 cut ops, LT-M21 CR-1..CR-5) | PASS (CPU) | Host PASS, CPU tier: forge log `HIVE-M20-ops-light-185351` at pre-stack head 1e16a8d, re-run at stacked head queued (`test-tensor` 438 pass / 0 fail plain and ASan/UBSan; `test-tensor-mutations` 10/10 tensor mutants caught). EXP / LOG: `OMEGA_TU_EXP` / `OMEGA_TU_LOG` call the E1 Omega-defined polynomials `omega_math_exp` / `omega_math_log` (`src/omega_numeric.c:458`, `:501`) once per element through the CPU `transc` entry; the result must equal a direct E1 call bit for bit. NOT correctly rounded: E1 checks them only against binary128 within EXP 40 ulp, LOG 4 ulp (`tests/test_omega_numeric.c:376-384`; `docs/numeric/E1_GAP_TABLE.md` row 8 and the `CPU_TIER_INDEPENDENT_ORACLE` line). RELU: no E1 op; tensor-layer bit select with no float arithmetic (`src/tensor/omega_tensor.c` `relu_bits`): x > +0 (incl. +subnormals, +inf) -> x; +0, -0, negatives, -inf -> +0.0; any NaN -> canonical qNaN `OMEGA_QNAN_BITS` 0x7fc00000 (`src/omega_numeric.h:48`, as E1 returns, e.g. `src/omega_numeric.c:459`). Tests: section 7b runs EXP / LOG / RELU raw-bit vs direct E1 (RELU vs an independent field-classifying reference) on dense, transposed, strided-slice, transpose-of-slice, broadcast row/scalar and rank-0 views incl. specials (+-0, +-inf, NaN payloads, subnormals, +-2^31, exp cutoffs 88.7228 / -104); section 7c: RELU explicit bit table (-0 -> +0, NaN payloads -> 0x7fc00000), served without a `transc` entry, F16 / stale refused. Mutants `TRANSC_EXP_LOG_SWAP`, `RELU_NEG_ZERO`, `RELU_NAN_PAYLOAD` | | GB10 EXP / LOG tensor ops | MISSING_IMPLEMENTATION | E1 itself has no GB10 kernel for the Omega exp / log polynomials (`src/omega_numeric.c:585-588`, `gb10_encoded` false (`src/omega_numeric.h:250`); `docs/numeric/E1_GAP_TABLE.md` row 10) | | 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 | | 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.c b/src/tensor/omega_tensor.c index e13576c98..478646750 100644 --- a/src/tensor/omega_tensor.c +++ b/src/tensor/omega_tensor.c @@ -706,7 +706,7 @@ int omega_tensor_unary(OmegaTensorCtx *ctx, OmegaTensorUnaryOp op, OmegaTensor a size_t n = (size_t)in.elements; float *src = malloc(n * sizeof(float)); if (!src) return OMEGA_TENSOR_ERR_CAPACITY; - gather(s, &in, src); /* MUT:UNARY_VIEW_STRIDE */ + gather(s, &in, src); /* MUT:UNARY_VIEW_STRIDE MUT:GELU_VIEW_STRIDE */ void *buf; rc = new_dense(ctx, OMEGA_DT_F32, in.rank, in.shape, out, &buf); if (!rc) { diff --git a/src/tensor/omega_tensor.h b/src/tensor/omega_tensor.h index e29b553b9..a54d16df3 100644 --- a/src/tensor/omega_tensor.h +++ b/src/tensor/omega_tensor.h @@ -125,6 +125,11 @@ typedef enum { OMEGA_TU_EXP, /* omega_math_exp, E1 polynomial */ OMEGA_TU_LOG, /* omega_math_log, E1 polynomial */ OMEGA_TU_RELU, /* bit select in the tensor layer (needs no transc) */ + /* LT-M21 CR-1 (unary2), same pattern: E1 sequence per element, bit-exact + * with calling it directly. Served by the realization's transc entry. */ + 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 */ OMEGA_TU_COUNT } OmegaTensorUnaryOp; @@ -169,8 +174,8 @@ typedef struct { float *out, size_t n); 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), bit-exact - * with the E1 CPU sequences. + * (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. * 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 8fe8e5e34..6bc0d3c3f 100644 --- a/src/tensor/omega_tensor_cpu.c +++ b/src/tensor/omega_tensor_cpu.c @@ -20,6 +20,8 @@ static float (*const TRANSC_FN[OMEGA_TU_COUNT])(float) = { [OMEGA_TU_TANH] = omega_math_tanh, /* E1 Omega-defined polynomials, src/omega_numeric.c:458 and :501 (cut ops) */ [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, }; static int cpu_transc(OmegaTensorUnaryOp op, const float *a, float *out, size_t n) { diff --git a/tests/test_omega_tensor.c b/tests/test_omega_tensor.c index c3c44fe32..1435bfbdb 100644 --- a/tests/test_omega_tensor.c +++ b/tests/test_omega_tensor.c @@ -780,6 +780,9 @@ static const TranscCase TRANSC_CASES[] = { * :501), not correctly rounded, bit-exact with the direct call. */ {OMEGA_TU_EXP, omega_math_exp, "EXP"}, {OMEGA_TU_LOG, omega_math_log, "LOG"}, + {OMEGA_TU_RSQRT, omega_math_rsqrt, "RSQRT"}, + {OMEGA_TU_ERF, omega_math_erf, "ERF"}, + {OMEGA_TU_GELU, omega_math_gelu, "GELU"}, }; #define NTRANSC (sizeof(TRANSC_CASES) / sizeof(TRANSC_CASES[0])) @@ -1075,6 +1078,80 @@ static void test_relu_unary(void) { } } +/* ---- 7c. RSQRT / ERF / GELU special values (LT-M21 CR-1, unary2) --------- + * Edge inputs from docs/numeric/E1_TRANSCENDENTAL_CONTRACT.md (RSQRT row 45: + * +0 -> +inf, -0 -> -inf, x < 0 -> NaN, +inf -> +0, subnormals; ERF row 48: + * |x| >= 4 -> +-1, +-0 -> +-0, seam at 0.5; GELU row 50: x >= 8 -> x, + * x < -15.5 -> -0, +-0 -> +-0). Every op runs on a dense tensor and on a + * step-2 strided slice with offset 1 (odd positions only hold the specials), + * compared bit for bit with direct E1 calls; then the contract-stated values + * are checked on the tensor output. */ +static const uint32_t U2_SPECIALS[] = { + 0x00000000U, 0x80000000U, 0x7f800000U, 0xff800000U, 0x7fc00000U, 0xffc00123U, 0x7fa00001U, + 0x00000001U, 0x80000001U, 0x007fffffU, 0x00800000U, 0x7f7fffffU, 0xbf800000U, 0x40800000U, + 0x40800000U ^ 0x80000000U, 0x407fffffU, 0xc07fffffU, 0x3f000000U, 0xbf000000U, 0x3effffffU, + 0x41000000U, 0x40ffffffU, 0xc1780000U, 0xc1780001U, 0xc177ffffU, 0x40000000U, 0x40800001U, + 0x3f800000U, 0x3fb504f3U, 0xc0000000U, +}; +#define NU2 (sizeof(U2_SPECIALS) / sizeof(U2_SPECIALS[0])) + +static uint32_t u2_out(OmegaTensorUnaryOp op, uint32_t xbits) { + float x = omega_bits_to_float(xbits); + OmegaTensor X = mk(0, NULL, &x), O = {0, 0}; + uint32_t r = 0xdeadbeefU; + if (!omega_tensor_unary(g, op, X, &O)) { + float *got = rd(O); + if (got) r = omega_float_to_bits(got[0]); + free(got); + omega_tensor_release(g, O); + } + omega_tensor_release(g, X); + return r; +} + +static void test_unary2_specials(void) { + float dense[NU2], wide[2 * NU2 + 1]; + size_t idx[NU2]; + for (size_t i = 0; i < NU2; i++) dense[i] = omega_bits_to_float(U2_SPECIALS[i]); + for (size_t i = 0; i < 2 * NU2 + 1; i++) wide[i] = 1234.5f; /* filler at even positions */ + for (size_t i = 0; i < NU2; i++) wide[1 + 2 * i] = dense[i]; + uint64_t sd[1] = {NU2}, sw[1] = {2 * NU2 + 1}, st[1] = {1}, sp[1] = {2 * NU2 + 1}, se[1] = {2}; + OmegaTensor D = mk(1, sd, dense), W = mk(1, sw, wide), S = {0, 0}; + int rcs = omega_tensor_slice(g, W, st, sp, se, &S); + CHECK(rcs == 0, "unary2: step-2 slice rc=%d", rcs); + for (size_t k = 0; k < NTRANSC; k++) { + const TranscCase *tc = &TRANSC_CASES[k]; + if (tc->op != OMEGA_TU_RSQRT && tc->op != OMEGA_TU_ERF && tc->op != OMEGA_TU_GELU) continue; + for (size_t i = 0; i < NU2; i++) idx[i] = i; + transc_check(tc, D, dense, idx, NU2, "special values dense"); + for (size_t i = 0; i < NU2; i++) idx[i] = 1 + 2 * i; + if (!rcs) transc_check(tc, S, wide, idx, NU2, "special values step-2 slice"); + } + struct { OmegaTensorUnaryOp op; uint32_t x, y; const char *what; } want[] = { + {OMEGA_TU_RSQRT, 0x00000000U, 0x7f800000U, "RSQRT(+0) = +inf"}, + {OMEGA_TU_RSQRT, 0x80000000U, 0xff800000U, "RSQRT(-0) = -inf"}, + {OMEGA_TU_RSQRT, 0xbf800000U, 0x7fc00000U, "RSQRT(-1) = canonical qNaN"}, + {OMEGA_TU_RSQRT, 0x7f800000U, 0x00000000U, "RSQRT(+inf) = +0"}, + {OMEGA_TU_RSQRT, 0x40800000U, 0x3f000000U, "RSQRT(4) = 0.5 exact"}, + {OMEGA_TU_ERF, 0x80000000U, 0x80000000U, "ERF(-0) = -0"}, + {OMEGA_TU_ERF, 0x40800000U, 0x3f800000U, "ERF(4) = +1"}, + {OMEGA_TU_ERF, 0xff800000U, 0xbf800000U, "ERF(-inf) = -1"}, + {OMEGA_TU_ERF, 0x7fa00001U, 0x7fc00000U, "ERF(sNaN) = canonical qNaN"}, + {OMEGA_TU_GELU, 0x80000000U, 0x80000000U, "GELU(-0) = -0"}, + {OMEGA_TU_GELU, 0x41000000U, 0x41000000U, "GELU(8) = 8"}, + {OMEGA_TU_GELU, 0x7f800000U, 0x7f800000U, "GELU(+inf) = +inf"}, + {OMEGA_TU_GELU, 0xff800000U, 0x80000000U, "GELU(-inf) = -0"}, + {OMEGA_TU_GELU, 0xc1780001U, 0x80000000U, "GELU(just below -15.5) = -0"}, + }; + for (size_t i = 0; i < sizeof(want) / sizeof(want[0]); i++) { + uint32_t y = u2_out(want[i].op, want[i].x); + CHECK(y == want[i].y, "unary2 contract value %s (got 0x%08x)", want[i].what, y); + } + omega_tensor_release(g, D); + omega_tensor_release(g, W); + if (!rcs) omega_tensor_release(g, S); +} + /* ---- 8. determinism ------------------------------------------------------- */ static void kat_digest(uint8_t out[32]) { uint64_t save = g_rng; @@ -1121,6 +1198,7 @@ int main(void) { test_mutations(); test_transc_unary(); test_relu_unary(); + test_unary2_specials(); 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 27227f5d5..4e3962eab 100755 --- a/tools/tensor_mutations.sh +++ b/tools/tensor_mutations.sh @@ -24,7 +24,9 @@ TRANSC_EXP2_LOG2_SWAP|omega_tensor_cpu.c|s/= omega_math_exp2, \(.*\)= omega_math UNARY_VIEW_STRIDE|omega_tensor.c|s/gather(s, .in, src);/dense_strides(in.rank, in.shape, in.strides); gather(s, \&in, src);/ TRANSC_EXP_LOG_SWAP|omega_tensor_cpu.c|s/= omega_math_exp, \(.*\)= omega_math_log,/= omega_math_log, \1= omega_math_exp,/ 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;/' +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);/' # Baseline: the unmutated build must PASS, otherwise every mutant would # look "caught" (e.g. the test refusing an unsuitable FP environment).