Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/evidence/FB1-CUT4B-0396844/gpu_elementwise_test.c # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / evidence / FB1-CUT4B-0396844 / gpu_elementwise_test.c` 386 lines ยท 27042 bytes Raw Blame History 1 /* gpu_elementwise_test: FB-1 cut 4 gate for omega_gpu_elementwise_api. 2 * 3 * ./gpu_elementwise_test --host-only refusals, codegen, word fixtures, register budget, 4 * nvdisasm listing check when /usr/local/cuda/bin/nvdisasm exists 5 * ./gpu_elementwise_test --out receipt.json chip gate: every op against the host oracle, then its mutant 6 * (a deliberately wrong kernel) which the same check must catch 7 * ./gpu_elementwise_test --timing --out t.json cut 4b gate: persistent device, 100 calls each of rmsnorm 1x2048 8 * and swiglu 5632 under 0.5 ms median, one device open per process 9 * 10 * Oracles re-state aien-sovereign-core crates/aien-inference-abi/src/tensor.rs (main `5fdab70`) in C: 11 * rmsnorm sums in f64, rope computes cos/sin in f64 and casts to f32 before f32 math, swiglu is f64. 12 * This file is compiled with -ffp-contract=off so the f32 oracle math has no hidden fused multiply-add. 13 * 14 * Tolerances (why): EX2 1e-6 rel (PTX ex2.approx.f32: 2^-22.5 rel, we allow 2x). XCHG exact. 15 * RMSNORM 1e-5 rel + 1e-6 abs (f32 sum of up to 16384 squares against an f64 sum, plus RSQ seed with one 16 * Newton step; the mutant moves the scale by about 40 percent). ROPE exact (same f32 ops, same order). 17 * SWIGLU 1e-5 rel + 1e-6 abs (EX2 and RCP approximations, each about 2^-22 rel; the mutant flips the sign 18 * of the exponent, which is wrong by a factor of e^(2g)). 19 */ 20 #include "omega_gpu_elementwise_api.h" 21 #include "omega_blackwell_codegen.h" 22 #include "omega_gpu_session.h" 23 #include 24 #include 25 #include 26 #include 27 #include 28 #include 29 #include 30 31 static int g_checks, g_failed; 32 #define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0) 33 34 static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } 35 static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / `16777216`.0f); } 36 37 /* ----------------------------------------------------------------- oracles */ 38 static void oracle_rmsnorm(const float *x, const float *w, float eps, float *out, size_t dim) { 39 double ss = 0.0; 40 for (size_t i = 0; i < dim; i++) ss += (double)x[i] * (double)x[i]; 41 double mean = ss / (double)dim; 42 float scale = (float)(1.0 / sqrt(mean + (double)eps)); 43 for (size_t i = 0; i < dim; i++) { float t = x[i] * scale; out[i] = t * w[i]; } 44 } 45 static void oracle_rope_tables(size_t head_dim, size_t pos, float theta, float *cos_half, float *sin_half) { 46 size_t half = head_dim / 2; 47 for (size_t i = 0; i < half; i++) { 48 double exponent = (double)(2 * i) / (double)head_dim; 49 double freq = 1.0 / pow((double)theta, exponent); 50 double rot = (double)pos * freq; 51 sin_half[i] = (float)sin(rot); 52 cos_half[i] = (float)cos(rot); 53 } 54 } 55 static void oracle_rope(const float *v, size_t heads, size_t head_dim, const float *cos_half, const float *sin_half, float *out) { 56 size_t half = head_dim / 2; 57 for (size_t h = 0; h < heads; h++) 58 for (size_t i = 0; i < half; i++) { 59 size_t i0 = h * head_dim + i, i1 = i0 + half; 60 float q0 = v[i0], q1 = v[i1], c = cos_half[i], s = sin_half[i]; 61 float a = q0 * c, b = q1 * s; out[i0] = a - b; 62 float d = q0 * s, e = q1 * c; out[i1] = d + e; 63 } 64 } 65 static void oracle_swiglu(const float *g, const float *u, float *out, size_t n) { 66 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]); } 67 } 68 69 /* |got - want| <= rel*|want| + abs; returns the number of violations and the worst scaled error */ 70 static size_t compare(const float *got, const float *want, size_t n, double rel, double abs_tol, double *worst) { 71 size_t bad = 0; *worst = 0.0; 72 for (size_t i = 0; i < n; i++) { 73 double d = fabs((double)got[i] - (double)want[i]); 74 double lim = rel * fabs((double)want[i]) + abs_tol; 75 double scaled = lim > 0 ? d / lim : (d > 0 ? INFINITY : 0.0); 76 if (scaled > *worst) *worst = scaled; 77 if (!(d <= lim)) bad++; /* NaN counts as bad */ 78 } 79 return bad; 80 } 81 82 /* ------------------------------------------------------------- host only */ 83 static int nvdisasm_check(OmegaGpuEwOp op, const char *name, const char *const *must, int n_must) { 84 const char *nv = "/usr/local/cuda/bin/nvdisasm"; 85 if (access(nv, X_OK) != 0) { printf("nvdisasm: not present, listing check skipped for %s\n", name); return 0; } 86 OmegaBlackwellKernel k; memset(&k, 0, sizeof k); 87 if (omega_gpu_elementwise_codegen(op, NULL, 0.0f, &k) != OMEGA_GPU_EW_OK) return -1; 88 char bin[64], lst[64], cmd[256]; 89 snprintf(bin, sizeof bin, "/tmp/omega_ew_%s_%d.bin", name, (int)getpid()); 90 snprintf(lst, sizeof lst, "/tmp/omega_ew_%s_%d.sass", name, (int)getpid()); 91 FILE *f = fopen(bin, "wb"); if (!f) { free(k.code); return -1; } 92 fwrite(k.code, 1, k.code_size, f); fclose(f); 93 snprintf(cmd, sizeof cmd, "%s -b SM121 %s > %s 2>&1", nv, bin, lst); 94 int rc = system(cmd); 95 free(k.code); 96 FILE *l = fopen(lst, "r"); if (!l) return -1; 97 char line[512]; int seen[16] = {0}; int bad = 0, insns = 0; 98 while (fgets(line, sizeof line, l)) { 99 if (!strstr(line, "/*0")) continue; 100 insns++; 101 if (strstr(line, "?") || strstr(line, "error")) bad++; 102 for (int i = 0; i < n_must; i++) if (strstr(line, must[i])) seen[i] = 1; 103 } 104 fclose(l); 105 int miss = 0; 106 for (int i = 0; i < n_must; i++) if (!seen[i]) { printf(" %s: nvdisasm listing lacks %s\n", name, must[i]); miss++; } 107 printf("nvdisasm %s: rc=%d insns=%d undecodable=%d missing=%d (%s)\n", name, rc, insns, bad, miss, lst); 108 unlink(bin); 109 return (rc == 0 && bad == 0 && miss == 0) ? 0 : -1; 110 } 111 112 static void host_only(void) { 113 CHECK(omega_blackwell_verify_codegen_fixtures_fb1cut4() == 0, "word fixtures MUFU_EX2 / BAR_SYNC / LDS32 / STS32 (rc %d)", omega_blackwell_verify_codegen_fixtures_fb1cut4()); 114 float a[256] = {0}, b[256] = {0}, c[256] = {0}; 115 CHECK(omega_gpu_rmsnorm_f32(0, 128, a, b, 1e-5f, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "rows=0 refused"); 116 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"); 117 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"); 118 CHECK(omega_gpu_rope_f32(1, 63, a, b, b, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "odd head_dim refused"); 119 CHECK(omega_gpu_rope_f32(1, 64, NULL, b, b, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "NULL vector refused"); 120 CHECK(omega_gpu_swiglu_f32(0, a, b, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "n=0 refused"); 121 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"); 122 CHECK(strcmp(omega_gpu_elementwise_rc_name(OMEGA_GPU_EW_UNWRITTEN), "UNWRITTEN") == 0, "rc name"); 123 124 struct { OmegaGpuEwOp op; const char *name; const char *must[6]; int n; } ops[] = { 125 { OMEGA_GPU_EW_EX2, "ex2", { "MUFU.EX2", "@P0 EXIT", "STG.E" }, 3 }, 126 { OMEGA_GPU_EW_XCHG, "xchg", { "STS [R", "BAR.SYNC", "LDS R" }, 3 }, 127 { OMEGA_GPU_EW_RMSNORM, "rmsnorm", { "SHFL.DOWN", "BAR.SYNC", "MUFU.RSQ", "@!P0 BRA", "STS [R", "LDS R" }, 6 }, 128 { OMEGA_GPU_EW_ROPE, "rope", { "FADD", "FMUL", "STG.E" }, 3 }, 129 { OMEGA_GPU_EW_SWIGLU, "swiglu", { "MUFU.EX2", "MUFU.RCP", "@P0 EXIT" }, 3 }, 130 }; 131 for (size_t i = 0; i < sizeof ops / sizeof ops[0]; i++) { 132 OmegaBlackwellKernel k; memset(&k, 0, sizeof k); 133 int rc = omega_gpu_elementwise_codegen(ops[i].op, NULL, 0.0f, &k); 134 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); 135 printf("codegen %s: %zu insns, %u gprs, %u ugprs\n", ops[i].name, k.insn_count, k.gpr_count, k.uniform_gpr_count); 136 uint8_t digest[32]; memcpy(digest, k.code_digest, 32); 137 free(k.code); 138 /* the mutant must be a different kernel */ 139 omega_gpu_elementwise_test_set_mutant((int)ops[i].op); 140 memset(&k, 0, sizeof k); 141 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); 142 free(k.code); 143 omega_gpu_elementwise_test_set_mutant(0); 144 CHECK(nvdisasm_check(ops[i].op, ops[i].name, ops[i].must, ops[i].n) == 0, "%s nvdisasm listing decodes and shows the expected instructions", ops[i].name); 145 } 146 } 147 148 /* ------------------------------------------------------------- chip gate */ 149 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; 150 static Case g_cases[16]; static int g_ncases; 151 152 static void record(Case c, FILE *out) { 153 g_cases[g_ncases++] = c; 154 CHECK(c.pass, "%s rc=%s violations=%zu worst=%g", c.name, omega_gpu_elementwise_rc_name(c.rc), c.bad, c.worst); 155 CHECK(c.mutant_caught, "%s mutant caught (rc=%s violations=%zu)", c.name, omega_gpu_elementwise_rc_name(c.mutant_rc), c.mutant_bad); 156 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", 157 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, 158 omega_gpu_elementwise_rc_name(c.mutant_rc), c.mutant_bad, c.mutant_caught ? "CAUGHT" : "NOT CAUGHT"); 159 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}", 160 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, 161 omega_gpu_elementwise_rc_name(c.mutant_rc), c.mutant_bad, c.mutant_caught ? "true" : "false"); 162 } 163 164 static int chip(const char *out_path) { 165 FILE *out = out_path ? fopen(out_path, "w") : NULL; 166 if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } 167 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\":["); 168 uint32_t seed = 0xfb1c0004u; 169 OmegaGpuEwInfo info; 170 171 /* 1. EX2 probe, with a negative control on the comparison itself */ 172 { 173 size_t n = 4096; float *x = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4), *wrong = malloc(n * 4); 174 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); } 175 Case c = { .name = "ex2_probe", .n = n }; 176 c.rc = omega_gpu_ex2_f32((uint32_t)n, x, got, &info); 177 c.bad = compare(got, want, n, 1e-6, 0.0, &c.worst); 178 double w2; size_t neg = compare(got, wrong, n, 1e-6, 0.0, &w2); 179 CHECK(neg > n / 2, "ex2 negative control: the check rejects a wrong expectation (%zu of %zu flagged)", neg, n); 180 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; 181 omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_EX2); 182 c.mutant_rc = omega_gpu_ex2_f32((uint32_t)n, x, got, &info); 183 c.mutant_bad = compare(got, want, n, 1e-6, 0.0, &w2); 184 c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; 185 omega_gpu_elementwise_test_set_mutant(0); 186 record(c, out); free(x); free(got); free(want); free(wrong); 187 } 188 /* 2. shared-memory exchange probe (STS, BAR.SYNC, LDS) */ 189 { 190 size_t n = 4096; uint32_t *x = malloc(n * 4), *got = malloc(n * 4); 191 for (size_t i = 0; i < n; i++) x[i] = lcg(&seed); 192 Case c = { .name = "shared_xchg_probe", .n = n }; 193 c.rc = omega_gpu_shared_xchg_u32((uint32_t)n, x, got, &info); 194 for (size_t i = 0; i < n; i++) if (got[i] != x[i ^ (OMEGA_GPU_EW_THREADS - 1)]) c.bad++; 195 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; 196 omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_XCHG); 197 c.mutant_rc = omega_gpu_shared_xchg_u32((uint32_t)n, x, got, &info); 198 for (size_t i = 0; i < n; i++) if (got[i] != x[i ^ (OMEGA_GPU_EW_THREADS - 1)]) c.mutant_bad++; 199 c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; 200 omega_gpu_elementwise_test_set_mutant(0); 201 record(c, out); free(x); free(got); 202 } 203 /* 3. rmsnorm at the model hidden sizes (TinyLlama and Llama-3.2-1B: 2048), plus small and large */ 204 { size_t dims[3] = { 2048, 256, 8192 }; size_t rowss[3] = { 4, 1, 2 }; 205 for (int t = 0; t < 3; t++) { 206 size_t dim = dims[t], rows = rowss[t], n = rows * dim; float eps = 1e-5f; 207 float *x = malloc(n * 4), *w = malloc(dim * 4), *got = malloc(n * 4), *want = malloc(n * 4), *again = malloc(n * 4); 208 for (size_t i = 0; i < n; i++) x[i] = frand(&seed, -3.0f, 3.0f); 209 for (size_t i = 0; i < dim; i++) w[i] = frand(&seed, 0.5f, 1.5f); 210 for (size_t r = 0; r < rows; r++) oracle_rmsnorm(x + r * dim, w, eps, want + r * dim, dim); 211 char name[64]; snprintf(name, sizeof name, "rmsnorm_%zux%zu", rows, dim); 212 Case c = { .name = strdup(name), .n = n }; 213 c.rc = omega_gpu_rmsnorm_f32((uint32_t)rows, (uint32_t)dim, x, w, eps, got, &info); 214 c.bad = compare(got, want, n, 1e-5, 1e-6, &c.worst); 215 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; 216 OmegaGpuEwInfo i2; int rc2 = omega_gpu_rmsnorm_f32((uint32_t)rows, (uint32_t)dim, x, w, eps, again, &i2); 217 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); 218 omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_RMSNORM); 219 double w2; c.mutant_rc = omega_gpu_rmsnorm_f32((uint32_t)rows, (uint32_t)dim, x, w, eps, got, &info); 220 c.mutant_bad = compare(got, want, n, 1e-5, 1e-6, &w2); 221 c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; 222 omega_gpu_elementwise_test_set_mutant(0); 223 record(c, out); free(x); free(w); free(got); free(want); free(again); 224 } } 225 /* 4. rope: TinyLlama (32 q heads, 4 kv heads, head_dim 64, theta 10000) and Llama-3.2-1B (32/8, 64, 500000) */ 226 { 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 } }; 227 for (int t = 0; t < 4; t++) { 228 size_t heads = rs[t].heads, hd = rs[t].hd, n = heads * hd, half = hd / 2; 229 float *v = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4), *ch = malloc(half * 4), *sh = malloc(half * 4); 230 for (size_t i = 0; i < n; i++) v[i] = frand(&seed, -4.0f, 4.0f); 231 oracle_rope_tables(hd, rs[t].pos, rs[t].theta, ch, sh); 232 oracle_rope(v, heads, hd, ch, sh, want); 233 char name[64]; snprintf(name, sizeof name, "rope_%zuh_%zud_pos%zu_theta%g", heads, hd, rs[t].pos, rs[t].theta); 234 Case c = { .name = strdup(name), .n = n }; 235 c.rc = omega_gpu_rope_f32((uint32_t)heads, (uint32_t)hd, v, ch, sh, got, &info); 236 c.bad = compare(got, want, n, 0.0, 0.0, &c.worst); 237 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; 238 omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_ROPE); 239 double w2; c.mutant_rc = omega_gpu_rope_f32((uint32_t)heads, (uint32_t)hd, v, ch, sh, got, &info); 240 c.mutant_bad = compare(got, want, n, 0.0, 0.0, &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(v); free(got); free(want); free(ch); free(sh); 244 } } 245 /* 5. swiglu at the model ffn sizes (TinyLlama 5632, Llama-3.2-1B 8192) and an odd tail */ 246 { size_t ns[3] = { 5632, 8192, 1000 }; 247 for (int t = 0; t < 3; t++) { 248 size_t n = ns[t]; 249 float *g = malloc(n * 4), *u = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4); 250 for (size_t i = 0; i < n; i++) { g[i] = frand(&seed, -12.0f, 12.0f); u[i] = frand(&seed, -2.0f, 2.0f); } 251 oracle_swiglu(g, u, want, n); 252 char name[64]; snprintf(name, sizeof name, "swiglu_%zu", n); 253 Case c = { .name = strdup(name), .n = n }; 254 c.rc = omega_gpu_swiglu_f32((uint32_t)n, g, u, got, &info); 255 c.bad = compare(got, want, n, 1e-5, 1e-6, &c.worst); 256 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; 257 omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_SWIGLU); 258 double w2; c.mutant_rc = omega_gpu_swiglu_f32((uint32_t)n, g, u, got, &info); 259 c.mutant_bad = compare(got, want, n, 1e-5, 1e-6, &w2); 260 c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0; 261 omega_gpu_elementwise_test_set_mutant(0); 262 record(c, out); free(g); free(u); free(got); free(want); 263 } } 264 /* 6. stale-SM-instruction-cache guard (cut 1b finding, omega #256): 10 distinct kernels 265 * (5 ops + 5 mutants) in rotation, two rounds. Every launch uploads code into a freshly 266 * allocated page, so a kernel routinely lands on the code address its predecessor used, 267 * and 10 keys evict the 8-slot kernel cache. Each clean launch must match the oracle and 268 * each mutant must still be caught; a stale icache would make a mutant pass or a clean 269 * kernel fail. Red was observed by cut 1b on matmul before its invalidate (receipt 270 * FB1-CUT1B-`f629798`); this build carries the invalidate, so the case is a guard. */ 271 { 272 enum { RN = 256, RH = 4, RD = 64 }; 273 float *x = malloc(RN * 4), *w = malloc(RN * 4), *u = malloc(RN * 4), *got = malloc(RN * 4), *want = malloc(RN * 4); 274 float *ch = malloc(RD / 2 * 4), *sh = malloc(RD / 2 * 4); 275 uint32_t *xu = malloc(RN * 4), *gu = malloc(RN * 4); 276 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); } 277 oracle_rope_tables(RD, 17, 10000.0f, ch, sh); 278 Case c = { .name = "icache_reuse_rotation", .n = 20 }; 279 c.rc = OMEGA_GPU_EW_OK; c.mutant_rc = OMEGA_GPU_EW_OK; c.mutant_caught = 1; 280 const int order[10] = { OMEGA_GPU_EW_EX2, -OMEGA_GPU_EW_SWIGLU, OMEGA_GPU_EW_XCHG, -OMEGA_GPU_EW_EX2, OMEGA_GPU_EW_RMSNORM, 281 -OMEGA_GPU_EW_XCHG, OMEGA_GPU_EW_SWIGLU, -OMEGA_GPU_EW_ROPE, OMEGA_GPU_EW_ROPE, -OMEGA_GPU_EW_RMSNORM }; 282 for (int step = 0; step < 20; step++) { 283 int round = step / 10, k = step % 10; 284 int mut = order[k] < 0, op = mut ? -order[k] : order[k]; 285 omega_gpu_elementwise_test_set_mutant(mut ? op : 0); 286 int rc; size_t bad = 0; double wst; 287 switch (op) { 288 case OMEGA_GPU_EW_EX2: 289 for (size_t i = 0; i < RN; i++) want[i] = (float)exp2((double)x[i]); 290 rc = omega_gpu_ex2_f32(RN, x, got, &info); bad = compare(got, want, RN, 1e-6, 0.0, &wst); break; 291 case OMEGA_GPU_EW_XCHG: 292 rc = omega_gpu_shared_xchg_u32(RN, xu, gu, &info); 293 for (size_t i = 0; i < RN; i++) { if (gu[i] != xu[i ^ (OMEGA_GPU_EW_THREADS - 1)]) bad++; } 294 break; 295 case OMEGA_GPU_EW_RMSNORM: 296 oracle_rmsnorm(x, w, 1e-5f, want, RN); 297 rc = omega_gpu_rmsnorm_f32(1, RN, x, w, 1e-5f, got, &info); bad = compare(got, want, RN, 1e-5, 1e-6, &wst); break; 298 case OMEGA_GPU_EW_ROPE: 299 oracle_rope(x, RH, RD, ch, sh, want); 300 rc = omega_gpu_rope_f32(RH, RD, x, ch, sh, got, &info); bad = compare(got, want, RH * RD, 0.0, 0.0, &wst); break; 301 default: 302 oracle_swiglu(x, u, want, RN); 303 rc = omega_gpu_swiglu_f32(RN, x, u, got, &info); bad = compare(got, want, RN, 1e-5, 1e-6, &wst); break; 304 } 305 omega_gpu_elementwise_test_set_mutant(0); 306 int ok = rc == OMEGA_GPU_EW_OK && bad == 0; 307 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); } 308 if (mut && ok) { c.mutant_bad++; c.mutant_caught = 0; printf(" rotation %d/%d: mutant op %d NOT caught\n", round, k, op); } 309 c.chip_ns += info.elapsed_ns; c.calls += info.chip_calls; c.unwritten += info.unwritten_words; 310 } 311 c.pass = c.bad == 0 && c.rc == OMEGA_GPU_EW_OK; 312 record(c, out); 313 free(x); free(w); free(u); free(got); free(want); free(ch); free(sh); free(xu); free(gu); 314 } 315 /* cut 4b: the whole battery (clean and mutant kernels, cache clears) ran on ONE device open */ 316 CHECK(omega_gpu_session_open_count() == 1, "device opened once for the battery (opens=%u)", omega_gpu_session_open_count()); 317 printf("device_opens=%u\n", omega_gpu_session_open_count()); 318 if (out) { 319 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"); 320 fclose(out); 321 } 322 return 0; 323 } 324 325 /* ------------------------------------------------------------ timing gate (cut 4b) */ 326 /* The persistent device: 100 calls each of rmsnorm 1x2048 and swiglu 5632 (the TinyLlama 327 * decode shapes), every call checked against the oracle, median wall time per call under 328 * 0.5 ms (cut 4 opened and closed the device per call: ~40 ms). The device must have been 329 * opened exactly once in this process. */ 330 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; } 331 static double now_ms(void) { struct timespec t; clock_gettime(CLOCK_MONOTONIC, &t); return t.tv_sec * 1e3 + t.tv_nsec / 1e6; } 332 static int timing(const char *out_path) { 333 FILE *out = out_path ? fopen(out_path, "w") : NULL; 334 if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; } 335 if (out) fprintf(out, "{\"schema\":\"OMEGA_GPU_ELEMENTWISE_TIMING_V1\",\"gate_median_ms\":0.5,\"calls\":100,\"cases\":["); 336 uint32_t seed = 0xfb1c004bu; 337 enum { N = 100 }; 338 for (int t = 0; t < 2; t++) { 339 const char *name = t == 0 ? "rmsnorm_1x2048" : "swiglu_5632"; 340 size_t n = t == 0 ? 2048 : 5632; 341 float *a = malloc(n * 4), *b = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4); 342 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); } 343 if (t == 0) oracle_rmsnorm(a, b, 1e-5f, want, n); else oracle_swiglu(a, b, want, n); 344 double samples[N]; int ok_calls = 0; size_t bad_total = 0; double worst = 0; uint64_t chip_sum = 0; int rc_last = 0; 345 OmegaGpuEwInfo info; 346 /* warm-up: first call opens the device and generates the kernel (not timed) */ 347 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); 348 double first_ms = info.call_ns / 1e6; 349 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()); 350 for (int i = 0; i < N; i++) { 351 double t0 = now_ms(); 352 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); 353 samples[i] = now_ms() - t0; 354 double w; size_t bad = compare(got, want, n, 1e-5, 1e-6, &w); 355 if (w > worst) worst = w; 356 bad_total += bad; chip_sum += info.elapsed_ns; rc_last = rc; 357 if (rc == OMEGA_GPU_EW_OK && bad == 0) ok_calls++; 358 } 359 qsort(samples, N, sizeof samples[0], cmp_d); 360 double median = samples[N / 2], p90 = samples[N * 9 / 10], mn = samples[0], mx = samples[N - 1]; 361 uint32_t opens = omega_gpu_session_open_count(); 362 int pass = ok_calls == N && bad_total == 0 && median < 0.5 && opens == 1; 363 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()); 364 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", 365 pass ? "PASS" : "FAIL", name, first_ms, ok_calls, N, bad_total, worst, median, p90, mn, mx, chip_sum / 1e3 / N, opens); 366 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}", 367 t ? "," : "", name, first_ms, ok_calls, bad_total, worst, median, p90, mn, mx, chip_sum / 1e3 / N, opens, pass ? "true" : "false"); 368 free(a); free(b); free(got); free(want); 369 } 370 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); } 371 return 0; 372 } 373 374 int main(int argc, char **argv) { 375 const char *out_path = NULL; int host = 0, timing_mode = 0; 376 for (int i = 1; i < argc; i++) { 377 if (strcmp(argv[i], "--host-only") == 0) host = 1; 378 else if (strcmp(argv[i], "--timing") == 0) timing_mode = 1; 379 else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; 380 } 381 if (host) host_only(); 382 else if (timing_mode) { if (timing(out_path) != 0) return 2; } 383 else if (chip(out_path) != 0) return 2; 384 printf("%s: %d checks, %d failed\n", g_failed ? "FAIL" : "PASS", g_checks, g_failed); 385 return g_failed ? 1 : 0; 386 }