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