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_tensor_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 | * M20 OMEGA_TENSOR GB10 parity gate (docs/tensor/M20_OMEGA_TENSOR.md). |
| 19 | * |
| 20 | * test_omega_tensor_gb10 host self checks only; opens no device |
| 21 | * test_omega_tensor_gb10 --chip host self checks, then the parity gate |
| 22 | * and the mutant sweep on the GB10. Needs |
| 23 | * GB10_CHIP_RUN=1 in the environment; run |
| 24 | * it through tests/run_tensor_chip.sh |
| 25 | * (lock, pins, receipt), never directly. |
| 26 | * |
| 27 | * Parity: every case runs through the CPU table and the GB10 table from the |
| 28 | * same seeded inputs; both must return OK and give the same value id |
| 29 | * (omega_tensor_value_id: dtype + shape + values, any NaN = canonical NaN, |
| 30 | * -0 != +0). Cases: general matmul (1x1x1, 129x3x257, odd K across the |
| 31 | * 32-lane tile edges, broadcast batches, a transposed view operand, special |
| 32 | * values), reductions with n across warp/tile edges (31, 32, 33, 1023, 1024, |
| 33 | * 1025, 32769) for SUM/MAX/MIN/MEAN, multi-row reductions along both axes, |
| 34 | * elementwise binary/unary/fma/cast over a grid of specials (NaN, -0, inf, |
| 35 | * subnormals) plus random bit patterns, the eleven bounded-contract unary ops |
| 36 | * of the transc entry (EXP2 LOG2 SIGMOID TANH SIN COS ERF GELU RSQRT EXP LOG) |
| 37 | * over the same grid, broadcast elementwise and one call |
| 38 | * longer than a launch chunk. The six CMP_* mask compare ops (CR-3) are NOT |
| 39 | * parity cases: the GB10 table has no compare entry, so a host self check |
| 40 | * requires it to refuse each with OMEGA_TENSOR_ERR_REALIZATION (nothing made, |
| 41 | * no device call), with a counterexample table that does set compare. |
| 42 | * |
| 43 | * Mutants (in-process realization tables over a base table: the CPU table in |
| 44 | * host mode, the GB10 table in chip mode): WRONG_REDUCE_ORDER (rows reduced |
| 45 | * reversed), FFMA_MATMUL (matmul products pairwise contracted with FFMA), |
| 46 | * SWAPPED_OPERAND (a and b exchanged). Each must give a different value id on |
| 47 | * at least one case where both sides returned OK; a mutant that is not caught |
| 48 | * fails the test. No libm, no CUDA. |
| 49 | */ |
| 50 | #include <stdio.h> |
| 51 | #include <stdlib.h> |
| 52 | #include <string.h> |
| 53 | |
| 54 | #include "omega_numeric.h" |
| 55 | #include "omega_numeric_divsqrt_gb10.h" |
| 56 | #include "omega_numeric_reduce.h" |
| 57 | #include "omega_tensor.h" |
| 58 | |
| 59 | static int g_pass, g_fail; |
| 60 | #define CHECK(cond, ...) do { if (cond) g_pass++; else { g_fail++; \ |
| 61 | printf("FAIL %s:%d: ", __FILE__, __LINE__); printf(__VA_ARGS__); printf("\n"); } } while (0) |
| 62 | |
| 63 | /* ---- inputs ---------------------------------------------------------------- */ |
| 64 | static uint64_t g_rng; |
| 65 | static uint64_t rnd(void) { g_rng ^= g_rng << 13; g_rng ^= g_rng >> 7; g_rng ^= g_rng << 17; return g_rng; } |
| 66 | |
| 67 | static const uint32_t SPECIALS[] = { |
| 68 | 0x00000000U, 0x80000000U, 0x7f800000U, 0xff800000U, 0x7fc00000U, 0x7fa00001U, 0xffc00123U, |
| 69 | 0x00000001U, 0x80000001U, 0x007fffffU, 0x00800000U, 0x7f7fffffU, 0xff7fffffU, 0x3f800000U, |
| 70 | 0xbf800000U, 0x3f000000U, 0x4b800000U, 0x33800000U, 0x3effffffU, 0x7f000000U, |
| 71 | }; |
| 72 | #define NSPECIAL (sizeof(SPECIALS) / sizeof(SPECIALS[0])) |
| 73 | |
| 74 | /* any FP32: 1/4 specials, 1/4 small-range normals, 1/2 raw bit patterns */ |
| 75 | static float rand_f32(void) { |
| 76 | uint64_t r = rnd(); |
| 77 | switch (r & 3u) { |
| 78 | case 0: return omega_bits_to_float(SPECIALS[(r >> 8) % NSPECIAL]); |
| 79 | case 1: return omega_bits_to_float((uint32_t)((r >> 32) & 0x807fffffU) | ((uint32_t)(120 + ((r >> 8) % 16)) << 23)); |
| 80 | default: return omega_bits_to_float((uint32_t)(r >> 16)); |
| 81 | } |
| 82 | } |
| 83 | /* finite values over a moderate exponent range (sums keep their bits) */ |
| 84 | static float rand_mod(void) { |
| 85 | uint64_t r = rnd(); |
| 86 | return omega_bits_to_float((uint32_t)((r >> 32) & 0x807fffffU) | ((uint32_t)(110 + ((r >> 8) % 30)) << 23)); |
| 87 | } |
| 88 | |
| 89 | /* ---- cases ----------------------------------------------------------------- */ |
| 90 | typedef enum { K_MM, K_MM_T, K_RED, K_BIN, K_UN, K_FMA, K_CAST, K_BCAST_ADD, K_BCAST_FMA, K_BIG_ADD, K_TRANSC } Kind; |
| 91 | enum { MUT_ORDER = 1u, MUT_FFMA = 2u, MUT_SWAP = 4u }; |
| 92 | |
| 93 | typedef struct { |
| 94 | char name[64]; |
| 95 | Kind kind; |
| 96 | int op; /* reduce / binary op, cast kind */ |
| 97 | int dist; /* 0 rand_mod, 1 rand_f32 */ |
| 98 | uint32_t ra, rb, axis; |
| 99 | uint64_t sa[4], sb[4]; |
| 100 | unsigned mut; /* mutants that run this case */ |
| 101 | } Case; |
| 102 | |
| 103 | #define MAX_CASES 160 |
| 104 | static Case g_cases[MAX_CASES]; |
| 105 | static size_t g_ncases; |
| 106 | |
| 107 | static Case *add_case(const char *name, Kind k, unsigned mut) { |
| 108 | if (g_ncases >= MAX_CASES) { printf("FAIL too many cases\n"); exit(2); } |
| 109 | Case *c = &g_cases[g_ncases++]; |
| 110 | memset(c, 0, sizeof(*c)); |
| 111 | snprintf(c->name, sizeof(c->name), "%s", name); |
| 112 | c->kind = k; |
| 113 | c->mut = mut; |
| 114 | return c; |
| 115 | } |
| 116 | static void mm_case(const char *name, uint32_t ra, const uint64_t *sa, uint32_t rb, const uint64_t *sb, |
| 117 | int dist, unsigned mut) { |
| 118 | Case *c = add_case(name, K_MM, mut); |
| 119 | c->ra = ra; c->rb = rb; c->dist = dist; |
| 120 | memcpy(c->sa, sa, ra * sizeof(uint64_t)); |
| 121 | memcpy(c->sb, sb, rb * sizeof(uint64_t)); |
| 122 | } |
| 123 | |
| 124 | static const char *RED_NAME[OMEGA_TR_COUNT] = { "SUM", "MAX", "MIN", "MEAN" }; |
| 125 | /* Sized to the 20 parity binary ops (everything before OMEGA_TB_CMP_EQ). The CMP_* |
| 126 | * ops are not parity cases (the GB10 table has no compare entry), so no name |
| 127 | * is ever looked up for them and no entry stays NULL. */ |
| 128 | static const char *BIN_NAME[OMEGA_TB_CMP_EQ] = { |
| 129 | "ADD", "SUB", "MUL", "DIV", "MIN", "MAX", "SEL_GE", "SEL_LT", "SEL_LE", "SEL_GT", "SEL_EQ", "SEL_NE", |
| 130 | "SEL_NUM", "SEL_NAN", "SEL_LTU", "SEL_LEU", "SEL_GTU", "SEL_GEU", "SEL_EQU", "SEL_NEU", |
| 131 | }; |
| 132 | |
| 133 | /* The bounded-contract unary ops the GB10 transc entry serves, listed |
| 134 | * independently of the table, with the E1 row-10 kernel name each must run. */ |
| 135 | static const struct { OmegaTensorUnaryOp op; const char *name; } TRANSC_OPS[] = { |
| 136 | { OMEGA_TU_EXP2, "EXP2" }, { OMEGA_TU_LOG2, "LOG2" }, { OMEGA_TU_SIGMOID, "SIGMOID" }, |
| 137 | { OMEGA_TU_TANH, "TANH" }, { OMEGA_TU_SIN, "SIN" }, { OMEGA_TU_COS, "COS" }, |
| 138 | { OMEGA_TU_ERF, "ERF" }, { OMEGA_TU_GELU, "GELU" }, { OMEGA_TU_RSQRT, "RSQRT" }, |
| 139 | { OMEGA_TU_EXP, "EXP" }, { OMEGA_TU_LOG, "LOG" }, |
| 140 | }; |
| 141 | #define NTRANSC (sizeof(TRANSC_OPS) / sizeof(TRANSC_OPS[0])) |
| 142 | |
| 143 | static void build_cases(void) { |
| 144 | char nm[64]; |
| 145 | /* general matmul */ |
| 146 | mm_case("mm_1x1x1", 2, (uint64_t[]){ 1, 1 }, 2, (uint64_t[]){ 1, 1 }, 0, 0); |
| 147 | mm_case("mm_129x3x257", 2, (uint64_t[]){ 129, 3 }, 2, (uint64_t[]){ 3, 257 }, 0, 0); |
| 148 | static const uint64_t KS[] = { 31, 32, 33, 63, 64, 65, 1023, 1024, 1025 }; |
| 149 | for (size_t i = 0; i < sizeof(KS) / sizeof(KS[0]); i++) { |
| 150 | snprintf(nm, sizeof(nm), "mm_2x%llux3", (unsigned long long)KS[i]); |
| 151 | unsigned mut = (KS[i] == 33 || KS[i] == 65) ? (MUT_ORDER | MUT_FFMA) : 0; |
| 152 | mm_case(nm, 2, (uint64_t[]){ 2, KS[i] }, 2, (uint64_t[]){ KS[i], 3 }, 0, mut); |
| 153 | } |
| 154 | mm_case("mm_bcast_3x1x4x5_2x5x6", 4, (uint64_t[]){ 3, 1, 4, 5 }, 3, (uint64_t[]){ 2, 5, 6 }, 0, MUT_FFMA); |
| 155 | mm_case("mm_bcast_2x3x33_33x4", 3, (uint64_t[]){ 2, 3, 33 }, 2, (uint64_t[]){ 33, 4 }, 0, MUT_FFMA); |
| 156 | mm_case("mm_specials_3x33x2", 2, (uint64_t[]){ 3, 33 }, 2, (uint64_t[]){ 33, 2 }, 1, 0); |
| 157 | Case *t = add_case("mm_viewT_5x7x3", K_MM_T, 0); |
| 158 | t->ra = 2; t->sa[0] = 7; t->sa[1] = 5; t->rb = 2; t->sb[0] = 7; t->sb[1] = 3; |
| 159 | /* reductions with n across warp/tile edges */ |
| 160 | static const uint64_t NS[] = { 31, 32, 33, 1023, 1024, 1025, 32769 }; |
| 161 | for (int op = 0; op < OMEGA_TR_COUNT; op++) { |
| 162 | for (size_t i = 0; i < sizeof(NS) / sizeof(NS[0]); i++) { |
| 163 | snprintf(nm, sizeof(nm), "red_%s_n%llu", RED_NAME[op], (unsigned long long)NS[i]); |
| 164 | unsigned mut = (op == OMEGA_TR_SUM && (NS[i] == 33 || NS[i] == 1025)) ? MUT_ORDER : 0; |
| 165 | Case *c = add_case(nm, K_RED, mut); |
| 166 | c->op = op; c->ra = 1; c->sa[0] = NS[i]; |
| 167 | } |
| 168 | for (int s = 0; s < 2; s++) { |
| 169 | snprintf(nm, sizeof(nm), "red_%s_specials_n%d", RED_NAME[op], s ? 1025 : 33); |
| 170 | Case *c = add_case(nm, K_RED, 0); |
| 171 | c->op = op; c->dist = 1; c->ra = 1; c->sa[0] = s ? 1025 : 33; |
| 172 | } |
| 173 | snprintf(nm, sizeof(nm), "red_%s_rows_3x1025_axis1", RED_NAME[op]); |
| 174 | Case *c = add_case(nm, K_RED, 0); |
| 175 | c->op = op; c->ra = 2; c->sa[0] = 3; c->sa[1] = 1025; c->axis = 1; |
| 176 | snprintf(nm, sizeof(nm), "red_%s_cols_1025x3_axis0", RED_NAME[op]); |
| 177 | c = add_case(nm, K_RED, 0); |
| 178 | c->op = op; c->ra = 2; c->sa[0] = 1025; c->sa[1] = 3; c->axis = 0; |
| 179 | } |
| 180 | /* elementwise */ |
| 181 | /* ops before OMEGA_TB_CMP_EQ only: the 20 cases of the original gate. The |
| 182 | * CMP_* ops are refused by the GB10 table, checked in host_checks. */ |
| 183 | for (int op = 0; op < OMEGA_TB_CMP_EQ; op++) { |
| 184 | snprintf(nm, sizeof(nm), "bin_%s", BIN_NAME[op]); |
| 185 | unsigned mut = (op == OMEGA_TB_SUB || op == OMEGA_TB_DIV || op == OMEGA_TB_SEL_LT) ? MUT_SWAP : 0; |
| 186 | Case *c = add_case(nm, K_BIN, mut); |
| 187 | c->op = op; |
| 188 | } |
| 189 | add_case("un_SQRT", K_UN, 0); |
| 190 | for (size_t k = 0; k < NTRANSC; k++) { |
| 191 | snprintf(nm, sizeof(nm), "un_%s", TRANSC_OPS[k].name); |
| 192 | add_case(nm, K_TRANSC, 0)->op = TRANSC_OPS[k].op; |
| 193 | } |
| 194 | add_case("fma_FFMA_V", K_FMA, 0); |
| 195 | static const char *CAST_NAME[4] = { "cast_F32_TO_F16", "cast_F32_TO_BF16", "cast_F16_TO_F32", "cast_BF16_TO_F32" }; |
| 196 | for (int k = 0; k < 4; k++) add_case(CAST_NAME[k], K_CAST, 0)->op = k; |
| 197 | add_case("bcast_add_4x1x3_5x1", K_BCAST_ADD, MUT_SWAP); |
| 198 | add_case("bcast_fma_2x1_1x3_3", K_BCAST_FMA, 0); |
| 199 | add_case("big_add_70001", K_BIG_ADD, 0); |
| 200 | } |
| 201 | |
| 202 | /* specials grid (a, b over all pairs) then random bit patterns */ |
| 203 | #define ELEM_N (NSPECIAL * NSPECIAL + 600u) |
| 204 | static void elem_inputs(float *a, float *b, float *c) { |
| 205 | size_t i = 0; |
| 206 | for (size_t p = 0; p < NSPECIAL; p++) |
| 207 | for (size_t q = 0; q < NSPECIAL; q++, i++) { |
| 208 | a[i] = omega_bits_to_float(SPECIALS[p]); |
| 209 | b[i] = omega_bits_to_float(SPECIALS[q]); |
| 210 | if (c) c[i] = omega_bits_to_float(SPECIALS[(p + q) % NSPECIAL]); |
| 211 | } |
| 212 | for (; i < ELEM_N; i++) { a[i] = rand_f32(); b[i] = rand_f32(); if (c) c[i] = rand_f32(); } |
| 213 | } |
| 214 | |
| 215 | static uint64_t nelem(uint32_t r, const uint64_t *s) { uint64_t n = 1; for (uint32_t i = 0; i < r; i++) n *= s[i]; return n; } |
| 216 | |
| 217 | static int mk(OmegaTensorCtx *ctx, uint32_t rank, const uint64_t *shape, int dist, OmegaTensor *out) { |
| 218 | uint64_t n = nelem(rank, shape); |
| 219 | float *d = malloc((size_t)n * sizeof(float)); |
| 220 | if (!d) return OMEGA_TENSOR_ERR_CAPACITY; |
| 221 | for (uint64_t i = 0; i < n; i++) d[i] = dist ? rand_f32() : rand_mod(); |
| 222 | int rc = omega_tensor_from_f32(ctx, rank, shape, d, out); |
| 223 | free(d); |
| 224 | return rc; |
| 225 | } |
| 226 | |
| 227 | static int run_body(OmegaTensorCtx *ctx, const Case *c, OmegaTensor *out) { |
| 228 | OmegaTensor a = { 0, 0 }, b = { 0, 0 }, x = { 0, 0 }; |
| 229 | int rc; |
| 230 | switch (c->kind) { |
| 231 | case K_MM: |
| 232 | rc = mk(ctx, c->ra, c->sa, c->dist, &a); |
| 233 | if (!rc) rc = mk(ctx, c->rb, c->sb, c->dist, &b); |
| 234 | return rc ? rc : omega_tensor_matmul(ctx, a, b, out); |
| 235 | case K_MM_T: |
| 236 | rc = mk(ctx, c->ra, c->sa, 0, &x); |
| 237 | if (!rc) rc = omega_tensor_transpose(ctx, x, &a); |
| 238 | if (!rc) rc = mk(ctx, c->rb, c->sb, 0, &b); |
| 239 | return rc ? rc : omega_tensor_matmul(ctx, a, b, out); |
| 240 | case K_RED: |
| 241 | rc = mk(ctx, c->ra, c->sa, c->dist, &a); |
| 242 | return rc ? rc : omega_tensor_reduce(ctx, (OmegaTensorReduceOp)c->op, a, c->axis, false, out); |
| 243 | case K_BIN: case K_UN: case K_FMA: case K_TRANSC: { |
| 244 | float *va = malloc(ELEM_N * sizeof(float)), *vb = malloc(ELEM_N * sizeof(float)), |
| 245 | *vc = malloc(ELEM_N * sizeof(float)); |
| 246 | OmegaTensor tc = { 0, 0 }; |
| 247 | uint64_t s = ELEM_N; |
| 248 | rc = (va && vb && vc) ? OMEGA_TENSOR_OK : OMEGA_TENSOR_ERR_CAPACITY; |
| 249 | if (!rc) elem_inputs(va, vb, vc); |
| 250 | if (!rc) rc = omega_tensor_from_f32(ctx, 1, &s, va, &a); |
| 251 | if (!rc) rc = omega_tensor_from_f32(ctx, 1, &s, vb, &b); |
| 252 | if (!rc) rc = omega_tensor_from_f32(ctx, 1, &s, vc, &tc); |
| 253 | free(va); free(vb); free(vc); |
| 254 | if (rc) return rc; |
| 255 | if (c->kind == K_BIN) return omega_tensor_binary(ctx, (OmegaTensorBinaryOp)c->op, a, b, out); |
| 256 | if (c->kind == K_UN) return omega_tensor_unary(ctx, OMEGA_TU_SQRT, a, out); |
| 257 | if (c->kind == K_TRANSC) return omega_tensor_unary(ctx, (OmegaTensorUnaryOp)c->op, a, out); |
| 258 | return omega_tensor_fma(ctx, a, b, tc, out); |
| 259 | } |
| 260 | case K_CAST: { |
| 261 | uint64_t s = ELEM_N; |
| 262 | if (c->op < 2) { |
| 263 | float *va = malloc(ELEM_N * sizeof(float)), *vb = malloc(ELEM_N * sizeof(float)); |
| 264 | rc = (va && vb) ? OMEGA_TENSOR_OK : OMEGA_TENSOR_ERR_CAPACITY; |
| 265 | if (!rc) elem_inputs(va, vb, NULL); |
| 266 | if (!rc) rc = omega_tensor_from_f32(ctx, 1, &s, va, &a); |
| 267 | free(va); free(vb); |
| 268 | return rc ? rc : omega_tensor_cast(ctx, a, c->op == 0 ? OMEGA_DT_F16 : OMEGA_DT_BF16, out); |
| 269 | } |
| 270 | static const uint16_t SP16[] = { 0x0000, 0x8000, 0x7c00, 0xfc00, 0x7e00, 0x7d01, 0x0001, 0x8001, |
| 271 | 0x03ff, 0x0400, 0x7bff, 0x3c00, 0x7f80, 0xff80, 0x7fc0, 0x7f81, |
| 272 | 0x0080, 0x007f }; |
| 273 | uint16_t *h = malloc(ELEM_N * sizeof(uint16_t)); |
| 274 | if (!h) return OMEGA_TENSOR_ERR_CAPACITY; |
| 275 | for (size_t i = 0; i < ELEM_N; i++) |
| 276 | h[i] = i < sizeof(SP16) / sizeof(SP16[0]) ? SP16[i] : (uint16_t)(rnd() >> 24); |
| 277 | OmegaDType dt = c->op == 2 ? OMEGA_DT_F16 : OMEGA_DT_BF16; |
| 278 | rc = omega_tensor_from_data(ctx, dt, 1, &s, h, &a); |
| 279 | free(h); |
| 280 | return rc ? rc : omega_tensor_cast(ctx, a, OMEGA_DT_F32, out); |
| 281 | } |
| 282 | case K_BCAST_ADD: |
| 283 | rc = mk(ctx, 3, (uint64_t[]){ 4, 1, 3 }, 1, &a); |
| 284 | if (!rc) rc = mk(ctx, 2, (uint64_t[]){ 5, 1 }, 1, &b); |
| 285 | return rc ? rc : omega_tensor_binary(ctx, OMEGA_TB_SUB, a, b, out); |
| 286 | case K_BCAST_FMA: { |
| 287 | OmegaTensor tc = { 0, 0 }; |
| 288 | rc = mk(ctx, 2, (uint64_t[]){ 2, 1 }, 1, &a); |
| 289 | if (!rc) rc = mk(ctx, 2, (uint64_t[]){ 1, 3 }, 1, &b); |
| 290 | if (!rc) rc = mk(ctx, 1, (uint64_t[]){ 3 }, 1, &tc); |
| 291 | return rc ? rc : omega_tensor_fma(ctx, a, b, tc, out); |
| 292 | } |
| 293 | case K_BIG_ADD: |
| 294 | rc = mk(ctx, 1, (uint64_t[]){ 70001 }, 1, &a); |
| 295 | if (!rc) rc = mk(ctx, 1, (uint64_t[]){ 70001 }, 1, &b); |
| 296 | return rc ? rc : omega_tensor_binary(ctx, OMEGA_TB_ADD, a, b, out); |
| 297 | } |
| 298 | return OMEGA_TENSOR_ERR_BAD_ARGS; |
| 299 | } |
| 300 | |
| 301 | static uint64_t seed_of(const char *s) { |
| 302 | uint64_t h = 0xcbf29ce484222325ULL; |
| 303 | for (; *s; s++) { h ^= (uint8_t)*s; h *= 0x100000001b3ULL; } |
| 304 | return h ? h : 1; |
| 305 | } |
| 306 | |
| 307 | /* Runs one case through real; status and value id (zeroed unless OK). |
| 308 | * *e1 gets the underlying E1 code of the last numeric failure (0 if none), |
| 309 | * read before the context is destroyed. Diagnostics only. */ |
| 310 | typedef struct { uint8_t *bytes; size_t n, esz; } RawOut; /* output elements, diagnostics only */ |
| 311 | |
| 312 | static size_t dtype_size(OmegaDType d) { return d == OMEGA_DT_F32 ? 4u : 2u; } |
| 313 | |
| 314 | static int run_case(const OmegaTensorRealization *real, const Case *c, uint8_t id[32], int *e1, RawOut *raw) { |
| 315 | OmegaTensorCtx *ctx; |
| 316 | memset(id, 0, 32); |
| 317 | *e1 = 0; |
| 318 | if (raw) { raw->bytes = NULL; raw->n = 0; raw->esz = 0; } |
| 319 | int rc = omega_tensor_ctx_create(64, real, &ctx); |
| 320 | if (rc) return rc; |
| 321 | g_rng = seed_of(c->name); |
| 322 | OmegaTensor out = { 0, 0 }; |
| 323 | rc = run_body(ctx, c, &out); |
| 324 | if (!rc) rc = omega_tensor_value_id(ctx, out, id); |
| 325 | if (!rc && raw) { /* copy the output elements out; a failure here only loses the diagnostics */ |
| 326 | OmegaTensorInfo inf; |
| 327 | if (omega_tensor_info(ctx, out, &inf) == OMEGA_TENSOR_OK && inf.elements > 0) { |
| 328 | size_t esz = dtype_size(inf.dtype), bytes = (size_t)inf.elements * esz; |
| 329 | uint8_t *b = malloc(bytes); |
| 330 | if (b && omega_tensor_read(ctx, out, b, bytes) == OMEGA_TENSOR_OK) { |
| 331 | raw->bytes = b; raw->n = (size_t)inf.elements; raw->esz = esz; |
| 332 | } else { |
| 333 | free(b); |
| 334 | } |
| 335 | } |
| 336 | } |
| 337 | *e1 = omega_tensor_last_numeric_error(ctx); |
| 338 | omega_tensor_ctx_destroy(ctx); |
| 339 | return rc; |
| 340 | } |
| 341 | |
| 342 | static void hex8(const uint8_t id[32], char s[17]) { |
| 343 | for (int i = 0; i < 8; i++) snprintf(s + 2 * i, 3, "%02x", id[i]); |
| 344 | } |
| 345 | |
| 346 | /* ---- DIFFER diagnostics (print only; no check depends on them) ------------- |
| 347 | * The E1 executors pre-fill every output buffer with the byte 0x55 before |
| 348 | * submission (src/omega_numeric_gb10.c:129, src/omega_numeric_reduce_gb10.c:341, |
| 349 | * src/omega_numeric_divsqrt_gb10.c:1077). An output element still holding that |
| 350 | * pattern was most likely read before the chip wrote it. */ |
| 351 | static uint32_t elem_bits(const RawOut *o, size_t i) { |
| 352 | if (o->esz == 4) { uint32_t v; memcpy(&v, o->bytes + i * 4, 4); return v; } |
| 353 | uint16_t h; memcpy(&h, o->bytes + i * 2, 2); return h; |
| 354 | } |
| 355 | |
| 356 | /* TENSOR_GB10_FILL: elements of the output still holding the pre-fill pattern. |
| 357 | * Printed for every DIFFER case, and for any other case where the count is > 0. */ |
| 358 | static void fill_line(const char *name, const char *tag, const RawOut *o, bool always) { |
| 359 | uint32_t pat = o->esz == 4 ? 0x55555555u : 0x5555u; |
| 360 | size_t n = 0; |
| 361 | for (size_t i = 0; i < o->n; i++) n += elem_bits(o, i) == pat; |
| 362 | if (always || n > 0) |
| 363 | printf("TENSOR_GB10_FILL %s count=%zu of %zu pattern=0x%0*x tag=%s\n", name, n, o->n, |
| 364 | (int)(o->esz * 2), (unsigned)pat, tag); |
| 365 | } |
| 366 | |
| 367 | static void diff_detail(const char *name, const char *tag, const RawOut *a, const RawOut *b) { |
| 368 | if (!a->bytes || !b->bytes || a->n != b->n || a->esz != b->esz) { |
| 369 | printf("TENSOR_GB10_FIRST_DIFF %s unavailable (cpu n=%zu esz=%zu, gb10 n=%zu esz=%zu) tag=%s\n", name, |
| 370 | a->n, a->esz, b->n, b->esz, tag); |
| 371 | if (b->bytes) fill_line(name, tag, b, true); |
| 372 | return; |
| 373 | } |
| 374 | size_t first = a->n, last = 0, count = 0; |
| 375 | for (size_t i = 0; i < a->n; i++) |
| 376 | if (elem_bits(a, i) != elem_bits(b, i)) { |
| 377 | if (first == a->n) first = i; |
| 378 | last = i; |
| 379 | count++; |
| 380 | } |
| 381 | if (count == 0) { /* ids differ but bits agree: NaN payloads, which the value id canonicalizes */ |
| 382 | printf("TENSOR_GB10_FIRST_DIFF %s none: 0 differing elements of %zu tag=%s\n", name, a->n, tag); |
| 383 | } else { |
| 384 | int w = (int)(a->esz * 2); |
| 385 | printf("TENSOR_GB10_FIRST_DIFF %s idx=%zu of %zu cpu_bits=0x%0*x gb10_bits=0x%0*x differing=%zu last_idx=%zu tag=%s\n", |
| 386 | name, first, a->n, w, (unsigned)elem_bits(a, first), w, (unsigned)elem_bits(b, first), count, last, tag); |
| 387 | } |
| 388 | fill_line(name, tag, b, true); |
| 389 | } |
| 390 | |
| 391 | typedef struct { size_t cases, both_ok, differ, not_ok; } Cmp; |
| 392 | |
| 393 | /* Reference CPU vs test table over the cases selected by mask (0 = all). */ |
| 394 | static Cmp compare(const OmegaTensorRealization *test, unsigned mask, const char *tag, bool verbose) { |
| 395 | Cmp r = { 0, 0, 0, 0 }; |
| 396 | for (size_t i = 0; i < g_ncases; i++) { |
| 397 | const Case *c = &g_cases[i]; |
| 398 | if (mask && !(c->mut & mask)) continue; |
| 399 | uint8_t ia[32], ib[32]; |
| 400 | int ea, eb; |
| 401 | RawOut oa, ob; |
| 402 | int ra = run_case(omega_tensor_cpu_realization(), c, ia, &ea, &oa); |
| 403 | fprintf(stderr, "GB10_CASE_BEGIN %s %s\n", tag, c->name); /* ties stderr call lines to the case */ |
| 404 | int rb = run_case(test, c, ib, &eb, &ob); |
| 405 | r.cases++; |
| 406 | bool ok = ra == OMEGA_TENSOR_OK && rb == OMEGA_TENSOR_OK; |
| 407 | bool eq = ok && memcmp(ia, ib, 32) == 0; |
| 408 | if (ok) r.both_ok++; else r.not_ok++; |
| 409 | if (ok && !eq) r.differ++; |
| 410 | if (verbose || !eq) { |
| 411 | char ha[17], hb[17]; |
| 412 | hex8(ia, ha); hex8(ib, hb); |
| 413 | printf("%s_CASE %s cpu=%s rc=%d test=%s rc=%d %s", tag, c->name, ha, ra, hb, rb, |
| 414 | eq ? "EQUAL" : ok ? "DIFFER" : "NOT_OK"); |
| 415 | if (!ok) printf(" cpu_e1=%d test_e1=%d", ea, eb); /* diagnostics only */ |
| 416 | printf("\n"); |
| 417 | } |
| 418 | if (ok && !eq) diff_detail(c->name, tag, &oa, &ob); |
| 419 | else if (ob.bytes) fill_line(c->name, tag, &ob, false); |
| 420 | free(oa.bytes); |
| 421 | free(ob.bytes); |
| 422 | } |
| 423 | return r; |
| 424 | } |
| 425 | |
| 426 | /* ---- mutant realizations over a base table ---------------------------------- */ |
| 427 | static const OmegaTensorRealization *g_base; |
| 428 | |
| 429 | static int base_rows(OmegaTensorReduceOp op, const float *x, size_t rows, size_t n, float *out) { |
| 430 | if (g_base->reduce_rows) return g_base->reduce_rows(op, x, rows, n, out); |
| 431 | for (size_t r = 0; r < rows; r++) { |
| 432 | int rc = g_base->reduce(op, x + r * n, n, &out[r]); |
| 433 | if (rc) return rc; |
| 434 | } |
| 435 | return 0; |
| 436 | } |
| 437 | static int pass_elem(OmegaNumericOp op, const float *a, const float *b, const float *c, float *out, size_t n) { |
| 438 | return g_base->elementwise(op, a, b, c, out, n); |
| 439 | } |
| 440 | static int pass_reduce(OmegaTensorReduceOp op, const float *x, size_t n, float *out) { |
| 441 | return base_rows(op, x, 1, n, out); |
| 442 | } |
| 443 | |
| 444 | /* WRONG_REDUCE_ORDER: every row reduced back to front. */ |
| 445 | static int m_order_rows(OmegaTensorReduceOp op, const float *x, size_t rows, size_t n, float *out) { |
| 446 | float *rev = malloc(rows * n * sizeof(float)); |
| 447 | if (!rev) return OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 448 | for (size_t r = 0; r < rows; r++) |
| 449 | for (size_t i = 0; i < n; i++) rev[r * n + i] = x[r * n + n - 1 - i]; |
| 450 | int rc = base_rows(op, rev, rows, n, out); |
| 451 | free(rev); |
| 452 | return rc; |
| 453 | } |
| 454 | static int m_order_reduce(OmegaTensorReduceOp op, const float *x, size_t n, float *out) { |
| 455 | return m_order_rows(op, x, 1, n, out); |
| 456 | } |
| 457 | |
| 458 | /* FFMA_MATMUL: remembers the operands of the last FMUL; a SUM over exactly |
| 459 | * those products is replaced by r[k] = FFMA(a[2k], b[2k], a[2k+1]*b[2k+1]) |
| 460 | * (contracted, one rounding less) and the tree over r. */ |
| 461 | static float *st_a, *st_b; |
| 462 | static size_t st_n; |
| 463 | static int m_ffma_elem(OmegaNumericOp op, const float *a, const float *b, const float *c, float *out, size_t n) { |
| 464 | st_n = 0; |
| 465 | if (op == OMEGA_NOP_FMUL && b) { |
| 466 | float *na = realloc(st_a, n * sizeof(float)), *nb = realloc(st_b, n * sizeof(float)); |
| 467 | if (na) st_a = na; |
| 468 | if (nb) st_b = nb; |
| 469 | if (na && nb) { memcpy(st_a, a, n * sizeof(float)); memcpy(st_b, b, n * sizeof(float)); st_n = n; } |
| 470 | } |
| 471 | return g_base->elementwise(op, a, b, c, out, n); |
| 472 | } |
| 473 | static int m_ffma_rows(OmegaTensorReduceOp op, const float *x, size_t rows, size_t n, float *out) { |
| 474 | if (op != OMEGA_TR_SUM || st_n != rows * n || n < 2) { st_n = 0; return base_rows(op, x, rows, n, out); } |
| 475 | st_n = 0; |
| 476 | size_t h = n / 2, m = (n + 1) / 2; |
| 477 | float *ae = malloc(rows * h * sizeof(float)), *be = malloc(rows * h * sizeof(float)); |
| 478 | float *ao = malloc(rows * h * sizeof(float)), *bo = malloc(rows * h * sizeof(float)); |
| 479 | float *q = malloc(rows * h * sizeof(float)), *f = malloc(rows * h * sizeof(float)); |
| 480 | float *rr = malloc(rows * m * sizeof(float)); |
| 481 | int rc = (ae && be && ao && bo && q && f && rr) ? 0 : OMEGA_NUMERIC_ERR_BAD_ARGS; |
| 482 | if (!rc) { |
| 483 | for (size_t r = 0; r < rows; r++) |
| 484 | for (size_t k = 0; k < h; k++) { |
| 485 | ae[r * h + k] = st_a[r * n + 2 * k]; be[r * h + k] = st_b[r * n + 2 * k]; |
| 486 | ao[r * h + k] = st_a[r * n + 2 * k + 1]; bo[r * h + k] = st_b[r * n + 2 * k + 1]; |
| 487 | } |
| 488 | rc = g_base->elementwise(OMEGA_NOP_FMUL, ao, bo, NULL, q, rows * h); |
| 489 | if (!rc) rc = g_base->elementwise(OMEGA_NOP_FFMA_V, ae, be, q, f, rows * h); |
| 490 | } |
| 491 | if (!rc) { |
| 492 | for (size_t r = 0; r < rows; r++) { |
| 493 | for (size_t k = 0; k < h; k++) rr[r * m + k] = f[r * h + k]; |
| 494 | if (m > h) rr[r * m + h] = x[r * n + n - 1]; |
| 495 | } |
| 496 | rc = base_rows(OMEGA_TR_SUM, rr, rows, m, out); |
| 497 | } |
| 498 | free(ae); free(be); free(ao); free(bo); free(q); free(f); free(rr); |
| 499 | return rc; |
| 500 | } |
| 501 | static int m_ffma_reduce(OmegaTensorReduceOp op, const float *x, size_t n, float *out) { |
| 502 | return m_ffma_rows(op, x, 1, n, out); |
| 503 | } |
| 504 | |
| 505 | /* SWAPPED_OPERAND: a and b exchanged in every two-operand call. */ |
| 506 | static int m_swap_elem(OmegaNumericOp op, const float *a, const float *b, const float *c, float *out, size_t n) { |
| 507 | return b ? g_base->elementwise(op, b, a, c, out, n) : g_base->elementwise(op, a, b, c, out, n); |
| 508 | } |
| 509 | |
| 510 | typedef struct { const char *name; unsigned mask; OmegaTensorRealization table; } Mutant; |
| 511 | static const Mutant MUTANTS[] = { |
| 512 | { "WRONG_REDUCE_ORDER", MUT_ORDER, |
| 513 | { .name = "MUTANT_WRONG_REDUCE_ORDER", .reduce_order = OMEGA_TENSOR_REDUCE_DECLARED_ORDER, |
| 514 | .elementwise = pass_elem, .reduce = m_order_reduce, .reduce_rows = m_order_rows } }, |
| 515 | { "FFMA_MATMUL", MUT_FFMA, |
| 516 | { .name = "MUTANT_FFMA_MATMUL", .reduce_order = OMEGA_TENSOR_REDUCE_DECLARED_ORDER, |
| 517 | .elementwise = m_ffma_elem, .reduce = m_ffma_reduce, .reduce_rows = m_ffma_rows } }, |
| 518 | { "SWAPPED_OPERAND", MUT_SWAP, |
| 519 | { .name = "MUTANT_SWAPPED_OPERAND", .reduce_order = OMEGA_TENSOR_REDUCE_DECLARED_ORDER, |
| 520 | .elementwise = m_swap_elem, .reduce = pass_reduce, .reduce_rows = base_rows } }, |
| 521 | }; |
| 522 | #define NMUTANTS (sizeof(MUTANTS) / sizeof(MUTANTS[0])) |
| 523 | |
| 524 | /* Every mutant over base must differ on >= 1 case where both sides were OK. */ |
| 525 | static size_t run_mutants(const OmegaTensorRealization *base, const char *tag) { |
| 526 | g_base = base; |
| 527 | size_t caught = 0; |
| 528 | for (size_t i = 0; i < NMUTANTS; i++) { |
| 529 | char t[96]; |
| 530 | snprintf(t, sizeof(t), "%s_MUTANT_%s", tag, MUTANTS[i].name); |
| 531 | Cmp r = compare(&MUTANTS[i].table, MUTANTS[i].mask, t, false); |
| 532 | bool ok = r.differ > 0; |
| 533 | caught += ok; |
| 534 | printf("%s: %s (%zu of %zu cases differ, %zu not OK)\n", t, ok ? "caught" : "NOT CAUGHT", |
| 535 | r.differ, r.cases, r.not_ok); |
| 536 | CHECK(ok, "mutant %s not caught over %s", MUTANTS[i].name, base->name); |
| 537 | CHECK(r.not_ok == 0, "mutant %s: %zu cases did not run OK on both sides", MUTANTS[i].name, r.not_ok); |
| 538 | } |
| 539 | free(st_a); free(st_b); st_a = st_b = NULL; st_n = 0; |
| 540 | return caught; |
| 541 | } |
| 542 | |
| 543 | /* ---- host self checks (no device) ------------------------------------------- */ |
| 544 | /* Ops the semantic layer submits (omega_tensor.c), listed independently. */ |
| 545 | static const OmegaNumericOp USED_OPS[] = { |
| 546 | OMEGA_NOP_SQRT, OMEGA_NOP_FFMA_V, OMEGA_NOP_FMUL, OMEGA_NOP_FADD, OMEGA_NOP_FSUB, OMEGA_NOP_DIV, |
| 547 | OMEGA_NOP_FMNMX_MIN, OMEGA_NOP_FMNMX_MAX, OMEGA_NOP_FSETP_SEL, OMEGA_NOP_FSETP_LT_SEL, |
| 548 | OMEGA_NOP_FSETP_LE_SEL, OMEGA_NOP_FSETP_GT_SEL, OMEGA_NOP_FSETP_EQ_SEL, OMEGA_NOP_FSETP_NE_SEL, |
| 549 | OMEGA_NOP_FSETP_NUM_SEL, OMEGA_NOP_FSETP_NAN_SEL, OMEGA_NOP_FSETP_LTU_SEL, OMEGA_NOP_FSETP_LEU_SEL, |
| 550 | OMEGA_NOP_FSETP_GTU_SEL, OMEGA_NOP_FSETP_GEU_SEL, OMEGA_NOP_FSETP_EQU_SEL, OMEGA_NOP_FSETP_NEU_SEL, |
| 551 | OMEGA_NOP_F32_TO_F16, OMEGA_NOP_F32_TO_BF16, OMEGA_NOP_F16_TO_F32, OMEGA_NOP_BF16_TO_F32, |
| 552 | }; |
| 553 | #define NUSED (sizeof(USED_OPS) / sizeof(USED_OPS[0])) |
| 554 | |
| 555 | static bool is_used(OmegaNumericOp op) { |
| 556 | for (size_t i = 0; i < NUSED; i++) if (USED_OPS[i] == op) return true; |
| 557 | return false; |
| 558 | } |
| 559 | |
| 560 | /* The map is right iff every used op names its own encoded E1 op and passes |
| 561 | * the pre-submission check, and every other op is unmapped. Returns the |
| 562 | * number of problems. */ |
| 563 | static int check_map(const char *(*name_of)(OmegaNumericOp), bool verbose) { |
| 564 | static float a[32], b[32], c[32], o[32]; |
| 565 | for (int i = 0; i < 32; i++) { a[i] = 1.0f; b[i] = 2.0f; c[i] = 0.5f; } |
| 566 | int bad = 0; |
| 567 | for (int op = 0; op < OMEGA_NOP_COUNT; op++) { |
| 568 | const char *nm = name_of((OmegaNumericOp)op); |
| 569 | if (!is_used((OmegaNumericOp)op)) { |
| 570 | if (nm) { bad++; if (verbose) printf(" map: op %d not used by the tensor layer but mapped to %s\n", op, nm); } |
| 571 | continue; |
| 572 | } |
| 573 | const OmegaNumericOpInfo *info = nm ? omega_numeric_op_find(nm) : NULL; |
| 574 | if (!info || info->op != (OmegaNumericOp)op || !info->gb10_encoded) { |
| 575 | bad++; |
| 576 | if (verbose) printf(" map: used op %d -> %s is not its own GB10-encoded op\n", op, nm ? nm : "(none)"); |
| 577 | continue; |
| 578 | } |
| 579 | char err[256]; |
| 580 | int rc = omega_numeric_submit_check(nm, a, b, c, o, 32, err, sizeof(err)); |
| 581 | if (rc != OMEGA_NUMERIC_OK) { bad++; if (verbose) printf(" map: %s refused: %s\n", nm, err); } |
| 582 | } |
| 583 | return bad; |
| 584 | } |
| 585 | static const char *mut_map_wrong_op(OmegaNumericOp op) { |
| 586 | return op == OMEGA_NOP_FSUB ? "FADD" : omega_tensor_gb10_op_name(op); |
| 587 | } |
| 588 | static const char *mut_map_exp(OmegaNumericOp op) { |
| 589 | return op == OMEGA_NOP_EXP ? "EXP" : omega_tensor_gb10_op_name(op); |
| 590 | } |
| 591 | static const char *mut_map_uniform_ffma(OmegaNumericOp op) { |
| 592 | return op == OMEGA_NOP_FFMA_V ? "FFMA" : omega_tensor_gb10_op_name(op); |
| 593 | } |
| 594 | |
| 595 | /* The transc map is right iff every listed op names its own row-10 kernel and |
| 596 | * that kernel's host model (omega_ds_host_exec, the instruction list the chip |
| 597 | * runs) equals the CPU table's transc result bit for bit on the specials and |
| 598 | * 512 random patterns, and every other unary op is unmapped. Returns the |
| 599 | * number of problems. Opens no device. */ |
| 600 | static int check_transc_map(int (*kernel_of)(OmegaTensorUnaryOp), bool verbose) { |
| 601 | enum { NIN = NSPECIAL + 512 }; |
| 602 | static float in[NIN], want[NIN]; |
| 603 | uint64_t saved = g_rng; |
| 604 | g_rng = seed_of("transc_map"); |
| 605 | for (size_t i = 0; i < NIN; i++) in[i] = i < NSPECIAL ? omega_bits_to_float(SPECIALS[i]) : rand_f32(); |
| 606 | g_rng = saved; |
| 607 | const OmegaTensorRealization *cpu = omega_tensor_cpu_realization(); |
| 608 | int bad = 0; |
| 609 | for (int op = 0; op < OMEGA_TU_COUNT; op++) { |
| 610 | int k = kernel_of((OmegaTensorUnaryOp)op); |
| 611 | const char *want_name = NULL; |
| 612 | for (size_t t = 0; t < NTRANSC; t++) if ((int)TRANSC_OPS[t].op == op) want_name = TRANSC_OPS[t].name; |
| 613 | if (!want_name) { |
| 614 | if (k >= 0) { bad++; if (verbose) printf(" transc map: unary op %d not served but mapped to kernel %d\n", op, k); } |
| 615 | continue; |
| 616 | } |
| 617 | if (k < 0 || k >= OMEGA_DS_OP_COUNT || strcmp(omega_ds_op_name((OmegaDsOp)k), want_name) != 0) { |
| 618 | bad++; |
| 619 | if (verbose) printf(" transc map: %s -> kernel %d is not its own row-10 kernel\n", want_name, k); |
| 620 | continue; |
| 621 | } |
| 622 | if (cpu->transc((OmegaTensorUnaryOp)op, in, want, NIN) != OMEGA_NUMERIC_OK) { |
| 623 | bad++; if (verbose) printf(" transc map: CPU table refused %s\n", want_name); |
| 624 | continue; |
| 625 | } |
| 626 | size_t diff = 0, first = 0; |
| 627 | for (size_t i = 0; i < NIN; i++) { |
| 628 | uint32_t h = omega_ds_host_exec((OmegaDsOp)k, omega_float_to_bits(in[i]), 0); |
| 629 | if (h != omega_float_to_bits(want[i]) && diff++ == 0) first = i; |
| 630 | } |
| 631 | if (diff) { |
| 632 | bad++; |
| 633 | if (verbose) |
| 634 | printf(" transc map: %s kernel host model differs from the CPU table on %zu of %u inputs (first 0x%08x)\n", |
| 635 | want_name, diff, (unsigned)NIN, omega_float_to_bits(in[first])); |
| 636 | } |
| 637 | } |
| 638 | return bad; |
| 639 | } |
| 640 | static int mut_transc_exp_log(OmegaTensorUnaryOp op) { |
| 641 | return op == OMEGA_TU_EXP ? omega_tensor_gb10_transc_kernel(OMEGA_TU_LOG) |
| 642 | : op == OMEGA_TU_LOG ? omega_tensor_gb10_transc_kernel(OMEGA_TU_EXP) : omega_tensor_gb10_transc_kernel(op); |
| 643 | } |
| 644 | static int mut_transc_relu(OmegaTensorUnaryOp op) { |
| 645 | return op == OMEGA_TU_RELU ? (int)OMEGA_DS_SQRT : omega_tensor_gb10_transc_kernel(op); |
| 646 | } |
| 647 | |
| 648 | /* CR-3 mask compare ops. The GB10 table has no compare entry, so the semantic |
| 649 | * layer must refuse each CMP_* op with OMEGA_TENSOR_ERR_REALIZATION before it |
| 650 | * gathers anything or calls the table: no tensor or storage slot is made, the |
| 651 | * output handle stays zero and no numeric error is recorded (so no device was |
| 652 | * reached). Returns the number of problems. */ |
| 653 | static const OmegaTensorBinaryOp CMP_OPS[] = { |
| 654 | OMEGA_TB_CMP_EQ, OMEGA_TB_CMP_NE, OMEGA_TB_CMP_LT, OMEGA_TB_CMP_LE, OMEGA_TB_CMP_GT, OMEGA_TB_CMP_GE, |
| 655 | }; |
| 656 | #define NCMP_OPS (sizeof(CMP_OPS) / sizeof(CMP_OPS[0])) |
| 657 | |
| 658 | static int check_cmp_refused(const OmegaTensorRealization *real, bool verbose) { |
| 659 | static const uint64_t shape[1] = { 4 }; |
| 660 | static const float da[4] = { 1.0f, 2.0f, 3.0f, 4.0f }, db[4] = { 1.0f, 5.0f, 3.0f, 0.0f }; |
| 661 | OmegaTensorCtx *ctx = NULL; |
| 662 | OmegaTensor a = { 0, 0 }, b = { 0, 0 }; |
| 663 | if (omega_tensor_ctx_create(8, real, &ctx) != OMEGA_TENSOR_OK) { |
| 664 | if (verbose) printf(" cmp: context create refused\n"); |
| 665 | return 1; |
| 666 | } |
| 667 | if (omega_tensor_from_f32(ctx, 1, shape, da, &a) != OMEGA_TENSOR_OK || |
| 668 | omega_tensor_from_f32(ctx, 1, shape, db, &b) != OMEGA_TENSOR_OK) { |
| 669 | if (verbose) printf(" cmp: input tensors not made\n"); |
| 670 | omega_tensor_ctx_destroy(ctx); |
| 671 | return 1; |
| 672 | } |
| 673 | uint32_t t0 = 0, s0 = 0; |
| 674 | omega_tensor_live_counts(ctx, &t0, &s0); |
| 675 | int bad = 0; |
| 676 | for (size_t i = 0; i < NCMP_OPS; i++) { |
| 677 | OmegaTensor out = { 0, 0 }; |
| 678 | int rc = omega_tensor_binary(ctx, CMP_OPS[i], a, b, &out); |
| 679 | uint32_t t1 = 0, s1 = 0; |
| 680 | omega_tensor_live_counts(ctx, &t1, &s1); |
| 681 | if (rc != OMEGA_TENSOR_ERR_REALIZATION) { |
| 682 | bad++; |
| 683 | if (verbose) printf(" cmp: op %d returned %d, expected %d\n", (int)CMP_OPS[i], rc, OMEGA_TENSOR_ERR_REALIZATION); |
| 684 | } |
| 685 | if (t1 != t0 || s1 != s0 || out.slot != 0 || out.generation != 0) { |
| 686 | bad++; |
| 687 | if (verbose) printf(" cmp: op %d made a tensor or storage slot\n", (int)CMP_OPS[i]); |
| 688 | } |
| 689 | if (omega_tensor_last_numeric_error(ctx) != 0) { |
| 690 | bad++; |
| 691 | if (verbose) printf(" cmp: op %d reached a numeric call\n", (int)CMP_OPS[i]); |
| 692 | } |
| 693 | } |
| 694 | omega_tensor_ctx_destroy(ctx); |
| 695 | return bad; |
| 696 | } |
| 697 | |
| 698 | /* counterexample table: the GB10 table WITH a compare entry (a stub that fills +0.0) */ |
| 699 | static int cmp_stub(OmegaNumericOp sel_op, const float *a, const float *b, float *out, size_t n) { |
| 700 | (void)sel_op; (void)a; (void)b; |
| 701 | for (size_t i = 0; i < n; i++) out[i] = 0.0f; |
| 702 | return OMEGA_NUMERIC_OK; |
| 703 | } |
| 704 | |
| 705 | static void host_checks(void) { |
| 706 | const OmegaTensorRealization *g = omega_tensor_gb10_realization(); |
| 707 | CHECK(g && g->name && g->elementwise && g->reduce && g->reduce_rows && g->reduce_order && g->transc, |
| 708 | "GB10 table incomplete"); |
| 709 | CHECK(g && strcmp(g->reduce_order, OMEGA_TENSOR_REDUCE_DECLARED_ORDER) == 0 && |
| 710 | strcmp(g->reduce_order, OMEGA_REDUCE_DECLARED_ORDER) == 0, "GB10 table order string"); |
| 711 | OmegaTensorCtx *ctx = NULL; |
| 712 | CHECK(omega_tensor_ctx_create(8, g, &ctx) == OMEGA_TENSOR_OK, "ctx_create refused the GB10 table"); |
| 713 | omega_tensor_ctx_destroy(ctx); |
| 714 | /* counterexample: the same table with another order string is refused */ |
| 715 | OmegaTensorRealization wrong = *g; |
| 716 | wrong.reduce_order = "SEQUENTIAL_LEFT_TO_RIGHT"; |
| 717 | ctx = NULL; |
| 718 | CHECK(omega_tensor_ctx_create(8, &wrong, &ctx) == OMEGA_TENSOR_ERR_REALIZATION && !ctx, |
| 719 | "GB10 table with a wrong order string accepted"); |
| 720 | |
| 721 | /* op map, and its counterexamples */ |
| 722 | CHECK(check_map(omega_tensor_gb10_op_name, true) == 0, "GB10 op map"); |
| 723 | CHECK(check_map(mut_map_wrong_op, false) > 0, "map check missed FSUB->FADD"); |
| 724 | CHECK(check_map(mut_map_exp, false) > 0, "map check missed EXP (not an elementwise op of the tensor layer)"); |
| 725 | CHECK(check_map(mut_map_uniform_ffma, false) > 0, "map check missed FFMA_V->FFMA (uniform c)"); |
| 726 | |
| 727 | /* refusals before any device is opened: ops the tensor layer's elementwise |
| 728 | * table does not submit (EXP and LOG go through the transc entry) and sizes |
| 729 | * past OMEGA_REDUCE_GB10_MAX_N */ |
| 730 | static const OmegaNumericOp REFUSED[] = { OMEGA_NOP_EXP, OMEGA_NOP_LOG, OMEGA_NOP_FFMA, OMEGA_NOP_MUFU_RCP, |
| 731 | OMEGA_NOP_LDS_STS, OMEGA_NOP_SHFL_DOWN, OMEGA_NOP_REDUCE_SUM }; |
| 732 | float a[4] = { 1.0f, 2.0f, 3.0f, 4.0f }, b[4] = { 1.0f, 1.0f, 1.0f, 1.0f }, o[4]; |
| 733 | for (size_t i = 0; i < sizeof(REFUSED) / sizeof(REFUSED[0]); i++) { |
| 734 | int rc = g->elementwise(REFUSED[i], a, b, b, o, 4); |
| 735 | CHECK(rc == OMEGA_NUMERIC_ERR_NOT_ENCODED, "GB10 table did not refuse op %d (rc %d)", REFUSED[i], rc); |
| 736 | } |
| 737 | CHECK(g->elementwise((OmegaNumericOp)OMEGA_NOP_COUNT, a, b, b, o, 4) == OMEGA_NUMERIC_ERR_NOT_ENCODED, |
| 738 | "GB10 table did not refuse an out-of-range op"); |
| 739 | float r1; |
| 740 | CHECK(g->reduce(OMEGA_TR_SUM, NULL, OMEGA_REDUCE_GB10_MAX_N + 1, &r1) == OMEGA_NUMERIC_ERR_OPERANDS, |
| 741 | "reduce past OMEGA_REDUCE_GB10_MAX_N not refused as OPERANDS"); |
| 742 | CHECK(g->reduce_rows(OMEGA_TR_SUM, NULL, 1, OMEGA_REDUCE_GB10_MAX_N + 1, &r1) == OMEGA_NUMERIC_ERR_OPERANDS, |
| 743 | "reduce_rows past OMEGA_REDUCE_GB10_MAX_N not refused as OPERANDS"); |
| 744 | CHECK(g->reduce((OmegaTensorReduceOp)OMEGA_TR_COUNT, a, 4, &r1) == OMEGA_NUMERIC_ERR_BAD_ARGS, |
| 745 | "unknown reduce op not refused"); |
| 746 | |
| 747 | /* transc entry: map vs the kernels' host models, its counterexamples, and |
| 748 | * refusals of the unary ops it does not serve (before any device call) */ |
| 749 | CHECK(check_transc_map(omega_tensor_gb10_transc_kernel, true) == 0, "GB10 transc map"); |
| 750 | CHECK(check_transc_map(mut_transc_exp_log, false) > 0, "transc map check missed EXP<->LOG"); |
| 751 | CHECK(check_transc_map(mut_transc_relu, false) > 0, "transc map check missed RELU mapped to a kernel"); |
| 752 | static const OmegaTensorUnaryOp TU_REFUSED[] = { OMEGA_TU_SQRT, OMEGA_TU_RELU, OMEGA_TU_NEG }; |
| 753 | for (size_t i = 0; i < sizeof(TU_REFUSED) / sizeof(TU_REFUSED[0]); i++) { |
| 754 | int rc = g->transc(TU_REFUSED[i], a, o, 4); |
| 755 | CHECK(rc == OMEGA_NUMERIC_ERR_NOT_ENCODED, "GB10 transc did not refuse unary op %d (rc %d)", TU_REFUSED[i], rc); |
| 756 | } |
| 757 | CHECK(g->transc((OmegaTensorUnaryOp)OMEGA_TU_COUNT, a, o, 4) == OMEGA_NUMERIC_ERR_NOT_ENCODED, |
| 758 | "GB10 transc did not refuse an out-of-range op"); |
| 759 | CHECK(g->transc(OMEGA_TU_EXP, NULL, o, 4) == OMEGA_NUMERIC_ERR_BAD_ARGS, "GB10 transc took a NULL input"); |
| 760 | |
| 761 | /* CR-3 compare: no compare entry, each CMP_* op refused with ERR_REALIZATION, |
| 762 | * nothing made, no device call; and the counterexample: the same table with |
| 763 | * a compare entry set must fail this very check */ |
| 764 | CHECK(g->compare == NULL, "GB10 table has a compare entry (CMP_* must be refused)"); |
| 765 | CHECK(check_cmp_refused(g, true) == 0, "GB10 table does not refuse the CMP_* ops cleanly"); |
| 766 | OmegaTensorRealization with_cmp = *g; |
| 767 | with_cmp.compare = cmp_stub; |
| 768 | CHECK(check_cmp_refused(&with_cmp, false) > 0, "CMP refusal check missed a table with a compare entry"); |
| 769 | |
| 770 | /* the harness itself, on the CPU: CPU vs CPU equal on every case, and |
| 771 | * every mutant (over the CPU table) caught */ |
| 772 | Cmp self = compare(omega_tensor_cpu_realization(), 0, "HOST_SELF", false); |
| 773 | CHECK(self.cases == g_ncases && self.both_ok == g_ncases && self.differ == 0, |
| 774 | "harness CPU vs CPU: %zu cases, %zu OK, %zu differ", self.cases, self.both_ok, self.differ); |
| 775 | size_t caught = run_mutants(omega_tensor_cpu_realization(), "HOST"); |
| 776 | printf("HOST_HARNESS_MUTANTS: %s (%zu of %zu caught over the CPU table)\n", |
| 777 | caught == NMUTANTS ? "PASS" : "FAIL", caught, NMUTANTS); |
| 778 | } |
| 779 | |
| 780 | int main(int argc, char **argv) { |
| 781 | bool chip = argc == 2 && strcmp(argv[1], "--chip") == 0; |
| 782 | if (argc > 1 && !chip) { printf("usage: %s [--chip]\n", argv[0]); return 2; } |
| 783 | if (!omega_numeric_fpenv_ok()) { printf("FPCR not RNE/no-FTZ: refusing to run\n"); return 2; } |
| 784 | if (chip) { |
| 785 | const char *e = getenv("GB10_CHIP_RUN"); |
| 786 | if (!e || strcmp(e, "1") != 0) { |
| 787 | printf("--chip needs GB10_CHIP_RUN=1 (run through tests/run_tensor_chip.sh)\n"); |
| 788 | return 2; |
| 789 | } |
| 790 | } |
| 791 | build_cases(); |
| 792 | printf("cases: %zu\n", g_ncases); |
| 793 | host_checks(); |
| 794 | int host_fail = g_fail; |
| 795 | printf("TENSOR_GB10_HOST: %s (%d checks, %d failed)\n", host_fail ? "FAIL" : "PASS", g_pass + g_fail, host_fail); |
| 796 | if (!chip) { |
| 797 | printf("M20 Tensor GB10 host self checks: %s (no device opened)\n", g_fail ? "FAIL" : "PASS"); |
| 798 | return g_fail ? 1 : 0; |
| 799 | } |
| 800 | |
| 801 | const OmegaTensorRealization *g = omega_tensor_gb10_realization(); |
| 802 | Cmp p = compare(g, 0, "TENSOR_GB10", true); |
| 803 | bool parity = p.cases == g_ncases && p.both_ok == g_ncases && p.differ == 0; |
| 804 | CHECK(parity, "GB10 parity: %zu cases, %zu OK on both, %zu differ", p.cases, p.both_ok, p.differ); |
| 805 | printf("TENSOR_GB10_PARITY: %s (%zu of %zu cases bit-exact value ids)\n", parity ? "PASS" : "FAIL", |
| 806 | p.both_ok - p.differ, p.cases); |
| 807 | size_t caught = run_mutants(g, "TENSOR_GB10"); |
| 808 | printf("TENSOR_GB10_MUTANTS: %s (%zu of %zu caught on chip)\n", caught == NMUTANTS ? "PASS" : "FAIL", |
| 809 | caught, NMUTANTS); |
| 810 | printf("%d checks, %d failed\n", g_pass + g_fail, g_fail); |
| 811 | printf("M20 Tensor GB10 Verdict: %s\n", g_fail == 0 && parity && caught == NMUTANTS ? "PASS" : "FAIL"); |
| 812 | return g_fail ? 1 : 0; |
| 813 | } |