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_qmma.c

215 lines · 11786 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* omega_blackwell_codegen_qmma.c: QMMA.16832.F32.E4M3.E4M3 placeholder, patch, probe and peak kernels (W-L1G). See the header. */
3#include "omega_blackwell_codegen_qmma.h"
4#include "omega_bw_em.h"
5#include "omega_bw_em_q.h"
6#include "omega_gpu_fmt.h"
7#include "omega_gpu_om_ir.h"
8#include "cudapp_native.h"
9
10#ifndef CUDAPP_MIGRATION_PENDING_C_CODEGEN_QMMA
11#define CUDAPP_MIGRATION_PENDING_C_CODEGEN_QMMA 0
12#endif
13#ifndef CUDAPP_MIGRATION_PENDING_C_QMMA_PROBE_REST
14#define CUDAPP_MIGRATION_PENDING_C_QMMA_PROBE_REST 0
15#endif
16
17#if CUDAPP_MIGRATION_PENDING_C_QMMA_PROBE_REST
18static uint32_t rd32q(const uint8_t *p) { return (uint32_t)p[0] | (uint32_t)p[1] << 8 | (uint32_t)p[2] << 16 | (uint32_t)p[3] << 24; }
19#else
20static uint32_t rd32q(const uint8_t *p) {
21 uint64_t r = 0;
22 return omega_qm_host(&r, 1, p[0], p[1], p[2], p[3], 0) == OMEGA_OK ? (uint32_t)r : 0;
23}
24#endif
25#if CUDAPP_MIGRATION_PENDING_C_CODEGEN_QMMA
26static void wr32q(uint8_t *p, uint32_t v) { p[0] = (uint8_t)v; p[1] = (uint8_t)(v >> 8); p[2] = (uint8_t)(v >> 16); p[3] = (uint8_t)(v >> 24); }
27#endif
28
29#if CUDAPP_MIGRATION_PENDING_C_QMMA_PROBE_REST
30int omega_qmma_is_mark(const BlackwellIRInsn *in) { return in && in->op == BW_IR_HMMA_BF16 && in->imm == OMEGA_QMMA_MARK; }
31#else
32int omega_qmma_is_mark(const BlackwellIRInsn *in) {
33 uint64_t r = 0;
34 return in && omega_qm_host(&r, 0, in->op, in->imm, 0, 0, 0) == OMEGA_OK && r == 1;
35}
36#endif
37
38int omega_qmma_patch_code(const BlackwellIRProgram *prog, uint8_t *code, size_t len) {
39 if (!prog || !code || len < prog->count * 16u) return -1;
40 int n = 0;
41#if CUDAPP_MIGRATION_PENDING_C_CODEGEN_QMMA
42 for (size_t i = 0; i < prog->count; i++) {
43 if (prog->insns[i].op == BW_IR_FMNMX_MAX && prog->insns[i].imm == OMEGA_QMMA_MARK_F2E4) {
44 uint8_t *w = code + i * 16u; uint32_t w0 = rd32q(w), w1 = rd32q(w + 4), w2 = rd32q(w + 8);
45 if ((w0 & 0xffffu) != 0x7209u || (w1 & 0xffffff00u) != 0 || w2 != 0x07800000u) return -1;
46 wr32q(w, (w0 & 0xffff0000u) | 0x723eu); wr32q(w + 8, 0x048070ffu); n++; continue;
47 }
48 if (!omega_qmma_is_mark(&prog->insns[i])) continue;
49 uint8_t *w = code + i * 16u;
50 uint32_t w0 = rd32q(w), w1 = rd32q(w + 4), w2 = rd32q(w + 8);
51 if ((w0 & 0xffffu) != 0x723cu || (w1 & 0xffffff00u) != 0 || (w2 & 0xffffff00u) != 0x00041800u) return -1;
52 wr32q(w, (w0 & 0xffff0000u) | 0x727au);
53 wr32q(w + 8, (w2 & 0xffu) | 0x00002c00u);
54 n++;
55 }
56#else
57 for (size_t i = 0; i < prog->count; i++) {
58 uint64_t result = 0;
59 if (omega_cg_qmma_patch(&result, prog->insns[i].op, prog->insns[i].imm, code + i * 16u, 16) != OMEGA_OK || result > 1) return -1;
60 n += (int)result;
61 }
62#endif
63 if (n > 0) { size_t b = omega_bw_program_code_bytes(prog); omega_kernel_dump(code, b && b <= len ? b : prog->count * 16u); } /* the patched body is a different kernel */
64 return n;
65}
66
67#if CUDAPP_MIGRATION_PENDING_C_QMMA_PROBE_REST
68int omega_qmma_patch_selftest(char *msg, size_t msgn) {
69 static BlackwellIRProgram p; static uint8_t buf[64 * 16]; size_t len = 0;
70 omega_bw_ir_init(&p);
71 Em em; memset(&em, 0, sizeof em); em.p = &p; Em *e = &em;
72 int udesc = UV64(e), py = V64(e), a = V128(e), b = V64(e), c = V128(e);
73 if (e->err) { omega_gpu_fmt(msg, msgn, "vregs"); return -1; }
74 ldcu64(e, udesc, 0x358); ldc64(e, py, 0x380);
75 ldg_wide(e, BW_IR_LDG_E_128, a, py, 0, udesc, 1); ldg_wide(e, BW_IR_LDG_E_64, b, py, 16, udesc, 0); ldg_wide(e, BW_IR_LDG_E_128, c, py, 32, udesc, 0);
76 qmma(e, c, a, b, c, 15); nop(e, 15);
77 { int f = V(e); if (e->err) { omega_gpu_fmt(msg, msgn, "vregs"); return -1; } f2e4(e, f, c, 0, c, 1); stg(e, py, 64, f, udesc); }
78 hmma(e, c, a, b, c, 15); nop(e, 15);
79 for (unsigned q = 0; q < 4; q++) stg(e, py, 4u * q, c, udesc);
80 tail(e);
81 if (e->err || omega_bw_regalloc_solve(&p) != 0 || omega_bw_encode_program(&p, buf, sizeof buf, &len) != 0) { omega_gpu_fmt(msg, msgn, "build/encode failed"); return -1; }
82 int np = omega_qmma_patch_code(&p, buf, len);
83 if (np != 2) { omega_gpu_fmt(msg, msgn, "patched %d placeholders, expected 2", np); return -1; }
84 int seenq = 0, seenh = 0, seenf = 0;
85 for (size_t i = 0; i < p.count; i++) {
86 if (p.insns[i].op == BW_IR_FMNMX_MAX) { const uint8_t *w = buf + i * 16u; uint32_t w0 = rd32q(w), w1 = rd32q(w + 4), w2 = rd32q(w + 8);
87 if ((w0 & 0xffffu) != 0x723eu || (w1 & 0xffffff00u) != 0 || w2 != 0x048070ffu) { omega_gpu_fmt(msg, msgn, "F2FP.E4M3 word %zu: %08x %08x %08x", i, w0, w1, w2); return -1; } seenf++; continue; }
88 if (p.insns[i].op != BW_IR_HMMA_BF16) continue;
89 const uint8_t *w = buf + i * 16u; uint32_t w0 = rd32q(w), w1 = rd32q(w + 4), w2 = rd32q(w + 8);
90 if (omega_qmma_is_mark(&p.insns[i])) {
91 /* oracle 0x0414727a 0x0000000c 0x00002c14 with registers masked */
92 if ((w0 & 0xffffu) != 0x727au || (w1 & 0xffffff00u) != 0 || (w2 & 0xffffff00u) != 0x00002c00u) { omega_gpu_fmt(msg, msgn, "QMMA word %zu: %08x %08x %08x", i, w0, w1, w2); return -1; }
93 seenq++;
94 } else {
95 if ((w0 & 0xffffu) != 0x723cu || (w2 & 0xffffff00u) != 0x00041800u) { omega_gpu_fmt(msg, msgn, "HMMA word %zu changed: %08x %08x", i, w0, w2); return -1; }
96 seenh++;
97 }
98 }
99 if (seenq != 1 || seenh != 1 || seenf != 1) { omega_gpu_fmt(msg, msgn, "saw %d QMMA, %d HMMA", seenq, seenh); return -1; }
100 omega_gpu_fmt(msg, msgn, "QMMA.16832.F32.E4M3.E4M3 and F2FP.SATFINITE.E4M3.F32.PACK_AB_MERGE_C match the oracle bits, HMMA untouched");
101 return 0;
102}
103#else
104int omega_qmma_patch_selftest(char *msg, size_t msgn) {
105 static BlackwellIRProgram p;
106 static uint8_t buf[64 * 16];
107 size_t len = 0;
108 if (omega_gpu_om_ir_replay(omega_qm_self, 0, 0, 0, 0, &p, NULL) != OMEGA_OM_IR_OK ||
109 omega_bw_encode_program(&p, buf, sizeof buf, &len) != 0) {
110 omega_gpu_fmt(msg, msgn, "build/encode failed"); return -1;
111 }
112 int np = omega_qmma_patch_code(&p, buf, len);
113 if (np != 2) { omega_gpu_fmt(msg, msgn, "patched %d placeholders, expected 2", np); return -1; }
114 uint64_t seen = 2;
115 for (size_t i = 0; i < p.count; i++) {
116 const uint8_t *w = buf + i * 16u;
117 if (omega_qm_host(&seen, seen, p.insns[i].op, p.insns[i].imm,
118 rd32q(w), rd32q(w + 4), rd32q(w + 8)) != OMEGA_OK ||
119 seen == UINT64_MAX) {
120 omega_gpu_fmt(msg, msgn, "QMMA selftest word %zu changed", i); return -1;
121 }
122 }
123 if (seen != 65795) { omega_gpu_fmt(msg, msgn, "QMMA selftest counts changed"); return -1; }
124 omega_gpu_fmt(msg, msgn, "QMMA.16832.F32.E4M3.E4M3 and F2FP.SATFINITE.E4M3.F32.PACK_AB_MERGE_C match the oracle bits, HMMA untouched");
125 return 0;
126}
127#endif
128
129#if CUDAPP_MIGRATION_PENDING_C_CODEGEN_QMMA
130int omega_blackwell_codegen_qmma_probe_prog(int mutant, uint32_t wait_nops, BlackwellIRProgram *prog) {
131 if (!prog || mutant < 0 || mutant > 1 || wait_nops > 16) return -1;
132 omega_bw_ir_init(prog);
133 Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em;
134 int udesc = UV64(e), pin = V64(e), pout = V64(e), tid = V(e), cx = V(e), pa = V64(e), po = V64(e), pcase = V64(e), pb = V64(e);
135 int a = V128(e), b = V64(e), c = V128(e), d = V128(e);
136 if (e->err) return -1;
137 ldcu64(e, udesc, 0x358);
138 ldc64(e, pin, 0x380); ldc64(e, pout, 0x388);
139 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X);
140 imadw(e, pcase, cx, 1280u, pin); imadw(e, pa, tid, 16, pcase); imadw(e, pb, tid, 8, pcase);
141 imadw(e, po, cx, 512u, pout); imadw(e, po, tid, 16, po);
142 ldg_wide(e, BW_IR_LDG_E_128, a, pa, 0, udesc, 1);
143 ldg_wide(e, BW_IR_LDG_E_128, c, pa, 768, udesc, 0);
144 ldg_wide(e, BW_IR_LDG_E_64, b, pb, 512, udesc, 0);
145 qmma(e, d, a, b, mutant == 1 ? NOREG : c, 15);
146 for (uint32_t i = 0; i < wait_nops; i++) nop(e, 15);
147 for (unsigned q = 0; q < 4; q++) { BlackwellIRInsn i = I0(BW_IR_STG_E); i.src1_vreg = po; i.src2_vreg = d; i.src2_subreg = (uint8_t)q; i.imm = 4u * q; i.ureg = udesc; emit(e, i, K_STORE); }
148 tail(e);
149 if (e->err) return -1;
150 return omega_bw_regalloc_solve(prog);
151}
152
153int omega_blackwell_codegen_qmma_peak_prog(uint32_t iters, uint32_t chains, uint32_t stall, uint32_t threads, BlackwellIRProgram *prog) {
154 if (!prog || iters == 0 || chains == 0 || chains > 16 || stall == 0 || stall > 15 || threads == 0 || threads % 32 || threads > 1024) return -1;
155 omega_bw_ir_init(prog);
156 Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em;
157 int udesc = UV64(e), py = V64(e), tid = V(e), cta = V(e), one = V(e), cnt = V(e), a = V128(e), b = V64(e), acc[16];
158 for (uint32_t c = 0; c < chains; c++) acc[c] = V128(e);
159 if (e->err) return -1;
160 ldcu64(e, udesc, 0x358); ldc64(e, py, 0x380); s2r(e, tid, BW_SR_TID_X); s2r(e, cta, BW_SR_CTAID_X);
161 movi(e, one, 1);
162 for (unsigned q = 0; q < 4; q++) { BlackwellIRInsn m = I0(BW_IR_MOV_IMM); m.dst_vreg = a; m.dst_subreg = (uint8_t)q; m.imm = 0x38383838u; emit(e, m, K_FIXED); }
163 for (unsigned q = 0; q < 2; q++) { BlackwellIRInsn m = I0(BW_IR_MOV_IMM); m.dst_vreg = b; m.dst_subreg = (uint8_t)q; m.imm = 0x38383838u; emit(e, m, K_FIXED); }
164 for (uint32_t c = 0; c < chains; c++) for (unsigned q = 0; q < 4; q++) { BlackwellIRInsn mz = I0(BW_IR_MOV_RZ); mz.dst_vreg = acc[c]; mz.dst_subreg = (uint8_t)q; emit(e, mz, K_FIXED); }
165 movi(e, cnt, iters);
166 const int L = here(e);
167 loop_begin(e);
168 for (uint32_t c = 0; c < chains; c++) qmma(e, acc[c], a, b, acc[c], stall);
169 imadi(e, cnt, one, 0, 0xffffffffu, cnt);
170 isetp_ge_u32(e, cnt, one);
171 bra_if_p0(e, L);
172 loop_end(e);
173 nop(e, 15); nop(e, 15); nop(e, 15); nop(e, 15);
174 { int t = V(e); if (e->err) return -1;
175 imadi(e, t, cta, 0, threads, tid); imadw(e, py, t, 16u * chains, py);
176 for (uint32_t c = 0; c < chains; c++) for (unsigned q = 0; q < 4; q++) { BlackwellIRInsn st = I0(BW_IR_STG_E); st.src1_vreg = py; st.src2_vreg = acc[c]; st.src2_subreg = (uint8_t)q; st.imm = c * 16u + 4u * q; st.ureg = udesc; emit(e, st, K_STORE); }
177 }
178 tail(e);
179 if (e->err) return -1;
180 return omega_bw_regalloc_solve(prog);
181}
182
183int omega_blackwell_codegen_f2e4_probe_prog(BlackwellIRProgram *prog) {
184 if (!prog) return -1;
185 omega_bw_ir_init(prog);
186 Em em; memset(&em, 0, sizeof em); em.p = prog; Em *e = &em;
187 int udesc = UV64(e), px = V64(e), py = V64(e), tid = V(e), cx = V(e), gi = V(e), x = V64(e), d = V(e);
188 if (e->err) return -1;
189 ldcu64(e, udesc, 0x358); ldc64(e, px, 0x380); ldc64(e, py, 0x388);
190 s2r(e, tid, BW_SR_TID_X); s2r(e, cx, BW_SR_CTAID_X);
191 imadi(e, gi, cx, 0, 128u, tid);
192 imadw(e, px, gi, 8u, px); imadw(e, py, gi, 4u, py);
193 ldg_wide(e, BW_IR_LDG_E_64, x, px, 0, udesc, 1);
194 f2e4(e, d, x, 0, x, 1);
195 stg(e, py, 0, d, udesc);
196 tail(e);
197 if (e->err) return -1;
198 return omega_bw_regalloc_solve(prog);
199}
200#else
201int omega_blackwell_codegen_qmma_probe_prog(int mutant, uint32_t wait_nops, BlackwellIRProgram *prog) {
202 if (!prog || mutant < 0) return -1;
203 return omega_gpu_om_ir_replay(omega_cg_qmma, 0, (uint64_t)mutant, wait_nops, 0, prog, NULL) == OMEGA_OM_IR_OK ? 0 : -1;
204}
205
206int omega_blackwell_codegen_qmma_peak_prog(uint32_t iters, uint32_t chains, uint32_t stall, uint32_t threads, BlackwellIRProgram *prog) {
207 if (!prog) return -1;
208 return omega_gpu_om_ir_replay(omega_cg_qmma, 1, iters, chains, (uint64_t)stall | (uint64_t)threads << 32, prog, NULL) == OMEGA_OM_IR_OK ? 0 : -1;
209}
210
211int omega_blackwell_codegen_f2e4_probe_prog(BlackwellIRProgram *prog) {
212 if (!prog) return -1;
213 return omega_gpu_om_ir_replay(omega_cg_qmma, 2, 0, 0, 0, prog, NULL) == OMEGA_OM_IR_OK ? 0 : -1;
214}
215#endif