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

290 lines · 15414 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* omega_gpu_tcgemm_api.c: tensor-core FP8 grouped GEMM on the shared session (OWN-30B T3 cut 1). See the header.
3 * Submission path and buffer policy follow omega_gpu_fp8gemm_api.c (GPU-uncached buffers, one omega_gpu_session_launch). */
4#include "omega_gpu_tcgemm_api.h"
5#include "omega_gpu_session.h"
6#include "omega_gpu_fmt.h"
7#include "omega_gpu_native_runtime.h"
8#include "omega_gpu_code_alloc.h"
9#include <string.h>
10#include "omega_api_shells.h"
11#include "omega_api_views_rest.h"
12#include <stdlib.h>
13
14#define CACHE_SLOTS 8
15#define MARKER_TIMEOUT_MS 20000ull
16#define POISON_F32 (-999.0f)
17
18typedef struct { bool valid, pinned; OmegaTcGemmSpec spec; OmegaBlackwellKernel kernel; OmegaGpuMem code; } Slot;
19typedef struct { uint64_t w_va, s_va; uint32_t row0, cnt, pad[2]; } TileEntry;
20_Static_assert(sizeof(TileEntry) == OMEGA_TCGEMM_TILE_BYTES, "tile entry layout");
21_Static_assert(offsetof(TileEntry, row0) == 16 && offsetof(TileEntry, cnt) == 20, "tile view fields");
22
23static struct {
24 OmegaGpuScratch x_buf, y_buf, tile_buf, ridx_buf;
25 uint32_t rt, ct, warps;
26 int mutant;
27 bool hooked;
28 Slot cache[CACHE_SLOTS];
29 unsigned next;
30 OmegaGpuNativeCache native;
31} g;
32
33const char *omega_gpu_tcgemm_last_error(void) { return omega_gpu_session_last_error(); }
34const char *omega_gpu_tcgemm_rc_name(int rc) {
35 switch (rc) {
36 case OMEGA_GPU_TCGEMM_OK: return "OK";
37 case OMEGA_GPU_TCGEMM_BAD_ARGS: return "BAD_ARGS";
38 case OMEGA_GPU_TCGEMM_TOO_LARGE: return "TOO_LARGE";
39 case OMEGA_GPU_TCGEMM_CODEGEN_FAIL: return "CODEGEN_FAIL";
40 case OMEGA_GPU_TCGEMM_CHIP_FAIL: return "CHIP_FAIL";
41 default: return "UNKNOWN";
42 }
43}
44static void cache_clear_locked(void) {
45 for (unsigned i = 0; i < CACHE_SLOTS; i++) {
46 if (!g.cache[i].valid) continue;
47 if (g.cache[i].pinned) {
48 OmegaGpuNativeLease lease = omega_native_cache_lease(&g.native, i);
49 if (omega_native_lease_op(&lease, 5, 0) == 0) g.cache[i].pinned = false;
50 }
51 if (omega_native_cache_victim(&g.native, i, 0) != 1) continue;
52 omega_gpu_session_free(&g.cache[i].code);
53 (void)omega_native_cache_victim(&g.native, i, 1);
54 memset(&g.cache[i], 0, sizeof g.cache[i]);
55 }
56 g.next = 0;
57}
58static void on_session_close(void) {
59 cache_clear_locked();
60 omega_gpu_session_scratch_free(&g.x_buf); omega_gpu_session_scratch_free(&g.y_buf);
61 omega_gpu_session_scratch_free(&g.tile_buf); omega_gpu_session_scratch_free(&g.ridx_buf);
62}
63static OmegaGpuSession *dev_open_locked(void) {
64 if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; }
65 return omega_gpu_session_open();
66}
67void omega_gpu_tcgemm_cache_clear(void) { omega_gpu_session_lock(); cache_clear_locked(); omega_gpu_session_unlock(); }
68void omega_gpu_tcgemm_set_shape(uint32_t rt, uint32_t ct, uint32_t w) { omega_gpu_session_lock(); g.rt = rt; g.ct = ct; g.warps = w; cache_clear_locked(); omega_gpu_session_unlock(); }
69void omega_gpu_tcgemm_test_set_mutant(int m) { omega_gpu_session_lock(); g.mutant = m; cache_clear_locked(); omega_gpu_session_unlock(); }
70
71void omega_gpu_tcgemm_spec_for(uint32_t k, uint32_t n, OmegaTcGemmSpec *spec) {
72#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
73 memset(spec, 0, sizeof *spec);
74 spec->k = k;
75 omega_api_tcsp(g.rt, g.ct, n, &spec->rt, &spec->ct);
76 spec->warps = omega_api_tcw(g.warps);
77 spec->mutant = g.mutant;
78#else
79 _Static_assert(sizeof *spec == 5 * sizeof(uint32_t), "tensor-core specification view");
80 const uint32_t cfg[8] = {k, n, g.rt, g.ct, g.warps, (uint32_t)g.mutant};
81 uint64_t result = 0;
82 ar_check(omega_artcsp(&result, cfg, 8, spec, 5));
83#endif
84}
85
86static int kernel_for(const OmegaTcGemmSpec *spec, Slot **out, bool *hit) {
87 int fresh = omega_native_cache_refresh(&g.native, CACHE_SLOTS);
88 if (fresh < 0) return OMEGA_GPU_TCGEMM_CODEGEN_FAIL;
89 if (fresh) memset(g.cache, 0, sizeof g.cache);
90#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
91 for (unsigned i = 0; i < CACHE_SLOTS; i++) { Slot *s = &g.cache[i]; if (s->valid && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_TCGEMM_OK; } }
92#else
93 size_t found = ar_cache(g.cache, sizeof g.cache, CACHE_SLOTS, sizeof(Slot), offsetof(Slot, valid), offsetof(Slot, spec), spec, sizeof *spec);
94 if (found != SIZE_MAX) { *out = &g.cache[found]; *hit = true; return OMEGA_GPU_TCGEMM_OK; }
95#endif
96 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog);
97 if (!prog) { omega_gpu_session_set_error("tc gemm codegen: host memory"); return OMEGA_GPU_TCGEMM_CODEGEN_FAIL; }
98 int cg = omega_blackwell_codegen_tcgemm_prog(spec, prog);
99 size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0;
100 if (cg != 0 || need == 0) { omega_gpu_host_free(prog); omega_gpu_session_set_error("tc gemm codegen refused the shape"); return OMEGA_GPU_TCGEMM_CODEGEN_FAIL; }
101 const uint32_t decl = omega_bw_gpr_declared(prog->regalloc.peak_gpr_usage);
102 if (!omega_api_le128(decl) || !omega_api_regfit(spec->warps, decl)) {
103 char e[160]; omega_gpu_fmt(e, sizeof e, "tc gemm kernel: %u warps x %u declared registers do not fit", spec->warps, decl);
104 omega_gpu_session_set_error(e); omega_gpu_host_free(prog); return OMEGA_GPU_TCGEMM_TOO_LARGE;
105 }
106 uint8_t key[32]; uint64_t request[16];
107 int index = omega_native_shape_key(spec, sizeof *spec, key) == 0 ?
108 omega_native_cache_reserve(&g.native, key, omega_gpu_code_alloc_bytes(need), request) : -1;
109 if (index < 0) { omega_gpu_host_free(prog); return OMEGA_GPU_TCGEMM_TOO_LARGE; }
110 Slot *s = &g.cache[index];
111 OmegaGpuMem code = {0}; OmegaBlackwellKernel kernel;
112 if (omega_gpu_session_alloc_code(need, &code) != 0) cg = -1;
113 else cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel);
114 omega_gpu_host_free(prog);
115 if (cg != 0 || !kernel.code || kernel.code_size != need ||
116 omega_gpu_session_sync_to_gpu(&code) != 0 ||
117 omega_native_cache_finish(&g.native, (unsigned)index, request, 1) != 0) {
118 omega_gpu_session_free(&code);
119 (void)omega_native_cache_finish(&g.native, (unsigned)index, request, 0);
120 return OMEGA_GPU_TCGEMM_CHIP_FAIL;
121 }
122 if (s->valid) omega_gpu_session_free(&s->code);
123 memset(s, 0, sizeof *s);
124 s->valid = true; s->spec = *spec; s->kernel = kernel; s->code = code;
125 *out = s; *hit = false;
126 return OMEGA_GPU_TCGEMM_OK;
127}
128
129int omega_gpu_tcgemm_prepare(uint32_t k) {
130 OmegaTcGemmSpec sp; omega_gpu_tcgemm_spec_for(k, 128u, &sp);
131 omega_gpu_session_lock();
132 int rc = OMEGA_GPU_TCGEMM_OK; Slot *s = NULL; bool hit = false;
133 if (!dev_open_locked()) rc = OMEGA_GPU_TCGEMM_CHIP_FAIL;
134 else if ((rc = kernel_for(&sp, &s, &hit)) == OMEGA_GPU_TCGEMM_OK && !s->pinned) {
135 OmegaGpuNativeLease lease = omega_native_cache_lease(&g.native, (unsigned)(s - g.cache));
136 if (omega_native_lease_op(&lease, 5, 1) != 0) rc = OMEGA_GPU_TCGEMM_TOO_LARGE;
137 else s->pinned = true;
138 }
139 omega_gpu_session_unlock();
140 return rc;
141}
142
143int omega_gpu_tcgemm_f32(const OmegaGpuFp8Tensor *t, uint32_t M, const uint32_t *ids, const uint32_t *trow, const float *x, uint32_t x_rows, float *y, OmegaGpuTcGemmInfo *info) {
144 uint64_t t0 = omega_gpu_session_now_ns();
145 if (info) ar_words(info, sizeof *info / sizeof(uint32_t), NULL, 0);
146 uint32_t experts = 0, n = 0, k = 0;
147 if (!t) return OMEGA_GPU_TCGEMM_BAD_ARGS;
148 omega_gpu_fp8_tensor_shape(t, &experts, &n, &k);
149 if (!experts || !ids || !trow || !x || !y || M == 0 || x_rows == 0) return OMEGA_GPU_TCGEMM_BAD_ARGS;
150 OmegaTcGemmSpec spec; omega_gpu_tcgemm_spec_for(k, n, &spec);
151 if (!omega_tcgemm_spec_ok(&spec)) return OMEGA_GPU_TCGEMM_BAD_ARGS;
152 uint64_t plan[32] = {7, M, n, k, 0, spec.rt, spec.ct, spec.warps, x_rows};
153 (void)ar_plan(plan);
154 const uint32_t BM = (uint32_t)plan[16], span = (uint32_t)plan[17];
155 if (n % span || n % 128u) return OMEGA_GPU_TCGEMM_BAD_ARGS;
156 uint32_t segs = 0, ntiles = 0;
157#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
158 for (uint32_t i = 0; i < M;) {
159 if (ids[i] >= experts) return OMEGA_GPU_TCGEMM_BAD_ARGS;
160 uint32_t j = i; while (j < M && ids[j] == ids[i]) { if (trow[j] >= x_rows) return OMEGA_GPU_TCGEMM_BAD_ARGS; j++; }
161 if (j < M && ids[j] < ids[i]) return OMEGA_GPU_TCGEMM_BAD_ARGS;
162 segs++; ntiles += (j - i + BM - 1) / BM; i = j;
163 }
164#else
165 const uint64_t group_cfg[4] = {experts, x_rows, BM};
166 uint64_t grouped = 0;
167 ar_check(omega_argroups(&grouped, group_cfg, 4, ids, M, trow, M));
168 if (grouped == UINT64_MAX) return OMEGA_GPU_TCGEMM_BAD_ARGS;
169 segs = (uint32_t)grouped; ntiles = (uint32_t)(grouped >> 32);
170#endif
171 if (ntiles > 65535u) return OMEGA_GPU_TCGEMM_TOO_LARGE;
172 const uint32_t grid_x = (uint32_t)plan[25];
173 plan[4] = ntiles;
174 (void)ar_plan(plan);
175
176 omega_gpu_session_lock();
177 int rc = OMEGA_GPU_TCGEMM_OK;
178 if (!dev_open_locked()) { omega_gpu_session_unlock(); return OMEGA_GPU_TCGEMM_CHIP_FAIL; }
179 int src = omega_gpu_session_scratch(&g.x_buf, plan[19]);
180 if (src == 0) src = omega_gpu_session_scratch(&g.y_buf, plan[20]);
181 if (src == 0) src = omega_gpu_session_scratch(&g.tile_buf, plan[21]);
182 if (src == 0) src = omega_gpu_session_scratch(&g.ridx_buf, plan[22]);
183 if (src != 0) { omega_gpu_session_unlock(); return src == OMEGA_GPU_SESSION_SCRATCH_RESERVED ? OMEGA_GPU_TCGEMM_TOO_LARGE : OMEGA_GPU_TCGEMM_CHIP_FAIL; }
184 Slot *ks = NULL; bool hit = false;
185 rc = kernel_for(&spec, &ks, &hit);
186 if (rc != OMEGA_GPU_TCGEMM_OK) { omega_gpu_session_unlock(); return rc; }
187
188 TileEntry *tab = g.tile_buf.mem.cpu; uint32_t *ridx = g.ridx_buf.mem.cpu; uint32_t nt = 0;
189#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
190 uint64_t distinct = 0;
191#endif
192 for (uint32_t i = 0; i < M;) {
193#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
194 uint32_t j = i; while (j < M && ids[j] == ids[i]) j++;
195 uint64_t w_va = 0, s_va = 0; (void)omega_gpu_fp8_tensor_entry(t, ids[i], &w_va, &s_va);
196 for (uint32_t r = i; r < j; r += BM) { memset(&tab[nt], 0, sizeof tab[nt]); tab[nt].w_va = w_va; tab[nt].s_va = s_va; tab[nt].row0 = r; tab[nt].cnt = j - r < BM ? j - r : BM; nt++; }
197 distinct += (uint64_t)n * k; i = j;
198#else
199 uint64_t end = 0, next = 0;
200 ar_check(omega_arrun(&end, i, ids, M));
201 uint64_t cfg[8] = {nt, i, end, BM};
202 (void)omega_gpu_fp8_tensor_entry(t, ids[i], &cfg[4], &cfg[5]);
203 ar_check(omega_argtile(&next, cfg, 8, tab, plan[27]));
204 nt = (uint32_t)next; i = (uint32_t)end;
205#endif
206 }
207#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
208 { uint32_t *tmp = malloc(((size_t)M + BM) * 4u); if (!tmp) { omega_gpu_session_unlock(); return OMEGA_GPU_TCGEMM_CHIP_FAIL; }
209 for (uint32_t i = 0; i < M + BM; i++) tmp[i] = i < M ? trow[i] : 0u;
210 memcpy(ridx, tmp, ((size_t)M + BM) * 4u); free(tmp); }
211 { /* permute in cached memory, one sequential copy per row into the uncached buffer */
212 uint16_t *row = malloc((size_t)k * 2u); uint16_t *xd = g.x_buf.mem.cpu;
213 if (!row) { omega_gpu_session_unlock(); return OMEGA_GPU_TCGEMM_CHIP_FAIL; }
214 for (uint32_t r = 0; r < x_rows; r++) { omega_tcgemm_permute_x(x + (size_t)r * k, k, row); memcpy(xd + (size_t)r * k, row, (size_t)k * 2u); }
215 free(row);
216 }
217#else
218 uint64_t packed = 0;
219 ar_check(omega_arpad(&packed, trow, M, ridx, plan[28]));
220 if (packed) abort();
221 ar_check(omega_artcperm(&packed, x, plan[24], g.x_buf.mem.cpu, plan[24]));
222 if (packed) abort();
223#endif
224 float *yd = g.y_buf.mem.cpu;
225#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
226 for (size_t i = 0; i < (size_t)M * n; i++) yd[i] = POISON_F32;
227#else
228 uint32_t poison; memcpy(&poison, &(float){POISON_F32}, sizeof poison);
229 ar_fill((uint32_t *)yd, plan[23], poison);
230#endif
231 if (omega_gpu_session_sync_to_gpu(&g.tile_buf.mem) != 0 || omega_gpu_session_sync_to_gpu(&g.ridx_buf.mem) != 0 ||
232 omega_gpu_session_sync_to_gpu(&g.x_buf.mem) != 0 || omega_gpu_session_sync_to_gpu(&g.y_buf.mem) != 0) rc = OMEGA_GPU_TCGEMM_CHIP_FAIL;
233
234 uint32_t args[OMEGA_TCGEMM_ARGS_WORDS];
235 const uint64_t va[4] = { g.tile_buf.mem.va, g.ridx_buf.mem.va, g.x_buf.mem.va, g.y_buf.mem.va };
236#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
237 for (int i = 0; i < 4; i++) { args[2 * i] = (uint32_t)va[i]; args[2 * i + 1] = (uint32_t)(va[i] >> 32); }
238 args[8] = n;
239#else
240 ar_args_extra(args, OMEGA_TCGEMM_ARGS_WORDS, va, 4, &n, 1);
241#endif
242 const uint32_t decl = omega_bw_gpr_declared(ks->kernel.gpr_count);
243 if (info) {
244#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
245 info->rows = M; info->segments = segs; info->tiles = nt; info->n = n; info->k = k; info->rt = spec.rt; info->ct = spec.ct; info->warps = spec.warps;
246 info->grid_x = grid_x; info->grid_y = nt; info->gpr_declared = decl;
247 info->weight_bytes = distinct; info->streamed_bytes = (uint64_t)nt * n * k;
248 info->act_bytes = (uint64_t)nt * grid_x * BM * k * 2u; info->kernel_cache_hit = hit;
249#else
250 _Static_assert(sizeof *info == 24 * sizeof(uint32_t), "tensor-core information extent");
251 _Static_assert(offsetof(OmegaGpuTcGemmInfo, weight_bytes) == 48, "tensor-core information byte counters");
252 _Static_assert(offsetof(OmegaGpuTcGemmInfo, kernel_cache_hit) == 92, "tensor-core information cache hit");
253 const uint64_t cfg[16] = {M, segs, nt, n, k, spec.rt, spec.ct, spec.warps, grid_x, decl, hit};
254 uint64_t result = 0;
255 ar_check(omega_artcinfo(&result, cfg, 16, info, 24));
256#endif
257 }
258 if (rc == OMEGA_GPU_TCGEMM_OK) {
259#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
260 OmegaGpuLaunch L = {
261 .code_va = ks->code.va, .gpr_count = decl,
262 .threads_x = 32u * spec.warps, .threads_y = 1, .grid_x = grid_x, .grid_y = nt,
263 .num_elements = (size_t)M * n,
264 .args = args, .n_args = OMEGA_TCGEMM_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS,
265 .shared_bytes = omega_tcgemm_shared_bytes(&spec),
266 };
267#else
268 OmegaGpuLaunch L;
269 const uint64_t launch[16] = {ks->code.va, decl, plan[18], 1, grid_x, nt, plan[23],
270 (uintptr_t)args, OMEGA_TCGEMM_ARGS_WORDS, MARKER_TIMEOUT_MS, plan[26]};
271 ar_launch(&L, launch);
272#endif
273 if (!omega_bw_gpr_declared_ok(L.gpr_count, ks->kernel.gpr_count)) { omega_gpu_session_set_error("declared registers below peak + 2: refusing to launch"); rc = OMEGA_GPU_TCGEMM_CHIP_FAIL; }
274 else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL) != 0) rc = OMEGA_GPU_TCGEMM_CHIP_FAIL;
275 }
276 if (rc == OMEGA_GPU_TCGEMM_OK && omega_gpu_session_sync_to_cpu(&g.y_buf.mem) != 0) rc = OMEGA_GPU_TCGEMM_CHIP_FAIL;
277 if (rc == OMEGA_GPU_TCGEMM_OK) {
278 size_t unchanged = 0;
279#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
280 for (size_t i = 0; i < (size_t)M * n; i++) if (yd[i] == POISON_F32) unchanged++;
281#else
282 unchanged = ar_count((const uint32_t *)yd, plan[23], poison);
283#endif
284 if (unchanged) { omega_gpu_session_set_error("output poison survived"); rc = OMEGA_GPU_TCGEMM_CHIP_FAIL; }
285 else ar_copy(y, yd, plan[20]);
286 }
287 omega_gpu_session_unlock();
288 if (info) info->call_ns = omega_gpu_session_now_ns() - t0;
289 return rc;
290}