interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / evidence / FB1-CUT4-ac0d42c / gpu_elementwise_test.c

278 lines · 18382 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 *
8 * Oracles re-state aien-sovereign-core crates/aien-inference-abi/src/tensor.rs (main 5fdab70) in C:
9 * rmsnorm sums in f64, rope computes cos/sin in f64 and casts to f32 before f32 math, swiglu is f64.
10 * This file is compiled with -ffp-contract=off so the f32 oracle math has no hidden fused multiply-add.
11 *
12 * Tolerances (why): EX2 1e-6 rel (PTX ex2.approx.f32: 2^-22.5 rel, we allow 2x). XCHG exact.
13 * RMSNORM 1e-5 rel + 1e-6 abs (f32 sum of up to 16384 squares against an f64 sum, plus RSQ seed with one
14 * Newton step; the mutant moves the scale by about 40 percent). ROPE exact (same f32 ops, same order).
15 * SWIGLU 1e-5 rel + 1e-6 abs (EX2 and RCP approximations, each about 2^-22 rel; the mutant flips the sign
16 * of the exponent, which is wrong by a factor of e^(2g)).
17 */
18#include "omega_gpu_elementwise_api.h"
19#include "omega_blackwell_codegen.h"
20#include <inttypes.h>
21#include <math.h>
22#include <stdio.h>
23#include <stdlib.h>
24#include <string.h>
25#include <time.h>
26#include <unistd.h>
27
28static int g_checks, g_failed;
29#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0)
30
31static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; }
32static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / 16777216.0f); }
33
34/* ----------------------------------------------------------------- oracles */
35static void oracle_rmsnorm(const float *x, const float *w, float eps, float *out, size_t dim) {
36 double ss = 0.0;
37 for (size_t i = 0; i < dim; i++) ss += (double)x[i] * (double)x[i];
38 double mean = ss / (double)dim;
39 float scale = (float)(1.0 / sqrt(mean + (double)eps));
40 for (size_t i = 0; i < dim; i++) { float t = x[i] * scale; out[i] = t * w[i]; }
41}
42static void oracle_rope_tables(size_t head_dim, size_t pos, float theta, float *cos_half, float *sin_half) {
43 size_t half = head_dim / 2;
44 for (size_t i = 0; i < half; i++) {
45 double exponent = (double)(2 * i) / (double)head_dim;
46 double freq = 1.0 / pow((double)theta, exponent);
47 double rot = (double)pos * freq;
48 sin_half[i] = (float)sin(rot);
49 cos_half[i] = (float)cos(rot);
50 }
51}
52static void oracle_rope(const float *v, size_t heads, size_t head_dim, const float *cos_half, const float *sin_half, float *out) {
53 size_t half = head_dim / 2;
54 for (size_t h = 0; h < heads; h++)
55 for (size_t i = 0; i < half; i++) {
56 size_t i0 = h * head_dim + i, i1 = i0 + half;
57 float q0 = v[i0], q1 = v[i1], c = cos_half[i], s = sin_half[i];
58 float a = q0 * c, b = q1 * s; out[i0] = a - b;
59 float d = q0 * s, e = q1 * c; out[i1] = d + e;
60 }
61}
62static void oracle_swiglu(const float *g, const float *u, float *out, size_t n) {
63 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]); }
64}
65
66/* |got - want| <= rel*|want| + abs; returns the number of violations and the worst scaled error */
67static size_t compare(const float *got, const float *want, size_t n, double rel, double abs_tol, double *worst) {
68 size_t bad = 0; *worst = 0.0;
69 for (size_t i = 0; i < n; i++) {
70 double d = fabs((double)got[i] - (double)want[i]);
71 double lim = rel * fabs((double)want[i]) + abs_tol;
72 double scaled = lim > 0 ? d / lim : (d > 0 ? INFINITY : 0.0);
73 if (scaled > *worst) *worst = scaled;
74 if (!(d <= lim)) bad++; /* NaN counts as bad */
75 }
76 return bad;
77}
78
79/* ------------------------------------------------------------- host only */
80static int nvdisasm_check(OmegaGpuEwOp op, const char *name, const char *const *must, int n_must) {
81 const char *nv = "/usr/local/cuda/bin/nvdisasm";
82 if (access(nv, X_OK) != 0) { printf("nvdisasm: not present, listing check skipped for %s\n", name); return 0; }
83 OmegaBlackwellKernel k; memset(&k, 0, sizeof k);
84 if (omega_gpu_elementwise_codegen(op, NULL, 0.0f, &k) != OMEGA_GPU_EW_OK) return -1;
85 char bin[64], lst[64], cmd[256];
86 snprintf(bin, sizeof bin, "/tmp/omega_ew_%s_%d.bin", name, (int)getpid());
87 snprintf(lst, sizeof lst, "/tmp/omega_ew_%s_%d.sass", name, (int)getpid());
88 FILE *f = fopen(bin, "wb"); if (!f) { free(k.code); return -1; }
89 fwrite(k.code, 1, k.code_size, f); fclose(f);
90 snprintf(cmd, sizeof cmd, "%s -b SM121 %s > %s 2>&1", nv, bin, lst);
91 int rc = system(cmd);
92 free(k.code);
93 FILE *l = fopen(lst, "r"); if (!l) return -1;
94 char line[512]; int seen[16] = {0}; int bad = 0, insns = 0;
95 while (fgets(line, sizeof line, l)) {
96 if (!strstr(line, "/*0")) continue;
97 insns++;
98 if (strstr(line, "?") || strstr(line, "error")) bad++;
99 for (int i = 0; i < n_must; i++) if (strstr(line, must[i])) seen[i] = 1;
100 }
101 fclose(l);
102 int miss = 0;
103 for (int i = 0; i < n_must; i++) if (!seen[i]) { printf(" %s: nvdisasm listing lacks %s\n", name, must[i]); miss++; }
104 printf("nvdisasm %s: rc=%d insns=%d undecodable=%d missing=%d (%s)\n", name, rc, insns, bad, miss, lst);
105 unlink(bin);
106 return (rc == 0 && bad == 0 && miss == 0) ? 0 : -1;
107}
108
109static void host_only(void) {
110 CHECK(omega_blackwell_verify_codegen_fixtures_fb1cut4() == 0, "word fixtures MUFU_EX2 / BAR_SYNC / LDS32 / STS32 (rc %d)", omega_blackwell_verify_codegen_fixtures_fb1cut4());
111 float a[256] = {0}, b[256] = {0}, c[256] = {0};
112 CHECK(omega_gpu_rmsnorm_f32(0, 128, a, b, 1e-5f, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "rows=0 refused");
113 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");
114 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");
115 CHECK(omega_gpu_rope_f32(1, 63, a, b, b, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "odd head_dim refused");
116 CHECK(omega_gpu_rope_f32(1, 64, NULL, b, b, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "NULL vector refused");
117 CHECK(omega_gpu_swiglu_f32(0, a, b, c, NULL) == OMEGA_GPU_EW_BAD_ARGS, "n=0 refused");
118 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");
119 CHECK(strcmp(omega_gpu_elementwise_rc_name(OMEGA_GPU_EW_UNWRITTEN), "UNWRITTEN") == 0, "rc name");
120
121 struct { OmegaGpuEwOp op; const char *name; const char *must[6]; int n; } ops[] = {
122 { OMEGA_GPU_EW_EX2, "ex2", { "MUFU.EX2", "@P0 EXIT", "STG.E" }, 3 },
123 { OMEGA_GPU_EW_XCHG, "xchg", { "STS [R", "BAR.SYNC", "LDS R" }, 3 },
124 { OMEGA_GPU_EW_RMSNORM, "rmsnorm", { "SHFL.DOWN", "BAR.SYNC", "MUFU.RSQ", "@!P0 BRA", "STS [R", "LDS R" }, 6 },
125 { OMEGA_GPU_EW_ROPE, "rope", { "FADD", "FMUL", "STG.E" }, 3 },
126 { OMEGA_GPU_EW_SWIGLU, "swiglu", { "MUFU.EX2", "MUFU.RCP", "@P0 EXIT" }, 3 },
127 };
128 for (size_t i = 0; i < sizeof ops / sizeof ops[0]; i++) {
129 OmegaBlackwellKernel k; memset(&k, 0, sizeof k);
130 int rc = omega_gpu_elementwise_codegen(ops[i].op, NULL, 0.0f, &k);
131 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);
132 printf("codegen %s: %zu insns, %u gprs, %u ugprs\n", ops[i].name, k.insn_count, k.gpr_count, k.uniform_gpr_count);
133 uint8_t digest[32]; memcpy(digest, k.code_digest, 32);
134 free(k.code);
135 /* the mutant must be a different kernel */
136 omega_gpu_elementwise_test_set_mutant((int)ops[i].op);
137 memset(&k, 0, sizeof k);
138 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);
139 free(k.code);
140 omega_gpu_elementwise_test_set_mutant(0);
141 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);
142 }
143}
144
145/* ------------------------------------------------------------- chip gate */
146typedef 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;
147static Case g_cases[16]; static int g_ncases;
148
149static void record(Case c, FILE *out) {
150 g_cases[g_ncases++] = c;
151 CHECK(c.pass, "%s rc=%s violations=%zu worst=%g", c.name, omega_gpu_elementwise_rc_name(c.rc), c.bad, c.worst);
152 CHECK(c.mutant_caught, "%s mutant caught (rc=%s violations=%zu)", c.name, omega_gpu_elementwise_rc_name(c.mutant_rc), c.mutant_bad);
153 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",
154 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,
155 omega_gpu_elementwise_rc_name(c.mutant_rc), c.mutant_bad, c.mutant_caught ? "CAUGHT" : "NOT CAUGHT");
156 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}",
157 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,
158 omega_gpu_elementwise_rc_name(c.mutant_rc), c.mutant_bad, c.mutant_caught ? "true" : "false");
159}
160
161static int chip(const char *out_path) {
162 FILE *out = out_path ? fopen(out_path, "w") : NULL;
163 if (out_path && !out) { printf("cannot open %s\n", out_path); return 2; }
164 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\":[");
165 uint32_t seed = 0xfb1c0004u;
166 OmegaGpuEwInfo info;
167
168 /* 1. EX2 probe, with a negative control on the comparison itself */
169 {
170 size_t n = 4096; float *x = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4), *wrong = malloc(n * 4);
171 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); }
172 Case c = { .name = "ex2_probe", .n = n };
173 c.rc = omega_gpu_ex2_f32((uint32_t)n, x, got, &info);
174 c.bad = compare(got, want, n, 1e-6, 0.0, &c.worst);
175 double w2; size_t neg = compare(got, wrong, n, 1e-6, 0.0, &w2);
176 CHECK(neg > n / 2, "ex2 negative control: the check rejects a wrong expectation (%zu of %zu flagged)", neg, n);
177 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;
178 omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_EX2);
179 c.mutant_rc = omega_gpu_ex2_f32((uint32_t)n, x, got, &info);
180 c.mutant_bad = compare(got, want, n, 1e-6, 0.0, &w2);
181 c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0;
182 omega_gpu_elementwise_test_set_mutant(0);
183 record(c, out); free(x); free(got); free(want); free(wrong);
184 }
185 /* 2. shared-memory exchange probe (STS, BAR.SYNC, LDS) */
186 {
187 size_t n = 4096; uint32_t *x = malloc(n * 4), *got = malloc(n * 4);
188 for (size_t i = 0; i < n; i++) x[i] = lcg(&seed);
189 Case c = { .name = "shared_xchg_probe", .n = n };
190 c.rc = omega_gpu_shared_xchg_u32((uint32_t)n, x, got, &info);
191 for (size_t i = 0; i < n; i++) if (got[i] != x[i ^ (OMEGA_GPU_EW_THREADS - 1)]) c.bad++;
192 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;
193 omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_XCHG);
194 c.mutant_rc = omega_gpu_shared_xchg_u32((uint32_t)n, x, got, &info);
195 for (size_t i = 0; i < n; i++) if (got[i] != x[i ^ (OMEGA_GPU_EW_THREADS - 1)]) c.mutant_bad++;
196 c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0;
197 omega_gpu_elementwise_test_set_mutant(0);
198 record(c, out); free(x); free(got);
199 }
200 /* 3. rmsnorm at the model hidden sizes (TinyLlama and Llama-3.2-1B: 2048), plus small and large */
201 { size_t dims[3] = { 2048, 256, 8192 }; size_t rowss[3] = { 4, 1, 2 };
202 for (int t = 0; t < 3; t++) {
203 size_t dim = dims[t], rows = rowss[t], n = rows * dim; float eps = 1e-5f;
204 float *x = malloc(n * 4), *w = malloc(dim * 4), *got = malloc(n * 4), *want = malloc(n * 4), *again = malloc(n * 4);
205 for (size_t i = 0; i < n; i++) x[i] = frand(&seed, -3.0f, 3.0f);
206 for (size_t i = 0; i < dim; i++) w[i] = frand(&seed, 0.5f, 1.5f);
207 for (size_t r = 0; r < rows; r++) oracle_rmsnorm(x + r * dim, w, eps, want + r * dim, dim);
208 char name[64]; snprintf(name, sizeof name, "rmsnorm_%zux%zu", rows, dim);
209 Case c = { .name = strdup(name), .n = n };
210 c.rc = omega_gpu_rmsnorm_f32((uint32_t)rows, (uint32_t)dim, x, w, eps, got, &info);
211 c.bad = compare(got, want, n, 1e-5, 1e-6, &c.worst);
212 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;
213 OmegaGpuEwInfo i2; int rc2 = omega_gpu_rmsnorm_f32((uint32_t)rows, (uint32_t)dim, x, w, eps, again, &i2);
214 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);
215 omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_RMSNORM);
216 double w2; c.mutant_rc = omega_gpu_rmsnorm_f32((uint32_t)rows, (uint32_t)dim, x, w, eps, got, &info);
217 c.mutant_bad = compare(got, want, n, 1e-5, 1e-6, &w2);
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(w); free(got); free(want); free(again);
221 } }
222 /* 4. rope: TinyLlama (32 q heads, 4 kv heads, head_dim 64, theta 10000) and Llama-3.2-1B (32/8, 64, 500000) */
223 { 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 } };
224 for (int t = 0; t < 4; t++) {
225 size_t heads = rs[t].heads, hd = rs[t].hd, n = heads * hd, half = hd / 2;
226 float *v = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4), *ch = malloc(half * 4), *sh = malloc(half * 4);
227 for (size_t i = 0; i < n; i++) v[i] = frand(&seed, -4.0f, 4.0f);
228 oracle_rope_tables(hd, rs[t].pos, rs[t].theta, ch, sh);
229 oracle_rope(v, heads, hd, ch, sh, want);
230 char name[64]; snprintf(name, sizeof name, "rope_%zuh_%zud_pos%zu_theta%g", heads, hd, rs[t].pos, rs[t].theta);
231 Case c = { .name = strdup(name), .n = n };
232 c.rc = omega_gpu_rope_f32((uint32_t)heads, (uint32_t)hd, v, ch, sh, got, &info);
233 c.bad = compare(got, want, n, 0.0, 0.0, &c.worst);
234 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;
235 omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_ROPE);
236 double w2; c.mutant_rc = omega_gpu_rope_f32((uint32_t)heads, (uint32_t)hd, v, ch, sh, got, &info);
237 c.mutant_bad = compare(got, want, n, 0.0, 0.0, &w2);
238 c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0;
239 omega_gpu_elementwise_test_set_mutant(0);
240 record(c, out); free(v); free(got); free(want); free(ch); free(sh);
241 } }
242 /* 5. swiglu at the model ffn sizes (TinyLlama 5632, Llama-3.2-1B 8192) and an odd tail */
243 { size_t ns[3] = { 5632, 8192, 1000 };
244 for (int t = 0; t < 3; t++) {
245 size_t n = ns[t];
246 float *g = malloc(n * 4), *u = malloc(n * 4), *got = malloc(n * 4), *want = malloc(n * 4);
247 for (size_t i = 0; i < n; i++) { g[i] = frand(&seed, -12.0f, 12.0f); u[i] = frand(&seed, -2.0f, 2.0f); }
248 oracle_swiglu(g, u, want, n);
249 char name[64]; snprintf(name, sizeof name, "swiglu_%zu", n);
250 Case c = { .name = strdup(name), .n = n };
251 c.rc = omega_gpu_swiglu_f32((uint32_t)n, g, u, got, &info);
252 c.bad = compare(got, want, n, 1e-5, 1e-6, &c.worst);
253 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;
254 omega_gpu_elementwise_test_set_mutant(OMEGA_GPU_EW_SWIGLU);
255 double w2; c.mutant_rc = omega_gpu_swiglu_f32((uint32_t)n, g, u, got, &info);
256 c.mutant_bad = compare(got, want, n, 1e-5, 1e-6, &w2);
257 c.mutant_caught = c.mutant_rc != OMEGA_GPU_EW_OK || c.mutant_bad > 0;
258 omega_gpu_elementwise_test_set_mutant(0);
259 record(c, out); free(g); free(u); free(got); free(want);
260 } }
261 if (out) {
262 fprintf(out, "],\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", g_checks, g_failed, g_failed == 0 ? "OMEGA_GPU_ELEMENTWISE_PASS" : "OMEGA_GPU_ELEMENTWISE_FAIL");
263 fclose(out);
264 }
265 return 0;
266}
267
268int main(int argc, char **argv) {
269 const char *out_path = NULL; int host = 0;
270 for (int i = 1; i < argc; i++) {
271 if (strcmp(argv[i], "--host-only") == 0) host = 1;
272 else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i];
273 }
274 if (host) host_only();
275 else if (chip(out_path) != 0) return 2;
276 printf("%s: %d checks, %d failed\n", g_failed ? "FAIL" : "PASS", g_checks, g_failed);
277 return g_failed ? 1 : 0;
278}