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_pfhost_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_pfhost_test.c` 279 lines · 16832 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* gpu_pfhost_test.c (OWN-30B W-L1G-D, 2026-10-14): router logits and MoE combine for the whole-prompt prefill 3 * (omega_blackwell_codegen_pfhost.h). Both kernels must equal the host loops of the Rust driver BIT FOR BIT. 4 * --host-only reference self-checks (packing, the -0.0 start, mutant references differ) 5 * --sim kernels in the warp simulator bit-identical to the reference, both scheduling orders; mutants caught 6 * --chip (under flock /tmp/aien-gb10.lock) through the device API at --tokens rows: bit-identical, mutants caught, medians 7 * Verdict line last: "GPU_PFHOST: PASS|FAIL". */ 8 #include "omega_blackwell_codegen_pfhost.h" 9 #include "omega_gpu_pfhost.h" 10 #include "omega_gpu_prefill.h" 11 #include "bw_warp_sim_tc.h" 12 #include 13 #include 14 #include 15 #include 16 17 static uint64_t rng = 0x9e3779b97f4a7c15ull; 18 static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)(rng >> 16); } 19 static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } 20 static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } 21 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; } 22 static float rand_unit(void) { return ((float)(rnd() & 0xffffff) / `16777216`.0f) * 2.0f - 1.0f; } 23 /* router weights as the checkpoint has them: bf16 values widened to f32 */ 24 static float rand_bf16w(void) { return u2f(f2u(rand_unit() * 0.08f) & 0xffff0000u); } 25 /* activations: RMSNorm-like magnitudes with some outliers and exact zeros */ 26 static float rand_act(void) { float v = rand_unit() * 2.0f; int r = rnd() % 64; if (r == 0) v *= 40.0f; else if (r == 1) v = 0.0f; return v; } 27 28 /* ---------------- references (compiled with -ffp-contract=off: a * b then +, as Rust) ---------------- */ 29 static void rl_ref(const float *x, const float *w, uint32_t rows, uint32_t k, uint32_t e, float *lg, int mutant) { 30 const uint32_t kk = mutant == 2 ? k - 128u : k; 31 for (uint32_t r = 0; r < rows; r++) 32 for (uint32_t ex = 0; ex < e; ex++) { 33 float acc = -0.0f; 34 for (uint32_t j = 0; j < kk; j++) acc = mutant == 1 ? fmaf(w[(size_t)ex * k + j], x[(size_t)r * k + j], acc) : acc + w[(size_t)ex * k + j] * x[(size_t)r * k + j]; 35 lg[(size_t)r * e + ex] = acc; 36 } 37 } 38 static void mc_ref(const float *down, const uint32_t *inv, const float *wt, uint32_t rows, uint32_t h, uint32_t K, float *out, int mutant) { 39 const uint32_t used = mutant == 2 ? K - 1u : K; 40 for (uint32_t r = 0; r < rows; r++) 41 for (uint32_t c = 0; c < h; c++) { 42 float acc = 0.0f; 43 for (uint32_t s = 0; s < used; s++) { 44 const float d = down[(size_t)inv[(size_t)r * K + s] * h + c], ww = wt[(size_t)r * K + s]; 45 acc = mutant == 1 ? fmaf(ww, d, acc) : acc + ww * d; 46 } 47 out[(size_t)r * h + c] = acc; 48 } 49 } 50 static size_t diff_bits(const float *a, const float *b, size_t n) { size_t bad = 0; for (size_t i = 0; i < n; i++) if (f2u(a[i]) != f2u(b[i])) bad++; return bad; } 51 52 typedef struct { uint32_t rows, k, e, t, rp; float *x, *w, *wq, *ref; } RlProb; 53 static void rl_make(RlProb *p, uint32_t rows, uint32_t k, uint32_t e, uint32_t t) { 54 p->rows = rows; p->k = k; p->e = e; p->t = t; p->rp = (rows + t - 1u) / t * t; 55 p->x = malloc((size_t)p->rp * k * 4); p->w = malloc((size_t)e * k * 4); 56 p->wq = malloc(omega_gpu_pf_router_packed_bytes(e, k)); p->ref = malloc((size_t)rows * e * 4); 57 for (size_t i = 0; i < (size_t)p->rp * k; i++) p->x[i] = i < (size_t)rows * k ? rand_act() : u2f(0x7fc00000u); /* pad rows NaN: must not leak */ 58 for (size_t i = 0; i < (size_t)e * k; i++) p->w[i] = rand_bf16w(); 59 if (rows > 2) memset(p->x + 2 * (size_t)k, 0, k * 4); /* an all-zero row: every logit -0.0 + 0 products */ 60 omega_gpu_pf_router_pack(e, k, p->w, p->wq); 61 rl_ref(p->x, p->w, rows, k, e, p->ref, 0); 62 } 63 static void rl_free(RlProb *p) { free(p->x); free(p->w); free(p->wq); free(p->ref); } 64 65 typedef struct { uint32_t rows, h, K; float *down, *wt, *ref; uint32_t *inv; } McProb; 66 static void mc_make(McProb *p, uint32_t rows, uint32_t h, uint32_t K) { 67 p->rows = rows; p->h = h; p->K = K; 68 const size_t g = (size_t)rows * K; 69 p->down = malloc(g * h * 4); p->wt = malloc(g * 4); p->inv = malloc(g * 4); p->ref = malloc((size_t)rows * h * 4); 70 for (size_t i = 0; i < g * h; i++) p->down[i] = rand_act() * 0.5f; 71 for (size_t i = 0; i < g; i++) { p->wt[i] = (float)(rnd() & 0xffff) / 65536.0f * 0.4f; p->inv[i] = (uint32_t)i; } 72 for (size_t i = g; i > 1; i--) { size_t j = rnd() % i; uint32_t t = p->inv[i - 1]; p->inv[i - 1] = p->inv[j]; p->inv[j] = t; } /* a permutation, like the expert sort */ 73 mc_ref(p->down, p->inv, p->wt, rows, h, K, p->ref, 0); 74 } 75 static void mc_free(McProb *p) { free(p->down); free(p->wt); free(p->inv); free(p->ref); } 76 77 /* ---------------- host-only ---------------- */ 78 static int host_checks(void) { 79 int fail = 0; 80 /* packing: wq[g][ex][q] = w[ex][4g + q], pad group zero */ 81 { const uint32_t e = 32, k = 256; float *w = malloc(e * k * 4), *wq = malloc(omega_gpu_pf_router_packed_bytes(e, k)); 82 for (uint32_t i = 0; i < e * k; i++) w[i] = (float)i; 83 omega_gpu_pf_router_pack(e, k, w, wq); 84 size_t bad = 0; 85 for (uint32_t g = 0; g < k / 4; g++) for (uint32_t ex = 0; ex < e; ex++) for (uint32_t q = 0; q < 4; q++) if (wq[(g * e + ex) * 4 + q] != w[ex * k + 4 * g + q]) bad++; 86 for (uint32_t i = 0; i < e * 4; i++) if (f2u(wq[(k / 4) * e * 4 + i]) != 0) bad++; 87 printf("host pack R4: %zu wrong\n", bad); if (bad) fail++; 88 free(w); free(wq); } 89 /* the sum starts at -0.0 (Rust f32 Sum): an all-zero row gives -0.0 + (+0.0 products) = +0.0, a row of -0.0 products stays -0.0 */ 90 { float z = -0.0f; z = z + 0.0f * -1.0f; printf("host -0.0 start: -0 + (-0) = %s\n", signbit(z) ? "-0 (ok)" : "+0 (WRONG)"); if (!signbit(z)) fail++; } 91 /* the mutant references really differ */ 92 { RlProb p; rl_make(&p, 5, 256, 32, 4); float *m = malloc(5 * 32 * 4); 93 for (int mu = 1; mu <= 2; mu++) { rl_ref(p.x, p.w, 5, 256, 32, m, mu); size_t d = diff_bits(m, p.ref, 5 * 32); printf("host router mutant %d reference differs in %zu of 160\n", mu, d); if (!d) fail++; } 94 free(m); rl_free(&p); } 95 { McProb p; mc_make(&p, 3, 128, 8); float *m = malloc(3 * 128 * 4); 96 for (int mu = 1; mu <= 2; mu++) { mc_ref(p.down, p.inv, p.wt, 3, 128, 8, m, mu); size_t d = diff_bits(m, p.ref, 3 * 128); printf("host combine mutant %d reference differs in %zu of 384\n", mu, d); if (!d) fail++; } 97 free(m); mc_free(&p); } 98 return fail; 99 } 100 101 /* ---------------- simulator ---------------- */ 102 static int rl_sim_one(const RlProb *p, int mutant, int order, float *lg, char *msg, size_t msgn) { 103 OmegaRlSpec sp = { p->k, p->e, p->t, mutant }; 104 static BlackwellIRProgram prog; 105 if (omega_blackwell_codegen_rl_prog(&sp, &prog) != 0) { snprintf(msg, msgn, "codegen refused"); return -1; } 106 const size_t lgb = (size_t)p->rp * p->e * 4, wqb = omega_gpu_pf_router_packed_bytes(p->e, p->k); 107 uint8_t *smem = malloc(omega_rl_shared_bytes(&sp)); 108 static WsSim ws; memset(&ws, 0, sizeof ws); 109 ws.p = &prog; ws.threads = p->e; ws.smem = smem; ws.smem_bytes = omega_rl_shared_bytes(&sp); ws.order = order; 110 uint64_t a[3] = { (uintptr_t)p->x, (uintptr_t)p->wq, (uintptr_t)lg }; 111 for (int i = 0; i < 3; i++) { ws.cbank[(0x380 + 8 * i) / 4] = (uint32_t)a[i]; ws.cbank[(0x380 + 8 * i) / 4 + 1] = (uint32_t)(a[i] >> 32); } 112 ws.ranges[0].lo = (uintptr_t)p->x; ws.ranges[0].hi = (uintptr_t)p->x + (size_t)p->rp * p->k * 4; 113 ws.ranges[1].lo = (uintptr_t)p->wq; ws.ranges[1].hi = (uintptr_t)p->wq + wqb; 114 ws.ranges[2].lo = (uintptr_t)lg; ws.ranges[2].hi = (uintptr_t)lg + lgb; 115 ws.nranges = 3; 116 memset(lg, 0xee, lgb); 117 int err = ws_run(&ws, p->rp / p->t, 1); 118 if (err) snprintf(msg, msgn, "simulator %.150s", ws.msg); 119 free(smem); 120 return err ? -1 : 0; 121 } 122 static int mc_sim_one(const McProb *p, int mutant, int order, float *out, char *msg, size_t msgn) { 123 OmegaMcSpec sp = { p->h, p->K, mutant }; 124 static BlackwellIRProgram prog; 125 if (omega_blackwell_codegen_mc_prog(&sp, &prog) != 0) { snprintf(msg, msgn, "codegen refused"); return -1; } 126 const size_t g = (size_t)p->rows * p->K; 127 static WsSim ws; memset(&ws, 0, sizeof ws); 128 ws.p = &prog; ws.threads = p->h / 4; ws.order = order; 129 uint64_t a[4] = { (uintptr_t)p->down, (uintptr_t)p->inv, (uintptr_t)p->wt, (uintptr_t)out }; 130 for (int i = 0; i < 4; i++) { ws.cbank[(0x380 + 8 * i) / 4] = (uint32_t)a[i]; ws.cbank[(0x380 + 8 * i) / 4 + 1] = (uint32_t)(a[i] >> 32); } 131 ws.ranges[0].lo = (uintptr_t)p->down; ws.ranges[0].hi = (uintptr_t)p->down + g * p->h * 4; 132 ws.ranges[1].lo = (uintptr_t)p->inv; ws.ranges[1].hi = (uintptr_t)p->inv + g * 4; 133 ws.ranges[2].lo = (uintptr_t)p->wt; ws.ranges[2].hi = (uintptr_t)p->wt + g * 4; 134 ws.ranges[3].lo = (uintptr_t)out; ws.ranges[3].hi = (uintptr_t)out + (size_t)p->rows * p->h * 4; 135 ws.nranges = 4; 136 memset(out, 0xee, (size_t)p->rows * p->h * 4); 137 int err = ws_run(&ws, p->rows, 1); 138 if (err) snprintf(msg, msgn, "simulator %.150s", ws.msg); 139 return err ? -1 : 0; 140 } 141 static int sim_checks(void) { 142 int fail = 0; char msg[200]; 143 static const struct { uint32_t rows, k, e, t; } rc[] = { { 37, 2048, 128, 16 }, { 9, 256, 32, 4 }, { 21, 384, 64, 8 }, { 16, 2048, 128, 4 } }; 144 for (size_t c = 0; c < sizeof rc / sizeof rc[0]; c++) { 145 RlProb p; rl_make(&p, rc[c].rows, rc[c].k, rc[c].e, rc[c].t); 146 float *lg = malloc((size_t)p.rp * p.e * 4); 147 for (int order = 0; order < 2; order++) { 148 int r = rl_sim_one(&p, 0, order, lg, msg, sizeof msg); 149 size_t bad = r ? 0 : diff_bits(lg, p.ref, (size_t)p.rows * p.e); 150 printf("sim router k %u e %u t %u rows %u order %d: %s, %zu of %zu differ\n", p.k, p.e, p.t, p.rows, order, r ? msg : "ran", bad, (size_t)p.rows * p.e); 151 if (r || bad) fail++; 152 } 153 if (c == 0) for (int mu = 1; mu <= 2; mu++) { 154 int r = rl_sim_one(&p, mu, 0, lg, msg, sizeof msg); 155 size_t bad = r ? 0 : diff_bits(lg, p.ref, (size_t)p.rows * p.e); 156 printf("sim router mutant %d: %s\n", mu, r ? msg : bad ? "caught" : "NOT CAUGHT"); 157 if (r || !bad) fail++; 158 } 159 free(lg); rl_free(&p); 160 } 161 static const struct { uint32_t rows, h, K; } mc[] = { { 7, 2048, 8 }, { 5, 128, 4 }, { 3, 384, 16 } }; 162 for (size_t c = 0; c < sizeof mc / sizeof mc[0]; c++) { 163 McProb p; mc_make(&p, mc[c].rows, mc[c].h, mc[c].K); 164 float *out = malloc((size_t)p.rows * p.h * 4); 165 for (int order = 0; order < 2; order++) { 166 int r = mc_sim_one(&p, 0, order, out, msg, sizeof msg); 167 size_t bad = r ? 0 : diff_bits(out, p.ref, (size_t)p.rows * p.h); 168 printf("sim combine h %u top_k %u rows %u order %d: %s, %zu of %zu differ\n", p.h, p.K, p.rows, order, r ? msg : "ran", bad, (size_t)p.rows * p.h); 169 if (r || bad) fail++; 170 } 171 if (c == 0) for (int mu = 1; mu <= 2; mu++) { 172 int r = mc_sim_one(&p, mu, 0, out, msg, sizeof msg); 173 size_t bad = r ? 0 : diff_bits(out, p.ref, (size_t)p.rows * p.h); 174 printf("sim combine mutant %d: %s\n", mu, r ? msg : bad ? "caught" : "NOT CAUGHT"); 175 if (r || !bad) fail++; 176 } 177 free(out); mc_free(&p); 178 } 179 return fail; 180 } 181 182 /* ---------------- chip ---------------- */ 183 static int alloc_fill(int host, size_t bytes, const void *src, OmegaGpuMem *m) { 184 bytes = (bytes + 15u) & ~(size_t)15u; 185 int rc = host ? omega_gpu_pf_alloc_host(bytes, m) : omega_gpu_pf_alloc_dev(bytes, m); 186 if (rc) return rc; 187 if (!src) return 0; 188 if (host) { memcpy(m->cpu, src, bytes); return 0; } 189 return omega_gpu_pf_upload(m, 0, src, bytes); 190 } 191 static int rl_chip(uint32_t rows, int reps) { 192 int fail = 0; 193 const uint32_t k = 2048, e = 128, t = omega_gpu_pf_router_rows(); 194 RlProb p; rl_make(&p, rows, k, e, t); 195 const size_t lgb = (size_t)p.rp * e * 4; 196 OmegaGpuMem xd = { 0 }, wd = { 0 }, ld = { 0 }; 197 /* as the driver: x and logits in HOST (GPU-uncached) buffers, the packed weights GPU-cached */ 198 int rc = alloc_fill(1, (size_t)p.rp * k * 4, p.x, &xd); 199 if (!rc) rc = alloc_fill(0, omega_gpu_pf_router_packed_bytes(e, k), p.wq, &wd); 200 if (!rc) rc = alloc_fill(1, lgb, NULL, &ld); 201 uint64_t ns[32] = { 0 }; if (reps > 32) reps = 32; if (reps < 1) reps = 1; 202 float *lg = malloc(lgb); 203 for (int r = 0; r < reps && !rc; r++) { memset(ld.cpu, 0xee, lgb); rc = omega_gpu_pf_router_logits(k, e, rows, xd.va, wd.va, ld.va, &ns[r]); } 204 if (rc) { printf("ROUTER chip rows %u: %s (%s)\n", rows, omega_gpu_pf_rc_name(rc), omega_gpu_session_last_error()); fail++; } 205 else { 206 memcpy(lg, ld.cpu, lgb); 207 size_t bad = diff_bits(lg, p.ref, (size_t)rows * e); 208 qsort(ns, (size_t)reps, 8, cmp_u64); 209 printf("ROUTER chip k %u e %u t %u rows %u: %zu of %zu differ from the host loop; median %.3f ms\n", k, e, t, rows, bad, (size_t)rows * e, ns[reps / 2] / 1e6); 210 if (bad) fail++; 211 for (int mu = 1; mu <= 2; mu++) { 212 omega_gpu_pf_router_test_set_mutant(mu); 213 memset(ld.cpu, 0xee, lgb); 214 int mrc = omega_gpu_pf_router_logits(k, e, rows, xd.va, wd.va, ld.va, NULL); 215 omega_gpu_pf_router_test_set_mutant(0); 216 memcpy(lg, ld.cpu, lgb); 217 size_t mb = mrc ? 0 : diff_bits(lg, p.ref, (size_t)rows * e); 218 printf("ROUTER chip mutant %d: %s\n", mu, mrc ? "launch failed" : mb ? "caught" : "NOT CAUGHT"); 219 if (mrc || !mb) fail++; 220 } 221 } 222 free(lg); omega_gpu_pf_free(&xd); omega_gpu_pf_free(&wd); omega_gpu_pf_free(&ld); rl_free(&p); 223 return fail; 224 } 225 static int mc_chip(uint32_t rows, int reps) { 226 int fail = 0; 227 const uint32_t h = 2048, K = 8; 228 McProb p; mc_make(&p, rows, h, K); 229 const size_t g = (size_t)rows * K, ob = (size_t)rows * h * 4; 230 OmegaGpuMem dd = { 0 }, id = { 0 }, wd = { 0 }, od = { 0 }; 231 /* as the driver: down rows GPU-cached (the down GEMM writes them), inv / weights / out in HOST buffers */ 232 int rc = alloc_fill(0, g * h * 4, p.down, &dd); 233 if (!rc) rc = alloc_fill(1, g * 4, p.inv, &id); 234 if (!rc) rc = alloc_fill(1, g * 4, p.wt, &wd); 235 if (!rc) rc = alloc_fill(1, ob, NULL, &od); 236 uint64_t ns[32] = { 0 }; if (reps > 32) reps = 32; if (reps < 1) reps = 1; 237 float *out = malloc(ob); 238 for (int r = 0; r < reps && !rc; r++) { memset(od.cpu, 0xee, ob); rc = omega_gpu_pf_moe_combine(h, K, rows, dd.va, id.va, wd.va, od.va, &ns[r]); } 239 if (rc) { printf("COMBINE chip rows %u: %s (%s)\n", rows, omega_gpu_pf_rc_name(rc), omega_gpu_session_last_error()); fail++; } 240 else { 241 memcpy(out, od.cpu, ob); 242 size_t bad = diff_bits(out, p.ref, (size_t)rows * h); 243 qsort(ns, (size_t)reps, 8, cmp_u64); 244 printf("COMBINE chip h %u top_k %u rows %u: %zu of %zu differ from the host loop; median %.3f ms\n", h, K, rows, bad, (size_t)rows * h, ns[reps / 2] / 1e6); 245 if (bad) fail++; 246 for (int mu = 1; mu <= 2; mu++) { 247 omega_gpu_pf_combine_test_set_mutant(mu); 248 memset(od.cpu, 0xee, ob); 249 int mrc = omega_gpu_pf_moe_combine(h, K, rows, dd.va, id.va, wd.va, od.va, NULL); 250 omega_gpu_pf_combine_test_set_mutant(0); 251 memcpy(out, od.cpu, ob); 252 size_t mb = mrc ? 0 : diff_bits(out, p.ref, (size_t)rows * h); 253 printf("COMBINE chip mutant %d: %s\n", mu, mrc ? "launch failed" : mb ? "caught" : "NOT CAUGHT"); 254 if (mrc || !mb) fail++; 255 } 256 } 257 free(out); omega_gpu_pf_free(&dd); omega_gpu_pf_free(&id); omega_gpu_pf_free(&wd); omega_gpu_pf_free(&od); mc_free(&p); 258 return fail; 259 } 260 261 int main(int argc, char **argv) { 262 int host = 1, sim = 1, chip = 0, reps = 7; uint32_t tokens = 2048; 263 for (int i = 1; i < argc; i++) { 264 if (!strcmp(argv[i], "--host-only")) sim = 0; 265 else if (!strcmp(argv[i], "--sim")) host = 0; 266 else if (!strcmp(argv[i], "--chip")) { chip = 1; host = 0; sim = 0; } 267 else if (!strcmp(argv[i], "--tokens") && i + 1 < argc) tokens = (uint32_t)atoi(argv[++i]); 268 else if (!strcmp(argv[i], "--reps") && i + 1 < argc) reps = atoi(argv[++i]); 269 } 270 int fail = 0; 271 if (host) fail += host_checks(); 272 if (sim) fail += sim_checks(); 273 if (chip) { 274 fail += rl_chip(tokens, reps); fail += rl_chip(37, 3); 275 fail += mc_chip(tokens, reps); fail += mc_chip(7, 3); 276 } 277 printf("GPU_PFHOST: %s\n", fail ? "FAIL" : "PASS"); 278 return fail ? 1 : 0; 279 }