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_qgemm_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_qgemm_test.c` 528 lines · 36334 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* gpu_qgemm_test.c (OWN-30B L1 GEMM, W-L1G, 2026-10-14): the native FP8 prefill path (omega_blackwell_codegen_qg.h). 3 * --host-only reference self-checks 4 * --sim kernels in the warp simulator bit-identical to the C reference; mutants caught (a simulator error is a FAIL) 5 * --chip (under flock /tmp/aien-gb10.lock) through the device API: bit-identical to the reference, mutants caught, 6 * medians at --tokens 7 * Verdict line last: "GPU_QGEMM: PASS|FAIL". */ 8 #include "omega_blackwell_codegen_qg.h" 9 #include "omega_gpu_prefill.h" 10 #include "omega_gpu_fp8_api.h" 11 #include "bw_warp_sim_tc.h" 12 #include 13 #include 14 #include 15 #include 16 17 static uint64_t rng = 0x243f6a8885a308d3ull; 18 static uint32_t rnd(void) { rng ^= rng << 13; rng ^= rng >> 7; rng ^= rng << 17; return (uint32_t)(rng >> 16); } 19 static float u2f(uint32_t u) { float f; memcpy(&f, &u, 4); return f; } 20 static uint32_t f2u(float f) { uint32_t u; memcpy(&u, &f, 4); return u; } 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 23 /* ---------------- quantiser reference (header semantics) ---------------- */ 24 static void pq_ref(const float *x, uint32_t k, uint8_t *q, float *s, int mutant) { 25 for (uint32_t b = 0; b < k / 128; b++) { 26 float m = ldexpf(1.0f, -100); 27 if (mutant == 1) { /* lane-local amax: lane l sees elements 4l..4l+3 only (scale per lane; the reference keeps lane 31's) */ 28 for (uint32_t l = 0; l < 32; l++) { float ml = ldexpf(1.0f, -100); for (int j = 0; j < 4; j++) ml = fmaxf(ml, fabsf(x[128 * b + 4 * l + j])); if (l == 31) m = ml; } 29 } else for (uint32_t j = 0; j < 128; j++) m = fmaxf(m, fabsf(x[128 * b + j])); 30 uint32_t mb = f2u(m), E = mb >> 23, M = mb & 0x7fffff, c = mutant == 2 ? 0 : (M + 0x1fffff) >> 23; 31 float scale = u2f((E + c - 8) << 23), inv = u2f((262 - E - c) << 23); 32 s[b] = scale; 33 for (uint32_t j = 0; j < 128; j++) q[128 * b + j] = qm_e4m3_rne_sat(x[128 * b + j] * inv); 34 } 35 } 36 static float rand_act(void) { 37 float v = ((float)(rnd() & 0xffffff) / `16777216`.0f) * 2.0f - 1.0f; 38 int r = rnd() % 32; if (r == 0) v *= 300.0f; else if (r == 1) v *= 1e-3f; else if (r == 2) v = 0.0f; 39 return v; 40 } 41 static uint16_t bf16_rn(float f) { uint32_t u = f2u(f); return (uint16_t)((u + 0x7fffu + ((u >> 16) & 1u)) >> 16); } 42 /* rows x k activations; row 3 all zero, row 5 one huge outlier; bf16 copy when in_type is BF16 */ 43 static void make_x(uint32_t rows, uint32_t k, uint32_t in_type, float *x, uint16_t *xb) { 44 for (size_t i = 0; i < (size_t)rows * k; i++) x[i] = rand_act(); 45 if (rows > 3) memset(x + 3 * (size_t)k, 0, k * 4); 46 if (rows > 5) x[5 * (size_t)k + 77] = 3.0e4f; 47 if (in_type == OMEGA_QG_IN_BF16) for (size_t i = 0; i < (size_t)rows * k; i++) { xb[i] = bf16_rn(x[i]); x[i] = u2f((uint32_t)xb[i] << 16); } 48 } 49 static size_t pq_compare(const float *x, uint32_t rows, uint32_t k, const uint8_t *q, const float *s, int mutant) { 50 size_t bad = 0; uint8_t *rq = malloc(k); float *rs = malloc(k / 128 * 4); 51 for (uint32_t r = 0; r < rows; r++) { 52 pq_ref(x + (size_t)r * k, k, rq, rs, mutant); 53 for (uint32_t j = 0; j < k; j++) if (rq[j] != q[(size_t)r * k + j]) bad++; 54 for (uint32_t b = 0; b < k / 128; b++) if (f2u(rs[b]) != f2u(s[(size_t)r * (k / 128) + b])) bad++; 55 } 56 free(rq); free(rs); return bad; 57 } 58 59 static int pq_host(void) { 60 int fail = 0; 61 /* scale choice: amax / scale in (224, 448] for normal amax */ 62 for (int i = 0; i < 100000; i++) { 63 float x[128]; for (int j = 0; j < 128; j++) x[j] = rand_act() * ldexpf(1.0f, (int)(rnd() % 40) - 20); 64 uint8_t q[128]; float s[1]; pq_ref(x, 128, q, s, 0); 65 float m = 0; for (int j = 0; j < 128; j++) m = fmaxf(m, fabsf(x[j])); 66 if (m > 0 && !(m / s[0] <= 448.0f && m / s[0] > 224.0f)) { if (fail < 3) printf("scale out of range: amax %g scale %g\n", m, s[0]); fail++; } 67 double err = 0; for (int j = 0; j < 128; j++) { double d = fabs((double)qm_e4m3(q[j]) * s[0] - x[j]); if (d > err) err = d; } 68 if (m > 0 && err > m * (1.0 / 16 + 1e-6)) { if (fail < 3) printf("quantisation error %g vs amax %g\n", err, m); fail++; } 69 } 70 printf("quantiser reference: %s\n", fail ? "FAIL" : "PASS (amax/scale in (224, 448], error within half an e4m3 ulp)"); 71 return fail; 72 } 73 74 static int pq_sim_one(uint32_t k, uint32_t in_type, uint32_t groups, uint32_t rows, int mutant, size_t *bad_out, char *msg, size_t msgn) { 75 OmegaPqSpec sp = { k, in_type, mutant, groups }; 76 static BlackwellIRProgram prog; 77 if (omega_blackwell_codegen_pq_prog(&sp, &prog) != 0) { snprintf(msg, msgn, "codegen refused"); return -1; } 78 float *x = malloc((size_t)rows * k * 4); uint16_t *xb = malloc((size_t)rows * k * 2); 79 uint8_t *q = malloc((size_t)rows * k); float *s = malloc((size_t)rows * (k / 128) * 4); 80 make_x(rows, k, in_type, x, xb); 81 memset(q, 0xee, (size_t)rows * k); memset(s, 0xee, (size_t)rows * (k / 128) * 4); 82 static WsSim ws; memset(&ws, 0, sizeof ws); 83 ws.p = &prog; ws.threads = 32u * (groups ? groups : k / 128); 84 const void *xin = in_type == OMEGA_QG_IN_F32 ? (const void *)x : (const void *)xb; 85 uint64_t a[3] = { (uintptr_t)xin, (uintptr_t)q, (uintptr_t)s }; 86 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); } 87 ws.ranges[0].lo = (uintptr_t)xin; ws.ranges[0].hi = (uintptr_t)xin + (size_t)rows * k * (in_type == OMEGA_QG_IN_F32 ? 4 : 2); 88 ws.ranges[1].lo = (uintptr_t)q; ws.ranges[1].hi = (uintptr_t)q + (size_t)rows * k; 89 ws.ranges[2].lo = (uintptr_t)s; ws.ranges[2].hi = (uintptr_t)s + (size_t)rows * (k / 128) * 4; 90 ws.nranges = 3; 91 int err = ws_run(&ws, rows, groups ? k / 128 / groups : 1); 92 if (err) snprintf(msg, msgn, "simulator %.150s", ws.msg); 93 *bad_out = err ? 0 : pq_compare(x, rows, k, q, s, 0); 94 free(x); free(xb); free(q); free(s); 95 return err ? -1 : 0; 96 } 97 static int pq_sim(void) { 98 int fail = 0; char msg[200]; 99 /* groups > 0: one row split over k / (128 groups) CTAs (the Qwen3.8 widths 5120, 6144, 17408) */ 100 static const struct { uint32_t k, in_type, groups; } cs[] = { { 2048, OMEGA_QG_IN_F32, 0 }, { 768, OMEGA_QG_IN_BF16, 0 }, { 4096, OMEGA_QG_IN_F32, 0 }, { 128, OMEGA_QG_IN_BF16, 0 }, 101 { 5120, OMEGA_QG_IN_F32, 20 }, { 6144, OMEGA_QG_IN_BF16, 24 }, { 17408, OMEGA_QG_IN_BF16, 17 }, { 2048, OMEGA_QG_IN_F32, 4 } }; 102 for (size_t c = 0; c < sizeof cs / sizeof cs[0]; c++) { 103 size_t bad = 0; int rc = pq_sim_one(cs[c].k, cs[c].in_type, cs[c].groups, 9, 0, &bad, msg, sizeof msg); 104 printf("sim quantiser k %u %s groups %u: %s, %zu differ\n", cs[c].k, cs[c].in_type ? "bf16" : "f32", cs[c].groups, rc ? msg : "ran", bad); 105 if (rc || bad) fail++; 106 } 107 for (int m = 1; m <= 3; m++) { 108 size_t bad = 0; int rc = pq_sim_one(m == 3 ? 5120 : 2048, OMEGA_QG_IN_F32, m == 3 ? 20 : 0, 9, m, &bad, msg, sizeof msg); 109 printf("sim quantiser mutant %d: %s\n", m, rc ? msg : bad ? "caught" : "NOT CAUGHT"); 110 if (rc || !bad) fail++; 111 } 112 return fail; 113 } 114 115 static int pq_chip(uint32_t tokens, int reps) { 116 int fail = 0; 117 /* k > 4096 runs the split-row form (omega_gpu_pf_quant picks groups); its mutant drops the ctaid.y offset */ 118 static const struct { uint32_t k, in_type, rows_mul; } cs[] = { { 2048, OMEGA_QG_IN_F32, 1 }, { 768, OMEGA_QG_IN_BF16, 8 }, { 5120, OMEGA_QG_IN_F32, 1 }, 119 { 6144, OMEGA_QG_IN_BF16, 1 }, { 17408, OMEGA_QG_IN_BF16, 1 } }; 120 for (size_t c = 0; c < sizeof cs / sizeof cs[0]; c++) { 121 const uint32_t k = cs[c].k, rows = tokens * cs[c].rows_mul, eb = cs[c].in_type == OMEGA_QG_IN_F32 ? 4 : 2; 122 float *x = malloc((size_t)rows * k * 4); uint16_t *xb = malloc((size_t)rows * k * 2); 123 uint8_t *q = malloc((size_t)rows * k); float *s = malloc((size_t)rows * (k / 128) * 4); 124 make_x(rows, k, cs[c].in_type, x, xb); 125 OmegaGpuMem xd = { 0 }, qd = { 0 }, sd = { 0 }; 126 const size_t xbytes = (size_t)rows * k * eb, qbytes = (size_t)rows * k, sbytes = ((size_t)rows * (k / 128) * 4 + 15) & ~(size_t)15; 127 int rc = omega_gpu_pf_alloc_dev(xbytes, &xd); 128 if (!rc) rc = omega_gpu_pf_alloc_dev(qbytes, &qd); 129 if (!rc) rc = omega_gpu_pf_alloc_dev(sbytes, &sd); 130 if (!rc) rc = omega_gpu_pf_upload(&xd, 0, cs[c].in_type == OMEGA_QG_IN_F32 ? (const void *)x : (const void *)xb, xbytes); 131 uint64_t ns[32] = { 0 }; if (reps > 32) reps = 32; 132 for (int r = 0; r < reps && !rc; r++) rc = omega_gpu_pf_quant(k, cs[c].in_type, rows, xd.va, qd.va, sd.va, &ns[r]); 133 if (!rc) rc = omega_gpu_pf_download(&qd, 0, q, qbytes); 134 if (!rc) { float *st = malloc(sbytes); rc = omega_gpu_pf_download(&sd, 0, st, sbytes); memcpy(s, st, (size_t)rows * (k / 128) * 4); free(st); } 135 if (rc) { printf("quantiser chip k %u: %s (%s)\n", k, omega_gpu_pf_rc_name(rc), omega_gpu_session_last_error()); fail++; } 136 else { 137 size_t bad = pq_compare(x, rows, k, q, s, 0); 138 qsort(ns, reps, 8, cmp_u64); 139 printf("QUANT chip k %u %s rows %u: %zu differ from the reference; median %.3f ms (%.0f GB/s moved)\n", k, cs[c].in_type ? "bf16" : "f32", rows, bad, 140 ns[reps / 2] / 1e6, (double)(xbytes + qbytes + sbytes) / ns[reps / 2]); 141 if (bad) fail++; 142 const int mut = k > 4096 ? 3 : 1; 143 /* junk-fill the output first: mutant 3 rewrites groups 0 .. G-1 with right values and leaves the rest alone, 144 so stale results of the good run would hide it */ 145 memset(q, 0xee, qbytes); 146 int mrc = omega_gpu_pf_upload(&qd, 0, q, qbytes); 147 omega_gpu_pf_quant_test_set_mutant(mut); 148 if (!mrc) mrc = omega_gpu_pf_quant(k, cs[c].in_type, rows, xd.va, qd.va, sd.va, NULL); 149 omega_gpu_pf_quant_test_set_mutant(0); 150 if (!mrc) mrc = omega_gpu_pf_download(&qd, 0, q, qbytes); 151 size_t mb = mrc ? 0 : pq_compare(x, rows > 64 ? 64 : rows, k, q, s, 0); 152 printf("QUANT chip k %u mutant %d: %s\n", k, mut, mrc ? "launch failed" : mb ? "caught" : "NOT CAUGHT"); 153 if (mrc || !mb) fail++; 154 } 155 omega_gpu_pf_free(&xd); omega_gpu_pf_free(&qd); omega_gpu_pf_free(&sd); 156 free(x); free(xb); free(q); free(s); 157 } 158 return fail; 159 } 160 161 /* ---------------- grouped GEMM on QMMA ---------------- */ 162 static const QmmaModel GB10Q = { 32, 26, 0, 2, 0 }; /* chip-exact, evidence OWN30B-L1G-QMMA-PFGEMM-`c742aa13` */ 163 typedef struct { 164 uint32_t E, n, k, M, xr, layout, direct, pad; 165 uint8_t *w, *xq; /* weights [E][n][k] (checkpoint order, read in place), quantised activations (xr + pad rows) */ 166 uint16_t *sbf; /* scale_inv bf16 */ 167 float *swf, *xs, *x; /* GEMV-tensor scale table [E][n/128][KB] f32 * 2^56, activation scales (xr + pad rows), true activations */ 168 uint32_t *eid, *src, *counts; 169 } QProb; 170 static uint8_t rand_e4m3(void) { for (;;) { uint8_t c = (uint8_t)rnd(); if ((c & 0x7f) != 0x7f) return c; } } 171 static void qprob_make(QProb *p, uint32_t E, uint32_t n, uint32_t k, uint32_t M, uint32_t xr, uint32_t layout, uint32_t direct, uint32_t pad) { 172 memset(p, 0, sizeof *p); 173 p->E = E; p->n = n; p->k = k; p->M = M; p->xr = direct ? M : xr; p->layout = layout; p->direct = direct; p->pad = pad; 174 const uint32_t KB = k / 128, XR = p->xr + pad; 175 const size_t wn = (size_t)E * n * k, sn = (size_t)E * (n / 128) * KB; 176 p->w = malloc(wn); p->sbf = malloc(sn * 2); p->swf = malloc(sn * 4); 177 p->x = calloc((size_t)XR * k, 4); p->xq = calloc((size_t)XR * k, 1); p->xs = malloc((size_t)XR * KB * 4); 178 p->eid = malloc(M * 4); p->src = malloc(M * 4); p->counts = calloc(E, 4); 179 for (size_t i = 0; i < wn; i++) p->w[i] = rand_e4m3(); 180 for (size_t i = 0; i < sn; i++) { float s = ldexpf(1.0f + (float)(rnd() & 127) / 128.0f, -(int)(rnd() % 12) - 4); p->sbf[i] = (uint16_t)(f2u(s) >> 16); } 181 if (omega_fp8_pack_scales(p->sbf, sn, p->swf, 0) != 0) { printf("scale pack refused\n"); exit(2); } 182 for (size_t i = 0; i < (size_t)p->xr * k; i++) p->x[i] = rand_act(); 183 if (p->xr > 3) memset(p->x + 3 * (size_t)k, 0, k * 4); 184 for (uint32_t r = 0; r < XR; r++) pq_ref(p->x + (size_t)r * k, k, p->xq + (size_t)r * k, p->xs + (size_t)r * KB, 0); 185 uint32_t i = 0; 186 for (uint32_t e = 0; e < E && i < M; e++) { uint32_t c = e + 1 == E ? M - i : (uint32_t)((uint64_t)(rnd() % 2000) * 2 * M / (2000ull * E)); if (c > M - i) c = M - i; for (uint32_t q = 0; q < c; q++) p->eid[i + q] = e; p->counts[e] = c; i += c; } 187 for (uint32_t q = 0; q < M; q++) p->src[q] = direct ? q : rnd() % p->xr; 188 } 189 static void qprob_free(QProb *p) { free(p->w); free(p->xq); free(p->sbf); free(p->swf); free(p->xs); free(p->x); free(p->eid); free(p->src); free(p->counts); } 190 191 /* the kernel's arithmetic for output row i, physical column c */ 192 static float qemu_phys(const QProb *p, uint32_t i, uint32_t c) { 193 const uint32_t k = p->k, KB = k / 128, e = p->eid[i], s = p->src[i]; 194 /* physical column c -> weight row: F32 c; SWIGLU gate 8G + h % 8 (h < 8) or up I + 8G + h % 8, G = c / 16, h = c % 16 */ 195 const uint32_t wrow = p->layout == OMEGA_GPU_PF_PLAIN ? c : 8 * (c / 16) + c % 8 + (c % 16 >= 8 ? p->n / 2 : 0); 196 const uint8_t *xr = p->xq + (size_t)s * k, *wr = p->w + ((size_t)e * p->n + wrow) * k; 197 const float *swt = p->swf + ((size_t)e * (p->n / 128) + wrow / 128) * KB; 198 float acc = 0.0f; 199 for (uint32_t kb = 0; kb < KB; kb++) { 200 const float sc = p->xs[(size_t)s * KB + kb] * (swt[kb] * 0x1p-56f); 201 for (uint32_t h = 0; h < 2; h++) { 202 float T = 0.0f; 203 for (uint32_t v = 0; v < 2; v++) { 204 float av[32], bv[32]; 205 for (uint32_t t = 0; t < 4; t++) for (uint32_t j = 0; j < 4; j++) { 206 const uint32_t o = 128 * kb + 64 * h + 16 * t + 8 * v + j; 207 av[4 * t + j] = qm_e4m3(xr[o]); bv[4 * t + j] = qm_e4m3(wr[o]); 208 av[16 + 4 * t + j] = qm_e4m3(xr[o + 4]); bv[16 + 4 * t + j] = qm_e4m3(wr[o + 4]); 209 } 210 T = qm_dot32(&GB10Q, av, bv, T); 211 } 212 acc = fmaf(T, sc, acc); 213 } 214 } 215 return acc; 216 } 217 /* the kernel's SwiGLU epilogue (omega_blackwell_codegen_qg.c: FMNMX t-cap, MUFU.EX2, MUFU.RCP). Both MUFU results go 218 * through the chip's flush rule (tests/bw_mufu_ftz.h, omega #462). Not observable with the t-cap: t <= 125 keeps 219 * 1 / (2^t + 1) >= 2^-125 normal, and an EX2 result below 2^-126 (t < -126) rounds to the same d = 1.0 after the +1 as 220 * its flushed zero; qg_host checks this bit for bit. Removing the cap (issue #462) is a separate chip-gated change. */ 221 static uint16_t qswiglu_bits(float g, float u) { 222 float t = fminf(g * u2f(0xbfb8aa3bu), 125.0f), ex = ws_mufu_ex2(t), d = ex + 1.0f, r = ws_mufu_rcp(d), o = g * r; 223 o = o * u; 224 return bf16_rn(o); 225 } 226 static void qemu_row(const QProb *p, uint32_t i, float *yf, uint16_t *ya) { 227 if (p->layout == OMEGA_GPU_PF_PLAIN) { for (uint32_t c = 0; c < p->n; c++) yf[c] = qemu_phys(p, i, c); return; } 228 for (uint32_t a = 0; a < p->n / 2; a++) { uint32_t G = a / 8, m = a % 8; ya[a] = qswiglu_bits(qemu_phys(p, i, 16 * G + m), qemu_phys(p, i, 16 * G + 8 + m)); } 229 } 230 /* double oracle on the ORIGINAL weights and the dequantised activations: logical output col (PLAIN) or gate/up of index a */ 231 static double qoracle(const QProb *p, uint32_t i, uint32_t col, double *mag) { 232 const uint32_t k = p->k, KB = k / 128, e = p->eid[i], s = p->src[i]; 233 double acc = 0, m = 0; 234 for (uint32_t j = 0; j < k; j++) { 235 const double sw = u2f((uint32_t)p->sbf[((size_t)e * (p->n / 128) + col / 128) * KB + j / 128] << 16); 236 const double t = (double)qm_e4m3(p->w[((size_t)e * p->n + col) * k + j]) * sw * qm_e4m3(p->xq[(size_t)s * k + j]) * p->xs[(size_t)s * KB + j / 128]; 237 acc += t; m += fabs(t); 238 } 239 *mag = m; return acc; 240 } 241 242 typedef struct { uint64_t w_va, s_va; uint32_t row0, cnt; uint64_t pad; } QTile; 243 static uint32_t qmake_tiles(const QProb *p, uint32_t bm, QTile *t) { 244 uint32_t nt = 0, row = 0; const uint32_t KB = p->k / 128; 245 for (uint32_t e = 0; e < p->E; e++) { for (uint32_t r = 0; r < p->counts[e]; r += bm) { 246 t[nt].w_va = (uintptr_t)(p->w + (size_t)e * p->n * p->k); t[nt].s_va = (uintptr_t)(p->swf + (size_t)e * (p->n / 128) * KB); 247 t[nt].row0 = row + r; t[nt].cnt = p->counts[e] - r < bm ? p->counts[e] - r : bm; t[nt].pad = 0; nt++; } row += p->counts[e]; } 248 return nt; 249 } 250 static size_t qcompare(const QProb *p, const void *y, const uint32_t *rows, uint32_t nrows, int tol_ulp, size_t *ulp1) { 251 size_t bad = 0; if (ulp1) *ulp1 = 0; 252 float *yf = malloc(p->n * 4); uint16_t *ya = malloc(p->n); 253 for (uint32_t q = 0; q < nrows; q++) { 254 const uint32_t i = rows[q]; qemu_row(p, i, yf, ya); 255 if (p->layout == OMEGA_GPU_PF_PLAIN) { const uint32_t *g = (const uint32_t *)y + (size_t)i * p->n; for (uint32_t c = 0; c < p->n; c++) if (g[c] != f2u(yf[c])) { if (getenv("PFDBG") && bad < 6) printf(" row %u col %u got %.9g want %.9g\n", i, c, u2f(g[c]), yf[c]); bad++; } } 256 else { const uint16_t *g = (const uint16_t *)y + (size_t)i * (p->n / 2); 257 for (uint32_t c = 0; c < p->n / 2; c++) if (g[c] != ya[c]) { int d = abs((int)g[c] - (int)ya[c]); if (d <= tol_ulp && ((g[c] ^ ya[c]) & 0x8000) == 0) { if (ulp1) (*ulp1)++; } else { if (getenv("PFDBG") && bad < 12) { const uint32_t G = c / 8, mm = c % 8; printf(" row %u act %u got %04x want %04x (g %.9g u %.9g)\n", i, c, g[c], ya[c], qemu_phys(p, i, 16 * G + mm), qemu_phys(p, i, 16 * G + 8 + mm)); } bad++; } } } 258 } 259 free(yf); free(ya); 260 return bad; 261 } 262 263 /* omega #462: with the t-cap the MUFU flush never changes a SwiGLU output bit. Compare qswiglu_bits against the IEEE 264 * form over every g in [80, 110] and [-110, -80] (the EX2-subnormal window t in (-150, -126] and the cap t = 125, both 265 * signs) plus a stride-65537 sample of all f32. Not a red test: there is no input for which the two differ. */ 266 static int qg_mufu_ftz_unobservable(void) { 267 uint64_t n = 0, diff = 0; 268 const float us[2] = { 1.0f, -3.0f }; 269 for (int pass = 0; pass < 3; pass++) { 270 uint64_t lo = pass == 0 ? f2u(80.0f) : pass == 1 ? f2u(-80.0f) : 0, hi = pass == 0 ? f2u(110.0f) : pass == 1 ? f2u(-110.0f) : 0xffffffffull, step = pass == 2 ? 65537 : 1; 271 for (uint64_t b = lo; b <= hi; b += step) { 272 float g = u2f((uint32_t)b); 273 if (isnan(g)) continue; 274 float t = fminf(g * u2f(0xbfb8aa3bu), 125.0f), r = 1.0f / (exp2f(t) + 1.0f); 275 for (int k = 0; k < 2; k++) { n++; if (bf16_rn((g * r) * us[k]) != qswiglu_bits(g, us[k])) diff++; } 276 } 277 } 278 printf("%s qgemm SwiGLU: MUFU flush vs IEEE, %llu inputs, %llu differ (t-cap keeps it unobservable, #462)\n", diff ? "FAIL" : "PASS", (unsigned long long)n, (unsigned long long)diff); 279 return diff ? 1 : 0; 280 } 281 static int qg128_host(void) { 282 const uint32_t dims[][3] = { 283 {16384,5120,OMEGA_QG_EPI_F32}, {14336,5120,OMEGA_QG_EPI_F32}, 284 {5120,6144,OMEGA_QG_EPI_F32}, {34816,5120,OMEGA_QG_EPI_SWIGLU}, 285 {5120,17408,OMEGA_QG_EPI_F32} 286 }; 287 int fail=0; 288 for (size_t c=0;cmaxstore)maxstore=p.insns[j].imm; 296 stores++; 297 } 298 int ok=stores && maxstore<=8388607u && p.regalloc.peak_gpr_usage<=BW_PHYS_GPR_HW_LIMIT 299 && declared>=p.regalloc.peak_gpr_usage+2u && (uint64_t)declared*256u<=65536u; 300 printf("%s 128-row shape %zu peak %u declared %u max-store %u\n",ok?"PASS":"FAIL",c,p.regalloc.peak_gpr_usage,declared,maxstore); 301 if(!ok)fail++; 302 } 303 OmegaQgSpec bad={128,8,8,OMEGA_QG_EPI_F32,1,17536,45,0,0}; 304 if(omega_qg_spec_ok(&bad)){printf("FAIL out-of-range store admitted\n");fail++;} 305 return fail; 306 } 307 308 static int qg_host(void) { 309 int fail = qg_mufu_ftz_unobservable() + qg128_host(); 310 for (uint32_t layout = 0; layout < 3; layout += 2) { 311 QProb p; qprob_make(&p, 3, 256, 384, 40, 17, layout, 0, 0); 312 double worst = 0; 313 for (uint32_t i = 0; i < p.M; i += 3) { 314 if (layout == OMEGA_GPU_PF_PLAIN) for (uint32_t c = 0; c < p.n; c++) { double m, o = qoracle(&p, i, c, &m); double d = fabs(qemu_phys(&p, i, c) - o) / (m + 1e-30); if (d > worst) worst = d; } 315 else for (uint32_t a = 0; a < p.n / 2; a++) { uint32_t G = a / 8, mm = a % 8; double m1, m2; 316 double og = qoracle(&p, i, a, &m1), ou = qoracle(&p, i, p.n / 2 + a, &m2); 317 double dg = fabs(qemu_phys(&p, i, 16 * G + mm) - og) / (m1 + 1e-30), du = fabs(qemu_phys(&p, i, 16 * G + 8 + mm) - ou) / (m2 + 1e-30); 318 if (dg > worst) worst = dg; 319 if (du > worst) worst = du; } 320 } 321 const int ok = worst < 1e-6; 322 printf("qgemm emulation vs double oracle (%s): worst error %.3g of the sum of |terms| %s\n", layout ? "swiglu_nat" : "plain", worst, ok ? "PASS" : "FAIL"); 323 if (!ok) fail++; 324 qprob_free(&p); 325 } 326 return fail; 327 } 328 329 static int qg_sim_run(const QProb *p, const OmegaQgSpec *sp, int order, void *y, char *msg, size_t msgn) { 330 static BlackwellIRProgram prog; 331 if (omega_blackwell_codegen_qgemm_prog(sp, &prog) != 0) { snprintf(msg, msgn, "codegen refused (spec_ok %d, insns %zu)", omega_qg_spec_ok(sp), prog.count); return -1; } 332 const uint32_t bm = 16u * sp->rt, span = omega_qg_span(sp), KB = p->k / 128; 333 QTile *tiles = calloc(p->M + 1, sizeof *tiles); 334 const uint32_t nt = qmake_tiles(p, bm, tiles); 335 uint32_t *ridx = malloc((p->M + bm) * 4); for (uint32_t i = 0; i < p->M + bm; i++) ridx[i] = i < p->M ? p->src[i] : 0; 336 const size_t ob = p->layout == OMEGA_GPU_PF_PLAIN ? 4 : 1, yb = (size_t)(p->M + bm) * p->n * ob; 337 memset(y, 0xee, yb); 338 uint8_t *smem = malloc(omega_qg_shared_bytes(sp)); 339 static WsSim s; memset(&s, 0, sizeof s); 340 s.p = &prog; s.threads = 32u * sp->warps; s.smem = smem; s.smem_bytes = omega_qg_shared_bytes(sp); s.order = order; 341 const uint64_t a[5] = { (uintptr_t)tiles, sp->direct ? 0 : (uintptr_t)ridx, (uintptr_t)p->xq, (uintptr_t)y, (uintptr_t)p->xs }; 342 for (int i = 0; i < 5; i++) { s.cbank[(0x380 + 8 * i) / 4] = (uint32_t)a[i]; s.cbank[(0x380 + 8 * i) / 4 + 1] = (uint32_t)(a[i] >> 32); } 343 s.cbank[0x3a8 / 4] = p->n; s.cbank[0x3ac / 4] = nt; 344 const size_t XR = p->xr + p->pad; 345 s.ranges[0].lo = (uintptr_t)tiles; s.ranges[0].hi = (uintptr_t)(tiles + nt); 346 s.ranges[1].lo = (uintptr_t)ridx; s.ranges[1].hi = (uintptr_t)(ridx + p->M + bm); 347 s.ranges[2].lo = (uintptr_t)p->xq; s.ranges[2].hi = (uintptr_t)(p->xq + XR * p->k); 348 s.ranges[3].lo = (uintptr_t)y; s.ranges[3].hi = (uintptr_t)y + (size_t)p->M * p->n * ob; 349 s.ranges[4].lo = (uintptr_t)p->w; s.ranges[4].hi = (uintptr_t)(p->w + (size_t)p->E * p->n * p->k); 350 s.ranges[5].lo = (uintptr_t)p->swf; s.ranges[5].hi = (uintptr_t)(p->swf + (size_t)p->E * (p->n / 128) * KB); 351 s.ranges[6].lo = (uintptr_t)p->xs; s.ranges[6].hi = (uintptr_t)(p->xs + XR * KB); 352 s.nranges = 7; 353 const uint32_t group_size = 1u << sp->group; 354 const int err = ws_run(&s, (p->n / span) * group_size, (nt + group_size - 1u) / group_size); 355 if (err) snprintf(msg, msgn, "simulator %.150s", s.msg); 356 free(tiles); free(ridx); free(smem); 357 return err ? -1 : 0; 358 } 359 static int qg_sim(void) { 360 int fail = 0; char msg[200]; 361 static const struct { uint32_t E, n, k, M, xr, layout, direct, rt, warps; } cs[] = { 362 { 3, 256, 384, 300, 120, OMEGA_GPU_PF_SWIGLU_NAT, 0, 4, 8 }, 363 { 3, 256, 256, 200, 0, OMEGA_GPU_PF_PLAIN, 1, 4, 8 }, 364 { 2, 256, 128, 150, 64, OMEGA_GPU_PF_PLAIN, 0, 4, 8 }, 365 { 2, 512, 1024, 140, 0, OMEGA_GPU_PF_SWIGLU_NAT, 1, 4, 8 }, 366 /* Extended-register 128-row shape: direct/indexed and tail stores. */ 367 { 3, 256, 384, 259, 300, OMEGA_GPU_PF_SWIGLU_NAT, 0, 8, 8 }, 368 { 3, 256, 256, 257, 0, OMEGA_GPU_PF_PLAIN, 1, 8, 8 }, 369 { 2, 256, 128, 127, 150, OMEGA_GPU_PF_PLAIN, 0, 8, 8 }, 370 { 1, 512, 1024, 129, 0, OMEGA_GPU_PF_SWIGLU_NAT, 1, 8, 8 }, 371 /* 32-row shape: paired scale rows, tails, direct and indexed input. */ 372 { 3, 256, 384, 97, 120, OMEGA_GPU_PF_SWIGLU_NAT, 0, 2, 4 }, 373 { 3, 256, 256, 65, 0, OMEGA_GPU_PF_PLAIN, 1, 2, 4 }, 374 { 2, 256, 128, 31, 64, OMEGA_GPU_PF_PLAIN, 0, 2, 4 }, 375 { 1, 512, 1024, 30, 0, OMEGA_GPU_PF_SWIGLU_NAT, 1, 2, 4 }, 376 }; 377 for (size_t c = 0; c < sizeof cs / sizeof cs[0]; c++) { 378 const uint32_t bm = 16 * cs[c].rt; 379 QProb p; qprob_make(&p, cs[c].E, cs[c].n, cs[c].k, cs[c].M, cs[c].xr, cs[c].layout, cs[c].direct, bm); 380 OmegaQgSpec sp = { cs[c].k, cs[c].rt, cs[c].warps, cs[c].layout == OMEGA_GPU_PF_PLAIN ? OMEGA_QG_EPI_F32 : OMEGA_QG_EPI_SWIGLU, cs[c].direct, cs[c].n, 45, 0, 0 }; 381 uint32_t *rows = malloc(p.M * 4); for (uint32_t i = 0; i < p.M; i++) rows[i] = i; 382 void *y = malloc((size_t)(p.M + bm) * p.n * 4); 383 const uint32_t groups[] = {0,1,3,4}; 384 for (size_t gi=0;gi 32) reps = 32; 451 for (int r = 0; r < reps && !rc; r++) rc = omega_gpu_pf_qgemm(n, k, layout, tab.va, nt, rva, xd.va, sd.va, yd.va, &ns[r]); 452 if (!rc) rc = omega_gpu_pf_download(&yd, 0, y1, yb & ~(size_t)15); 453 if (!rc) rc = omega_gpu_pf_upload(&yd, 0, y2, yb & ~(size_t)15); /* zero the output, run once more: identical result required */ 454 uint64_t ns2 = 0; 455 if (!rc) rc = omega_gpu_pf_qgemm(n, k, layout, tab.va, nt, rva, xd.va, sd.va, yd.va, &ns2); 456 if (!rc) rc = omega_gpu_pf_download(&yd, 0, y2, yb & ~(size_t)15); 457 if (rc) { printf("%s run: %s (%s)\n", name, omega_gpu_pf_rc_name(rc), omega_gpu_session_last_error()); fail++; } 458 else { 459 const size_t outb = (size_t)M * n * ob; 460 printf("%s second launch identical: %s\n", name, memcmp(y1, y2, outb) ? "NO" : "yes"); 461 if (memcmp(y1, y2, outb)) fail++; 462 uint32_t *rows = malloc(check_rows * 4); for (int q = 0; q < check_rows; q++) rows[q] = q == 0 ? 0 : q == 1 ? M - 1 : rnd() % M; 463 size_t ulp1 = 0; const size_t bad = qcompare(&p, y1, rows, check_rows, layout == OMEGA_GPU_PF_PLAIN ? 0 : 1, &ulp1); 464 printf("%s bit-identity on %d sampled rows: %zu differ", name, check_rows, bad); 465 if (layout != OMEGA_GPU_PF_PLAIN) printf(" (beyond 1 bf16 ulp; %zu within 1 ulp of %zu)", ulp1, (size_t)check_rows * n / 2); 466 printf("\n"); 467 if (bad) fail++; 468 qsort(ns, reps, 8, cmp_u64); 469 const double fl = 2.0 * M * n * k, med = (double)ns[reps / 2]; 470 printf("QGEMM%s %s tokens %u M %u n %u k %u tiles %u: median %.3f ms (min %.3f), %.1f TFLOPS, weights %.0f MB -> %.0f GB/s of distinct weights\n", g_cached ? "-cached" : "-inplace", name, tokens, M, n, k, nt, 471 med / 1e6, ns[0] / 1e6, fl / med / 1e3, (double)E * n * k / 1e6, (double)E * n * k / med); 472 if (mutants) for (int m = 1; m <= 4; m++) { 473 omega_gpu_pf_qgemm_test_set_mutant(m); 474 int mrc = omega_gpu_pf_qgemm(n, k, layout, tab.va, nt, rva, xd.va, sd.va, yd.va, NULL); 475 omega_gpu_pf_qgemm_test_set_mutant(0); 476 if (!mrc) mrc = omega_gpu_pf_download(&yd, 0, y2, yb & ~(size_t)15); 477 const size_t mb = mrc ? 0 : qcompare(&p, y2, rows, check_rows > 8 ? 8 : check_rows, layout == OMEGA_GPU_PF_PLAIN ? 0 : 1, NULL); 478 printf("%s mutant %d: %s\n", name, m, mrc ? "launch failed" : mb ? "caught" : "NOT CAUGHT"); 479 if (mrc || !mb) fail++; 480 } 481 free(rows); 482 } 483 free(y1); free(y2); 484 omega_gpu_fp8_tensor_free(wt); omega_gpu_pf_free(&wc); omega_gpu_pf_free(&sc); omega_gpu_pf_free(&xd); omega_gpu_pf_free(&sd); omega_gpu_pf_free(&yd); omega_gpu_pf_free(&tab); omega_gpu_pf_free(&ridx); 485 qprob_free(&p); 486 return fail; 487 } 488 489 int main(int argc, char **argv) { 490 int host = 1, sim = 1, chip = 0, reps = 7, rows = 64; uint32_t tokens = 3600, rt = 0, warps = 0, wait = 0, waitset = 0, group = 0; const char *only = NULL; 491 for (int i = 1; i < argc; i++) { 492 if (!strcmp(argv[i], "--host-only")) sim = 0; 493 else if (!strcmp(argv[i], "--sim")) host = 0; 494 else if (!strcmp(argv[i], "--chip")) { chip = 1; host = 0; sim = 0; } 495 else if (!strcmp(argv[i], "--tokens") && i + 1 < argc) tokens = (uint32_t)atoi(argv[++i]); 496 else if (!strcmp(argv[i], "--reps") && i + 1 < argc) reps = atoi(argv[++i]); 497 else if (!strcmp(argv[i], "--rows") && i + 1 < argc) rows = atoi(argv[++i]); 498 else if (!strcmp(argv[i], "--shape") && i + 1 < argc) only = argv[++i]; 499 else if (!strcmp(argv[i], "--cached")) g_cached = 1; 500 else if (!strcmp(argv[i], "--rt") && i + 1 < argc) rt = (uint32_t)atoi(argv[++i]); 501 else if (!strcmp(argv[i], "--warps") && i + 1 < argc) warps = (uint32_t)atoi(argv[++i]); 502 else if (!strcmp(argv[i], "--wait") && i + 1 < argc) { wait = (uint32_t)atoi(argv[++i]); waitset = 1; } 503 else if (!strcmp(argv[i], "--group") && i + 1 < argc) group = (uint32_t)atoi(argv[++i]); 504 else { fprintf(stderr, "GPU_QGEMM: INVALID argument %s\n", argv[i]); return 2; } 505 } 506 static const char *shapes[] = { "gateup", "down", "qkv", "o", "q38gu", "q38down", "q38mix", "q38o" }; 507 const int q38 = only && !strcmp(only, "q38"); /* --shape q38: the quantiser plus the four Qwen3.8 shapes */ 508 int selected = q38; 509 for (int s = 0; s < 8; s++) if (!only || !strcmp(only, shapes[s])) selected++; 510 if (!selected) { fprintf(stderr, "GPU_QGEMM: INVALID shape '%s'\n", only); return 2; } 511 if (!host && !sim && !chip) { fprintf(stderr, "GPU_QGEMM: NOT_RUN (no mode selected)\n"); return 2; } 512 int fail = 0, cases = 0; 513 if (host) { fail += pq_host(); fail += qg_host(); cases++; } 514 if (sim) { fail += pq_sim(); fail += qg_sim(); cases++; } 515 if (chip) { 516 omega_gpu_pf_qgemm_set_group(group); 517 printf("qgemm selected group %u\n",group); 518 if (rt || warps || waitset) omega_gpu_pf_qgemm_set_shape(rt ? rt : 4, warps ? warps : 8, waitset ? wait : 45); 519 if (!only || q38) { fail += pq_chip(tokens, reps); cases++; } 520 for (int s = 0; s < 8; s++) { 521 const int run = q38 ? s >= 4 : only ? !strcmp(only, shapes[s]) : s < 4; 522 if (run) { fail += qg_chip_shape(shapes[s], tokens, reps, rows, q38 ? s < 6 : !only || s < 2); cases++; } 523 } 524 } 525 if (!cases) { fprintf(stderr, "GPU_QGEMM: NOT_RUN (no cases executed)\n"); return 2; } 526 printf("GPU_QGEMM: %s\n", fail ? "FAIL" : "PASS"); 527 return fail ? 1 : 0; 528 }