CUDApp
CUDA++: AIEN’s GPU machine-code toolchain, runtime and driver client for NVIDIA GPUs without the CUDA toolkit. Hardware coverage and driver replacement limits are explicit.
Brought by drakestapleton
CUDApp / src / omega_blackwell_codegen_qg.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* omega_blackwell_codegen_qg.c: native FP8 prefill kernels (W-L1G). See the header. */ |
| 3 | #include "omega_blackwell_codegen_qg.h" |
| 4 | #include "cudapp_native.h" |
| 5 | #ifndef CUDAPP_MIGRATION_PENDING_C_CODEGEN_QG |
| 6 | #define CUDAPP_MIGRATION_PENDING_C_CODEGEN_QG 0 |
| 7 | #endif |
| 8 | #if CUDAPP_MIGRATION_PENDING_C_CODEGEN_QG |
| 9 | #include "omega_bw_em.h" |
| 10 | #include "omega_bw_em_q.h" |
| 11 | |
| 12 | static void fmul_r(Em *e, int d, unsigned dsub, int a, unsigned asub, int b, unsigned bsub) { |
| 13 | BlackwellIRInsn i = I0(BW_IR_FMUL); i.dst_vreg = d; i.dst_subreg = (uint8_t)dsub; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.src2_vreg = b; i.src2_subreg = (uint8_t)bsub; emit(e, i, K_FIXED); |
| 14 | } |
| 15 | static void fmax_r(Em *e, int d, int a, unsigned asub, int b, unsigned bsub) { |
| 16 | BlackwellIRInsn i = I0(BW_IR_FMNMX_MAX); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.src2_vreg = b; i.src2_subreg = (uint8_t)bsub; emit(e, i, K_FIXED); |
| 17 | } |
| 18 | |
| 19 | int omega_pq_spec_ok(const OmegaPqSpec *s) { |
| 20 | if (!s || s->k < 128u || s->k > 32768u || s->k % 128u) return 0; |
| 21 | const uint32_t kb = s->k / 128u, g = s->groups ? s->groups : kb; |
| 22 | return g <= 32u && kb % g == 0 && s->in_type <= OMEGA_QG_IN_BF16 && s->mutant >= 0 && s->mutant <= 3; |
| 23 | } |
| 24 | |
| 25 | int omega_blackwell_codegen_pq_prog(const OmegaPqSpec *spec, BlackwellIRProgram *prog) { |
| 26 | if (!omega_pq_spec_ok(spec) || !prog) return -1; |
| 27 | const uint32_t K = spec->k, KB = K / 128u; |
| 28 | omega_bw_ir_init(prog); |
| 29 | Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; |
| 30 | int udesc = UV64(e), px = V64(e), pq = V64(e), ps = V64(e), tid = V(e), row = V(e), warp = V(e); |
| 31 | int v = V128(e), w = V64(e), a = V128(e), m = V(e), t = V(e), fl = V(e), E = V(e), M = V(e), c = V(e), k1 = V(e), kneg = V(e), k262 = V(e); |
| 32 | int sb = V(e), ib = V(e), d0 = V(e), d1 = V(e), word = V(e); |
| 33 | if (e->err) return -1; |
| 34 | ldcu64(e, udesc, 0x358); ldc64(e, px, 0x380); ldc64(e, pq, 0x388); ldc64(e, ps, 0x390); |
| 35 | s2r(e, tid, BW_SR_TID_X); s2r(e, row, BW_SR_CTAID_X); |
| 36 | if (spec->groups) { /* row split over ctaid.y: this CTA's groups start at y G */ |
| 37 | int cy = V(e); if (e->err) return -1; |
| 38 | s2r(e, cy, BW_SR_CTAID_Y); if (spec->mutant != 3) imadi(e, tid, cy, 0, 32u * spec->groups, tid); |
| 39 | } |
| 40 | shr(e, warp, tid, 0, 5); |
| 41 | if (spec->in_type == OMEGA_QG_IN_F32) { imadw(e, px, row, K * 4u, px); imadw(e, px, tid, 16u, px); ldg_wide(e, BW_IR_LDG_E_128, v, px, 0, udesc, 1); } |
| 42 | else { imadw(e, px, row, K * 2u, px); imadw(e, px, tid, 8u, px); ldg_wide(e, BW_IR_LDG_E_64, w, px, 0, udesc, 1); } |
| 43 | imadw(e, pq, row, K, pq); imadw(e, pq, tid, 4u, pq); |
| 44 | imadw(e, ps, row, KB * 4u, ps); imadw(e, ps, warp, 4u, ps); |
| 45 | movi(e, fl, 0x0d800000u); /* 2^-100 */ |
| 46 | movi(e, k1, 0x001fffffu); |
| 47 | movi(e, kneg, 0xfc000000u); /* -8 << 23 */ |
| 48 | movi(e, k262, 0x83000000u); /* 262 << 23 */ |
| 49 | if (spec->in_type == OMEGA_QG_IN_BF16) { /* bf16 pairs -> f32: element 2j in the low half of word j */ |
| 50 | for (unsigned j = 0; j < 2; j++) { |
| 51 | BlackwellIRInsn lo = I0(BW_IR_IMAD); lo.dst_vreg = v; lo.dst_subreg = (uint8_t)(2u * j); lo.src1_vreg = w; lo.src1_subreg = (uint8_t)j; lo.imm = 65536u; lo.src3_vreg = NOREG; emit(e, lo, K_FIXED); |
| 52 | BlackwellIRInsn hi = I0(BW_IR_LOP3_AND); hi.dst_vreg = v; hi.dst_subreg = (uint8_t)(2u * j + 1u); hi.src1_vreg = w; hi.src1_subreg = (uint8_t)j; hi.imm = 0xffff0000u; emit(e, hi, K_FIXED); |
| 53 | } |
| 54 | } |
| 55 | for (unsigned q = 0; q < 4; q++) { BlackwellIRInsn an = I0(BW_IR_LOP3_AND); an.dst_vreg = a; an.dst_subreg = (uint8_t)q; an.src1_vreg = v; an.src1_subreg = (uint8_t)q; an.imm = 0x7fffffffu; emit(e, an, K_FIXED); } |
| 56 | fmax_r(e, m, a, 0, a, 1); fmax_r(e, m, m, 0, a, 2); fmax_r(e, m, m, 0, a, 3); fmax_r(e, m, m, 0, fl, 0); |
| 57 | if (spec->mutant != 1) for (uint32_t l = 16; l >= 1; l >>= 1) { shfl_bfly(e, t, m, l); fmax_r(e, m, m, 0, t, 0); } |
| 58 | shr(e, E, m, 0, 23); |
| 59 | andi(e, M, m, 0, 0x007fffffu); |
| 60 | if (spec->mutant == 2) movi(e, c, 0); |
| 61 | else { imadi(e, c, M, 0, 1u, k1); shr(e, c, c, 0, 23); } |
| 62 | iadd(e, E, E, c); /* E + c */ |
| 63 | imadi(e, sb, E, 0, 1u << 23, kneg); /* scale bits (E + c - 8) << 23 */ |
| 64 | imadi(e, ib, E, 0, 0xff800000u, k262); /* inv bits (262 - E - c) << 23 */ |
| 65 | for (unsigned q = 0; q < 4; q++) fmul_r(e, v, q, v, q, ib, 0); |
| 66 | f2e4(e, d0, v, 1, v, 0); |
| 67 | f2e4(e, d1, v, 3, v, 2); |
| 68 | imadi(e, word, d1, 0, 65536u, d0); |
| 69 | stg(e, pq, 0, word, udesc); |
| 70 | stg(e, ps, 0, sb, udesc); |
| 71 | tail(e); |
| 72 | if (e->err) return -1; |
| 73 | return omega_bw_regalloc_solve(prog); |
| 74 | } |
| 75 | |
| 76 | /* ======================= grouped GEMM on QMMA (see the header) ======================= */ |
| 77 | int omega_qg_spec_ok(const OmegaQgSpec *s) { |
| 78 | if (!s) return 0; |
| 79 | if (s->k < 128u || s->k > 32768u || s->k % 128u) return 0; /* the k loop counts at run time: no k-sized immediate */ |
| 80 | uint64_t shape = 0; |
| 81 | if (omega_native_qgshape(&shape, s->rt, s->warps, s->n, s->epi) != OMEGA_OK || shape != 1) return 0; |
| 82 | if (s->epi > OMEGA_QG_EPI_SWIGLU || s->direct > 1 || s->wait > 120u) return 0; |
| 83 | if (s->epi == OMEGA_QG_EPI_SWIGLU && s->n % 256u) return 0; /* gate and up halves start on a 128-row scale block */ |
| 84 | if (s->n == 0 || s->n % (s->warps * 16u)) return 0; |
| 85 | if ((16u * s->rt * 4u) % (32u * s->warps)) return 0; /* whole 16-byte staging slots per thread */ |
| 86 | if (s->mutant < 0 || s->mutant > 4) return 0; |
| 87 | uint64_t plan[64] = {0}, ok = 0; |
| 88 | return omega_native_qgorder(&ok, plan, 64, s->group, s->n / (s->warps * 16u), 1, 1) == OMEGA_OK && ok == 1; |
| 89 | } |
| 90 | uint32_t omega_qg_shared_bytes(const OmegaQgSpec *s) { return omega_qg_spec_ok(s) ? 2u * (16u * s->rt * 64u + 16u * s->rt * 4u) : 0u; } |
| 91 | uint32_t omega_qg_span(const OmegaQgSpec *s) { return omega_qg_spec_ok(s) ? s->warps * 16u : 0u; } |
| 92 | |
| 93 | static void ffma_s(Em *e, int d, unsigned dsub, int a, unsigned asub, int b, unsigned bsub, int c, unsigned csub) { |
| 94 | BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.dst_subreg = (uint8_t)dsub; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; |
| 95 | i.src2_vreg = b; i.src2_subreg = (uint8_t)bsub; i.src3_vreg = c; i.src3_subreg = (uint8_t)csub; emit(e, i, K_FIXED); |
| 96 | } |
| 97 | static void mufu_q(Em *e, BlackwellIROpcode op, int d, int a) { BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = a; emit(e, i, K_VAR); } |
| 98 | |
| 99 | /* Primitive adapter for the instruction stream owned by om/prefill-shape/order.om. |
| 100 | * It contains no coordinate mapping policy. Roles: cx, cy, temporary, tile count. */ |
| 101 | static int qg_order_emit(Em *e, const uint64_t p[64], const int role[4], int *branch) { |
| 102 | if (p[2] > 6) return -1; |
| 103 | for (uint64_t i = 0; i < p[2]; i++) { |
| 104 | const uint64_t *v = p + 3 + 5 * i; |
| 105 | if (v[1] > 3 || v[2] > 3 || v[3] > 3 || v[4] > UINT32_MAX) return -1; |
| 106 | const int d = role[v[1]], a = role[v[2]], b = role[v[3]]; |
| 107 | const uint32_t imm = (uint32_t)v[4]; |
| 108 | switch (v[0]) { |
| 109 | case 1: andi(e, d, a, 0, imm); break; |
| 110 | case 2: shr(e, d, a, 0, imm); break; |
| 111 | case 3: imadi(e, d, a, 0, imm, b); break; |
| 112 | case 4: ldc32(e, d, imm); break; |
| 113 | case 5: isetp_ge_u32(e, a, b); break; |
| 114 | case 6: if (!branch || *branch >= 0) return -1; *branch = bra_if_p0_fwd(e); break; |
| 115 | default: return -1; |
| 116 | } |
| 117 | } |
| 118 | return e->err ? -1 : 0; |
| 119 | } |
| 120 | |
| 121 | int omega_blackwell_codegen_qgemm_prog(const OmegaQgSpec *spec, BlackwellIRProgram *prog) { |
| 122 | if (!omega_qg_spec_ok(spec) || !prog) return -1; |
| 123 | const uint32_t K = spec->k, RT = spec->rt, W = spec->warps, T = 32u * W; |
| 124 | const uint32_t BM = 16u * RT, KB = K / 128u, SPAN = W * 16u, NJ = BM * 4u / T, ABUF = BM * 64u, BUF = ABUF + BM * 4u; |
| 125 | const uint32_t NS = spec->epi == OMEGA_QG_EPI_SWIGLU ? 2u : 1u; /* scale rows per warp: gate + up block, or one block */ |
| 126 | omega_bw_ir_init(prog); |
| 127 | /* The 128-row candidate keeps more accumulators live. Use the existing |
| 128 | * opt-in allocation range; the runtime still validates declared registers |
| 129 | * times threads. Blackwell Tuning Guide, physical page 13. */ |
| 130 | if (RT == 8 && omega_bw_ir_set_gpr_limit(prog, BW_PHYS_GPR_HW_LIMIT) != 0) return -1; |
| 131 | Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; |
| 132 | uint64_t order[64] = {0}, epilogue[64] = {0}, ok = 0; |
| 133 | if (omega_native_qgorder(&ok, order, 64, spec->group, spec->n / SPAN, 1, 1) != OMEGA_OK || ok != 1) return -1; |
| 134 | if (omega_native_qgorder(&ok, epilogue, 64, spec->group, spec->n / SPAN, 1, 0) != OMEGA_OK || ok != 1) return -1; |
| 135 | int at_exit = -1; |
| 136 | |
| 137 | int udesc = UV64(e), ptab = V64(e), pidx = V64(e), px = V64(e), pxs = V64(e); |
| 138 | int tid = V(e), cx = V(e), cy = V(e), warp = V(e), lane = V(e), g = V(e), tq = V(e), one = V(e), tent = V64(e); |
| 139 | int wbase = V64(e), sbase = V64(e), rc = V64(e), row0 = V(e), n0w = V(e), tmp = V(e), tmp2 = V(e), rb = V(e), la = V(e), lsa = V(e); |
| 140 | int pw[2] = { V64(e), V64(e) }, ps[2] = { V64(e), NS > 1 ? V64(e) : NOREG }; |
| 141 | if (e->err) return -1; |
| 142 | ldcu64(e, udesc, 0x358); |
| 143 | ldc64(e, ptab, 0x380); if (!spec->direct) ldc64(e, pidx, 0x388); ldc64(e, px, 0x390); ldc64(e, pxs, 0x3a0); |
| 144 | s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); |
| 145 | movi(e, one, 1); |
| 146 | if (order[2]) { |
| 147 | int ntl = V(e); |
| 148 | const int role[4] = {cx, cy, tmp, ntl}; |
| 149 | if (e->err || qg_order_emit(e, order, role, &at_exit)) return -1; |
| 150 | } |
| 151 | shr(e, warp, tid, 0, 5); andi(e, lane, tid, 0, 31); shr(e, g, lane, 0, 2); andi(e, tq, lane, 0, 3); |
| 152 | imadw(e, tent, cy, OMEGA_QG_TILE_BYTES, ptab); |
| 153 | ldg_wide(e, BW_IR_LDG_E_64, wbase, tent, 0, udesc, 1); |
| 154 | ldg_wide(e, BW_IR_LDG_E_64, sbase, tent, 8, udesc, 0); |
| 155 | ldg_wide(e, BW_IR_LDG_E_64, rc, tent, 16, udesc, 0); |
| 156 | imadi(e, row0, rc, 0, 1, NOREG); |
| 157 | imadi(e, n0w, cx, 0, SPAN, NOREG); imadi(e, tmp, warp, 0, 16u, NOREG); iadd(e, n0w, n0w, tmp); |
| 158 | /* weight rows of the GEMV tensor (original order): F32 rows n0w + g (+ 8 for column tile 1); SWIGLU gate rows n0w / 2 + g, up rows n / 2 later */ |
| 159 | if (NS > 1) shr(e, rb, n0w, 0, 1); else imadi(e, rb, n0w, 0, 1u, NOREG); |
| 160 | iadd(e, tmp, rb, g); imadw(e, pw[0], tmp, K, wbase); imadi(e, tmp2, tq, 0, 16u, NOREG); imadw(e, pw[0], tmp2, 1, pw[0]); |
| 161 | imadi(e, tmp, one, 0, NS > 1 ? spec->n / 2u : 8u, NOREG); imadw(e, pw[1], tmp, K, pw[0]); |
| 162 | /* scales [n / 128][KB] f32 * 2^56 (omega_gpu_fp8_api.h): the block of row rb (and of rb + n / 2) */ |
| 163 | shr(e, tmp, rb, 0, 7); imadw(e, ps[0], tmp, KB * 4u, sbase); |
| 164 | if (NS > 1) { imadi(e, tmp, one, 0, spec->n / 2u, rb); shr(e, tmp, tmp, 0, 7); imadw(e, ps[1], tmp, KB * 4u, sbase); } |
| 165 | imadi(e, la, lane, 0, 16u, NOREG); |
| 166 | imadi(e, lsa, g, 0, 16u, NOREG); |
| 167 | |
| 168 | /* staging: slot = tid + jj T -> row r = slot / 4, 16-byte segment t4 = tid % 4 of the 64-byte chunk */ |
| 169 | int gx[4], gs[4], sa[4], ssa[4]; |
| 170 | for (uint32_t jj = 0; jj < NJ; jj++) { gx[jj] = V64(e); gs[jj] = V64(e); sa[jj] = V(e); ssa[jj] = V(e); } |
| 171 | int r = V(e), sidx = V64(e), srow = V(e), t4 = V(e), hi = V(e), gg = V(e), ti = V(e); |
| 172 | if (e->err) return -1; |
| 173 | andi(e, t4, tid, 0, 3); |
| 174 | for (uint32_t jj = 0; jj < NJ; jj++) { |
| 175 | shr(e, r, tid, 0, 2); imadi(e, r, one, 0, jj * T / 4u, r); |
| 176 | iadd(e, tmp, row0, r); |
| 177 | if (spec->direct) imadi(e, srow, tmp, 0, 1u, NOREG); |
| 178 | else { imadw(e, sidx, tmp, 4u, pidx); ldg32(e, srow, sidx, udesc, 1); } |
| 179 | imadw(e, gx[jj], srow, K, px); imadi(e, tmp2, t4, 0, 16u, NOREG); imadw(e, gx[jj], tmp2, 1, gx[jj]); |
| 180 | imadw(e, gs[jj], srow, KB * 4u, pxs); |
| 181 | /* A slot: i * 1024 + (gg * 4 + t4) * 16 + 4 hi, i = r / 16, gg = r % 8, hi = (r / 8) % 2 */ |
| 182 | shr(e, ti, r, 0, 4); andi(e, gg, r, 0, 7); shr(e, hi, r, 0, 3); andi(e, hi, hi, 0, 1); |
| 183 | imadi(e, tmp, gg, 0, 4u, t4); imadi(e, tmp, tmp, 0, 16u, NOREG); imadi(e, tmp, ti, 0, 1024u, tmp); imadi(e, sa[jj], hi, 0, 4u, tmp); |
| 184 | /* scale slot: ABUF + ((i / 2) * 8 + gg) * 16 + 4 * ((i % 2) * 2 + hi) */ |
| 185 | shr(e, tmp, ti, 0, 1); imadi(e, tmp, tmp, 0, 8u, gg); imadi(e, tmp, tmp, 0, 16u, NOREG); |
| 186 | andi(e, tmp2, ti, 0, 1); imadi(e, tmp2, tmp2, 0, 2u, hi); imadi(e, ssa[jj], tmp2, 0, 4u, tmp); |
| 187 | } |
| 188 | |
| 189 | int acc[16], wa[2], wb[2], tq2[2], aq = V128(e), sap = V128(e), sc = V128(e), stgr[4], scv[4], cntk = V(e), onef1 = V(e), c2m56 = V(e); |
| 190 | int rr[2] = { V(e), NS > 1 ? V(e) : NOREG }, rrn[2] = { V(e), NS > 1 ? V(e) : NOREG }; |
| 191 | for (uint32_t x = 0; x < RT * 2u; x++) acc[x] = V128(e); |
| 192 | for (uint32_t j = 0; j < 2; j++) { wa[j] = V128(e); wb[j] = V128(e); tq2[j] = V128(e); } |
| 193 | for (uint32_t jj = 0; jj < NJ; jj++) { stgr[jj] = V128(e); scv[jj] = V(e); } |
| 194 | if (e->err) return -1; |
| 195 | movi(e, onef1, 0x3f800000u); |
| 196 | movi(e, c2m56, 0x23800000u); /* 2^-56: undoes the GEMV tensor's scale fold exactly (sw * 2^-56 is the checkpoint bf16 scale) */ |
| 197 | |
| 198 | #define ISSUE(wset, sb, xoff, woff, with_scale) do { \ |
| 199 | for (uint32_t jj = 0; jj < NJ; jj++) { ldw_pf(e, BW_IR_LDG_E_128, stgr[jj], gx[jj], (xoff), udesc, (sb)); ldw_pf(e, BW_IR_LDG_E, scv[jj], gs[jj], 0u, udesc, (sb)); } \ |
| 200 | for (uint32_t j = 0; j < 2; j++) ldw_pf(e, BW_IR_LDG_E_128, (wset)[j], pw[j], (woff), udesc, (sb)); \ |
| 201 | if (with_scale) for (uint32_t s = 0; s < NS; s++) ldw_pf(e, BW_IR_LDG_E, rrn[s], ps[s], 0u, udesc, (sb)); } while (0) |
| 202 | #define STAGE(buf) do { for (uint32_t jj = 0; jj < NJ; jj++) { static const uint32_t off[4] = { 0, 8, 512, 520 }; \ |
| 203 | for (unsigned q = 0; q < 4; q++) { BlackwellIRInsn st = I0(BW_IR_STS32); imadi(e, tmp, one, 0, (buf) + off[q], sa[jj]); \ |
| 204 | st.src1_vreg = tmp; st.src2_vreg = stgr[jj]; st.src2_subreg = (uint8_t)q; emit(e, st, K_STORE); } \ |
| 205 | { BlackwellIRInsn st = I0(BW_IR_STS32); imadi(e, tmp, one, 0, (buf) + ABUF, ssa[jj]); st.src1_vreg = tmp; st.src2_vreg = scv[jj]; emit(e, st, K_STORE); } } \ |
| 206 | bar_sync(e); } while (0) |
| 207 | #define COMPUTE(buf, wset, odd) do { \ |
| 208 | for (uint32_t i = 0; i < RT; i++) { \ |
| 209 | const uint32_t ii = spec->mutant == 3 ? (i ^ 1u) : i; \ |
| 210 | if ((i & 1u) == 0) lds128(e, sap, lsa, (buf) + ABUF + (i / 2u) * 128u, 1); \ |
| 211 | lds128(e, aq, la, (buf) + (ii * 2u) * 512u, 1); \ |
| 212 | for (uint32_t j = 0; j < 2; j++) qmma_s(e, tq2[j], aq, (wset)[j], 0, NOREG, 2); \ |
| 213 | if (!(spec->mutant == 4 && (odd))) { lds128(e, aq, la, (buf) + (ii * 2u + 1u) * 512u, 1); \ |
| 214 | for (uint32_t j = 0; j < 2; j++) qmma_s(e, tq2[j], aq, (wset)[j], 2, tq2[j], 2); } \ |
| 215 | for (uint32_t h = 0; h < 2; h++) for (uint32_t j = 0; j < 2; j++) { \ |
| 216 | BlackwellIRInsn m = I0(BW_IR_FMUL); m.dst_vreg = sc; m.dst_subreg = (uint8_t)(2u * h + j); \ |
| 217 | m.src1_vreg = sap; m.src1_subreg = (uint8_t)(2u * (i & 1u) + h); m.src2_vreg = rr[NS > 1 ? j : 0]; \ |
| 218 | if (spec->mutant == 1) { m.src1_vreg = onef1; m.src1_subreg = 0; } /* drops sa: s = 1.0 * sw */ \ |
| 219 | emit(e, m, K_FIXED); } \ |
| 220 | { uint32_t wl = spec->wait; while (wl > 0) { uint32_t st = wl > 15u ? 15u : wl; nop(e, st); wl -= st; } } \ |
| 221 | for (uint32_t j = 0; j < 2; j++) for (unsigned q = 0; q < 4; q++) { const uint32_t x = i * 2u + j; \ |
| 222 | ffma_s(e, acc[x], q, tq2[j], q, sc, 2u * (q >> 1) + j, acc[x], q); } \ |
| 223 | } } while (0) |
| 224 | |
| 225 | for (uint32_t x = 0; x < RT * 2u; x++) for (unsigned q = 0; q < 4; q++) { BlackwellIRInsn mz = I0(BW_IR_MOV_RZ); mz.dst_vreg = acc[x]; mz.dst_subreg = (uint8_t)q; emit(e, mz, K_FIXED); } |
| 226 | ISSUE(wa, 3u, 0u, 0u, 1); |
| 227 | consume(e, 3); |
| 228 | for (uint32_t s = 0; s < NS; s++) fmul_r(e, rr[s], 0, rrn[s], 0, c2m56, 0); |
| 229 | STAGE(0u); |
| 230 | movi(e, cntk, KB); |
| 231 | |
| 232 | const int L_k = here(e); |
| 233 | loop_begin(e); |
| 234 | ISSUE(wb, 4u, 64u, 64u, 0); |
| 235 | consume(e, 3); |
| 236 | COMPUTE(0u, wa, 0); |
| 237 | consume(e, 4); |
| 238 | STAGE(BUF); |
| 239 | { int nf = V(e), inc = V(e); if (e->err) return -1; |
| 240 | imadi(e, nf, one, 0, 0xfffffffeu, cntk); shr(e, nf, nf, 0, 31); imadi(e, nf, nf, 0, 0xffffffffu, one); |
| 241 | for (uint32_t jj = 0; jj < NJ; jj++) { imadw(e, gx[jj], nf, 128u, gx[jj]); imadw(e, gs[jj], nf, 4u, gs[jj]); } |
| 242 | for (uint32_t j = 0; j < 2; j++) imadw(e, pw[j], nf, 128u, pw[j]); |
| 243 | imadi(e, inc, nf, 0, spec->mutant == 2 ? 0u : 4u, NOREG); for (uint32_t s = 0; s < NS; s++) imadw(e, ps[s], inc, 1, ps[s]); |
| 244 | } |
| 245 | ISSUE(wa, 3u, 0u, 0u, 1); |
| 246 | COMPUTE(BUF, wb, 1); |
| 247 | consume(e, 3); |
| 248 | STAGE(0u); |
| 249 | for (uint32_t s = 0; s < NS; s++) fmul_r(e, rr[s], 0, rrn[s], 0, c2m56, 0); |
| 250 | imadi(e, cntk, one, 0, 0xffffffffu, cntk); |
| 251 | isetp_ge_u32(e, cntk, one); |
| 252 | bra_if_p0(e, L_k); |
| 253 | loop_end(e); |
| 254 | #undef ISSUE |
| 255 | #undef STAGE |
| 256 | #undef COMPUTE |
| 257 | |
| 258 | /* ---- epilogue (as omega_blackwell_codegen_tc2.c, natural SwiGLU order, no extra scale) ---- */ |
| 259 | int g2 = V(e), cnt2 = V(e), rrow = V(e), onee = V(e), pyr = V64(e); |
| 260 | { |
| 261 | int tid2 = V(e), cx2 = V(e), cy2 = V(e), lane2 = V(e), tq3 = V(e), tab2 = V64(e), ten2 = V64(e), rc2 = V64(e), py2 = V64(e), n2 = V(e), nw2 = V(e), r02 = V(e); |
| 262 | if (e->err) return -1; |
| 263 | ldc64(e, tab2, 0x380); ldc64(e, py2, 0x398); ldc32(e, n2, 0x3a8); |
| 264 | s2r(e, tid2, BW_SR_TID_X); s2r(e, cx2, BW_SR_CTAID_X); s2r(e, cy2, BW_SR_CTAID_Y); |
| 265 | const int role[4] = {cx2, cy2, tmp2, NOREG}; |
| 266 | if (qg_order_emit(e, epilogue, role, NULL)) return -1; |
| 267 | imadw(e, ten2, cy2, OMEGA_QG_TILE_BYTES, tab2); |
| 268 | ldg_wide(e, BW_IR_LDG_E_64, rc2, ten2, 16, udesc, 1); |
| 269 | andi(e, lane2, tid2, 0, 31); shr(e, g2, lane2, 0, 2); andi(e, tq3, lane2, 0, 3); |
| 270 | imadi(e, r02, rc2, 0, 1, NOREG); imadi(e, cnt2, rc2, 1, 1, NOREG); movi(e, onee, 1); |
| 271 | shr(e, tmp, tid2, 0, 5); imadi(e, tmp, tmp, 0, 16u, NOREG); imadi(e, nw2, cx2, 0, SPAN, tmp); |
| 272 | if (spec->epi == OMEGA_QG_EPI_F32) { |
| 273 | iadd(e, tmp, r02, g2); imadr(e, tmp, tmp, n2, nw2); imadi(e, tmp2, tq3, 0, 2u, NOREG); iadd(e, tmp, tmp, tmp2); imadw(e, pyr, tmp, 4u, py2); |
| 274 | } else { |
| 275 | iadd(e, tmp, r02, g2); imadr(e, tmp, tmp, n2, NOREG); imadw(e, pyr, tmp, 1u, py2); |
| 276 | shr(e, tmp, nw2, 0, 1); imadi(e, tmp2, tq3, 0, 2u, tmp); imadw(e, pyr, tmp2, 2u, pyr); |
| 277 | } |
| 278 | } |
| 279 | int kf = NOREG, onef = NOREG, c125 = NOREG, tg = NOREG, tex = NOREG, o0 = NOREG, o1 = NOREG, pk = NOREG; |
| 280 | if (spec->epi == OMEGA_QG_EPI_SWIGLU) { |
| 281 | kf = V(e); onef = V(e); c125 = V(e); tg = V(e); tex = V(e); o0 = V(e); o1 = V(e); pk = V(e); |
| 282 | if (e->err) return -1; |
| 283 | movi(e, kf, 0xbfb8aa3bu); movi(e, onef, 0x3f800000u); |
| 284 | movi(e, c125, 0x42fa0000u); /* 125.0f: caps t so 1 / (2^t + 1) stays a normal f32 (the chip flushes subnormal MUFU.RCP results: hardware, evidence OWN30B-L1G-SWIGLU-DIAG-ca29218e) */ |
| 285 | } |
| 286 | if (e->err) return -1; |
| 287 | const uint32_t ystride = spec->epi == OMEGA_QG_EPI_F32 ? spec->n * 4u : spec->n; |
| 288 | int skips[16]; uint32_t nskip = 0; |
| 289 | const int at_bssy = bssy_fwd(e); |
| 290 | for (uint32_t i = 0; i < RT; i++) for (uint32_t h = 0; h < 2; h++) { |
| 291 | const uint32_t roff = (16u * i + 8u * h) * ystride; |
| 292 | imadi(e, rrow, onee, 0, 16u * i + 8u * h, g2); |
| 293 | isetp_ge_u32(e, rrow, cnt2); |
| 294 | skips[nskip++] = bra_if_p0_fwd(e); |
| 295 | if (spec->epi == OMEGA_QG_EPI_F32) { |
| 296 | for (uint32_t j = 0; j < 2; j++) for (unsigned q = 0; q < 2; q++) { BlackwellIRInsn st = I0(BW_IR_STG_E); st.src1_vreg = pyr; st.src2_vreg = acc[i * 2 + j]; st.src2_subreg = (uint8_t)(2u * h + q); st.imm = roff + j * 32u + 4u * q; st.ureg = udesc; emit(e, st, K_STORE); } |
| 297 | } else { |
| 298 | const int G = acc[i * 2], U = acc[i * 2 + 1]; |
| 299 | for (unsigned q = 0; q < 2; q++) { |
| 300 | const int o = q ? o1 : o0; const unsigned sub = 2u * h + q; |
| 301 | fmul_r(e, tg, 0, G, sub, kf, 0); |
| 302 | { BlackwellIRInsn mn = I0(BW_IR_FMNMX_MIN); mn.dst_vreg = tg; mn.src1_vreg = tg; mn.src2_vreg = c125; emit(e, mn, K_FIXED); } |
| 303 | mufu_q(e, BW_IR_MUFU_EX2, tex, tg); |
| 304 | fadd(e, tex, tex, onef); |
| 305 | mufu_q(e, BW_IR_MUFU_RCP, tex, tex); |
| 306 | fmul_r(e, o, 0, G, sub, tex, 0); |
| 307 | fmul_r(e, o, 0, o, 0, U, sub); |
| 308 | } |
| 309 | f2fp_pack(e, pk, o1, o0); |
| 310 | stg(e, pyr, roff, pk, udesc); |
| 311 | } |
| 312 | } |
| 313 | { const int at_bsync = here(e); |
| 314 | bsync(e); |
| 315 | for (uint32_t q = 0; q < nskip; q++) patch_bra(e, skips[q], at_bsync); |
| 316 | patch_bra(e, at_bssy, at_bsync + 1); } |
| 317 | if (at_exit >= 0) patch_bra(e, at_exit, here(e)); |
| 318 | tail(e); |
| 319 | if (e->err) return -1; |
| 320 | return omega_bw_regalloc_solve(prog); |
| 321 | } |
| 322 | #else /* default: first-party seeded Omega; C above is differential-only */ |
| 323 | #include "omega_codegen_rest_wire.h" |
| 324 | int omega_pq_spec_ok(const OmegaPqSpec *s) { |
| 325 | uint64_t meta[OMEGA_OM_IR_META] = {0}; |
| 326 | if (omega_rest_query(omega_rest_qg_wire, 0, s, NULL, meta) != 0) return 0; |
| 327 | return (int)(int64_t)meta[48]; |
| 328 | } |
| 329 | int omega_blackwell_codegen_pq_prog(const OmegaPqSpec *spec, BlackwellIRProgram *prog) { |
| 330 | return omega_gpu_om_ir_replay(omega_rest_qg_wire, 1, (uintptr_t)spec, 0, 0, prog, NULL) == 0 ? 0 : -1; |
| 331 | } |
| 332 | int omega_qg_spec_ok(const OmegaQgSpec *s) { |
| 333 | uint64_t meta[OMEGA_OM_IR_META] = {0}; |
| 334 | if (omega_rest_query(omega_rest_qg_wire, 2, s, NULL, meta) != 0) return 0; |
| 335 | return (int)(int64_t)meta[48]; |
| 336 | } |
| 337 | uint32_t omega_qg_shared_bytes(const OmegaQgSpec *s) { |
| 338 | uint64_t meta[OMEGA_OM_IR_META] = {0}; |
| 339 | if (omega_rest_query(omega_rest_qg_wire, 3, s, NULL, meta) != 0) return 0; |
| 340 | return (uint32_t)(int64_t)meta[48]; |
| 341 | } |
| 342 | uint32_t omega_qg_span(const OmegaQgSpec *s) { |
| 343 | uint64_t meta[OMEGA_OM_IR_META] = {0}; |
| 344 | if (omega_rest_query(omega_rest_qg_wire, 4, s, NULL, meta) != 0) return 0; |
| 345 | return (uint32_t)(int64_t)meta[48]; |
| 346 | } |
| 347 | int omega_blackwell_codegen_qgemm_prog(const OmegaQgSpec *spec, BlackwellIRProgram *prog) { |
| 348 | return omega_gpu_om_ir_replay(omega_rest_qg_wire, 5, (uintptr_t)spec, 0, 0, prog, NULL) == 0 ? 0 : -1; |
| 349 | } |
| 350 | #endif /* CUDAPP_MIGRATION_PENDING_C_CODEGEN_QG */ |
| 351 |