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