interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / src / omega_gpu_gemm.c

234 lines · 11626 bytes Raw Blame History
1/* omega_gpu_gemm.c -- matrix multiply of any shape on the GB10 tensor cores (C3). See the header. */
2#include "omega_gpu_gemm.h"
3#include "omega_bw_om_ir.h"
4#include "omega_blackwell_codegen_qmma.h"
5#include "omega_native.h"
6#include "omega_gpu_session.h"
7
8#include <stdlib.h>
9#include <string.h>
10
11#define POISON 0x7fa00badu /* a signalling NaN no tensor-core op writes: a word still holding it was never stored */
12#define TIMEOUT_MS 60000ull
13#define FRAG 512u /* bytes of one warp fragment: 32 lanes x 16 */
14
15static int pow124(uint32_t v) { return v == 1u || v == 2u || v == 4u; }
16
17void omega_gpu_gemm_default_cfg(OmegaGpuGemmType type, OmegaGemmCfg *cfg) {
18 memset(cfg, 0, sizeof *cfg);
19 cfg->type = type; cfg->rt = 2; cfg->ct = 4; cfg->wm = 2; cfg->wn = 2; cfg->stall = type == OMEGA_GPU_GEMM_BF16 ? 4u : 2u;
20}
21
22int omega_gpu_gemm_cfg_ok(const OmegaGemmCfg *c) {
23 return c && (c->type == OMEGA_GPU_GEMM_BF16 || c->type == OMEGA_GPU_GEMM_E4M3) && pow124(c->rt) && pow124(c->ct) &&
24 pow124(c->wm) && pow124(c->wn) && c->stall >= 1u && c->stall <= 15u && c->mutant >= 0 && c->mutant <= 3 &&
25 4u * (4u * c->rt + 2u * c->ct + c->rt * c->ct) <= 100u; /* registers: two operand sets + accumulators, R2..R127 in all */
26}
27
28int omega_gpu_gemm_codegen(const OmegaGemmCfg *cfg, OmegaGemmKind kind, BlackwellIRProgram *prog) {
29 if (!cfg || !prog || (unsigned)kind >= OMEGA_GEMM_K_COUNT || cfg->mutant < 0) {
30 if (prog) omega_bw_ir_init(prog);
31 return -1;
32 }
33 uint64_t sp[8] = {
34 (uint64_t)cfg->type, cfg->rt, cfg->ct, cfg->wm, cfg->wn, cfg->stall, (uint64_t)cfg->mutant, (uint64_t)kind
35 };
36 uint64_t ins[8192], meta[512];
37 memset(ins, 0, sizeof(ins));
38 memset(meta, 0, sizeof(meta));
39 uint64_t rc = 1;
40 if (omega2_cg_gpu_gemm_emit(&rc, sp, 8, ins, 8192, meta, 512) != OMEGA_OK || rc != 0) {
41 omega_bw_ir_init(prog);
42 return -1;
43 }
44 return omega_bw_ir_from_om(ins, meta, prog);
45}
46
47/* ------------------------------------------------------------------ plan and driver */
48
49int omega_gpu_gemm_plan(const OmegaGemmCfg *c, uint32_t M, uint32_t N, uint32_t K, OmegaGemmPlan *p) {
50 if (!c || !p || c->mutant < 0) return OMEGA_GPU_GEMM_BAD_ARGS;
51 uint64_t sp[10] = {
52 (uint64_t)c->type, c->rt, c->ct, c->wm, c->wn, c->stall, (uint64_t)c->mutant, M, N, K
53 };
54 uint64_t res[18];
55 memset(res, 0, sizeof(res));
56 uint64_t rc = 1;
57 if (omega2_cg_gpu_gemm_plan(&rc, sp, 10, res, 18) != OMEGA_OK) return OMEGA_GPU_GEMM_BAD_ARGS;
58 if (rc == 2u) return OMEGA_GPU_GEMM_TOO_LARGE;
59 if (rc != 0u) return OMEGA_GPU_GEMM_BAD_ARGS;
60 memset(p, 0, sizeof *p);
61 p->Mp = (uint32_t)res[0];
62 p->Np = (uint32_t)res[1];
63 p->Kp = (uint32_t)res[2];
64 p->KS2 = (uint32_t)res[3];
65 p->a_bytes = res[4];
66 p->b_bytes = res[5];
67 p->ap_bytes = res[6];
68 p->bp_bytes = res[7];
69 p->grid[OMEGA_GEMM_K_PACK_A][0] = (uint32_t)res[8];
70 p->grid[OMEGA_GEMM_K_PACK_A][1] = (uint32_t)res[9];
71 p->grid[OMEGA_GEMM_K_PACK_B][0] = (uint32_t)res[10];
72 p->grid[OMEGA_GEMM_K_PACK_B][1] = (uint32_t)res[11];
73 p->grid[OMEGA_GEMM_K_MAIN][0] = (uint32_t)res[12];
74 p->grid[OMEGA_GEMM_K_MAIN][1] = (uint32_t)res[13];
75 p->threads[OMEGA_GEMM_K_PACK_A] = (uint32_t)res[14];
76 p->threads[OMEGA_GEMM_K_PACK_B] = (uint32_t)res[15];
77 p->threads[OMEGA_GEMM_K_MAIN] = (uint32_t)res[16];
78 return OMEGA_GPU_GEMM_OK;
79}
80
81static void put64(uint32_t *a, unsigned w, uint64_t v) { a[w] = (uint32_t)v; a[w + 1] = (uint32_t)(v >> 32); }
82
83int omega_gpu_gemm_drive(const OmegaGemmCfg *c, unsigned layout, uint32_t M, uint32_t N, uint32_t K, uint64_t a_va,
84 uint64_t b_va, uint64_t ap_va, uint64_t bp_va, uint64_t c_va, OmegaGemmLaunchFn launch, void *ctx) {
85 if (!launch || (layout & ~3u)) return OMEGA_GPU_GEMM_BAD_ARGS;
86 OmegaGemmPlan p;
87 int rc = omega_gpu_gemm_plan(c, M, N, K, &p);
88 if (rc != OMEGA_GPU_GEMM_OK) return rc;
89 uint32_t a[OMEGA_GPU_GEMM_ARGS_WORDS];
90 /* A element (m, k): row-major m K + k, column-major k M + m */
91 memset(a, 0, sizeof a);
92 put64(a, 0, a_va); put64(a, 2, ap_va); a[4] = M; a[5] = K;
93 a[6] = layout & OMEGA_GPU_GEMM_A_COL ? 1u : K; a[7] = layout & OMEGA_GPU_GEMM_A_COL ? M : 1u; a[8] = p.KS2;
94 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;
95 /* B element (k, n) as packed row n: row-major k N + n, column-major n K + k */
96 memset(a, 0, sizeof a);
97 put64(a, 0, b_va); put64(a, 2, bp_va); a[4] = N; a[5] = K;
98 a[6] = layout & OMEGA_GPU_GEMM_B_COL ? K : 1u; a[7] = layout & OMEGA_GPU_GEMM_B_COL ? 1u : N; a[8] = p.KS2;
99 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;
100 memset(a, 0, sizeof a);
101 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;
102 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;
103 return OMEGA_GPU_GEMM_OK;
104}
105
106/* ------------------------------------------------------------------ the chip */
107
108static struct {
109 int set[2];
110 OmegaGemmCfg cfg[2];
111 OmegaGpuMem code[2][OMEGA_GEMM_K_COUNT];
112 uint32_t gpr[2][OMEGA_GEMM_K_COUNT];
113 int hooked;
114} g;
115
116/* lock held; the session calls it right before it closes the device (also in omega_gpu_session_recover): the kernels go
117 * with the device, the next call builds them again on the new one */
118static void on_session_close(void) {
119 for (unsigned t = 0; t < 2; t++)
120 for (unsigned k = 0; k < OMEGA_GEMM_K_COUNT; k++) if (g.code[t][k].cpu) omega_gpu_session_free(&g.code[t][k]);
121 memset(g.code, 0, sizeof g.code);
122}
123
124static const OmegaGemmCfg *cfg_of(OmegaGpuGemmType t) {
125 if (!g.set[t]) { omega_gpu_gemm_default_cfg(t, &g.cfg[t]); g.set[t] = 1; }
126 return &g.cfg[t];
127}
128
129int omega_gpu_gemm_test_set_cfg(OmegaGpuGemmType type, const OmegaGemmCfg *cfg) {
130 if ((unsigned)type > OMEGA_GPU_GEMM_E4M3 || (cfg && (!omega_gpu_gemm_cfg_ok(cfg) || cfg->type != type))) return OMEGA_GPU_GEMM_BAD_ARGS;
131 omega_gpu_session_lock();
132 for (unsigned k = 0; k < OMEGA_GEMM_K_COUNT; k++) if (g.code[type][k].cpu) omega_gpu_session_free(&g.code[type][k]);
133 memset(g.code[type], 0, sizeof g.code[type]);
134 if (cfg) g.cfg[type] = *cfg; else omega_gpu_gemm_default_cfg(type, &g.cfg[type]);
135 g.set[type] = 1;
136 omega_gpu_session_unlock();
137 return OMEGA_GPU_GEMM_OK;
138}
139
140const char *omega_gpu_gemm_rc_name(int rc) {
141 switch (rc) {
142 case OMEGA_GPU_GEMM_OK: return "OK";
143 case OMEGA_GPU_GEMM_BAD_ARGS: return "BAD_ARGS";
144 case OMEGA_GPU_GEMM_TOO_LARGE: return "TOO_LARGE";
145 case OMEGA_GPU_GEMM_CODEGEN_FAIL: return "CODEGEN_FAIL";
146 case OMEGA_GPU_GEMM_CHIP_FAIL: return "CHIP_FAIL";
147 default: return "?";
148 }
149}
150
151/* lock held */
152static int kernel_locked(OmegaGpuGemmType t, OmegaGemmKind k) {
153 if (g.code[t][k].cpu) return OMEGA_GPU_GEMM_OK;
154 if (!g.hooked) { /* without the hook a kernel would outlive its device */
155 if (omega_gpu_session_on_close(on_session_close) != 0) return OMEGA_GPU_GEMM_CHIP_FAIL;
156 g.hooked = 1;
157 }
158 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog);
159 if (!prog) return OMEGA_GPU_GEMM_CODEGEN_FAIL;
160 int rc = OMEGA_GPU_GEMM_OK;
161 size_t need = 0;
162 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;
163 else if (omega_gpu_session_alloc_code(need, &g.code[t][k]) != 0) rc = OMEGA_GPU_GEMM_CHIP_FAIL;
164 else {
165 OmegaBlackwellKernel kk;
166 if (omega_blackwell_kernel_encode_into(prog, g.code[t][k].cpu, need, &kk) != 0 || kk.code_size != need ||
167 omega_qmma_patch_code(prog, g.code[t][k].cpu, need) < 0) rc = OMEGA_GPU_GEMM_CODEGEN_FAIL;
168 else if ((g.gpr[t][k] = omega_bw_gpr_declared(kk.gpr_count)) > 255u) rc = OMEGA_GPU_GEMM_CODEGEN_FAIL;
169 else if (omega_gpu_session_sync_to_gpu(&g.code[t][k]) != 0) rc = OMEGA_GPU_GEMM_CHIP_FAIL;
170 if (g.gpr[t][k] < 64u) g.gpr[t][k] = 64u;
171 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]); }
172 }
173 if (rc == OMEGA_GPU_GEMM_CODEGEN_FAIL) omega_gpu_session_set_error("gemm: our codegen refused a kernel");
174 omega_gpu_host_free(prog);
175 return rc;
176}
177
178typedef struct { OmegaGpuGemmType t; uint64_t pack_ns, main_ns; } ChipCtx;
179
180/* lock held */
181static 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) {
182 ChipCtx *c = ctx;
183 if (kernel_locked(c->t, k) != OMEGA_GPU_GEMM_OK) return -1;
184 OmegaGpuLaunch L = {
185 .code_va = g.code[c->t][k].va, .gpr_count = g.gpr[c->t][k], .threads_x = threads, .threads_y = 1,
186 .grid_x = gx, .grid_y = gy, .num_elements = (size_t)gx * gy * threads,
187 .args = args, .n_args = OMEGA_GPU_GEMM_ARGS_WORDS, .timeout_ms = TIMEOUT_MS,
188 };
189 uint64_t ns = 0;
190 if (omega_gpu_session_launch(&L, &ns, NULL) != 0) return -1;
191 if (k == OMEGA_GEMM_K_MAIN) c->main_ns += ns; else c->pack_ns += ns;
192 return 0;
193}
194
195static uint64_t up16(uint64_t v) { return (v + 255u) & ~(uint64_t)255u; }
196
197int omega_gpu_gemm(OmegaGpuGemmType type, unsigned layout, uint32_t M, uint32_t N, uint32_t K, const void *A, const void *B,
198 float *C, OmegaGpuGemmInfo *info) {
199 uint64_t t0 = omega_gpu_session_now_ns();
200 if (info) memset(info, 0, sizeof *info);
201 if ((unsigned)type > OMEGA_GPU_GEMM_E4M3 || (layout & ~3u) || !A || !B || !C) return OMEGA_GPU_GEMM_BAD_ARGS;
202 omega_gpu_session_lock();
203 const OmegaGemmCfg *cfg = cfg_of(type);
204 OmegaGemmPlan p;
205 int rc = omega_gpu_gemm_plan(cfg, M, N, K, &p);
206 if (rc != OMEGA_GPU_GEMM_OK) { omega_gpu_session_unlock(); return rc; }
207 /* one buffer: A | B | packed A | packed B | C (each 256-byte aligned; A and B carry 16 bytes of readable slack) */
208 const uint64_t oa = 0, ob = up16(p.a_bytes + 16u), oap = ob + up16(p.b_bytes + 16u), obp = oap + up16(p.ap_bytes),
209 oc = obp + up16(p.bp_bytes), total = oc + (uint64_t)M * N * 4u;
210 OmegaGpuMem m;
211 if (!omega_gpu_session_open() || omega_gpu_session_alloc(total, &m) != 0) { omega_gpu_session_unlock(); return OMEGA_GPU_GEMM_CHIP_FAIL; }
212 uint8_t *base = m.cpu;
213 memcpy(base + oa, A, p.a_bytes); memcpy(base + ob, B, p.b_bytes);
214 uint32_t *cw = (uint32_t *)(base + oc);
215 for (uint64_t i = 0; i < (uint64_t)M * N; i++) cw[i] = POISON;
216 ChipCtx cc = { type, 0, 0 };
217 if (omega_gpu_session_sync_to_gpu(&m) != 0) rc = OMEGA_GPU_GEMM_CHIP_FAIL;
218 else rc = omega_gpu_gemm_drive(cfg, layout, M, N, K, m.va + oa, m.va + ob, m.va + oap, m.va + obp, m.va + oc, chip_launch, &cc);
219 if (rc == OMEGA_GPU_GEMM_OK && omega_gpu_session_sync_to_cpu(&m) != 0) rc = OMEGA_GPU_GEMM_CHIP_FAIL;
220 if (rc == OMEGA_GPU_GEMM_OK) {
221 uint64_t unwritten = 0;
222 for (uint64_t i = 0; i < (uint64_t)M * N; i++) unwritten += cw[i] == POISON;
223 if (unwritten) { omega_gpu_session_set_error("gemm: output words never written"); rc = OMEGA_GPU_GEMM_CHIP_FAIL; }
224 else memcpy(C, cw, (size_t)M * N * 4u);
225 }
226 if (info) {
227 info->pack_ns = cc.pack_ns; info->main_ns = cc.main_ns;
228 for (unsigned k = 0; k < OMEGA_GEMM_K_COUNT; k++) info->gpr[k] = g.gpr[type][k];
229 }
230 omega_gpu_session_free(&m);
231 omega_gpu_session_unlock();
232 if (info) info->call_ns = omega_gpu_session_now_ns() - t0;
233 return rc;
234}