diff --git a/.crumb b/.crumb index cc616f81a..cd5b7dd7b 100644 --- a/.crumb +++ b/.crumb @@ -87,14 +87,14 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "74dd1cf62bb487f5c421f1e4290e0d5a90e49dc7a92076549721f7f9713efbb8", - "last_commit": "1e6dfa90", + "digest": "922c70bc1e6025d8bdba6aa57147f41d0aff3e4303bc5c396e80c0d576fae62d", + "last_commit": "03e4e6a8", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "e59264059051e10ee92dea5caafb09147f1c0c41b6a9eb81427cc6174def0a99", - "last_commit": "1e6dfa90", + "digest": "d4b26789b662d538601b2dacee7e99667ef84c9d12a57dbeee3b894465bb449e", + "last_commit": "03e4e6a8", "name": "tests", "purpose": "Directory tests. Purpose not yet described by a human." }, @@ -105,15 +105,15 @@ "purpose": "Directory tools. Purpose not yet described by a human." } ], - "children_root": "sha256:a953af5fdd1a4235e4d8bfcb2b54f033acef97313676e86434cee60f5c9c5ad9", - "compiled_at": "2026-10-12T06:04:50Z", + "children_root": "sha256:6232137ddc358962d5043d7aa6545a11f6303026e0eb2925f40c965d6f28d172", + "compiled_at": "2026-10-12T06:51:27Z", "compiler": "crumb-compile/1", - "digest": "sha256:61b5e122ea6b024ad7fd6435a5222deca840eba9c8166f5489340ed94f786f8c", + "digest": "sha256:b8c39a9e1a051ae8db34dc8f3e6e74d62b8935603f2473b65b0e7d8982f759aa", "evidence_files": 7245, "evidence_root": "sha256:035beef5cdb99afaefd1743640f5e7eeaaed13c4a5ec6c8b4b6a4cb0c528407a", "files": 9732, - "generated_at_commit": "1e6dfa909f2ea951f99597dd68bb89fd62ac2291", - "source_tree": "sha256:7e228749e3ccd7483a117d513a98994675b2c49289228e05d04f684c6f8c53b4" + "generated_at_commit": "03e4e6a844248a735e135c4ac2d9da5b064b7680", + "source_tree": "sha256:6c0e9e7ff6d2d82743fc6c5e6b3107eb7a92fdcf7f31ea8f8464d6b96d1b357f" }, "owner_lane": "", "provenance": { diff --git a/src/.crumb b/src/.crumb index 2dae93978..ad44c5e3d 100644 --- a/src/.crumb +++ b/src/.crumb @@ -176,14 +176,14 @@ } ], "children_root": "sha256:ce7e4b528bec54e15d5f9525026d958e6eab78b88614c65b8e7c51d44c0243af", - "compiled_at": "2026-10-12T06:04:50Z", + "compiled_at": "2026-10-12T06:51:27Z", "compiler": "crumb-compile/1", - "digest": "sha256:74dd1cf62bb487f5c421f1e4290e0d5a90e49dc7a92076549721f7f9713efbb8", + "digest": "sha256:922c70bc1e6025d8bdba6aa57147f41d0aff3e4303bc5c396e80c0d576fae62d", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 451, - "generated_at_commit": "1e6dfa909f2ea951f99597dd68bb89fd62ac2291", - "source_tree": "sha256:21b0010475caae87009c904b3a3156df6c586c7c1bca28c061af049f02eb1cbe" + "generated_at_commit": "03e4e6a844248a735e135c4ac2d9da5b064b7680", + "source_tree": "sha256:b0f6d99356474d7426edd89658e7ddcf5c6f9624ab4a059d56915477cf3780c3" }, "owner_lane": "", "provenance": { diff --git a/src/omega_blackwell_codegen.c b/src/omega_blackwell_codegen.c index 162b23c31..081f7db8b 100644 --- a/src/omega_blackwell_codegen.c +++ b/src/omega_blackwell_codegen.c @@ -379,13 +379,18 @@ static int encode_single_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc * w[3] = insn->control ? insn->control : 0x000f2200; break; - case BW_IR_STG_E: - /* STG.E desc[URd][Ra.64], Rb */ + case BW_IR_STG_E: { + /* STG.E desc[URd][Ra.64+imm], Rb. imm = signed 24-bit byte offset at bits 40..63 (w[1] bits + * 8..31), as for LDG; words from nvcc 13.0.88 -arch=sm_121 (omega_blackwell_verify_codegen_fixtures_fragmm, + * 2026-10-12: STG.E desc[UR4][R4.64+0x4], R9 = 0x0000040904007986 0x000fe8000c101904). */ + int32_t off = (int32_t)insn->imm; + if (off < -(1 << 23) || off >= (1 << 23)) return -1; w[0] = 0x7986 | ((uint32_t)(src1 & 0xff) << 24); - w[1] = (uint32_t)(src2 & 0xff); + w[1] = (uint32_t)(src2 & 0xff) | (((uint32_t)off & 0xffffffU) << 8); w[2] = 0x0c101900 | (uint32_t)(ureg & 0xff); w[3] = insn->control ? insn->control : 0x000fe200; break; + } case BW_IR_STG_EF: /* STG.E.MMIO.GPU. Uncached store, meant to be visible without a fence. */ @@ -877,6 +882,13 @@ int omega_blackwell_verify_codegen_fixtures_fragmm(void) { { BW_IR_LDS128, 4, 20, -1, -1, -1, 0, 0, 0x14047984U, 0x00000000U, 0x00000c00U, 0, "LDS.128 R4, [R20]" }, { BW_IR_LDS128, 8, 20, -1, -1, -1, 0x40, 0, 0x14087984U, 0x00004000U, 0x00000c00U, 0, "LDS.128 R8, [R20+0x40]" }, { BW_IR_LDS128, 12, 20, -1, -1, -1, 0x100, 0, 0x140c7984U, 0x00010000U, 0x00000c00U, 0, "LDS.128 R12, [R20+0x100]" }, + /* STG.E with an immediate offset (blocked prefill matmul, 2026-10-12): nvcc 13.0.88 -arch=sm_121 on float stores + * at register + constant offsets (~/.claude/jobs/ade3c837/tmp/stg-oracle/stg.cu); the second word is the + * offset-free STG word with UR4 */ + { BW_IR_STG_E, -1, 4, 8, -1, 4, 0, 0, 0x04007986U, 0x00000008U, 0x0c101904U, 0, "STG.E desc[UR4][R4.64], R8" }, + { BW_IR_STG_E, -1, 4, 9, -1, 4, 0x4, 0, 0x04007986U, 0x00000409U, 0x0c101904U, 0, "STG.E desc[UR4][R4.64+0x4], R9" }, + { BW_IR_STG_E, -1, 4, 10, -1, 4, 0x4000, 0, 0x04007986U, 0x0040000aU, 0x0c101904U, 0, "STG.E desc[UR4][R4.64+0x4000], R10" }, + { BW_IR_STG_E, -1, 6, 11, -1, 4, 0xfff00000u, 0, 0x06007986U, 0xf000000bU, 0x0c101904U, 0, "STG.E desc[UR4][R6.64+-0x100000], R11" }, }; for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { BlackwellIRInsn in; @@ -1893,6 +1905,213 @@ int omega_blackwell_codegen_matmul_frag(const OmegaMatMulSpec *spec, uint32_t gr * loads on SB2 and again with A on SB2 and B on SB4 (2026-10-07 07:50Z and 07:54Z, omega #328), * while unroll 4 (48 registers) ran every Qwen3 decode shape correctly. Cause UNKNOWN; the next * experiment is the unroll-4 kernel launched with 72 declared registers (one variable). */ +/* ---- 2026-10-12: row/column-blocked fragment kernel (prefill rows) ------------------------ + * + * The kernel above gives one warp one 16x8 output tile: every B fragment is streamed once per row + * tile and every A fragment once per column tile, so at m = 128 on GB10 the three Qwen3-8B prefill + * shapes move about 4.7 GB per call at the DRAM rate (128x4096x24576: 20.9 ms, 2026-10-12 baseline). + * Here one warp owns RT row tiles x CT column tiles (RT*CT accumulators) over all of K: per K step + * it loads RT A fragments and CT B fragments and runs RT*CT HMMAs, so each loaded fragment feeds + * RT (B) or CT (A) tensor ops. Same packing, grid rules and C layout as the kernel above: the A + * fragments of row tile i sit at +i*KS*512 bytes, the B fragments of column tile j at +j*KS*256, + * both immediate offsets of the same cursors; the C tile (i, j) is at +i*16*N*4 + j*32 bytes from + * the two row pointers (STG.E immediate offsets, fixtures in omega_blackwell_verify_codegen_fixtures_fragmm). + * Loads per pass stay at or under 8 (unroll*(RT+CT)), the count the proven unroll-4 kernel issues; + * unroll 8 of that kernel (16 loads per pass) never completed on the GB10 and the cause is UNKNOWN, + * so this kernel does not go past that envelope. The HMMA chain keeps the dependent-chain control + * words (stall 15 then NOP stall 14) although the RT*CT accumulators are independent: a longer stall + * is never wrong, and the loads dominate the pass. CTA = one warp, as above; a grid of + * (grid_x, row blocks); column blocks advance by grid_x per pass of the column loop. */ +int omega_blackwell_codegen_matmul_frag_block_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, + uint32_t rt, uint32_t ct, int mutant, BlackwellIRProgram *prog) { + if (!spec || !prog) return -1; + if (spec->m == 0 || spec->k == 0 || spec->n == 0) return -1; + if (spec->precision != OMEGA_MATMUL_PRECISION_FP16 && spec->precision != OMEGA_MATMUL_PRECISION_BF16) return -1; + if (spec->m % 16 != 0 || spec->n % 8 != 0 || spec->k % 16 != 0) return -1; + if (rt != 1 && rt != 2 && rt != 4 && rt != 8) return -1; + if (ct != 1 && ct != 2 && ct != 4) return -1; + if (rt * ct > 16 || rt * ct < 2) return -1; /* 1x1 is the kernel above */ + if (unroll != 1 && unroll != 2) return -1; + if (unroll * (rt + ct) > 8) return -1; + const uint32_t K = spec->k, N = spec->n, NT = N / 8, KS = K / 16; + if (KS % unroll != 0 || NT % ct != 0) return -1; + const uint32_t ITERS = KS / unroll, NB = NT / ct; + if (grid_x == 0 || grid_x > NB) return -1; + if (mutant && ITERS < 2) return -1; + /* every index and immediate below must fit: 32 bits for the index arithmetic, signed 24 bits for + * the load/store offsets (rows up to OMEGA_BW_MATMUL_MAX_M) */ + if ((uint64_t)NT * KS * 32u > 0xffffffffull || (uint64_t)(OMEGA_BW_MATMUL_MAX_M / 16u) * KS * 32u > 0xffffffffull || + (uint64_t)grid_x * ct * KS * 256u > 0xffffffffull || (uint64_t)N * 32u > 0xffffffffull || + (uint64_t)OMEGA_BW_MATMUL_MAX_M * N > 0xffffffffull) + return -1; + if ((uint64_t)(rt - 1) * KS * 512u + (unroll - 1) * 512u >= (1ull << 23) || + (uint64_t)(ct - 1) * KS * 256u + (unroll - 1) * 256u >= (1ull << 23) || + (uint64_t)(rt - 1) * 16u * N * 4u + (ct - 1) * 32u + 4u >= (1ull << 23)) + return -1; + + omega_bw_ir_init(prog); + int v_ptr_a = omega_bw_ir_alloc_vreg64(prog), v_ptr_b = omega_bw_ir_alloc_vreg64(prog), v_ptr_c = omega_bw_ir_alloc_vreg64(prog); + int uv_desc = omega_bw_ir_alloc_uvreg64(prog); + int v_tid = omega_bw_ir_alloc_vreg(prog), v_ctaid_x = omega_bw_ir_alloc_vreg(prog), v_ctaid_y = omega_bw_ir_alloc_vreg(prog); + int v_group = omega_bw_ir_alloc_vreg(prog), v_tig = omega_bw_ir_alloc_vreg(prog), v_tcol2 = omega_bw_ir_alloc_vreg(prog); + int v_r0 = omega_bw_ir_alloc_vreg(prog), v_r1 = omega_bw_ir_alloc_vreg(prog), v_c1 = omega_bw_ir_alloc_vreg(prog); + int v_idx = omega_bw_ir_alloc_vreg(prog), v_col = omega_bw_ir_alloc_vreg(prog), v_cnt = omega_bw_ir_alloc_vreg(prog); + int v_pa = omega_bw_ir_alloc_vreg64(prog), v_pbcol = omega_bw_ir_alloc_vreg64(prog); + int v_pc0 = omega_bw_ir_alloc_vreg64(prog), v_pc1 = omega_bw_ir_alloc_vreg64(prog); + int v_pac = omega_bw_ir_alloc_vreg64(prog), v_pbc = omega_bw_ir_alloc_vreg64(prog); + int v_rd[16], v_ra[2][8], v_rb[2][4]; + for (uint32_t t = 0; t < rt * ct; t++) v_rd[t] = omega_bw_ir_alloc_vreg128(prog); + for (uint32_t u = 0; u < unroll; u++) { + for (uint32_t i = 0; i < rt; i++) v_ra[u][i] = omega_bw_ir_alloc_vreg128(prog); + for (uint32_t j = 0; j < ct; j++) v_rb[u][j] = omega_bw_ir_alloc_vreg64(prog); + } + if (v_rb[unroll - 1][ct - 1] < 0 || v_ra[unroll - 1][rt - 1] < 0 || v_rd[rt * ct - 1] < 0 || uv_desc < 0) return -1; + + const uint32_t CTRL_STG_RDBAR3 = 0x0007e200u, CTRL_IMADW_WAIT3 = 0x009fcc00u, CTRL_MOVRZ_WAIT3 = 0x008fe200u; + + /* prologue: descriptor, pointers, ids (as the kernel above; a CTA's row block is rt row tiles) */ + BW_APPEND(prog, .op = BW_IR_LDCU64, .dst_vreg = uv_desc, .imm = 0x358, .is_uniform = true); + BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_a, .imm = 0x380); + BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_b, .imm = 0x388); + BW_APPEND(prog, .op = BW_IR_LDC64, .dst_vreg = v_ptr_c, .imm = 0x390); + BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_tid, .imm = BW_SR_TID_X); + BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_x, .imm = BW_SR_CTAID_X); + BW_APPEND(prog, .op = BW_IR_S2R, .dst_vreg = v_ctaid_y, .imm = BW_SR_CTAID_Y); + BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_c1, .imm = 1); + BW_APPEND(prog, .op = BW_IR_SHF_R, .dst_vreg = v_group, .src1_vreg = v_tid, .imm = 2); + BW_APPEND(prog, .op = BW_IR_LOP3_AND, .dst_vreg = v_tig, .src1_vreg = v_tid, .imm = 3); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_tcol2, .src1_vreg = v_tig, .src2_vreg = -1, .imm = 2, .src3_vreg = -1); + /* r0 = ctaid_y * 16 * rt + group: row of this lane in row tile 0 of the block; r1 = r0 + 8 */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r0, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16u * rt, .src3_vreg = v_group); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 8, .src3_vreg = v_r0); + /* pa = A + (ctaid_y * rt * KS * 32 + tid) * 16: row tile 0 of this block, this lane's 16 bytes of step 0 */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = rt * KS * 32u, .src3_vreg = v_tid); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pa, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 16, .src3_vreg = v_ptr_a); + /* col = ctaid_x * ct: first column tile of this CTA's first column block */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = ct, .src3_vreg = -1); + /* pbcol = B + (col * KS * 32 + tid) * 8: column tile col, this lane's 8 bytes of step 0 */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = KS * 32u, .src3_vreg = v_tid); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 8, .src3_vreg = v_ptr_b); + /* pc0 = C + (r0*N + col*8 + tcol2)*4 ; pc1 with r1 */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r0, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_r1, .src2_vreg = -1, .imm = N, .src3_vreg = v_tcol2); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_idx, .src1_vreg = v_col, .src2_vreg = -1, .imm = 8, .src3_vreg = v_idx); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_idx, .src2_vreg = -1, .imm = 4, .src3_vreg = v_ptr_c); + + /* ---- column-block loop: zero the rt*ct accumulators (the first MOV waits SB3: the previous + * block's stores have read them), rewind the K cursors ---- */ + const int L_col = (int)prog->count; + for (uint32_t t = 0; t < rt * ct; t++) + for (uint32_t s = 0; s < 4; s++) + BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd[t], .dst_subreg = (int)s, + .control = (t == 0 && s == 0) ? CTRL_MOVRZ_WAIT3 : 0); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pac, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pa); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0, .src3_vreg = v_pbcol); + BW_APPEND(prog, .op = BW_IR_MOV_IMM, .dst_vreg = v_cnt, .imm = mutant ? ITERS - 1 : ITERS); + + /* ---- K loop: `unroll` steps of (rt A + ct B) loads, then the rt*ct*unroll HMMAs ---- */ + const int L_k = (int)prog->count; + for (uint32_t u = 0; u < unroll; u++) { + for (uint32_t i = 0; i < rt; i++) + BW_APPEND(prog, .op = BW_IR_LDG_E_128, .dst_vreg = v_ra[u][i], .src1_vreg = v_pac, .src2_vreg = -1, .src3_vreg = -1, + .ureg = uv_desc, .imm = u * 512u + i * KS * 512u, .control = BW_FRAG_CTRL_LDG_A); + for (uint32_t j = 0; j < ct; j++) + BW_APPEND(prog, .op = BW_IR_LDG_E_64, .dst_vreg = v_rb[u][j], .src1_vreg = v_pbc, .src2_vreg = -1, .src3_vreg = -1, + .ureg = uv_desc, .imm = u * 256u + j * KS * 256u, .control = BW_FRAG_CTRL_LDG_B); + } + const BlackwellIROpcode hmma = (spec->precision == OMEGA_MATMUL_PRECISION_BF16) ? BW_IR_HMMA_BF16 : BW_IR_HMMA_F16; + for (uint32_t u = 0; u < unroll; u++) + for (uint32_t i = 0; i < rt; i++) + for (uint32_t j = 0; j < ct; j++) { + const int rd = v_rd[i * ct + j]; + BW_APPEND(prog, .op = hmma, .dst_vreg = rd, .src1_vreg = v_ra[u][i], .src2_vreg = v_rb[u][j], .src3_vreg = rd, + .control = BW_FRAG_CTRL_HMMA); + BW_APPEND(prog, .op = BW_IR_NOP, .dst_vreg = -1, .src1_vreg = -1, .src2_vreg = -1, .src3_vreg = -1, .ureg = -1, + .control = BW_FRAG_CTRL_NOP); + } + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pac, .src1_vreg = v_c1, .src2_vreg = -1, .imm = unroll * 512u, .src3_vreg = v_pac); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbc, .src1_vreg = v_c1, .src2_vreg = -1, .imm = unroll * 256u, .src3_vreg = v_pbc); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_cnt, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 0xffffffffu, .src3_vreg = v_cnt); + BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_cnt, .src2_vreg = -1, .imm = 1, .src3_vreg = -1); + { + int here = (int)prog->count; + BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_k - here), .predicate_p0 = true); + } + const int L_k_end = (int)prog->count - 1; + + /* ---- store the rt*ct tiles: tile (i, j) at +i*16*N*4 + j*32 bytes from pc0 (rows r0) and pc1 (rows r1) ---- */ + for (uint32_t i = 0; i < rt; i++) + for (uint32_t j = 0; j < ct; j++) { + const int rd = v_rd[i * ct + j]; + const uint32_t off = i * 16u * N * 4u + j * 32u; + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc0, .src2_vreg = rd, .src2_subreg = 0, .src3_vreg = -1, .ureg = uv_desc, .imm = off, .control = CTRL_STG_RDBAR3); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc0, .src2_vreg = rd, .src2_subreg = 1, .src3_vreg = -1, .ureg = uv_desc, .imm = off + 4u, .control = CTRL_STG_RDBAR3); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc1, .src2_vreg = rd, .src2_subreg = 2, .src3_vreg = -1, .ureg = uv_desc, .imm = off, .control = CTRL_STG_RDBAR3); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc1, .src2_vreg = rd, .src2_subreg = 3, .src3_vreg = -1, .ureg = uv_desc, .imm = off + 4u, .control = CTRL_STG_RDBAR3); + } + /* next column block: col += grid_x*ct ; C by grid_x*ct*8 f32 (waits SB3 first) ; B by grid_x*ct tiles of KS*256 bytes */ + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * ct, .src3_vreg = v_col); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc0, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * ct * 32u, .src3_vreg = v_pc0, .control = CTRL_IMADW_WAIT3); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * ct * 32u, .src3_vreg = v_pc1); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_pbcol, .src1_vreg = v_c1, .src2_vreg = -1, .imm = grid_x * ct * KS * 256u, .src3_vreg = v_pbcol); + BW_APPEND(prog, .op = BW_IR_ISETP_GE, .dst_vreg = -1, .src1_vreg = v_col, .src2_vreg = -1, .imm = NT, .src3_vreg = -1); + { + int here = (int)prog->count; + BW_APPEND(prog, .op = BW_IR_BRA, .imm = (uint32_t)(int32_t)(L_col - here), .predicate_p0 = true, .predicate_not = true); + } + const int L_col_end = (int)prog->count - 1; + BW_APPEND(prog, .op = BW_IR_EXIT); + BW_APPEND(prog, .op = BW_IR_BRA); + + /* loop-carried values live to the end of their loop (as the kernel above) */ + const int col_carried[] = { v_c1, v_col, v_pa, v_pbcol, v_pc0, v_pc1 }; + for (size_t i = 0; i < sizeof col_carried / sizeof col_carried[0]; i++) bw_live_until(prog, col_carried[i], L_col_end); + for (uint32_t t = 0; t < rt * ct; t++) bw_live_until(prog, v_rd[t], L_col_end); + const int k_carried[] = { v_pac, v_pbc, v_cnt }; + for (size_t i = 0; i < sizeof k_carried / sizeof k_carried[0]; i++) bw_live_until(prog, k_carried[i], L_k_end); + bw_keep_alive(prog, L_k, L_k_end, L_k_end); + return omega_bw_regalloc_solve(prog); +} + +int omega_blackwell_codegen_matmul_frag_block(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, + uint32_t rt, uint32_t ct, int mutant, OmegaBlackwellKernel *kernel) { + if (!spec || !kernel) return -1; + BlackwellIRProgram prog; + if (omega_blackwell_codegen_matmul_frag_block_prog(spec, grid_x, unroll, rt, ct, mutant, &prog) != 0) return -1; + size_t max_bytes = (size_t)BW_MAX_IR_INSNS * 16u; + kernel->code = malloc(max_bytes); + if (!kernel->code) return -1; + size_t emitted_size = 0; + if (omega_bw_encode_program(&prog, kernel->code, max_bytes, &emitted_size) != 0) { + free(kernel->code); + kernel->code = NULL; + return -1; + } + kernel->code_size = emitted_size; + kernel->insn_count = emitted_size / 16; + kernel->gpr_count = prog.regalloc.peak_gpr_usage; + kernel->uniform_gpr_count = prog.regalloc.peak_ugpr_usage; + sha256_hash(kernel->code, kernel->code_size, kernel->code_digest); + return 0; +} + +/* The row and column tiles one warp owns in the blocked kernel: the largest of 4, 2, 1 dividing the + * row tiles (rt), and for rt > 1 the largest of 4, 2, 1 dividing the column tiles (ct); rt = ct = 1 + * is the single-tile kernel (decode rows). */ +void omega_matmul_frag_block_shape(uint32_t mt, uint32_t nt, uint32_t *rt, uint32_t *ct) { + uint32_t r = mt % 4u == 0 ? 4u : mt % 2u == 0 ? 2u : 1u; + *rt = r; + *ct = r > 1 ? (nt % 4u == 0 ? 4u : nt % 2u == 0 ? 2u : 1u) : 1u; +} + +/* loads per pass at or under 8 (see the kernel comment): unroll 2 only when 2*(rt+ct) fits and KS is even */ +uint32_t omega_matmul_frag_block_unroll(uint32_t kp, uint32_t rt, uint32_t ct) { + const uint32_t ks = kp / 16u; + return (ks % 2u == 0 && 2u * (rt + ct) <= 8u) ? 2u : 1u; +} + uint32_t omega_matmul_frag_unroll(uint32_t kp) { const uint32_t ks = kp / 16u; return ks % 4u == 0 ? 4u : ks % 2u == 0 ? 2u : 1u; diff --git a/src/omega_blackwell_codegen.h b/src/omega_blackwell_codegen.h index bfbde3e7e..744f6d630 100644 --- a/src/omega_blackwell_codegen.h +++ b/src/omega_blackwell_codegen.h @@ -237,6 +237,17 @@ int omega_blackwell_codegen_matmul_frag(const OmegaMatMulSpec *spec, uint32_t gr /* The largest of 4, 2, 1 that divides kp / 16 (unroll 8 is generated for experiments only: it * did not complete on the GB10, see the definition). */ uint32_t omega_matmul_frag_unroll(uint32_t kp); +/* 2026-10-12: blocked fragment kernel for prefill rows. One warp owns rt row tiles x ct column tiles + * (rt in 1, 2, 4, 8; ct in 1, 2, 4; 2 <= rt*ct <= 16; unroll 1 or 2 with unroll*(rt+ct) <= 8; nt % ct == 0; + * grid_x <= nt/ct). Same packing, argument words and C layout as the single-tile kernel; a CTA's + * row block is rt row tiles (grid_y = row tiles / rt). mutant != 0 drops the last K pass. */ +int omega_blackwell_codegen_matmul_frag_block_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, + uint32_t rt, uint32_t ct, int mutant, BlackwellIRProgram *prog); +int omega_blackwell_codegen_matmul_frag_block(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, + uint32_t rt, uint32_t ct, int mutant, OmegaBlackwellKernel *kernel); +/* rt, ct for mt row tiles and nt column tiles (1, 1 = the single-tile kernel), and the unroll the API uses */ +void omega_matmul_frag_block_shape(uint32_t mt, uint32_t nt, uint32_t *rt, uint32_t *ct); +uint32_t omega_matmul_frag_block_unroll(uint32_t kp, uint32_t rt, uint32_t ct); /* a: m x k row-major. dst: mp x kp elements, fully written (zero padding). */ void omega_matmul_frag_pack_a(const uint16_t *a, uint32_t m, uint32_t k, uint32_t mp, uint32_t kp, uint16_t *dst); /* b: k x n row-major. dst: the column tiles [nt0, nt1), (nt1 - nt0) * kp * 8 elements, fully diff --git a/src/omega_gpu_matmul_api.c b/src/omega_gpu_matmul_api.c index e356e005e..c172e2993 100644 --- a/src/omega_gpu_matmul_api.c +++ b/src/omega_gpu_matmul_api.c @@ -42,6 +42,7 @@ typedef struct { bool valid; bool pinned; /* built by omega_gpu_matmul_prepare: never evicted */ uint32_t kp, np, grid_x; + uint32_t rt, ct; /* tiles per warp (omega_matmul_frag_block_shape); 1, 1 = the single-tile kernel */ int mutant; OmegaMatMulSpec spec; OmegaBlackwellKernel kernel; @@ -167,8 +168,8 @@ void omega_gpu_matmul_unreserve(void) { omega_gpu_session_unlock(); } -static int kernel_for(uint32_t kp, uint32_t np, uint32_t grid_x, CacheSlot **out, bool *hit); -static void launch_shape(uint32_t mp, uint32_t np, uint32_t *rows_per_launch, uint32_t *grid_x); +static int kernel_for(uint32_t kp, uint32_t np, uint32_t grid_x, uint32_t rt, uint32_t ct, CacheSlot **out, bool *hit); +static void launch_shape(uint32_t mp, uint32_t np, uint32_t *rows_per_launch, uint32_t *grid_x, uint32_t *rt, uint32_t *ct); int omega_gpu_matmul_prepare(uint32_t m, uint32_t k, uint32_t n) { if (m == 0 || k == 0 || n == 0) { omega_gpu_session_set_error("matmul prepare: zero dimension"); return OMEGA_GPU_MATMUL_BAD_ARGS; } @@ -177,14 +178,14 @@ int omega_gpu_matmul_prepare(uint32_t m, uint32_t k, uint32_t n) { return OMEGA_GPU_MATMUL_TOO_LARGE; } const uint32_t mp = round_up(m, OMEGA_GPU_MATMUL_TILE_M), kp = round_up(k, OMEGA_GPU_MATMUL_TILE_K), np = round_up(n, OMEGA_GPU_MATMUL_TILE_N); - uint32_t rows_per_launch, grid_x; + uint32_t rows_per_launch, grid_x, rt, ct; omega_gpu_session_lock(); int rc = OMEGA_GPU_MATMUL_OK; if (!dev_open_locked()) rc = OMEGA_GPU_MATMUL_CHIP_FAIL; else { - launch_shape(mp, np, &rows_per_launch, &grid_x); + launch_shape(mp, np, &rows_per_launch, &grid_x, &rt, &ct); CacheSlot *s = NULL; bool hit = false; - rc = kernel_for(kp, np, grid_x, &s, &hit); + rc = kernel_for(kp, np, grid_x, rt, ct, &s, &hit); if (rc == OMEGA_GPU_MATMUL_OK) s->pinned = true; /* a prepared kernel is never evicted */ } omega_gpu_session_unlock(); @@ -218,29 +219,34 @@ void omega_gpu_matmul_test_set_unroll(uint32_t unroll) { /* ---- one launch shape (the kernel cache key bakes in grid_x) ---- */ -/* rows per launch: all of them unless the row tiles alone exceed the CTA budget; grid_x fills the rest of the budget */ -static void launch_shape(uint32_t mp, uint32_t np, uint32_t *rows_per_launch, uint32_t *grid_x) { +/* rows per launch: all of them unless the row blocks alone exceed the CTA budget; grid_x fills the rest of + * the budget. A CTA (one warp) owns rt row tiles x ct column tiles (omega_matmul_frag_block_shape, 2026-10-12): + * rt = ct = 1 for decode rows (the single-tile kernel), 4 x 4 at m = 128. Every launch of a call has a row + * count divisible by 16*rt (rows_per_launch is, and so is the remainder, since rt divides mp/16). */ +static void launch_shape(uint32_t mp, uint32_t np, uint32_t *rows_per_launch, uint32_t *grid_x, uint32_t *rt, uint32_t *ct) { const uint32_t budget = g.cta_budget ? g.cta_budget : OMEGA_GPU_MATMUL_MAX_CTAS, mt = mp / 16u, nt = np / 8u; - *rows_per_launch = mt <= budget ? mp : budget * 16u; - uint32_t gx = budget / (*rows_per_launch / 16u); + omega_matmul_frag_block_shape(mt, nt, rt, ct); + const uint32_t rb = mt / *rt, nb = nt / *ct; + *rows_per_launch = rb <= budget ? mp : budget * 16u * *rt; + uint32_t gx = budget / (*rows_per_launch / 16u / *rt); if (gx == 0) gx = 1; - if (gx > nt) gx = nt; + if (gx > nb) gx = nb; *grid_x = gx; } /* ---- kernel cache (caller holds the lock, device open) ---- */ -static int kernel_for(uint32_t kp, uint32_t np, uint32_t grid_x, CacheSlot **out, bool *hit) { +static int kernel_for(uint32_t kp, uint32_t np, uint32_t grid_x, uint32_t rt, uint32_t ct, CacheSlot **out, bool *hit) { for (unsigned i = 0; i < g.slots; i++) { CacheSlot *s = &g.cache[i]; - if (s->valid && s->kp == kp && s->np == np && s->grid_x == grid_x && s->mutant == g.mutant) { + if (s->valid && s->kp == kp && s->np == np && s->grid_x == grid_x && s->rt == rt && s->ct == ct && s->mutant == g.mutant) { *out = s; *hit = true; return OMEGA_GPU_MATMUL_OK; } } if (g.sealed) { char e[160]; - snprintf(e, sizeof e, "serving reservation exceeded: matmul kernel kp=%u np=%u grid_x=%u was not prepared", kp, np, grid_x); + snprintf(e, sizeof e, "serving reservation exceeded: matmul kernel kp=%u np=%u grid_x=%u rt=%u ct=%u was not prepared", kp, np, grid_x, rt, ct); omega_gpu_session_set_error(e); return OMEGA_GPU_MATMUL_TOO_LARGE; } @@ -259,17 +265,20 @@ static int kernel_for(uint32_t kp, uint32_t np, uint32_t grid_x, CacheSlot **out if (omega_matmul_spec_init(&spec, 16, kp, np, OMEGA_MATMUL_PRECISION_BF16) != 0) return OMEGA_GPU_MATMUL_TOO_LARGE; OmegaBlackwellKernel kernel; memset(&kernel, 0, sizeof kernel); - uint32_t unroll = g.unroll ? g.unroll : omega_matmul_frag_unroll(kp); + const bool blocked = rt > 1 || ct > 1; + /* the test unroll knob applies to the single-tile kernel; the blocked kernel keeps its own rule */ + uint32_t unroll = blocked ? omega_matmul_frag_block_unroll(kp, rt, ct) : g.unroll ? g.unroll : omega_matmul_frag_unroll(kp); /* the mutant drops the last pass, so it needs two: halve the unroll of a one-pass shape */ - if (g.mutant && !g.unroll) while (unroll > 1 && kp / 16u / unroll < 2) unroll /= 2; - if (omega_blackwell_codegen_matmul_frag(&spec, grid_x, unroll, g.mutant, &kernel) != 0 || - !kernel.code || kernel.code_size == 0) + if (g.mutant && (blocked || !g.unroll)) while (unroll > 1 && kp / 16u / unroll < 2) unroll /= 2; + int cg = blocked ? omega_blackwell_codegen_matmul_frag_block(&spec, grid_x, unroll, rt, ct, g.mutant, &kernel) + : omega_blackwell_codegen_matmul_frag(&spec, grid_x, unroll, g.mutant, &kernel); + if (cg != 0 || !kernel.code || kernel.code_size == 0) return OMEGA_GPU_MATMUL_CODEGEN_FAIL; NvrmMem code; if (omega_gpu_session_alloc_code(kernel.code_size, &code) != 0) { omega_blackwell_kernel_free(&kernel); return OMEGA_GPU_MATMUL_CHIP_FAIL; } memcpy(code.cpu, kernel.code, kernel.code_size); if (s->valid) { omega_blackwell_kernel_free(&s->kernel); omega_gpu_session_free(&s->code); } - s->valid = true; s->pinned = false; s->kp = kp; s->np = np; s->grid_x = grid_x; s->mutant = g.mutant; + s->valid = true; s->pinned = false; s->kp = kp; s->np = np; s->grid_x = grid_x; s->rt = rt; s->ct = ct; s->mutant = g.mutant; s->spec = spec; s->kernel = kernel; s->code = code; *out = s; *hit = false; return OMEGA_GPU_MATMUL_OK; @@ -280,7 +289,7 @@ static int kernel_for(uint32_t kp, uint32_t np, uint32_t grid_x, CacheSlot **out static int launch(const CacheSlot *ks, uint64_t a_va, uint64_t b_va, uint64_t c_va, uint32_t grid_x, uint32_t grid_y, uint64_t *elapsed_ns, uint32_t *marker_out) { uint32_t cbank_args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; - omega_blackwell_build_cbank_args_matmul(cbank_args, a_va, b_va, c_va, grid_y * 16u, ks->kp, ks->np); + omega_blackwell_build_cbank_args_matmul(cbank_args, a_va, b_va, c_va, grid_y * 16u * ks->rt, ks->kp, ks->np); OmegaGpuLaunch L = { .code_va = ks->code.va, .gpr_count = ks->kernel.gpr_count, .threads_x = 32, .threads_y = 1, .grid_x = grid_x, .grid_y = grid_y, @@ -355,9 +364,9 @@ static int matmul_core(uint32_t m, const uint16_t *a, const OmegaGpuTensor *b, f omega_gpu_session_lock(); int rc = OMEGA_GPU_MATMUL_OK; if (!dev_open_locked()) { omega_gpu_session_unlock(); return OMEGA_GPU_MATMUL_CHIP_FAIL; } - uint32_t rows_per_launch, grid_x; - launch_shape(mp, np, &rows_per_launch, &grid_x); - uint32_t grid_y_full = rows_per_launch / 16u; + uint32_t rows_per_launch, grid_x, rt, ct; + launch_shape(mp, np, &rows_per_launch, &grid_x, &rt, &ct); + uint32_t grid_y_full = rows_per_launch / 16u / rt; if (info) { info->m = m; info->k = k; info->n = n; info->padded_m = mp; info->padded_n = np; info->padded_k = kp; info->k_slices = 1; info->rows_per_call = rows_per_launch; info->grid_x = grid_x; info->grid_y = grid_y_full; @@ -392,9 +401,9 @@ static int matmul_core(uint32_t m, const uint16_t *a, const OmegaGpuTensor *b, f for (uint32_t r0 = 0; r0 < mp && rc == OMEGA_GPU_MATMUL_OK; r0 += rows_per_launch) { uint32_t rows = mp - r0 < rows_per_launch ? mp - r0 : rows_per_launch; - uint32_t grid_y = rows / 16u; + uint32_t grid_y = rows / 16u / rt; CacheSlot *ks = NULL; bool hit = false; - rc = kernel_for(kp, np, grid_x, &ks, &hit); + rc = kernel_for(kp, np, grid_x, rt, ct, &ks, &hit); if (rc != OMEGA_GPU_MATMUL_OK) break; if (info && !hit) info->kernel_cache_hit = false; rc = launch(ks, g.a_buf.mem.va + (uint64_t)r0 * kp * 2u, b->mem.va, g.c_buf.mem.va + (uint64_t)r0 * np * 4u, diff --git a/tests/.crumb b/tests/.crumb index e4b7ff7fd..c1b7dffa8 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -290,14 +290,14 @@ } ], "children_root": "sha256:9c94c5a57dda7727107b9e1271460c33b2cde72e796459e1ae401d4d4aeaec78", - "compiled_at": "2026-10-12T06:04:50Z", + "compiled_at": "2026-10-12T06:51:27Z", "compiler": "crumb-compile/1", - "digest": "sha256:e59264059051e10ee92dea5caafb09147f1c0c41b6a9eb81427cc6174def0a99", + "digest": "sha256:d4b26789b662d538601b2dacee7e99667ef84c9d12a57dbeee3b894465bb449e", "evidence_files": 16, "evidence_root": "sha256:96d08097822ad95f4e562c19e0ba5df5a035774c48632f1832912c40a27bd27a", "files": 1022, - "generated_at_commit": "1e6dfa909f2ea951f99597dd68bb89fd62ac2291", - "source_tree": "sha256:9522e4fc71462ea268256a7459b9800891ff1e8a85ebbd08f1b254dd2d5029a7" + "generated_at_commit": "03e4e6a844248a735e135c4ac2d9da5b064b7680", + "source_tree": "sha256:545d8dca817b4a6271e5ce24554973207f99c0bcbbde4e87ca4d6d93efcb41d9" }, "owner_lane": "", "provenance": { diff --git a/tests/gpu_matmul_api_test.c b/tests/gpu_matmul_api_test.c index 484feae28..ef3e3b1b9 100644 --- a/tests/gpu_matmul_api_test.c +++ b/tests/gpu_matmul_api_test.c @@ -139,6 +139,49 @@ static void frag_host_checks(void) { omega_blackwell_codegen_matmul_frag(&spec, 1, 2, 0, &kernel) != 0, "unroll not dividing K/16 refused"); CHECK(omega_blackwell_codegen_matmul_frag(&spec, 1, 3, 0, &kernel) != 0, "unroll 3 refused"); CHECK(omega_blackwell_codegen_matmul_frag(&spec, 3, 1, 0, &kernel) != 0, "fragment grid_x beyond the column tiles refused"); + /* 2026-10-12: the blocked kernel (rt row tiles x ct column tiles per warp) generates for the Qwen3-8B + * prefill shapes at m = 128 (rt 4, ct 4, grid_x 512 at the daemon's 1024-CTA budget) and the small + * parity shapes; it fits the allocator's 128 registers; its mutant differs; the envelope is enforced */ + struct { uint32_t k, n, gx, rt, ct; } bk[] = { {4096, 24576, 512, 4, 4}, {12288, 4096, 128, 4, 4}, {4096, 6144, 192, 4, 4}, + {128, 128, 4, 4, 4}, {32, 32, 1, 2, 4}, {16, 8, 1, 2, 1}, {1024, 8, 1, 4, 1}, + {64, 64, 2, 4, 2}, {48, 16, 1, 2, 2} }; + for (size_t i = 0; i < sizeof bk / sizeof bk[0]; i++) { + OmegaMatMulSpec spec; OmegaBlackwellKernel kernel; memset(&kernel, 0, sizeof kernel); + const uint32_t u = omega_matmul_frag_block_unroll(bk[i].k, bk[i].rt, bk[i].ct); + int rc = omega_matmul_spec_init(&spec, 16, bk[i].k, bk[i].n, OMEGA_MATMUL_PRECISION_BF16) == 0 && + omega_blackwell_codegen_matmul_frag_block(&spec, bk[i].gx, u, bk[i].rt, bk[i].ct, 0, &kernel) == 0 && kernel.code_size > 0; + CHECK(rc, "blocked kernel K=%u N=%u gx=%u rt=%u ct=%u unroll=%u generated", bk[i].k, bk[i].n, bk[i].gx, bk[i].rt, bk[i].ct, u); + CHECK(u * (bk[i].rt + bk[i].ct) <= 8, "blocked kernel K=%u rt=%u ct=%u issues at most 8 loads per pass (unroll %u)", bk[i].k, bk[i].rt, bk[i].ct, u); + if (i < 3) printf("blocked kernel K=%u N=%u rt=%u ct=%u unroll %u: %zu instructions, %u registers\n", bk[i].k, bk[i].n, bk[i].rt, bk[i].ct, u, kernel.insn_count, kernel.gpr_count); + OmegaBlackwellKernel mut; memset(&mut, 0, sizeof mut); + if (omega_blackwell_codegen_matmul_frag_block(&spec, bk[i].gx, u, bk[i].rt, bk[i].ct, 1, &mut) == 0) { + CHECK(memcmp(mut.code_digest, kernel.code_digest, 32) != 0, "blocked mutant differs K=%u N=%u", bk[i].k, bk[i].n); + omega_blackwell_kernel_free(&mut); + } else CHECK(bk[i].k / 16 / u < 2, "blocked mutant refused only with one pass (K=%u)", bk[i].k); + omega_blackwell_kernel_free(&kernel); + } + { + uint32_t rt, ct; + omega_matmul_frag_block_shape(8, 3072, &rt, &ct); CHECK(rt == 4 && ct == 4, "block shape m=128 n=24576 is 4x4"); + omega_matmul_frag_block_shape(1, 3072, &rt, &ct); CHECK(rt == 1 && ct == 1, "block shape m=1 is the single-tile kernel"); + omega_matmul_frag_block_shape(7, 3072, &rt, &ct); CHECK(rt == 1 && ct == 1, "block shape m=112 (7 row tiles) is the single-tile kernel"); + omega_matmul_frag_block_shape(6, 6, &rt, &ct); CHECK(rt == 2 && ct == 2, "block shape 6 row tiles, 6 column tiles is 2x2"); + omega_matmul_frag_block_shape(4, 1, &rt, &ct); CHECK(rt == 4 && ct == 1, "block shape 4 row tiles, 1 column tile is 4x1"); + CHECK(omega_matmul_frag_block_unroll(4096, 4, 4) == 1 && omega_matmul_frag_block_unroll(4096, 2, 2) == 2 && + omega_matmul_frag_block_unroll(48, 2, 2) == 1 && omega_matmul_frag_block_unroll(4096, 4, 2) == 1, "blocked unroll rule (loads per pass <= 8)"); + OmegaMatMulSpec s; OmegaBlackwellKernel kk; memset(&kk, 0, sizeof kk); + CHECK(omega_matmul_spec_init(&s, 16, 4096, 6144, OMEGA_MATMUL_PRECISION_BF16) == 0, "spec for refusals"); + CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 1, 1, 1, 1, 0, &kk) != 0, "blocked 1x1 refused (single-tile kernel)"); + CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 1, 1, 3, 2, 0, &kk) != 0, "blocked rt 3 refused"); + CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 1, 1, 8, 4, 0, &kk) != 0, "blocked 8x4 (32 accumulators) refused"); + CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 1, 1, 8, 1, 0, &kk) != 0, "blocked 8x1 (9 loads per pass) refused"); + CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 1, 2, 4, 4, 0, &kk) != 0, "blocked unroll 2 at 4x4 (16 loads per pass) refused"); + CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 1, 4, 2, 2, 0, &kk) != 0, "blocked unroll 4 refused"); + CHECK(omega_blackwell_codegen_matmul_frag_block(&s, 193, 1, 4, 4, 0, &kk) != 0, "blocked grid_x beyond the column blocks refused"); + OmegaMatMulSpec s3; CHECK(omega_matmul_spec_init(&s3, 16, 64, 24, OMEGA_MATMUL_PRECISION_BF16) == 0, "spec n=24"); + CHECK(omega_blackwell_codegen_matmul_frag_block(&s3, 1, 1, 2, 4, 0, &kk) != 0, "blocked ct not dividing the column tiles refused"); + CHECK(omega_blackwell_codegen_matmul_frag_block(&s3, 1, 1, 2, 2, 0, &kk) != 0, "blocked ct 2 on 3 column tiles refused"); + } } static void host_only(void) { @@ -289,6 +332,9 @@ static int timing(const char *shapes, const char *out_path) { double t0 = now_ms(); int rc = omega_gpu_matmul_resident_bf16(m, ah, t, c, &info); samples[i] = now_ms() - t0; + /* OMEGA_GPU_MATMUL_TIMING_NO_ORACLE=1: the per-call host oracle (m x n x k double MACs) only on the first + * call; at m = 128 it is minutes per shape and the timing is the point (2026-10-12). */ + if (i == 0 && getenv("OMEGA_GPU_MATMUL_TIMING_NO_ORACLE")) omega_gpu_matmul_set_oracle(0); if (rc == OMEGA_GPU_MATMUL_OK) ok_calls++; chip_ns_sum += info.elapsed_ns; if (i == 0) memcpy(c_first, c, nc * sizeof *c); @@ -301,7 +347,7 @@ static int timing(const char *shapes, const char *out_path) { size_t bad_first = 0, bad_last = 0; double e2e_first = e2e_max_rel(m, k, n, a, b, c_first, &bad_first); double e2e_last = e2e_max_rel(m, k, n, a, b, c, &bad_last); - int pass = ok_calls == 100 && bad_first == 0 && bad_last == 0 && repeat_mismatch == 0 && median < 5.0; + int pass = ok_calls == 100 && bad_first == 0 && bad_last == 0 && repeat_mismatch == 0 && (m > 1 || median < 5.0); /* the 5 ms gate is the GEMV (m = 1) gate */ CHECK(pass, "timing %ux%ux%u ok_calls=%d median_ms=%.3f e2e_first=%g e2e_last=%g bad=%zu/%zu repeat_mismatch=%zu", m, k, n, ok_calls, median, e2e_first, e2e_last, bad_first, bad_last, repeat_mismatch); printf("%s timing %ux%ux%u upload_ms=%.1f ok_calls=%d median_ms=%.3f p90_ms=%.3f min_ms=%.3f max_ms=%.3f chip_ns_mean=%" PRIu64 " grid=%ux%u e2e_first=%g e2e_last=%g repeat_mismatch=%zu\n", @@ -313,6 +359,7 @@ static int timing(const char *shapes, const char *out_path) { ok_calls ? chip_ns_sum / (uint64_t)ok_calls : 0, info.grid_x, info.grid_y, e2e_first, e2e_last, bad_first, bad_last, repeat_mismatch); first = 0; } + omega_gpu_matmul_set_oracle(1); omega_gpu_tensor_free(t); free(a); free(b); free(c); free(c_first); free(ah); if (omega_gpu_matmul_is_blocked()) break;