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 / tests / gpu_exact_head_test.c

324 lines · 22212 bytes Raw Blame History
1/*
2 * gpu_exact_head_test: the exact two-pass lm_head (HUM lever L314) on the Qwen3.6-35B-A3B head.
3 * --sim warp-simulator proofs of the new kernels: XBOUND (lo <= s - c|h|, hi >= s + c|h| on every row despite f32
4 * rounding, tight to 2^-8, rows past count untouched; mutant 1 caught). The MASK recheck form is proven in
5 * gpu_bf16_gemv_test --sim (candidates bit-identical to the plain kernel, mutant 4 caught).
6 * --chip <export-dir> <hidden.bin> [--limit N] [--mutant 1] [--out receipt.json] per hidden record (the 35B host
7 * reference's final hidden states): the full bf16 head + ARGMAX x2 as one list, the two-pass head (FP8 block
8 * screen, XBOUND, ARGMAX x2 for tau, MASK recheck, ARGMAX x2) as another; token identical, every candidate
9 * logit bit-identical to the full head's, timing of both lists. --mutant 1 (XBOUND without the bound) must
10 * lose tokens. Inputs from sc q36_exact_head export (weights stay outside git).
11 * Verdict line last: "gpu_exact_head_test: N checks, F failed: PASS|FAIL".
12 */
13#include "omega_blackwell_codegen_dec.h"
14#include "omega_gpu_bf16_gemv_api.h"
15#include "omega_gpu_fp8_api.h"
16#include "omega_gpu_cmdlist.h"
17#include "omega_gpu_session.h"
18#include "bw_warp_sim_k.h"
19#include <inttypes.h>
20#include <math.h>
21#include <stdio.h>
22#include <stdlib.h>
23#include <string.h>
24
25static unsigned g_checks, g_failed;
26#define CHECK(cond, ...) do { g_checks++; if (!(cond)) { g_failed++; printf("FAIL: "); printf(__VA_ARGS__); printf("\n"); } } while (0)
27static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; }
28static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / 16777216.0f); }
29
30static void put64(uint32_t *cb, uint32_t off, const void *p) { uint64_t a = (uintptr_t)p; cb[off / 4] = (uint32_t)a; cb[off / 4 + 1] = (uint32_t)(a >> 32); }
31/* launch argument words start at the constant bank's 0x380 */
32static void put64v(uint32_t *args, uint32_t off, uint64_t a) { args[(off - 0x380u) / 4] = (uint32_t)a; args[(off - 0x380u) / 4 + 1] = (uint32_t)(a >> 32); }
33
34/* XBOUND on n rows (grid ceil(n / 256), the last CTA partial) at hidden width W (0 = 2048). hscale spreads |h| over decades.
35 * Returns the number of rows whose lo / hi break the bound (or are loose by more than 2^-8 relative); *untouched = rows past n left alone. */
36static int sim_xbound_w(uint32_t n, uint32_t width, int mutant, uint32_t seed, float hscale, size_t *bad, size_t *loose, int *untouched, char *msg, size_t msgn) {
37 static BlackwellIRProgram prog;
38 OmegaDecSpec spec; memset(&spec, 0, sizeof spec);
39 spec.kind = OMEGA_DEC_XBOUND; spec.width = width; spec.mutant = mutant;
40 const uint32_t H = width ? width : 2048u;
41 if (omega_blackwell_codegen_dec_prog(&spec, &prog) != 0) { snprintf(msg, msgn, "codegen refused"); return -1; }
42 const uint32_t ctas = (n + 255u) / 256u, cap = ctas * 256u + 8u;
43 float *s = aligned_alloc(16, cap * 4), *c = aligned_alloc(16, cap * 4), *h = aligned_alloc(16, H * 4);
44 float *lo = aligned_alloc(16, cap * 4), *hi = aligned_alloc(16, cap * 4);
45 uint8_t *smem = aligned_alloc(16, OMEGA_DEC_SHARED_BYTES);
46 uint32_t st = seed;
47 for (uint32_t i = 0; i < H; i++) h[i] = frand(&st, -1.0f, 1.0f) * hscale * (i % 97 == 0 ? 40.0f : 1.0f);
48 if (H > 2048u) h[H - 1] = 250.0f * hscale; /* tail-element sensitivity: proves all H elements contribute to |h| */
49 for (uint32_t i = 0; i < cap; i++) {
50 s[i] = frand(&st, -30.0f, 30.0f) * (i % 7 == 0 ? 1e3f : 1.0f);
51 c[i] = frand(&st, 0.0f, 0.02f);
52 lo[i] = hi[i] = -999.0f;
53 }
54 s[0] = 0.0f; c[1] = 0.0f; s[2] = -0.0f;
55 double hn = 0; for (uint32_t i = 0; i < H; i++) hn += (double)h[i] * h[i];
56 hn = sqrt(hn);
57 WsSim w; memset(&w, 0, sizeof w);
58 w.p = &prog; w.threads = OMEGA_DEC_THREADS; w.order = (int)(seed & 1u); w.smem = smem; w.smem_bytes = OMEGA_DEC_SHARED_BYTES;
59 put64(w.cbank, OMEGA_DEC_XB_S, s); put64(w.cbank, OMEGA_DEC_XB_C, c); put64(w.cbank, OMEGA_DEC_XB_H, h);
60 put64(w.cbank, OMEGA_DEC_XB_LO, lo); put64(w.cbank, OMEGA_DEC_XB_HI, hi); w.cbank[OMEGA_DEC_XB_N / 4] = n;
61 w.nranges = 5;
62 w.ranges[0].lo = (uintptr_t)s; w.ranges[0].hi = (uintptr_t)(s + cap);
63 w.ranges[1].lo = (uintptr_t)c; w.ranges[1].hi = (uintptr_t)(c + cap);
64 w.ranges[2].lo = (uintptr_t)h; w.ranges[2].hi = (uintptr_t)(h + H);
65 w.ranges[3].lo = (uintptr_t)lo; w.ranges[3].hi = (uintptr_t)(lo + cap);
66 w.ranges[4].lo = (uintptr_t)hi; w.ranges[4].hi = (uintptr_t)(hi + cap);
67 int err = ws_run(&w, ctas, 1);
68 if (err) snprintf(msg, msgn, "%s (pc %d)", w.msg, w.err_pc);
69 else {
70 size_t b = 0, l = 0;
71 for (uint32_t i = 0; i < n; i++) {
72 const double B = (double)c[i] * hn, sv = s[i];
73 if (!((double)lo[i] <= sv - B && (double)hi[i] >= sv + B)) b++;
74 const double slack = B * 0x1p-8 + fabs(sv) * 0x1p-19 + 1e-29;
75 if (sv - (double)lo[i] > B + slack || (double)hi[i] - sv > B + slack) l++;
76 }
77 int u = 1; for (uint32_t i = n; i < cap; i++) u &= lo[i] == -999.0f && hi[i] == -999.0f;
78 *bad = b; *loose = l; *untouched = u;
79 snprintf(msg, msgn, "%u rows (H=%u): %zu break the bound, %zu looser than 2^-8, rows past count %s", n, H, b, l, u ? "untouched" : "WRITTEN");
80 }
81 free(s); free(c); free(h); free(lo); free(hi); free(smem);
82 return err;
83}
84static int sim_xbound(uint32_t n, int mutant, uint32_t seed, float hscale, size_t *bad, size_t *loose, int *untouched, char *msg, size_t msgn) {
85 return sim_xbound_w(n, 0, mutant, seed, hscale, bad, loose, untouched, msg, msgn);
86}
87
88/* XBOUND mode 1 (SCALE) on n values: out == in * 2^64 bit for bit, values past n untouched */
89static int sim_xscale(uint32_t n, uint32_t seed, size_t *bad, char *msg, size_t msgn) {
90 static BlackwellIRProgram prog;
91 OmegaDecSpec spec; memset(&spec, 0, sizeof spec);
92 spec.kind = OMEGA_DEC_XBOUND; spec.mode = 1;
93 if (omega_blackwell_codegen_dec_prog(&spec, &prog) != 0) { snprintf(msg, msgn, "codegen refused"); return -1; }
94 const uint32_t ctas = (n + 255u) / 256u, cap = ctas * 256u + 8u;
95 float *in = aligned_alloc(16, cap * 4), *out = aligned_alloc(16, cap * 4);
96 uint32_t st = seed;
97 for (uint32_t i = 0; i < cap; i++) { in[i] = frand(&st, -3.0f, 3.0f) * (i % 5 == 0 ? 1e-20f : 1.0f); out[i] = -999.0f; }
98 in[0] = 0.0f; in[1] = -0.0f; in[2] = 1e30f;
99 WsSim w; memset(&w, 0, sizeof w);
100 w.p = &prog; w.threads = OMEGA_DEC_THREADS; w.order = (int)(seed & 1u);
101 put64(w.cbank, OMEGA_DEC_XB_S, in); put64(w.cbank, OMEGA_DEC_XB_LO, out); w.cbank[OMEGA_DEC_XB_N / 4] = n;
102 w.nranges = 2;
103 w.ranges[0].lo = (uintptr_t)in; w.ranges[0].hi = (uintptr_t)(in + cap);
104 w.ranges[1].lo = (uintptr_t)out; w.ranges[1].hi = (uintptr_t)(out + cap);
105 int err = ws_run(&w, ctas, 1);
106 if (err) snprintf(msg, msgn, "%s (pc %d)", w.msg, w.err_pc);
107 else {
108 size_t b = 0;
109 for (uint32_t i = 0; i < n; i++) { const float want = in[i] * 0x1p64f; b += memcmp(&out[i], &want, 4) != 0; }
110 for (uint32_t i = n; i < cap; i++) b += out[i] != -999.0f;
111 *bad = b;
112 snprintf(msg, msgn, "%u values: %zu differ from h * 2^64 or written past count", n, b);
113 }
114 free(in); free(out);
115 return err;
116}
117
118static void sim_battery(void) {
119 static const struct { uint32_t n, seed; float hscale; } pts[] = { { 700, 3, 1.0f }, { 256, 4, 1e-3f }, { 1000, 5, 30.0f }, { 37, 6, 1.0f } };
120 for (size_t i = 0; i < sizeof pts / sizeof pts[0]; i++) {
121 char msg[300]; size_t bad = 1, loose = 1; int u = 0;
122 int err = sim_xbound(pts[i].n, 0, pts[i].seed, pts[i].hscale, &bad, &loose, &u, msg, sizeof msg);
123 CHECK(!err && bad == 0 && loose == 0 && u, "sim XBOUND n %u: %s", pts[i].n, msg);
124 printf("sim XBOUND n %u |h| x%g: %s %s\n", pts[i].n, pts[i].hscale, err ? "ERROR" : bad || loose || !u ? "MISMATCH" : "ok", msg);
125 }
126 char msg[300]; size_t bad = 0, loose = 0; int u = 0;
127 int err = sim_xbound(700, 1, 3, 1.0f, &bad, &loose, &u, msg, sizeof msg);
128 CHECK(!err && bad != 0, "sim XBOUND mutant 1 must be caught: %s", msg);
129 printf("sim XBOUND mutant 1: %s %s\n", err ? "ERROR" : bad ? "caught" : "NOT CAUGHT", msg);
130 OmegaDecSpec m2; memset(&m2, 0, sizeof m2); m2.kind = OMEGA_DEC_XBOUND; m2.mutant = 2;
131 static BlackwellIRProgram p2;
132 CHECK(omega_blackwell_codegen_dec_prog(&m2, &p2) != 0, "XBOUND refuses mutant 2");
133 for (uint32_t n2 = 2048; n2 >= 37; n2 = n2 == 2048 ? 37 : 0) {
134 size_t xb = 1; int xe = sim_xscale(n2, 7, &xb, msg, sizeof msg);
135 CHECK(!xe && xb == 0, "sim XBOUND SCALE n %u: %s", n2, msg);
136 printf("sim XBOUND SCALE n %u: %s %s\n", n2, xe ? "ERROR" : xb ? "MISMATCH" : "ok", msg);
137 }
138 m2.mutant = 1; m2.mode = 1;
139 CHECK(omega_blackwell_codegen_dec_prog(&m2, &p2) != 0, "XBOUND SCALE refuses a mutant");
140 /* 9B (4096) and 27B (5120) hidden widths */
141 static const uint32_t q38_widths[] = { 4096u, 5120u };
142 for (size_t i = 0; i < sizeof q38_widths / sizeof q38_widths[0]; i++) {
143 size_t b = 1, l = 1; int u2 = 0;
144 int e2 = sim_xbound_w(530, q38_widths[i], 0, 11 + (uint32_t)i, 1.0f, &b, &l, &u2, msg, sizeof msg);
145 CHECK(!e2 && b == 0 && l == 0 && u2, "sim XBOUND H=%u: %s", q38_widths[i], msg);
146 printf("sim XBOUND H=%u: %s %s\n", q38_widths[i], e2 ? "ERROR" : b || l || !u2 ? "MISMATCH" : "ok", msg);
147 }
148 {
149 size_t b = 0, l = 0; int u2 = 0;
150 int e2 = sim_xbound_w(530, 5120u, 1, 12, 1.0f, &b, &l, &u2, msg, sizeof msg);
151 CHECK(!e2 && b != 0, "sim XBOUND H=5120 mutant 1 must be caught: %s", msg);
152 printf("sim XBOUND H=5120 mutant 1: %s %s\n", e2 ? "ERROR" : b ? "caught" : "NOT CAUGHT", msg);
153 }
154 {
155 size_t xb = 1; int xe = sim_xscale(5120u, 13, &xb, msg, sizeof msg);
156 CHECK(!xe && xb == 0, "sim XBOUND SCALE n 5120: %s", msg);
157 printf("sim XBOUND SCALE n 5120: %s %s\n", xe ? "ERROR" : xb ? "MISMATCH" : "ok", msg);
158 }
159}
160
161/* ------------------------------------------------------------- chip */
162#define XH_N 248320u
163#define XH_K 2048u
164#define NEG_PAD (-1e30f)
165
166static void *read_all(const char *dir, const char *name, size_t want) {
167 char path[1024]; snprintf(path, sizeof path, "%s/%s", dir, name);
168 FILE *f = fopen(path, "rb"); if (!f) { printf("cannot open %s\n", path); return NULL; }
169 void *p = malloc(want ? want : 1); size_t got = p ? fread(p, 1, want, f) : 0;
170 int extra = fgetc(f) != EOF; fclose(f);
171 if (got != want || extra) { printf("%s: expected exactly %zu bytes\n", path, want); free(p); return NULL; }
172 return p;
173}
174
175typedef struct { OmegaGpuMem code; uint32_t gpr; } XKern;
176static int xkern(XKern *k, OmegaDecKind kind, uint32_t mode, int mutant) {
177 OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = kind; s.mode = mode; s.mutant = mutant;
178 static BlackwellIRProgram prog;
179 if (omega_blackwell_codegen_dec_prog(&s, &prog) != 0) return -1;
180 size_t need = omega_bw_program_code_bytes(&prog);
181 if (!need || omega_gpu_session_alloc_code(need, &k->code) != 0) return -1;
182 OmegaBlackwellKernel kk;
183 if (omega_blackwell_kernel_encode_into(&prog, k->code.cpu, need, &kk) != 0 || kk.code_size != need) return -1;
184 if (omega_gpu_session_sync_to_gpu(&k->code) != 0) return -1;
185 k->gpr = omega_bw_gpr_declared(kk.gpr_count); if (k->gpr < 64u) k->gpr = 64u;
186 return 0;
187}
188static int xadd(OmegaGpuCmdList *cl, const XKern *k, uint32_t gx, size_t elems, const uint32_t *args, uint32_t n_args) {
189 OmegaGpuLaunch L; memset(&L, 0, sizeof L);
190 L.code_va = k->code.va; L.gpr_count = k->gpr; L.threads_x = OMEGA_DEC_THREADS; L.threads_y = 1; L.grid_x = gx; L.grid_y = 1;
191 L.num_elements = elems; L.args = args; L.n_args = n_args; L.timeout_ms = 20000;
192 return omega_gpu_cmdlist_add(cl, &L) < 0 ? -1 : 0;
193}
194/* ARGMAX x2 over v (padded to ctas * 2048): value -> out_v, index -> out_i */
195static int xargmax(OmegaGpuCmdList *cl, const XKern *a0k, const XKern *a1k, uint32_t ctas, uint64_t v, uint64_t amv, uint64_t ami, uint64_t out_v, uint64_t out_i) {
196 uint32_t a0[OMEGA_DEC_AM_WORDS], a1[OMEGA_DEC_AM_WORDS]; memset(a0, 0, sizeof a0); memset(a1, 0, sizeof a1);
197 put64v(a0, OMEGA_DEC_AM_V, v); put64v(a0, OMEGA_DEC_AM_OV, amv); put64v(a0, OMEGA_DEC_AM_OI, ami);
198 put64v(a1, OMEGA_DEC_AM_V, amv); put64v(a1, OMEGA_DEC_AM_I, ami); put64v(a1, OMEGA_DEC_AM_OV, out_v); put64v(a1, OMEGA_DEC_AM_OI, out_i);
199 if (xadd(cl, a0k, ctas, (size_t)ctas * OMEGA_DEC_ARGMAX_SPAN, a0, OMEGA_DEC_AM_WORDS)) return -1;
200 return xadd(cl, a1k, 1, OMEGA_DEC_ARGMAX_SPAN, a1, OMEGA_DEC_AM_WORDS);
201}
202static int cmp_u64(const void *a, const void *b) { uint64_t x = *(const uint64_t *)a, y = *(const uint64_t *)b; return x < y ? -1 : x > y; }
203static double pct(uint64_t *v, size_t n, double p) { qsort(v, n, sizeof *v, cmp_u64); return (double)v[(size_t)((double)(n - 1) * p + 0.5)]; }
204
205/* Full head vs two-pass head on every hidden record: token identical, candidate logits bit-identical, timing. */
206static void chip(const char *dir, const char *hidden_path, uint32_t limit, int mutant, const char *out_path) {
207 const uint32_t n = XH_N, k = XH_K, ctas = (n + OMEGA_DEC_ARGMAX_SPAN - 1) / OMEGA_DEC_ARGMAX_SPAN, P = ctas * OMEGA_DEC_ARGMAX_SPAN;
208 uint16_t *head = read_all(dir, "head.bf16", (size_t)n * k * 2);
209 uint8_t *codes = read_all(dir, "fp8b.codes", (size_t)n * k);
210 uint16_t *scales = read_all(dir, "fp8b.scales", (size_t)(n / 128) * (k / 128) * 2);
211 float *coef = read_all(dir, "coef.f32", (size_t)n * 4);
212 FILE *hf = fopen(hidden_path, "rb");
213 CHECK(head && codes && scales && coef && hf, "chip inputs readable (export dir %s, hidden %s)", dir, hidden_path);
214 if (!head || !codes || !scales || !coef || !hf) return;
215 fseek(hf, 0, SEEK_END); const size_t rec_bytes = 16 + 4 * (size_t)k, nrec_all = (size_t)ftell(hf) / rec_bytes; fseek(hf, 0, SEEK_SET);
216 const size_t nrec = limit && limit < nrec_all ? limit : nrec_all;
217 uint8_t *recs = malloc(nrec * rec_bytes);
218 CHECK(recs && fread(recs, rec_bytes, nrec, hf) == nrec, "read %zu hidden records", nrec);
219 fclose(hf);
220 OmegaGpuBf16Tensor *tb = NULL; OmegaGpuFp8Tensor *t8 = NULL;
221 int rb = omega_gpu_bf16_tensor_upload(n, k, head, &tb), r8 = omega_gpu_fp8_tensor_upload(1, n, k, codes, scales, &t8);
222 CHECK(rb == 0 && r8 == 0, "upload bf16 head (%d) and fp8b screen (%d): %s", rb, r8, omega_gpu_session_last_error());
223 free(head); free(codes); free(scales);
224 if (rb || r8) return;
225 float *xw = calloc(k, 4), *yw = malloc((size_t)n * 4); xw[0] = 1.0f;
226 CHECK(omega_gpu_bf16_gemv_f32(tb, xw, yw, NULL) == 0 && omega_gpu_fp8_gemv_f32(t8, 0, xw, yw, NULL) == 0, "warm-up launches");
227 free(xw); free(yw);
228 XKern kb, ka0, ka1;
229 /* cmdlist_create takes the session lock itself: create the lists before holding it (it is not recursive) */
230 OmegaGpuCmdList *clF = NULL, *clT = NULL;
231 int cr = omega_gpu_cmdlist_create(16, &clF) | omega_gpu_cmdlist_create(16, &clT);
232 omega_gpu_session_lock();
233 int kr = xkern(&kb, OMEGA_DEC_XBOUND, 0, mutant) | xkern(&ka0, OMEGA_DEC_ARGMAX, 0, 0) | xkern(&ka1, OMEGA_DEC_ARGMAX, 1, 0);
234 /* one buffer: h | h8 | s | coef | hi | lo (P) | y (P) | yf (P) | amv | ami | tau | tok | tokf | tab */
235 const size_t o_h = 0, o_h8 = o_h + k * 4, o_s = o_h8 + k * 4, o_c = o_s + (size_t)n * 4, o_hi = o_c + (size_t)n * 4, o_lo = o_hi + (size_t)n * 4,
236 o_y = o_lo + (size_t)P * 4, o_yf = o_y + (size_t)P * 4, o_amv = o_yf + (size_t)P * 4, o_ami = o_amv + 8192, o_tau = o_ami + 8192,
237 o_tok = o_tau + 16, o_tokf = o_tok + 16, o_tab = o_tokf + 16, total = o_tab + 64;
238 OmegaGpuMem buf; memset(&buf, 0, sizeof buf);
239 int ar = omega_gpu_session_alloc(total, &buf);
240 CHECK(kr == 0 && ar == 0 && cr == 0, "kernels, buffer (%zu bytes) and command lists", total);
241 if (kr || ar || cr) { omega_gpu_session_unlock(); return; }
242 uint8_t *B = buf.cpu; const uint64_t V = buf.va;
243 memcpy(B + o_c, coef, (size_t)n * 4); free(coef);
244 for (uint32_t i = n; i < P; i++) { ((float *)(B + o_lo))[i] = NEG_PAD; ((float *)(B + o_y))[i] = NEG_PAD; ((float *)(B + o_yf))[i] = NEG_PAD; }
245 for (uint32_t i = 0; i < 2048; i++) ((float *)(B + o_amv))[i] = NEG_PAD;
246 uint64_t wva = 0, sva = 0; omega_gpu_fp8_tensor_entry(t8, 0, &wva, &sva);
247 memcpy(B + o_tab, &wva, 8); memcpy(B + o_tab + 8, &sva, 8);
248 uint64_t *tf = malloc(nrec * 8), *tt = malloc(nrec * 8);
249 size_t same = 0, host_same = 0, bits_bad = 0, missing = 0, csum = 0, cmax = 0, sub_fail = 0, *cs = malloc(nrec * sizeof *cs);
250 OmegaGpuCmdListOpts opts; memset(&opts, 0, sizeof opts);
251 for (size_t r = 0; r < nrec; r++) {
252 const uint8_t *rec = recs + r * rec_bytes; uint32_t host_full; memcpy(&host_full, rec + 12, 4);
253 float *h = (float *)(B + o_h), *h8 = (float *)(B + o_h8);
254 memcpy(h, rec + 16, (size_t)k * 4);
255 for (uint32_t i = 0; i < k; i++) h8[i] = h[i] * 0x1p64f; /* exact: a power of two */
256 omega_gpu_cmdlist_reset(clF); omega_gpu_cmdlist_reset(clT);
257 int e = omega_gpu_session_sync_to_gpu(&buf);
258 e |= omega_gpu_bf16_gemv_record(clF, tb, V + o_h, V + o_yf);
259 e |= xargmax(clF, &ka0, &ka1, ctas, V + o_yf, V + o_amv, V + o_ami, V + o_tokf + 4, V + o_tokf);
260 e |= omega_gpu_fp8_gemv_grouped_record(clT, t8, 1, V + o_tab, V + o_h8, 1, V + o_s);
261 uint32_t xb[OMEGA_DEC_XB_WORDS]; memset(xb, 0, sizeof xb);
262 put64v(xb, OMEGA_DEC_XB_S, V + o_s); put64v(xb, OMEGA_DEC_XB_C, V + o_c); put64v(xb, OMEGA_DEC_XB_H, V + o_h);
263 put64v(xb, OMEGA_DEC_XB_LO, V + o_lo); put64v(xb, OMEGA_DEC_XB_HI, V + o_hi); xb[(OMEGA_DEC_XB_N - 0x380u) / 4] = n;
264 e |= xadd(clT, &kb, (n + OMEGA_DEC_THREADS - 1) / OMEGA_DEC_THREADS, n, xb, OMEGA_DEC_XB_WORDS);
265 e |= xargmax(clT, &ka0, &ka1, ctas, V + o_lo, V + o_amv, V + o_ami, V + o_tau, V + o_tau + 4);
266 e |= omega_gpu_bf16_gemv_mask_record(clT, tb, V + o_h, V + o_y, V + o_hi, V + o_tau);
267 e |= xargmax(clT, &ka0, &ka1, ctas, V + o_y, V + o_amv, V + o_ami, V + o_tok + 4, V + o_tok);
268 OmegaGpuCmdListStats sf, st; memset(&sf, 0, sizeof sf); memset(&st, 0, sizeof st);
269 if (!e) e = omega_gpu_cmdlist_submit(clF, &opts, &sf);
270 if (!e) e = omega_gpu_cmdlist_submit(clT, &opts, &st);
271 if (!e) e = omega_gpu_session_sync_to_cpu(&buf);
272 if (e) { sub_fail++; printf("record %zu: chip error: %s\n", r, omega_gpu_session_last_error()); break; }
273 uint32_t tok, tokf; memcpy(&tok, B + o_tok, 4); memcpy(&tokf, B + o_tokf, 4);
274 const float *y = (const float *)(B + o_y), *yf = (const float *)(B + o_yf);
275 size_t c = 0;
276 for (uint32_t i = 0; i < n; i++) {
277 if (y[i] == -INFINITY) continue;
278 c++; bits_bad += memcmp(&y[i], &yf[i], 4) != 0;
279 }
280 missing += tokf < n && y[tokf] == -INFINITY;
281 same += tok == tokf; host_same += tokf == host_full;
282 cs[r] = c; csum += c; if (c > cmax) cmax = c;
283 tf[r] = sf.elapsed_ns; tt[r] = st.elapsed_ns;
284 if (tok != tokf && mutant == 0) printf("record %zu: two-pass %u != full %u (|C| %zu)\n", r, tok, tokf, c);
285 }
286 omega_gpu_session_unlock();
287 const size_t done = nrec - sub_fail;
288 if (mutant == 0) {
289 CHECK(sub_fail == 0 && same == done, "two-pass token == full head token on %zu / %zu records", same, done);
290 CHECK(bits_bad == 0 && missing == 0, "candidate logits bit-identical to the full head (%zu differ, %zu full argmax missing)", bits_bad, missing);
291 } else CHECK(sub_fail == 0 && same < done, "mutant %d must lose a token: %zu / %zu identical", mutant, same, done);
292 double cmean = done ? (double)csum / done : 0, c99 = done ? (double)pct(cs, done, 0.99) : 0;
293 double f50 = pct(tf, done, 0.5) / 1e3, t50 = pct(tt, done, 0.5) / 1e3, f99 = pct(tf, done, 0.99) / 1e3, t99 = pct(tt, done, 0.99) / 1e3;
294 printf("chip: %zu records, identical %zu, host-full agrees %zu, |C| mean %.1f p99 %.0f max %zu\n", done, same, host_same, cmean, c99, cmax);
295 printf("chip: full head list p50 %.1f us p99 %.1f us; two-pass list p50 %.1f us p99 %.1f us\n", f50, f99, t50, t99);
296 if (out_path) {
297 FILE *o = fopen(out_path, "w");
298 if (o) {
299 fprintf(o, "{\n \"test\": \"gpu_exact_head_test\", \"mutant\": %d, \"records\": %zu, \"identical\": %zu, \"host_full_agrees\": %zu,\n", mutant, done, same, host_same);
300 fprintf(o, " \"candidate_bits_differ\": %zu, \"full_argmax_missing\": %zu, \"C_mean\": %.3f, \"C_p99\": %.0f, \"C_max\": %zu,\n", bits_bad, missing, cmean, c99, cmax);
301 fprintf(o, " \"full_list_us\": { \"p50\": %.2f, \"p99\": %.2f }, \"two_pass_list_us\": { \"p50\": %.2f, \"p99\": %.2f },\n", f50, f99, t50, t99);
302 fprintf(o, " \"bytes\": { \"full_head\": %" PRIu64 ", \"fp8b_screen\": %" PRIu64 ", \"coef\": %u, \"row\": %u }\n}\n",
303 (uint64_t)n * k * 2, (uint64_t)n * k + (uint64_t)(n / 128) * (k / 128) * 2, n * 4, k * 2);
304 fclose(o);
305 }
306 }
307 free(recs); free(tf); free(tt); free(cs);
308}
309
310int main(int argc, char **argv) {
311 int sim = 0, mutant = 0; uint32_t limit = 0; const char *dir = NULL, *hidden = NULL, *out = NULL;
312 for (int i = 1; i < argc; i++) {
313 if (strcmp(argv[i], "--sim") == 0) sim = 1;
314 else if (strcmp(argv[i], "--chip") == 0 && i + 2 < argc) { dir = argv[++i]; hidden = argv[++i]; }
315 else if (strcmp(argv[i], "--limit") == 0 && i + 1 < argc) limit = (uint32_t)strtoul(argv[++i], NULL, 10);
316 else if (strcmp(argv[i], "--mutant") == 0 && i + 1 < argc) mutant = atoi(argv[++i]);
317 else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out = argv[++i];
318 else { fprintf(stderr, "usage: gpu_exact_head_test --sim | --chip <export-dir> <hidden.bin> [--limit N] [--mutant 1] [--out receipt.json]\n"); return 2; }
319 }
320 if (sim) sim_battery();
321 if (dir) chip(dir, hidden, limit, mutant, out);
322 printf("gpu_exact_head_test: %u checks, %u failed: %s\n", g_checks, g_failed, g_failed || !g_checks ? "FAIL" : "PASS");
323 return g_failed || !g_checks ? 1 : 0;
324}