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
omega-mv / tests / test_omega_numeric_transc_gb10.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | /* |
| 18 | * E1 row 10: GB10 realizations of the frozen transcendental sequences |
| 19 | * (src/omega_numeric_transc.c; natural EXP and LOG: omega_math_exp and |
| 20 | * omega_math_log in src/omega_numeric.c), bit-identical to the CPU tier. |
| 21 | * |
| 22 | * Modes (one per run): |
| 23 | * (none) host tier: each op's kernel builds, passes the |
| 24 | * structural check and carries its recorded digest; the |
| 25 | * host model of the body (src/omega_numeric_divsqrt_gb10.c) |
| 26 | * equals omega_math_<op> on the edge set, on every 4093rd |
| 27 | * 32-bit input and on 10^6 random inputs. No device. |
| 28 | * --host-all OP host model of OP on all 2^32 inputs (16 threads) |
| 29 | * --digest SHA-256 of each transcendental kernel |
| 30 | * --chip [OP ...] GB10: every 2^32 input of each OP (default: all |
| 31 | * transcendental ops), 256 launches of 2^24, each output |
| 32 | * compared bit for bit with omega_math_<op> (NaN results |
| 33 | * included: the CPU tier writes 0x7fc00000). One RESULT |
| 34 | * line per op, then a VERDICT line. |
| 35 | * --host-all and --chip also print "<OP> full-domain digest <hex>" over the |
| 36 | * outputs they produced (model or chip), with the frozen definition of |
| 37 | * tests/test_omega_transc.c: B_c,b = sha256 of the 2^14 FP32 output words |
| 38 | * (little-endian) for inputs c*2^24 + b*2^14 .. + 2^14 - 1 in increasing |
| 39 | * order, C_c = sha256(B_c,0 .. B_c,1023), D = sha256(C_0 .. C_255). The |
| 40 | * chip RESULT line carries it as digest=<hex> (exhaustive runs only). |
| 41 | * Output lines: [PASS] / [FAIL] ID, RESULT ..., VERDICT ... |
| 42 | */ |
| 43 | #include "omega_numeric.h" |
| 44 | #include "omega_numeric_divsqrt_gb10.h" |
| 45 | #include "omega_numeric_transc.h" |
| 46 | #include "omega_blackwell_encoder.h" |
| 47 | |
| 48 | #include <inttypes.h> |
| 49 | #include <pthread.h> |
| 50 | #include <stdio.h> |
| 51 | #include <stdlib.h> |
| 52 | #include <string.h> |
| 53 | |
| 54 | #include "sha256.h" |
| 55 | |
| 56 | static const OmegaDsOp OPS[] = { OMEGA_DS_EXP2, OMEGA_DS_LOG2, OMEGA_DS_SIGMOID, OMEGA_DS_TANH, OMEGA_DS_SIN, OMEGA_DS_COS, OMEGA_DS_ERF, OMEGA_DS_GELU, OMEGA_DS_RSQRT, OMEGA_DS_EXP, OMEGA_DS_LOG }; |
| 57 | #define N_OPS (sizeof(OPS) / sizeof(OPS[0])) |
| 58 | |
| 59 | static int g_pass, g_fail; |
| 60 | static void check(int ok, const char *id) { |
| 61 | if (ok) { g_pass++; printf("[PASS] %s\n", id); } |
| 62 | else { g_fail++; printf("[FAIL] %s\n", id); } |
| 63 | } |
| 64 | |
| 65 | /* The CPU tier the GB10 must equal, as bits. */ |
| 66 | static uint32_t want_bits(OmegaDsOp op, uint32_t a) { |
| 67 | float x = omega_bits_to_float(a), r; |
| 68 | switch (op) { |
| 69 | case OMEGA_DS_EXP2: r = omega_math_exp2(x); break; |
| 70 | case OMEGA_DS_LOG2: r = omega_math_log2(x); break; |
| 71 | case OMEGA_DS_SIGMOID: r = omega_math_sigmoid(x); break; |
| 72 | case OMEGA_DS_TANH: r = omega_math_tanh(x); break; |
| 73 | case OMEGA_DS_SIN: r = omega_math_sin(x); break; |
| 74 | case OMEGA_DS_COS: r = omega_math_cos(x); break; |
| 75 | case OMEGA_DS_ERF: r = omega_math_erf(x); break; |
| 76 | case OMEGA_DS_GELU: r = omega_math_gelu(x); break; |
| 77 | case OMEGA_DS_RSQRT: r = omega_math_rsqrt(x); break; |
| 78 | case OMEGA_DS_EXP: r = omega_math_exp(x); break; |
| 79 | case OMEGA_DS_LOG: r = omega_math_log(x); break; |
| 80 | default: return 0xffffffffu; |
| 81 | } |
| 82 | return omega_float_to_bits(r); |
| 83 | } |
| 84 | |
| 85 | static int find_op(const char *name, OmegaDsOp *op) { |
| 86 | for (size_t i = 0; i < N_OPS; i++) |
| 87 | if (!strcmp(name, omega_ds_op_name(OPS[i]))) { *op = OPS[i]; return 0; } |
| 88 | return -1; |
| 89 | } |
| 90 | |
| 91 | static const uint32_t EDGE_MAG[] = { |
| 92 | 0x00000000u, 0x00000001u, 0x00000002u, 0x00400000u, 0x007fffffu, 0x00800000u, 0x00800001u, |
| 93 | 0x33800000u, 0x34000000u, 0x3effffffu, 0x3f000000u, 0x3f3504f3u, 0x3f3504f4u, 0x3f7fffffu, |
| 94 | 0x3f800000u, 0x3f800001u, 0x3fb504f3u, 0x3fb504f4u, 0x40000000u, 0x4a7fffffu, 0x4a800000u, |
| 95 | 0x42fe0000u, 0x42ffffffu, 0x43000000u, 0x43000001u, 0x43160000u, 0x4316ffffu, 0x43170000u, |
| 96 | 0x43170001u, 0x42fc0000u, 0x42fd0000u, 0x7f7fffffu, 0x7f800000u, 0x7f800001u, 0x7fc00000u, |
| 97 | 0x7fffffffu, |
| 98 | /* EXP: the +-104 and 88.722839 cuts, 2^k scaling branches; LOG: sqrt(2) cut */ |
| 99 | 0x42cffffeu, 0x42cfffffu, 0x42d00000u, 0x42d00001u, 0x42b17217u, 0x42b17218u, 0x42b17219u, |
| 100 | 0x42aeac4fu, 0x42aeac50u, 0x42fc0001u, 0x3fb504f2u, 0x3fb504f5u, 0x3f317217u, 0x3f317218u, |
| 101 | }; |
| 102 | #define N_EDGE_MAG (sizeof(EDGE_MAG) / sizeof(EDGE_MAG[0])) |
| 103 | |
| 104 | static uint64_t g_rng = 0x9e3779b97f4a7c15ull; |
| 105 | static uint64_t rnd(void) { |
| 106 | g_rng ^= g_rng << 13; g_rng ^= g_rng >> 7; g_rng ^= g_rng << 17; |
| 107 | return g_rng; |
| 108 | } |
| 109 | |
| 110 | static int model_ok(OmegaDsOp op, uint32_t a, uint64_t *bad) { |
| 111 | uint32_t got = omega_ds_host_exec(op, a, 0), want = want_bits(op, a); |
| 112 | if (got == want) return 1; |
| 113 | if (*bad < 8) printf(" %s 0x%08x: model 0x%08x cpu 0x%08x\n", omega_ds_op_name(op), a, got, want); |
| 114 | (*bad)++; |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static int host_tier(void) { |
| 119 | char id[160], err[256]; |
| 120 | for (size_t i = 0; i < N_OPS; i++) { |
| 121 | OmegaDsOp op = OPS[i]; |
| 122 | const char *on = omega_ds_op_name(op); |
| 123 | static uint8_t code[OMEGA_DS_MAX_CODE_BYTES]; |
| 124 | size_t len = 0; |
| 125 | OmegaDsInsn body[OMEGA_DS_MAX_BODY]; |
| 126 | size_t nb = omega_ds_body(op, body, OMEGA_DS_MAX_BODY); |
| 127 | snprintf(id, sizeof(id), "TG_%s_BODY_BUILDS (%zu insns)", on, nb); |
| 128 | check(nb > 0, id); |
| 129 | int fp = 0; |
| 130 | for (size_t k = 0; k < nb; k++) fp += body[k].kind == DSK_FADD_R || body[k].kind == DSK_FMUL_R || body[k].kind == DSK_FFMA_R; |
| 131 | snprintf(id, sizeof(id), "TG_%s_BODY_HAS_FP_FORMS (%d FADD/FMUL/FFMA)", on, fp); |
| 132 | check(fp > 0, id); |
| 133 | snprintf(id, sizeof(id), "TG_%s_KERNEL_BUILDS", on); |
| 134 | check(omega_ds_build_kernel(op, code, sizeof(code), &len) == 0, id); |
| 135 | int rc = omega_ds_check_kernel(op, code, len, OMEGA_DS_GPR_COUNT, err, sizeof(err)); |
| 136 | if (rc) printf(" %s\n", err); |
| 137 | snprintf(id, sizeof(id), "TG_%s_KERNEL_PASSES_STRUCTURAL_CHECK", on); |
| 138 | check(rc == OMEGA_NUMERIC_OK, id); |
| 139 | rc = omega_ds_check_digest(op, code, len, err, sizeof(err)); |
| 140 | if (rc) printf(" %s\n", err); |
| 141 | snprintf(id, sizeof(id), "TG_%s_KERNEL_DIGEST_IS_RECORDED", on); |
| 142 | check(rc == OMEGA_NUMERIC_OK, id); |
| 143 | |
| 144 | uint64_t bad = 0, n = 0; |
| 145 | for (size_t k = 0; k < 2 * N_EDGE_MAG; k++, n++) model_ok(op, EDGE_MAG[k / 2] | (k & 1 ? 0x80000000u : 0), &bad); |
| 146 | snprintf(id, sizeof(id), "TG_%s_HOST_MODEL_EDGE_SET (%" PRIu64 " inputs)", on, n); |
| 147 | check(bad == 0, id); |
| 148 | bad = 0; n = 0; |
| 149 | for (uint64_t u = 0; u < (1ull << 32); u += 4093, n++) model_ok(op, (uint32_t)u, &bad); |
| 150 | snprintf(id, sizeof(id), "TG_%s_HOST_MODEL_STRIDE_4093 (%" PRIu64 " inputs)", on, n); |
| 151 | check(bad == 0, id); |
| 152 | bad = 0; |
| 153 | for (n = 0; n < 1000000; n++) model_ok(op, (uint32_t)rnd(), &bad); |
| 154 | snprintf(id, sizeof(id), "TG_%s_HOST_MODEL_RANDOM_1M", on); |
| 155 | check(bad == 0, id); |
| 156 | } |
| 157 | printf("SUMMARY passed=%d failed=%d\n", g_pass, g_fail); |
| 158 | return g_fail ? 1 : 0; |
| 159 | } |
| 160 | |
| 161 | static int digests(void) { |
| 162 | for (size_t i = 0; i < N_OPS; i++) { |
| 163 | static uint8_t code[OMEGA_DS_MAX_CODE_BYTES]; |
| 164 | uint8_t dg[32]; |
| 165 | size_t len = 0; |
| 166 | if (omega_ds_build_kernel(OPS[i], code, sizeof(code), &len) != 0 || |
| 167 | omega_blackwell_compute_code_digest(code, len, dg) != 0) return 1; |
| 168 | printf("%s %zu ", omega_ds_op_name(OPS[i]), len / 16); |
| 169 | for (int k = 0; k < 32; k++) printf("%02x", dg[k]); |
| 170 | printf("\n"); |
| 171 | } |
| 172 | return 0; |
| 173 | } |
| 174 | |
| 175 | /* ---- exhaustive comparisons (threads) ---------------------------------------- */ |
| 176 | |
| 177 | #define T 16 |
| 178 | #define BLOCK_BITS 14u /* digest block: 2^14 inputs */ |
| 179 | static uint8_t g_blk[1u << (32u - BLOCK_BITS)][32]; /* B_c,b, indexed by input >> 14 */ |
| 180 | |
| 181 | typedef struct { |
| 182 | OmegaDsOp op; |
| 183 | uint64_t lo, hi; /* inputs [lo, hi), 2^14-aligned */ |
| 184 | const uint32_t *chip; /* chip outputs for inputs base..; NULL: model */ |
| 185 | uint64_t base; |
| 186 | uint64_t bad, fill; |
| 187 | uint32_t first_in[4], first_got[4], first_want[4]; |
| 188 | } Slice; |
| 189 | |
| 190 | static void *slice_run(void *p) { |
| 191 | Slice *s = p; |
| 192 | static __thread uint8_t buf[4u << BLOCK_BITS]; |
| 193 | for (uint64_t u = s->lo; u < s->hi; u++) { |
| 194 | uint32_t got = s->chip ? s->chip[u - s->base] : omega_ds_host_exec(s->op, (uint32_t)u, 0); |
| 195 | uint32_t want = want_bits(s->op, (uint32_t)u); |
| 196 | uint32_t i = (uint32_t)u & ((1u << BLOCK_BITS) - 1u); |
| 197 | buf[4 * i] = (uint8_t)got; buf[4 * i + 1] = (uint8_t)(got >> 8); |
| 198 | buf[4 * i + 2] = (uint8_t)(got >> 16); buf[4 * i + 3] = (uint8_t)(got >> 24); |
| 199 | if (i == (1u << BLOCK_BITS) - 1u) sha256_hash(buf, sizeof(buf), g_blk[u >> BLOCK_BITS]); |
| 200 | if (got != want) { |
| 201 | if (s->bad < 4) { s->first_in[s->bad] = (uint32_t)u; s->first_got[s->bad] = got; s->first_want[s->bad] = want; } |
| 202 | s->bad++; |
| 203 | s->fill += s->chip && got == 0x55555555u; |
| 204 | } |
| 205 | } |
| 206 | return NULL; |
| 207 | } |
| 208 | |
| 209 | static void digest_of(const uint8_t (*dig)[32], uint32_t n, uint8_t out[32]) { |
| 210 | sha256_ctx c; |
| 211 | sha256_init(&c); |
| 212 | for (uint32_t i = 0; i < n; i++) sha256_update(&c, dig[i], 32); |
| 213 | sha256_final(&c, out); |
| 214 | } |
| 215 | |
| 216 | /* D over g_blk after a full 2^32 pass, as lowercase hex. */ |
| 217 | static void full_domain_digest(char hex[65]) { |
| 218 | static uint8_t ch[256][32]; |
| 219 | uint8_t d[32]; |
| 220 | for (uint32_t c = 0; c < 256; c++) digest_of((const uint8_t (*)[32])g_blk[c << (24u - BLOCK_BITS)], 1u << (24u - BLOCK_BITS), ch[c]); |
| 221 | digest_of((const uint8_t (*)[32])ch, 256, d); |
| 222 | for (int k = 0; k < 32; k++) sprintf(hex + 2 * k, "%02x", d[k]); |
| 223 | } |
| 224 | |
| 225 | /* Compares [lo, hi) with T threads; adds to *bad and *fill. */ |
| 226 | static int compare_range(OmegaDsOp op, uint64_t lo, uint64_t hi, const uint32_t *chip, uint64_t *bad, uint64_t *fill, uint64_t shown) { |
| 227 | pthread_t th[T]; |
| 228 | Slice s[T]; |
| 229 | for (int i = 0; i < T; i++) { |
| 230 | memset(&s[i], 0, sizeof(s[i])); |
| 231 | s[i].op = op; s[i].chip = chip; s[i].base = lo; |
| 232 | s[i].lo = lo + (hi - lo) * i / T; s[i].hi = lo + (hi - lo) * (i + 1) / T; |
| 233 | if (pthread_create(&th[i], NULL, slice_run, &s[i]) != 0) { |
| 234 | for (int j = 0; j < i; j++) pthread_join(th[j], NULL); /* never leave a worker on this stack */ |
| 235 | return -1; |
| 236 | } |
| 237 | } |
| 238 | for (int i = 0; i < T; i++) { |
| 239 | if (pthread_join(th[i], NULL) != 0) return -1; |
| 240 | for (uint64_t k = 0; k < s[i].bad && k < 4 && shown + k < 16; k++) |
| 241 | printf(" %s 0x%08x: %s 0x%08x cpu 0x%08x\n", omega_ds_op_name(op), s[i].first_in[k], |
| 242 | chip ? "chip" : "model", s[i].first_got[k], s[i].first_want[k]); |
| 243 | shown += s[i].bad; |
| 244 | *bad += s[i].bad; *fill += s[i].fill; |
| 245 | } |
| 246 | return 0; |
| 247 | } |
| 248 | |
| 249 | static int host_all(OmegaDsOp op) { |
| 250 | uint64_t bad = 0, fill = 0; |
| 251 | char hex[65]; |
| 252 | omega_ds_host_exec(op, 0, 0); /* fill the body cache before threads */ |
| 253 | if (compare_range(op, 0, 1ull << 32, NULL, &bad, &fill, 0) != 0) { printf("[FAIL] threads\n"); return 2; } |
| 254 | full_domain_digest(hex); |
| 255 | printf("RESULT host %s checked=4294967296 mismatches=%" PRIu64 "\n", omega_ds_op_name(op), bad); |
| 256 | printf("%s full-domain digest %s\n", omega_ds_op_name(op), hex); |
| 257 | return bad ? 1 : 0; |
| 258 | } |
| 259 | |
| 260 | #ifndef OMEGA_NUMERIC_CPU_ONLY |
| 261 | #define BATCH (1u << 24) |
| 262 | static int chip(OmegaDsOp *ops, size_t nops) { |
| 263 | uint32_t *a = malloc((size_t)BATCH * 4), *o = malloc((size_t)BATCH * 4); |
| 264 | if (!a || !o) return 2; |
| 265 | int any_fail = 0, any_notrun = 0; |
| 266 | for (size_t i = 0; i < nops; i++) { |
| 267 | OmegaDsOp op = ops[i]; |
| 268 | uint64_t checked = 0, bad = 0, fill = 0; |
| 269 | int dev_err = 0; |
| 270 | for (uint32_t bi = 0; bi < 256 && !dev_err; bi++) { |
| 271 | for (uint32_t k = 0; k < BATCH; k++) a[k] = (bi << 24) | k; |
| 272 | int rc = omega_ds_gb10_run(op, a, NULL, o, BATCH); |
| 273 | if (rc != OMEGA_NUMERIC_OK) { printf(" %s batch %u: device rc %d\n", omega_ds_op_name(op), bi, rc); dev_err = 1; break; } |
| 274 | if (compare_range(op, (uint64_t)bi << 24, ((uint64_t)bi + 1) << 24, o, &bad, &fill, bad) != 0) { dev_err = 1; break; } |
| 275 | checked += BATCH; |
| 276 | if (bi % 16 == 15) { printf(" %s batch %u/256 done, mismatches so far %" PRIu64 "\n", omega_ds_op_name(op), bi + 1, bad); fflush(stdout); } |
| 277 | } |
| 278 | const char *v = bad ? "FAIL" : (dev_err || checked != (1ull << 32)) ? "NOT_RUN" : "PASS"; |
| 279 | any_fail |= !strcmp(v, "FAIL"); |
| 280 | any_notrun |= !strcmp(v, "NOT_RUN"); |
| 281 | char hex[65] = "none"; |
| 282 | if (checked == (1ull << 32)) { |
| 283 | full_domain_digest(hex); |
| 284 | printf("%s full-domain digest %s\n", omega_ds_op_name(op), hex); |
| 285 | } |
| 286 | printf("RESULT chip %s checked=%" PRIu64 " exhaustive=%s mismatches=%" PRIu64 " unwritten=%" PRIu64 " verdict=%s digest=%s\n", |
| 287 | omega_ds_op_name(op), checked, checked == (1ull << 32) ? "true" : "false", bad, fill, v, hex); |
| 288 | } |
| 289 | const char *v = any_fail ? "FAIL" : any_notrun ? "NOT_RUN" : "PASS"; |
| 290 | printf("VERDICT %s\n", v); |
| 291 | free(a); free(o); |
| 292 | return strcmp(v, "PASS") ? 1 : 0; |
| 293 | } |
| 294 | #endif |
| 295 | |
| 296 | int main(int argc, char **argv) { |
| 297 | setvbuf(stdout, NULL, _IOLBF, 0); |
| 298 | if (argc >= 2 && !strcmp(argv[1], "--digest")) return digests(); |
| 299 | if (argc >= 3 && !strcmp(argv[1], "--host-all")) { |
| 300 | OmegaDsOp op; |
| 301 | if (find_op(argv[2], &op) != 0) { printf("unknown op %s\n", argv[2]); return 2; } |
| 302 | return host_all(op); |
| 303 | } |
| 304 | if (argc >= 2 && !strcmp(argv[1], "--chip")) { |
| 305 | #ifdef OMEGA_NUMERIC_CPU_ONLY |
| 306 | printf("VERDICT NOT_RUN (CPU-only build)\n"); |
| 307 | return 2; |
| 308 | #else |
| 309 | OmegaDsOp ops[N_OPS]; |
| 310 | size_t n = 0; |
| 311 | for (int i = 2; i < argc; i++) { |
| 312 | if (n >= N_OPS || find_op(argv[i], &ops[n]) != 0) { printf("unknown op %s\nVERDICT NOT_RUN\n", argv[i]); return 2; } |
| 313 | n++; |
| 314 | } |
| 315 | if (n == 0) { memcpy(ops, OPS, sizeof(OPS)); n = N_OPS; } |
| 316 | return chip(ops, n); |
| 317 | #endif |
| 318 | } |
| 319 | return host_tier(); |
| 320 | } |