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_cmdlist_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_cmdlist_test.c` 619 lines · 49633 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* 3 * gpu_cmdlist_test: OWN-30B W-L2 decode launch collapse (omega_gpu_cmdlist.h + omega_blackwell_codegen_dec.h). 4 * --host-only codegen budgets and refusals of the four glue kernels (no device) 5 * --sim warp-simulator parity of AXPY chain, RES (ADD / COMBINE / NORM, scale 1 and 2^64), SWIGLU, ARGMAX 6 * (two stages, tie case) against the host reference; mutants caught 7 * (default) chip, caller holds the GB10 lock: 8 * baseline: L dependent AXPY launches one omega_gpu_session_launch at a time 9 * main: the same L launches recorded once and submitted once (PCAS) and in INLINE mode; x bit-identical 10 * to the baseline, every launch semaphore DONE; negative control skip_launch (must fail, first_missing 11 * named, x short by one a); BARRIER_NONE recorded (informational); per-launch medians (--reps N); 12 * glue kernels in one list: RES ADD|NORM, SWIGLU, RES COMBINE|NORM, ARGMAX x2 against the host 13 * reference; mutants caught on chip. --out receipt.json 14 * Verdict: exit 0 when every CHECK passed. 15 */ 16 #include "omega_gpu_cmdlist.h" 17 #include "omega_blackwell_codegen_dec.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 26 #include "test_out_file.h" 27 28 static 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 static uint32_t lcg(uint32_t *s) { *s = *s * 1664525u + 1013904223u; return *s; } 31 static float frand(uint32_t *s, float lo, float hi) { return lo + (hi - lo) * ((float)(lcg(s) >> 8) / `16777216`.0f); } 32 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; } 33 static uint64_t median_u64(uint64_t *v, unsigned n) { qsort(v, n, sizeof *v, cmp_u64); return v[n / 2]; } 34 35 #define H OMEGA_DEC_HIDDEN 36 #define TK OMEGA_DEC_TOPK 37 #define INTER OMEGA_DEC_INTER 38 #define VOCAB 151936u 39 #define AM_CTAS ((VOCAB + OMEGA_DEC_ARGMAX_SPAN - 1) / OMEGA_DEC_ARGMAX_SPAN) /* 75 */ 40 #define AM_PAD (AM_CTAS * OMEGA_DEC_ARGMAX_SPAN) /* 153600 */ 41 #define EPS 1e-6f 42 #define SCALE_2P64 `18446744073709551616`.0f 43 44 /* ---- host references (float, the kernel's operation order where it matters; -ffp-contract=off) ---- */ 45 static void ref_res(uint32_t mode, float *x, const float *a, const float *w, const float *nw, float *y, float eps, float scale) { 46 if (mode & OMEGA_DEC_RES_ADD) for (unsigned i = 0; i < H; i++) x[i] = x[i] + a[i]; 47 if (mode & OMEGA_DEC_RES_COMBINE) for (unsigned i = 0; i < H; i++) { 48 float m = w[0] * a[i]; 49 for (unsigned g = 1; g < TK; g++) { float t = w[g] * a[g * H + i]; m = m + t; } 50 x[i] = x[i] + m; 51 } 52 if (mode & OMEGA_DEC_RES_NORM) { 53 double sq = 0; for (unsigned i = 0; i < H; i++) sq += (double)x[i] * x[i]; 54 float rs = (float)(1.0 / sqrt(sq / H + (double)eps)); 55 for (unsigned i = 0; i < H; i++) { float v = x[i] * rs; v = v * nw[i]; y[i] = v * scale; } 56 } 57 } 58 static void ref_swiglu(const float *gu, float *act, float scale) { 59 for (unsigned g = 0; g < TK; g++) for (unsigned c = 0; c < INTER; c++) { 60 float v = gu[g * 2 * INTER + c], u = gu[g * 2 * INTER + INTER + c]; 61 float s = v / (1.0f + expf(-v)); 62 act[g * INTER + c] = s * u * scale; 63 } 64 } 65 static uint32_t ref_argmax(const float *v, uint32_t n) { uint32_t b = 0; for (uint32_t i = 1; i < n; i++) if (v[i] > v[b]) b = i; return b; } 66 static int close_rel(float a, float b, float rel, float abs_) { float d = fabsf(a - b); return d <= abs_ || d <= rel * fabsf(b); } 67 68 /* ---- one glue-kernel case: buffers in plain host memory (sim) or in device buffers (chip) ---- */ 69 typedef struct { 70 float *x, *a8, *w, *nw, *y, *gu, *act, *logits, *ov; uint32_t *oi; 71 float x_ref[H], y_ref[H], act_ref[TK * INTER]; uint32_t am_ref; 72 } Glue; 73 static void glue_fill(Glue *g, uint32_t seed, int tie) { 74 uint32_t s = seed; 75 for (unsigned i = 0; i < H; i++) { g->x[i] = frand(&s, -2, 2); g->nw[i] = frand(&s, 0.5f, 1.5f); } 76 for (unsigned i = 0; i < TK * H; i++) g->a8[i] = frand(&s, -1, 1); 77 for (unsigned i = 0; i < TK; i++) g->w[i] = frand(&s, 0, 0.3f); 78 for (unsigned i = 0; i < TK * 2 * INTER; i++) g->gu[i] = frand(&s, -4, 4); 79 for (unsigned i = 0; i < AM_PAD; i++) g->logits[i] = i < VOCAB ? frand(&s, -50, 50) : -1e30f; 80 if (tie) { g->logits[1007] = 60.0f; g->logits[90007] = 60.0f; } /* lowest index wins: 1007 (a j == 7 slot) */ 81 for (unsigned i = 0; i < OMEGA_DEC_ARGMAX_SPAN; i++) { g->ov[i] = -1e30f; g->oi[i] = 0; } 82 g->am_ref = ref_argmax(g->logits, VOCAB); 83 } 84 85 /* ---- kernels ---- */ 86 typedef struct { BlackwellIRProgram prog; OmegaGpuMem code; uint32_t gpr; int built; } Kern; 87 static Kern K_axpy, K_res_an, K_res_cn, K_sw, K_am0, K_am1, K_res_m1, K_res_m2, K_res_cn_m1, K_sw_m1, K_am_m1, K_axpy_big; 88 89 static int kern_prog(Kern *k, const OmegaDecSpec *sp) { k->built = 0; return omega_blackwell_codegen_dec_prog(sp, &k->prog); } 90 static int kern_upload(Kern *k) { 91 size_t need = omega_bw_program_code_bytes(&k->prog); 92 if (need == 0 || omega_gpu_session_alloc_code(need, &k->code) != 0) return -1; 93 OmegaBlackwellKernel kk; 94 if (omega_blackwell_kernel_encode_into(&k->prog, k->code.cpu, need, &kk) != 0 || omega_gpu_session_sync_to_gpu(&k->code) != 0) return -1; 95 k->gpr = omega_bw_gpr_declared(kk.gpr_count); 96 if (k->gpr < 64u) k->gpr = 64u; 97 k->built = 1; 98 return 0; 99 } 100 static void put64(uint32_t *a, unsigned off, uint64_t v) { a[(off - 0x380) / 4] = (uint32_t)v; a[(off - 0x380) / 4 + 1] = (uint32_t)(v >> 32); } 101 static void putf(uint32_t *a, unsigned off, float f) { memcpy(&a[(off - 0x380) / 4], &f, 4); } 102 103 #define AXPY_STRIDE 2048u /* small launch: 8 CTAs x 256 threads, one element each */ 104 #define AXPY_BIG_STRIDE 65536u /* heavy launch for the no-barrier control: 256 CTAs x 16 iterations */ 105 #define AXPY_BIG_ITERS 16u 106 107 static int specs_build(void) { 108 OmegaDecSpec s; 109 memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_AXPY; s.stride = AXPY_STRIDE; if (kern_prog(&K_axpy, &s)) return -1; 110 memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_AXPY; s.stride = AXPY_BIG_STRIDE; if (kern_prog(&K_axpy_big, &s)) return -1; 111 memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_RES; s.mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM; if (kern_prog(&K_res_an, &s)) return -1; 112 memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_RES; s.mode = OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM; if (kern_prog(&K_res_cn, &s)) return -1; 113 memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_RES; s.mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM; s.mutant = 1; if (kern_prog(&K_res_m1, &s)) return -1; 114 memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_RES; s.mode = OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM; s.mutant = 2; if (kern_prog(&K_res_m2, &s)) return -1; 115 memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_RES; s.mode = OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM; s.mutant = 1; if (kern_prog(&K_res_cn_m1, &s)) return -1; 116 memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_SWIGLU; if (kern_prog(&K_sw, &s)) return -1; 117 memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_SWIGLU; s.mutant = 1; if (kern_prog(&K_sw_m1, &s)) return -1; 118 memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_ARGMAX; s.mode = 0; if (kern_prog(&K_am0, &s)) return -1; 119 memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_ARGMAX; s.mode = 1; if (kern_prog(&K_am1, &s)) return -1; 120 memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_ARGMAX; s.mode = 0; s.mutant = 1; if (kern_prog(&K_am_m1, &s)) return -1; 121 return 0; 122 } 123 124 /* ---- host-only ---- */ 125 static int host_only(void) { 126 const struct { const char *name; OmegaDecSpec s; } cases[] = { 127 { "axpy", { .kind = OMEGA_DEC_AXPY, .stride = AXPY_STRIDE } }, 128 { "res add|norm", { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM } }, 129 { "res combine|norm", { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM } }, 130 { "res add", { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD } }, 131 { "swiglu", { .kind = OMEGA_DEC_SWIGLU } }, 132 { "argmax", { .kind = OMEGA_DEC_ARGMAX } }, 133 { "argmax indexed", { .kind = OMEGA_DEC_ARGMAX, .mode = 1 } }, 134 }; 135 for (unsigned i = 0; i < sizeof cases / sizeof cases[0]; i++) { 136 uint32_t insns = 0, gprs = 0; 137 int rc = omega_dec_prog_stats(&cases[i].s, &insns, &gprs); 138 CHECK(rc == 0, "codegen %s", cases[i].name); 139 CHECK(gprs <= 200, "%s gprs %u within budget", cases[i].name, gprs); 140 printf("codegen %s: %u insns, peak %u gprs, declared %u\n", cases[i].name, insns, gprs, omega_bw_gpr_declared(gprs)); 141 } 142 OmegaDecSpec bad[] = { { .kind = OMEGA_DEC_AXPY }, { .kind = OMEGA_DEC_AXPY, .stride = 100 }, { .kind = OMEGA_DEC_RES }, 143 { .kind = OMEGA_DEC_RES, .mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_COMBINE }, { .kind = (OmegaDecKind)0 }, { .kind = OMEGA_DEC_RES, .mode = 8 } }; 144 for (unsigned i = 0; i < sizeof bad / sizeof bad[0]; i++) CHECK(omega_dec_prog_stats(&bad[i], NULL, NULL) != 0, "refusal %u", i); 145 /* widths (sc #642): 5120 and 8192 build; not a multiple of 1024, above 8192, COMBINE off 2048, SWIGLU off 256 refuse */ 146 OmegaDecSpec w; memset(&w, 0, sizeof w); w.kind = OMEGA_DEC_RES; w.mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM; 147 w.width = 5120; CHECK(omega_dec_prog_stats(&w, NULL, NULL) == 0, "codegen res width 5120"); 148 w.width = 8192; CHECK(omega_dec_prog_stats(&w, NULL, NULL) == 0, "codegen res width 8192"); 149 w.width = 3000; CHECK(omega_dec_prog_stats(&w, NULL, NULL) != 0, "refusal res width 3000"); 150 w.width = 9216; CHECK(omega_dec_prog_stats(&w, NULL, NULL) != 0, "refusal res width 9216"); 151 w.width = 5120; w.mode = OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM; CHECK(omega_dec_prog_stats(&w, NULL, NULL) != 0, "refusal combine width 5120"); 152 memset(&w, 0, sizeof w); w.kind = OMEGA_DEC_SWIGLU; w.width = 17408; CHECK(omega_dec_prog_stats(&w, NULL, NULL) == 0, "codegen swiglu width 17408"); 153 w.width = 17409; CHECK(omega_dec_prog_stats(&w, NULL, NULL) != 0, "refusal swiglu width 17409"); 154 return g_failed ? 1 : 0; 155 } 156 157 /* ---- simulator ---- */ 158 static int sim_one(Kern *k, const uint32_t *args, unsigned n_args, uint32_t gx, uint32_t gy, WsRange *ranges, int nr, const char *what) { 159 static uint8_t smem[OMEGA_DEC_SHARED_BYTES]; 160 WsSim s; memset(&s, 0, sizeof s); 161 s.p = &k->prog; s.threads = OMEGA_DEC_THREADS; s.smem = smem; s.smem_bytes = sizeof smem; 162 memcpy(&s.cbank[0x380 / 4], args, n_args * 4u); 163 s.nranges = nr; for (int i = 0; i < nr; i++) s.ranges[i] = ranges[i]; 164 int err = ws_run(&s, gx, gy); 165 CHECK(err == 0, "sim %s: %s (pc %d)", what, s.msg, s.err_pc); 166 return err; 167 } 168 static int sim_mode(void) { 169 if (specs_build()) { printf("FAIL: codegen\n"); return 1; } 170 /* AXPY chain: 3 launches x += a over 2048 */ 171 { 172 static float x[AXPY_STRIDE], a[AXPY_STRIDE], xr[AXPY_STRIDE]; 173 uint32_t s = 7; for (unsigned i = 0; i < AXPY_STRIDE; i++) { x[i] = frand(&s, -1, 1); a[i] = frand(&s, -1, 1); xr[i] = x[i]; } 174 uint32_t args[OMEGA_DEC_AXPY_WORDS] = { 0 }; put64(args, OMEGA_DEC_AXPY_X, (uintptr_t)x); put64(args, OMEGA_DEC_AXPY_A, (uintptr_t)a); args[(OMEGA_DEC_AXPY_ITERS - 0x380) / 4] = 1; 175 WsRange r[2] = { { (uintptr_t)x, (uintptr_t)x + sizeof x }, { (uintptr_t)a, (uintptr_t)a + sizeof a } }; 176 for (int l = 0; l < 3; l++) { if (sim_one(&K_axpy, args, OMEGA_DEC_AXPY_WORDS, AXPY_STRIDE / OMEGA_DEC_THREADS, 1, r, 2, "axpy")) return 1; for (unsigned i = 0; i < AXPY_STRIDE; i++) xr[i] = xr[i] + a[i]; } 177 CHECK(memcmp(x, xr, sizeof x) == 0, "sim axpy chain bit-identical"); 178 } 179 /* glue kernels */ 180 static float x[H], a8[TK * H], w[TK], nw[H], y[H], gu[TK * 2 * INTER], act[TK * INTER], logits[AM_PAD], ov[OMEGA_DEC_ARGMAX_SPAN]; 181 static uint32_t oi[OMEGA_DEC_ARGMAX_SPAN]; 182 Glue g = { x, a8, w, nw, y, gu, act, logits, ov, oi, { 0 }, { 0 }, { 0 }, 0 }; 183 WsRange rr[6] = { { (uintptr_t)x, (uintptr_t)x + sizeof x }, { (uintptr_t)a8, (uintptr_t)a8 + sizeof a8 }, { (uintptr_t)w, (uintptr_t)w + sizeof w }, 184 { (uintptr_t)nw, (uintptr_t)nw + sizeof nw }, { (uintptr_t)y, (uintptr_t)y + sizeof y }, { (uintptr_t)gu, (uintptr_t)gu + sizeof gu } }; 185 const float scales[2] = { 1.0f, SCALE_2P64 }; 186 for (int sc = 0; sc < 2; sc++) for (int mode = 0; mode < 2; mode++) for (int mut = 0; mut < 2; mut++) { 187 glue_fill(&g, 100 + sc, 0); 188 memcpy(g.x_ref, x, sizeof x); 189 const uint32_t M = mode == 0 ? (OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM) : (OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM); 190 ref_res(M, g.x_ref, a8, w, nw, g.y_ref, EPS, scales[sc]); 191 uint32_t args[OMEGA_DEC_RES_WORDS] = { 0 }; 192 put64(args, OMEGA_DEC_RES_X, (uintptr_t)x); put64(args, OMEGA_DEC_RES_A, (uintptr_t)a8); put64(args, OMEGA_DEC_RES_W, (uintptr_t)w); 193 put64(args, OMEGA_DEC_RES_NW, (uintptr_t)nw); put64(args, OMEGA_DEC_RES_Y, (uintptr_t)y); putf(args, OMEGA_DEC_RES_EPS, EPS); putf(args, OMEGA_DEC_RES_SCALE, scales[sc]); 194 Kern *k = mut ? (mode == 0 ? &K_res_m1 : &K_res_m2) : (mode == 0 ? &K_res_an : &K_res_cn); 195 if (sim_one(k, args, OMEGA_DEC_RES_WORDS, 1, 1, rr, 5, "res")) return 1; 196 int xbad = memcmp(x, g.x_ref, sizeof x) != 0, ybad = 0; 197 for (unsigned i = 0; i < H; i++) if (!close_rel(y[i], g.y_ref[i], 2e-5f, 1e-6f)) ybad++; 198 if (!mut) { CHECK(!xbad, "sim res mode %u scale %d: x bit-identical", M, sc); CHECK(ybad == 0, "sim res mode %u scale %d: y within 2e-5 (%d bad)", M, sc, ybad); } 199 else CHECK(xbad || ybad, "sim res mutant (mode %u) caught", M); 200 printf("sim res mode %u scale %s mutant %d: x %s, y %d/%u outside tolerance\n", M, sc ? "2^64" : "1", mut, xbad ? "DIFFERS" : "identical", ybad, H); 201 } 202 for (int mut = 0; mut < 2; mut++) { 203 glue_fill(&g, 200, 0); 204 ref_swiglu(gu, g.act_ref, 1.0f); 205 uint32_t args[OMEGA_DEC_SW_WORDS] = { 0 }; put64(args, OMEGA_DEC_SW_GU, (uintptr_t)gu); put64(args, OMEGA_DEC_SW_ACT, (uintptr_t)act); putf(args, OMEGA_DEC_SW_SCALE, 1.0f); 206 WsRange r2[2] = { rr[5], { (uintptr_t)act, (uintptr_t)act + sizeof act } }; 207 if (sim_one(mut ? &K_sw_m1 : &K_sw, args, OMEGA_DEC_SW_WORDS, INTER / OMEGA_DEC_THREADS, TK, r2, 2, "swiglu")) return 1; 208 int bad = 0; for (unsigned i = 0; i < TK * INTER; i++) if (!close_rel(act[i], g.act_ref[i], 5e-5f, 1e-6f)) bad++; 209 if (!mut) CHECK(bad == 0, "sim swiglu within 5e-5 (%d bad)", bad); else CHECK(bad > 0, "sim swiglu mutant caught"); 210 printf("sim swiglu mutant %d: %d/%u outside tolerance\n", mut, bad, TK * INTER); 211 } 212 for (int tie = 0; tie < 2; tie++) for (int mut = 0; mut < 2; mut++) { 213 glue_fill(&g, 300 + tie, tie); 214 uint32_t a0[OMEGA_DEC_AM_WORDS] = { 0 }, a1[OMEGA_DEC_AM_WORDS] = { 0 }; 215 static float ov2[1]; static uint32_t oi2[1]; 216 put64(a0, OMEGA_DEC_AM_V, (uintptr_t)logits); put64(a0, OMEGA_DEC_AM_I, 0); put64(a0, OMEGA_DEC_AM_OV, (uintptr_t)ov); put64(a0, OMEGA_DEC_AM_OI, (uintptr_t)oi); 217 put64(a1, OMEGA_DEC_AM_V, (uintptr_t)ov); put64(a1, OMEGA_DEC_AM_I, (uintptr_t)oi); put64(a1, OMEGA_DEC_AM_OV, (uintptr_t)ov2); put64(a1, OMEGA_DEC_AM_OI, (uintptr_t)oi2); 218 WsRange r3[4] = { { (uintptr_t)logits, (uintptr_t)logits + sizeof logits }, { (uintptr_t)ov, (uintptr_t)ov + sizeof ov }, { (uintptr_t)oi, (uintptr_t)oi + sizeof oi }, 219 { (uintptr_t)ov2, (uintptr_t)ov2 + 4 } }; 220 WsRange r4[5] = { r3[0], r3[1], r3[2], r3[3], { (uintptr_t)oi2, (uintptr_t)oi2 + 4 } }; 221 if (sim_one(mut ? &K_am_m1 : &K_am0, a0, OMEGA_DEC_AM_WORDS, AM_CTAS, 1, r4, 5, "argmax stage 1")) return 1; 222 if (sim_one(&K_am1, a1, OMEGA_DEC_AM_WORDS, 1, 1, r4, 5, "argmax stage 2")) return 1; 223 if (!mut) CHECK(oi2[0] == g.am_ref && ov2[0] == logits[g.am_ref], "sim argmax tie %d: %u (ref %u)", tie, oi2[0], g.am_ref); 224 else if (tie) CHECK(oi2[0] != g.am_ref, "sim argmax mutant caught (max at a j == 7 slot)"); 225 printf("sim argmax tie %d mutant %d: index %u value %g (ref %u)\n", tie, mut, oi2[0], ov2[0], g.am_ref); 226 } 227 return g_failed ? 1 : 0; 228 } 229 230 /* ---- Qwen3.8-27B widths (sc #642): RES over 5120, SWIGLU over one dense 17408 row ---- */ 231 #define WH 5120u 232 #define WI 17408u 233 static int sim_wide(void) { 234 static Kern kw[4]; 235 static float x[WH], xr[WH], a[WH], nw[WH], y[WH], yr[WH], gu[2 * WI], act[WI], actr[WI]; 236 const uint32_t modes[3] = { OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM, OMEGA_DEC_RES_NORM, OMEGA_DEC_RES_ADD }; 237 for (int mi = 0; mi < 3; mi++) for (int mut = 0; mut < 2; mut++) { 238 if (mut && !(modes[mi] & OMEGA_DEC_RES_NORM)) continue; 239 OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_RES; s.mode = modes[mi]; s.mutant = mut; s.width = WH; 240 if (kern_prog(&kw[0], &s)) { CHECK(0, "codegen res width %u mode %u", WH, modes[mi]); return 1; } 241 uint32_t sd = 300 + mi; 242 for (unsigned i = 0; i < WH; i++) { x[i] = frand(&sd, -2, 2); a[i] = frand(&sd, -1, 1); nw[i] = frand(&sd, 0.5f, 1.5f); y[i] = -7.0f; } 243 memcpy(xr, x, sizeof x); 244 /* ref_res at width WH */ 245 if (modes[mi] & OMEGA_DEC_RES_ADD) for (unsigned i = 0; i < WH; i++) xr[i] = xr[i] + a[i]; 246 if (modes[mi] & OMEGA_DEC_RES_NORM) { 247 double sq = 0; for (unsigned i = 0; i < WH; i++) sq += (double)xr[i] * xr[i]; 248 float rs = (float)(1.0 / sqrt(sq / WH + (double)EPS)); 249 for (unsigned i = 0; i < WH; i++) { float v = xr[i] * rs; v = v * nw[i]; yr[i] = v * SCALE_2P64; } 250 } 251 uint32_t args[OMEGA_DEC_RES_WORDS] = { 0 }; 252 put64(args, OMEGA_DEC_RES_X, (uintptr_t)x); put64(args, OMEGA_DEC_RES_A, (uintptr_t)a); put64(args, OMEGA_DEC_RES_NW, (uintptr_t)nw); 253 put64(args, OMEGA_DEC_RES_Y, (uintptr_t)y); putf(args, OMEGA_DEC_RES_EPS, EPS); putf(args, OMEGA_DEC_RES_SCALE, SCALE_2P64); 254 WsRange r[4] = { { (uintptr_t)x, (uintptr_t)x + sizeof x }, { (uintptr_t)a, (uintptr_t)a + sizeof a }, 255 { (uintptr_t)nw, (uintptr_t)nw + sizeof nw }, { (uintptr_t)y, (uintptr_t)y + sizeof y } }; 256 if (sim_one(&kw[0], args, OMEGA_DEC_RES_WORDS, 1, 1, r, 4, "res wide")) return 1; 257 int xbad = memcmp(x, xr, sizeof x) != 0, ybad = 0; 258 if (modes[mi] & OMEGA_DEC_RES_NORM) for (unsigned i = 0; i < WH; i++) if (!close_rel(y[i], yr[i], 2e-5f, 1e-6f)) ybad++; 259 if (!mut) { CHECK(!xbad, "sim res width %u mode %u: x bit-identical", WH, modes[mi]); CHECK(ybad == 0, "sim res width %u mode %u: y within 2e-5 (%d bad)", WH, modes[mi], ybad); } 260 else CHECK(ybad > 0, "sim res width %u mode %u mutant caught", WH, modes[mi]); 261 printf("sim res width %u mode %u mutant %d: x %s, y %d/%u outside tolerance\n", WH, modes[mi], mut, xbad ? "DIFFERS" : "identical", ybad, WH); 262 } 263 for (int mut = 0; mut < 2; mut++) { 264 OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_SWIGLU; s.mutant = mut; s.width = WI; 265 if (kern_prog(&kw[1], &s)) { CHECK(0, "codegen swiglu width %u", WI); return 1; } 266 uint32_t sd = 400; 267 for (unsigned i = 0; i < 2 * WI; i++) gu[i] = frand(&sd, -4, 4); 268 for (unsigned c = 0; c < WI; c++) { float v = gu[c], u = gu[WI + c]; actr[c] = v / (1.0f + expf(-v)) * u * SCALE_2P64; act[c] = -7.0f; } 269 uint32_t args[OMEGA_DEC_SW_WORDS] = { 0 }; put64(args, OMEGA_DEC_SW_GU, (uintptr_t)gu); put64(args, OMEGA_DEC_SW_ACT, (uintptr_t)act); putf(args, OMEGA_DEC_SW_SCALE, SCALE_2P64); 270 WsRange r[2] = { { (uintptr_t)gu, (uintptr_t)gu + sizeof gu }, { (uintptr_t)act, (uintptr_t)act + sizeof act } }; 271 if (sim_one(&kw[1], args, OMEGA_DEC_SW_WORDS, WI / OMEGA_DEC_THREADS, 1, r, 2, "swiglu wide")) return 1; 272 int bad = 0; for (unsigned i = 0; i < WI; i++) if (!close_rel(act[i], actr[i], 5e-5f, 1e-6f)) bad++; 273 if (!mut) CHECK(bad == 0, "sim swiglu width %u within 5e-5 (%d bad)", WI, bad); else CHECK(bad > 0, "sim swiglu width %u mutant caught", WI); 274 printf("sim swiglu width %u mutant %d: %d/%u outside tolerance\n", WI, mut, bad, WI); 275 } 276 /* RES rows form: 3 rows of [5120], ADD|NORM, row r at r * 5120 (nw shared); the mutant drops the weight */ 277 { 278 enum { ROWS = 3 }; 279 static float rx[ROWS * WH], rxr[ROWS * WH], ra[ROWS * WH], ry[ROWS * WH], ryr[ROWS * WH]; 280 for (int mut = 0; mut < 2; mut++) { 281 OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_RES; s.mode = OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM | OMEGA_DEC_RES_ROWS; s.mutant = mut; s.width = WH; 282 if (kern_prog(&kw[2], &s)) { CHECK(0, "codegen res rows"); return 1; } 283 uint32_t sd = 500; 284 for (unsigned i = 0; i < ROWS * WH; i++) { rx[i] = frand(&sd, -2, 2); ra[i] = frand(&sd, -1, 1); ry[i] = -7.0f; } 285 for (unsigned i = 0; i < WH; i++) nw[i] = frand(&sd, 0.5f, 1.5f); 286 for (unsigned r = 0; r < ROWS; r++) { 287 float *xx = rxr + r * WH; 288 for (unsigned i = 0; i < WH; i++) xx[i] = rx[r * WH + i] + ra[r * WH + i]; 289 double sq = 0; for (unsigned i = 0; i < WH; i++) sq += (double)xx[i] * xx[i]; 290 float rs = (float)(1.0 / sqrt(sq / WH + (double)EPS)); 291 for (unsigned i = 0; i < WH; i++) { float v = xx[i] * rs; v = v * nw[i]; ryr[r * WH + i] = v * SCALE_2P64; } 292 } 293 uint32_t args[OMEGA_DEC_RES_WORDS] = { 0 }; 294 put64(args, OMEGA_DEC_RES_X, (uintptr_t)rx); put64(args, OMEGA_DEC_RES_A, (uintptr_t)ra); put64(args, OMEGA_DEC_RES_NW, (uintptr_t)nw); 295 put64(args, OMEGA_DEC_RES_Y, (uintptr_t)ry); putf(args, OMEGA_DEC_RES_EPS, EPS); putf(args, OMEGA_DEC_RES_SCALE, SCALE_2P64); 296 WsRange r[4] = { { (uintptr_t)rx, (uintptr_t)rx + sizeof rx }, { (uintptr_t)ra, (uintptr_t)ra + sizeof ra }, 297 { (uintptr_t)nw, (uintptr_t)nw + sizeof nw }, { (uintptr_t)ry, (uintptr_t)ry + sizeof ry } }; 298 if (sim_one(&kw[2], args, OMEGA_DEC_RES_WORDS, 1, ROWS, r, 4, "res rows")) return 1; 299 int xbad = memcmp(rx, rxr, sizeof rx) != 0, ybad = 0; 300 for (unsigned i = 0; i < ROWS * WH; i++) if (!close_rel(ry[i], ryr[i], 2e-5f, 1e-6f)) ybad++; 301 if (!mut) { CHECK(!xbad, "sim res rows: x bit-identical"); CHECK(ybad == 0, "sim res rows: y within 2e-5 (%d bad)", ybad); } 302 else CHECK(ybad > 0, "sim res rows mutant caught"); 303 printf("sim res rows %d x %u mutant %d: x %s, y %d/%u outside tolerance\n", ROWS, WH, mut, xbad ? "DIFFERS" : "identical", ybad, ROWS * WH); 304 } 305 OmegaDecSpec bad; memset(&bad, 0, sizeof bad); bad.kind = OMEGA_DEC_RES; bad.mode = OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_ROWS; 306 CHECK(omega_dec_prog_stats(&bad, NULL, NULL) != 0, "refusal res rows with combine"); 307 bad.mode = OMEGA_DEC_RES_ROWS; CHECK(omega_dec_prog_stats(&bad, NULL, NULL) != 0, "refusal res rows alone"); 308 } 309 /* GATHER: 3 rows, input stride 14343, out width 14336 (the Qwen3.8 attention row), random source table */ 310 { 311 enum { ROWS = 3, GW = 14336, GS = 14343 }; 312 static float gin[ROWS * GS], gout[ROWS * GW]; 313 static uint32_t tab[GW]; 314 for (int mut = 0; mut < 2; mut++) { 315 OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_GATHER; s.mutant = mut; s.width = GW; s.stride = GS; 316 if (kern_prog(&kw[3], &s)) { CHECK(0, "codegen gather"); return 1; } 317 uint32_t sd = 600; 318 for (unsigned i = 0; i < ROWS * GS; i++) gin[i] = frand(&sd, -3, 3); 319 for (unsigned i = 0; i < GW; i++) { sd = sd * 1664525u + 1013904223u; tab[i] = sd % (GS - 1); } 320 for (unsigned i = 0; i < ROWS * GW; i++) gout[i] = -7.0f; 321 uint32_t args[OMEGA_DEC_GA_WORDS] = { 0 }; 322 put64(args, OMEGA_DEC_GA_IN, (uintptr_t)gin); put64(args, OMEGA_DEC_GA_TAB, (uintptr_t)tab); put64(args, OMEGA_DEC_GA_OUT, (uintptr_t)gout); 323 WsRange r[3] = { { (uintptr_t)gin, (uintptr_t)gin + sizeof gin }, { (uintptr_t)tab, (uintptr_t)tab + sizeof tab }, { (uintptr_t)gout, (uintptr_t)gout + sizeof gout } }; 324 if (sim_one(&kw[3], args, OMEGA_DEC_GA_WORDS, (GW + OMEGA_DEC_THREADS - 1) / OMEGA_DEC_THREADS, ROWS, r, 3, "gather")) return 1; 325 int bad = 0; 326 for (unsigned rr = 0; rr < ROWS; rr++) for (unsigned n = 0; n < GW; n++) if (memcmp(&gout[rr * GW + n], &gin[rr * GS + tab[n]], 4)) bad++; 327 if (!mut) CHECK(bad == 0, "sim gather bit-exact (%d bad)", bad); else CHECK(bad > 0, "sim gather mutant caught"); 328 printf("sim gather %d x %u mutant %d: %d/%u wrong\n", ROWS, GW, mut, bad, ROWS * GW); 329 } 330 OmegaDecSpec bad; memset(&bad, 0, sizeof bad); bad.kind = OMEGA_DEC_GATHER; bad.width = 0; bad.stride = 1; 331 CHECK(omega_dec_prog_stats(&bad, NULL, NULL) != 0, "refusal gather width 0"); 332 } 333 /* GATE in place over 6144 + 100 values (a tail CTA), mutant drops the gate */ 334 { 335 enum { GN = 6244 }; 336 static float a[GN], g[GN], ref[GN]; 337 for (int mut = 0; mut < 2; mut++) { 338 OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_GATE; s.mutant = mut; 339 if (kern_prog(&kw[3], &s)) { CHECK(0, "codegen gate"); return 1; } 340 uint32_t sd = 700; 341 for (unsigned i = 0; i < GN; i++) { a[i] = frand(&sd, -3, 3) * SCALE_2P64; g[i] = frand(&sd, -8, 8); ref[i] = a[i] * (1.0f / (1.0f + expf(-g[i]))); } 342 uint32_t args[OMEGA_DEC_GT_WORDS] = { 0 }; 343 put64(args, OMEGA_DEC_GT_A, (uintptr_t)a); put64(args, OMEGA_DEC_GT_G, (uintptr_t)g); put64(args, OMEGA_DEC_GT_OUT, (uintptr_t)a); 344 args[(OMEGA_DEC_GT_N - 0x380) / 4] = GN; 345 WsRange r[2] = { { (uintptr_t)a, (uintptr_t)a + sizeof a }, { (uintptr_t)g, (uintptr_t)g + sizeof g } }; 346 if (sim_one(&kw[3], args, OMEGA_DEC_GT_WORDS, (GN + OMEGA_DEC_THREADS - 1) / OMEGA_DEC_THREADS, 1, r, 2, "gate")) return 1; 347 int bad = 0; for (unsigned i = 0; i < GN; i++) if (!close_rel(a[i], ref[i], 5e-5f, 1e-6f)) bad++; 348 if (!mut) CHECK(bad == 0, "sim gate within 5e-5 (%d bad)", bad); else CHECK(bad > 0, "sim gate mutant caught"); 349 printf("sim gate %u mutant %d: %d/%u outside tolerance\n", GN, mut, bad, GN); 350 } 351 } 352 /* SWIGLU over rows: the group index is the row (gu [3][2 * 17408] -> act [3][17408]) */ 353 { 354 enum { ROWS = 3 }; 355 static float rgu[ROWS * 2 * WI], ract[ROWS * WI]; 356 OmegaDecSpec s; memset(&s, 0, sizeof s); s.kind = OMEGA_DEC_SWIGLU; s.width = WI; 357 if (kern_prog(&kw[1], &s)) { CHECK(0, "codegen swiglu rows"); return 1; } 358 uint32_t sd = 800; 359 for (unsigned i = 0; i < ROWS * 2 * WI; i++) rgu[i] = frand(&sd, -4, 4); 360 uint32_t args[OMEGA_DEC_SW_WORDS] = { 0 }; put64(args, OMEGA_DEC_SW_GU, (uintptr_t)rgu); put64(args, OMEGA_DEC_SW_ACT, (uintptr_t)ract); putf(args, OMEGA_DEC_SW_SCALE, 1.0f); 361 WsRange r[2] = { { (uintptr_t)rgu, (uintptr_t)rgu + sizeof rgu }, { (uintptr_t)ract, (uintptr_t)ract + sizeof ract } }; 362 if (sim_one(&kw[1], args, OMEGA_DEC_SW_WORDS, WI / OMEGA_DEC_THREADS, ROWS, r, 2, "swiglu rows")) return 1; 363 int bad = 0; 364 for (unsigned rr = 0; rr < ROWS; rr++) for (unsigned c = 0; c < WI; c++) { 365 float v = rgu[rr * 2 * WI + c], u = rgu[rr * 2 * WI + WI + c], want = v / (1.0f + expf(-v)) * u; 366 if (!close_rel(ract[rr * WI + c], want, 5e-5f, 1e-6f)) bad++; 367 } 368 CHECK(bad == 0, "sim swiglu rows within 5e-5 (%d bad)", bad); 369 printf("sim swiglu rows %d x %u: %d outside tolerance\n", ROWS, WI, bad); 370 } 371 return g_failed ? 1 : 0; 372 } 373 374 /* ---- chip ---- */ 375 static OmegaGpuLaunch axpy_launch(const Kern *k, uint32_t *args, uint32_t stride, uint32_t iters) { 376 OmegaGpuLaunch L; memset(&L, 0, sizeof L); 377 L.code_va = k->code.va; L.gpr_count = k->gpr; L.threads_x = OMEGA_DEC_THREADS; L.threads_y = 1; L.grid_x = stride / OMEGA_DEC_THREADS; L.grid_y = 1; 378 L.num_elements = (size_t)stride * iters; L.args = args; L.n_args = OMEGA_DEC_AXPY_WORDS; L.timeout_ms = 20000; 379 return L; 380 } 381 static int chip_fail(const char *what) { printf("FAIL: %s: %s\n", what, omega_gpu_session_last_error()); g_checks++; g_failed++; return 1; } 382 383 typedef struct { uint32_t L; uint64_t base_ns, pcas_ns, inline_ns, base_wall_ns, pcas_wall_ns, pcas_submits, inline_submits; int pcas_ok, inline_ok; uint64_t stamps_span_ns, stamps_min_ns, stamps_max_ns, stamps_chip_ns; } Row; 384 385 static int chip_run(const char *out_path, uint32_t reps) { 386 FILE *out = out_path ? test_out_open(out_path) : NULL; 387 if (out_path && !out) { printf("cannot write %s\n", out_path); return 1; } 388 if (specs_build()) { printf("FAIL: codegen\n"); return 1; } 389 OmegaGpuCmdList *cl = NULL; 390 if (omega_gpu_cmdlist_create(OMEGA_GPU_CMDLIST_MAX_LAUNCHES, &cl) != 0) return chip_fail("cmdlist create"); 391 omega_gpu_session_lock(); 392 Kern *ks[] = { &K_axpy, &K_axpy_big, &K_res_an, &K_res_cn, &K_res_m1, &K_res_m2, &K_res_cn_m1, &K_sw, &K_sw_m1, &K_am0, &K_am1, &K_am_m1 }; 393 for (unsigned i = 0; i < sizeof ks / sizeof ks[0]; i++) if (kern_upload(ks[i])) { omega_gpu_session_unlock(); return chip_fail("kernel upload"); } 394 OmegaGpuMem bx, ba, bx0; 395 const size_t big = (size_t)AXPY_BIG_STRIDE * AXPY_BIG_ITERS * 4u; 396 if (omega_gpu_session_alloc(big, &bx) || omega_gpu_session_alloc(big, &ba) || omega_gpu_session_alloc(big, &bx0)) { omega_gpu_session_unlock(); return chip_fail("alloc"); } 397 uint32_t s = 11; float *x0 = bx0.cpu, *a = ba.cpu, *x = bx.cpu; 398 for (size_t i = 0; i < big / 4; i++) { x0[i] = frand(&s, -1, 1); a[i] = frand(&s, -1, 1); } 399 omega_gpu_session_sync_to_gpu(&ba); 400 uint32_t args[OMEGA_DEC_AXPY_WORDS] = { 0 }; put64(args, OMEGA_DEC_AXPY_X, bx.va); put64(args, OMEGA_DEC_AXPY_A, ba.va); args[(OMEGA_DEC_AXPY_ITERS - 0x380) / 4] = 1; 401 OmegaGpuLaunch L = axpy_launch(&K_axpy, args, AXPY_STRIDE, 1); 402 static float base[AXPY_STRIDE], xref[AXPY_STRIDE]; 403 const uint32_t Ls[] = { 64, 289, 1024 }; 404 Row rows[3]; memset(rows, 0, sizeof rows); 405 if (out) fprintf(out, "{\"receipt\":\"OMEGA_GPU_CMDLIST_V1\",\"reps\":%u,\"spin_us\":%u,\"rows\":[", reps, omega_gpu_session_spin_us()); 406 for (unsigned li = 0; li < 3; li++) { 407 const uint32_t N = Ls[li]; 408 Row *r = &rows[li]; r->L = N; 409 memcpy(xref, x0, sizeof xref); for (uint32_t l = 0; l < N; l++) for (unsigned i = 0; i < AXPY_STRIDE; i++) xref[i] = xref[i] + a[i]; 410 /* baseline: one session launch at a time */ 411 uint64_t *tb = calloc(reps, 8), *tw = calloc(reps, 8), *tp = calloc(reps, 8), *tpw = calloc(reps, 8), *ti = calloc(reps, 8); 412 int base_ok = 1; 413 for (uint32_t rep = 0; rep < reps && base_ok; rep++) { 414 memcpy(x, x0, sizeof base); omega_gpu_session_sync_to_gpu(&bx); 415 uint64_t ns = 0; struct timespec t0, t1; clock_gettime(CLOCK_MONOTONIC, &t0); 416 for (uint32_t l = 0; l < N; l++) if (omega_gpu_session_launch(&L, &ns, NULL) != 0) { base_ok = 0; chip_fail("baseline launch"); break; } 417 clock_gettime(CLOCK_MONOTONIC, &t1); 418 omega_gpu_session_sync_to_cpu(&bx); 419 if (rep == 0) memcpy(base, x, sizeof base); 420 tb[rep] = ns; tw[rep] = (uint64_t)(t1.tv_sec - t0.tv_sec) * 1000000000ull + (uint64_t)(t1.tv_nsec - t0.tv_nsec); 421 } 422 if (!base_ok) break; 423 CHECK(memcmp(base, xref, sizeof base) == 0, "L=%u baseline x == host reference (bit-identical)", N); 424 r->base_ns = median_u64(tb, reps); r->base_wall_ns = median_u64(tw, reps); 425 /* main: one list, PCAS */ 426 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); o.mode = OMEGA_GPU_CMDLIST_PCAS; o.timeout_ms = 20000; 427 OmegaGpuCmdListStats st; int ok = 1; 428 for (uint32_t rep = 0; rep < reps && ok; rep++) { 429 memcpy(x, x0, sizeof base); omega_gpu_session_sync_to_gpu(&bx); 430 omega_gpu_cmdlist_reset(cl); 431 for (uint32_t l = 0; l < N; l++) if (omega_gpu_cmdlist_add(cl, &L) < 0) { ok = 0; chip_fail("cmdlist add"); break; } 432 if (!ok) break; 433 struct timespec t0, t1; clock_gettime(CLOCK_MONOTONIC, &t0); 434 int rc = omega_gpu_cmdlist_submit(cl, &o, &st); 435 clock_gettime(CLOCK_MONOTONIC, &t1); 436 omega_gpu_session_sync_to_cpu(&bx); 437 if (rc != 0) { ok = 0; printf("PCAS L=%u rep %u: submit rc %d (%s) sems_done %u first_missing %d\n", N, rep, rc, omega_gpu_session_last_error(), st.sems_done, st.first_missing); break; } 438 if (memcmp(x, base, sizeof base) != 0) { ok = 0; printf("PCAS L=%u rep %u: x differs from the baseline\n", N, rep); break; } 439 tp[rep] = st.elapsed_ns; tpw[rep] = (uint64_t)(t1.tv_sec - t0.tv_sec) * 1000000000ull + (uint64_t)(t1.tv_nsec - t0.tv_nsec); 440 r->pcas_submits = st.submits; 441 } 442 r->pcas_ok = ok; 443 CHECK(ok, "L=%u PCAS list: every rep bit-identical to the baseline, all semaphores DONE", N); 444 if (ok) { r->pcas_ns = median_u64(tp, reps); r->pcas_wall_ns = median_u64(tpw, reps); } 445 if (omega_gpu_cmdlist_is_latched()) { printf("cmdlist latched; stopping\n"); break; } 446 /* instrument: GPU timestamps around every launch (stamps on), x still identical; stamps off -> accessor refuses */ 447 if (ok) { 448 static uint64_t stamps[1025]; 449 memcpy(x, x0, sizeof base); omega_gpu_session_sync_to_gpu(&bx); 450 omega_gpu_cmdlist_reset(cl); 451 for (uint32_t l = 0; l < N; l++) (void)omega_gpu_cmdlist_add(cl, &L); 452 OmegaGpuCmdListOpts os = o; os.stamps = 1; 453 int rc = omega_gpu_cmdlist_submit(cl, &os, &st); 454 omega_gpu_session_sync_to_cpu(&bx); 455 int cnt = omega_gpu_cmdlist_stamps(cl, stamps, 1025); 456 int mono = 1; uint64_t mn = UINT64_MAX, mx = 0; 457 for (int i = 1; i < cnt; i++) { if (stamps[i] < stamps[i - 1]) mono = 0; uint64_t d = stamps[i] - stamps[i - 1]; if (d < mn) mn = d; if (d > mx) mx = d; } 458 const uint64_t span = cnt > 1 ? stamps[cnt - 1] - stamps[0] : 0; 459 CHECK(rc == 0 && memcmp(x, base, sizeof base) == 0 && cnt == (int)N + 1 && mono && span > 0 && span <= st.elapsed_ns, 460 "L=%u stamps on: rc %d, x identical, %d stamps (want %u), monotonic %d, span %.1f us <= chip %.1f us, per launch min %.2f max %.2f us", 461 N, rc, cnt, N + 1, mono, span / 1e3, st.elapsed_ns / 1e3, mn / 1e3, mx / 1e3); 462 r->stamps_span_ns = span; r->stamps_min_ns = mn; r->stamps_max_ns = mx; r->stamps_chip_ns = st.elapsed_ns; 463 memcpy(x, x0, sizeof base); omega_gpu_session_sync_to_gpu(&bx); 464 omega_gpu_cmdlist_reset(cl); 465 for (uint32_t l = 0; l < N; l++) (void)omega_gpu_cmdlist_add(cl, &L); 466 rc = omega_gpu_cmdlist_submit(cl, &o, &st); 467 cnt = omega_gpu_cmdlist_stamps(cl, stamps, 1025); 468 CHECK(rc == 0 && cnt < 0, "L=%u stamps off: submit rc %d, stamps accessor refuses (%d)", N, rc, cnt); 469 } 470 /* INLINE mode (the session's own words per launch, several submissions) */ 471 o.mode = OMEGA_GPU_CMDLIST_INLINE; ok = 1; 472 for (uint32_t rep = 0; rep < reps && ok; rep++) { 473 memcpy(x, x0, sizeof base); omega_gpu_session_sync_to_gpu(&bx); 474 omega_gpu_cmdlist_reset(cl); 475 for (uint32_t l = 0; l < N; l++) if (omega_gpu_cmdlist_add(cl, &L) < 0) { ok = 0; chip_fail("cmdlist add"); break; } 476 if (!ok) break; 477 int rc = omega_gpu_cmdlist_submit(cl, &o, &st); 478 omega_gpu_session_sync_to_cpu(&bx); 479 if (rc != 0) { ok = 0; printf("INLINE L=%u rep %u: submit rc %d (%s) sems_done %u first_missing %d\n", N, rep, rc, omega_gpu_session_last_error(), st.sems_done, st.first_missing); break; } 480 if (memcmp(x, base, sizeof base) != 0) { ok = 0; printf("INLINE L=%u rep %u: x differs from the baseline\n", N, rep); break; } 481 ti[rep] = st.elapsed_ns; r->inline_submits = st.submits; 482 } 483 r->inline_ok = ok; 484 CHECK(ok, "L=%u INLINE list: every rep bit-identical to the baseline, all semaphores DONE", N); 485 if (ok) r->inline_ns = median_u64(ti, reps); 486 if (omega_gpu_cmdlist_is_latched()) { printf("cmdlist latched; stopping\n"); break; } 487 printf("L=%u: baseline %.1f us/launch (chip %.1f us, wall %.1f us per launch); PCAS %s %.2f us/launch (wall %.2f, %" PRIu64 " submits); INLINE %s %.2f us/launch (%" PRIu64 " submits)\n", 488 N, r->base_ns / 1e3 / N, r->base_ns / 1e3 / N, r->base_wall_ns / 1e3 / N, r->pcas_ok ? "ok" : "FAIL", r->pcas_ns / 1e3 / N, r->pcas_wall_ns / 1e3 / N, r->pcas_submits, 489 r->inline_ok ? "ok" : "FAIL", r->inline_ns / 1e3 / N, r->inline_submits); 490 if (out) fprintf(out, "%s{\"L\":%u,\"baseline_ns\":%" PRIu64 ",\"baseline_wall_ns\":%" PRIu64 ",\"pcas_ok\":%d,\"pcas_ns\":%" PRIu64 ",\"pcas_wall_ns\":%" PRIu64 ",\"pcas_submits\":%" PRIu64 ",\"inline_ok\":%d,\"inline_ns\":%" PRIu64 ",\"inline_submits\":%" PRIu64 ",\"stamps_span_ns\":%" PRIu64 ",\"stamps_min_ns\":%" PRIu64 ",\"stamps_max_ns\":%" PRIu64 ",\"stamps_chip_ns\":%" PRIu64 "}", 491 li ? "," : "", N, r->base_ns, r->base_wall_ns, r->pcas_ok, r->pcas_ns, r->pcas_wall_ns, r->pcas_submits, r->inline_ok, r->inline_ns, r->inline_submits, r->stamps_span_ns, r->stamps_min_ns, r->stamps_max_ns, r->stamps_chip_ns); 492 free(tb); free(tw); free(tp); free(tpw); free(ti); 493 } 494 if (out) fprintf(out, "],"); 495 /* negative control: drop launch 32 of 64 from the stream; the semaphore check must name it */ 496 if (!omega_gpu_cmdlist_is_latched()) { 497 const uint32_t N = 64; 498 memcpy(xref, x0, sizeof xref); for (uint32_t l = 0; l + 1 < N; l++) for (unsigned i = 0; i < AXPY_STRIDE; i++) xref[i] = xref[i] + a[i]; 499 memcpy(x, x0, sizeof base); omega_gpu_session_sync_to_gpu(&bx); 500 omega_gpu_cmdlist_reset(cl); 501 for (uint32_t l = 0; l < N; l++) omega_gpu_cmdlist_add(cl, &L); 502 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); o.mode = OMEGA_GPU_CMDLIST_PCAS; o.timeout_ms = 20000; o.skip_launch = 33; 503 OmegaGpuCmdListStats st; int rc = omega_gpu_cmdlist_submit(cl, &o, &st); 504 omega_gpu_session_sync_to_cpu(&bx); 505 CHECK(rc != 0 && st.first_missing == 32 && st.sems_done == N - 1, "negative control: skipped launch 32 caught (rc %d, first_missing %d, sems_done %u)", rc, st.first_missing, st.sems_done); 506 CHECK(memcmp(x, xref, sizeof base) == 0, "negative control: x == x0 + 63 a (the other 63 launches ran)"); 507 printf("negative control skip_launch=33: rc %d first_missing %d sems_done %u/%u x %s\n", rc, st.first_missing, st.sems_done, N, memcmp(x, xref, sizeof base) == 0 ? "x0+63a" : "UNEXPECTED"); 508 if (out) fprintf(out, "\"skip_control\":{\"rc\":%d,\"first_missing\":%d,\"sems_done\":%u},", rc, st.first_missing, st.sems_done); 509 } 510 /* informational: no WFI between heavy dependent launches (256 CTAs x 16 iterations each, 32 launches) */ 511 if (!omega_gpu_cmdlist_is_latched()) { 512 const uint32_t N = 32; const size_t n = (size_t)AXPY_BIG_STRIDE * AXPY_BIG_ITERS; 513 float *xr = malloc(n * 4); memcpy(xr, x0, n * 4); 514 for (uint32_t l = 0; l < N; l++) for (size_t i = 0; i < n; i++) xr[i] = xr[i] + a[i]; 515 uint32_t ab[OMEGA_DEC_AXPY_WORDS] = { 0 }; put64(ab, OMEGA_DEC_AXPY_X, bx.va); put64(ab, OMEGA_DEC_AXPY_A, ba.va); ab[(OMEGA_DEC_AXPY_ITERS - 0x380) / 4] = AXPY_BIG_ITERS; 516 OmegaGpuLaunch LB = axpy_launch(&K_axpy_big, ab, AXPY_BIG_STRIDE, AXPY_BIG_ITERS); 517 size_t diff_wfi = 0, diff_none = 0; uint64_t ns_wfi = 0, ns_none = 0; int rc_wfi = -1, rc_none = -1; 518 for (int variant = 0; variant < 2; variant++) { 519 memcpy(x, x0, n * 4); omega_gpu_session_sync_to_gpu(&bx); 520 omega_gpu_cmdlist_reset(cl); 521 for (uint32_t l = 0; l < N; l++) omega_gpu_cmdlist_add(cl, &LB); 522 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); o.mode = OMEGA_GPU_CMDLIST_PCAS; o.timeout_ms = 20000; 523 o.barrier = variant ? OMEGA_GPU_CMDLIST_BARRIER_NONE : OMEGA_GPU_CMDLIST_BARRIER_WFI; 524 OmegaGpuCmdListStats st; int rc = omega_gpu_cmdlist_submit(cl, &o, &st); 525 omega_gpu_session_sync_to_cpu(&bx); 526 size_t d = 0; for (size_t i = 0; i < n; i++) if (x[i] != xr[i]) d++; 527 if (variant) { diff_none = d; ns_none = st.elapsed_ns; rc_none = rc; } else { diff_wfi = d; ns_wfi = st.elapsed_ns; rc_wfi = rc; } 528 if (omega_gpu_cmdlist_is_latched()) break; 529 } 530 CHECK(rc_wfi == 0 && diff_wfi == 0, "heavy chain with WFI: rc %d, %zu of %zu elements differ", rc_wfi, diff_wfi, n); 531 printf("heavy chain (32 x 1M elements): WFI rc %d %zu differ %.1f us; BARRIER_NONE rc %d %zu differ %.1f us (informational: %s)\n", 532 rc_wfi, diff_wfi, ns_wfi / 1e3, rc_none, diff_none, ns_none / 1e3, 533 diff_none ? "grids overlapped without the barrier: the WFI is load-bearing" : "no difference seen; this does not prove ordering without a barrier"); 534 if (out) fprintf(out, "\"no_barrier_control\":{\"wfi_rc\":%d,\"wfi_diff\":%zu,\"wfi_ns\":%" PRIu64 ",\"none_rc\":%d,\"none_diff\":%zu,\"none_ns\":%" PRIu64 "},", rc_wfi, diff_wfi, ns_wfi, rc_none, diff_none, ns_none); 535 free(xr); 536 } 537 /* glue kernels in one list: RES ADD|NORM, SWIGLU, RES COMBINE|NORM, ARGMAX x2; then mutants */ 538 if (!omega_gpu_cmdlist_is_latched()) { 539 OmegaGpuMem m; if (omega_gpu_session_alloc(4u * (H + TK * H + TK + H + H + TK * 2 * INTER + TK * INTER + AM_PAD + 2 * OMEGA_DEC_ARGMAX_SPAN + 2 * OMEGA_DEC_ARGMAX_SPAN), &m)) { omega_gpu_session_unlock(); return chip_fail("alloc glue"); } 540 float *p = m.cpu; uint64_t va = m.va; 541 Glue g; g.x = p; g.a8 = p + H; g.w = g.a8 + TK * H; g.nw = g.w + TK; g.y = g.nw + H; g.gu = g.y + H; g.act = g.gu + TK * 2 * INTER; g.logits = g.act + TK * INTER; 542 g.ov = g.logits + AM_PAD; g.oi = (uint32_t *)(g.ov + OMEGA_DEC_ARGMAX_SPAN); float *ov2 = (float *)(g.oi + OMEGA_DEC_ARGMAX_SPAN); uint32_t *oi2 = (uint32_t *)(ov2 + OMEGA_DEC_ARGMAX_SPAN); 543 #define VA(ptr) (va + (uint64_t)((const uint8_t *)(ptr) - (const uint8_t *)p)) 544 static float x_after_an[H], y_an[H], y_cn[H], act_ref[TK * INTER], x_after_cn[H]; 545 /* 0 real; 1 norm-without-weight in slot 2 (the norm whose output is compared: slot 0 norm output is overwritten by 546 * slot 2, chip run r2 showed a slot-0 mutant cannot show); 2 combine skips expert 7 in slot 2; 3 argmax mutant */ 547 for (int mut = 0; mut < 4; mut++) { 548 for (int tie = mut == 0 ? 0 : 1; tie < 2; tie++) { /* mutants run on the tie case: the max sits in a j == 7 slot */ 549 glue_fill(&g, 400 + tie, tie); 550 memcpy(x_after_an, g.x, sizeof x_after_an); ref_res(OMEGA_DEC_RES_ADD | OMEGA_DEC_RES_NORM, x_after_an, g.a8, g.w, g.nw, y_an, EPS, 1.0f); 551 ref_swiglu(g.gu, act_ref, 1.0f); 552 memcpy(x_after_cn, x_after_an, sizeof x_after_cn); ref_res(OMEGA_DEC_RES_COMBINE | OMEGA_DEC_RES_NORM, x_after_cn, g.a8, g.w, g.nw, y_cn, EPS, SCALE_2P64); 553 omega_gpu_session_sync_to_gpu(&m); 554 uint32_t ra[OMEGA_DEC_RES_WORDS] = { 0 }, rb[OMEGA_DEC_RES_WORDS] = { 0 }, sa[OMEGA_DEC_SW_WORDS] = { 0 }, a0[OMEGA_DEC_AM_WORDS] = { 0 }, a1[OMEGA_DEC_AM_WORDS] = { 0 }; 555 put64(ra, OMEGA_DEC_RES_X, VA(g.x)); put64(ra, OMEGA_DEC_RES_A, VA(g.a8)); put64(ra, OMEGA_DEC_RES_W, VA(g.w)); put64(ra, OMEGA_DEC_RES_NW, VA(g.nw)); put64(ra, OMEGA_DEC_RES_Y, VA(g.y)); putf(ra, OMEGA_DEC_RES_EPS, EPS); putf(ra, OMEGA_DEC_RES_SCALE, 1.0f); 556 memcpy(rb, ra, sizeof rb); putf(rb, OMEGA_DEC_RES_SCALE, SCALE_2P64); 557 put64(sa, OMEGA_DEC_SW_GU, VA(g.gu)); put64(sa, OMEGA_DEC_SW_ACT, VA(g.act)); putf(sa, OMEGA_DEC_SW_SCALE, 1.0f); 558 put64(a0, OMEGA_DEC_AM_V, VA(g.logits)); put64(a0, OMEGA_DEC_AM_I, 0); put64(a0, OMEGA_DEC_AM_OV, VA(g.ov)); put64(a0, OMEGA_DEC_AM_OI, VA(g.oi)); 559 put64(a1, OMEGA_DEC_AM_V, VA(g.ov)); put64(a1, OMEGA_DEC_AM_I, VA(g.oi)); put64(a1, OMEGA_DEC_AM_OV, VA(ov2)); put64(a1, OMEGA_DEC_AM_OI, VA(oi2)); 560 OmegaGpuLaunch l[5]; memset(l, 0, sizeof l); 561 for (int i = 0; i < 5; i++) { l[i].threads_x = OMEGA_DEC_THREADS; l[i].threads_y = 1; l[i].grid_y = 1; l[i].timeout_ms = 20000; } 562 Kern *k0 = &K_res_an, *k2 = mut == 1 ? &K_res_cn_m1 : mut == 2 ? &K_res_m2 : &K_res_cn, *k3 = mut == 3 ? &K_am_m1 : &K_am0; 563 l[0].code_va = k0->code.va; l[0].gpr_count = k0->gpr; l[0].grid_x = 1; l[0].num_elements = H; l[0].args = ra; l[0].n_args = OMEGA_DEC_RES_WORDS; 564 l[1].code_va = K_sw.code.va; l[1].gpr_count = K_sw.gpr; l[1].grid_x = INTER / OMEGA_DEC_THREADS; l[1].grid_y = TK; l[1].num_elements = TK * INTER; l[1].args = sa; l[1].n_args = OMEGA_DEC_SW_WORDS; 565 l[2].code_va = k2->code.va; l[2].gpr_count = k2->gpr; l[2].grid_x = 1; l[2].num_elements = H; l[2].args = rb; l[2].n_args = OMEGA_DEC_RES_WORDS; 566 l[3].code_va = k3->code.va; l[3].gpr_count = k3->gpr; l[3].grid_x = AM_CTAS; l[3].num_elements = AM_PAD; l[3].args = a0; l[3].n_args = OMEGA_DEC_AM_WORDS; 567 l[4].code_va = K_am1.code.va; l[4].gpr_count = K_am1.gpr; l[4].grid_x = 1; l[4].num_elements = OMEGA_DEC_ARGMAX_SPAN; l[4].args = a1; l[4].n_args = OMEGA_DEC_AM_WORDS; 568 omega_gpu_cmdlist_reset(cl); 569 int ok = 1; for (int i = 0; i < 5; i++) if (omega_gpu_cmdlist_add(cl, &l[i]) < 0) { ok = 0; chip_fail("glue add"); } 570 if (!ok) break; 571 OmegaGpuCmdListOpts o; memset(&o, 0, sizeof o); o.mode = OMEGA_GPU_CMDLIST_PCAS; o.timeout_ms = 20000; 572 OmegaGpuCmdListStats st; int rc = omega_gpu_cmdlist_submit(cl, &o, &st); 573 omega_gpu_session_sync_to_cpu(&m); 574 int xbad = memcmp(g.x, x_after_cn, sizeof x_after_cn) != 0, ybad = 0, abad = 0; 575 for (unsigned i = 0; i < H; i++) if (!close_rel(g.y[i], y_cn[i], 2e-5f, 1e-6f)) ybad++; 576 for (unsigned i = 0; i < TK * INTER; i++) if (!close_rel(g.act[i], act_ref[i], 5e-5f, 1e-6f)) abad++; 577 int ambad = oi2[0] != g.am_ref; 578 if (mut == 0) { 579 CHECK(rc == 0, "glue list rc %d (%s)", rc, rc ? omega_gpu_session_last_error() : ""); 580 CHECK(!xbad, "glue tie %d: residual x after ADD then COMBINE bit-identical to the host reference", tie); 581 CHECK(ybad == 0, "glue tie %d: final RMSNorm (scale 2^64) within 2e-5 (%d bad)", tie, ybad); 582 CHECK(abad == 0, "glue tie %d: SwiGLU within 5e-5 (%d bad)", tie, abad); 583 CHECK(!ambad, "glue tie %d: argmax %u == host %u (value %g)", tie, oi2[0], g.am_ref, ov2[0]); 584 printf("glue list tie %d: rc %d, 5 launches in %u submit(s) %.1f us chip; x %s, y %d bad, act %d bad, argmax %u (ref %u)\n", tie, rc, st.submits, st.elapsed_ns / 1e3, xbad ? "DIFFERS" : "identical", ybad, abad, oi2[0], g.am_ref); 585 if (out) fprintf(out, "\"glue_tie%d\":{\"rc\":%d,\"chip_ns\":%" PRIu64 ",\"x_identical\":%d,\"y_bad\":%d,\"act_bad\":%d,\"argmax\":%u,\"argmax_ref\":%u},", tie, rc, st.elapsed_ns, !xbad, ybad, abad, oi2[0], g.am_ref); 586 } else { 587 int caught = mut == 3 ? ambad : (xbad || ybad); 588 CHECK(rc == 0 && caught, "chip mutant %d caught (rc %d, x %s, y %d bad, argmax %u ref %u)", mut, rc, xbad ? "differs" : "same", ybad, oi2[0], g.am_ref); 589 printf("chip mutant %d: rc %d, x %s, y %d bad, argmax %u (ref %u): %s\n", mut, rc, xbad ? "differs" : "same", ybad, oi2[0], g.am_ref, caught ? "caught" : "NOT CAUGHT"); 590 } 591 if (omega_gpu_cmdlist_is_latched()) break; 592 } 593 if (omega_gpu_cmdlist_is_latched()) break; 594 } 595 #undef VA 596 omega_gpu_session_free(&m); 597 } 598 omega_gpu_session_free(&bx); omega_gpu_session_free(&ba); omega_gpu_session_free(&bx0); 599 omega_gpu_session_unlock(); 600 omega_gpu_cmdlist_free(cl); 601 if (out) { fprintf(out, "\"checks\":%d,\"failed\":%d,\"verdict\":\"%s\"}\n", g_checks, g_failed, g_failed ? "FAIL" : "PASS"); fclose(out); } 602 return g_failed ? 1 : 0; 603 } 604 605 int main(int argc, char **argv) { 606 int host = 0, sim = 0; uint32_t reps = 20; const char *out = NULL; 607 for (int i = 1; i < argc; i++) { 608 if (strcmp(argv[i], "--host-only") == 0) host = 1; 609 else if (strcmp(argv[i], "--sim") == 0) sim = 1; 610 else if (strcmp(argv[i], "--reps") == 0 && i + 1 < argc) reps = (uint32_t)atoi(argv[++i]); 611 else if (strcmp(argv[i], "--spin-us") == 0 && i + 1 < argc) omega_gpu_session_set_spin_us((uint32_t)atoi(argv[++i])); 612 else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out = argv[++i]; 613 else { printf("usage: %s [--host-only|--sim] [--reps N] [--spin-us U] [--out receipt.json]\n", argv[0]); return 2; } 614 } 615 if (reps == 0) reps = 1; 616 int rc = host ? host_only() : sim ? (sim_mode() | sim_wide()) : chip_run(out, reps); 617 printf("%s: %d checks, %d failed: %s\n", host ? "host-only" : sim ? "sim" : "chip", g_checks, g_failed, rc ? "FAIL" : "PASS"); 618 return rc; 619 }