Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/src/omega_gpu_gemm.c # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / src / omega_gpu_gemm.c` 691 lines · 37897 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* omega_gpu_gemm.c -- matrix multiply of any shape on the GB10 tensor cores (C3). See the header. */ 3 #include "omega_gpu_gemm.h" 4 #include "omega_bw_em.h" 5 #include "omega_bw_em_q.h" 6 #include "omega_gpu_session.h" 7 #include "omega_gpu_mem_cached.h" 8 #include "cudapp_native.h" 9 #include "omega_gpu_om_ir.h" 10 #ifndef CUDAPP_MIGRATION_PENDING_C_GEMM_BUILDER 11 #define CUDAPP_MIGRATION_PENDING_C_GEMM_BUILDER 0 12 #endif 13 #ifndef CUDAPP_MIGRATION_PENDING_C_GEMM_HOST_PLAN 14 #define CUDAPP_MIGRATION_PENDING_C_GEMM_HOST_PLAN 0 15 #endif 16 17 #include 18 #include 19 20 #define POISON 0x7fa00badu /* a signalling NaN no tensor-core op writes: a word still holding it was never stored */ 21 #define TIMEOUT_MS 60000ull 22 #define FRAG 512u /* bytes of one warp fragment: 32 lanes x 16 */ 23 24 #if CUDAPP_MIGRATION_PENDING_C_GEMM_HOST_PLAN 25 static uint32_t esz(OmegaGpuGemmType t) { return t == OMEGA_GPU_GEMM_E4M3 ? 1u : t == OMEGA_GPU_GEMM_F32_FFMA ? 4u : 2u; } 26 static uint32_t kstep(OmegaGpuGemmType t) { return t == OMEGA_GPU_GEMM_E4M3 ? 32u : 16u; } 27 static int pow124(uint32_t v) { return v == 1u || v == 2u || v == 4u; } 28 static int type_ok(unsigned t) { return t <= OMEGA_GPU_GEMM_F32_FFMA; } 29 static int band_ok(uint32_t b) { return b <= 32u && (b & (b - 1u)) == 0u; } 30 #else 31 static uint64_t host_value(uint64_t op, uint64_t a, uint64_t b, uint64_t c, uint64_t d) { 32 uint64_t result; 33 if (omega_gemm_value(&result, op, a, b, c, d) != OMEGA_OK) abort(); 34 return result; 35 } 36 static void host_cfg(uint64_t x[32], const OmegaGemmCfg *c) { 37 memset(x, 0, 32 * sizeof *x); 38 x[0] = (unsigned)c->type; x[1] = c->rt; x[2] = c->ct; x[3] = c->wm; 39 x[4] = c->wn; x[5] = c->stall; x[6] = (unsigned)c->mutant; x[7] = c->band; 40 } 41 static uint32_t esz(OmegaGpuGemmType t) { return (uint32_t)host_value(0, (unsigned)t, 0, 0, 0); } 42 #if CUDAPP_MIGRATION_PENDING_C_GEMM_BUILDER 43 static uint32_t kstep(OmegaGpuGemmType t) { return (uint32_t)host_value(1, (unsigned)t, 0, 0, 0); } 44 #endif 45 static int type_ok(unsigned t) { return (int)host_value(2, t, 0, 0, 0); } 46 #endif 47 #define FFMA_THREADS 128u 48 49 void omega_gpu_gemm_default_cfg(OmegaGpuGemmType type, OmegaGemmCfg *cfg) { 50 memset(cfg, 0, sizeof *cfg); 51 /* rt 2 ct 4, 2 x 4 warps, column bands of 8 CTA tiles: the fastest of the GemmTune exploration sweeps on GB10 52 * (evidence/GEMM-TUNE-*); stall 4 bf16 and f16, 2 e4m3 as before (F32_FFMA has its own grid and ignores the tile) */ 53 cfg->type = type; cfg->rt = 2; cfg->ct = 4; cfg->wm = 2; cfg->wn = 4; cfg->band = 8; 54 cfg->stall = type == OMEGA_GPU_GEMM_E4M3 ? 2u : 4u; 55 } 56 57 /* registers of MAIN for operands and accumulators: two operand sets (4 rt + 2 ct quads) and rt ct accumulator quads; at most 58 * 160 (rt 4 ct 4). Up to MAIN_REGS_DEFAULT they fit R2..R127 (BW_PHYS_GPR_MAX) beside the ~25 address and loop registers; 59 * above it MAIN opts into BW_PHYS_GPR_HW_LIMIT (R2..R245). */ 60 static uint32_t main_regs(const OmegaGemmCfg *c) { 61 #if CUDAPP_MIGRATION_PENDING_C_GEMM_HOST_PLAN 62 return 4u * (4u * c->rt + 2u * c->ct + c->rt * c->ct); 63 #else 64 return (uint32_t)host_value(3, c->rt, c->ct, 0, 0); 65 #endif 66 } 67 #define MAIN_REGS_DEFAULT 100u 68 69 int omega_gpu_gemm_cfg_ok(const OmegaGemmCfg *c) { 70 #if CUDAPP_MIGRATION_PENDING_C_GEMM_HOST_PLAN 71 return c && type_ok(c->type) && pow124(c->rt) && pow124(c->ct) && 72 pow124(c->wm) && pow124(c->wn) && c->stall >= 1u && c->stall <= 15u && c->mutant >= 0 && c->mutant <= 3 && 73 band_ok(c->band); 74 #else 75 uint64_t x[32], result; 76 if (!c) return 0; 77 host_cfg(x, c); 78 return omega_gemm_cfg(&result, x, 32) == OMEGA_OK && result != 0; 79 #endif 80 } 81 82 /* ------------------------------------------------------------------ IR helpers */ 83 84 #if CUDAPP_MIGRATION_PENDING_C_GEMM_BUILDER 85 typedef struct { int bssy, bra; } Region; 86 static Region skip_if_p0(Em *e) { Region r; r.bssy = bssy_fwd(e); r.bra = bra_if_p0_fwd(e); return r; } 87 static void region_end(Em *e, Region r) { int at = here(e); bsync(e); patch_bra(e, r.bra, at); patch_bra(e, r.bssy, at + 1); } 88 /* d = a - b */ 89 static void isub(Em *e, int d, int a, int b) { imadi(e, d, b, 0, 0xffffffffu, a); } 90 /* d = 1 when a < b (both below 2^31), else 0 */ 91 static void lt_bit(Em *e, int d, int a, int b) { isub(e, d, a, b); shr(e, d, d, 0, 31); } 92 static void and_r(Em *e, int d, int a, int b) { lop3(e, d, a, b, NOREG, 0xc0u); } 93 /* tensor-core op d = a * b[bsub..bsub+1] + c: HMMA.16816.F32.BF16, HMMA.16816.F32 (fp16 operands) or QMMA.16832 (patched) */ 94 static void mma(Em *e, OmegaGpuGemmType t, int d, int a, int b, unsigned bsub, int c, uint32_t stall) { 95 BlackwellIRInsn i = I0(t == OMEGA_GPU_GEMM_F16 ? BW_IR_HMMA_F16 : BW_IR_HMMA_BF16); 96 i.dst_vreg = d; i.src1_vreg = a; i.src2_vreg = b; i.src2_subreg = (uint8_t)bsub; i.src3_vreg = c; 97 if (t == OMEGA_GPU_GEMM_E4M3) i.imm = OMEGA_QMMA_MARK; 98 i.control = CW(stall, SB_NONE, SB_NONE, e->pending); e->pending = 0; 99 if (omega_bw_ir_append(e->p, &i) < 0) e->err = -1; 100 } 101 102 /* ------------------------------------------------------------------ PACK_A / PACK_B 103 * CTA (pair cx, row group cy), 128 threads; thread tid writes word tid of each of the F fragments of the block: 104 * lane L = tid / 4, register q = tid % 4, g = L / 4, t = L % 4, r = elements per register (2 bf16, 4 e4m3). 105 * A fragment f = u * rt + i: row 16 (rt cy + i) + g + 8 (q & 1), k = 2 step cx + step u + r t + 4 r (q >> 1) 106 * B fragment f = j: row 8 (ct cy + j) + g, k = 2 step cx + step (q >> 1) + r t + 4 r (q & 1) 107 * Element (row, k) of the source is at (row * row_stride + k * k_stride) * esz; past rows or kdim it is 0. */ 108 static int gen_pack(Em *e, const OmegaGemmCfg *c, int isb) { 109 const int fp8 = c->type == OMEGA_GPU_GEMM_E4M3; 110 const uint32_t ks = kstep(c->type), r = 4u / esz(c->type), F = isb ? c->ct : 2u * c->rt; 111 int udesc = UV64(e), psrc = V64(e), pdst = V64(e), R = V(e), Kd = V(e), sr = V(e), sk = V(e), ks2 = V(e); 112 int tid = V(e), cx = V(e), cy = V(e), one = V(e), g = V(e), t = V(e), qa = V(e), qb = V(e), row0 = V(e), k0 = V(e); 113 int tmp = V(e), pd = V64(e), row = V(e), vrow = V(e), rowoff = V(e), kx = V(e), v = V(e), off = V(e), word = V(e); 114 int addr[4], val[4], mask[4], odd[4]; 115 for (unsigned x = 0; x < 4; x++) { addr[x] = V64(e); val[x] = V(e); mask[x] = V(e); odd[x] = V(e); } 116 if (e->err) return -1; 117 ldcu64(e, udesc, 0x358); 118 ldc64(e, psrc, 0x380); ldc64(e, pdst, 0x388); 119 ldc32(e, R, 0x390); ldc32(e, Kd, 0x394); ldc32(e, sr, 0x398); ldc32(e, sk, 0x39c); ldc32(e, ks2, 0x3a0); 120 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); 121 movi(e, one, 1); 122 shr(e, g, tid, 0, 4); 123 shr(e, t, tid, 0, 2); andi(e, t, t, 0, 3); 124 andi(e, qa, tid, 0, 1); 125 shr(e, qb, tid, 0, 1); andi(e, qb, qb, 0, 1); 126 if (!isb) { 127 imadi(e, row0, cy, 0, 16u * c->rt, g); imadi(e, row0, qa, 0, 8u, row0); 128 imadi(e, k0, cx, 0, 2u * ks, NOREG); imadi(e, k0, t, 0, r, k0); imadi(e, k0, qb, 0, 4u * r, k0); 129 } else { 130 imadi(e, row0, cy, 0, 8u * c->ct, g); 131 imadi(e, k0, cx, 0, 2u * ks, NOREG); imadi(e, k0, t, 0, r, k0); imadi(e, k0, qb, 0, ks, k0); imadi(e, k0, qa, 0, 4u * r, k0); 132 } 133 /* this thread's word of fragment 0: ((cy * KS2 + cx) * F * 128 + tid) words */ 134 imadr(e, tmp, cy, ks2, cx); imadi(e, tmp, tmp, 0, F * 128u, tid); imadw(e, pd, tmp, 4u, pdst); 135 for (uint32_t f = 0; f < F; f++) { 136 const uint32_t drow = isb ? 8u * f : 16u * (f % c->rt), dk = isb ? 0u : ks * (f / c->rt); 137 imadi(e, row, one, 0, drow, row0); 138 lt_bit(e, vrow, row, R); 139 imadr(e, rowoff, row, sr, NOREG); 140 for (uint32_t x = 0; x < r; x++) { 141 imadi(e, kx, one, 0, dk + x, k0); 142 lt_bit(e, v, kx, Kd); 143 and_r(e, v, v, vrow); 144 imadr(e, off, kx, sk, rowoff); 145 imadr(e, off, off, v, NOREG); /* outside: element 0, always readable, then masked */ 146 if (c->mutant == 2) movi(e, mask[x], 0xffffffffu); /* the red check: element 0 leaks in past the edges */ 147 else imadi(e, mask[x], v, 0, 0xffffffffu, NOREG); 148 if (fp8) { andi(e, odd[x], off, 0, 1u); andi(e, off, off, 0, 0xfffffffeu); imadw(e, addr[x], off, 1u, psrc); } 149 else imadw(e, addr[x], off, 2u, psrc); 150 } 151 for (uint32_t x = 0; x < r; x++) ldg_wide(e, BW_IR_LDG_E_U16, val[x], addr[x], 0, udesc, x == 0); 152 for (uint32_t x = 0; x < r; x++) { 153 if (fp8) { /* the byte at off: low or high half of the u16 */ 154 andi(e, kx, val[x], 0, 0xffu); 155 shr(e, v, val[x], 0, 8); andi(e, v, v, 0, 0xffu); 156 isub(e, v, v, kx); 157 imadr(e, val[x], v, odd[x], kx); 158 } 159 and_r(e, val[x], val[x], mask[x]); 160 } 161 imadi(e, word, val[0], 0, 1u, NOREG); 162 for (uint32_t x = 1; x < r; x++) imadi(e, word, val[x], 0, 1u << (x * (32u / r)), word); 163 stg(e, pd, f * FRAG, word, udesc); 164 } 165 tail(e); 166 return e->err; 167 } 168 169 /* ------------------------------------------------------------------ MAIN */ 170 static int gen_main(Em *e, const OmegaGemmCfg *c) { 171 const OmegaGpuGemmType ty = c->type; 172 const uint32_t RT = c->rt, CT = c->ct, FA = 2u * RT, FB = CT; 173 int udesc = UV64(e), pa = V64(e), pb = V64(e), pc = V64(e), M = V(e), N = V(e), ks2 = V(e); 174 int tid = V(e), cx = V(e), cy = V(e), warp = V(e), lane = V(e), g = V(e), t = V(e), one = V(e), zr = V(e); 175 int rga = V(e), rgb = V(e), tmp = V(e), cnt = V(e); 176 int acc[16], as[2][8], bs[2][4]; 177 for (uint32_t x = 0; x < RT * CT; x++) acc[x] = V128(e); 178 for (unsigned s = 0; s < 2; s++) { for (uint32_t f = 0; f < FA; f++) as[s][f] = V128(e); for (uint32_t j = 0; j < FB; j++) bs[s][j] = V128(e); } 179 if (e->err) return -1; 180 ldcu64(e, udesc, 0x358); 181 ldc64(e, pa, 0x380); ldc64(e, pb, 0x388); ldc64(e, pc, 0x390); 182 ldc32(e, M, 0x398); ldc32(e, N, 0x39c); ldc32(e, ks2, 0x3a0); 183 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); 184 movi(e, one, 1); movrz(e, zr); 185 if (c->band > 1u) { 186 /* banded grid: ctaid.x = tile row * band + column within the band, ctaid.y = band; tile column = band * band index 187 * + column within the band. The last band may stick out past the tile columns: those CTAs leave at once. */ 188 int gx = V(e); 189 if (e->err) return -1; 190 ldc32(e, gx, 0x3a4); 191 andi(e, tmp, cx, 0, c->band - 1u); 192 imadi(e, rgb, cy, 0, c->band, tmp); 193 shr(e, cy, cx, 0, (uint32_t)log2u(c->band)); 194 isetp_ge_u32(e, rgb, gx); 195 { BlackwellIRInsn x = I0(BW_IR_EXIT); x.predicate_p0 = true; emit(e, x, K_EXIT); } 196 imadi(e, cx, rgb, 0, 1u, NOREG); 197 } 198 shr(e, warp, tid, 0, 5); andi(e, lane, tid, 0, 31u); 199 /* warp (wm, wn) = (warp / WN, warp % WN); row groups rga = cy WM + wm, rgb = cx WN + wn */ 200 shr(e, tmp, warp, 0, (uint32_t)log2u(c->wn)); imadi(e, rga, cy, 0, c->wm, tmp); 201 andi(e, tmp, warp, 0, c->wn - 1u); imadi(e, rgb, cx, 0, c->wn, tmp); 202 imadr(e, tmp, rga, ks2, NOREG); imadw(e, pa, tmp, FA * FRAG, pa); 203 imadr(e, tmp, rgb, ks2, NOREG); imadw(e, pb, tmp, FB * FRAG, pb); 204 imadi(e, tmp, lane, 0, 16u, NOREG); imadw(e, pa, tmp, 1u, pa); imadw(e, pb, tmp, 1u, pb); 205 for (uint32_t x = 0; x < RT * CT; 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); } 206 shr(e, cnt, ks2, 0, 1); /* loop iterations: two k pairs each */ 207 208 #define ISSUE(s, sb) do { \ 209 for (uint32_t f = 0; f < FA; f++) ldw_pf(e, BW_IR_LDG_E_128, as[s][f], pa, f * FRAG, udesc, (sb)); \ 210 for (uint32_t j = 0; j < FB; j++) ldw_pf(e, BW_IR_LDG_E_128, bs[s][j], pb, j * FRAG, udesc, (sb)); \ 211 imadw(e, pa, one, FA * FRAG, pa); imadw(e, pb, one, FB * FRAG, pb); } while (0) 212 #define COMPUTE(s) do { \ 213 for (uint32_t u = 0; u < 2; u++) { \ 214 if (c->mutant == 1 && u == 1) break; \ 215 for (uint32_t i = 0; i < RT; i++) for (uint32_t j = 0; j < CT; j++) \ 216 mma(e, ty, acc[i * CT + j], as[s][u * RT + i], bs[s][j], 2u * u, acc[i * CT + j], c->stall); } } while (0) 217 218 ISSUE(0, 3u); 219 const int top = here(e); 220 loop_begin(e); 221 ISSUE(1, 4u); 222 consume(e, 3); 223 COMPUTE(0); 224 nop(e, 15); /* the tensor ops have read set 0 before it is reloaded */ 225 ISSUE(0, 3u); /* the last iteration reads one pair past the end: slack */ 226 consume(e, 4); 227 COMPUTE(1); 228 nop(e, 15); 229 imadi(e, cnt, one, 0, 0xffffffffu, cnt); 230 isetp_ge_u32(e, cnt, one); 231 bra_if_p0(e, top); 232 loop_end(e); 233 #undef ISSUE 234 #undef COMPUTE 235 consume(e, 3); 236 nop(e, 15); nop(e, 15); nop(e, 15); nop(e, 15); /* the last results settle before the stores read them */ 237 238 /* stores: C[m][n] for m = 16 (RT rga + i) + 8 h + g, n = 8 (CT rgb + j) + 2 t + cc, when m < M and n < N */ 239 int mb = V(e), nb = V(e), m = V(e), n = V(e), vm = V(e), vn = V(e), rowoff = V(e), ca = V64(e); 240 if (e->err) return -1; 241 shr(e, g, lane, 0, 2); andi(e, t, lane, 0, 3u); 242 imadi(e, mb, rga, 0, 16u * RT, g); 243 imadi(e, tmp, t, 0, 2u, NOREG); imadi(e, nb, rgb, 0, 8u * CT, tmp); 244 for (uint32_t i = 0; i < RT; i++) for (uint32_t h = 0; h < 2; h++) { 245 imadi(e, m, one, 0, 16u * i + 8u * h, mb); 246 lt_bit(e, vm, m, M); 247 imadr(e, rowoff, m, N, NOREG); 248 for (uint32_t j = 0; j < CT; j++) for (uint32_t cc = 0; cc < 2; cc++) { 249 imadi(e, n, one, 0, 8u * j + cc, nb); 250 lt_bit(e, vn, n, N); 251 and_r(e, vn, vn, vm); 252 isetp_ge_u32(e, zr, vn); /* P0: 0 >= ok, i.e. outside: skip */ 253 Region rg = skip_if_p0(e); 254 iadd(e, tmp, rowoff, n); imadw(e, ca, tmp, 4u, pc); 255 BlackwellIRInsn st = I0(BW_IR_STG_E); st.src1_vreg = ca; st.src2_vreg = acc[i * CT + j]; 256 st.src2_subreg = (uint8_t)(2u * h + (c->mutant == 3 ? 1u - cc : cc)); st.ureg = udesc; emit(e, st, K_STORE); 257 region_end(e, rg); 258 } 259 } 260 tail(e); 261 return e->err; 262 } 263 #endif 264 265 /* ------------------------------------------------------------------ F32_FFMA (the reference path, GemmGeneral) 266 * CTA (cx, row m = cy), FFMA_THREADS threads; thread tid owns C[m][n], n = cx FFMA_THREADS + tid, and runs 267 * acc = +0; for k = 0 .. K-1: acc = fma(A[m][k], B[k][n], acc) with one FFMA (IEEE round-to-nearest, fused) per k. 268 * A is dense M x K, B dense K x N, both f32 row-major. Lanes past N read column 0 and do not store. 269 * mutant 1 (the red check): FADD instead of FFMA, the product never formed. */ 270 #if CUDAPP_MIGRATION_PENDING_C_GEMM_BUILDER 271 static int gen_ffma(Em *e, const OmegaGemmCfg *c) { 272 int udesc = UV64(e), pa = V64(e), pb = V64(e), pc = V64(e), M = V(e), N = V(e), K = V(e); 273 int tid = V(e), cx = V(e), cy = V(e), one = V(e), zr = V(e), n = V(e), vn = V(e), nc = V(e), tmp = V(e); 274 int acc = V(e), av = V(e), bv = V(e), cnt = V(e), ca = V64(e); 275 if (e->err) return -1; 276 ldcu64(e, udesc, 0x358); 277 ldc64(e, pa, 0x380); ldc64(e, pb, 0x388); ldc64(e, pc, 0x390); 278 ldc32(e, M, 0x398); ldc32(e, N, 0x39c); ldc32(e, K, 0x3a0); 279 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y); 280 movi(e, one, 1); movrz(e, zr); 281 imadi(e, n, cx, 0, FFMA_THREADS, tid); 282 lt_bit(e, vn, n, N); 283 imadr(e, nc, n, vn, NOREG); /* past N: column 0, always readable */ 284 imadr(e, tmp, cy, K, NOREG); imadw(e, pa, tmp, 4u, pa); /* row m of A */ 285 imadw(e, pb, nc, 4u, pb); /* column n of B */ 286 movrz(e, acc); 287 imadi(e, cnt, K, 0, 1u, NOREG); 288 const int top = here(e); 289 loop_begin(e); 290 ldg32(e, av, pa, udesc, 1); 291 ldg32(e, bv, pb, udesc, 0); 292 if (c->mutant == 1) fadd(e, acc, av, acc); 293 else ffma(e, acc, av, bv, 0, acc); 294 imadw(e, pa, one, 4u, pa); 295 imadw(e, pb, N, 4u, pb); 296 imadi(e, cnt, one, 0, 0xffffffffu, cnt); 297 isetp_ge_u32(e, cnt, one); 298 bra_if_p0(e, top); 299 loop_end(e); 300 isetp_ge_u32(e, zr, vn); /* P0: outside N, skip the store */ 301 Region rg = skip_if_p0(e); 302 imadr(e, tmp, cy, N, n); imadw(e, ca, tmp, 4u, pc); 303 stg(e, ca, 0, acc, udesc); 304 region_end(e, rg); 305 (void)M; 306 tail(e); 307 return e->err; 308 } 309 #endif 310 311 /* Checked Omega records are replayed into the existing allocator/encoder. No GPU arithmetic lives here. */ 312 #if !CUDAPP_MIGRATION_PENDING_C_GEMM_BUILDER 313 static int gen_omega(const OmegaGemmCfg *c, OmegaGemmKind kind, BlackwellIRProgram *p) { 314 uint64_t cfg[8] = { c->type, c->rt, c->ct, c->wm, c->wn, c->stall, (uint64_t)c->mutant, c->band }; 315 uint64_t ins[8192], meta[512], result; 316 if (omega_gemm_any_ir(&result, cfg, 8, kind, ins, 8192, meta, 512) || result || 317 meta[0] > 1024 || meta[1] > 256 || meta[2] != 1 || meta[4] || meta[5] > 1) return -1; 318 for (uint64_t j = 0; j < meta[1]; j++) { 319 uint64_t k = meta[64 + j]; 320 int v = k == 4 ? omega_bw_ir_alloc_vreg128(p) : k == 2 ? omega_bw_ir_alloc_vreg64(p) : 321 k == 1 ? omega_bw_ir_alloc_vreg(p) : -1; 322 if (v != (int)j) return -1; 323 } 324 if (omega_bw_ir_alloc_uvreg64(p) != 0) return -1; 325 for (uint64_t j = 0; j < meta[0]; j++) { 326 uint64_t *r = ins + j * 8; 327 BlackwellIRInsn i = I0((BlackwellIROpcode)r[0]); 328 i.dst_vreg = r[1] == UINT64_MAX ? -1 : (int)r[1]; 329 i.src1_vreg = r[2] == UINT64_MAX ? -1 : (int)r[2]; 330 i.src2_vreg = r[3] == UINT64_MAX ? -1 : (int)r[3]; 331 i.src3_vreg = r[4] == UINT64_MAX ? -1 : (int)r[4]; 332 i.ureg = r[5] == UINT64_MAX ? -1 : (int)r[5]; 333 i.imm = (uint32_t)r[6]; i.control = (uint32_t)(r[6] >> 32); 334 i.is_uniform = (r[7] & 1) != 0; i.predicate_p0 = (r[7] & 2) != 0; i.predicate_not = (r[7] & 4) != 0; 335 i.dst_subreg = (uint8_t)(r[7] >> 16); i.src1_subreg = (uint8_t)(r[7] >> 24); 336 i.src2_subreg = (uint8_t)(r[7] >> 32); i.src3_subreg = (uint8_t)(r[7] >> 40); 337 if (omega_bw_ir_append(p, &i) < 0) return -1; 338 } 339 if (meta[5]) { 340 /* Apply the standard back-edge lifetime fixup at the loop end, before later stores. */ 341 int end = (int)meta[17]; 342 for (int v = 0; v < p->regalloc.num_vregs; v++) { 343 OmegaLiveInterval *iv = &p->regalloc.intervals[v]; 344 if (iv->first_def >= 0 && iv->first_def < (int)meta[16] && iv->last_use >= (int)meta[16] && iv->last_use < end) 345 iv->last_use = end; 346 } 347 } 348 return 0; 349 } 350 #endif 351 352 int omega_gpu_gemm_codegen(const OmegaGemmCfg *cfg, OmegaGemmKind kind, BlackwellIRProgram *prog) { 353 if (!omega_gpu_gemm_cfg_ok(cfg) || !prog || (unsigned)kind >= OMEGA_GEMM_K_COUNT) return -1; 354 if (cfg->type == OMEGA_GPU_GEMM_F32_FFMA && kind != OMEGA_GEMM_K_MAIN) return -1; /* no packs */ 355 omega_bw_ir_init(prog); 356 if (kind == OMEGA_GEMM_K_MAIN && main_regs(cfg) > MAIN_REGS_DEFAULT && omega_bw_ir_set_gpr_limit(prog, BW_PHYS_GPR_HW_LIMIT) != 0) return -1; 357 Em em; memset(&em, 0, sizeof em); em.p = prog; 358 int rc; 359 #if CUDAPP_MIGRATION_PENDING_C_GEMM_BUILDER 360 rc = cfg->type == OMEGA_GPU_GEMM_F32_FFMA ? gen_ffma(&em, cfg) 361 : kind == OMEGA_GEMM_K_MAIN ? gen_main(&em, cfg) : gen_pack(&em, cfg, kind == OMEGA_GEMM_K_PACK_B); 362 #else 363 if (cfg->type == OMEGA_GPU_GEMM_F32_FFMA) 364 rc = omega_gpu_om_ir_replay(omega_gemm_scalar, 0, (uint64_t)cfg->mutant, 0, 0, prog, NULL); 365 else rc = gen_omega(cfg, kind, prog); 366 #endif 367 if (rc != 0 || em.err || em.depth != 0) return -1; 368 return omega_bw_regalloc_solve(prog); 369 } 370 371 /* ------------------------------------------------------------------ plan and driver */ 372 373 #if CUDAPP_MIGRATION_PENDING_C_GEMM_HOST_PLAN 374 static uint32_t roundup(uint32_t v, uint32_t m) { return (v + m - 1u) / m * m; } 375 376 int omega_gpu_gemm_plan(const OmegaGemmCfg *c, uint32_t M, uint32_t N, uint32_t K, OmegaGemmPlan *p) { 377 if (!omega_gpu_gemm_cfg_ok(c) || !p || !M || !N || !K) return OMEGA_GPU_GEMM_BAD_ARGS; 378 if (M > OMEGA_GPU_GEMM_MAX_DIM || N > OMEGA_GPU_GEMM_MAX_DIM || K > OMEGA_GPU_GEMM_MAX_DIM) return OMEGA_GPU_GEMM_TOO_LARGE; 379 memset(p, 0, sizeof *p); 380 const uint32_t ks = kstep(c->type), ga = 16u * c->rt, gb = 8u * c->ct, FA = 2u * c->rt, FB = c->ct; 381 p->Mp = roundup(M, ga * c->wm); p->Np = roundup(N, gb * c->wn); p->Kp = roundup(K, 4u * ks); 382 p->KS2 = p->Kp / (2u * ks); 383 p->a_bytes = (uint64_t)M * K * esz(c->type); p->b_bytes = (uint64_t)K * N * esz(c->type); 384 p->ap_bytes = (uint64_t)(p->Mp / ga) * p->KS2 * FA * FRAG + 2u * FA * FRAG; 385 p->bp_bytes = (uint64_t)(p->Np / gb) * p->KS2 * FB * FRAG + 2u * FB * FRAG; 386 p->grid[OMEGA_GEMM_K_PACK_A][0] = p->KS2; p->grid[OMEGA_GEMM_K_PACK_A][1] = p->Mp / ga; 387 p->grid[OMEGA_GEMM_K_PACK_B][0] = p->KS2; p->grid[OMEGA_GEMM_K_PACK_B][1] = p->Np / gb; 388 const uint32_t gx = p->Np / (gb * c->wn), gy = p->Mp / (ga * c->wm); 389 p->grid[OMEGA_GEMM_K_MAIN][0] = gx; p->grid[OMEGA_GEMM_K_MAIN][1] = gy; 390 if (c->band > 1u) { p->grid[OMEGA_GEMM_K_MAIN][0] = c->band * gy; p->grid[OMEGA_GEMM_K_MAIN][1] = (gx + c->band - 1u) / c->band; } 391 p->threads[OMEGA_GEMM_K_PACK_A] = p->threads[OMEGA_GEMM_K_PACK_B] = 128u; 392 p->threads[OMEGA_GEMM_K_MAIN] = 32u * c->wm * c->wn; 393 if (c->type == OMEGA_GPU_GEMM_F32_FFMA) { /* no packs: one thread per C element */ 394 p->ap_bytes = p->bp_bytes = 0; 395 memset(p->grid, 0, sizeof p->grid); memset(p->threads, 0, sizeof p->threads); 396 p->grid[OMEGA_GEMM_K_MAIN][0] = (N + FFMA_THREADS - 1u) / FFMA_THREADS; p->grid[OMEGA_GEMM_K_MAIN][1] = M; 397 p->threads[OMEGA_GEMM_K_MAIN] = FFMA_THREADS; 398 } 399 return OMEGA_GPU_GEMM_OK; 400 } 401 402 static void put64(uint32_t *a, unsigned w, uint64_t v) { a[w] = (uint32_t)v; a[w + 1] = (uint32_t)(v >> 32); } 403 404 /* elements one operand spans: rows of the stored layout times its leading dimension (0: dense) */ 405 static int ld_span(int col, uint32_t rows_m, uint32_t K, uint64_t ld, uint64_t *span, uint64_t *eff) { 406 const uint64_t outer = col ? K : rows_m, inner = col ? rows_m : K; /* stored rows, stored row length */ 407 if (ld == 0) ld = inner; 408 if (ld < inner || ld > 0xffffffffull) return -1; 409 *eff = ld; *span = (outer - 1u) * ld + inner; 410 return *span > 0xffffffffull ? -1 : 0; /* the packs index elements with 32-bit arithmetic */ 411 } 412 413 int omega_gpu_gemm_span(unsigned layout, uint32_t M, uint32_t N, uint32_t K, uint64_t lda, uint64_t ldb, uint64_t *a_elems, uint64_t *b_elems) { 414 uint64_t ea, eb, sa, sb; 415 if ((layout & ~3u) || !M || !N || !K || ld_span(layout & OMEGA_GPU_GEMM_A_COL, M, K, lda, &sa, &ea) != 0 || 416 ld_span((layout & OMEGA_GPU_GEMM_B_COL) == 0, N, K, ldb, &sb, &eb) != 0) return OMEGA_GPU_GEMM_BAD_ARGS; 417 if (a_elems) *a_elems = sa; 418 if (b_elems) *b_elems = sb; 419 return OMEGA_GPU_GEMM_OK; 420 } 421 422 int omega_gpu_gemm_drive_ld(const OmegaGemmCfg *c, unsigned layout, uint32_t M, uint32_t N, uint32_t K, uint64_t a_va, uint64_t lda, 423 uint64_t b_va, uint64_t ldb, uint64_t ap_va, uint64_t bp_va, uint64_t c_va, OmegaGemmLaunchFn launch, void *ctx) { 424 if (!launch || (layout & ~3u)) return OMEGA_GPU_GEMM_BAD_ARGS; 425 OmegaGemmPlan p; 426 int rc = omega_gpu_gemm_plan(c, M, N, K, &p); 427 if (rc != OMEGA_GPU_GEMM_OK) return rc; 428 uint64_t ea, eb, sp; 429 if (ld_span(layout & OMEGA_GPU_GEMM_A_COL, M, K, lda, &sp, &ea) != 0 || 430 ld_span((layout & OMEGA_GPU_GEMM_B_COL) == 0, N, K, ldb, &sp, &eb) != 0) return OMEGA_GPU_GEMM_BAD_ARGS; 431 uint32_t a[OMEGA_GPU_GEMM_ARGS_WORDS]; 432 if (c->type == OMEGA_GPU_GEMM_F32_FFMA) { /* dense row-major A and B only */ 433 if (layout || ea != K || eb != N) return OMEGA_GPU_GEMM_BAD_ARGS; 434 memset(a, 0, sizeof a); 435 put64(a, 0, a_va); put64(a, 2, b_va); put64(a, 4, c_va); a[6] = M; a[7] = N; a[8] = K; 436 return launch(ctx, OMEGA_GEMM_K_MAIN, a, p.grid[2][0], p.grid[2][1], p.threads[2]) != 0 ? OMEGA_GPU_GEMM_CHIP_FAIL : OMEGA_GPU_GEMM_OK; 437 } 438 /* A element (m, k): row-major m lda + k, column-major k lda + m */ 439 memset(a, 0, sizeof a); 440 put64(a, 0, a_va); put64(a, 2, ap_va); a[4] = M; a[5] = K; 441 a[6] = layout & OMEGA_GPU_GEMM_A_COL ? 1u : (uint32_t)ea; a[7] = layout & OMEGA_GPU_GEMM_A_COL ? (uint32_t)ea : 1u; a[8] = p.KS2; 442 if (launch(ctx, OMEGA_GEMM_K_PACK_A, a, p.grid[0][0], p.grid[0][1], p.threads[0]) != 0) return OMEGA_GPU_GEMM_CHIP_FAIL; 443 /* B element (k, n) as packed row n: row-major k ldb + n, column-major n ldb + k */ 444 memset(a, 0, sizeof a); 445 put64(a, 0, b_va); put64(a, 2, bp_va); a[4] = N; a[5] = K; 446 a[6] = layout & OMEGA_GPU_GEMM_B_COL ? (uint32_t)eb : 1u; a[7] = layout & OMEGA_GPU_GEMM_B_COL ? 1u : (uint32_t)eb; a[8] = p.KS2; 447 if (launch(ctx, OMEGA_GEMM_K_PACK_B, a, p.grid[1][0], p.grid[1][1], p.threads[1]) != 0) return OMEGA_GPU_GEMM_CHIP_FAIL; 448 memset(a, 0, sizeof a); 449 put64(a, 0, ap_va); put64(a, 2, bp_va); put64(a, 4, c_va); a[6] = M; a[7] = N; a[8] = p.KS2; 450 a[9] = p.Np / (8u * c->ct * c->wn); 451 if (launch(ctx, OMEGA_GEMM_K_MAIN, a, p.grid[2][0], p.grid[2][1], p.threads[2]) != 0) return OMEGA_GPU_GEMM_CHIP_FAIL; 452 return OMEGA_GPU_GEMM_OK; 453 } 454 #else 455 int omega_gpu_gemm_plan(const OmegaGemmCfg *c, uint32_t M, uint32_t N, uint32_t K, OmegaGemmPlan *p) { 456 uint64_t x[32], y[64], result; 457 if (!c || !p) return OMEGA_GPU_GEMM_BAD_ARGS; 458 host_cfg(x, c); x[8] = M; x[9] = N; x[10] = K; 459 if (omega_gemm_plan(&result, x, 32, y, 64) != OMEGA_OK) return OMEGA_GPU_GEMM_BAD_ARGS; 460 if (result) return -(int)result; 461 memset(p, 0, sizeof *p); 462 p->Mp = (uint32_t)y[0]; p->Np = (uint32_t)y[1]; p->Kp = (uint32_t)y[2]; p->KS2 = (uint32_t)y[3]; 463 p->a_bytes = y[4]; p->b_bytes = y[5]; p->ap_bytes = y[6]; p->bp_bytes = y[7]; 464 for (unsigned k = 0; k < 3; k++) { 465 p->grid[k][0] = (uint32_t)y[8 + 2*k]; p->grid[k][1] = (uint32_t)y[9 + 2*k]; 466 p->threads[k] = (uint32_t)y[14 + k]; 467 } 468 return OMEGA_GPU_GEMM_OK; 469 } 470 int omega_gpu_gemm_span(unsigned layout, uint32_t M, uint32_t N, uint32_t K, uint64_t lda, uint64_t ldb, 471 uint64_t *a_elems, uint64_t *b_elems) { 472 uint64_t x[32] = {0}, y[64], result; 473 x[8] = M; x[9] = N; x[10] = K; x[11] = layout; x[12] = lda; x[13] = ldb; 474 if (omega_gemm_span(&result, x, 32, y, 64) != OMEGA_OK) return OMEGA_GPU_GEMM_BAD_ARGS; 475 if (result) return -(int)result; 476 if (a_elems) *a_elems = y[17]; 477 if (b_elems) *b_elems = y[18]; 478 return OMEGA_GPU_GEMM_OK; 479 } 480 int omega_gpu_gemm_drive_ld(const OmegaGemmCfg *c, unsigned layout, uint32_t M, uint32_t N, uint32_t K, uint64_t a_va, uint64_t lda, 481 uint64_t b_va, uint64_t ldb, uint64_t ap_va, uint64_t bp_va, uint64_t c_va, OmegaGemmLaunchFn launch, void *ctx) { 482 uint64_t x[32], y[64], result; 483 if (!c || !launch) return OMEGA_GPU_GEMM_BAD_ARGS; 484 host_cfg(x, c); x[8] = M; x[9] = N; x[10] = K; x[11] = layout; x[12] = lda; x[13] = ldb; 485 x[14] = a_va; x[15] = b_va; x[16] = ap_va; x[17] = bp_va; x[18] = c_va; 486 if (omega_gemm_host_drive(&result, x, 32, y, 64) != OMEGA_OK) return OMEGA_GPU_GEMM_BAD_ARGS; 487 if (result) return -(int)result; 488 for (unsigned k = (unsigned)y[54]; k < 3; k++) { 489 uint32_t args[OMEGA_GPU_GEMM_ARGS_WORDS]; 490 for (unsigned w = 0; w < OMEGA_GPU_GEMM_ARGS_WORDS; w++) args[w] = (uint32_t)y[24 + 10*k + w]; 491 if (launch(ctx, (OmegaGemmKind)k, args, (uint32_t)y[8+2*k], (uint32_t)y[9+2*k], (uint32_t)y[14+k])) 492 return OMEGA_GPU_GEMM_CHIP_FAIL; 493 } 494 return OMEGA_GPU_GEMM_OK; 495 } 496 #endif 497 498 int omega_gpu_gemm_drive(const OmegaGemmCfg *c, unsigned layout, uint32_t M, uint32_t N, uint32_t K, uint64_t a_va, 499 uint64_t b_va, uint64_t ap_va, uint64_t bp_va, uint64_t c_va, OmegaGemmLaunchFn launch, void *ctx) { 500 return omega_gpu_gemm_drive_ld(c, layout, M, N, K, a_va, 0, b_va, 0, ap_va, bp_va, c_va, launch, ctx); 501 } 502 503 /* ------------------------------------------------------------------ the chip */ 504 505 #define NTYPES 4u 506 static struct { 507 int set[NTYPES]; 508 OmegaGemmCfg cfg[NTYPES]; 509 OmegaGpuMem code[NTYPES][OMEGA_GEMM_K_COUNT]; 510 uint32_t gpr[NTYPES][OMEGA_GEMM_K_COUNT]; 511 OmegaGpuMem pk; /* packed A | packed B, kept between calls and grown on demand */ 512 int pk_cached; /* pk is GPU-cached (default; env AIEN_OMEGA_GEMM_PACK_CACHED=0 allocates it GPU-uncached) */ 513 int hooked; 514 } g; 515 516 /* lock held; the session calls it right before it closes the device (also in omega_gpu_session_recover): the kernels and 517 * the packed scratch go with the device, the next call builds them again on the new one */ 518 static void on_session_close(void) { 519 for (unsigned t = 0; t < NTYPES; t++) 520 for (unsigned k = 0; k < OMEGA_GEMM_K_COUNT; k++) if (g.code[t][k].cpu) omega_gpu_session_free(&g.code[t][k]); 521 memset(g.code, 0, sizeof g.code); 522 if (g.pk.cpu) omega_gpu_session_free(&g.pk); 523 memset(&g.pk, 0, sizeof g.pk); 524 } 525 526 /* lock held: without the hook a kernel or the scratch would outlive its device */ 527 static int hook_locked(void) { 528 if (!g.hooked) { 529 if (omega_gpu_session_on_close(on_session_close) != 0) return -1; 530 g.hooked = 1; 531 } 532 return 0; 533 } 534 535 /* lock held, device open. The packed operands are DEVICE-PRIVATE: PACK_A / PACK_B write them, MAIN reads them, the CPU never 536 * touches them, so they may be GPU-cached (src/omega_gpu_platform_linux.c ref_dma_alloc: no CPU_READ, no HOST_POLLED; same 537 * pattern as the decode graph's dbuf, src/omega_gpu_q36_graph.c). Every packed word MAIN uses is written by a PACK launch of 538 * the same call before MAIN runs, so a line a previous call left in L2 is overwritten, never read. MAIN reads every packed 539 * fragment once per CTA row or column it serves, so in L2 the reuse across CTAs no longer goes back to DRAM (GemmTune). When 540 * the scratch grows, the old one is synced to the GPU before it is freed: that queues the L2 invalidate prefix 541 * (omega #457) on the next submission, so no clean line of the freed pages survives into whatever reuses them. */ 542 static int packed_locked(uint64_t bytes, OmegaGpuMem *out) { 543 if (hook_locked() != 0) return -1; 544 #if CUDAPP_MIGRATION_PENDING_C_GEMM_HOST_PLAN 545 if (g.pk.cpu && g.pk.size >= bytes) { *out = g.pk; return 0; } 546 #else 547 if (host_value(5, (uintptr_t)g.pk.cpu, g.pk.size, bytes, 0)) { *out = g.pk; return 0; } 548 #endif 549 if (g.pk.cpu) { 550 if (g.pk_cached) (void)omega_gpu_session_sync_to_gpu(&g.pk); 551 omega_gpu_session_free(&g.pk); 552 memset(&g.pk, 0, sizeof g.pk); 553 } 554 const char *v = getenv("AIEN_OMEGA_GEMM_PACK_CACHED"); 555 #if CUDAPP_MIGRATION_PENDING_C_GEMM_HOST_PLAN 556 g.pk_cached = !(v && v[0] == '0' && v[1] == 0); 557 #else 558 g.pk_cached = (int)host_value(6, v != NULL, v ? (unsigned char)v[0] : 0, 559 v && v[0] ? (unsigned char)v[1] : 0, 0); 560 #endif 561 if ((g.pk_cached ? omega_gpu_session_alloc_cached(bytes, &g.pk) : omega_gpu_session_alloc(bytes, &g.pk)) != 0) { memset(&g.pk, 0, sizeof g.pk); return -1; } 562 *out = g.pk; 563 return 0; 564 } 565 566 static const OmegaGemmCfg *cfg_of(OmegaGpuGemmType t) { 567 if (!g.set[t]) { omega_gpu_gemm_default_cfg(t, &g.cfg[t]); g.set[t] = 1; } 568 return &g.cfg[t]; 569 } 570 571 int omega_gpu_gemm_test_set_cfg(OmegaGpuGemmType type, const OmegaGemmCfg *cfg) { 572 if (!type_ok((unsigned)type) || (cfg && (!omega_gpu_gemm_cfg_ok(cfg) || cfg->type != type))) return OMEGA_GPU_GEMM_BAD_ARGS; 573 omega_gpu_session_lock(); 574 for (unsigned k = 0; k < OMEGA_GEMM_K_COUNT; k++) if (g.code[type][k].cpu) omega_gpu_session_free(&g.code[type][k]); 575 memset(g.code[type], 0, sizeof g.code[type]); 576 if (cfg) g.cfg[type] = *cfg; else omega_gpu_gemm_default_cfg(type, &g.cfg[type]); 577 g.set[type] = 1; 578 omega_gpu_session_unlock(); 579 return OMEGA_GPU_GEMM_OK; 580 } 581 582 const char *omega_gpu_gemm_rc_name(int rc) { 583 switch (rc) { 584 case OMEGA_GPU_GEMM_OK: return "OK"; 585 case OMEGA_GPU_GEMM_BAD_ARGS: return "BAD_ARGS"; 586 case OMEGA_GPU_GEMM_TOO_LARGE: return "TOO_LARGE"; 587 case OMEGA_GPU_GEMM_CODEGEN_FAIL: return "CODEGEN_FAIL"; 588 case OMEGA_GPU_GEMM_CHIP_FAIL: return "CHIP_FAIL"; 589 case OMEGA_GPU_GEMM_UNSUPPORTED: return "UNSUPPORTED"; 590 default: return "?"; 591 } 592 } 593 594 /* lock held */ 595 static int kernel_locked(OmegaGpuGemmType t, OmegaGemmKind k) { 596 if (g.code[t][k].cpu) return OMEGA_GPU_GEMM_OK; 597 if (hook_locked() != 0) return OMEGA_GPU_GEMM_CHIP_FAIL; 598 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); 599 if (!prog) return OMEGA_GPU_GEMM_CODEGEN_FAIL; 600 int rc = OMEGA_GPU_GEMM_OK; 601 size_t need = 0; 602 if (omega_gpu_gemm_codegen(cfg_of(t), k, prog) != 0 || (need = omega_bw_program_code_bytes(prog)) == 0) rc = OMEGA_GPU_GEMM_CODEGEN_FAIL; 603 else if (omega_gpu_session_alloc_code(need, &g.code[t][k]) != 0) rc = OMEGA_GPU_GEMM_CHIP_FAIL; 604 else { 605 OmegaBlackwellKernel kk; 606 if (omega_blackwell_kernel_encode_into(prog, g.code[t][k].cpu, need, &kk) != 0 || kk.code_size != need || 607 omega_qmma_patch_code(prog, g.code[t][k].cpu, need) < 0) rc = OMEGA_GPU_GEMM_CODEGEN_FAIL; 608 else if ((g.gpr[t][k] = omega_bw_gpr_declared(kk.gpr_count)) > 255u) rc = OMEGA_GPU_GEMM_CODEGEN_FAIL; 609 else if (omega_gpu_session_sync_to_gpu(&g.code[t][k]) != 0) rc = OMEGA_GPU_GEMM_CHIP_FAIL; 610 if (g.gpr[t][k] < 64u) g.gpr[t][k] = 64u; 611 if (rc != OMEGA_GPU_GEMM_OK) { omega_gpu_session_free(&g.code[t][k]); memset(&g.code[t][k], 0, sizeof g.code[t][k]); } 612 } 613 if (rc == OMEGA_GPU_GEMM_CODEGEN_FAIL) omega_gpu_session_set_error("gemm: our codegen refused a kernel"); 614 omega_gpu_host_free(prog); 615 return rc; 616 } 617 618 typedef struct { OmegaGpuGemmType t; uint64_t pack_ns, main_ns; } ChipCtx; 619 620 /* lock held */ 621 static int chip_launch(void *ctx, OmegaGemmKind k, const uint32_t args[OMEGA_GPU_GEMM_ARGS_WORDS], uint32_t gx, uint32_t gy, uint32_t threads) { 622 ChipCtx *c = ctx; 623 if (kernel_locked(c->t, k) != OMEGA_GPU_GEMM_OK) return -1; 624 OmegaGpuLaunch L = { 625 .code_va = g.code[c->t][k].va, .gpr_count = g.gpr[c->t][k], .threads_x = threads, .threads_y = 1, 626 .grid_x = gx, .grid_y = gy, .num_elements = (size_t)gx * gy * threads, 627 .args = args, .n_args = OMEGA_GPU_GEMM_ARGS_WORDS, .timeout_ms = TIMEOUT_MS, 628 }; 629 uint64_t ns = 0; 630 if (omega_gpu_session_launch(&L, &ns, NULL) != 0) return -1; 631 if (k == OMEGA_GEMM_K_MAIN) c->main_ns += ns; else c->pack_ns += ns; 632 return 0; 633 } 634 635 static uint64_t up16(uint64_t v) { 636 #if CUDAPP_MIGRATION_PENDING_C_GEMM_HOST_PLAN 637 return (v + 255u) & ~(uint64_t)255u; 638 #else 639 return host_value(4, v, 0, 0, 0); 640 #endif 641 } 642 643 int omega_gpu_gemm_ld(OmegaGpuGemmType type, unsigned layout, uint32_t M, uint32_t N, uint32_t K, const void *A, uint64_t lda, 644 const void *B, uint64_t ldb, float *C, OmegaGpuGemmInfo *info) { 645 uint64_t t0 = omega_gpu_session_now_ns(); 646 if (info) memset(info, 0, sizeof *info); 647 if (!type_ok((unsigned)type) || (layout & ~3u) || !A || !B || !C) return OMEGA_GPU_GEMM_BAD_ARGS; 648 omega_gpu_session_lock(); 649 const OmegaGemmCfg *cfg = cfg_of(type); 650 OmegaGemmPlan p; 651 uint64_t sa = 0, sb = 0; 652 int rc = omega_gpu_gemm_plan(cfg, M, N, K, &p); 653 if (rc == OMEGA_GPU_GEMM_OK) rc = omega_gpu_gemm_span(layout, M, N, K, lda, ldb, &sa, &sb); 654 if (rc != OMEGA_GPU_GEMM_OK) { omega_gpu_session_unlock(); return rc; } 655 sa *= esz(type); sb *= esz(type); /* bytes the operands span with their leading dimensions */ 656 /* GPU-uncached buffer, CPU-visible: A | B | C (each 256-byte aligned; A and B carry 16 bytes of readable slack). 657 * The packed operands live in the scratch (packed_locked): packed A | packed B. F32_FFMA packs nothing. */ 658 const uint64_t oa = 0, ob = up16(sa + 16u), oc = ob + up16(sb + 16u), total = oc + (uint64_t)M * N * 4u; 659 const uint64_t obp = up16(p.ap_bytes), pk_total = obp + p.bp_bytes; 660 OmegaGpuMem m, pk; 661 memset(&pk, 0, sizeof pk); 662 if (!omega_gpu_session_open() || omega_gpu_session_alloc(total, &m) != 0) { omega_gpu_session_unlock(); return OMEGA_GPU_GEMM_CHIP_FAIL; } 663 if (pk_total && packed_locked(pk_total, &pk) != 0) { omega_gpu_session_free(&m); omega_gpu_session_unlock(); return OMEGA_GPU_GEMM_CHIP_FAIL; } 664 uint8_t *base = m.cpu; 665 memcpy(base + oa, A, sa); memcpy(base + ob, B, sb); 666 uint32_t *cw = (uint32_t *)(base + oc); 667 for (uint64_t i = 0; i < (uint64_t)M * N; i++) cw[i] = POISON; 668 ChipCtx cc = { type, 0, 0 }; 669 if (omega_gpu_session_sync_to_gpu(&m) != 0) rc = OMEGA_GPU_GEMM_CHIP_FAIL; 670 else rc = omega_gpu_gemm_drive_ld(cfg, layout, M, N, K, m.va + oa, lda, m.va + ob, ldb, pk.va, pk.va + obp, m.va + oc, chip_launch, &cc); 671 if (rc == OMEGA_GPU_GEMM_OK && omega_gpu_session_sync_to_cpu(&m) != 0) rc = OMEGA_GPU_GEMM_CHIP_FAIL; 672 if (rc == OMEGA_GPU_GEMM_OK) { 673 uint64_t unwritten = 0; 674 for (uint64_t i = 0; i < (uint64_t)M * N; i++) unwritten += cw[i] == POISON; 675 if (unwritten) { omega_gpu_session_set_error("gemm: output words never written"); rc = OMEGA_GPU_GEMM_CHIP_FAIL; } 676 else memcpy(C, cw, (size_t)M * N * 4u); 677 } 678 if (info) { 679 info->pack_ns = cc.pack_ns; info->main_ns = cc.main_ns; 680 for (unsigned k = 0; k < OMEGA_GEMM_K_COUNT; k++) info->gpr[k] = g.gpr[type][k]; 681 } 682 omega_gpu_session_free(&m); 683 omega_gpu_session_unlock(); 684 if (info) info->call_ns = omega_gpu_session_now_ns() - t0; 685 return rc; 686 } 687 688 int omega_gpu_gemm(OmegaGpuGemmType type, unsigned layout, uint32_t M, uint32_t N, uint32_t K, const void *A, const void *B, 689 float *C, OmegaGpuGemmInfo *info) { 690 return omega_gpu_gemm_ld(type, layout, M, N, K, A, 0, B, 0, C, info); 691 }