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_moe2.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* omega_blackwell_codegen_moe2.c: FP8 e4m3 block-scaled GEMV, variant 2 (row-pipelined). See the header. |
| 3 | * |
| 4 | * Scheduling: variant 1's chip-learned rules (every instruction waits on everything pending; variable-latency |
| 5 | * results through scoreboard 0, every load sets the read barrier 1 and the next load of a batch waits on it, |
| 6 | * uniform loads scoreboard 2; fixed-latency producers stall 6; ISETP stalls 14 before a predicated branch), |
| 7 | * plus one new rule: the loads of the NEXT row group land through scoreboard 3 (SB_B), which no instruction |
| 8 | * waits on except the register moves that take the set over (K_MOVB). The emitter's `pending` mask therefore |
| 9 | * never carries SB_B: the arithmetic of the current row group runs with the next one's loads in flight. |
| 10 | * Hazards covered: (1) the moves read set B before the next loads into B are issued (program order, fixed |
| 11 | * latency); (2) the address registers of a batch are protected by SB_RD as in variant 1; (3) every load that |
| 12 | * was issued is consumed by a move before EXIT (loads are issued iff the row group exists, and the loop runs |
| 13 | * exactly over the existing row groups). */ |
| 14 | #include "omega_blackwell_codegen_moe2.h" |
| 15 | #include <string.h> |
| 16 | |
| 17 | #include "cudapp_native.h" |
| 18 | #include "omega_gpu_om_ir.h" |
| 19 | |
| 20 | #ifndef CUDAPP_MIGRATION_PENDING_C_CODEGEN_MOE2 |
| 21 | #define CUDAPP_MIGRATION_PENDING_C_CODEGEN_MOE2 0 |
| 22 | #endif |
| 23 | #if CUDAPP_MIGRATION_PENDING_C_CODEGEN_MOE2 |
| 24 | #define CW(stall, wbar, rbar, wait) \ |
| 25 | (((uint32_t)(stall) << 9) | ((uint32_t)(wbar) << 14) | ((uint32_t)(rbar) << 17) | ((uint32_t)(wait) << 20)) |
| 26 | #define CW_YIELD 0x2000u |
| 27 | #define SB_NONE 7u |
| 28 | #define SB_GPR 0u |
| 29 | #define SB_RD 1u |
| 30 | #define SB_UGPR 2u |
| 31 | #define SB_B 3u |
| 32 | #define NOREG (-1) |
| 33 | #define MAX_LOOPS 4 |
| 34 | #define MAX_KS 16u |
| 35 | #define MAX_ROWS 8u |
| 36 | |
| 37 | enum { K_FIXED, K_PRED, K_VAR, K_VAR_MORE, K_UVAR, K_STORE, K_SYNC, K_EXIT, K_BRA, K_BSSY, K_BSYNC, K_LDB, K_LDB_MORE, K_MOVB }; |
| 38 | |
| 39 | typedef struct { |
| 40 | BlackwellIRProgram *p; |
| 41 | uint32_t pending; |
| 42 | int err; |
| 43 | int open_start[MAX_LOOPS]; |
| 44 | int depth; |
| 45 | } Em; |
| 46 | |
| 47 | static void emit(Em *e, BlackwellIRInsn in, int kind) { |
| 48 | uint32_t wait = e->pending; |
| 49 | switch (kind) { |
| 50 | case K_FIXED: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 51 | case K_PRED: in.control = CW(14, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 52 | case K_VAR: in.control = CW(4, SB_GPR, SB_RD, wait) | CW_YIELD; e->pending = (1u << SB_GPR) | (1u << SB_RD); break; |
| 53 | case K_VAR_MORE: in.control = CW(4, SB_GPR, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; e->pending |= (1u << SB_GPR) | (1u << SB_RD); break; |
| 54 | case K_UVAR: in.control = CW(4, SB_UGPR, SB_NONE, wait) | CW_YIELD; e->pending = 1u << SB_UGPR; break; |
| 55 | case K_STORE: in.control = CW(4, SB_NONE, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; |
| 56 | case K_SYNC: in.control = CW(6, SB_NONE, SB_NONE, wait); e->pending = 0; break; |
| 57 | case K_EXIT: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; |
| 58 | case K_BRA: in.control = CW(5, SB_NONE, SB_NONE, wait); e->pending = in.predicate_p0 ? wait : 0; break; |
| 59 | case K_BSSY: in.control = CW(1, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; |
| 60 | case K_BSYNC: in.control = CW(5, SB_NONE, SB_NONE, wait) | CW_YIELD; e->pending = 0; break; |
| 61 | /* next-row-group loads: data on SB_B (not auto-waited), address operands on SB_RD (waited by the next instruction) */ |
| 62 | case K_LDB: in.control = CW(4, SB_B, SB_RD, wait) | CW_YIELD; e->pending = 1u << SB_RD; break; |
| 63 | case K_LDB_MORE: in.control = CW(4, SB_B, SB_RD, (e->pending & (1u << SB_RD))) | CW_YIELD; e->pending = 1u << SB_RD; break; |
| 64 | /* the first move of set B into set A: the one wait on SB_B */ |
| 65 | case K_MOVB: in.control = CW(6, SB_NONE, SB_NONE, wait | (1u << SB_B)); e->pending = 0; break; |
| 66 | default: e->err = -1; return; |
| 67 | } |
| 68 | if (omega_bw_ir_append(e->p, &in) < 0) e->err = -1; |
| 69 | } |
| 70 | |
| 71 | static BlackwellIRInsn I0(BlackwellIROpcode op) { |
| 72 | BlackwellIRInsn in; memset(&in, 0, sizeof in); |
| 73 | in.op = op; in.dst_vreg = NOREG; in.src1_vreg = NOREG; in.src2_vreg = NOREG; in.src3_vreg = NOREG; in.ureg = NOREG; |
| 74 | return in; |
| 75 | } |
| 76 | static int V(Em *e) { int v = omega_bw_ir_alloc_vreg(e->p); if (v < 0) e->err = -1; return v; } |
| 77 | static int V64(Em *e) { int v = omega_bw_ir_alloc_vreg64(e->p); if (v < 0) e->err = -1; return v; } |
| 78 | static int V128(Em *e) { int v = omega_bw_ir_alloc_vreg128(e->p); if (v < 0) e->err = -1; return v; } |
| 79 | static int UV64(Em *e) { int v = omega_bw_ir_alloc_uvreg64(e->p); if (v < 0) e->err = -1; return v; } |
| 80 | static int here(const Em *e) { return (int)e->p->count; } |
| 81 | |
| 82 | static void ldcu64(Em *e, int ud, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDCU64); i.dst_vreg = ud; i.imm = off; i.is_uniform = true; emit(e, i, K_UVAR); } |
| 83 | static void ldc64(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC64); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } |
| 84 | static void ldc32(Em *e, int d, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_LDC); i.dst_vreg = d; i.imm = off; emit(e, i, K_VAR); } |
| 85 | static void s2r(Em *e, int d, uint32_t sr) { BlackwellIRInsn i = I0(BW_IR_S2R); i.dst_vreg = d; i.imm = sr; emit(e, i, K_VAR); } |
| 86 | static void movi(Em *e, int d, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_MOV_IMM); i.dst_vreg = d; i.imm = imm; emit(e, i, K_FIXED); } |
| 87 | static void movrz(Em *e, int d) { BlackwellIRInsn i = I0(BW_IR_MOV_RZ); i.dst_vreg = d; emit(e, i, K_FIXED); } |
| 88 | /* d = a * imm + c (c NOREG = RZ); a may be a sub-register of a quad */ |
| 89 | static void imadi(Em *e, int d, int a, unsigned asub, uint32_t imm, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED); } |
| 90 | /* d = a * b + c (registers) */ |
| 91 | static void imadr(Em *e, int d, int a, int b, int c) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src3_vreg = c; emit(e, i, K_FIXED); } |
| 92 | /* d64 = a * imm + c64 */ |
| 93 | static void imadw(Em *e, int d64, int a, uint32_t imm, int c64) { BlackwellIRInsn i = I0(BW_IR_IMAD_WIDE); i.dst_vreg = d64; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c64; emit(e, i, K_FIXED); } |
| 94 | /* d[dsub] = s[ssub] (IMAD d = s * 1 + RZ, sub-register destination); kind K_MOVB for the first move of a set */ |
| 95 | static void movsub(Em *e, int d, unsigned dsub, int s, unsigned ssub, int kind) { BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.dst_subreg = (uint8_t)dsub; i.src1_vreg = s; i.src1_subreg = (uint8_t)ssub; i.imm = 1; emit(e, i, kind); } |
| 96 | static void shr(Em *e, int d, int a, unsigned asub, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; emit(e, i, K_FIXED); } |
| 97 | static void andi(Em *e, int d, int a, unsigned asub, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_LOP3_AND); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.imm = imm; emit(e, i, K_FIXED); } |
| 98 | static void xorr(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_LOP3_XOR); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } |
| 99 | static void ldg_wide(Em *e, BlackwellIROpcode op, int d, int addr64, uint32_t imm, int udesc, int kind) { |
| 100 | BlackwellIRInsn i = I0(op); i.dst_vreg = d; i.src1_vreg = addr64; i.imm = imm; i.ureg = udesc; emit(e, i, kind); |
| 101 | } |
| 102 | static void ldg32(Em *e, int d, int addr64, int udesc, int kind) { BlackwellIRInsn i = I0(BW_IR_LDG_E); i.dst_vreg = d; i.src1_vreg = addr64; i.ureg = udesc; emit(e, i, kind); } |
| 103 | static void stg(Em *e, int addr64, uint32_t imm, int val, int udesc) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = addr64; i.src2_vreg = val; i.imm = imm; i.ureg = udesc; emit(e, i, K_STORE); } |
| 104 | static void lds128(Em *e, int d128, int addr, uint32_t imm) { BlackwellIRInsn i = I0(BW_IR_LDS128); i.dst_vreg = d128; i.src1_vreg = addr; i.imm = imm; emit(e, i, K_VAR); } |
| 105 | static void sts32(Em *e, int addr, int val) { BlackwellIRInsn i = I0(BW_IR_STS32); i.src1_vreg = addr; i.src2_vreg = val; emit(e, i, K_STORE); } |
| 106 | static void bar_sync(Em *e) { emit(e, I0(BW_IR_BAR_SYNC), K_SYNC); } |
| 107 | static void fadd(Em *e, int d, int a, int b) { BlackwellIRInsn i = I0(BW_IR_FADD); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_FIXED); } |
| 108 | /* d = a * b[bsub] + c */ |
| 109 | static void ffma(Em *e, int d, int a, int b, unsigned bsub, int c) { BlackwellIRInsn i = I0(BW_IR_FFMA); i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src2_subreg = (uint8_t)bsub; i.src3_vreg = c; emit(e, i, K_FIXED); } |
| 110 | static void shfl_down(Em *e, int d, int a, uint32_t off) { BlackwellIRInsn i = I0(BW_IR_SHFL_DOWN); i.dst_vreg = d; i.src1_vreg = a; i.imm = off; emit(e, i, K_VAR); } |
| 111 | static void isetp_ge_u32(Em *e, int a, int b) { BlackwellIRInsn i = I0(BW_IR_ISETP_GE_U32); i.src1_vreg = a; i.src2_vreg = b; emit(e, i, K_PRED); } |
| 112 | static void bra(Em *e, int target) { BlackwellIRInsn i = I0(BW_IR_BRA); i.imm = (uint32_t)(target - here(e)); emit(e, i, K_BRA); } |
| 113 | static int bra_if_p0_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BRA); i.predicate_p0 = true; i.imm = 1; emit(e, i, K_BRA); return at; } |
| 114 | static void patch_bra(Em *e, int at, int target) { if (!e->err) e->p->insns[at].imm = (uint32_t)(target - at); } |
| 115 | static int bssy_fwd(Em *e) { int at = here(e); BlackwellIRInsn i = I0(BW_IR_BSSY); i.bar_reg = 0; i.imm = 2; emit(e, i, K_BSSY); return at; } |
| 116 | static void bsync(Em *e) { BlackwellIRInsn i = I0(BW_IR_BSYNC); i.bar_reg = 0; emit(e, i, K_BSYNC); } |
| 117 | static void tail(Em *e) { |
| 118 | emit(e, I0(BW_IR_EXIT), K_EXIT); |
| 119 | BlackwellIRInsn self = I0(BW_IR_BRA); |
| 120 | self.control = 0x000fc000u; |
| 121 | if (omega_bw_ir_append(e->p, &self) < 0) e->err = -1; |
| 122 | } |
| 123 | |
| 124 | /* Loops: a value defined before the loop and read inside it stays live until the back-branch |
| 125 | * (the linear-scan allocator knows no back edges; same rule as variant 1). */ |
| 126 | static void loop_begin(Em *e) { if (e->depth >= MAX_LOOPS) { e->err = -1; return; } e->open_start[e->depth++] = here(e); } |
| 127 | static void loop_end(Em *e) { |
| 128 | if (e->depth <= 0) { e->err = -1; return; } |
| 129 | int start = e->open_start[--e->depth], end = here(e) - 1; |
| 130 | OmegaRegAlloc *ra = &e->p->regalloc; |
| 131 | for (int v = 0; v < ra->num_vregs; v++) { |
| 132 | OmegaLiveInterval *iv = &ra->intervals[v]; |
| 133 | if (iv->first_def >= 0 && iv->first_def < start && iv->last_use >= start && iv->last_use < end) iv->last_use = end; |
| 134 | } |
| 135 | } |
| 136 | |
| 137 | static int log2u(uint32_t v) { int l = 0; while ((1u << l) < v) l++; return (1u << l) == v ? l : -1; } |
| 138 | |
| 139 | int omega_fp8_gemv2_spec_ok(const OmegaFp8GemvSpec *s) { |
| 140 | if (!s) return 0; |
| 141 | if (s->lane_bytes != 8 && s->lane_bytes != 16) return 0; |
| 142 | if (s->rows != 1 && s->rows != 2 && s->rows != 4 && s->rows != 8) return 0; |
| 143 | if (s->warps < 1 || s->warps > 32) return 0; |
| 144 | if (s->k == 0 || s->k > OMEGA_FP8_MAX_K || s->k % OMEGA_FP8_BLOCK || s->k % (32u * s->lane_bytes)) return 0; |
| 145 | if (s->mutant < 0 || s->mutant > 3) return 0; |
| 146 | const uint32_t ks = s->k / (32u * s->lane_bytes); |
| 147 | if (ks > MAX_KS || s->rows * ks * s->lane_bytes > 128u) return 0; /* whole row group per register set */ |
| 148 | if (s->k > 512u * s->warps) return 0; /* at most 16 staging rounds */ |
| 149 | if ((uint64_t)(s->rows - 1) * s->k + s->lane_bytes >= (1u << 23)) return 0; |
| 150 | return 1; |
| 151 | } |
| 152 | |
| 153 | /* k * 4 for the staged row, plus one 16-byte dummy slot only when the last staging round is partial (clamped threads store there): |
| 154 | * k = 4096 at 16 warps is exact: 16384 bytes. (When the QMD cap was 16384, a 16400-byte variant was refused by the QMD builder, |
| 155 | * receipt OWN30B-FP8-GEMV-V2 r1; the cap is now 32768, the SM-config target the QMD declares.) */ |
| 156 | uint32_t omega_fp8_gemv2_shared_bytes(const OmegaFp8GemvSpec *spec) { |
| 157 | if (!omega_fp8_gemv2_spec_ok(spec)) return 0u; |
| 158 | return spec->k * 4u + (spec->k % (32u * spec->warps) ? 16u : 0u); |
| 159 | } |
| 160 | |
| 161 | /* e4m3 byte i of word w -> f32 bits (value * 2^-120): ((b & 0x7F) << 20) | ((b & 0x80) << 24). Variant 1's sequence. */ |
| 162 | static void decode_byte(Em *e, int f, int wq, unsigned word, unsigned i, int u, int a, int s) { |
| 163 | const uint32_t M = 0x07F00000u; |
| 164 | if (i < 3) imadi(e, u, wq, word, 1u << (20u - 8u * i), NOREG); |
| 165 | else shr(e, u, wq, word, 4); |
| 166 | andi(e, a, u, 0, M); |
| 167 | andi(e, s, wq, word, 0x80u << (8u * i)); |
| 168 | if (i < 3) imadi(e, f, s, 0, 1u << (24u - 8u * i), a); |
| 169 | else imadi(e, f, s, 0, 1u, a); |
| 170 | } |
| 171 | |
| 172 | typedef struct { |
| 173 | int w[MAX_ROWS][MAX_KS]; /* weight bytes: row r, step s (quad or pair) */ |
| 174 | int sc[MAX_KS]; /* scale word per step */ |
| 175 | } RegSet; |
| 176 | |
| 177 | int omega_blackwell_codegen_fp8_gemv2_prog(const OmegaFp8GemvSpec *spec, BlackwellIRProgram *prog) { |
| 178 | if (!omega_fp8_gemv2_spec_ok(spec) || !prog) return -1; |
| 179 | const uint32_t K = spec->k, LB = spec->lane_bytes, R = spec->rows, W = spec->warps; |
| 180 | const uint32_t KS = K / (32u * LB), BPS = 32u * LB / OMEGA_FP8_BLOCK, LPB = OMEGA_FP8_BLOCK / LB, KB = K / OMEGA_FP8_BLOCK; |
| 181 | const int log2lpb = log2u(LPB), log2rows_per_block = log2u(OMEGA_FP8_BLOCK / R); |
| 182 | const uint32_t WORDS = LB / 4u; |
| 183 | const BlackwellIROpcode ldw = LB == 16 ? BW_IR_LDG_E_128 : BW_IR_LDG_E_64; |
| 184 | if (log2lpb < 0 || log2rows_per_block < 0) return -1; |
| 185 | omega_bw_ir_init(prog); |
| 186 | Em em; memset(&em, 0, sizeof em); em.p = prog; |
| 187 | Em *e = &em; |
| 188 | |
| 189 | /* ---- prologue (variant 1's) ---- */ |
| 190 | int udesc = UV64(e), ptab = V64(e), px = V64(e), py = V64(e); |
| 191 | int rgn = V(e), xs = V(e), ys = V(e), stride = V(e); |
| 192 | int tid = V(e), cx = V(e), cy = V(e), warp = V(e), lane = V(e), one = V(e), gidx = V(e); |
| 193 | int tent = V64(e), wbase = V64(e), sbase = V64(e), off = V(e), xrow = V64(e), yrow = V64(e); |
| 194 | int wlane = V(e), slane = V(e), rgl = V(e), rgc = V(e); |
| 195 | if (e->err) return -1; |
| 196 | ldcu64(e, udesc, 0x358); |
| 197 | ldc64(e, ptab, OMEGA_FP8_ARG_TAB); ldc64(e, px, OMEGA_FP8_ARG_X); ldc64(e, py, OMEGA_FP8_ARG_Y); |
| 198 | ldc32(e, rgn, OMEGA_FP8_ARG_RG); ldc32(e, xs, OMEGA_FP8_ARG_XSTRIDE); ldc32(e, ys, OMEGA_FP8_ARG_YSTRIDE); ldc32(e, stride, OMEGA_FP8_ARG_STRIDE); |
| 199 | s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); |
| 200 | movi(e, one, 1); |
| 201 | shr(e, warp, tid, 0, 5); |
| 202 | andi(e, lane, tid, 0, 31); |
| 203 | if (spec->mutant == 3) xorr(e, gidx, cy, one); else imadi(e, gidx, cy, 0, 1, NOREG); |
| 204 | imadw(e, tent, gidx, 16, ptab); |
| 205 | ldg_wide(e, BW_IR_LDG_E_64, wbase, tent, 0, udesc, K_VAR); |
| 206 | ldg_wide(e, BW_IR_LDG_E_64, sbase, tent, 8, udesc, K_VAR_MORE); |
| 207 | imadr(e, off, cy, xs, NOREG); imadw(e, xrow, off, 1, px); |
| 208 | imadr(e, off, cy, ys, NOREG); imadw(e, yrow, off, 1, py); |
| 209 | imadi(e, wlane, lane, 0, LB, NOREG); /* byte offset of this lane's chunk in a row */ |
| 210 | shr(e, slane, lane, 0, (uint32_t)log2lpb); imadi(e, slane, slane, 0, 4, NOREG); /* scale word of this lane in a step */ |
| 211 | imadi(e, rgl, cx, 0, W, warp); /* first row group of this warp: the load cursor */ |
| 212 | imadi(e, rgc, rgl, 0, 1, NOREG); /* the compute cursor */ |
| 213 | |
| 214 | /* ---- registers: two sets, x quad, accumulators, temporaries ---- */ |
| 215 | RegSet A, B; |
| 216 | int acc[MAX_ROWS], dot[MAX_ROWS], xq = V128(e); |
| 217 | for (uint32_t r = 0; r < R; r++) { acc[r] = V(e); dot[r] = V(e); for (uint32_t s = 0; s < KS; s++) { A.w[r][s] = LB == 16 ? V128(e) : V64(e); B.w[r][s] = LB == 16 ? V128(e) : V64(e); } } |
| 218 | for (uint32_t s = 0; s < KS; s++) { A.sc[s] = V(e); B.sc[s] = V(e); } |
| 219 | int pw = V64(e), ps = V64(e), psd = V64(e), pyr = V64(e), pya = V64(e), srg = V(e), xl0 = V(e); |
| 220 | int u = V(e), a = V(e), sg = V(e), f = V(e), t = V(e); |
| 221 | if (e->err) return -1; |
| 222 | |
| 223 | /* ---- stage this group's x' row in shared memory (k floats), one 32-bit word per thread per round (variant 1 deep) ---- */ |
| 224 | { |
| 225 | const uint32_t rounds = (K + 32u * W - 1u) / (32u * W); |
| 226 | int pxt = V64(e), sa0 = V(e), sa = V(e), sap = V(e), pxs[16], xst[16], kq = V(e), over = V(e), tq = V(e), qs = V(e), qg = V(e); |
| 227 | for (uint32_t j = 0; j < rounds; j++) { pxs[j] = V64(e); xst[j] = V(e); } |
| 228 | if (e->err) return -1; |
| 229 | imadw(e, pxt, tid, 4, xrow); |
| 230 | imadi(e, sa0, tid, 0, 4, NOREG); |
| 231 | for (uint32_t j = 0; j < rounds; j++) imadw(e, pxs[j], one, j * 128u * W, pxt); |
| 232 | if (K % (32u * W)) { |
| 233 | const uint32_t j = rounds - 1u; |
| 234 | imadi(e, kq, tid, 0, 1, NOREG); imadi(e, kq, one, 0, j * 32u * W, kq); /* q */ |
| 235 | imadi(e, tq, kq, 0, 0xffffffffu, NOREG); imadi(e, tq, one, 0, K - 1u, tq); /* K - 1 - q */ |
| 236 | shr(e, over, tq, 0, 31); /* 1 iff q >= K */ |
| 237 | imadi(e, tq, kq, 0, 1, NOREG); imadi(e, tq, one, 0, (uint32_t)(0u - K), tq); /* q - K */ |
| 238 | imadr(e, qs, over, tq, NOREG); imadi(e, qs, qs, 0, 0xffffffffu, kq); /* K when over, else q */ |
| 239 | imadi(e, qg, tq, 0, 1, NOREG); imadi(e, qg, one, 0, 1, qg); /* q - K + 1 */ |
| 240 | imadr(e, qg, over, qg, NOREG); imadi(e, qg, qg, 0, 0xffffffffu, kq); /* K - 1 when over, else q */ |
| 241 | imadw(e, pxs[j], qg, 4, xrow); |
| 242 | imadi(e, sap, qs, 0, 4, NOREG); |
| 243 | } |
| 244 | for (uint32_t j = 0; j < rounds; j++) ldg32(e, xst[j], pxs[j], udesc, j == 0 ? K_VAR : K_VAR_MORE); |
| 245 | for (uint32_t j = 0; j < rounds; j++) { |
| 246 | if (K % (32u * W) && j == rounds - 1u) { sts32(e, sap, xst[j]); continue; } |
| 247 | imadi(e, sa, one, 0, j * 128u * W, sa0); sts32(e, sa, xst[j]); |
| 248 | } |
| 249 | bar_sync(e); |
| 250 | imadi(e, xl0, lane, 0, LB * 4u, NOREG); /* this lane's x' chunk of step 0, shared byte offset */ |
| 251 | } |
| 252 | |
| 253 | /* ---- the loads of one row group into set B (rows * KS weight loads, then KS scale words) ---- */ |
| 254 | #define ISSUE_B() do { \ |
| 255 | imadw(e, pw, rgl, R * K, wbase); imadw(e, pw, wlane, 1, pw); \ |
| 256 | shr(e, srg, rgl, 0, (uint32_t)log2rows_per_block); \ |
| 257 | imadw(e, ps, srg, KB * 4u, sbase); imadw(e, ps, slane, 1, ps); \ |
| 258 | int kind = K_LDB; \ |
| 259 | for (uint32_t s_ = 0; s_ < KS; s_++) for (uint32_t r_ = 0; r_ < R; r_++) { ldg_wide(e, ldw, B.w[r_][s_], pw, r_ * K + s_ * 32u * LB, udesc, kind); kind = K_LDB_MORE; } \ |
| 260 | for (uint32_t s_ = 0; s_ < KS; s_++) { \ |
| 261 | if (s_ == 0) ldg32(e, B.sc[0], ps, udesc, kind); \ |
| 262 | else { imadw(e, psd, one, spec->mutant == 2 ? 0u : s_ * BPS * 4u, ps); ldg32(e, B.sc[s_], psd, udesc, K_LDB_MORE); } \ |
| 263 | kind = K_LDB_MORE; \ |
| 264 | } \ |
| 265 | imadi(e, rgl, stride, 0, 1, rgl); \ |
| 266 | } while (0) |
| 267 | |
| 268 | /* ---- prologue: nothing to do, or the first row group's loads ---- */ |
| 269 | isetp_ge_u32(e, rgl, rgn); |
| 270 | const int bra_none = bra_if_p0_fwd(e); |
| 271 | ISSUE_B(); |
| 272 | |
| 273 | /* ---- row-group loop: take set B over, issue the next row group into B, compute set A, reduce, store ---- */ |
| 274 | const int L_rg = here(e); |
| 275 | loop_begin(e); |
| 276 | { |
| 277 | int kind = K_MOVB; |
| 278 | for (uint32_t s = 0; s < KS; s++) for (uint32_t r = 0; r < R; r++) for (uint32_t q = 0; q < WORDS; q++) { movsub(e, A.w[r][s], q, B.w[r][s], q, kind); kind = K_FIXED; } |
| 279 | for (uint32_t s = 0; s < KS; s++) movsub(e, A.sc[s], 0, B.sc[s], 0, K_FIXED); |
| 280 | } |
| 281 | isetp_ge_u32(e, rgl, rgn); |
| 282 | const int bra_skip_issue = bra_if_p0_fwd(e); |
| 283 | ISSUE_B(); |
| 284 | const int L_compute = here(e); |
| 285 | patch_bra(e, bra_skip_issue, L_compute); |
| 286 | for (uint32_t r = 0; r < R; r++) movrz(e, acc[r]); |
| 287 | for (uint32_t s = 0; s < KS; s++) { |
| 288 | for (uint32_t r = 0; r < R; r++) movrz(e, dot[r]); |
| 289 | for (uint32_t word = 0; word < WORDS; word++) { |
| 290 | lds128(e, xq, xl0, s * 128u * LB + word * 16u); |
| 291 | for (uint32_t r = 0; r < R; r++) |
| 292 | for (unsigned i = 0; i < 4; i++) { |
| 293 | decode_byte(e, f, A.w[r][s], word, i, u, a, sg); |
| 294 | ffma(e, dot[r], f, xq, i, dot[r]); |
| 295 | } |
| 296 | } |
| 297 | for (uint32_t r = 0; r < R; r++) { |
| 298 | if (spec->mutant == 1) fadd(e, acc[r], acc[r], dot[r]); |
| 299 | else ffma(e, acc[r], dot[r], A.sc[s], 0, acc[r]); |
| 300 | } |
| 301 | } |
| 302 | /* warp reduction, lane 0 stores (variant 1's sequence) */ |
| 303 | for (uint32_t r = 0; r < R; r++) |
| 304 | for (uint32_t o = 16; o >= 1; o /= 2) { shfl_down(e, t, acc[r], o); fadd(e, acc[r], acc[r], t); } |
| 305 | imadw(e, pyr, rgc, R * 4u, yrow); |
| 306 | isetp_ge_u32(e, lane, one); |
| 307 | const int at_bssy = bssy_fwd(e); |
| 308 | const int bra_skip = bra_if_p0_fwd(e); |
| 309 | for (uint32_t r = 0; r < R; r++) { imadw(e, pya, one, r * 4u, pyr); stg(e, pya, 0, acc[r], udesc); } |
| 310 | const int at_bsync = here(e); |
| 311 | bsync(e); |
| 312 | patch_bra(e, bra_skip, at_bsync); |
| 313 | patch_bra(e, at_bssy, at_bsync + 1); |
| 314 | imadi(e, rgc, stride, 0, 1, rgc); |
| 315 | isetp_ge_u32(e, rgc, rgn); |
| 316 | const int bra_done = bra_if_p0_fwd(e); |
| 317 | bra(e, L_rg); |
| 318 | loop_end(e); |
| 319 | #undef ISSUE_B |
| 320 | const int L_end = here(e); |
| 321 | patch_bra(e, bra_none, L_end); |
| 322 | patch_bra(e, bra_done, L_end); |
| 323 | tail(e); |
| 324 | if (e->err) return -1; |
| 325 | return omega_bw_regalloc_solve(prog); |
| 326 | } |
| 327 | #else |
| 328 | /* API/null handling and plain memory serialization only. All construction and |
| 329 | * validation decisions live in the seeded Omega unit. The callback carries the |
| 330 | * original spec pointer synchronously through the existing replay interface. */ |
| 331 | static uint32_t moe2_unit(uint64_t *result, uint64_t ptr, uint64_t mode, |
| 332 | uint64_t unused_a, uint64_t unused_b, |
| 333 | void *records, uint64_t record_n, void *meta, uint64_t meta_n) { |
| 334 | const OmegaFp8GemvSpec *s = (const OmegaFp8GemvSpec *)(uintptr_t)ptr; |
| 335 | const uint64_t fields[16] = {s->k, s->lane_bytes, s->rows, s->warps, |
| 336 | s->depth, s->sched, s->coal, s->wfirst, (uint32_t)s->mutant, mode}; |
| 337 | (void)unused_a; (void)unused_b; |
| 338 | return omega_cg_moe2(result, fields, 16, records, record_n, meta, meta_n); |
| 339 | } |
| 340 | static uint32_t moe2_policy(const OmegaFp8GemvSpec *s, uint64_t mode) { |
| 341 | uint64_t records[8192], meta[512], result = 0; |
| 342 | if (!s || moe2_unit(&result, (uintptr_t)s, mode, 0, 0, |
| 343 | records, 8192, meta, 512) != OMEGA_OK) return 0; |
| 344 | return (uint32_t)result; |
| 345 | } |
| 346 | int omega_fp8_gemv2_spec_ok(const OmegaFp8GemvSpec *s) { return (int)moe2_policy(s, 1); } |
| 347 | uint32_t omega_fp8_gemv2_shared_bytes(const OmegaFp8GemvSpec *s) { return moe2_policy(s, 3); } |
| 348 | |
| 349 | int omega_blackwell_codegen_fp8_gemv2_prog(const OmegaFp8GemvSpec *s, BlackwellIRProgram *p) { |
| 350 | if (!s || !p) return -1; |
| 351 | if (omega_gpu_om_ir_replay(moe2_unit, (uintptr_t)s, 0, 0, 0, p, NULL)) return -1; |
| 352 | return 0; |
| 353 | } |
| 354 | #endif |
| 355 | |
| 356 | int omega_fp8_gemv2_prog_stats(const OmegaFp8GemvSpec *spec, uint32_t *insns, uint32_t *gprs) { |
| 357 | static BlackwellIRProgram prog; /* 1024 insns: too big for a stack frame in a test loop */ |
| 358 | if (omega_blackwell_codegen_fp8_gemv2_prog(spec, &prog) != 0) return -1; |
| 359 | if (insns) *insns = (uint32_t)prog.count; |
| 360 | if (gprs) *gprs = prog.regalloc.peak_gpr_usage; |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | #ifndef OMEGA_GPU_NO_POSIX |
| 365 | #include <stdlib.h> |
| 366 | int omega_blackwell_codegen_fp8_gemv2(const OmegaFp8GemvSpec *spec, OmegaBlackwellKernel *kernel) { |
| 367 | if (!kernel) return -1; |
| 368 | BlackwellIRProgram *prog = calloc(1, sizeof *prog); |
| 369 | if (!prog) return -1; |
| 370 | int rc = omega_blackwell_codegen_fp8_gemv2_prog(spec, prog); |
| 371 | size_t need = rc == 0 ? omega_bw_program_code_bytes(prog) : 0; |
| 372 | uint8_t *buf = need ? malloc(need) : NULL; |
| 373 | if (!buf) { free(prog); return -1; } |
| 374 | rc = omega_blackwell_kernel_encode_into(prog, buf, need, kernel); |
| 375 | free(prog); |
| 376 | if (rc != 0) { free(buf); return -1; } |
| 377 | return 0; /* kernel->code points into buf; omega_blackwell_kernel_free releases it */ |
| 378 | } |
| 379 | #endif |