interplane.Get Omega
Read in your language

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

branch main Branches Tags History

CUDApp / src / omega_blackwell_codegen_tgemm.c

244 lines · 16601 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* omega_blackwell_codegen_tgemm.c: device-resident bf16 training GEMM (trainer lane E1, cut 1: NN, F32 overwrite). See the
3 * header. The K loop, staging and scheduling follow omega_blackwell_codegen_tc2.c (chip gate OWN30B-L1G-GEMM-1e76e728). */
4#include "omega_blackwell_codegen_tgemm.h"
5#include "cudapp_native.h"
6#include "omega_gpu_om_ir.h"
7#ifndef CUDAPP_MIGRATION_PENDING_C_CODEGEN_TGEMM
8#define CUDAPP_MIGRATION_PENDING_C_CODEGEN_TGEMM 0
9#endif
10#if CUDAPP_MIGRATION_PENDING_C_CODEGEN_TGEMM
11#include "omega_bw_em.h"
12
13int omega_tgemm_spec_ok(const OmegaTgemmSpec *s) {
14 if (!s) return 0;
15 if (s->k < 128u || s->k > 65536u || s->k % 128u) return 0;
16 if (s->rt != 2 && s->rt != 4 && s->rt != 8) return 0;
17 if (s->ct != 2) return 0; /* the paired column tiles share one B word per lane */
18 if (s->warps != 8 && s->warps != 16) return 0;
19 if (s->kc != 32) return 0;
20 if (s->aqn != 1 && s->aqn != 2) return 0;
21 if (s->layout != OMEGA_TGEMM_LAYOUT_NN || s->epi != OMEGA_TGEMM_EPI_F32) return 0;
22 const uint32_t span = s->warps * 8u * s->ct;
23 if (s->n == 0 || s->n % span || s->n >= (1u << 17)) return 0;
24 if ((16u * s->rt * (s->kc / 4u)) % (32u * s->warps)) return 0; /* whole LDG.64 staging pieces per thread */
25 { /* the pieces of one thread are RJ rows apart: constant load offsets that must stay under the encoder's 2^23 */
26 const uint32_t RJ = 32u * s->warps / (s->kc / 4u), nj = 16u * s->rt * (s->kc / 4u) / (32u * s->warps);
27 if (RJ % 16u) return 0;
28 if ((uint64_t)(nj - 1u) * RJ * s->k * 2u + (uint64_t)(128u / s->kc - 1u) * s->kc * 2u >= (1u << 23)) return 0;
29 }
30 if (2u * 16u * s->rt * s->kc * 2u > 16384u) return 0; /* QMD shared cap */
31 if (32u * span > 2u * 16u * s->rt * s->kc * 2u) return 0; /* an 8-row output pass must fit the staging buffers */
32 if ((8u * span) % (32u * s->warps)) return 0;
33 if (s->mutant < 0 || s->mutant > 5) return 0;
34 return 1;
35}
36uint32_t omega_tgemm_shared_bytes(const OmegaTgemmSpec *s) { return omega_tgemm_spec_ok(s) ? 2u * 16u * s->rt * s->kc * 2u : 0u; }
37uint32_t omega_tgemm_span(const OmegaTgemmSpec *s) { return omega_tgemm_spec_ok(s) ? s->warps * 8u * s->ct : 0u; }
38uint32_t omega_tgemm_bm(const OmegaTgemmSpec *s) { return omega_tgemm_spec_ok(s) ? 16u * s->rt : 0u; }
39
40/* d = lut(a[asub], b[bsub], c) */
41static void lop3s(Em *e, int d, int a, unsigned asub, int b, unsigned bsub, int c, uint32_t lut) {
42 BlackwellIRInsn i = I0(BW_IR_LOP3_LUT); i.dst_vreg = d; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.src2_vreg = b; i.src2_subreg = (uint8_t)bsub; i.src3_vreg = c; i.imm = lut; emit(e, i, K_FIXED);
43}
44/* d[dsub] = a * imm + c */
45static void imadi_d(Em *e, int d, unsigned dsub, int a, uint32_t imm, int c) {
46 BlackwellIRInsn i = I0(BW_IR_IMAD); i.dst_vreg = d; i.dst_subreg = (uint8_t)dsub; i.src1_vreg = a; i.imm = imm; i.src3_vreg = c; emit(e, i, K_FIXED);
47}
48/* d[dsub] = a >> imm (SHF.R.U32.HI) */
49static void shr_d(Em *e, int d, unsigned dsub, int a, uint32_t imm) {
50 BlackwellIRInsn i = I0(BW_IR_SHF_R); i.dst_vreg = d; i.dst_subreg = (uint8_t)dsub; i.src1_vreg = a; i.imm = imm; emit(e, i, K_FIXED);
51}
52/* d[dsub] = a[asub] + b */
53static void iadd_d(Em *e, int d, unsigned dsub, int a, unsigned asub, int b) {
54 BlackwellIRInsn i = I0(BW_IR_IADD3); i.dst_vreg = d; i.dst_subreg = (uint8_t)dsub; i.src1_vreg = a; i.src1_subreg = (uint8_t)asub; i.src2_vreg = b; emit(e, i, K_FIXED);
55}
56static void sts32_sub(Em *e, int addr, int val, unsigned sub) {
57 BlackwellIRInsn st = I0(BW_IR_STS32); st.src1_vreg = addr; st.src2_vreg = val; st.src2_subreg = (uint8_t)sub; emit(e, st, K_STORE);
58}
59
60int omega_blackwell_codegen_tgemm_prog(const OmegaTgemmSpec *spec, BlackwellIRProgram *prog) {
61 if (!omega_tgemm_spec_ok(spec) || !prog) return -1;
62 const uint32_t K = spec->k, N = spec->n, RT = spec->rt, CT = spec->ct, W = spec->warps, T = 32u * W, KC = spec->kc, AQN = spec->aqn;
63 const uint32_t BM = 16u * RT, SPAN = W * 8u * CT, NS = KC / 16u /* HMMA steps per chunk */, NCH = 128u / KC /* chunks per block */;
64 const uint32_t PPR = KC / 4u /* LDG.64 pieces per row of a chunk */, NJ = BM * PPR / T, BUF = BM * KC * 2u;
65 omega_bw_ir_init(prog);
66 Em em; memset(&em, 0, sizeof em); em.p = prog;
67 Em *e = &em;
68
69 /* ---- prologue: arguments, ids, tile entry ---- */
70 int udesc = UV64(e), ptab = V64(e), pa = V64(e), pbb = V64(e);
71 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);
72 int rk = V64(e) /* row0, kb0 */, nkb = V64(e) /* nkb, pad */;
73 int n0w = V(e), tmp = V(e), tmp2 = V(e), pb = V64(e), la = V(e), msel = V(e);
74 if (e->err) return -1;
75 ldcu64(e, udesc, 0x358);
76 ldc64(e, ptab, 0x380); ldc64(e, pa, 0x388); ldc64(e, pbb, 0x390);
77 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X); s2r(e, cy, BW_SR_CTAID_Y);
78 movi(e, one, 1);
79 shr(e, warp, tid, 0, 5); andi(e, lane, tid, 0, 31); shr(e, g, lane, 0, 2); andi(e, tq, lane, 0, 3);
80 imadw(e, tent, cy, OMEGA_TGEMM_TILE_BYTES, ptab);
81 ldg_wide(e, BW_IR_LDG_E_64, rk, tent, 8, udesc, 1);
82 ldg_wide(e, BW_IR_LDG_E_64, nkb, tent, 16, udesc, 0);
83 /* this warp's first output column n0w; B fragment pointer: row kb0 * 128 + 8 t, column n0w + 4 (g / 2) */
84 imadi(e, n0w, cx, 0, SPAN, NOREG); imadi(e, tmp, warp, 0, 8u * CT, NOREG); iadd(e, n0w, n0w, tmp);
85 imadi(e, tmp, rk, 1, 128u, NOREG); imadi(e, tmp, tq, 0, 8u, tmp); imadi(e, tmp, tmp, 0, N, n0w);
86 shr(e, tmp2, g, 0, 1); imadi(e, tmp, tmp2, 0, 4u, tmp); imadw(e, pb, tmp, 2u, pbb);
87 /* half-word select mask: odd g takes the high word of its 8 B row bytes (mutant 4 swaps the parity) */
88 andi(e, tmp, g, 0, 1); if (spec->mutant == 4) imadi(e, tmp, tmp, 0, 0xffffffffu, one);
89 imadi(e, msel, tmp, 0, 0xffffffffu, NOREG);
90 imadi(e, la, lane, 0, 16u, NOREG);
91
92 /* ---- staging assignment: piece = tid + jj * T -> row r = piece / PPR, pp = piece % PPR (4 bf16 at k 4 pp) ---- */
93 /* piece jj of a thread is piece 0 moved RJ = T / PPR rows down: the same pp, row + jj RJ, so one address register and one
94 * shared offset serve all pieces (constant byte offsets jj * RJ * K * 2 on the load, jj * (RJ / 16) * NS * 512 on the store) */
95 const uint32_t RJ = T / PPR, GXJ = RJ * K * 2u, SAJ = (RJ / 16u) * NS * 512u;
96 int gx = V64(e), sa = V(e), r = V(e), pp = V(e), hh = V(e);
97 if (e->err) return -1;
98 {
99 shr(e, r, tid, 0, (uint32_t)log2u(PPR)); andi(e, pp, tid, 0, PPR - 1u);
100 /* global: a + ((row0 + r) * K + kb0 * 128 + 4 pp) * 2 */
101 iadd(e, tmp, rk, r); imadw(e, gx, tmp, K * 2u, pa);
102 imadi(e, tmp, rk, 1, 256u, NOREG); imadi(e, tmp, pp, 0, 8u, tmp); imadw(e, gx, tmp, 1, gx);
103 /* shared: (r / 16) * NS * 512 + (pp & 1) * 512 + (4 (r % 8) + pp / 2) * 16 + 4 ((r / 8) & 1) */
104 shr(e, tmp, r, 0, 4); imadi(e, tmp, tmp, 0, NS * 512u, NOREG);
105 andi(e, tmp2, pp, 0, 1); imadi(e, tmp, tmp2, 0, 512u, tmp);
106 andi(e, tmp2, r, 0, 7); imadi(e, tmp2, tmp2, 0, 4u, NOREG); shr(e, hh, pp, 0, 1); iadd(e, tmp2, tmp2, hh); imadi(e, tmp, tmp2, 0, 16u, tmp);
107 shr(e, hh, r, 0, 3); andi(e, hh, hh, 0, 1); imadi(e, sa, hh, 0, 4u, tmp);
108 }
109
110 /* ---- loop-wide registers ---- */
111 int acc[16], bq[2][8], stgr[8], aq[2], wsel[2], bE = V64(e), bO = V64(e), tw = V(e), cntk = V(e);
112 for (uint32_t x = 0; x < RT * CT; x++) acc[x] = V128(e);
113 for (unsigned set = 0; set < 2; set++) for (uint32_t j = 0; j < 4u * NS; j++) bq[set][j] = V64(e);
114 for (uint32_t jj = 0; jj < NJ; jj++) stgr[jj] = V64(e);
115 for (uint32_t q = 0; q < AQN; q++) aq[q] = V128(e);
116 for (unsigned j = 0; j < 2; j++) wsel[j] = V(e);
117 if (e->err) return -1;
118
119 /* A pieces of chunk c (immediate c * KC * 2) and the B rows of the chunk (pb points at its row kb0 * 128 + chunk * KC + 8 t) */
120 #define ISSUE(set, sb, chunk) do { \
121 for (uint32_t jj = 0; jj < NJ; jj++) ldw_pf(e, BW_IR_LDG_E_64, stgr[jj], gx, jj * GXJ + (chunk) * KC * 2u, udesc, (sb)); \
122 for (uint32_t s = 0; s < NS; s++) for (uint32_t j = 0; j < 4; j++) ldw_pf(e, BW_IR_LDG_E_64, bq[set][s * 4u + j], pb, (4u * s + j) * N * 2u, udesc, (sb)); } while (0)
123 #define STAGE(buf) do { for (uint32_t jj = 0; jj < NJ; jj++) { \
124 imadi(e, tmp, one, 0, (buf) + jj * SAJ, sa); sts32_sub(e, tmp, stgr[jj], 0); \
125 imadi(e, tmp, one, 0, (buf) + jj * SAJ + 8u, sa); sts32_sub(e, tmp, stgr[jj], 1); } bar_sync(e); } while (0)
126 /* B fragments of step s: W_j = half word (g % 2) of row word j (LOP3 select); E: (W0.lo, W1.lo), (W2.lo, W3.lo) by
127 * IMAD W1 * 2^16 + (W0 & 0xffff); O: (W0.hi, W1.hi), (W2.hi, W3.hi) by (W0 >> 16) + (W1 & 0xffff0000). (IMAD.HI.U32 would do
128 * the O half in one instruction, but its Rc is a 64-bit pair on the chip: odd columns came back wrong, first chip run.) */
129 #define BFRAG(set, s) do { for (unsigned h = 0; h < 2; h++) { \
130 for (unsigned j = 0; j < 2; j++) lop3s(e, wsel[j], bq[set][(s) * 4u + 2 * h + j], 0, bq[set][(s) * 4u + 2 * h + j], 1, msel, 0xd8u); \
131 andi(e, tw, wsel[0], 0, 0xffffu); imadi_d(e, bE, h, wsel[1], 0x10000u, tw); \
132 andi(e, tw, wsel[1], 0, 0xffff0000u); shr_d(e, bO, h, wsel[0], 16u); iadd_d(e, bO, h, bO, h, tw); } } while (0)
133 #define COMPUTE(buf, set, last) do { \
134 for (uint32_t s = 0; s < NS; s++) { \
135 if (spec->mutant == 1 && (last) && s == NS - 1u) break; \
136 BFRAG(set, s); \
137 for (uint32_t i0 = 0; i0 < RT; i0 += AQN) { \
138 const uint32_t nb = RT - i0 < AQN ? RT - i0 : AQN; \
139 for (uint32_t q = 0; q < nb; q++) { uint32_t i = i0 + q, ii = spec->mutant == 3 ? (i ^ 1u) : i; \
140 lds128(e, aq[q], la, (buf) + (ii * NS + s) * 512u, q == 0); } \
141 for (uint32_t q = 0; q < nb; q++) { const uint32_t x = (i0 + q) * CT; \
142 hmma(e, acc[x], aq[q], bE, acc[x], 8); hmma(e, acc[x + 1], aq[q], bO, acc[x + 1], 8); } \
143 } \
144 } } while (0)
145
146 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); }
147 ISSUE(0, 3u, 0u);
148 consume(e, 3);
149 STAGE(0u);
150 imadi(e, cntk, nkb, 0, 1, NOREG);
151
152 /* ---- K loop: one 128-wide block (NCH chunks) per iteration; sets and buffers alternate per chunk (NCH even) ---- */
153 const int L_k = here(e);
154 loop_begin(e);
155 for (uint32_t c = 0; c < NCH; c++) {
156 const unsigned cur = c & 1u, nxt = cur ^ 1u; /* set / buffer holding chunk c; the other receives chunk c + 1 */
157 const uint32_t cbuf = cur ? BUF : 0u, nbuf = nxt ? BUF : 0u;
158 if (c + 1u < NCH) {
159 imadw(e, pb, one, KC * N * 2u, pb); /* B rows of chunk c + 1 */
160 ISSUE(nxt, nxt ? 4u : 3u, c + 1u);
161 } else {
162 /* the next block's chunk 0: advance only when a block follows (nf = cntk != 1); the last iteration re-reads its own */
163 int nf = V(e); if (e->err) return -1;
164 imadi(e, nf, one, 0, 0xfffffffeu, cntk); shr(e, nf, nf, 0, 31); imadi(e, nf, nf, 0, 0xffffffffu, one);
165 imadw(e, gx, nf, spec->mutant == 2 ? 0u : 256u, gx);
166 imadw(e, pb, nf, KC * N * 2u, pb);
167 ISSUE(nxt, nxt ? 4u : 3u, 0u);
168 }
169 COMPUTE(cbuf, cur, c + 1u == NCH);
170 consume(e, nxt ? 4u : 3u);
171 STAGE(nbuf);
172 }
173 imadi(e, cntk, one, 0, 0xffffffffu, cntk);
174 isetp_ge_u32(e, cntk, one);
175 bra_if_p0(e, L_k);
176 loop_end(e);
177 #undef ISSUE
178 #undef STAGE
179 #undef BFRAG
180 #undef COMPUTE
181
182 /* ---- epilogue: the HMMA results must have settled. The tile goes out through shared memory in 8-row passes so that every
183 * warp store writes 128 contiguous bytes: storing straight from the fragment layout (a lane owns 16 bytes per row, one 4-byte
184 * STG per word) touched 16 half-filled sectors per instruction and cost more than half of the kernel time at K 768
185 * (chip probe: the no-store mutant 5 ran 8192x768x768 in 0.150 ms against 0.334 ms). Addresses in fresh registers. ---- */
186 nop(e, 15); nop(e, 14);
187 {
188 const uint32_t RJ2 = T / SPAN /* rows per read step */, NJ2 = 8u * SPAN / T /* words per thread per pass */;
189 int tid2 = V(e), cx2 = V(e), cy2 = V(e), lane2 = V(e), g2 = V(e), tq2 = V(e), warp2 = V(e), tab2 = V64(e), ten2 = V64(e), cva2 = V64(e), rk2 = V64(e);
190 int onee = V(e), p0 = V64(e), pp2 = V64(e), sw[4], sr[4], rd[4];
191 for (unsigned w = 0; w < 4; w++) sw[w] = V(e);
192 for (uint32_t j = 0; j < NJ2; j++) { sr[j] = V(e); rd[j] = V(e); }
193 if (e->err) return -1;
194 ldc64(e, tab2, 0x380);
195 s2r(e, tid2, BW_SR_TID_X); s2r(e, cx2, BW_SR_CTAID_X); s2r(e, cy2, BW_SR_CTAID_Y);
196 imadw(e, ten2, cy2, OMEGA_TGEMM_TILE_BYTES, tab2);
197 ldg_wide(e, BW_IR_LDG_E_64, cva2, ten2, 0, udesc, 1);
198 ldg_wide(e, BW_IR_LDG_E_64, rk2, ten2, 8, udesc, 0);
199 andi(e, lane2, tid2, 0, 31); shr(e, g2, lane2, 0, 2); andi(e, tq2, lane2, 0, 3); shr(e, warp2, tid2, 0, 5); movi(e, onee, 1);
200 /* the lane's four slots of a pass: (g * SPAN + warp * 16 + 4 t + w) * 4 (row g of the pass, the warp's columns) */
201 imadi(e, tmp, g2, 0, SPAN, NOREG); imadi(e, tmp, warp2, 0, 16u, tmp); imadi(e, tmp, tq2, 0, 4u, tmp); imadi(e, tmp2, tmp, 0, 4u, NOREG);
202 for (unsigned w = 0; w < 4; w++) imadi(e, sw[w], onee, 0, 4u * w, tmp2);
203 /* the thread's read slots: word q = tid + T j (consecutive lanes, consecutive words): row q / SPAN = tid / SPAN + j RJ2 */
204 for (uint32_t j = 0; j < NJ2; j++) { imadi(e, tmp, onee, 0, j * T, tid2); imadi(e, sr[j], tmp, 0, 4u, NOREG); }
205 /* p0 = c + ((row0 + tid / SPAN) * N + cx * SPAN + tid % SPAN) * 4; pass (i, h) adds (16 i + 8 h) rows, read step j adds j RJ2 rows */
206 shr(e, tmp, tid2, 0, (uint32_t)log2u(SPAN)); iadd(e, tmp, rk2, tmp); imadi(e, tmp, tmp, 0, N, NOREG);
207 imadi(e, tmp2, cx2, 0, SPAN, tmp); andi(e, tmp, tid2, 0, SPAN - 1u); iadd(e, tmp, tmp, tmp2); imadw(e, p0, tmp, 4u, cva2);
208 for (uint32_t i = 0; i < RT; i++) for (unsigned h = 0; h < 2; h++) {
209 if (spec->mutant == 5) continue; /* timing probe only: no output */
210 bar_sync(e); /* the previous pass's reads (or the K loop's last LDS) are complete before the slots are rewritten */
211 for (unsigned w = 0; w < 4; w++) sts32_sub(e, sw[w], acc[i * CT + (w & 1u)], 2u * h + (w >> 1));
212 bar_sync(e);
213 for (uint32_t j = 0; j < NJ2; j++) { BlackwellIRInsn ld = I0(BW_IR_LDS32); ld.dst_vreg = rd[j]; ld.src1_vreg = sr[j]; emit(e, ld, j == 0 ? K_VAR : K_VAR_MORE); }
214 imadw(e, pp2, onee, (16u * i + 8u * h) * N * 4u, p0);
215 for (uint32_t j = 0; j < NJ2; j++) stg(e, pp2, j * RJ2 * N * 4u, rd[j], udesc);
216 }
217 }
218 tail(e);
219 if (e->err) return -1;
220 if (prog->count > OMEGA_TGEMM_MAX_INSNS) return -1;
221 if (omega_bw_regalloc_solve(prog) != 0) return -1;
222 if (!omega_bw_gpr_declared_ok(omega_bw_gpr_declared(prog->regalloc.peak_gpr_usage), prog->regalloc.peak_gpr_usage) || omega_bw_gpr_declared(prog->regalloc.peak_gpr_usage) > 128u) return -1;
223 return 0;
224}
225#else
226static uint32_t tgemm_query(const OmegaTgemmSpec *s, uint64_t kind) {
227 if (!s) return 0;
228 const uint64_t fields[10] = {s->k,s->n,s->rt,s->ct,s->warps,s->kc,s->aqn,s->layout,s->epi,(uint64_t)s->mutant};
229 uint64_t result = 0;
230 if (omega_cg_tgemm_spec(&result,fields,sizeof fields/sizeof fields[0],kind) != OMEGA_OK || result > UINT32_MAX) return 0;
231 return (uint32_t)result;
232}
233int omega_tgemm_spec_ok(const OmegaTgemmSpec *s) { return (int)tgemm_query(s,0); }
234uint32_t omega_tgemm_shared_bytes(const OmegaTgemmSpec *s) { return tgemm_query(s,1); }
235uint32_t omega_tgemm_span(const OmegaTgemmSpec *s) { return tgemm_query(s,2); }
236uint32_t omega_tgemm_bm(const OmegaTgemmSpec *s) { return tgemm_query(s,3); }
237int omega_blackwell_codegen_tgemm_prog(const OmegaTgemmSpec *s, BlackwellIRProgram *prog) {
238 if (!prog || !omega_tgemm_spec_ok(s)) return -1;
239 uint64_t shape = (uint64_t)s->rt | (uint64_t)s->warps << 8 | (uint64_t)s->aqn << 16 | (uint64_t)s->mutant << 24;
240 if (omega_gpu_om_ir_replay(omega_cg_tgemm,s->k,s->n,shape,0,prog,NULL) != OMEGA_OM_IR_OK) return -1;
241 uint32_t declared = omega_bw_gpr_declared(prog->regalloc.peak_gpr_usage);
242 return omega_bw_gpr_declared_ok(declared,prog->regalloc.peak_gpr_usage) && declared <= 128u ? 0 : -1;
243}
244#endif