omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
GitHub aien-dev/omega-mv#330 (code only)
Fetch it
curl -fsSL https://interplane.io/drakestapleton/omega-mv/patches/f3d99fcb0626f680.bundle -o f3d99fcb0626f680.bundle git fetch f3d99fcb0626f680.bundle refs/pull/330/head
Provenance
github-import 1
aien-dev/omega-mv
330
1b9538bf86094c38a2ca9eb476d9d4f3f54dd955
{"kind":"github-import","owner":"aien-dev","repo":"omega-mv","number":330,"url":"https://github.com/aien-dev/omega-mv/pull/330","export_sha256":"967c79e26a893657b5cdaa45efd442be5dbd2ecfe68e25f9a8c1b5da4d60d452","imported_utc":"2026-11-18T16:37:22Z","metadata":"unavailable","state_derived":true}
Changes
diff --git a/.crumb b/.crumb index fffb274db..3c4c1de65 100644 --- a/.crumb +++ b/.crumb @@ -87,14 +87,14 @@ "purpose": "Directory spec. Purpose not yet described by a human." }, { - "digest": "fc811240fb8c5840adf9f00fcae85a1a8cf4832232e0b01af273dce5bfdd94de", - "last_commit": "8fa3768", + "digest": "b53b56d9c9e4a8a392f8bbaec46330557ef6bb9e73616b2b1bd79571e58cfdce", + "last_commit": "dfc602b", "name": "src", "purpose": "Directory src. Purpose not yet described by a human." }, { - "digest": "0dc1710b156c18dc1ad2ec7c311476c66184878ab9dd9dd250c879748cd0c5b4", - "last_commit": "8fa3768", + "digest": "36d064d2eb869f98033575e088496ed4b6fbdb1158364c62046ef5b6b8354386", + "last_commit": "dfc602b", "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:b03bc004d9ac6c42471db83334b1ebf67c7d57eff23f968006369ef1dc08d263", - "compiled_at": "2026-10-07T07:22:31Z", + "children_root": "sha256:079098299f5e409c9a1a9f046e60830ba24de77ccd29635fc7173cb528054efc", + "compiled_at": "2026-10-07T11:04:26Z", "compiler": "crumb-compile/1", - "digest": "sha256:ba844abceb616ab797b178857a06b32416eb5ff1e66a682c397461b3182f1c74", + "digest": "sha256:641a1be5ccb611a3bb2dedd0a63b62fe1fc3c7d46c1c514e5d27fe74f7ad2746", "evidence_files": 4818, "evidence_root": "sha256:07642731585ad396c5d9b566a4b8df7807018f494d322dffce41bd04fb51e553", "files": 6658, - "generated_at_commit": "8fa37681cc28fdd7a8fb235c53c2ce746615be4b", - "source_tree": "sha256:095235834cb8e8c52a65875a40a626c245a9de8d0de3766f0a0ab79e6f27a232" + "generated_at_commit": "dfc602b8e0542ffdf74e361b57cecc859f74e0f1", + "source_tree": "sha256:2787eaa509e7d4b0fbefc99fab65d184ca38682af7271de7aa52b54340d47838" }, "owner_lane": "", "provenance": { diff --git a/src/.crumb b/src/.crumb index 63c450a14..444030c7c 100644 --- a/src/.crumb +++ b/src/.crumb @@ -170,14 +170,14 @@ } ], "children_root": "sha256:564cf410317a627d546c0c234bd632ba414ca943ecca4fd4c8eccc45d863bb48", - "compiled_at": "2026-10-07T07:22:31Z", + "compiled_at": "2026-10-07T11:04:26Z", "compiler": "crumb-compile/1", - "digest": "sha256:fc811240fb8c5840adf9f00fcae85a1a8cf4832232e0b01af273dce5bfdd94de", + "digest": "sha256:b53b56d9c9e4a8a392f8bbaec46330557ef6bb9e73616b2b1bd79571e58cfdce", "evidence_files": 0, "evidence_root": "sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855", "files": 412, - "generated_at_commit": "8fa37681cc28fdd7a8fb235c53c2ce746615be4b", - "source_tree": "sha256:5deac563c306d5903469c05986691c14729a09144f2a8263e39e0eda890ce731" + "generated_at_commit": "dfc602b8e0542ffdf74e361b57cecc859f74e0f1", + "source_tree": "sha256:92674383c43b8613c2113d58b4203dc20a8b4ae044c6dd0f22537f10aeb683d5" }, "owner_lane": "", "provenance": { diff --git a/src/omega_blackwell_codegen.c b/src/omega_blackwell_codegen.c index 38f73412a..1ed52ba13 100644 --- a/src/omega_blackwell_codegen.c +++ b/src/omega_blackwell_codegen.c @@ -268,7 +268,7 @@ static int encode_single_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc * w[0] = 0x00007918; w[1] = 0x00000000; w[2] = 0x00000000; - w[3] = 0x000fc000; + w[3] = insn->control ? insn->control : 0x000fc000; /* control: a timed pause (fragment matmul) */ break; case BW_IR_MOV_RZ: @@ -430,6 +430,20 @@ static int encode_single_insn(const BlackwellIRInsn *insn, const OmegaRegAlloc * w[3] = insn->control ? insn->control : 0x001fca00; break; + case BW_IR_LDG_E_64: + case BW_IR_LDG_E_128: { + /* LDG.E.64 / LDG.E.128 Rd, desc[URd][Ra.64+imm]. imm = signed 24-bit byte offset at + * bits 40..63 (w[1] bits 8..31); size field in w[2] bits 9..11 (32 = 4, 64 = 5, 128 = 6). + * Words from nvcc 13.0.88 -arch=sm_121 (omega_blackwell_verify_codegen_fixtures_fragmm). */ + int32_t off = (int32_t)insn->imm; + if (off < -(1 << 23) || off >= (1 << 23)) return -1; + w[0] = 0x7981U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); + w[1] = (uint32_t)(ureg & 0xff) | (((uint32_t)off & 0xffffffU) << 8); + w[2] = insn->op == BW_IR_LDG_E_64 ? 0x0c1e1b00U : 0x0c1e1d00U; + w[3] = insn->control ? insn->control : 0x000f2200; + break; + } + case BW_IR_LDG_E_U16: /* LDG.E.U16 Rd, desc[URd][Ra.64] */ w[0] = 0x7981U | ((uint32_t)(dst & 0xff) << 16) | ((uint32_t)(src1 & 0xff) << 24); @@ -822,6 +836,51 @@ int omega_blackwell_encode_one(const BlackwellIRInsn *insn, uint32_t w[4]) { return encode_single_insn(insn, &ra, w); } +/* omega #328: golden words for the fragment-major matmul. Oracle: nvcc 13.0.88 -arch=sm_121 + * -cubin -O3, cuobjdump -sass, on a kernel with uint2 / uint4 loads at immediate offsets and a + * dependent mma.m16n8k16 bf16 chain (source and dump sealed in + * ~/workspace/investigations/2026-10-07-qwen3-decode-matmul/oracle/frag_plain.{cu,sass}): + * LDG.E.64 R2, desc[UR8][R30.64] ; 0x000000081e027981 0x000ea8000c1e1b00 + * LDG.E.64 R28, desc[UR8][R30.64+0x100] ; 0x000100081e1c7981 0x000ee8000c1e1b00 + * LDG.E.128 R8, desc[UR8][R32.64] ; 0x0000000820087981 0x000ea8000c1e1d00 + * LDG.E.128 R20, desc[UR8][R32.64+0x1000] ; 0x0010000820147981 0x000e9e000c1e1d00 + * HMMA.16816.F32.BF16 R8, R12, R6, R8 ; 0x000000060c08723c 0x010fe20000041808 + * NOP ; 0x0000000000007918 0x000fdc0000000000 + * The dependent HMMA chain nvcc emits is HMMA (stall 15) then NOP (stall 14): 29 cycles from + * one HMMA to the next that reads its accumulator. w3 is compared for the NOP only (it is the + * whole point of that instruction). 0 = all good. */ +int omega_blackwell_verify_codegen_fixtures_fragmm(void) { + static const struct { + BlackwellIROpcode op; int d, a, b, c, u; uint32_t imm, control; uint32_t w0, w1, w2, w3; const char *text; + } fx[] = { + { BW_IR_LDG_E_64, 2, 30, -1, -1, 8, 0, 0, 0x1e027981U, 0x00000008U, 0x0c1e1b00U, 0, "LDG.E.64 R2, desc[UR8][R30.64]" }, + { BW_IR_LDG_E_64, 28, 30, -1, -1, 8, 0x100, 0, 0x1e1c7981U, 0x00010008U, 0x0c1e1b00U, 0, "LDG.E.64 R28, desc[UR8][R30.64+0x100]" }, + { BW_IR_LDG_E_128, 8, 32, -1, -1, 8, 0, 0, 0x20087981U, 0x00000008U, 0x0c1e1d00U, 0, "LDG.E.128 R8, desc[UR8][R32.64]" }, + { BW_IR_LDG_E_128, 20, 32, -1, -1, 8, 0x1000, 0, 0x20147981U, 0x00100008U, 0x0c1e1d00U, 0, "LDG.E.128 R20, desc[UR8][R32.64+0x1000]" }, + { BW_IR_HMMA_BF16, 8, 12, 6, 8, -1, 0, 0, 0x0c08723cU, 0x00000006U, 0x00041808U, 0, "HMMA.16816.F32.BF16 R8, R12, R6, R8" }, + { BW_IR_NOP, -1, -1, -1, -1, -1, 0, 0x000fdc00U, 0x00007918U, 0, 0, 0x000fdc00U, "NOP (stall 14)" }, + }; + for (int i = 0; i < (int)(sizeof fx / sizeof fx[0]); i++) { + BlackwellIRInsn in; + memset(&in, 0, sizeof in); + in.op = fx[i].op; in.dst_vreg = fx[i].d; in.src1_vreg = fx[i].a; in.src2_vreg = fx[i].b; + in.src3_vreg = fx[i].c; in.ureg = fx[i].u; in.imm = fx[i].imm; in.control = fx[i].control; + uint32_t w[4]; + if (omega_blackwell_encode_one(&in, w) != 0) return -(2 * i + 1); + if (w[0] != fx[i].w0 || w[1] != fx[i].w1 || w[2] != fx[i].w2 || (fx[i].w3 && w[3] != fx[i].w3)) return -(2 * i + 2); + } + /* offsets outside the signed 24-bit field are refused, not wrapped */ + BlackwellIRInsn big; + memset(&big, 0, sizeof big); + big.op = BW_IR_LDG_E_64; big.dst_vreg = 2; big.src1_vreg = 30; big.src2_vreg = -1; big.src3_vreg = -1; big.ureg = 8; + big.imm = 1u << 23; + uint32_t w[4]; + if (omega_blackwell_encode_one(&big, w) == 0) return -100; + big.imm = (uint32_t)-(1 << 23); + if (omega_blackwell_encode_one(&big, w) != 0) return -101; + return 0; +} + /* omega #308: BSSY / BSYNC golden words. Oracle: nvcc 13.0.88 -arch=sm_121 -cubin -O3 on the * kernels in docs/gpu-reconvergence-308.md, cuobjdump -sass (low 64-bit word, then high; the * top 23 bits of the high word are the control word and are not compared here): @@ -1630,6 +1689,227 @@ int omega_blackwell_codegen_matmul_tensor_loop(const OmegaMatMulSpec *spec, uint return 0; } +/* ---- omega #328: fragment-major tensor matmul ---------------------------------------- + * + * The looped kernel above reads B as four scattered 2-byte loads per K step and waits for + * them before the next step (pack waits SB2), so each warp has one 256-byte step in flight; + * at m = 1 on GB10 that streams the weights at about 28 GB/s (omega #328 measurement). + * Here A and B arrive in the order HMMA.16816 consumes them (omega_matmul_frag_pack_a / _b): + * A block (mt, ks): 512 bytes, lane L at L*16: a0..a7, one LDG.E.128 per step; + * B block (nt, ks): 256 bytes, lane L at L*8: b0..b3, one LDG.E.64 per step; + * consecutive K steps of one tile are adjacent, so a warp streams its column tile. Each loop + * pass issues `unroll` steps of loads at immediate offsets (A on SB2, B on SB4), then runs the HMMA + * chain: every HMMA waits SB2 and SB4 (stall 15) and a NOP (stall 14) follows it, the 29 cycles nvcc + * puts between dependent HMMA.16816 (omega_blackwell_verify_codegen_fixtures_fragmm). + * The cursors advance only after the chain, when every load has landed (the HMMAs waited + * SB2 and SB4), as in the looped kernel. Stores, the column-tile loop and the C layout (row-major + * f32) are the looped kernel's. */ +/* A loads set SB2 and B loads SB4 (nvcc spreads loads over several scoreboards too); at the + * product unroll of 4 that is 4 loads per scoreboard per pass. The scoreboard depth is not + * documented to us; splitting did not make unroll 8 complete (omega_matmul_frag_unroll). */ +#define BW_FRAG_CTRL_LDG_A 0x000ea800u /* stall 4, yield, sets SB2 (nvcc word) */ +#define BW_FRAG_CTRL_LDG_B 0x000f2800u /* stall 4, yield, sets SB4 (nvcc word) */ +#define BW_FRAG_CTRL_HMMA 0x014fde00u /* stall 15, waits SB2 and SB4 (nvcc: dependent HMMA chain) */ +#define BW_FRAG_CTRL_NOP 0x000fdc00u /* stall 14 (nvcc, between dependent HMMAs) */ + +static void bw_live_until(BlackwellIRProgram *prog, int vreg, int last_idx) { + if (prog->regalloc.intervals[vreg].last_use < last_idx) prog->regalloc.intervals[vreg].last_use = last_idx; +} + +int omega_blackwell_codegen_matmul_frag_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, + 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 (unroll != 1 && unroll != 2 && unroll != 4 && unroll != 8) return -1; + const uint32_t K = spec->k, N = spec->n, NT = N / 8, KS = K / 16; + if (KS % unroll != 0) return -1; + const uint32_t ITERS = KS / unroll; + if (grid_x == 0 || grid_x > NT) return -1; + if (mutant && ITERS < 2) return -1; + /* every index and immediate below must fit 32 bits (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 * KS * 256u > 0xffffffffull || (uint64_t)N * 32u > 0xffffffffull || + (uint64_t)OMEGA_BW_MATMUL_MAX_M * N > 0xffffffffull) + 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_tc1 = omega_bw_ir_alloc_vreg64(prog), v_tc3 = omega_bw_ir_alloc_vreg64(prog); + int v_rd = omega_bw_ir_alloc_vreg128(prog); + int v_ra[8], v_rb[8]; + for (uint32_t u = 0; u < unroll; u++) { v_ra[u] = omega_bw_ir_alloc_vreg128(prog); v_rb[u] = omega_bw_ir_alloc_vreg64(prog); } + if (v_rb[unroll - 1] < 0 || v_ra[unroll - 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 looped kernel) */ + 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); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_r0, .src1_vreg = v_ctaid_y, .src2_vreg = -1, .imm = 16, .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 * KS * 32 + tid) * 16: this CTA's row tile, 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 = 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); + BW_APPEND(prog, .op = BW_IR_IMAD, .dst_vreg = v_col, .src1_vreg = v_ctaid_x, .src2_vreg = -1, .imm = 1, .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-tile loop ---- */ + const int L_col = (int)prog->count; + BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 0, .control = CTRL_MOVRZ_WAIT3); + BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 1); + BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 2); + BW_APPEND(prog, .op = BW_IR_MOV_RZ, .dst_vreg = v_rd, .dst_subreg = 3); + 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 loads, then the HMMA chain ---- */ + const int L_k = (int)prog->count; + for (uint32_t u = 0; u < unroll; u++) { + BW_APPEND(prog, .op = BW_IR_LDG_E_128, .dst_vreg = v_ra[u], .src1_vreg = v_pac, .src2_vreg = -1, .src3_vreg = -1, + .ureg = uv_desc, .imm = u * 512u, .control = BW_FRAG_CTRL_LDG_A); + BW_APPEND(prog, .op = BW_IR_LDG_E_64, .dst_vreg = v_rb[u], .src1_vreg = v_pbc, .src2_vreg = -1, .src3_vreg = -1, + .ureg = uv_desc, .imm = u * 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++) { + BW_APPEND(prog, .op = hmma, .dst_vreg = v_rd, .src1_vreg = v_ra[u], .src2_vreg = v_rb[u], .src3_vreg = v_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 16x8 tile (as the looped kernel) ---- */ + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tc1, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 4, .src3_vreg = v_pc0); + BW_APPEND(prog, .op = BW_IR_IMAD_WIDE, .dst_vreg = v_tc3, .src1_vreg = v_c1, .src2_vreg = -1, .imm = 4, .src3_vreg = v_pc1); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc0, .src2_vreg = v_rd, .src2_subreg = 0, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_tc1, .src2_vreg = v_rd, .src2_subreg = 1, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_pc1, .src2_vreg = v_rd, .src2_subreg = 2, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); + BW_APPEND(prog, .op = BW_IR_STG_E, .src1_vreg = v_tc3, .src2_vreg = v_rd, .src2_subreg = 3, .src3_vreg = -1, .ureg = uv_desc, .control = CTRL_STG_RDBAR3); + /* next column tile: col += grid_x ; C by grid_x*8 f32 (waits SB3 first) ; B by grid_x 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, .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 * 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 * 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 * 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 (the allocator knows no back edges). + * Only these are read in a later pass than the one that wrote them; prologue temporaries + * are dead before the first loop and may be reused. K-loop temporaries (fragments) are + * kept to the end of the pass, as in the looped kernel. */ + const int col_carried[] = { v_c1, v_col, v_pa, v_pbcol, v_pc0, v_pc1, v_rd }; + for (size_t i = 0; i < sizeof col_carried / sizeof col_carried[0]; i++) bw_live_until(prog, col_carried[i], 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(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, + int mutant, OmegaBlackwellKernel *kernel) { + if (!spec || !kernel) return -1; + BlackwellIRProgram prog; + if (omega_blackwell_codegen_matmul_frag_prog(spec, grid_x, unroll, mutant, &prog) != 0) return -1; + size_t max_bytes = 4096; + 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; +} + +/* Capped at 4: unroll 8 (72 registers, 16 loads per pass) never completed on the GB10, with all + * 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). */ +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; +} + +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) { + const uint32_t KS = kp / 16u; + memset(dst, 0, (size_t)mp * kp * sizeof *dst); + for (uint32_t r = 0; r < m; r++) { + const uint32_t mt = r / 16u, rr = r % 16u, group = rr % 8u, hi = rr / 8u; + for (uint32_t c = 0; c < k; c++) { + const uint32_t ks = c / 16u, cc = c % 16u, khi = cc / 8u, tig = (cc % 8u) / 2u, lo = cc % 2u; + const size_t lane = group * 4u + tig; + dst[(((size_t)mt * KS + ks) * 32u + lane) * 8u + khi * 4u + hi * 2u + lo] = a[(size_t)r * k + c]; + } + } +} + +void omega_matmul_frag_pack_b(const uint16_t *b, uint32_t k, uint32_t n, uint32_t kp, + uint32_t nt0, uint32_t nt1, uint16_t *dst) { + const uint32_t KS = kp / 16u; + memset(dst, 0, (size_t)(nt1 - nt0) * KS * 128u * sizeof *dst); + for (uint32_t row = 0; row < k; row++) { + const uint32_t ks = row / 16u, rr = row % 16u, khi = rr / 8u, tig = (rr % 8u) / 2u, lo = rr % 2u; + const uint16_t *src = &b[(size_t)row * n]; + for (uint32_t nt = nt0; nt < nt1; nt++) { + const uint32_t c0 = nt * 8u; + for (uint32_t group = 0; group < 8u && c0 + group < n; group++) { + const size_t lane = group * 4u + tig; + dst[(((size_t)(nt - nt0) * KS + ks) * 32u + lane) * 4u + khi * 2u + lo] = src[c0 + group]; + } + } + } +} + int omega_blackwell_verify_codegen_fixtures(void) { OmegaRegAlloc ra; memset(&ra, 0, sizeof(ra)); diff --git a/src/omega_blackwell_codegen.h b/src/omega_blackwell_codegen.h index 2358ea760..2ac9b8186 100644 --- a/src/omega_blackwell_codegen.h +++ b/src/omega_blackwell_codegen.h @@ -92,7 +92,12 @@ typedef enum { * continues as one. imm of BSSY = signed instruction delta to the join point (the * instruction after the BSYNC), like BW_IR_BRA; the encoder refuses deltas < 1. */ BW_IR_BSSY, - BW_IR_BSYNC + BW_IR_BSYNC, + /* Fragment-major matmul (omega #328, 2026-10-07), additive. Words matched against nvcc 13.0.88 + * -arch=sm_121 (omega_blackwell_verify_codegen_fixtures_fragmm). imm = signed byte offset, + * -2^23 .. 2^23-1; dst is a register pair (64) or an aligned quad (128). */ + BW_IR_LDG_E_64, /* LDG.E.64 Rd:Rd+1, desc[URd][Ra.64+imm] */ + BW_IR_LDG_E_128 /* LDG.E.128 Rd..Rd+3, desc[URd][Ra.64+imm] */ } BlackwellIROpcode; /* Barrier (reconvergence) registers: the sm_121 BSSY/BSYNC words carry a 4-bit barrier id @@ -210,6 +215,32 @@ int omega_blackwell_codegen_matmul_tensor_loop_prog(const OmegaMatMulSpec *spec, int omega_blackwell_codegen_matmul_tensor_loop(const OmegaMatMulSpec *spec, uint32_t grid_x, int mutant, OmegaBlackwellKernel *kernel); +/* omega #328: fragment-major tensor matmul. Same grid, C layout (row-major f32) and argument + * words as the looped kernel; A and B are in fragment order instead of row-major: + * A (mp x kp, from omega_matmul_frag_pack_a): block (mt, ks) of 256 bf16 at ((mt*KS + ks)*32 + * + lane)*8, lane = group*4 + tig, holding A[r][c], A[r][c+1], A[r+8][c], A[r+8][c+1], + * A[r][c+8], A[r][c+9], A[r+8][c+8], A[r+8][c+9] with r = 16mt + group, c = 16ks + 2tig; + * B (kp x np, from omega_matmul_frag_pack_b): block (nt, ks) of 128 bf16 at ((nt*KS + ks)*32 + * + lane)*4 holding B[k][j], B[k+1][j], B[k+8][j], B[k+9][j] with k = 16ks + 2tig, + * j = 8nt + group; KS = kp / 16, entries outside the real matrix are zero. + * One LDG.E.128 (A) and one LDG.E.64 (B) per K step; `unroll` (1, 2, 4 or 8, dividing KS) + * steps of loads are issued before the HMMA chain. mutant != 0 drops the last pass (test + * oracle only). Each tile occupies 16*kp (A) and 8*kp (B) elements, so a row-tile offset into + * A is the same in bytes as for row-major A. */ +int omega_blackwell_codegen_matmul_frag_prog(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, + int mutant, BlackwellIRProgram *prog); +int omega_blackwell_codegen_matmul_frag(const OmegaMatMulSpec *spec, uint32_t grid_x, uint32_t unroll, + int mutant, OmegaBlackwellKernel *kernel); +/* 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); +/* 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 + * written; it is the part of the packed tensor starting at element nt0 * kp * 8. */ +void omega_matmul_frag_pack_b(const uint16_t *b, uint32_t k, uint32_t n, uint32_t kp, + uint32_t nt0, uint32_t nt1, uint16_t *dst); + /* Unit test for instruction encoding bitfield fixtures (Gate 3) */ int omega_blackwell_verify_codegen_fixtures(void); @@ -219,6 +250,9 @@ int omega_blackwell_verify_codegen_fixtures_fb1cut4(void); /* Prime race cut: golden words for LOP3_LUT, SHF_L_U32, IMAD_HI_U32 (0 = pass) */ int omega_blackwell_verify_codegen_fixtures_intops(void); +/* omega #328: golden words for LDG.E.64 / LDG.E.128 with offsets and the timed NOP (0 = pass) */ +int omega_blackwell_verify_codegen_fixtures_fragmm(void); + /* omega #308: golden words for BSSY / BSYNC (nvcc 13.0.88 sm_121 oracle), plus the * encoder's refusals (negative delta, barrier id > 15). 0 = pass. */ int omega_blackwell_verify_codegen_fixtures_reconv(void); diff --git a/src/omega_gpu_matmul_api.c b/src/omega_gpu_matmul_api.c index d27f2912f..73edb287d 100644 --- a/src/omega_gpu_matmul_api.c +++ b/src/omega_gpu_matmul_api.c @@ -26,13 +26,14 @@ #include <time.h> #define CACHE_SLOTS 8 +#define FRAG_PACK_TILES 64u /* column tiles per host packing chunk at upload */ #define MARKER_TIMEOUT_MS 20000ull /* a 1x2048x128256 call is ~0.1 s; 20 s is a stall, not a slow run */ #define POISON_F32 (-999.0f) #define ORACLE_TOL 1e-5 struct OmegaGpuTensor { uint32_t k, n, kp, np; - NvrmMem mem; /* kp x np bf16 */ + NvrmMem mem; /* kp x np bf16 in fragment order (omega_matmul_frag_pack_b) */ }; typedef struct { @@ -48,6 +49,7 @@ static struct { OmegaGpuScratch a_buf, c_buf; /* activation and result staging, grown to the high-water mark */ uint32_t cta_budget; int oracle, mutant; + uint32_t unroll; /* test override of the fragment kernel unroll; 0 = omega_matmul_frag_unroll */ bool hooked; CacheSlot cache[CACHE_SLOTS]; unsigned cache_next; @@ -129,6 +131,13 @@ void omega_gpu_matmul_test_set_mutant(int on) { omega_gpu_session_unlock(); } +void omega_gpu_matmul_test_set_unroll(uint32_t unroll) { + omega_gpu_session_lock(); + g.unroll = unroll; + cache_clear_locked(); + omega_gpu_session_unlock(); +} + /* ---- 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) { @@ -144,7 +153,11 @@ 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); - if (omega_blackwell_codegen_matmul_tensor_loop(&spec, grid_x, g.mutant, &kernel) != 0 || !kernel.code || kernel.code_size == 0) + uint32_t unroll = 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) 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; } @@ -187,14 +200,23 @@ int omega_gpu_tensor_upload_bf16(uint32_t k, uint32_t n, const uint16_t *b, Omeg OmegaGpuTensor *t = calloc(1, sizeof *t); if (!t) return OMEGA_GPU_MATMUL_BAD_ARGS; t->k = k; t->n = n; t->kp = round_up(k, OMEGA_GPU_MATMUL_TILE_K); t->np = round_up(n, OMEGA_GPU_MATMUL_TILE_N); + /* fragment order (omega_matmul_frag_pack_b), packed on the host FRAG_PACK_TILES column + * tiles at a time and copied in order into the uncached device buffer */ + const uint32_t nt = t->np / 8u; + uint16_t *h = malloc((size_t)FRAG_PACK_TILES * t->kp * 8u * sizeof *h); + if (!h) { free(t); return OMEGA_GPU_MATMUL_BAD_ARGS; } int rc = OMEGA_GPU_MATMUL_OK; omega_gpu_session_lock(); if (!dev_open_locked() || omega_gpu_session_alloc((size_t)t->kp * t->np * 2u, &t->mem) != 0) rc = OMEGA_GPU_MATMUL_CHIP_FAIL; omega_gpu_session_unlock(); - if (rc != OMEGA_GPU_MATMUL_OK) { free(t); return rc; } + if (rc != OMEGA_GPU_MATMUL_OK) { free(h); free(t); return rc; } uint16_t *d = t->mem.cpu; - if (t->np != n || t->kp != k) memset(d, 0, (size_t)t->kp * t->np * 2u); - for (uint32_t r = 0; r < k; r++) memcpy(&d[(size_t)r * t->np], &b[(size_t)r * n], (size_t)n * 2u); + for (uint32_t nt0 = 0; nt0 < nt; nt0 += FRAG_PACK_TILES) { + const uint32_t nt1 = nt - nt0 < FRAG_PACK_TILES ? nt : nt0 + FRAG_PACK_TILES; + omega_matmul_frag_pack_b(b, k, n, t->kp, nt0, nt1, h); + memcpy(&d[(size_t)nt0 * t->kp * 8u], h, (size_t)(nt1 - nt0) * t->kp * 8u * sizeof *h); + } + free(h); __asm__ volatile("dsb sy" ::: "memory"); *out = t; return OMEGA_GPU_MATMUL_OK; @@ -244,10 +266,13 @@ static int matmul_core(uint32_t m, const uint16_t *a, const OmegaGpuTensor *b, f if (omega_gpu_session_scratch(&g.a_buf, (size_t)mp * kp * 2u) != 0 || omega_gpu_session_scratch(&g.c_buf, (size_t)mp * np * 4u) != 0) { omega_gpu_session_unlock(); return OMEGA_GPU_MATMUL_CHIP_FAIL; } - /* activation rows in, zero padded; result rows poisoned */ - uint16_t *ad = g.a_buf.mem.cpu; - if (mp != m || kp != k) memset(ad, 0, (size_t)mp * kp * 2u); - for (uint32_t r = 0; r < m; r++) memcpy(&ad[(size_t)r * kp], &a[(size_t)r * k], (size_t)k * 2u); + /* activation rows in, fragment order (omega_matmul_frag_pack_a), zero padded, packed on the + * host and copied in order into the uncached buffer; result rows poisoned */ + uint16_t *ah = malloc((size_t)mp * kp * sizeof *ah); + if (!ah) { omega_gpu_session_unlock(); return OMEGA_GPU_MATMUL_BAD_ARGS; } + omega_matmul_frag_pack_a(a, m, k, mp, kp, ah); + memcpy(g.a_buf.mem.cpu, ah, (size_t)mp * kp * sizeof *ah); + free(ah); float *cd = g.c_buf.mem.cpu; for (size_t i = 0; i < (size_t)m * np; i++) cd[i] = POISON_F32; __asm__ volatile("dsb sy" ::: "memory"); diff --git a/src/omega_gpu_matmul_api.h b/src/omega_gpu_matmul_api.h index d0ebdb3d3..ccd5203a2 100644 --- a/src/omega_gpu_matmul_api.h +++ b/src/omega_gpu_matmul_api.h @@ -139,6 +139,9 @@ void omega_gpu_matmul_set_oracle(int on); * dropped) so the parity test can prove it catches a wrong kernel. Clears the * kernel cache. Production code must never call this. */ void omega_gpu_matmul_test_set_mutant(int on); +/* Test only: force the fragment kernel unroll (1, 2, 4 or 8; it must divide kp / 16 or the call + * fails CODEGEN_FAIL); 0 restores the automatic choice. Clears the kernel cache. */ +void omega_gpu_matmul_test_set_unroll(uint32_t unroll); #ifdef __cplusplus } diff --git a/tests/.crumb b/tests/.crumb index 34cac3133..d923b35b6 100644 --- a/tests/.crumb +++ b/tests/.crumb @@ -284,14 +284,14 @@ } ], "children_root": "sha256:c630c07cfd744174cf35b42b04d96bbb15dfedb76557947943c412d3219586c5", - "compiled_at": "2026-10-07T07:22:31Z", + "compiled_at": "2026-10-07T11:04:26Z", "compiler": "crumb-compile/1", - "digest": "sha256:0dc1710b156c18dc1ad2ec7c311476c66184878ab9dd9dd250c879748cd0c5b4", + "digest": "sha256:36d064d2eb869f98033575e088496ed4b6fbdb1158364c62046ef5b6b8354386", "evidence_files": 11, "evidence_root": "sha256:d5c1bceb4d57ef0eb42797c58801b5926509711614c3e38bb7105b6efa3942f2", "files": 818, - "generated_at_commit": "8fa37681cc28fdd7a8fb235c53c2ce746615be4b", - "source_tree": "sha256:aeaaa2b1fd17afee7fde13b6d5b297e3a07375a1e64f4023228678d1997c4727" + "generated_at_commit": "dfc602b8e0542ffdf74e361b57cecc859f74e0f1", + "source_tree": "sha256:c1af9f2c8e74ac619080109145f41831e831e42b4b907073d3c26052f0396430" }, "owner_lane": "", "provenance": { diff --git a/tests/gpu_matmul_api_test.c b/tests/gpu_matmul_api_test.c index aea419e98..484feae28 100644 --- a/tests/gpu_matmul_api_test.c +++ b/tests/gpu_matmul_api_test.c @@ -7,6 +7,7 @@ * exit 0 when every shape caught it ("mutant killed") * ./gpu_matmul_api_test --cta-budget N ... run with a different CTA budget (I42 experiment) * ./gpu_matmul_api_test --spin-us N ... marker-wait spin window for every launch (omega#328) + * ./gpu_matmul_api_test --unroll N ... force the fragment kernel unroll (omega #328 experiment) * * Shapes come from OMEGA_GPU_MATMUL_SHAPES ("m,k,n;m,k,n;..."), default below. * Pass rule per shape: rc OK, parity verified by the API oracle, end-to-end error < 1e-5 relative to @@ -59,6 +60,87 @@ static double e2e_max_rel(uint32_t m, uint32_t k, uint32_t n, const float *a, co return worst; } +/* omega #328 fragment-major kernel, host only. The unpack below is written from the PTX ISA + * table for mma.m16n8k16 (.bf16 A: a0,a1 row g cols 2t,2t+1; a2,a3 row g+8; a4,a5 row g cols + * 2t+8,2t+9; a6,a7 row g+8. B: b0,b1 rows 2t,2t+1 col g; b2,b3 rows 2t+8,2t+9; g = lane>>2, + * t = lane&3), independently of the packers: pack then unpack must give the matrix back. */ +static void frag_host_checks(void) { + CHECK(omega_blackwell_verify_codegen_fixtures_fragmm() == 0, "fragment matmul golden words (rc %d)", omega_blackwell_verify_codegen_fixtures_fragmm()); + struct { uint32_t m, k, n; } sh[] = { {1, 16, 8}, {1, 40, 13}, {17, 48, 24}, {33, 2560, 72}, {5, 160, 1000} }; + for (size_t s = 0; s < sizeof sh / sizeof sh[0]; s++) { + const uint32_t m = sh[s].m, k = sh[s].k, n = sh[s].n; + const uint32_t mp = (m + 15) / 16 * 16, kp = (k + 15) / 16 * 16, np = (n + 7) / 8 * 8, KS = kp / 16, NT = np / 8; + uint16_t *a = malloc((size_t)m * k * 2), *b = malloc((size_t)k * n * 2); + uint16_t *pa = malloc((size_t)mp * kp * 2), *pb = malloc((size_t)kp * np * 2); + uint32_t seed = 7 + (uint32_t)s; + for (size_t i = 0; i < (size_t)m * k; i++) a[i] = (uint16_t)(1 + lcg(&seed) % 60000); + for (size_t i = 0; i < (size_t)k * n; i++) b[i] = (uint16_t)(1 + lcg(&seed) % 60000); + omega_matmul_frag_pack_a(a, m, k, mp, kp, pa); + /* B in two uneven chunks, as the upload does in FRAG_PACK_TILES steps */ + uint32_t split = NT / 2; + omega_matmul_frag_pack_b(b, k, n, kp, 0, split, pb); + omega_matmul_frag_pack_b(b, k, n, kp, split, NT, pb + (size_t)split * kp * 8); + size_t bad_a = 0, bad_b = 0; + for (uint32_t mt = 0; mt < mp / 16; mt++) + for (uint32_t ks = 0; ks < KS; ks++) + for (uint32_t lane = 0; lane < 32; lane++) { + const uint32_t gg = lane >> 2, t = lane & 3; + const uint32_t rows[8] = { gg, gg, gg + 8, gg + 8, gg, gg, gg + 8, gg + 8 }; + const uint32_t cols[8] = { 2 * t, 2 * t + 1, 2 * t, 2 * t + 1, 2 * t + 8, 2 * t + 9, 2 * t + 8, 2 * t + 9 }; + for (int e = 0; e < 8; e++) { + uint32_t r = mt * 16 + rows[e], c = ks * 16 + cols[e]; + uint16_t want = (r < m && c < k) ? a[(size_t)r * k + c] : 0; + if (pa[(((size_t)mt * KS + ks) * 32 + lane) * 8 + e] != want) bad_a++; + } + } + for (uint32_t nt = 0; nt < NT; nt++) + for (uint32_t ks = 0; ks < KS; ks++) + for (uint32_t lane = 0; lane < 32; lane++) { + const uint32_t gg = lane >> 2, t = lane & 3; + const uint32_t rows[4] = { 2 * t, 2 * t + 1, 2 * t + 8, 2 * t + 9 }; + for (int e = 0; e < 4; e++) { + uint32_t r = ks * 16 + rows[e], c = nt * 8 + gg; + uint16_t want = (r < k && c < n) ? b[(size_t)r * n + c] : 0; + if (pb[(((size_t)nt * KS + ks) * 32 + lane) * 4 + e] != want) bad_b++; + } + } + CHECK(bad_a == 0 && bad_b == 0, "fragment pack round trip %ux%ux%u (A bad %zu, B bad %zu)", m, k, n, bad_a, bad_b); + free(a); free(b); free(pa); free(pb); + } + /* the kernel generates for every padded Qwen3-4B and Llama-3.2-1B decode shape at the API's unroll */ + struct { uint32_t k, n, gx; } ok[] = { {16, 8, 1}, {2560, 4096, 256}, {2560, 1024, 128}, {4096, 2560, 256}, {2560, 9728, 256}, + {9728, 2560, 256}, {2560, 151936, 256}, {2048, 8192, 64}, {8192, 2048, 64}, + {2048, 128256, 64}, {16384, 262144, 64}, {48, 16, 2} }; + for (size_t i = 0; i < sizeof ok / sizeof ok[0]; i++) { + OmegaMatMulSpec spec; OmegaBlackwellKernel kernel; memset(&kernel, 0, sizeof kernel); + const uint32_t u = omega_matmul_frag_unroll(ok[i].k); + int rc = omega_matmul_spec_init(&spec, 16, ok[i].k, ok[i].n, OMEGA_MATMUL_PRECISION_BF16) == 0 && + omega_blackwell_codegen_matmul_frag(&spec, ok[i].gx, u, 0, &kernel) == 0 && kernel.code_size > 0; + CHECK(rc, "fragment kernel K=%u N=%u gx=%u unroll=%u generated", ok[i].k, ok[i].n, ok[i].gx, u); + CHECK(kernel.gpr_count <= 64, "fragment kernel K=%u N=%u unroll=%u fits 64 registers (got %u)", ok[i].k, ok[i].n, u, kernel.gpr_count); + if (i == 1) printf("fragment kernel K=2560 N=4096 unroll %u: %zu instructions, %u registers\n", u, kernel.insn_count, kernel.gpr_count); + OmegaBlackwellKernel mut; memset(&mut, 0, sizeof mut); + if (omega_blackwell_codegen_matmul_frag(&spec, ok[i].gx, u, 1, &mut) == 0) { + CHECK(memcmp(mut.code_digest, kernel.code_digest, 32) != 0, "fragment mutant differs K=%u N=%u", ok[i].k, ok[i].n); + omega_blackwell_kernel_free(&mut); + } else CHECK(ok[i].k / 16 / u < 2, "fragment mutant refused only with one pass (K=%u)", ok[i].k); + omega_blackwell_kernel_free(&kernel); + } + CHECK(omega_matmul_frag_unroll(2560) == 4 && omega_matmul_frag_unroll(48) == 1 && omega_matmul_frag_unroll(64) == 4 && + omega_matmul_frag_unroll(32) == 2 && omega_matmul_frag_unroll(128) == 4, "unroll choice (capped at 4)"); + { /* unroll 8 still generates (experiments) */ + OmegaMatMulSpec s8; OmegaBlackwellKernel k8; memset(&k8, 0, sizeof k8); + CHECK(omega_matmul_spec_init(&s8, 16, 2560, 4096, OMEGA_MATMUL_PRECISION_BF16) == 0 && + omega_blackwell_codegen_matmul_frag(&s8, 256, 8, 0, &k8) == 0 && k8.code_size > 0, "unroll 8 generates"); + omega_blackwell_kernel_free(&k8); + } + OmegaMatMulSpec spec; OmegaBlackwellKernel kernel; memset(&kernel, 0, sizeof kernel); + CHECK(omega_matmul_spec_init(&spec, 16, 48, 16, OMEGA_MATMUL_PRECISION_BF16) == 0 && + 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"); +} + static void host_only(void) { float a[16 * 16] = {0}, b[16 * 16] = {0}, c[16 * 16] = {0}; OmegaGpuMatmulInfo info; @@ -106,6 +188,7 @@ static void host_only(void) { memset(&kernel, 0, sizeof kernel); CHECK(omega_matmul_spec_init(&spec, 16, 16, 8, OMEGA_MATMUL_PRECISION_BF16) == 0 && omega_blackwell_codegen_matmul(&spec, &kernel) == 0 && kernel.code_size > 0, "16x16x8 single-tile kernel generated"); omega_blackwell_kernel_free(&kernel); + frag_host_checks(); omega_gpu_matmul_cache_clear(); } @@ -254,6 +337,7 @@ int main(int argc, char **argv) { else if (strcmp(argv[i], "--spin-us") == 0 && i + 1 < argc) { if (omega_gpu_session_set_spin_us((uint32_t)strtoul(argv[++i], NULL, 10)) != 0) { fprintf(stderr, "--spin-us out of range\n"); return 2; } } + else if (strcmp(argv[i], "--unroll") == 0 && i + 1 < argc) omega_gpu_matmul_test_set_unroll((uint32_t)strtoul(argv[++i], NULL, 10)); else if (strcmp(argv[i], "--out") == 0 && i + 1 < argc) out_path = argv[++i]; } const char *shapes = getenv("OMEGA_GPU_MATMUL_SHAPES");