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_pfhost.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* omega_blackwell_codegen_pfhost.c: router logits and MoE combine for the whole-prompt prefill (W-L1G-D). See the header. */ |
| 3 | #include "omega_blackwell_codegen_pfhost.h" |
| 4 | #include "omega_bw_em.h" |
| 5 | #include "omega_gpu_om_ir.h" |
| 6 | #include "cudapp_native.h" |
| 7 | #ifndef CUDAPP_MIGRATION_PENDING_C_CODEGEN_PFHOST |
| 8 | #define CUDAPP_MIGRATION_PENDING_C_CODEGEN_PFHOST 0 |
| 9 | #endif |
| 10 | |
| 11 | #if CUDAPP_MIGRATION_PENDING_C_CODEGEN_PFHOST |
| 12 | |
| 13 | static void fmul_s(Em *e, int d, unsigned dsub, int a, unsigned asub, int b, unsigned bsub) { |
| 14 | 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); |
| 15 | } |
| 16 | static void fadd_s(Em *e, int d, unsigned dsub, int a, unsigned asub, int b, unsigned bsub) { |
| 17 | BlackwellIRInsn i = I0(BW_IR_FADD); 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); |
| 18 | } |
| 19 | /* d[dsub] = a[asub] * b[bsub] + d[dsub] (the mutant's fused form) */ |
| 20 | static void ffma_s(Em *e, int d, unsigned dsub, int a, unsigned asub, int b, unsigned bsub) { |
| 21 | 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; i.src2_vreg = b; i.src2_subreg = (uint8_t)bsub; |
| 22 | i.src3_vreg = d; i.src3_subreg = (uint8_t)dsub; emit(e, i, K_FIXED); |
| 23 | } |
| 24 | /* acc[asub] += a[asub2] * b[bsub] as FMUL then FADD (bit-identical to the host's a * b then +), or FFMA for mutant 1 */ |
| 25 | static void mul_add(Em *e, int fused, int acc, unsigned accsub, int a, unsigned asub, int b, unsigned bsub, int prod) { |
| 26 | if (fused) { ffma_s(e, acc, accsub, a, asub, b, bsub); return; } |
| 27 | fmul_s(e, prod, 0, a, asub, b, bsub); |
| 28 | fadd_s(e, acc, accsub, acc, accsub, prod, 0); |
| 29 | } |
| 30 | #endif |
| 31 | |
| 32 | /* ======================= router logits ======================= */ |
| 33 | #if CUDAPP_MIGRATION_PENDING_C_CODEGEN_PFHOST_ADMIT |
| 34 | int omega_rl_spec_ok(const OmegaRlSpec *s) { |
| 35 | if (!s) return 0; |
| 36 | if (s->k < 128u || s->k > 16384u || s->k % 128u) return 0; |
| 37 | if (s->e < 32u || s->e > 1024u || s->e % 32u) return 0; |
| 38 | if (s->t < 1u || s->t > 16u) return 0; |
| 39 | if (s->t % (s->e / 32u)) return 0; /* tile rows per pass = warps */ |
| 40 | if ((uint64_t)(s->k / 4u + 1u) * s->e * 16u > 0x7fffffffu) return 0; |
| 41 | return s->mutant >= 0 && s->mutant <= 2; |
| 42 | } |
| 43 | uint32_t omega_rl_shared_bytes(const OmegaRlSpec *s) { return omega_rl_spec_ok(s) ? s->t * 512u : 0u; } |
| 44 | #else |
| 45 | static uint32_t rl_admit_query(const OmegaRlSpec *s, uint64_t op) { |
| 46 | if (!s) return 0; |
| 47 | uint64_t r=0; |
| 48 | if (omega_pf_admit(&r,op,s->k,s->e,s->t,(uint32_t)s->mutant)) return 0; |
| 49 | return (uint32_t)r; |
| 50 | } |
| 51 | int omega_rl_spec_ok(const OmegaRlSpec *s) { return (int)rl_admit_query(s,0); } |
| 52 | uint32_t omega_rl_shared_bytes(const OmegaRlSpec *s) { return rl_admit_query(s,1); } |
| 53 | #endif |
| 54 | |
| 55 | int omega_blackwell_codegen_rl_prog(const OmegaRlSpec *spec, BlackwellIRProgram *prog) { |
| 56 | #if !CUDAPP_MIGRATION_PENDING_C_CODEGEN_PFHOST |
| 57 | if (!spec || !prog) return -1; |
| 58 | uint64_t config = (uint64_t)spec->t | ((uint64_t)(uint32_t)spec->mutant << 32); |
| 59 | return omega_gpu_om_ir_replay(omega_cg_pfhost, 0, spec->k, spec->e, config, prog, NULL) == OMEGA_OM_IR_OK ? 0 : -1; |
| 60 | #else |
| 61 | if (!omega_rl_spec_ok(spec) || !prog) return -1; |
| 62 | const uint32_t K = spec->k, E = spec->e, T = spec->t, W = E / 32u, PASSES = T / W, GB = E * 16u; |
| 63 | const uint32_t CHUNKS = spec->mutant == 2 ? K / 128u - 1u : K / 128u; |
| 64 | const int fused = spec->mutant == 1; |
| 65 | omega_bw_ir_init(prog); |
| 66 | Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; |
| 67 | int udesc = UV64(e), px = V64(e), pw = V64(e), pl = V64(e), tid = V(e), cta = V(e), warp = V(e), lane = V(e), one = V(e); |
| 68 | int row = V(e), ssa = V(e), tmp = V(e), cntc = V(e), cnti = V(e), lsa = V(e), prod = V(e); |
| 69 | int rA = V128(e), rB = V128(e); |
| 70 | int acc[16], vq[4], g[16]; |
| 71 | for (uint32_t t = 0; t < T; t++) acc[t] = V(e); |
| 72 | for (uint32_t i = 0; i < 4; i++) vq[i] = V128(e); |
| 73 | for (uint32_t p = 0; p < PASSES; p++) g[p] = V128(e); |
| 74 | if (e->err) return -1; |
| 75 | if (CHUNKS == 0) return -1; |
| 76 | |
| 77 | ldcu64(e, udesc, 0x358); ldc64(e, px, 0x380); ldc64(e, pw, 0x388); ldc64(e, pl, 0x390); |
| 78 | s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X); |
| 79 | movi(e, one, 1); |
| 80 | shr(e, warp, tid, 0, 5); |
| 81 | andi(e, lane, tid, 0, 31u); |
| 82 | imadi(e, row, cta, 0, T, warp); /* first tile row this thread stages */ |
| 83 | imadw(e, px, row, K * 4u, px); imadw(e, px, lane, 16u, px); |
| 84 | imadi(e, ssa, warp, 0, 512u, NOREG); imadi(e, ssa, lane, 0, 16u, ssa); |
| 85 | imadw(e, pw, tid, 16u, pw); |
| 86 | imadi(e, tmp, cta, 0, T * E, tid); imadw(e, pl, tmp, 4u, pl); |
| 87 | for (uint32_t t = 0; t < T; t++) movi(e, acc[t], 0x80000000u); /* -0.0: Rust's f32 sum starts there */ |
| 88 | ldw_pf(e, BW_IR_LDG_E_128, rA, pw, 0u, udesc, 3u); /* group 0 */ |
| 89 | movi(e, cntc, CHUNKS); |
| 90 | |
| 91 | #define COMPUTE(rv, immoff) do { \ |
| 92 | for (uint32_t b0 = 0; b0 < T; b0 += 4u) { \ |
| 93 | const uint32_t nb = T - b0 < 4u ? T - b0 : 4u; \ |
| 94 | for (uint32_t i = 0; i < nb; i++) lds128(e, vq[i], lsa, (b0 + i) * 512u + (immoff), i == 0); \ |
| 95 | for (uint32_t i = 0; i < nb; i++) for (unsigned q = 0; q < 4; q++) mul_add(e, fused, acc[b0 + i], 0, rv, q, vq[i], q, prod); \ |
| 96 | } } while (0) |
| 97 | |
| 98 | const int L_c = here(e); |
| 99 | loop_begin(e); |
| 100 | bar_sync(e); /* every reader of the previous tile is done */ |
| 101 | for (uint32_t p = 0; p < PASSES; p++) ldg_wide(e, BW_IR_LDG_E_128, g[p], px, p * W * K * 4u, udesc, p == 0); |
| 102 | for (uint32_t p = 0; p < PASSES; p++) for (unsigned q = 0; q < 4; q++) { |
| 103 | imadi(e, tmp, one, 0, p * W * 512u + 4u * q, ssa); |
| 104 | BlackwellIRInsn st = I0(BW_IR_STS32); st.src1_vreg = tmp; st.src2_vreg = g[p]; st.src2_subreg = (uint8_t)q; emit(e, st, K_STORE); |
| 105 | } |
| 106 | bar_sync(e); |
| 107 | imadw(e, px, one, 512u, px); |
| 108 | movi(e, lsa, 0); |
| 109 | movi(e, cnti, 16u); /* 128 k per chunk, 8 per iteration */ |
| 110 | const int L_i = here(e); |
| 111 | loop_begin(e); |
| 112 | consume(e, 3u); |
| 113 | ldw_pf(e, BW_IR_LDG_E_128, rB, pw, GB, udesc, 3u); |
| 114 | COMPUTE(rA, 0u); |
| 115 | consume(e, 3u); |
| 116 | ldw_pf(e, BW_IR_LDG_E_128, rA, pw, 2u * GB, udesc, 3u); |
| 117 | COMPUTE(rB, 16u); |
| 118 | imadw(e, pw, one, 2u * GB, pw); |
| 119 | imadi(e, lsa, one, 0, 32u, lsa); |
| 120 | imadi(e, cnti, one, 0, 0xffffffffu, cnti); |
| 121 | isetp_ge_u32(e, cnti, one); |
| 122 | bra_if_p0(e, L_i); |
| 123 | loop_end(e); |
| 124 | imadi(e, cntc, one, 0, 0xffffffffu, cntc); |
| 125 | isetp_ge_u32(e, cntc, one); |
| 126 | bra_if_p0(e, L_c); |
| 127 | loop_end(e); |
| 128 | #undef COMPUTE |
| 129 | consume(e, 3u); /* the last prefetch (the pad group) */ |
| 130 | for (uint32_t t = 0; t < T; t++) stg(e, pl, t * E * 4u, acc[t], udesc); |
| 131 | tail(e); |
| 132 | if (e->err) return -1; |
| 133 | return omega_bw_regalloc_solve(prog); |
| 134 | #endif |
| 135 | } |
| 136 | |
| 137 | /* ======================= MoE weighted combine ======================= */ |
| 138 | #if CUDAPP_MIGRATION_PENDING_C_CODEGEN_PFHOST_ADMIT |
| 139 | int omega_mc_spec_ok(const OmegaMcSpec *s) { |
| 140 | if (!s) return 0; |
| 141 | if (s->h < 128u || s->h > 4096u || s->h % 128u) return 0; |
| 142 | if (s->top_k < 4u || s->top_k > 16u || s->top_k % 4u) return 0; |
| 143 | return s->mutant >= 0 && s->mutant <= 2; |
| 144 | } |
| 145 | #else |
| 146 | int omega_mc_spec_ok(const OmegaMcSpec *s) { |
| 147 | if (!s) return 0; |
| 148 | uint64_t r=0; |
| 149 | if (omega_pf_admit(&r,2,s->h,s->top_k,0,(uint32_t)s->mutant)) return 0; |
| 150 | return (int)r; |
| 151 | } |
| 152 | #endif |
| 153 | |
| 154 | int omega_blackwell_codegen_mc_prog(const OmegaMcSpec *spec, BlackwellIRProgram *prog) { |
| 155 | #if !CUDAPP_MIGRATION_PENDING_C_CODEGEN_PFHOST |
| 156 | if (!spec || !prog) return -1; |
| 157 | return omega_gpu_om_ir_replay(omega_cg_pfhost, 1, spec->h, spec->top_k, (uint64_t)(uint32_t)spec->mutant, prog, NULL) == OMEGA_OM_IR_OK ? 0 : -1; |
| 158 | #else |
| 159 | if (!omega_mc_spec_ok(spec) || !prog) return -1; |
| 160 | const uint32_t H = spec->h, KS = spec->top_k, NQ = KS / 4u; |
| 161 | const uint32_t USED = spec->mutant == 2 ? KS - 1u : KS; |
| 162 | const int fused = spec->mutant == 1; |
| 163 | omega_bw_ir_init(prog); |
| 164 | Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em; |
| 165 | int udesc = UV64(e), pd = V64(e), pi = V64(e), pw = V64(e), po = V64(e), tid = V(e), cta = V(e), off = V(e), prod = V(e), accq = V128(e); |
| 166 | int idx[4], wt[4], v[16], pa[16]; |
| 167 | for (uint32_t q = 0; q < NQ; q++) { idx[q] = V128(e); wt[q] = V128(e); } |
| 168 | for (uint32_t s = 0; s < KS; s++) { v[s] = V128(e); pa[s] = V64(e); } |
| 169 | if (e->err) return -1; |
| 170 | ldcu64(e, udesc, 0x358); ldc64(e, pd, 0x380); ldc64(e, pi, 0x388); ldc64(e, pw, 0x390); ldc64(e, po, 0x398); |
| 171 | s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X); |
| 172 | imadw(e, pi, cta, KS * 4u, pi); imadw(e, pw, cta, KS * 4u, pw); |
| 173 | for (uint32_t q = 0; q < NQ; q++) ldg_wide(e, BW_IR_LDG_E_128, idx[q], pi, 16u * q, udesc, q == 0); |
| 174 | for (uint32_t q = 0; q < NQ; q++) ldg_wide(e, BW_IR_LDG_E_128, wt[q], pw, 16u * q, udesc, 0); |
| 175 | for (uint32_t s = 0; s < KS; s++) { imadi(e, off, idx[s / 4u], s % 4u, H / 4u, tid); imadw(e, pa[s], off, 16u, pd); } |
| 176 | for (uint32_t s = 0; s < KS; s++) ldg_wide(e, BW_IR_LDG_E_128, v[s], pa[s], 0u, udesc, s == 0); |
| 177 | for (unsigned q = 0; q < 4; q++) { BlackwellIRInsn mz = I0(BW_IR_MOV_RZ); mz.dst_vreg = accq; mz.dst_subreg = (uint8_t)q; emit(e, mz, K_FIXED); } |
| 178 | for (uint32_t s = 0; s < USED; s++) for (unsigned q = 0; q < 4; q++) mul_add(e, fused, accq, q, wt[s / 4u], s % 4u, v[s], q, prod); |
| 179 | imadi(e, off, cta, 0, H / 4u, tid); imadw(e, po, off, 16u, po); |
| 180 | for (unsigned q = 0; q < 4; q++) { BlackwellIRInsn st = I0(BW_IR_STG_E); st.src1_vreg = po; st.src2_vreg = accq; st.src2_subreg = (uint8_t)q; st.imm = 4u * q; st.ureg = udesc; emit(e, st, K_STORE); } |
| 181 | tail(e); |
| 182 | if (e->err) return -1; |
| 183 | return omega_bw_regalloc_solve(prog); |
| 184 | #endif |
| 185 | } |