From 2c514b1f0cd8229347c4f1362e1b51e109ab9882 Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Sat, 24 Oct 2026 21:29:06 -0500 Subject: [PATCH 1/2] q36 router: the split form, a logits launch of ceil(rows / warps) CTAs and a select launch, behind AIEN_OMEGA_Q36_ROUTER_FUSE=1 (sc L388; bit-identical outputs, the one-launch form's bytes unchanged) The router is 1.687 ms of a 20.594 ms 1-row 35B token (L310 timeline: 40 launches, 42 MB of gate weights) and the kind's own slot is 42 us per layer for 1.05 MB, i.e. 25 GB/s. The cost is not the launch: the neighbouring launches of the same command list cost 2.6-8.3 us each, so ~36 us of the router's slot is the kernel. The one-launch kernel is ONE CTA (grid 1 x 1, 32 warps): a single SM cannot fill the bus with 1 MB of gate, and the L2/DRAM ceiling measured on this chip is 236.9 GB/s. So the split form spreads the same arithmetic over ceil(rows / warps) CTAs -- one gate row per warp, 9 CTAs for the 35B's 257 rows -- and adds one small launch that selects: launch A (grid ctas): phase 1 (RMSNorm) as today, then phase 2 with warp cta * W + w owning row cta * W + w, += ctas * W; each row's dot is the same 32-lane mapping, the same depth-grouped loads and the same warp sum as the one-launch form; the logit goes to a f32 array in the launch buffer (LB_LOGIT, 16896) instead of shared. Every CTA recomputes phase 1 identically (same W, so the same reduction tree); only CTA 0 stores x'. launch B (grid 1): phase 3 and the epilogue unchanged, on the logits copied from that array into the same shared slots (LDG.E.STRONG.SYS: another CTA of the previous launch wrote them). Dependency and visibility proof, in the message's terms: A reads x (written by the preceding K_ADD launch) and writes the logits; B reads them. Both launches sit in the graph's one command list, and the list's per-launch barrier is MTHD_WAIT_FOR_IDLE + invalidate (omega_gpu_cmdlist.c:301-306) -- the mechanism all 419 launches of a token already rely on. No new semaphore, no spin, no grid barrier. Within A, the shared xn a warp reads is written by its own CTA (BAR.SYNC as today); CTA 0's x' stores are the only writer. Costs: one launch per layer more (81 instead of 40 for the router) and 1.0 KB per layer through global for the logits (the one chain this does not keep in shared memory). The one-launch form is the default and its generated code is byte-identical: tests/q36_codegen_hash prints 509 lines, all unchanged, plus "# new" split shapes. Host proof (no chip): tests/gpu_router_test.c sim_split_point runs the logits kernel (grid ctas) and the select kernel over the same simulated device block as the one-launch kernel and compares ids, weights, x' and both group tables byte for byte -- 4 shapes (32 warps x 9 CTAs, 8 warps x 33 CTAs, depth 4 and 8, norm_topk on and off, out_scale 2^64) and mutants 1-5 caught through the pair, at both the 30B (128 rows) and the 35B (257 rows, shared slot) shape. test-gpu-router and test-gpu-router-q36 run them. Measured on the chip: see the receipt (ms/token and tokens/s with the switch off and on, against the same binary's switch-off arm). --- Makefile | 11 +- src/omega_blackwell_codegen_router.c | 77 ++++++++++++-- src/omega_blackwell_codegen_router.h | 15 ++- src/omega_gpu_q36_graph.c | 8 ++ src/omega_gpu_router_api.c | 55 ++++++++-- src/omega_gpu_router_api.h | 11 ++ tests/gpu_router_test.c | 146 +++++++++++++++++++++++++-- tests/q36_codegen_hash.c | 13 +++ 8 files changed, 305 insertions(+), 31 deletions(-) diff --git a/Makefile b/Makefile index f15716157..045cf2385 100644 --- a/Makefile +++ b/Makefile @@ -2538,11 +2538,20 @@ test-gpu-decode-attn256: $(GPU_DATTN256_TEST) .PHONY: test-gpu-router GPU_ROUTER_TEST = $(OUT_DIR)/gpu_router_test $(GPU_ROUTER_TEST): tests/gpu_router_test.c tests/bw_warp_sim_k.h src/omega_gpu_router_api.h src/omega_blackwell_codegen_router.h src/omega_gpu_fp8_api.h $(OUT_DIR)/libomega_gpu.a - $(CC) $(CFLAGS) -ffp-contract=off -Itests -o $@ tests/gpu_router_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm + $(CC) $(CFLAGS) -ffp-contract=off -DWS_MAX_THREADS=1024 -Itests -o $@ tests/gpu_router_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm test-gpu-router: $(GPU_ROUTER_TEST) ./$(GPU_ROUTER_TEST) --host-only ./$(GPU_ROUTER_TEST) --sim +# the same at the 35B shape (256 routed experts + the shared gate row, sc L306): NR = 257 rows, so the split form's logits +# launch is ceil(257 / warps) CTAs -- the shape the q36 graph records (AIEN_OMEGA_Q36_ROUTER_FUSE) +GPU_ROUTER_TEST_Q36 = $(OUT_DIR)/gpu_router_test_q36 +$(GPU_ROUTER_TEST_Q36): tests/gpu_router_test.c tests/bw_warp_sim_k.h src/omega_gpu_router_api.h src/omega_blackwell_codegen_router.h src/omega_gpu_fp8_api.h $(OUT_DIR)/libomega_gpu.a + $(CC) $(CFLAGS) -ffp-contract=off -DWS_MAX_THREADS=1024 -DROUTER_TEST_Q36 -Itests -o $@ tests/gpu_router_test.c $(OUT_DIR)/libomega_gpu.a -lpthread -lm +test-gpu-router-q36: $(GPU_ROUTER_TEST_Q36) + ./$(GPU_ROUTER_TEST_Q36) --host-only + ./$(GPU_ROUTER_TEST_Q36) --sim + # Qwen3.8-27B Gated DeltaNet decode step (omega#542, 2026-10-18): one launch per layer (omega_gpu_gdn_api.h). # test-gpu-gdn is host-only plus the warp simulator over the whole grid; the chip run is ./build/gpu_gdn_test under the GB10 lock. .PHONY: test-gpu-gdn diff --git a/src/omega_blackwell_codegen_router.c b/src/omega_blackwell_codegen_router.c index 0fab8c4eb..99a9e4cb5 100644 --- a/src/omega_blackwell_codegen_router.c +++ b/src/omega_blackwell_codegen_router.c @@ -160,6 +160,15 @@ static void lane0_sts(Em *e, int lane, int one, int addr, Op v) { isetp_ge_u32(e, lane, one); Region r = region_skip_if_p0(e); sts32(e, addr, v); region_end(e, r); } +/* the same predicate, a global f32 store: the split form's logit (one writer per row) */ +static void lane0_stg(Em *e, int lane, int one, int addr64, Op v, int udesc) { + isetp_ge_u32(e, lane, one); + Region r = region_skip_if_p0(e); stg(e, BW_IR_STG_E, addr64, v, udesc); region_end(e, r); +} +/* a 32-bit global load of any opcode (BW_IR_LDG_E or BW_IR_LDG_STRONG_SYS: system scope, no stale cached copy) */ +static void ldg32(Em *e, BlackwellIROpcode op, int d, int addr64, int udesc, int first) { + BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, first ? K_VAR : K_VAR_MORE); +} static const uint32_t NEG_BIG = 0xF149F2CAu; /* -1e30f */ static const uint32_t C_2P23 = 0x4B000000u; /* 8388608.0f; also the OR mask that turns a small integer n into the float 2^23 + n */ @@ -186,10 +195,14 @@ int omega_blackwell_codegen_router_prog(const OmegaRouterSpec *spec, BlackwellIR const int MUT = spec->mutant; OmegaRouterSmem L; const uint32_t XST = spec->xstride, LBST = spec->lbstride; /* ROWS form when set */ + const uint32_t CTAS = spec->ctas, SEL = spec->select; /* the split form (see the header): 0,0 = the plain kernel */ if ((W != 8 && W != 16 && W != 32) || (D != 1 && D != 2 && D != 4 && D != 8) || MUT < 0 || MUT > 6 || omega_router_smem(spec, &L)) return -1; if ((XST != 0) != (LBST != 0) || (XST && (XST < OMEGA_ROUTER_HIDDEN * 4u || (XST & 15u) || (LBST & 15u))) || (MUT == 6 && !XST)) return -1; const uint32_t EX = spec->experts ? spec->experts : OMEGA_ROUTER_EXPERTS, SHX = spec->shared, EPL = EX / 32u; /* logits per lane */ if (MUT == 5 && !SHX) return -1; + /* the split form: ctas >= 2 is the logits kernel, select 1 is the select kernel, never both, never with the ROWS form + * (the rows kernel is one launch per layer for every conversation already) and never with mutant 6 */ + if (SEL > 1u || (SEL && CTAS) || CTAS == 1u || ((CTAS || SEL) && (XST || LBST)) || (SEL && MUT == 6)) return -1; const uint32_t E = 64u / W; /* x values per thread: 8, 4 or 2 */ omega_bw_ir_init(prog); Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; @@ -197,11 +210,14 @@ int omega_blackwell_codegen_router_prog(const OmegaRouterSpec *spec, BlackwellIR /* ---- arguments and identities ---- */ int udesc = UV64(e); int px = V64(e), pgate = V64(e), pnorm = V64(e), pxout = V64(e); + int plogit = NOREG; int eps = V(e), oscale = V(e); int tid = V(e), warp = V(e), lane = V(e), one = V(e), zero = V(e), c2p23 = V(e), cneg = V(e), ts = V(e), t1 = V(e), t2 = V(e); + int cta = NOREG; if (e->err) return -1; ldcu64(e, udesc, 0x358); ldc64(e, px, OMEGA_ROUTER_ARG_X); ldc64(e, pgate, OMEGA_ROUTER_ARG_GATE); ldc64(e, pnorm, OMEGA_ROUTER_ARG_NORM); ldc64(e, pxout, OMEGA_ROUTER_ARG_XOUT); + if (CTAS || SEL) { plogit = V64(e); if (e->err) return -1; ldc64(e, plogit, OMEGA_ROUTER_ARG_LOGIT); } /* the split form's logits */ if (XST) { /* ROWS form: this CTA's x row and output buffer */ int cy = V(e); if (e->err) return -1; @@ -214,8 +230,10 @@ int omega_blackwell_codegen_router_prog(const OmegaRouterSpec *spec, BlackwellIR movi(e, one, 1); movrz(e, R(zero)); movi(e, c2p23, C_2P23); movi(e, cneg, NEG_BIG); shr(e, warp, tid, 5); andi(e, R(lane), R(tid), 31u); + if (CTAS) { cta = V(e); if (e->err) return -1; s2r(e, cta, BW_SR_CTAID_X); } /* this CTA's first gate row = cta * W */ - /* ---- phase 1: RMSNorm ---- */ + /* ---- phase 1: RMSNorm (every CTA of the split form recomputes it identically: same W, so the same tree) ---- */ + if (!SEL) { int toff = V(e), xa = V64(e), wa = V64(e), sq = V(e), rs = V(e), a0 = V(e); int xq[2], wq[2]; Op xe[8], we[8]; @@ -263,19 +281,29 @@ int omega_blackwell_codegen_router_prog(const OmegaRouterSpec *spec, BlackwellIR sts32(e, a0, xe[i]); fmul(e, R(ts), xe[i], R(oscale)); if (i) imadw(e, xo, one, 4u, xo); - stg(e, BW_IR_STG_E, xo, R(ts), udesc); + if (CTAS) { /* the split form: CTA 0 is the one writer of x' (the other CTAs only keep xn in their shared) */ + isetp_ge_u32(e, cta, one); + Region rg = region_skip_if_p0(e); stg(e, BW_IR_STG_E, xo, R(ts), udesc); region_end(e, rg); + } else stg(e, BW_IR_STG_E, xo, R(ts), udesc); } } bar_sync(e); + } /* end of phase 1 (the select kernel skips it) */ - /* ---- phase 2: logits, warp w -> experts w, w + W, ... ---- */ - int c128 = V(e), ex = V(e), lane16 = V(e), lane32 = V(e), gl = V64(e), rowa = V64(e), la = V(e); - int acc[4], lo = V(e), hi = V(e), xs0 = V128(e), xs1 = V128(e), ld[8]; + /* ---- phase 2: logits, warp w -> experts w, w + W, ... (split form: warp cta * W + w -> experts, += ctas * W) ---- */ + int c128 = NOREG, ex = NOREG, lane16 = NOREG, lane32 = NOREG, gl = NOREG, rowa = NOREG, la = NOREG; + int acc[4] = { NOREG, NOREG, NOREG, NOREG }, lo = NOREG, hi = NOREG, xs0 = NOREG, xs1 = NOREG, ld[8]; + if (SEL) { /* the select kernel runs no logits: phase 3 only needs the two lane offsets */ + lane16 = V(e); lane32 = V(e); + imadi(e, R(lane16), R(lane), 16u, NOREG); imadi(e, R(lane32), R(lane), 32u, NOREG); + } else { + c128 = V(e); ex = V(e); lane16 = V(e); lane32 = V(e); gl = V64(e); rowa = V64(e); la = V(e); + lo = V(e); hi = V(e); xs0 = V128(e); xs1 = V128(e); /* the declaration order of the one-launch form (byte-identical code) */ for (unsigned i = 0; i < 4; i++) acc[i] = V(e); for (uint32_t d = 0; d < D; d++) ld[d] = V128(e); if (e->err) return -1; movi(e, c128, EX + SHX); /* rows: the experts, then the shared gate */ - mov(e, R(ex), R(warp)); + if (CTAS) imadi(e, R(ex), R(cta), W, warp); else mov(e, R(ex), R(warp)); imadi(e, R(lane16), R(lane), 16u, NOREG); imadi(e, R(lane32), R(lane), 32u, NOREG); imadw(e, gl, lane16, 1u, pgate); @@ -302,12 +330,42 @@ int omega_blackwell_codegen_router_prog(const OmegaRouterSpec *spec, BlackwellIR } fadd(e, R(acc[0]), R(acc[0]), R(acc[1])); fadd(e, R(acc[2]), R(acc[2]), R(acc[3])); fadd(e, R(acc[0]), R(acc[0]), R(acc[2])); warp_sum(e, acc[0], ts); - imadi(e, R(la), R(ex), 4u, NOREG); imadi(e, R(la), R(one), L.logit, la); - lane0_sts(e, lane, one, la, R(acc[0])); - imadi(e, R(ex), R(one), W, ex); + if (CTAS) { /* split form: the logit goes to the caller's f32 array (the select launch reads it) */ + int la64 = V64(e); + if (e->err) return -1; + imadw(e, la64, ex, 4u, plogit); + lane0_stg(e, lane, one, la64, R(acc[0]), udesc); + } else { + imadi(e, R(la), R(ex), 4u, NOREG); imadi(e, R(la), R(one), L.logit, la); + lane0_sts(e, lane, one, la, R(acc[0])); + } + if (CTAS) imadi(e, R(ex), R(one), CTAS * W, ex); else imadi(e, R(ex), R(one), W, ex); bra(e, L_e); loop_end(e); patch_bra(e, bra_edone, here(e)); + } /* end of the logits phase (the select kernel runs none) */ + + if (!CTAS) { /* the plain kernel and the select kernel: phase 3 and the epilogue */ + if (SEL) { /* the select kernel: the logits kernel's CTAs wrote them; copy them into the phase-3 shared slots. One + * predicated round per `threads` rows (uniform control flow: a warp may have a mix of copying and idle + * lanes, and the region's BSYNC reconverges it before phase 3's shuffles). */ + int cla = V64(e), cv = V(e), cd = V(e), crows = V(e), ci = V(e); + if (e->err) return -1; + movi(e, crows, EX + SHX); + mov(e, R(ci), R(tid)); + const uint32_t rounds = (EX + SHX + 32u * W - 1u) / (32u * W); + for (uint32_t k = 0; k < rounds; k++) { + if (k) imadi(e, R(ci), R(one), 32u * W, ci); /* ci += threads */ + isetp_ge_u32(e, ci, crows); + Region rg = region_skip_if_p0(e); + imadw(e, cla, ci, 4u, plogit); /* the logit of row ci */ + ldg32(e, BW_IR_LDG_STRONG_SYS, cv, cla, udesc, 1); /* another CTA of the previous launch wrote it: system scope */ + movi(e, cd, L.logit); /* its phase-3 slot: L.logit + ci * 4 */ + imadi(e, R(cd), R(ci), 4u, cd); + sts32(e, cd, R(cv)); + region_end(e, rg); + } + } bar_sync(e); /* ---- phase 3: top-8 (every warp, uniform barriers) ---- */ @@ -453,6 +511,7 @@ int omega_blackwell_codegen_router_prog(const OmegaRouterSpec *spec, BlackwellIR region_end(e, rg); } patch_bra(e, bra_tail, here(e)); + } /* end of phase 3 + epilogue */ tail(e); (void)zero; if (e->err) return -1; diff --git a/src/omega_blackwell_codegen_router.h b/src/omega_blackwell_codegen_router.h index a149b630f..8d5366ffd 100644 --- a/src/omega_blackwell_codegen_router.h +++ b/src/omega_blackwell_codegen_router.h @@ -75,7 +75,10 @@ #define OMEGA_ROUTER_ARG_EPS 0x3d0u /* f32 rms eps */ #define OMEGA_ROUTER_ARG_OUT_SCALE 0x3d4u /* f32 */ #define OMEGA_ROUTER_ARG_NORM_TOPK 0x3d8u /* u32 0 or 1 (config.json norm_topk_prob) */ -#define OMEGA_ROUTER_ARGS_WORDS 23u +#define OMEGA_ROUTER_ARG_LOGIT 0x3e0u /* split form: u64 to f32 [experts + shared], the logits the logits kernel writes + * (STG.E by lane 0 of the row's warp) and the select kernel reads (LDG.E.STRONG.SYS): + * written by another CTA, so it is read with the system-scope load */ +#define OMEGA_ROUTER_ARGS_WORDS 26u /* shared memory map (bytes) */ #define OMEGA_ROUTER_SM_XN 0u /* xn[2048] f32: 8192 */ @@ -103,6 +106,16 @@ typedef struct { * (>= 8192, a multiple of 16); CTA cy reads X + cy * xstride. */ uint32_t lbstride; /* ROWS form: bytes between consecutive output buffers (a multiple of 16): CTA cy writes * XOUT, IDS, WTS, TAB_GU and TAB_DN at + cy * lbstride. Both or neither of the strides are set. */ + /* ---- the split form (sc L388, AIEN_OMEGA_Q36_ROUTER_FUSE): the one launch of the plain form is latency-bound + * because a single CTA of 32 warps streams one layer's 1.05 MB gate at ~25 GB/s. The split form runs the same + * arithmetic in two launches of the same command list: `ctas` CTAs of W warps compute the logits of one layer + * (warp g = cta * W + warp owns row g, g += ctas * W) and write them to the f32 array ARG_LOGIT names, then one + * CTA selects (phase 3 + epilogue) from that array. Every per-row op is unchanged (one warp per row, the same + * lane -> column mapping, the same accumulation order and the same warp sum), phase 1 is recomputed identically + * by every CTA (same W, so the same reduction tree), and only CTA 0 stores x' -- so the outputs are + * bit-identical to the plain form. 0 = the plain form. */ + uint32_t ctas; /* split form, logits kernel: CTAs (>= 2); 0 = plain. Refused with the ROWS form and mutant 6. */ + uint32_t select; /* split form, select kernel: 1 = the phase-3 + epilogue kernel that reads ARG_LOGIT (grid 1) */ } OmegaRouterSpec; /* Shared memory map of a spec (bytes); the OMEGA_ROUTER_SM_* constants are the 128 / no-shared case. */ diff --git a/src/omega_gpu_q36_graph.c b/src/omega_gpu_q36_graph.c index 57fe6ddc2..e6794cc64 100644 --- a/src/omega_gpu_q36_graph.c +++ b/src/omega_gpu_q36_graph.c @@ -259,6 +259,14 @@ int omega_gpu_q36_graph_create_heads(const OmegaGpuQ36GraphConfig *cfg, uint32_t omega_gpu_session_unlock(); if (rc != OMEGA_GPU_DGRAPH_OK) { omega_gpu_q36_graph_free(g); return rc; } { const char *e = getenv("AIEN_OMEGA_Q36_BATCH"); if (e && e[0] == '1' && e[1] == 0 && omega_gpu_q36_graph_set_batch(g, 1) != OMEGA_GPU_DGRAPH_OK) { omega_gpu_q36_graph_free(g); return OMEGA_GPU_DGRAPH_CHIP_FAIL; } } + /* sc L388: the router's split form (two launches per layer, one launch per CTA-group of gate rows; ~25 GB/s -> the bus + * for the one-launch form). OFF unless AIEN_OMEGA_Q36_ROUTER_FUSE=1: then every layer's router is recorded as a logits + * launch of ceil(rows / warps) CTAs and a select launch, bit-identical outputs (omega_gpu_router_set_split). */ + { const char *e = getenv("AIEN_OMEGA_Q36_ROUTER_FUSE"); + if (e && e[0] == '1' && e[1] == 0) { + const uint32_t rows = omega_gpu_router_rows(cfg->router), ctas = (rows + OMEGA_GPU_ROUTER_WARPS - 1u) / OMEGA_GPU_ROUTER_WARPS; + if (omega_gpu_router_set_split(cfg->router, ctas) != OMEGA_GPU_ROUTER_OK) { omega_gpu_q36_graph_free(g); return OMEGA_GPU_DGRAPH_CHIP_FAIL; } + } } *out = g; return OMEGA_GPU_DGRAPH_OK; } diff --git a/src/omega_gpu_router_api.c b/src/omega_gpu_router_api.c index fc06a7b08..1bb406b92 100644 --- a/src/omega_gpu_router_api.c +++ b/src/omega_gpu_router_api.c @@ -37,9 +37,10 @@ #define LB_WTS 16448u #define LB_TAB_GU 16512u #define LB_TAB_DN 16640u /* 8 slots; with the shared slot (9) the down table is at LB_TAB_DN9 */ -#define LB_BYTES 16768u #define LB_TAB_DN9 16656u -#define LB_BYTES9 16800u +#define LB_LOGIT 16896u /* sc L388 (the split form): f32 [experts + shared], the logits the logits launch writes and the + * select launch reads; 256 bytes past the last table word (16800), so every offset above stays put */ +#define LB_LOGIT_BYTES(rows) ((((rows) * 4u) + 255u) & ~255u) struct OmegaGpuRouter { uint32_t layers; @@ -49,7 +50,7 @@ struct OmegaGpuRouter { OmegaGpuMem lb; /* launch buffer */ uint32_t experts, shared; /* routed experts (128 or 256) and the shared slot (0 or 1) */ uint32_t rows, slots; /* gate rows = experts + shared; output slots = 8 + shared */ - uint32_t lb_tab_dn, lb_bytes; + uint32_t lb_tab_dn, lb_bytes, split_ctas; /* split_ctas: 0 = the one-launch form, else the split form's CTAs (L388) */ }; #define ROWB(r) ((size_t)(r)->rows * OMEGA_ROUTER_ROW_BYTES) /* gate bytes per layer */ #define DIRB(r) ((size_t)(r)->rows * 16u) /* one directory per layer */ @@ -65,14 +66,16 @@ static struct { bool hooked; Slot slot; /* the plain kernel (grid 1 x 1) */ Slot rows; /* the ROWS form (lever 2: one launch per layer for every conversation); omega_gpu_router_prepare_rows */ + Slot split; /* the split form's logits kernel (grid ctas x 1, L388); omega_gpu_router_set_split */ + Slot sel; /* the split form's select kernel (grid 1 x 1, L388) */ uint32_t warps, depth; int mutant; } g; static int g_rec; /* 1 while a record call runs (lock held): kernel_for must not evict */ static void slot_free(Slot *s) { if (s->valid) omega_gpu_session_free(&s->code); memset(s, 0, sizeof *s); } -static void slot_clear_locked(void) { slot_free(&g.slot); slot_free(&g.rows); } -static void on_session_close(void) { memset(&g.slot, 0, sizeof g.slot); memset(&g.rows, 0, sizeof g.rows); } +static void slot_clear_locked(void) { slot_free(&g.slot); slot_free(&g.rows); slot_free(&g.split); slot_free(&g.sel); } +static void on_session_close(void) { memset(&g.slot, 0, sizeof g.slot); memset(&g.rows, 0, sizeof g.rows); memset(&g.split, 0, sizeof g.split); memset(&g.sel, 0, sizeof g.sel); } static OmegaGpuSession *dev_open_locked(void) { if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; } return omega_gpu_session_open(); @@ -103,7 +106,8 @@ int omega_gpu_router_alloc_shape(uint32_t layers, uint32_t experts, uint32_t sha OmegaGpuRouter *r = omega_gpu_host_calloc(1, sizeof *r); if (!r) return OMEGA_GPU_ROUTER_BAD_ARGS; r->layers = layers; r->experts = experts; r->shared = shared; r->rows = experts + shared; r->slots = OMEGA_ROUTER_TOPK + shared; - r->lb_tab_dn = shared ? LB_TAB_DN9 : LB_TAB_DN; r->lb_bytes = shared ? LB_BYTES9 : LB_BYTES; + r->lb_tab_dn = shared ? LB_TAB_DN9 : LB_TAB_DN; + r->lb_bytes = LB_LOGIT + LB_LOGIT_BYTES(r->rows); /* the one-launch layout plus the split form's logit array (L388) */ omega_gpu_session_lock(); int rc = OMEGA_GPU_ROUTER_OK; if (!dev_open_locked()) rc = OMEGA_GPU_ROUTER_CHIP_FAIL; @@ -186,7 +190,7 @@ int omega_gpu_router_read_back(OmegaGpuRouter *r, float *xout, uint64_t *tab_gu, /* ---- kernel ---- */ static int kernel_for(const OmegaRouterSpec *spec, Slot **out, bool *hit) { - Slot *s = spec->xstride ? &g.rows : &g.slot; + Slot *s = spec->select ? &g.sel : spec->ctas ? &g.split : spec->xstride ? &g.rows : &g.slot; if (s->valid && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_ROUTER_OK; } if (g_rec && s->valid) { omega_gpu_session_set_error("router record: kernel cache miss would evict the resident kernel (warm up before recording)"); return OMEGA_GPU_ROUTER_TOO_LARGE; } slot_free(s); @@ -229,14 +233,20 @@ static int router_core(OmegaGpuRouter *r, uint32_t layer, const float *x, float OmegaRouterSpec spec; spec_for(r, &spec, rows > 1u ? xstride : 0u, rows > 1u ? lbstride : 0u); OmegaRouterSmem sm; if (omega_router_smem(&spec, &sm) != 0) return OMEGA_GPU_ROUTER_BAD_ARGS; + /* L388: the split form of the one-row record path (two launches, logits and select; the header has the argument) */ + const int split = rec != NULL && rows <= 1u && r->split_ctas >= 2u; + if (split) { spec.ctas = r->split_ctas; if (spec.xstride || spec.lbstride || spec.mutant == 6 || spec.select) return OMEGA_GPU_ROUTER_BAD_ARGS; } + OmegaRouterSpec spec_sel = spec; + if (split) { spec_sel.ctas = 0; spec_sel.select = 1; } if (!rec) omega_gpu_session_lock(); #define UNLOCK() do { if (!rec) omega_gpu_session_unlock(); } while (0) int rc = OMEGA_GPU_ROUTER_OK; if (!dev_open_locked()) { UNLOCK(); return OMEGA_GPU_ROUTER_CHIP_FAIL; } - Slot *ks = NULL; bool hit = false; + Slot *ks = NULL, *kss = NULL; bool hit = false, hit_s = false; g_rec = rec != NULL; rc = kernel_for(&spec, &ks, &hit); + if (rc == OMEGA_GPU_ROUTER_OK && split) rc = kernel_for(&spec_sel, &kss, &hit_s); g_rec = 0; if (rc != OMEGA_GPU_ROUTER_OK) { UNLOCK(); return rc; } uint8_t *b = r->lb.cpu; @@ -251,6 +261,7 @@ static int router_core(OmegaGpuRouter *r, uint32_t layer, const float *x, float } uint32_t args[OMEGA_ROUTER_ARGS_WORDS]; + memset(args, 0, sizeof args); const uint64_t gate = r->gate.va + (uint64_t)layer * ROWB(r); const uint64_t norm = r->norm.va + (uint64_t)layer * OMEGA_ROUTER_HIDDEN * 4u; const uint64_t dgu = r->dir.va + (uint64_t)layer * 2u * DIRB(r), ddn = dgu + DIRB(r); @@ -259,17 +270,18 @@ static int router_core(OmegaGpuRouter *r, uint32_t layer, const float *x, float const uint64_t lb = rec_lb ? rec_lb : r->lb.va; /* record_to: the outputs land in the caller's buffer (one per conversation in the stroke) */ A64(0, rec ? dev_x : r->lb.va + LB_X); A64(2, gate); A64(4, norm); A64(6, lb + LB_XOUT); A64(8, lb + LB_IDS); A64(10, lb + LB_WTS); A64(12, dgu); A64(14, ddn); A64(16, lb + LB_TAB_GU); A64(18, lb + r->lb_tab_dn); + A64(24, lb + LB_LOGIT); /* the split form's logit array (the one-launch form never reads it) */ #undef A64 args[20] = ue; args[21] = uo; args[22] = norm_topk ? 1u : 0u; if (info) { info->layer = layer; info->warps = spec.warps; info->depth = spec.depth; info->threads = 32u * spec.warps; info->weight_bytes = ROWB(r); - info->kernel_cache_hit = hit; + info->kernel_cache_hit = hit && (!split || hit_s); } if (rc == OMEGA_GPU_ROUTER_OK) { OmegaGpuLaunch L = { .code_va = ks->code.va, .gpr_count = gpr_declared(ks->kernel.gpr_count), - .threads_x = 32u * spec.warps, .threads_y = 1, .grid_x = 1, .grid_y = rows > 1u ? rows : 1u, + .threads_x = 32u * spec.warps, .threads_y = 1, .grid_x = split ? r->split_ctas : 1, .grid_y = rows > 1u ? rows : 1u, .num_elements = (size_t)OMEGA_ROUTER_HIDDEN * (rows > 1u ? rows : 1u), .args = args, .n_args = OMEGA_ROUTER_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS, .shared_bytes = sm.total, @@ -277,6 +289,15 @@ static int router_core(OmegaGpuRouter *r, uint32_t layer, const float *x, float if (!omega_bw_gpr_declared_ok(L.gpr_count, ks->kernel.gpr_count)) { omega_gpu_session_set_error("declared registers below peak + 2: refusing to launch"); rc = OMEGA_GPU_ROUTER_CHIP_FAIL; } else if (rec) { if (omega_gpu_cmdlist_add(rec, &L) < 0) rc = OMEGA_GPU_ROUTER_CHIP_FAIL; } else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL) != 0) rc = OMEGA_GPU_ROUTER_CHIP_FAIL; + /* the split form: the select launch, right after the logits launch in the same list (the list's barrier makes the + * logits visible; the select kernel reads them with LDG.E.STRONG.SYS) */ + if (rc == OMEGA_GPU_ROUTER_OK && split) { + OmegaGpuLaunch S = L; + S.code_va = kss->code.va; S.gpr_count = gpr_declared(kss->kernel.gpr_count); + S.threads_x = 32u * spec_sel.warps; S.grid_x = 1; S.grid_y = 1; + if (!omega_bw_gpr_declared_ok(S.gpr_count, kss->kernel.gpr_count)) { omega_gpu_session_set_error("declared registers below peak + 2: refusing to launch"); rc = OMEGA_GPU_ROUTER_CHIP_FAIL; } + else if (omega_gpu_cmdlist_add(rec, &S) < 0) rc = OMEGA_GPU_ROUTER_CHIP_FAIL; + } } if (rc == OMEGA_GPU_ROUTER_OK && !rec && omega_gpu_session_sync_to_cpu(&r->lb) != 0) rc = OMEGA_GPU_ROUTER_CHIP_FAIL; if (rc == OMEGA_GPU_ROUTER_OK && !rec) { @@ -342,6 +363,20 @@ int omega_gpu_router_record_rows(OmegaGpuCmdList *cl, OmegaGpuRouter *r, uint32_ } return rc; } +/* The split form (L388): ctas 0 = the one-launch form; 2 .. OMEGA_GPU_ROUTER_MAX_ROWS*... = the logits launch's CTAs. The two + * kernels are built on the first record (a warm-up here keeps that off the timed path). */ +int omega_gpu_router_set_split(OmegaGpuRouter *r, uint32_t ctas) { + if (!r || ctas > 64u || ctas == 1u) return OMEGA_GPU_ROUTER_BAD_ARGS; + if (ctas && (r->lb_bytes < LB_LOGIT + LB_LOGIT_BYTES(r->rows))) return OMEGA_GPU_ROUTER_BAD_ARGS; /* the buffer predates L388 */ + omega_gpu_session_lock(); + r->split_ctas = ctas; + slot_free(&g.split); slot_free(&g.sel); /* the next record rebuilds them for the new shape */ + omega_gpu_session_unlock(); + return OMEGA_GPU_ROUTER_OK; +} +uint32_t omega_gpu_router_split(const OmegaGpuRouter *r) { return r ? r->split_ctas : 0u; } +uint32_t omega_gpu_router_rows(const OmegaGpuRouter *r) { return r ? r->rows : 0u; } + /* Builds (or finds) the ROWS kernel for these strides and keeps it resident beside the plain one (its own cache slot). */ int omega_gpu_router_prepare_rows(OmegaGpuRouter *r, uint32_t xstride, uint32_t lbstride) { if (!r || xstride < OMEGA_ROUTER_HIDDEN * 4u || (xstride & 15u) || lbstride < r->lb_bytes || (lbstride & 255u)) return OMEGA_GPU_ROUTER_BAD_ARGS; diff --git a/src/omega_gpu_router_api.h b/src/omega_gpu_router_api.h index 1232e8aaa..0e1abf560 100644 --- a/src/omega_gpu_router_api.h +++ b/src/omega_gpu_router_api.h @@ -112,6 +112,17 @@ int omega_gpu_router_record_to(struct OmegaGpuCmdList *cl, OmegaGpuRouter *r, ui int omega_gpu_router_prepare_rows(OmegaGpuRouter *r, uint32_t xstride, uint32_t lbstride); int omega_gpu_router_record_rows(struct OmegaGpuCmdList *cl, OmegaGpuRouter *r, uint32_t layer, uint64_t x_va, uint32_t xstride, float eps, int norm_topk, float out_scale, uint64_t lb_va, uint32_t lbstride, uint32_t rows, OmegaGpuRouterDevOut *out); +/* sc L388 (AIEN_OMEGA_Q36_ROUTER_FUSE): the split form of the one-row record path (record / record_to, rows 1). The layer's + * 1.05 MB gate is streamed by ONE CTA of 32 warps in the one-launch form, ~25 GB/s; the split form records two launches + * instead of one: `ctas` CTAs compute the logits (each warp one gate row, the row's dot exactly as the one-launch form + * computes it) into the launch buffer's logit array (LB_LOGIT: f32 [experts + shared]), and one CTA selects (top-8, the + * softmax denominator, the weights, the ids and the two group tables) from that array. Outputs are bit-identical to the + * one-launch form; the cost is one extra launch in the list. ctas 0 = the one-launch form (the default). A ctas above + * 64 or 1 is refused; the two launches need the launch-buffer slot, so the caller must keep the list ordering (the select + * launch is appended right after the logits launch). */ +int omega_gpu_router_set_split(OmegaGpuRouter *r, uint32_t ctas); +uint32_t omega_gpu_router_split(const OmegaGpuRouter *r); +uint32_t omega_gpu_router_rows(const OmegaGpuRouter *r); /* gate rows = experts + shared (257 for the 35B) */ uint32_t omega_gpu_router_layout_bytes(const OmegaGpuRouter *r); int omega_gpu_router_read_back(OmegaGpuRouter *r, float *xout, uint64_t *tab_gu, uint64_t *tab_dn); const char *omega_gpu_router_last_error(void); diff --git a/tests/gpu_router_test.c b/tests/gpu_router_test.c index 1ebf89399..7c9d6af4d 100644 --- a/tests/gpu_router_test.c +++ b/tests/gpu_router_test.c @@ -166,6 +166,23 @@ static void host_only(void) { { OmegaRouterSpec s = { .warps = 8, .depth = 8, .xstride = 4096, .lbstride = 16896 SPEC_SHAPE }; CHECK(omega_router_prog_stats(&s, NULL, NULL) != 0, "xstride below one row refused"); } { OmegaRouterSpec s = { .warps = 8, .depth = 8, .xstride = 8192, .lbstride = 16900 SPEC_SHAPE }; CHECK(omega_router_prog_stats(&s, NULL, NULL) != 0, "unaligned lbstride refused"); } { OmegaRouterSpec s = { .warps = 8, .depth = 8, .mutant = 6 SPEC_SHAPE }; CHECK(omega_router_prog_stats(&s, NULL, NULL) != 0, "mutant 6 without the ROWS form refused"); } + /* the split form (L388): the logits kernel (grid ctas) and the select kernel (grid 1) at every warp count, and the + * shapes it must refuse (ctas 1, ctas + select, either with the ROWS form, ctas with mutant 6) */ + for (unsigned i = 0; i < 3; i++) { + OmegaRouterSpec a = { .warps = ws[i], .depth = 4, .ctas = (NR + ws[i] - 1u) / ws[i] SPEC_SHAPE }; + OmegaRouterSpec b = { .warps = ws[i], .depth = 4, .select = 1 SPEC_SHAPE }; + uint32_t na = 0, ga = 0, nb = 0, gb = 0; + int ra = omega_router_prog_stats(&a, &na, &ga), rb = omega_router_prog_stats(&b, &nb, &gb); + uint32_t da = ((ga + 1 + 7) / 8) * 8, db = ((gb + 1 + 7) / 8) * 8; + CHECK(ra == 0 && na <= 1024 && 32u * ws[i] * da <= 65536u, "codegen split logits warps %u ctas %u: rc %d insns %u gprs %u declared %u", ws[i], a.ctas, ra, na, ga, da); + CHECK(rb == 0 && nb <= 1024 && 32u * ws[i] * db <= 65536u, "codegen split select warps %u: rc %d insns %u gprs %u declared %u", ws[i], rb, nb, gb, db); + if (ra == 0 && rb == 0) printf("codegen split warps %2u ctas %2u: logits %u insns (%u regs), select %u insns (%u regs)\n", ws[i], a.ctas, na, ga, nb, gb); + } + { OmegaRouterSpec s = { .warps = 8, .depth = 4, .ctas = 1 SPEC_SHAPE }; CHECK(omega_router_prog_stats(&s, NULL, NULL) != 0, "split ctas 1 refused"); } + { OmegaRouterSpec s = { .warps = 8, .depth = 4, .ctas = 2, .select = 1 SPEC_SHAPE }; CHECK(omega_router_prog_stats(&s, NULL, NULL) != 0, "split ctas + select refused"); } + { OmegaRouterSpec s = { .warps = 8, .depth = 4, .ctas = 2, .xstride = 8192, .lbstride = 16896 SPEC_SHAPE }; CHECK(omega_router_prog_stats(&s, NULL, NULL) != 0, "split with the ROWS form refused"); } + { OmegaRouterSpec s = { .warps = 8, .depth = 4, .select = 1, .xstride = 8192, .lbstride = 16896 SPEC_SHAPE }; CHECK(omega_router_prog_stats(&s, NULL, NULL) != 0, "select with the ROWS form refused"); } + { OmegaRouterSpec s = { .warps = 8, .depth = 4, .ctas = 2, .mutant = 6 SPEC_SHAPE }; CHECK(omega_router_prog_stats(&s, NULL, NULL) != 0, "split with mutant 6 refused"); } CHECK(OMEGA_ROUTER_ARGS_WORDS <= OMEGA_GPU_SESSION_MAX_ARGS, "argument words fit the constant bank window"); } @@ -173,15 +190,26 @@ static void host_only(void) { static uint64_t g_sim_issues; /* one "device" block: x | xout | ids | wts | tab_gu | tab_dn | dir_gu | dir_dn | norm; the gate row block apart */ enum { B_X = 0, B_XOUT = 8192, B_IDS = 16384, B_WTS = 16448, B_TGU = 16512, B_TDN = 16672, B_DGU = 16832, B_DDN = B_DGU + NR * 16, - B_NORM = B_DDN + NR * 16, B_BYTES = B_NORM + 8192 }; + B_NORM = B_DDN + NR * 16, B_BYTES = B_NORM + 8192, + B_LOGIT = B_BYTES, /* the split form's logit array (L388): f32 [NR], just past the launch buffer's body */ + B_ALL = B_LOGIT + ((NR * 4u + 63u) & ~63u) }; +#define NSIMARGS 13u /* the argument pairs the constant bank carries: 0x380 .. 0x3e0 (index 12 = OMEGA_ROUTER_ARG_LOGIT) */ static void fake_dirs(uint64_t *dgu, uint64_t *ddn) { for (unsigned e = 0; e < NR; e++) { dgu[2 * e] = 0x100000000ull + e * 16; dgu[2 * e + 1] = 0x200000000ull + e * 16 + 8; ddn[2 * e] = 0x300000000ull + e * 32; ddn[2 * e + 1] = 0x400000000ull + e * 32 + 16; } } +/* the device addresses one "block" hands the kernel: the ten argument pairs of the one-launch form and (index 12) the + * split form's logit array (L388); index 10, 11 are unused words of the window */ +static void sim_args_of(uint64_t A[NSIMARGS], uint8_t *blk, const uint16_t *gate) { + memset(A, 0, sizeof(uint64_t) * NSIMARGS); + A[0] = (uintptr_t)(blk + B_X); A[1] = (uintptr_t)gate; A[2] = (uintptr_t)(blk + B_NORM); A[3] = (uintptr_t)(blk + B_XOUT); + A[4] = (uintptr_t)(blk + B_IDS); A[5] = (uintptr_t)(blk + B_WTS); A[6] = (uintptr_t)(blk + B_DGU); A[7] = (uintptr_t)(blk + B_DDN); + A[8] = (uintptr_t)(blk + B_TGU); A[9] = (uintptr_t)(blk + B_TDN); A[12] = (uintptr_t)(blk + B_LOGIT); +} static int sim_point(uint32_t depth, int mutant, int norm_topk, float out_scale, uint32_t seed, float shared_scale, char *msg, size_t n, int *bad, int *margin_case) { static BlackwellIRProgram prog; OmegaRouterSpec spec = { .warps = 8, .depth = depth, .mutant = mutant SPEC_SHAPE }; OmegaRouterSmem sm; if (omega_blackwell_codegen_router_prog(&spec, &prog) != 0 || omega_router_smem(&spec, &sm) != 0) { snprintf(msg, n, "codegen refused"); return -1; } Case c; case_make_shared(&c, seed, 1.0f, norm_topk, shared_scale); - uint8_t *blk = aligned_alloc(16, B_BYTES); memset(blk, 0, B_BYTES); + uint8_t *blk = aligned_alloc(16, B_ALL); memset(blk, 0, B_ALL); uint16_t *gate = aligned_alloc(16, (size_t)NR * H * 2); memcpy(gate, c.gate, (size_t)NR * H * 2); memcpy(blk + B_X, c.x, sizeof c.x); memcpy(blk + B_NORM, c.nw, sizeof c.nw); uint64_t dgu[NR * 2], ddn[NR * 2]; fake_dirs(dgu, ddn); @@ -195,9 +223,8 @@ static int sim_point(uint32_t depth, int mutant, int norm_topk, float out_scale, for (unsigned i = 0; i < H; i++) xo[i] = -999.0f; for (unsigned i = 0; i < TS; i++) { wd[i] = -999.0f; idd[i] = 0xffffffffu; } memset(blk + B_TGU, 0, B_DGU - B_TGU); - uint64_t A[10] = { (uintptr_t)(blk + B_X), (uintptr_t)gate, (uintptr_t)(blk + B_NORM), (uintptr_t)(blk + B_XOUT), (uintptr_t)(blk + B_IDS), - (uintptr_t)(blk + B_WTS), (uintptr_t)(blk + B_DGU), (uintptr_t)(blk + B_DDN), (uintptr_t)(blk + B_TGU), (uintptr_t)(blk + B_TDN) }; - for (unsigned i = 0; i < 10; i++) { s.cbank[(0x380 + 8 * i) / 4] = (uint32_t)A[i]; s.cbank[(0x380 + 8 * i) / 4 + 1] = (uint32_t)(A[i] >> 32); } + uint64_t A[NSIMARGS]; sim_args_of(A, blk, gate); + for (unsigned i = 0; i < NSIMARGS; i++) { s.cbank[(0x380 + 8 * i) / 4] = (uint32_t)A[i]; s.cbank[(0x380 + 8 * i) / 4 + 1] = (uint32_t)(A[i] >> 32); } float fe = EPS, fo = out_scale; memcpy(&s.cbank[OMEGA_ROUTER_ARG_EPS / 4], &fe, 4); memcpy(&s.cbank[OMEGA_ROUTER_ARG_OUT_SCALE / 4], &fo, 4); s.cbank[OMEGA_ROUTER_ARG_NORM_TOPK / 4] = norm_topk ? 1u : 0u; @@ -223,11 +250,12 @@ static int sim_point(uint32_t depth, int mutant, int norm_topk, float out_scale, * (stride B_BYTES). Every row's outputs must be bit-identical to the plain kernel run on that row alone, and both * fragment orders must agree. Mutant 6 (every CTA reads row 0's x) must be caught. */ #define XST_SIM (H * 4u + 64u) -static void sim_args(WsSim *s, BlackwellIRProgram *prog, const OmegaRouterSmem *sm, int order, const uint64_t A[10], int norm_topk, float out_scale) { - static uint8_t smem[16384]; +static uint8_t g_sim_smem[16384]; /* one CTA's shared memory, the last CTA's contents after a run (the harness reads it) */ +static void sim_args(WsSim *s, BlackwellIRProgram *prog, const OmegaRouterSmem *sm, int order, const uint64_t A[NSIMARGS], int norm_topk, float out_scale) { + uint8_t *smem = g_sim_smem; memset(s, 0, sizeof *s); s->p = prog; s->threads = 256; s->order = order; s->smem = smem; s->smem_bytes = sm->total; - for (unsigned i = 0; i < 10; i++) { s->cbank[(0x380 + 8 * i) / 4] = (uint32_t)A[i]; s->cbank[(0x380 + 8 * i) / 4 + 1] = (uint32_t)(A[i] >> 32); } + for (unsigned i = 0; i < NSIMARGS; i++) { s->cbank[(0x380 + 8 * i) / 4] = (uint32_t)A[i]; s->cbank[(0x380 + 8 * i) / 4 + 1] = (uint32_t)(A[i] >> 32); } float fe = EPS, fo = out_scale; memcpy(&s->cbank[OMEGA_ROUTER_ARG_EPS / 4], &fe, 4); memcpy(&s->cbank[OMEGA_ROUTER_ARG_OUT_SCALE / 4], &fo, 4); s->cbank[OMEGA_ROUTER_ARG_NORM_TOPK / 4] = norm_topk ? 1u : 0u; @@ -267,8 +295,7 @@ static int sim_rows_point(uint32_t depth, int mutant, uint32_t rows, uint32_t se uint64_t dgu[NR * 2], ddn[NR * 2]; fake_dirs(dgu, ddn); memcpy(blk + B_DGU, dgu, sizeof dgu); memcpy(blk + B_DDN, ddn, sizeof ddn); Out *o = calloc(rows, sizeof *o), *copy = calloc(rows, sizeof *copy); int err = 0; - uint64_t A[10] = { (uintptr_t)xs, (uintptr_t)gate, (uintptr_t)(blk + B_NORM), (uintptr_t)(blk + B_XOUT), (uintptr_t)(blk + B_IDS), - (uintptr_t)(blk + B_WTS), (uintptr_t)(blk + B_DGU), (uintptr_t)(blk + B_DDN), (uintptr_t)(blk + B_TGU), (uintptr_t)(blk + B_TDN) }; + uint64_t A[NSIMARGS]; sim_args_of(A, blk, gate); A[0] = (uintptr_t)xs; /* the rows form's x: NR rows, stride XST_SIM */ for (int order = 0; order < 2 && !err; order++) { WsSim s; sim_args(&s, &prog, &sm, order, A, 1, 1.0f); s.nranges = 3; @@ -311,6 +338,73 @@ static int sim_rows_point(uint32_t depth, int mutant, uint32_t rows, uint32_t se free(blk); free(xs); free(gate); free(o); free(copy); case_free(&c[0]); free(c); return err; } +/* The split form (L388): the logits kernel (grid ctas, W warps; warp cta * W + w owns gate row cta * W + w, then + * += ctas * W) writes the logits, the select kernel (grid 1) runs phase 3 and the epilogue on them. The pair must give the + * one-launch kernel's outputs bit for bit (ids, weights, x', both tables) on the same device block, and the one-launch + * kernel's mutants must still be caught through the pair. */ +static int sim_split_point(uint32_t warps, uint32_t ctas, uint32_t depth, int mutant, int norm_topk, float out_scale, uint32_t seed, + char *msg, size_t n, int *bad, int *margin_case) { + static BlackwellIRProgram plain, lg, sel; + OmegaRouterSpec ps = { .warps = warps, .depth = depth, .mutant = mutant SPEC_SHAPE }; + OmegaRouterSpec a = ps; a.ctas = ctas; + OmegaRouterSpec b = ps; b.select = 1; + OmegaRouterSmem sm; + *bad = 0; *margin_case = 0; + if (omega_router_smem(&ps, &sm) != 0 || omega_blackwell_codegen_router_prog(&ps, &plain) != 0 || + omega_blackwell_codegen_router_prog(&a, &lg) != 0 || omega_blackwell_codegen_router_prog(&b, &sel) != 0) { snprintf(msg, n, "codegen refused"); return -1; } + Case c; case_make(&c, seed, 1.0f, norm_topk); + uint8_t *blk = aligned_alloc(16, B_ALL); memset(blk, 0, B_ALL); + uint16_t *gate = aligned_alloc(16, (size_t)NR * H * 2); memcpy(gate, c.gate, (size_t)NR * H * 2); + memcpy(blk + B_X, c.x, sizeof c.x); memcpy(blk + B_NORM, c.nw, sizeof c.nw); + uint64_t dgu[NR * 2], ddn[NR * 2]; fake_dirs(dgu, ddn); + memcpy(blk + B_DGU, dgu, sizeof dgu); memcpy(blk + B_DDN, ddn, sizeof ddn); + uint64_t A[NSIMARGS]; sim_args_of(A, blk, gate); + Out p, s; memset(&p, 0, sizeof p); memset(&s, 0, sizeof s); + int err = 0, same = 0; + { /* the one-launch kernel on its own */ + WsSim w; sim_args(&w, &plain, &sm, 0, A, norm_topk, out_scale); + w.threads = 32u * warps; + w.nranges = 2; + w.ranges[0].lo = (uintptr_t)blk; w.ranges[0].hi = (uintptr_t)blk + B_ALL; + w.ranges[1].lo = (uintptr_t)gate; w.ranges[1].hi = (uintptr_t)gate + (size_t)NR * H * 2; + poison_block(blk); + err = ws_run(&w, 1, 1); + if (err) snprintf(msg, n, "plain: %s (pc %d)", w.msg, w.err_pc); + else { g_sim_issues += w.warp_issues; if (block_out(blk, &p)) { err = -1; snprintf(msg, n, "plain: output words never written"); } } + } + if (!err) { /* the pair, with the two fragment orders of the select kernel */ + poison_block(blk); + WsSim w; sim_args(&w, &lg, &sm, 0, A, norm_topk, out_scale); + w.threads = 32u * warps; + w.nranges = 2; + w.ranges[0].lo = (uintptr_t)blk; w.ranges[0].hi = (uintptr_t)blk + B_ALL; + w.ranges[1].lo = (uintptr_t)gate; w.ranges[1].hi = (uintptr_t)gate + (size_t)NR * H * 2; + err = ws_run(&w, ctas, 1); + if (err) snprintf(msg, n, "logits (%u CTAs): %s (pc %d)", ctas, w.msg, w.err_pc); + else g_sim_issues += w.warp_issues; + } + for (int order = 0; order < 2 && !err; order++) { + WsSim w; sim_args(&w, &sel, &sm, order, A, norm_topk, out_scale); + w.threads = 32u * warps; + w.nranges = 2; + w.ranges[0].lo = (uintptr_t)blk; w.ranges[0].hi = (uintptr_t)blk + B_ALL; + w.ranges[1].lo = (uintptr_t)gate; w.ranges[1].hi = (uintptr_t)gate + (size_t)NR * H * 2; + err = ws_run(&w, 1, 1); + if (err) { snprintf(msg, n, "select (order %d): %s (pc %d)", order, w.msg, w.err_pc); break; } + g_sim_issues += w.warp_issues; + Out o; if (block_out(blk, &o)) { err = -1; snprintf(msg, n, "select: output words never written"); break; } + if (order == 0) s = o; + else if (memcmp(&s, &o, sizeof s) != 0) { err = -1; snprintf(msg, n, "the fragment order changed the pair's result"); } + } + if (!err) { + same = memcmp(&p, &s, sizeof p) == 0; + int orc = compare(&c, &s, out_scale, dgu, ddn, msg, n, margin_case); + *bad = orc | (same ? 0 : 32); + if (!same && !orc) snprintf(msg, n, "the pair differs from the one-launch kernel (ids %s)", memcmp(p.ids, s.ids, sizeof p.ids) ? "DIFFER" : "equal"); + } + free(blk); free(gate); case_free(&c); + return err; +} static void sim_battery(void) { struct { uint32_t depth; int norm_topk; float oscale; uint32_t seed; const char *name; } pts[] = { { 8, 1, 1.0f, 11, "depth 8 norm" }, { 8, 1, 1.0f, 12, "depth 8 norm seed 12" }, { 8, 0, 1.0f, 13, "depth 8 no-norm" }, @@ -347,11 +441,34 @@ static void sim_battery(void) { CHECK(!err && bad != 0, "sim rows mutant 6 must be caught: %s", msg); printf("sim rows mutant 6: %s (bad mask %d) %s\n", err ? "ERROR" : bad ? "caught" : "NOT CAUGHT", bad, msg); } + { /* L388 split form: the pair (logits launch of ctas CTAs + select launch) equals the one-launch kernel bit for bit */ + struct { uint32_t warps, depth, seed; int norm_topk; float oscale; const char *name; } sp[] = { + { 32, 4, 41, 1, 1.0f, "split w32 d4 norm" }, + { 32, 4, 42, 0, 1.0f, "split w32 d4 no-norm" }, + { 8, 4, 43, 1, 18446744073709551616.0f, "split w8 d4 norm out_scale 2^64" }, + { 32, 8, 44, 1, 1.0f, "split w32 d8 norm" }, + }; + for (size_t i = 0; i < sizeof sp / sizeof sp[0]; i++) { + char msg[300]; int bad = 0, mc = 0; + const uint32_t ctas = (NR + sp[i].warps - 1u) / sp[i].warps; + int err = sim_split_point(sp[i].warps, ctas, sp[i].depth, 0, sp[i].norm_topk, sp[i].oscale, sp[i].seed, msg, sizeof msg, &bad, &mc); + CHECK(!err && bad == 0, "sim %s (%u CTAs of %u warps): %s", sp[i].name, ctas, sp[i].warps, msg); + printf("sim %s (%u CTAs of %u warps): %s %s\n", sp[i].name, ctas, sp[i].warps, err ? "ERROR" : bad ? "MISMATCH" : "ok", msg); + } + for (int m = 1; m <= MUT_MAX; m++) { /* a mutant must still be caught through the pair */ + char msg[300]; int bad = 0, mc = 0; + const uint32_t ctas = (NR + 8u - 1u) / 8u; + int err = sim_split_point(8, ctas, 4, m, 1, 1.0f, 51, msg, sizeof msg, &bad, &mc); + CHECK(!err && bad != 0, "sim split mutant %d must be caught: %s", m, msg); + printf("sim split mutant %d: %s (bad mask %d) %s\n", m, err ? "ERROR" : bad ? "caught" : "NOT CAUGHT", bad, msg); + } + } printf("sim: %" PRIu64 " warp issues in total\n", g_sim_issues); } /* ------------------------------------------------------------- chip */ static int g_evict = 1; +static int g_split; /* L388: run the chip battery through the split form (two launches) instead of the one-launch form */ static void evict_host_caches(void) { static uint8_t *buf; const size_t sz = 512u << 20; static uint8_t pass; if (!buf) buf = malloc(sz); @@ -374,6 +491,14 @@ static int chip_run(const char *out_path, uint32_t reps) { printf("router alloc (2 layers): rc=%s %.1f ms (%s)\n", omega_gpu_router_rc_name(rc), now_ms() - t0, rc ? omega_gpu_router_last_error() : ""); CHECK(rc == OMEGA_GPU_ROUTER_OK, "router alloc"); if (rc) { if (out) fprintf(out, "],\"verdict\":\"FAIL\"}\n"); return 1; } + if (g_split) { /* L388: every call of this run records the split pair (the same outputs, two launches) */ + const uint32_t ctas = (omega_gpu_router_rows(r) + OMEGA_GPU_ROUTER_WARPS - 1u) / OMEGA_GPU_ROUTER_WARPS; + rc = omega_gpu_router_set_split(r, ctas); + printf("split form: %u CTAs of %u warps (rc %s)\n", ctas, OMEGA_GPU_ROUTER_WARPS, omega_gpu_router_rc_name(rc)); + CHECK(rc == OMEGA_GPU_ROUTER_OK, "set split"); + if (rc) { if (out) fprintf(out, "],\"verdict\":\"FAIL\"}\n"); return 1; } + if (out) fprintf(out, "{\"split_ctas\":%u},", ctas); + } uint64_t dgu[NR * 2], ddn[NR * 2]; fake_dirs(dgu, ddn); CHECK(omega_gpu_router_set_directory(r, 0, dgu, ddn) == OMEGA_GPU_ROUTER_OK, "set directory layer 0"); int first = 1, parity_fail = 0, margin_cases = 0; @@ -509,6 +634,7 @@ int main(int argc, char **argv) { else if (strcmp(argv[i], "--depth") == 0 && i + 1 < argc) depth = (uint32_t)atoi(argv[++i]); else if (strcmp(argv[i], "--spin-us") == 0 && i + 1 < argc) spin = (uint32_t)atoi(argv[++i]); else if (strcmp(argv[i], "--no-evict") == 0) g_evict = 0; + else if (strcmp(argv[i], "--split") == 0) g_split = 1; else { printf("unknown argument %s\n", argv[i]); return 2; } } if (host) host_only(); diff --git a/tests/q36_codegen_hash.c b/tests/q36_codegen_hash.c index a38276f56..52a412211 100644 --- a/tests/q36_codegen_hash.c +++ b/tests/q36_codegen_hash.c @@ -68,6 +68,19 @@ int main(void) { snprintf(n, sizeof n, "router256s.rows.w%u.d4.m%d", warps[w], m); omega_bw_ir_init(prog); emit(n, omega_blackwell_codegen_router_prog(&s, prog)); } + /* # new (sc L388, AIEN_OMEGA_Q36_ROUTER_FUSE): the split form at the 35B shape -- the logits kernel (grid ctas) and the + * select kernel (grid 1); the one-launch shapes above must stay byte-identical */ + printf("# new\n"); + for (int w = 0; w < 3; w++) + for (uint32_t d = 1; d <= 4; d *= 2) { + const uint32_t ctas = (256u + 1u + 32u * warps[w] - 1u) / (32u * warps[w]); + OmegaRouterSpec a = {.warps = warps[w], .depth = d, .experts = 256, .shared = 1, .ctas = ctas}; + OmegaRouterSpec b = {.warps = warps[w], .depth = d, .experts = 256, .shared = 1, .select = 1}; + snprintf(n, sizeof n, "router256s.split.logits.w%u.d%u", warps[w], d); omega_bw_ir_init(prog); + emit(n, omega_blackwell_codegen_router_prog(&a, prog)); + snprintf(n, sizeof n, "router256s.split.select.w%u.d%u", warps[w], d); omega_bw_ir_init(prog); + emit(n, omega_blackwell_codegen_router_prog(&b, prog)); + } /* the decode glue kernels (RES / SWIGLU / ARGMAX / GATHER / GATE / AXPY) at the widths the 30B and 27B graphs use */ const uint32_t widths[3] = {0, 5120, 17408}, strides[3] = {0, 256, 65536}; for (int k = 1; k <= 6; k++) From 6ec443bcde989cc54764c2c23c90a43cee50001d Mon Sep 17 00:00:00 2001 From: Drake Stapleton Date: Thu, 29 Oct 2026 15:37:40 -0500 Subject: [PATCH 2/2] fix: refresh stale crumbs so AIEN Checks can pass crumb verify reported 5 stale (CI). crumb compile only; no source change. DCO pending Drake. --- .crumb | 32 ++++++++++++++++---------------- mk/.crumb | 8 ++++---- src/.crumb | 8 ++++---- tests/.crumb | 8 ++++---- tools/.crumb | 10 +++++----- 5 files changed, 33 insertions(+), 33 deletions(-) diff --git a/.crumb b/.crumb index cb3d904f2..56cb49e00 100644 --- a/.crumb +++ b/.crumb @@ -59,8 +59,8 @@ "purpose": "Directory doctrine. Purpose not yet described by a human." }, { - "digest": "4c711384133e933fc175aab7d48daae8b597767d17eaa17464d560c758a687b7", - "last_commit": "e9dcfafb7", + "digest": "d8e540575553c9c3f243fd496cdfc03d65841d1a2c2d05005b8678489feba859", + "last_commit": "39d07c91a", "name": "mk", "purpose": "Directory mk. Purpose not yet described by a human." }, @@ -77,33 +77,33 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "8ab10b555fb095ec6fc706f96f7294d2b99ce62438600a493fe235ec11bcd0d6", - "last_commit": "2f44aa3b6", + "digest": "cd2bbc553bd6a3922f0e01822db95d2189d68c3d0cb8f1e917d597c1fadfbf8c", + "last_commit": "2c514b1f0", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "cc52017187608f4ee94d1480602ff214c614604c515bbabbde448c2733e555ab", - "last_commit": "e9dcfafb7", + "digest": "855693b7bd70e8054e35c49b3f572697344f212bfe8be551ea6a6b5bfc5d8d7b", + "last_commit": "2c514b1f0", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, { - "digest": "dbe7b08900fc053b23c993a25a3fe744e3874be2f23ee24f20e183797db53c27", - "last_commit": "e9dcfafb7", + "digest": "c86cd0893d1f95dee1febdf19efe3ae5a2fcc2ec2b619f9a876a4a28b80bf548", + "last_commit": "39d07c91a", "name": "tools", "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:22c5443cb1d244ce3757eeaf7b6be6dd6ae8da548460b2f9f7cce346fe627d2d", - "compiled_at": "2026-10-24T23:55:01Z", + "children_root": "sha256:026c0b70b650e90a39ea7f3e48814baa28659d4561bf99d61fa93e66eff71b44", + "compiled_at": "2026-10-29T20:37:38Z", "compiler": "crumb-compile/1", - "digest": "sha256:120d70c3009b2973340b89b2394656968f41de2eebd0dc31756a071315ceb358", - "evidence_files": 11663, - "evidence_root": "sha256:9a183185ae8671e6263ec276a9bac3332e357843bcfd0e071b2bd14a3a75efc0", - "files": 15080, - "generated_at_commit": "4cd9ad9845612038a49c15940811ce24c38c061d", - "source_tree": "sha256:14caf26ffe8d06a0538594b63e92e61957aa06455feef677dcb25e05224fc464" + "digest": "sha256:3b77255c4820e03fa34753d5af790239a6ace5fdf11d4b73770d50dc890e453c", + "evidence_files": 11666, + "evidence_root": "sha256:91249b110c57333badc93f4052f27ad9faba72fbbac89de4d1512a9ec8808721", + "files": 14988, + "generated_at_commit": "2c514b1f0cd8229347c4f1362e1b51e109ab9882", + "source_tree": "sha256:6e76cfb46eff538a481a8208274587bd2ff6d45a300f81c626d9d59ab3481c72" }, "owner_lane": "", "provenance": { diff --git a/mk/.crumb b/mk/.crumb index 79c40b88a..0e8ba2b91 100644 --- a/mk/.crumb +++ b/mk/.crumb @@ -7,14 +7,14 @@ "generated": { "children": [], "children_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "compiled_at": "2026-10-24T23:51:52Z", + "compiled_at": "2026-10-29T20:37:38Z", "compiler": "crumb-compile/1", - "digest": "sha256:4c711384133e933fc175aab7d48daae8b597767d17eaa17464d560c758a687b7", + "digest": "sha256:d8e540575553c9c3f243fd496cdfc03d65841d1a2c2d05005b8678489feba859", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 74, - "generated_at_commit": "c8d7d52946c22a7a8a730897dfff9e1cd9ad069a", - "source_tree": "sha256:035bce1bfdbd9e88ae98df0849e50d7bc64ab72563b896c9f506dbb9dd61eeca" + "generated_at_commit": "2c514b1f0cd8229347c4f1362e1b51e109ab9882", + "source_tree": "sha256:9cdcd24234f4b0247a8311a01b94b1c9ce42e5b2e619cfc268a7c09ff1c9d237" }, "owner_lane": "", "provenance": { diff --git a/src/.crumb b/src/.crumb index 76700a054..de274ee93 100644 --- a/src/.crumb +++ b/src/.crumb @@ -188,14 +188,14 @@ } ], "children_root": "sha256:4460b4a6d5ed4054d462af660a6a02691350a35a8f44197e60ce605e243f698e", - "compiled_at": "2026-10-24T23:55:01Z", + "compiled_at": "2026-10-29T20:37:38Z", "compiler": "crumb-compile/1", - "digest": "sha256:8ab10b555fb095ec6fc706f96f7294d2b99ce62438600a493fe235ec11bcd0d6", + "digest": "sha256:cd2bbc553bd6a3922f0e01822db95d2189d68c3d0cb8f1e917d597c1fadfbf8c", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 565, - "generated_at_commit": "4cd9ad9845612038a49c15940811ce24c38c061d", - "source_tree": "sha256:093f3a2aba4e74fd82076e3921f29b215d8fe508982a525f3c5b487c950e64bd" + "generated_at_commit": "2c514b1f0cd8229347c4f1362e1b51e109ab9882", + "source_tree": "sha256:a2e5307bded3f4f9dfa70099ce5d11d5ad4aa7c764e140808b4f6bd3f9ece51c" }, "owner_lane": "", "provenance": { diff --git a/tests/.crumb b/tests/.crumb index b2f41a3f8..dc1558a4e 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -302,14 +302,14 @@ } ], "children_root": "sha256:21fc25ed991f4066326a9270e13961ed8a45edc12064ce9e7846ab6782d5b72b", - "compiled_at": "2026-10-24T23:55:01Z", + "compiled_at": "2026-10-29T20:37:38Z", "compiler": "crumb-compile/1", - "digest": "sha256:cc52017187608f4ee94d1480602ff214c614604c515bbabbde448c2733e555ab", + "digest": "sha256:855693b7bd70e8054e35c49b3f572697344f212bfe8be551ea6a6b5bfc5d8d7b", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", "files": 1292, - "generated_at_commit": "4cd9ad9845612038a49c15940811ce24c38c061d", - "source_tree": "sha256:aa47c530df8df05215fc7536fe21612bfa187896b8f02e9219b525dc477ea8c3" + "generated_at_commit": "2c514b1f0cd8229347c4f1362e1b51e109ab9882", + "source_tree": "sha256:9f6146f58c678530943652b2aec8a0a97ad31d93227330c06c384510a215b598" }, "owner_lane": "", "provenance": { diff --git a/tools/.crumb b/tools/.crumb index b7ed2e98e..83a62208c 100644 --- a/tools/.crumb +++ b/tools/.crumb @@ -62,14 +62,14 @@ } ], "children_root": "sha256:8d3d3b952374b3b72e667521f07bf028c4c02bcefff772ad1a71cfff3e5266f2", - "compiled_at": "2026-10-24T23:52:12Z", + "compiled_at": "2026-10-29T20:37:38Z", "compiler": "crumb-compile/1", - "digest": "sha256:dbe7b08900fc053b23c993a25a3fe744e3874be2f23ee24f20e183797db53c27", + "digest": "sha256:c86cd0893d1f95dee1febdf19efe3ae5a2fcc2ec2b619f9a876a4a28b80bf548", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", - "files": 294, - "generated_at_commit": "51f4d9c65856420ea00459e8054b5869951b081e", - "source_tree": "sha256:a37bc026768d74bbf861e4cf26811f03a22efe9b5fb0ae28d646aedd9e5c9477" + "files": 295, + "generated_at_commit": "2c514b1f0cd8229347c4f1362e1b51e109ab9882", + "source_tree": "sha256:17d77462e54c4f104cf89c75c779be09f7bb83a18e3f436918e06147f9022d79" }, "owner_lane": "", "provenance": {