CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / tests / gpu_elementwise_test.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* gpu_elementwise_test: FB-1 cut 4 gate for omega_gpu_elementwise_api. |
| 3 | * |
| 4 | * ./gpu_elementwise_test --host-only refusals, codegen, word fixtures, register budget, |
| 5 | * listing check by Omega's own disassembler |
| 6 | * ./gpu_elementwise_test --out receipt.json chip gate: every op against the host oracle, then its mutant |
| 7 | * (a deliberately wrong kernel) which the same check must catch |
| 8 | * ./gpu_elementwise_test --timing --out t.json cut 4b gate: persistent device, 100 calls each of rmsnorm 1x2048 |
| 9 | * and swiglu 5632 under 0.5 ms median, one device open per process |
| 10 | * |
| 11 | * Oracles re-state aien-sovereign-core crates/aien-inference-abi/src/tensor.rs (main 5fdab70) in C: |
| 12 | * rmsnorm sums in f64, rope computes cos/sin in f64 and casts to f32 before f32 math, swiglu is f64. |
| 13 | * This file is compiled with -ffp-contract=off so the f32 oracle math has no hidden fused multiply-add. |
| 14 | * |
| 15 | * Tolerances (why): EX2 1e-6 rel (PTX ex2.approx.f32: 2^-22.5 rel, we allow 2x). XCHG exact. |
| 16 | * RMSNORM 1e-5 rel + 1e-6 abs (f32 sum of up to 16384 squares against an f64 sum, plus RSQ seed with one |
| 17 | * Newton step; the mutant moves the scale by about 40 percent). ROPE exact (same f32 ops, same order). |
| 18 | * SWIGLU 1e-5 rel + 1e-6 abs (EX2 and RCP approximations, each about 2^-22 rel; the mutant flips the sign |
| 19 | * of the exponent, which is wrong by a factor of e^(2g)). |
| 20 | */ |
| 21 | #include "omega_gpu_elementwise_api.h" |
| 22 | #include "omega_blackwell_codegen.h" |
| 23 | #include "omega_gpu_session.h" |
| 24 | #include "omega_sm121_disasm.h" |
| 25 | #include "out_sha.h" |
| 26 | #include <inttypes.h> |
| 27 | #include <math.h> |
| 28 | #include <stdio.h> |
| 29 | #include <stdlib.h> |
| 30 | #include <string.h> |
| 31 | #include <time.h> |
| 32 | #include <unistd.h> |
| 33 | #include "test_out_file.h" |
| 34 | |
| 35 | static int g_checks, g_failed; |
| 36 | #define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) |
| 37 | |
| 38 | static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } |
| 39 | static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / 16777216.0f); } |
| 40 | |
| 41 | /* ----------------------------------------------------------------- oracles */ |
| 42 | static void oracle_rmsnorm(const float *x, const float *w, float eps, float *out, size_t dim) { |
| 43 | double ss = 0.0; |
| 44 | for (size_t i = 0; i < dim; i++) ss += (double)x[i] * (double)x[i]; |
| 45 | double mean = ss / (double)dim; |
| 46 | float scale = (float)(1.0 / sqrt(mean + (double)eps)); |
| 47 | for (size_t i = 0; i < dim; i++) { float t = x[i] * scale; out[i] = t * w[i]; } |
| 48 | } |
| 49 | static void oracle_rope_tables(size_t head_dim, size_t pos, float theta, float *cos_half, float *sin_half) { |
| 50 | size_t half = head_dim / 2; |
| 51 | for (size_t i = 0; i < half; i++) { |
| 52 | double exponent = (double)(2 * i) / (double)head_dim; |
| 53 | double freq = 1.0 / pow((double)theta, exponent); |
| 54 | double rot = (double)pos * freq; |
| 55 | sin_half[i] = (float)sin(rot); |
| 56 | cos_half[i] = (float)cos(rot); |
| 57 | } |
| 58 | } |
| 59 | static void oracle_rope(const float *v, size_t heads, size_t head_dim, const float *cos_half, const float *sin_half, float *out) { |
| 60 | size_t half = head_dim / 2; |
| 61 | for (size_t h = 0; h < heads; h++) |
| 62 | for (size_t i = 0; i < half; i++) { |
| 63 | size_t i0 = h * head_dim + i, i1 = i0 + half; |
| 64 | float q0 = v[i0], q1 = v[i1], c = cos_half[i], s = sin_half[i]; |
| 65 | float a = q0 * c, b = q1 * s; out[i0] = a - b; |
| 66 | float d = q0 * s, e = q1 * c; out[i1] = d + e; |
| 67 | } |
| 68 | } |
| 69 | static void oracle_swiglu(const float *g, const float *u, float *out, size_t n) { |
| 70 | for (size_t i = 0; i < n; i++) { double gg = g[i]; double silu = gg / (1.0 + exp(-gg)); out[i] = (float)(silu * (double)u[i]); } |
| 71 | } |
| 72 | |
| 73 | /* |got - want| <= rel*|want| + abs; returns the number of violations and the worst scaled error */ |
| 74 | static size_t compare(const float *got, const float *want, size_t n, double rel, double abs_tol, double *worst) { |
| 75 | size_t bad = 0; *worst = 0.0; |
| 76 | for (size_t i = 0; i < n; i++) { |
| 77 | double d = fabs((double)got[i] - (double)want[i]); |
| 78 | double lim = rel * fabs((double)want[i]) + abs_tol; |
| 79 | double scaled = lim > 0 ? d / lim : (d > 0 ? INFINITY : 0.0); |
| 80 | if (scaled > *worst) *worst = scaled; |
| 81 | if (!(d <= lim)) bad++; /* NaN counts as bad */ |
| 82 | } |
| 83 | return bad; |
| 84 | } |
| 85 | |
| 86 | /* ------------------------------------------------------------- host only */ |
| 87 | static int sm121dis_check(OmegaGpuEwOp op, const char *name, const char *const *must, int n_must) { |
| 88 | OmegaBlackwellKernel k; memset(&k, 0, sizeof k); |
| 89 | if (omega_gpu_elementwise_codegen(op, NULL, 0.0f, &k) != OMEGA_GPU_EW_OK) return -1; |
| 90 | char lst[64]; |
| 91 | snprintf(lst, sizeof lst, "/tmp/omega_ew_%s_%d.sass.XXXXXX", name, (int)getpid()); |
| 92 | int fd = mkstemp(lst); /* private, unique listing; read back the same descriptor */ |
| 93 | FILE *f = fd >= 0 ? fdopen(fd, "w+") : NULL; |
| 94 | if (!f) { if (fd >= 0) close(fd); free(k.code); return -1; } |
| 95 | int rc = omega_sm121_listing(k.code, k.code_size, f); |
| 96 | free(k.code); |
| 97 | if (fflush(f) != 0 || fseek(f, 0, SEEK_SET) != 0) { fclose(f); return -1; } |
| 98 | FILE *l = f; |
| 99 | char line[512]; int seen[16] = {0}; int bad = 0, insns = 0; |
| 100 | while (fgets(line, sizeof line, l)) { |
| 101 | if (!strstr(line, "/*")) continue; /* every address line, 0x1000 and up too */ |
| 102 | insns++; |
| 103 | if (strstr(line, "?") || strstr(line, "error")) bad++; |
| 104 | for (int i = 0; i < n_must; i++) if (strstr(line, must[i])) seen[i] = 1; |
| 105 | } |
| 106 | if (ferror(l)) rc = -1; |
| 107 | if (fclose(l) != 0) rc = -1; |
| 108 | int miss = 0; |
| 109 | for (int i = 0; i < n_must; i++) if (!seen[i]) { printf(" %s: sm121dis listing lacks %s\n", name, must[i]); miss++; } |
| 110 | printf("sm121dis %s: rc=%d insns=%d undecodable=%d missing=%d (%s)\n", name, rc, insns, bad, miss, lst); |
| 111 | return (rc == 0 && bad == 0 && miss == 0) ? 0 : -1; |
| 112 | } |
| 113 | |
| 114 | static void host_only(void) { |
| 115 | /* swiglu_131073's tail is one element. Staging the plan's p[10] (524288 bytes) |
| 116 | * wrote past the caller buffer; the chunk's own length is 4. No device. */ |
| 117 | { |
| 118 | size_t b[4] = {0}; |
| 119 | uint32_t nch = 99; |
| 120 | CHECK(omega_gpu_elementwise_test_stage_bytes(131073, b, 4, &nch) == OMEGA_GPU_EW_OK |
| 121 | && nch == 2 && b[0] == 524288u && b[1] == 4u, |
| 122 | "n=131073 stages a 524288-byte chunk then a 4-byte tail (chunks %u %zu %zu)", nch, b[0], b[1]); |
| 123 | CHECK(b[1] != 524288u, "negative control: the tail is not the plan's full-chunk length"); |
| 124 | nch = 99; |
| 125 | CHECK(omega_gpu_elementwise_test_stage_bytes(131072, b, 4, &nch) == OMEGA_GPU_EW_OK |
| 126 | && nch == 1 && b[0] == 524288u, "a full chunk stages its own 524288 bytes (chunks %u %zu)", nch, b[0]); |
| 127 | nch = 99; |
| 128 | CHECK(omega_gpu_elementwise_test_stage_bytes(1, b, 4, &nch) == OMEGA_GPU_EW_OK |
| 129 | && nch == 1 && b[0] == 4u, "one element stages 4 bytes (chunks %u %zu)", nch, b[0]); |
| 130 | CHECK(omega_gpu_elementwise_test_stage_bytes(0, b, 4, &nch) == OMEGA_GPU_EW_BAD_ARGS, "n=0 stage probe refused"); |
| 131 | } |
| 132 | CHECK(omega_blackwell_verify_codegen_fixtures_fb1cut4() == 0, "word fixtures MUFU_EX2 / BAR_SYNC / LDS32 / STS32 (rc %d)", omega_blackwell_verify_codegen_fixtures_fb1cut4()); |
| 133 | float a[256] = {0}, b[256] = {0}, c[256] = {0}; |
| 134 | CHECK(omega_gpu_rmsnorm_f32(0, 128, a, b, 1e-5f, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "rows=0 refused"); |
| 135 | CHECK(omega_gpu_rmsnorm_f32(1, 100, a, b, 1e-5f, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "dim not a multiple of 128 refused"); |
| 136 | CHECK(omega_gpu_rmsnorm_f32(1, OMEGA_GPU_EW_MAX_DIM + 128, a, b, 1e-5f, c, NULL) == OMEGA_GPU_EW_TOO_LARGE, "dim over limit refused"); |
| 137 | CHECK(omega_gpu_rope_f32(1, 63, a, b, b, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "odd head_dim refused"); |
| 138 | CHECK(omega_gpu_rope_f32(1, 64, NULL, b, b, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "NULL vector refused"); |
| 139 | CHECK(omega_gpu_swiglu_f32(0, a, b, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "n=0 refused"); |
| 140 | CHECK(omega_gpu_shared_xchg_u32(100, (uint32_t *)a, (uint32_t *)c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "xchg n not a multiple of 128 refused"); |
| 141 | CHECK(strcmp(omega_gpu_elementwise_rc_name(OMEGA_GPU_EW_UNWRITTEN), "UNWRITTEN") == 0, "rc name"); |
| 142 | |
| 143 | struct { OmegaGpuEwOp op; const char *name; const char *must[6]; int n; } ops[] = { |
| 144 | { OMEGA_GPU_EW_EX2, "ex2", { "MUFU.EX2", "@P0 EXIT", "STG.E" }, 3 }, |
| 145 | { OMEGA_GPU_EW_XCHG, "xchg", { "STS [R", "BAR.SYNC", "LDS R" }, 3 }, |
| 146 | { OMEGA_GPU_EW_RMSNORM, "rmsnorm", { "SHFL.DOWN", "BAR.SYNC", "MUFU.RSQ", "@!P0 BRA", "STS [R", "LDS R" }, 6 }, |
| 147 | { OMEGA_GPU_EW_ROPE, "rope", { "FADD", "FMUL", "STG.E" }, 3 }, |
| 148 | { OMEGA_GPU_EW_SWIGLU, "swiglu", { "MUFU.EX2", "MUFU.RCP", "@P0 EXIT" }, 3 }, |
| 149 | }; |
| 150 | for (size_t i = 0; i < sizeof ops / sizeof ops[0]; i++) { |
| 151 | OmegaBlackwellKernel k; memset(&k, 0, sizeof k); |
| 152 | int rc = omega_gpu_elementwise_codegen(ops[i].op, NULL, 0.0f, &k); |
| 153 | CHECK(rc == OMEGA_GPU_EW_OK && k.code_size > 0 && k.gpr_count <= 64, "%s kernel generated (rc %s, %zu insns, %u gprs)", ops[i].name, omega_gpu_elementwise_rc_name(rc), k.insn_count, k.gpr_count); |
| 154 | printf("codegen %s: %zu insns, %u gprs, %u ugprs\n", ops[i].name, k.insn_count, k.gpr_count, k.uniform_gpr_count); |
| 155 | uint8_t digest[32]; memcpy(digest, k.code_digest, 32); |
| 156 | free(k.code); |
| 157 | /* the mutant must be a different kernel */ |
| 158 | omega_gpu_elementwise_test_set_mutant((int)ops[i].op); |
| 159 | memset(&k, 0, sizeof k); |
| 160 | CHECK(omega_gpu_elementwise_codegen(ops[i].op, NULL, 0.0f, &k) == OMEGA_GPU_EW_OK && memcmp(digest, k.code_digest, 32) != 0, "%s mutant kernel differs", ops[i].name); |
| 161 | free(k.code); |
| 162 | omega_gpu_elementwise_test_set_mutant(0); |
| 163 | CHECK(sm121dis_check(ops[i].op, ops[i].name, ops[i].must, ops[i].n) == 0, "%s sm121dis listing decodes and shows the expected instructions", ops[i].name); |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | /* ------------------------------------------------------------- chip gate */ |
| 168 | typedef struct { const char *name; size_t n; int rc; size_t bad; double worst; int pass; int mutant_rc; size_t mutant_bad; int mutant_caught; uint64_t chip_ns; uint32_t calls; uint32_t unwritten; } Case; |
| 169 | static Case g_cases[16]; static int g_ncases; |
| 170 | |
| 171 | static void record(Case c, FILE *out) { |
| 172 | g_cases[g_ncases++] = c; |
| 173 | CHECK(c.pass, "%s rc=%s violations=%zu worst=%g", c.name, omega_gpu_elementwise_rc_name(c.rc), c.bad, c.worst); |
| 174 | CHECK(c.mutant_caught, "%s mutant caught (rc=%s violations=%zu)", c.name, omega_gpu_elementwise_rc_name(c.mutant_rc), c.mutant_bad); |
| 175 | printf("%s %s n=%zu rc=%s violations=%zu worst_scaled_err=%g chip_ns=%" PRIu64 " calls=%u unwritten=%u | mutant rc=%s violations=%zu %s\n", |
| 176 | c.pass && c.mutant_caught ? "PASS" : "FAIL", c.name, c.n, omega_gpu_elementwise_rc_name(c.rc), c.bad, c.worst, c.chip_ns, c.calls, c.unwritten, |
| 177 | omega_gpu_elementwise_rc_name(c.mutant_rc), c.mutant_bad, c.mutant_caught ? "CAUGHT" : "NOT CAUGHT"); |
| 178 | if (out) fprintf(out, "%s{\"case\":\"%s\",\"n\":%zu,\"rc\":\"%s\",\"violations\":%zu,\"worst_scaled_err\":%g,\"pass\":%s,\"chip_elapsed_ns\":%" PRIu64 ",\"chip_calls\":%u,\"unwritten_words\":%u,\"mutant_rc\":\"%s\",\"mutant_violations\":%zu,\"mutant_caught\":%s}", |
| 179 | g_ncases > 1 ? "," : "", c.name, c.n, omega_gpu_elementwise_rc_name(c.rc), c.bad, c.worst, c.pass ? "true" : "false", c.chip_ns, c.calls, c.unwritten, |
| 180 | omega_gpu_elementwise_rc_name(c.mutant_rc), c.mutant_bad, c.mutant_caught ? "true" : "false"); |
| 181 | } |
| 182 | |
| 183 | static int chip(const char *out_path) { |
| 184 | FILE *out = out_path ? test_out_open(out_path) : NULL; |
| 185 | if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } |
| 186 | if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_ELEMENTWISE_V1\",\"tolerances\":{\"ex2_rel\":1e-6,\"xchg\":\"exact\",\"rmsnorm_rel\":1e-5,\"rmsnorm_abs\":1e-6,\"rope\":\"exact\",\"swiglu_rel\":1e-5,\"swiglu_abs\":1e-6},\"cases\":["); |
| 187 | uint32_t seed = 0xfb1c0004u; |
| 188 | OmegaGpuEwInfo info; |
| 189 | |
| 190 | /* 1. EX2 probe, with a negative control on the comparison itself */ |
| 191 | { |
| 192 | size_t n = 4096; float *x = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4), *wrong = malloc(n * 4); |
| 193 | for (size_t i = 0; i < n; i++) { x[i] = frand(&seed, -20.0f, 20.0f); want[i] = (float)exp2((double)x[i]); wrong[i] = (float)exp2((double)x[i] + 1.0); } |
| 194 | Case c = { .name = "ex2_probe", .n = n }; |
| 195 | c.rc = omega_gpu_ex2_f32((uint32_t)n, x, got, &info); |
| 196 | c.bad = compare(got, want, n, 1e-6, 0.0, &c.worst); |
| 197 | double w2; size_t neg = compare(got, wrong, n, 1e-6, 0.0, &w2); |
| 198 | CHECK(neg > n / 2, "ex2 negative control: the check rejects a wrong expectation (%zu of %zu flagged)", neg, n); |
| 199 | c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; |
| 200 | omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_EX2); |
| 201 | c.mutant_rc = omega_gpu_ex2_f32((uint32_t)n, x, got, &info); |
| 202 | c.mutant_bad = compare(got, want, n, 1e-6, 0.0, &w2); |
| 203 | c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; |
| 204 | omega_gpu_elementwise_test_set_mutant(0); |
| 205 | record(c, out); free(x); free(got); free(want); free(wrong); |
| 206 | } |
| 207 | /* 2. shared-memory exchange probe (STS, BAR.SYNC, LDS) */ |
| 208 | { |
| 209 | size_t n = 4096; uint32_t *x = malloc(n * 4), *got = malloc(n * 4); |
| 210 | for (size_t i = 0; i < n; i++) x[i] = lcg(&seed); |
| 211 | Case c = { .name = "shared_xchg_probe", .n = n }; |
| 212 | c.rc = omega_gpu_shared_xchg_u32((uint32_t)n, x, got, &info); |
| 213 | for (size_t i = 0; i < n; i++) if (got[i] != x[i ^ (OMEGA_GPU_EW_THREADS - 1)]) c.bad++; |
| 214 | c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; |
| 215 | omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_XCHG); |
| 216 | c.mutant_rc = omega_gpu_shared_xchg_u32((uint32_t)n, x, got, &info); |
| 217 | for (size_t i = 0; i < n; i++) if (got[i] != x[i ^ (OMEGA_GPU_EW_THREADS - 1)]) c.mutant_bad++; |
| 218 | c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; |
| 219 | omega_gpu_elementwise_test_set_mutant(0); |
| 220 | record(c, out); free(x); free(got); |
| 221 | } |
| 222 | /* 3. rmsnorm at the model hidden sizes (TinyLlama and Llama-3.2-1B: 2048), plus small and large */ |
| 223 | { size_t dims[3] = { 2048, 256, 8192 }; size_t rowss[3] = { 4, 1, 2 }; |
| 224 | for (int t = 0; t < 3; t++) { |
| 225 | size_t dim = dims[t], rows = rowss[t], n = rows * dim; float eps = 1e-5f; |
| 226 | float *x = malloc(n * 4), *w = malloc(dim * 4), *got = malloc(n * 4), *want = malloc(n * 4), *again = malloc(n * 4); |
| 227 | for (size_t i = 0; i < n; i++) x[i] = frand(&seed, -3.0f, 3.0f); |
| 228 | for (size_t i = 0; i < dim; i++) w[i] = frand(&seed, 0.5f, 1.5f); |
| 229 | for (size_t r = 0; r < rows; r++) oracle_rmsnorm(x + r * dim, w, eps, want + r * dim, dim); |
| 230 | char name[64]; snprintf(name, sizeof name, "rmsnorm_%zux%zu", rows, dim); |
| 231 | Case c = { .name = strdup(name), .n = n }; |
| 232 | c.rc = omega_gpu_rmsnorm_f32((uint32_t)rows, (uint32_t)dim, x, w, eps, got, &info); |
| 233 | if (c.rc == OMEGA_GPU_EW_OK) out_sha_buf(name, got, n * 4); |
| 234 | c.bad = compare(got, want, n, 1e-5, 1e-6, &c.worst); |
| 235 | c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; |
| 236 | OmegaGpuEwInfo i2; int rc2 = omega_gpu_rmsnorm_f32((uint32_t)rows, (uint32_t)dim, x, w, eps, again, &i2); |
| 237 | CHECK(rc2 == OMEGA_GPU_EW_OK && i2.kernel_cache_hit && memcmp(got, again, n * 4) == 0, "%s repeat run (cache hit) is bit-identical", name); |
| 238 | omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_RMSNORM); |
| 239 | double w2; c.mutant_rc = omega_gpu_rmsnorm_f32((uint32_t)rows, (uint32_t)dim, x, w, eps, got, &info); |
| 240 | c.mutant_bad = compare(got, want, n, 1e-5, 1e-6, &w2); |
| 241 | c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; |
| 242 | omega_gpu_elementwise_test_set_mutant(0); |
| 243 | record(c, out); free(x); free(w); free(got); free(want); free(again); |
| 244 | } } |
| 245 | /* 4. rope: TinyLlama (32 q heads, 4 kv heads, head_dim 64, theta 10000) and Llama-3.2-1B (32/8, 64, 500000) */ |
| 246 | { struct { size_t heads, hd, pos; float theta; } rs[4] = { { 32, 64, 17, 10000.0f }, { 4, 64, 17, 10000.0f }, { 32, 64, 1023, 500000.0f }, { 8, 64, 1023, 500000.0f } }; |
| 247 | for (int t = 0; t < 4; t++) { |
| 248 | size_t heads = rs[t].heads, hd = rs[t].hd, n = heads * hd, half = hd / 2; |
| 249 | float *v = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4), *ch = malloc(half * 4), *sh = malloc(half * 4); |
| 250 | for (size_t i = 0; i < n; i++) v[i] = frand(&seed, -4.0f, 4.0f); |
| 251 | oracle_rope_tables(hd, rs[t].pos, rs[t].theta, ch, sh); |
| 252 | oracle_rope(v, heads, hd, ch, sh, want); |
| 253 | char name[64]; snprintf(name, sizeof name, "rope_%zuh_%zud_pos%zu_theta%g", heads, hd, rs[t].pos, rs[t].theta); |
| 254 | Case c = { .name = strdup(name), .n = n }; |
| 255 | c.rc = omega_gpu_rope_f32((uint32_t)heads, (uint32_t)hd, v, ch, sh, got, &info); |
| 256 | c.bad = compare(got, want, n, 0.0, 0.0, &c.worst); |
| 257 | c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; |
| 258 | omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_ROPE); |
| 259 | double w2; c.mutant_rc = omega_gpu_rope_f32((uint32_t)heads, (uint32_t)hd, v, ch, sh, got, &info); |
| 260 | c.mutant_bad = compare(got, want, n, 0.0, 0.0, &w2); |
| 261 | c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; |
| 262 | omega_gpu_elementwise_test_set_mutant(0); |
| 263 | record(c, out); free(v); free(got); free(want); free(ch); free(sh); |
| 264 | } } |
| 265 | /* 5. swiglu at the model ffn sizes (TinyLlama 5632, Llama-3.2-1B 8192), an odd tail, one past the |
| 266 | * 1024-CTA chunk (two launches) and the Qwen3-8B prefill call 128 x 12288 (twelve launches; cut L) */ |
| 267 | { size_t ns[5] = { 5632, 8192, 1000, 131073, 1572864 }; |
| 268 | for (int t = 0; t < 5; t++) { |
| 269 | size_t n = ns[t]; |
| 270 | float *g = malloc(n * 4), *u = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4); |
| 271 | for (size_t i = 0; i < n; i++) { g[i] = frand(&seed, -12.0f, 12.0f); u[i] = frand(&seed, -2.0f, 2.0f); } |
| 272 | oracle_swiglu(g, u, want, n); |
| 273 | char name[64]; snprintf(name, sizeof name, "swiglu_%zu", n); |
| 274 | Case c = { .name = strdup(name), .n = n }; |
| 275 | c.rc = omega_gpu_swiglu_f32((uint32_t)n, g, u, got, &info); |
| 276 | c.bad = compare(got, want, n, 1e-5, 1e-6, &c.worst); |
| 277 | c.pass = c.rc == OMEGA_GPU_EW_OK && c.bad == 0; c.chip_ns = info.elapsed_ns; c.calls = info.chip_calls; c.unwritten = info.unwritten_words; |
| 278 | omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_SWIGLU); |
| 279 | double w2; c.mutant_rc = omega_gpu_swiglu_f32((uint32_t)n, g, u, got, &info); |
| 280 | c.mutant_bad = compare(got, want, n, 1e-5, 1e-6, &w2); |
| 281 | c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; |
| 282 | omega_gpu_elementwise_test_set_mutant(0); |
| 283 | record(c, out); free(g); free(u); free(got); free(want); |
| 284 | } } |
| 285 | /* 6. stale-SM-instruction-cache guard (cut 1b finding, omega #256): 10 distinct kernels |
| 286 | * (5 ops + 5 mutants) in rotation, two rounds. Every launch uploads code into a freshly |
| 287 | * allocated page, so a kernel routinely lands on the code address its predecessor used, |
| 288 | * and 10 keys evict the 8-slot kernel cache. Each clean launch must match the oracle and |
| 289 | * each mutant must still be caught; a stale icache would make a mutant pass or a clean |
| 290 | * kernel fail. Red was observed by cut 1b on matmul before its invalidate (receipt |
| 291 | * FB1-CUT1B-f629798); this build carries the invalidate, so the case is a guard. */ |
| 292 | { |
| 293 | enum { RN = 256, RH = 4, RD = 64 }; |
| 294 | float *x = malloc(RN * 4), *w = malloc(RN * 4), *u = malloc(RN * 4), *got = malloc(RN * 4), *want = malloc(RN * 4); |
| 295 | float *ch = malloc(RD / 2 * 4), *sh = malloc(RD / 2 * 4); |
| 296 | uint32_t *xu = malloc(RN * 4), *gu = malloc(RN * 4); |
| 297 | for (size_t i = 0; i < RN; i++) { x[i] = frand(&seed, -3.0f, 3.0f); w[i] = frand(&seed, 0.5f, 1.5f); u[i] = frand(&seed, -2.0f, 2.0f); xu[i] = lcg(&seed); } |
| 298 | oracle_rope_tables(RD, 17, 10000.0f, ch, sh); |
| 299 | Case c = { .name = "icache_reuse_rotation", .n = 20 }; |
| 300 | c.rc = OMEGA_GPU_EW_OK; c.mutant_rc = OMEGA_GPU_EW_OK; c.mutant_caught = 1; |
| 301 | const int order[10] = { OMEGA_GPU_EW_EX2, -OMEGA_GPU_EW_SWIGLU, OMEGA_GPU_EW_XCHG, -OMEGA_GPU_EW_EX2, OMEGA_GPU_EW_RMSNORM, |
| 302 | -OMEGA_GPU_EW_XCHG, OMEGA_GPU_EW_SWIGLU, -OMEGA_GPU_EW_ROPE, OMEGA_GPU_EW_ROPE, -OMEGA_GPU_EW_RMSNORM }; |
| 303 | for (int step = 0; step < 20; step++) { |
| 304 | int round = step / 10, k = step % 10; |
| 305 | int mut = order[k] < 0, op = mut ? -order[k] : order[k]; |
| 306 | omega_gpu_elementwise_test_set_mutant(mut ? op : 0); |
| 307 | int rc; size_t bad = 0; double wst; |
| 308 | switch (op) { |
| 309 | case OMEGA_GPU_EW_EX2: |
| 310 | for (size_t i = 0; i < RN; i++) want[i] = (float)exp2((double)x[i]); |
| 311 | rc = omega_gpu_ex2_f32(RN, x, got, &info); bad = compare(got, want, RN, 1e-6, 0.0, &wst); break; |
| 312 | case OMEGA_GPU_EW_XCHG: |
| 313 | rc = omega_gpu_shared_xchg_u32(RN, xu, gu, &info); |
| 314 | for (size_t i = 0; i < RN; i++) { if (gu[i] != xu[i ^ (OMEGA_GPU_EW_THREADS - 1)]) bad++; } |
| 315 | break; |
| 316 | case OMEGA_GPU_EW_RMSNORM: |
| 317 | oracle_rmsnorm(x, w, 1e-5f, want, RN); |
| 318 | rc = omega_gpu_rmsnorm_f32(1, RN, x, w, 1e-5f, got, &info); bad = compare(got, want, RN, 1e-5, 1e-6, &wst); break; |
| 319 | case OMEGA_GPU_EW_ROPE: |
| 320 | oracle_rope(x, RH, RD, ch, sh, want); |
| 321 | rc = omega_gpu_rope_f32(RH, RD, x, ch, sh, got, &info); bad = compare(got, want, RH * RD, 0.0, 0.0, &wst); break; |
| 322 | default: |
| 323 | oracle_swiglu(x, u, want, RN); |
| 324 | rc = omega_gpu_swiglu_f32(RN, x, u, got, &info); bad = compare(got, want, RN, 1e-5, 1e-6, &wst); break; |
| 325 | } |
| 326 | omega_gpu_elementwise_test_set_mutant(0); |
| 327 | int ok = rc == OMEGA_GPU_EW_OK && bad == 0; |
| 328 | if (!mut && !ok) { c.bad++; if (rc != OMEGA_GPU_EW_OK) c.rc = rc; printf(" rotation %d/%d: clean op %d FAILED rc=%s violations=%zu\n", round, k, op, omega_gpu_elementwise_rc_name(rc), bad); } |
| 329 | if (mut && ok) { c.mutant_bad++; c.mutant_caught = 0; printf(" rotation %d/%d: mutant op %d NOT caught\n", round, k, op); } |
| 330 | c.chip_ns += info.elapsed_ns; c.calls += info.chip_calls; c.unwritten += info.unwritten_words; |
| 331 | } |
| 332 | c.pass = c.bad == 0 && c.rc == OMEGA_GPU_EW_OK; |
| 333 | record(c, out); |
| 334 | free(x); free(w); free(u); free(got); free(want); free(ch); free(sh); free(xu); free(gu); |
| 335 | } |
| 336 | /* cut 4b: the whole battery (clean and mutant kernels, cache clears) ran on ONE device open */ |
| 337 | CHECK(omega_gpu_session_open_count() == 1, "device opened once for the battery (opens=%u)", omega_gpu_session_open_count()); |
| 338 | printf("device_opens=%u\n", omega_gpu_session_open_count()); |
| 339 | if (out) { |
| 340 | fprintf(out, "],\"device_opens\":%u,\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", omega_gpu_session_open_count(), g_checks, g_failed, g_failed == 0 ? "OMEGA_GPU_ELEMENTWISE_PASS" : "OMEGA_GPU_ELEMENTWISE_FAIL"); |
| 341 | fclose(out); |
| 342 | } |
| 343 | return 0; |
| 344 | } |
| 345 | |
| 346 | /* ------------------------------------------------------------ timing gate (cut 4b) */ |
| 347 | /* The persistent device: 100 calls each of rmsnorm 1x2048 and swiglu 5632 (the TinyLlama |
| 348 | * decode shapes), every call checked against the oracle, median wall time per call under |
| 349 | * 0.5 ms (cut 4 opened and closed the device per call: ~40 ms). The device must have been |
| 350 | * opened exactly once in this process. */ |
| 351 | static int cmp_d(const void *a, const void *b) { double x = *(const double *)a, y = *(const double *)b; return x < y ? -1 : x > y; } |
| 352 | static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; } |
| 353 | static int timing(const char *out_path) { |
| 354 | FILE *out = out_path ? test_out_open(out_path) : NULL; |
| 355 | if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } |
| 356 | if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_ELEMENTWISE_TIMING_V1\",\"gate_median_ms\":0.5,\"calls\":100,\"cases\":["); |
| 357 | uint32_t seed = 0xfb1c004bu; |
| 358 | enum { N = 100 }; |
| 359 | for (int t = 0; t < 2; t++) { |
| 360 | const char *name = t == 0 ? "rmsnorm_1x2048" : "swiglu_5632"; |
| 361 | size_t n = t == 0 ? 2048 : 5632; |
| 362 | float *a = malloc(n * 4), *b = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4); |
| 363 | for (size_t i = 0; i < n; i++) { a[i] = frand(&seed, -3.0f, 3.0f); b[i] = t == 0 ? frand(&seed, 0.5f, 1.5f) : frand(&seed, -2.0f, 2.0f); } |
| 364 | if (t == 0) oracle_rmsnorm(a, b, 1e-5f, want, n); else oracle_swiglu(a, b, want, n); |
| 365 | double samples[N]; int ok_calls = 0; size_t bad_total = 0; double worst = 0; uint64_t chip_sum = 0; int rc_last = 0; |
| 366 | OmegaGpuEwInfo info; |
| 367 | /* warm-up: first call opens the device and generates the kernel (not timed) */ |
| 368 | int rc0 = t == 0 ? omega_gpu_rmsnorm_f32(1, (uint32_t)n, a, b, 1e-5f, got, &info) : omega_gpu_swiglu_f32((uint32_t)n, a, b, got, &info); |
| 369 | double first_ms = info.call_ns / 1e6; |
| 370 | CHECK(rc0 == OMEGA_GPU_EW_OK, "%s first call rc=%s err=\"%s\"", name, omega_gpu_elementwise_rc_name(rc0), omega_gpu_elementwise_last_error()); |
| 371 | for (int i = 0; i < N; i++) { |
| 372 | double t0 = now_ms(); |
| 373 | int rc = t == 0 ? omega_gpu_rmsnorm_f32(1, (uint32_t)n, a, b, 1e-5f, got, &info) : omega_gpu_swiglu_f32((uint32_t)n, a, b, got, &info); |
| 374 | samples[i] = now_ms() - t0; |
| 375 | double w; size_t bad = compare(got, want, n, 1e-5, 1e-6, &w); |
| 376 | if (w > worst) worst = w; |
| 377 | bad_total += bad; chip_sum += info.elapsed_ns; rc_last = rc; |
| 378 | if (rc == OMEGA_GPU_EW_OK && bad == 0) ok_calls++; |
| 379 | } |
| 380 | qsort(samples, N, sizeof samples[0], cmp_d); |
| 381 | double median = samples[N / 2], p90 = samples[N * 9 / 10], mn = samples[0], mx = samples[N - 1]; |
| 382 | uint32_t opens = omega_gpu_session_open_count(); |
| 383 | int pass = ok_calls == N && bad_total == 0 && median < 0.5 && opens == 1; |
| 384 | CHECK(pass, "timing %s ok_calls=%d/%d violations=%zu median_ms=%.3f opens=%u last_rc=%s err=\"%s\"", name, ok_calls, N, bad_total, median, opens, omega_gpu_elementwise_rc_name(rc_last), omega_gpu_elementwise_last_error()); |
| 385 | printf("%s timing %s first_call_ms=%.3f ok_calls=%d/%d violations=%zu worst_scaled_err=%g median_ms=%.3f p90_ms=%.3f min_ms=%.3f max_ms=%.3f chip_us_mean=%.1f device_opens=%u\n", |
| 386 | pass ? "PASS" : "FAIL", name, first_ms, ok_calls, N, bad_total, worst, median, p90, mn, mx, chip_sum / 1e3 / N, opens); |
| 387 | if (out) fprintf(out, "%s{\"case\":\"%s\",\"first_call_ms\":%.3f,\"ok_calls\":%d,\"violations\":%zu,\"worst_scaled_err\":%g,\"median_ms\":%.4f,\"p90_ms\":%.4f,\"min_ms\":%.4f,\"max_ms\":%.4f,\"chip_us_mean\":%.1f,\"device_opens\":%u,\"pass\":%s}", |
| 388 | t ? "," : "", name, first_ms, ok_calls, bad_total, worst, median, p90, mn, mx, chip_sum / 1e3 / N, opens, pass ? "true" : "false"); |
| 389 | free(a); free(b); free(got); free(want); |
| 390 | } |
| 391 | if (out) { fprintf(out, "],\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", g_checks, g_failed, g_failed == 0 ? "OMEGA_GPU_ELEMENTWISE_TIMING_PASS" : "OMEGA_GPU_ELEMENTWISE_TIMING_FAIL"); fclose(out); } |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | int main(int argc, char **argv) { |
| 396 | const char *out_path = NULL; int host = 0, timing_mode = 0; |
| 397 | for (int i = 1; i < argc; i++) { |
| 398 | if (strcmp(argv[i], "--host-only") == 0) host = 1; |
| 399 | else if (strcmp(argv[i], "--timing") == 0) timing_mode = 1; |
| 400 | else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; |
| 401 | } |
| 402 | if (host) host_only(); |
| 403 | else if (timing_mode) { if (timing(out_path) != 0) return 2; } |
| 404 | else if (chip(out_path) != 0) return 2; |
| 405 | printf("%s: %d checks, %d failed\n", g_failed ? "FAIL" : "PASS", g_checks, g_failed); |
| 406 | return g_failed ? 1 : 0; |
| 407 | } |