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_c3_ab_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 | * C3 A/B harness for the reduce and ldst launchers (E1 gap table; omega PR #198). |
| 19 | * Sibling of tests/test_omega_unwritten_trap_gb10.c (the #201 harness, left unchanged): |
| 20 | * same output lines, different launchers under test. |
| 21 | * |
| 22 | * --chip [--repeats N] [--sizes a,b,c] [--ops SUM,MAX,MIN,MEAN,LDST] [--seed S] |
| 23 | * --arm GPU build only: prints "arm=<control|fix> c3_protect=<OFF|ON>" and exits 0, no device work |
| 24 | * Exit: 0 = clean, 1 = hits or device errors, 2 = refused / NOT_RUN. |
| 25 | * Defaults: --repeats 200, --sizes 1000003,65, --ops SUM,MAX,MEAN,LDST, --seed 0. |
| 26 | * |
| 27 | * Arms. The launchers src/omega_numeric_reduce_gb10.c (run_chunk) and src/omega_numeric_ldst_gb10.c |
| 28 | * (omega_ldst_gb10_run) carry the C3 protection of omega_ds_gb10_run (L2_FLUSH_DIRTY mem-op plus a second |
| 29 | * release marker 0x46464646 at marker_mem+0x10, waited for before every readback). Building every source |
| 30 | * with -DOMEGA_C3_PROTECT_OFF compiles that protection out of those two launchers (the control arm); the |
| 31 | * default build keeps it (the fix arm). The define does not touch omega_ds_gb10_run, so the MEAN final |
| 32 | * division is protected in both arms. The arm is printed on the start line. |
| 33 | * |
| 34 | * What counts as a hit (one OMEGA_UNWRITTEN_HIT line each, summed into OMEGA_UNWRITTEN_TRAP: <hits>/<runs>): |
| 35 | * reduce ops the call returned OMEGA_NUMERIC_OK and the result bits differ from omega_reduce_reference. |
| 36 | * Inputs are finite with |x| in [2^-15, 1), so every legitimate sum is below 2^20 and every |
| 37 | * legitimate max/min is below 1. run_chunk fills the device output with 0x55 bytes |
| 38 | * (src/omega_numeric_reduce_gb10.c, memset of out_mem), about 1.5e13 as a float. A result |
| 39 | * with biased exponent >= 0xA0 is labelled kind=poison, any other difference kind=wrong. |
| 40 | * The label is only a hint: every difference is a hit, and a poisoned MEAN is divided |
| 41 | * by n on the chip, so it is labelled wrong. |
| 42 | * LDST a B32 load, B32 store copy of n words (host in -> GPU load -> GPU store -> host out) |
| 43 | * returned OK and out differs from in anywhere. The host output is pre-filled with 0x55555555 |
| 44 | * (the launcher copies it to the device first) and no input word equals it, so a surviving |
| 45 | * 0x55555555 is an unwritten word (kind=poison). |
| 46 | * A call that returns OMEGA_NUMERIC_ERR_DEVICE (-4) is NOT a hit: it is a device error (OMEGA_UNWRITTEN_DEVERR |
| 47 | * line, counted in device_errors; the launcher also prints its own step line on stderr: GB10_DEVFAIL (#180) for reduce |
| 48 | * and divsqrt, OMEGA_DEVERR for ldst). |
| 49 | * Any other non-OK code means the launcher refused the input before touching the device: the harness prints |
| 50 | * OMEGA_UNWRITTEN_REFUSED and OMEGA_UNWRITTEN_TRAP: NOT_RUN and exits 2. |
| 51 | * The harness stops early only after 3 device errors in a row (a jammed seat), never on a single one. |
| 52 | * |
| 53 | * Never run this directly: chip runs go through the forge queue and are never killed or timed out. |
| 54 | */ |
| 55 | #include "omega_numeric.h" |
| 56 | #include "omega_numeric_divsqrt_gb10.h" |
| 57 | #include "omega_numeric_ldst_gb10.h" |
| 58 | #include "omega_numeric_reduce.h" |
| 59 | #include "omega_unwritten_trap.h" |
| 60 | |
| 61 | #include <inttypes.h> |
| 62 | #include <stdio.h> |
| 63 | #include <stdlib.h> |
| 64 | #include <string.h> |
| 65 | #include <time.h> |
| 66 | #include <unistd.h> |
| 67 | |
| 68 | #ifdef OMEGA_NUMERIC_CPU_ONLY |
| 69 | int main(void) { |
| 70 | printf("OMEGA_UNWRITTEN_TRAP: NOT_RUN (CPU-only build)\n"); |
| 71 | return 2; |
| 72 | } |
| 73 | |
| 74 | #else /* GB10 build */ |
| 75 | |
| 76 | #define POISON 0x55555555u |
| 77 | #define MAX_SIZES 8 |
| 78 | #define STOP_AFTER_CONSECUTIVE_DEVERR 3 |
| 79 | |
| 80 | #ifdef OMEGA_C3_PROTECT_OFF |
| 81 | #define ARM_NAME "control" |
| 82 | #define PROTECT_STATE "OFF" |
| 83 | #else |
| 84 | #define ARM_NAME "fix" |
| 85 | #define PROTECT_STATE "ON" |
| 86 | #endif |
| 87 | |
| 88 | typedef struct { |
| 89 | const char *name; |
| 90 | int ldst; /* 1: the ldst round trip, 0: a reduction */ |
| 91 | OmegaReduceOp rop; |
| 92 | } TestOp; |
| 93 | |
| 94 | static const TestOp ALL_OPS[] = { |
| 95 | { "SUM", 0, OMEGA_RED_SUM }, { "MAX", 0, OMEGA_RED_MAX }, { "MIN", 0, OMEGA_RED_MIN }, |
| 96 | { "MEAN", 0, OMEGA_RED_MEAN }, { "LDST", 1, OMEGA_RED_SUM }, |
| 97 | }; |
| 98 | #define N_ALL_OPS (sizeof(ALL_OPS) / sizeof(ALL_OPS[0])) |
| 99 | |
| 100 | static uint64_t g_rng = 0x9e3779b97f4a7c15ull; |
| 101 | static uint64_t rnd(void) { |
| 102 | g_rng ^= g_rng << 13; g_rng ^= g_rng >> 7; g_rng ^= g_rng << 17; |
| 103 | return g_rng; |
| 104 | } |
| 105 | |
| 106 | static uint32_t f2b(float f) { uint32_t b; memcpy(&b, &f, 4); return b; } |
| 107 | static float b2f(uint32_t b) { float f; memcpy(&f, &b, 4); return f; } |
| 108 | |
| 109 | /* finite, |x| in [2^-15, 1): biased exponent 0x70..0x7e, random sign and mantissa */ |
| 110 | static float gen_small(void) { |
| 111 | uint32_t r = (uint32_t)rnd(); |
| 112 | return b2f((r & 0x807fffffu) | ((0x70u + ((r >> 23) % 15u)) << 23)); |
| 113 | } |
| 114 | |
| 115 | static void utc_now(char *buf, size_t cap) { |
| 116 | time_t t = time(NULL); |
| 117 | struct tm tm; |
| 118 | gmtime_r(&t, &tm); |
| 119 | strftime(buf, cap, "%Y-%m-%dT%H:%M:%SZ", &tm); |
| 120 | } |
| 121 | |
| 122 | static int parse_sizes(const char *s, size_t *out, int max) { |
| 123 | int n = 0; |
| 124 | while (*s && n < max) { |
| 125 | char *e; |
| 126 | unsigned long v = strtoul(s, &e, 10); |
| 127 | if (e == s || v == 0 || v > OMEGA_REDUCE_MEAN_MAX_N) return -1; |
| 128 | out[n++] = (size_t)v; |
| 129 | if (*e && *e != ',') return -1; |
| 130 | s = *e == ',' ? e + 1 : e; |
| 131 | } |
| 132 | return *s ? -1 : n; |
| 133 | } |
| 134 | |
| 135 | static int parse_pos_long(const char *s, long *out) { |
| 136 | char *e; |
| 137 | long v = strtol(s, &e, 10); |
| 138 | if (e == s || *e || v <= 0) return -1; |
| 139 | *out = v; |
| 140 | return 0; |
| 141 | } |
| 142 | |
| 143 | static int parse_ops(const char *arg, const TestOp **out, int max) { |
| 144 | char buf[128]; |
| 145 | if (strlen(arg) >= sizeof buf) return -1; |
| 146 | strcpy(buf, arg); |
| 147 | int n = 0; |
| 148 | for (char *tok = strtok(buf, ","); tok; tok = strtok(NULL, ",")) { |
| 149 | const TestOp *found = NULL; |
| 150 | for (size_t i = 0; i < N_ALL_OPS; i++) |
| 151 | if (!strcmp(tok, ALL_OPS[i].name)) found = &ALL_OPS[i]; |
| 152 | if (!found || n >= max) return -1; |
| 153 | out[n++] = found; |
| 154 | } |
| 155 | return n; |
| 156 | } |
| 157 | |
| 158 | #define USAGE "usage: --chip [--repeats N] [--sizes a,b,c] [--ops SUM,MAX,MIN,MEAN,LDST] [--seed S]\n" |
| 159 | |
| 160 | int main(int argc, char **argv) { |
| 161 | setvbuf(stdout, NULL, _IOLBF, 0); |
| 162 | int chip = 0; |
| 163 | long repeats = 200, seed = 0; |
| 164 | size_t sizes[MAX_SIZES] = { 1000003u, 65u }; |
| 165 | int nsizes = 2; |
| 166 | const TestOp *ops[N_ALL_OPS]; |
| 167 | int nops = 0; |
| 168 | ops[nops++] = &ALL_OPS[0]; /* SUM */ |
| 169 | ops[nops++] = &ALL_OPS[1]; /* MAX */ |
| 170 | ops[nops++] = &ALL_OPS[3]; /* MEAN */ |
| 171 | ops[nops++] = &ALL_OPS[4]; /* LDST */ |
| 172 | for (int i = 1; i < argc; i++) { |
| 173 | if (!strcmp(argv[i], "--arm")) { printf("arm=%s c3_protect=%s\n", ARM_NAME, PROTECT_STATE); return 0; } |
| 174 | else if (!strcmp(argv[i], "--chip")) chip = 1; |
| 175 | else if (!strcmp(argv[i], "--repeats") && i + 1 < argc) { |
| 176 | if (parse_pos_long(argv[++i], &repeats) != 0) { printf("bad --repeats\nOMEGA_UNWRITTEN_TRAP: NOT_RUN\n"); return 2; } |
| 177 | } else if (!strcmp(argv[i], "--seed") && i + 1 < argc) { |
| 178 | char *e; |
| 179 | seed = strtol(argv[++i], &e, 10); |
| 180 | if (e == argv[i] || *e) { printf("bad --seed\nOMEGA_UNWRITTEN_TRAP: NOT_RUN\n"); return 2; } |
| 181 | } else if (!strcmp(argv[i], "--sizes") && i + 1 < argc) { |
| 182 | nsizes = parse_sizes(argv[++i], sizes, MAX_SIZES); |
| 183 | if (nsizes <= 0) { printf("bad --sizes (1..%u, at most %d)\nOMEGA_UNWRITTEN_TRAP: NOT_RUN\n", (unsigned)OMEGA_REDUCE_MEAN_MAX_N, MAX_SIZES); return 2; } |
| 184 | } else if (!strcmp(argv[i], "--ops") && i + 1 < argc) { |
| 185 | nops = parse_ops(argv[++i], ops, (int)N_ALL_OPS); |
| 186 | if (nops <= 0) { printf("bad --ops (SUM,MAX,MIN,MEAN,LDST)\nOMEGA_UNWRITTEN_TRAP: NOT_RUN\n"); return 2; } |
| 187 | } else { printf(USAGE); return 2; } |
| 188 | } |
| 189 | if (!chip) { printf(USAGE "OMEGA_UNWRITTEN_TRAP: NOT_RUN\n"); return 2; } |
| 190 | g_rng ^= (uint64_t)seed * 0xd1b54a32d192ed03ull; |
| 191 | if (!g_rng) g_rng = 1; |
| 192 | |
| 193 | size_t maxn = 0; |
| 194 | for (int i = 0; i < nsizes; i++) if (sizes[i] > maxn) maxn = sizes[i]; |
| 195 | float *fx[MAX_SIZES] = { 0 }; |
| 196 | uint32_t *ux[MAX_SIZES] = { 0 }, *o = malloc(maxn * 4); |
| 197 | int alloc_ok = o != NULL; |
| 198 | for (int i = 0; i < nsizes; i++) { |
| 199 | fx[i] = malloc(sizes[i] * sizeof(float)); |
| 200 | ux[i] = malloc(sizes[i] * sizeof(uint32_t)); |
| 201 | alloc_ok &= fx[i] != NULL && ux[i] != NULL; |
| 202 | } |
| 203 | if (!alloc_ok) { printf("out of memory\nOMEGA_UNWRITTEN_TRAP: NOT_RUN\n"); return 2; } |
| 204 | |
| 205 | char start_utc[40]; |
| 206 | utc_now(start_utc, sizeof(start_utc)); |
| 207 | printf("start=%s arm=%s c3_protect=%s repeats_per_op=%ld ops=%d sizes=%d seed=%ld\n", start_utc, ARM_NAME, PROTECT_STATE, |
| 208 | repeats, nops, nsizes, seed); |
| 209 | |
| 210 | const OmegaLdstSpec ldst_spec = { OMEGA_LD_B32, OMEGA_ST_B32, 4, 4, 0, 0 }; |
| 211 | uint64_t hits = 0, runs = 0, dev_errs = 0; |
| 212 | int consecutive = 0; |
| 213 | for (int oi = 0; oi < nops; oi++) { |
| 214 | const TestOp *t = ops[oi]; |
| 215 | uint32_t ref_bits[MAX_SIZES] = { 0 }; |
| 216 | /* inputs per size, fresh per op; the reduce reference is computed once per size */ |
| 217 | for (int si = 0; si < nsizes; si++) { |
| 218 | for (size_t k = 0; k < sizes[si]; k++) { |
| 219 | fx[si][k] = gen_small(); |
| 220 | uint32_t w = (uint32_t)rnd(); |
| 221 | ux[si][k] = w == POISON ? 0x3f800000u : w; |
| 222 | } |
| 223 | if (!t->ldst) { |
| 224 | float r = 0; |
| 225 | if (omega_reduce_reference(t->rop, fx[si], sizes[si], &r) != 0) { |
| 226 | printf("reference failed op=%s size=%zu\nOMEGA_UNWRITTEN_TRAP: NOT_RUN\n", t->name, sizes[si]); |
| 227 | return 2; |
| 228 | } |
| 229 | ref_bits[si] = f2b(r); |
| 230 | } |
| 231 | } |
| 232 | for (long rep = 0; rep < repeats; rep++) { |
| 233 | int si = (int)(rep % nsizes); |
| 234 | size_t n = sizes[si]; |
| 235 | int rc, bad = 0; |
| 236 | uint32_t got_bits = 0; |
| 237 | unsigned launches = 0; |
| 238 | OmegaUnwrittenReport r; |
| 239 | uint64_t mism = 0; |
| 240 | memset(&r, 0, sizeof r); |
| 241 | struct timespec s0, s1; |
| 242 | clock_gettime(CLOCK_MONOTONIC, &s0); |
| 243 | if (t->ldst) { |
| 244 | for (size_t k = 0; k < n; k++) o[k] = POISON; |
| 245 | omega_ldst_gb10_set_spec_id(0); |
| 246 | rc = omega_ldst_gb10_run(&ldst_spec, (const uint8_t *)ux[si], n * 4, (uint8_t *)o, n * 4, 0, n); |
| 247 | } else { |
| 248 | float g = 0; |
| 249 | rc = omega_reduce_gb10(t->rop, fx[si], n, &g); |
| 250 | launches = omega_reduce_gb10_last_launches(); |
| 251 | got_bits = f2b(g); |
| 252 | } |
| 253 | clock_gettime(CLOCK_MONOTONIC, &s1); |
| 254 | runs++; |
| 255 | if (rc != OMEGA_NUMERIC_OK && rc != OMEGA_NUMERIC_ERR_DEVICE) { |
| 256 | /* refused before any device work (bad arguments, operands, FPCR): a harness fault, not a device event */ |
| 257 | printf("OMEGA_UNWRITTEN_REFUSED op=%s run=%ld size=%zu rc=%d arm=%s\n", t->name, rep, n, rc, ARM_NAME); |
| 258 | printf("OMEGA_UNWRITTEN_TRAP: NOT_RUN\n"); |
| 259 | return 2; |
| 260 | } |
| 261 | if (rc != OMEGA_NUMERIC_OK) { |
| 262 | dev_errs++; |
| 263 | consecutive++; |
| 264 | printf("OMEGA_UNWRITTEN_DEVERR op=%s run=%ld size=%zu rc=%d launches=%u arm=%s\n", t->name, rep, n, rc, launches, ARM_NAME); |
| 265 | if (consecutive >= STOP_AFTER_CONSECUTIVE_DEVERR) { |
| 266 | printf("OMEGA_UNWRITTEN_STOP op=%s consecutive_device_errors=%d\n", t->name, consecutive); |
| 267 | goto done; /* a jammed seat: stop, never loop on it */ |
| 268 | } |
| 269 | continue; |
| 270 | } |
| 271 | consecutive = 0; |
| 272 | if (t->ldst) { |
| 273 | omega_unwritten_scan(o, n, POISON, OMEGA_UNWRITTEN_BLOCK, &r); |
| 274 | for (size_t k = 0; k < n; k++) if (o[k] != ux[si][k]) mism++; |
| 275 | bad = mism > 0; |
| 276 | } else { |
| 277 | bad = got_bits != ref_bits[si]; |
| 278 | } |
| 279 | if (bad) { |
| 280 | char now[40]; |
| 281 | long ms = (long)((s1.tv_sec - s0.tv_sec) * 1000 + (s1.tv_nsec - s0.tv_nsec) / 1000000); |
| 282 | utc_now(now, sizeof(now)); |
| 283 | hits++; |
| 284 | if (t->ldst) |
| 285 | printf("OMEGA_UNWRITTEN_HIT op=%s run=%ld size=%zu kind=%s count=%" PRIu64 " mismatches=%" PRIu64 " first=%" PRIu64 |
| 286 | " last=%" PRIu64 " whole_blocks=%d launch_ms=%ld at=%s arm=%s\n", |
| 287 | t->name, rep, n, r.count ? "poison" : "wrong", r.count, mism, r.first, r.last, r.whole_blocks, ms, now, ARM_NAME); |
| 288 | else |
| 289 | printf("OMEGA_UNWRITTEN_HIT op=%s run=%ld size=%zu kind=%s got=0x%08x want=0x%08x launches=%u launch_ms=%ld at=%s arm=%s\n", |
| 290 | t->name, rep, n, ((got_bits >> 23) & 0xffu) >= 0xa0u ? "poison" : "wrong", |
| 291 | got_bits, ref_bits[si], launches, ms, now, ARM_NAME); |
| 292 | } |
| 293 | if ((rep + 1) % 100 == 0) { |
| 294 | printf(" %s %ld/%ld runs, hits so far %" PRIu64 ", device errors so far %" PRIu64 "\n", t->name, rep + 1, repeats, hits, dev_errs); |
| 295 | fflush(stdout); |
| 296 | } |
| 297 | } |
| 298 | } |
| 299 | done: |
| 300 | printf("device_errors=%" PRIu64 "\n", dev_errs); |
| 301 | printf("OMEGA_UNWRITTEN_TRAP: %" PRIu64 "/%" PRIu64 "\n", hits, runs); |
| 302 | for (int i = 0; i < nsizes; i++) { free(fx[i]); free(ux[i]); } |
| 303 | free(o); |
| 304 | return (hits || dev_errs) ? 1 : 0; |
| 305 | } |
| 306 | #endif |