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

635 lines · 33346 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* omega_gpu_fp8gemm_api.c: FP8 grouped GEMM for prefill on the shared session (OWN-30B builder F item 26).
3 * See the header. Submission path and buffer policy are the FP8 GEMV API's: every buffer both sides touch is
4 * GPU-uncached (nvos.h NVOS32_ATTR2_GPU_CACHEABLE), one launch = omega_gpu_session_launch. */
5#include "omega_gpu_fp8gemm_api.h"
6#include "omega_gpu_session.h"
7#include "omega_gpu_cmdlist.h"
8#include "omega_gpu_fmt.h"
9#include "omega_gpu_native_runtime.h"
10#include "omega_gpu_code_alloc.h"
11#include "omega_api_shells.h"
12#include "omega_api_views_rest.h"
13#include <string.h>
14#include <stdlib.h>
15
16#define CACHE_SLOTS 8
17#define MARKER_TIMEOUT_MS 20000ull
18#define POISON_F32 (-999.0f)
19
20typedef struct {
21 bool valid, pinned;
22 bool exact;
23 uint8_t manifest[32];
24 OmegaFp8GemmSpec spec;
25 OmegaBlackwellKernel kernel;
26 OmegaGpuMem code;
27} Slot;
28_Static_assert(sizeof(OmegaFp8GemmSpec) == 6 * sizeof(uint32_t), "FP8 GEMM specification view");
29_Static_assert(sizeof(OmegaGpuFp8GemmInfo) == 22 * sizeof(uint32_t), "FP8 GEMM information view");
30_Static_assert(offsetof(OmegaGpuFp8GemmInfo, weight_bytes) == 40, "FP8 GEMM byte counters");
31_Static_assert(offsetof(OmegaGpuFp8GemmInfo, kernel_cache_hit) == 76, "FP8 GEMM cache hit");
32_Static_assert(offsetof(OmegaGpuFp8GemmInfo, general) == 80, "FP8 GEMM fallback indicator");
33static void write_info(OmegaGpuFp8GemmInfo *info, const uint64_t cfg[16]) {
34 uint64_t result = 0;
35 ar_check(omega_arginfo(&result, cfg, 16, info, 22));
36}
37#if !CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
38static void write_args(uint32_t *args, size_t words, const uint64_t va[5], const uint32_t scalar[2]) {
39 uint64_t result = 0;
40 ar_check(omega_argargs(&result, va, 5, scalar, 2, args, words));
41 if (result) abort();
42}
43#endif
44
45static struct {
46 OmegaGpuScratch x_buf, y_buf, seg_buf;
47 uint32_t mt, warps, ctas;
48 int mutant;
49 bool hooked;
50 Slot cache[CACHE_SLOTS];
51 unsigned next;
52 OmegaGpuNativeCache native;
53} g;
54
55const char *omega_gpu_fp8gemm_last_error(void) { return omega_gpu_session_last_error(); }
56const char *omega_gpu_fp8gemm_rc_name(int rc) {
57 switch (rc) {
58 case OMEGA_GPU_FP8GEMM_OK: return "OK";
59 case OMEGA_GPU_FP8GEMM_BAD_ARGS: return "BAD_ARGS";
60 case OMEGA_GPU_FP8GEMM_TOO_LARGE: return "TOO_LARGE";
61 case OMEGA_GPU_FP8GEMM_CODEGEN_FAIL: return "CODEGEN_FAIL";
62 case OMEGA_GPU_FP8GEMM_CHIP_FAIL: return "CHIP_FAIL";
63 default: return "UNKNOWN";
64 }
65}
66static void cache_clear_locked(void) {
67 for (unsigned i = 0; i < CACHE_SLOTS; i++) {
68 if (!g.cache[i].valid) continue;
69 if (g.cache[i].pinned) {
70 OmegaGpuNativeLease lease = omega_native_cache_lease(&g.native, i);
71 if (omega_native_lease_op(&lease, 5, 0) == 0) g.cache[i].pinned = false;
72 }
73 if (omega_native_cache_victim(&g.native, i, 0) != 1) continue;
74 omega_gpu_session_free(&g.cache[i].code);
75 (void)omega_native_cache_victim(&g.native, i, 1);
76 memset(&g.cache[i], 0, sizeof g.cache[i]);
77 }
78 g.next = 0;
79}
80static void on_session_close(void) {
81 cache_clear_locked();
82 omega_gpu_session_scratch_free(&g.x_buf);
83 omega_gpu_session_scratch_free(&g.y_buf);
84 omega_gpu_session_scratch_free(&g.seg_buf);
85}
86static OmegaGpuSession *dev_open_locked(void) {
87 if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; }
88 return omega_gpu_session_open();
89}
90void omega_gpu_fp8gemm_cache_clear(void) { omega_gpu_session_lock(); cache_clear_locked(); omega_gpu_session_unlock(); }
91void omega_gpu_fp8gemm_set_shape(uint32_t mt, uint32_t warps) { omega_gpu_session_lock(); g.mt = mt; g.warps = warps; cache_clear_locked(); omega_gpu_session_unlock(); }
92void omega_gpu_fp8gemm_set_cta_budget(uint32_t c) { omega_gpu_session_lock(); g.ctas = c; omega_gpu_session_unlock(); }
93void omega_gpu_fp8gemm_test_set_mutant(int m) { omega_gpu_session_lock(); g.mutant = m; cache_clear_locked(); omega_gpu_session_unlock(); }
94
95static uint32_t gpr_declared(uint32_t peak) { return omega_bw_gpr_declared(peak); } /* peak + 2 rounded up to 8: the chip hung at peak + 1 (omega_blackwell_codegen.h) */
96static bool register_file_fits(uint32_t warps, uint32_t peak) {
97 uint32_t decl = gpr_declared(peak);
98 if (omega_api_regfit(warps, decl)) return true;
99 char e[160]; omega_gpu_fmt(e, sizeof e, "fp8 gemm kernel: %u warps x %u declared registers exceed the 65536-register file", warps, decl);
100 omega_gpu_session_set_error(e);
101 return false;
102}
103int omega_gpu_fp8gemm_kernel_fits(const OmegaFp8GemmSpec *spec, uint32_t *declared) {
104 uint32_t peak = 0;
105 if (!spec || omega_fp8gemm_prog_stats(spec, NULL, &peak) != 0) { omega_gpu_session_set_error("fp8 gemm codegen refused the shape"); return OMEGA_GPU_FP8GEMM_CODEGEN_FAIL; }
106 if (declared) *declared = gpr_declared(peak);
107 return register_file_fits(spec->warps, peak) ? OMEGA_GPU_FP8GEMM_OK : OMEGA_GPU_FP8GEMM_TOO_LARGE;
108}
109
110void omega_gpu_fp8gemm_shape_for(uint32_t k, uint32_t *mt, uint32_t *warps, uint32_t *lane_bytes) {
111 if (lane_bytes) *lane_bytes = omega_api_lane(k);
112 omega_api_fp8_defaults(g.mt, g.warps, mt, warps);
113}
114/* dense = a few distinct experts with many rows each (qkv, o): 8 rows per pass measured best there */
115static void spec_for(uint32_t k, uint32_t segs, OmegaFp8GemmSpec *spec) {
116#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
117 memset(spec, 0, sizeof *spec);
118 spec->k = k;
119 spec->mt = omega_api_fp8mt(g.mt, segs);
120 omega_api_fp8_defaults(0, g.warps, NULL, &spec->warps);
121 spec->lane_bytes = omega_api_lane(k);
122 spec->mutant = g.mutant;
123#else
124 const uint32_t cfg[8] = {k, segs, g.mt, g.warps, (uint32_t)g.mutant};
125 uint64_t result = 0;
126 ar_check(omega_argspec(&result, cfg, 8, spec, 6));
127#endif
128}
129
130static int g_rec; /* 1 while a record call runs (lock held): kernel_for must not evict */
131static int kernel_for(const OmegaFp8GemmSpec *spec, Slot **out, bool *hit) {
132 int fresh = omega_native_cache_refresh(&g.native, CACHE_SLOTS);
133 if (fresh < 0) return OMEGA_GPU_FP8GEMM_CODEGEN_FAIL;
134 if (fresh) memset(g.cache, 0, sizeof g.cache); /* prior device handles are dead */
135#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
136 for (unsigned i = 0; i < CACHE_SLOTS; i++) {
137 Slot *s = &g.cache[i];
138 if (s->valid && !s->exact && memcmp(&s->spec, spec, sizeof *spec) == 0) { *out = s; *hit = true; return OMEGA_GPU_FP8GEMM_OK; }
139 }
140#else
141 const uint64_t query[8] = {CACHE_SLOTS, sizeof(Slot), offsetof(Slot, valid), offsetof(Slot, spec), sizeof *spec, offsetof(Slot, exact) + 1};
142 uint64_t found = UINT64_MAX;
143 ar_check(omega_arcache(&found, query, 8, g.cache, sizeof g.cache, spec, sizeof *spec));
144 if (found != UINT64_MAX) { *out = &g.cache[found]; *hit = true; return OMEGA_GPU_FP8GEMM_OK; }
145#endif
146 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog);
147 if (!prog) { omega_gpu_session_set_error("fp8 gemm codegen: host memory"); return OMEGA_GPU_FP8GEMM_CODEGEN_FAIL; }
148 int cg = omega_blackwell_codegen_fp8gemm_prog(spec, prog);
149 size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0;
150 if (cg != 0 || need == 0 || !register_file_fits(spec->warps, prog->regalloc.peak_gpr_usage)) {
151 omega_gpu_host_free(prog); return OMEGA_GPU_FP8GEMM_CODEGEN_FAIL;
152 }
153 uint8_t key[32]; uint64_t request[16];
154 int index = omega_native_shape_key(spec, sizeof *spec, key) == 0 ?
155 omega_native_cache_reserve(&g.native, key, omega_gpu_code_alloc_bytes(need), request) : -1;
156 if (index < 0) { omega_gpu_host_free(prog); return OMEGA_GPU_FP8GEMM_TOO_LARGE; }
157 Slot *s = &g.cache[index];
158 if (g_rec && s->valid) {
159 (void)omega_native_cache_finish(&g.native, (unsigned)index, request, 0);
160 omega_gpu_host_free(prog); return OMEGA_GPU_FP8GEMM_TOO_LARGE;
161 }
162 OmegaGpuMem code = {0}; OmegaBlackwellKernel kernel;
163 if (omega_gpu_session_alloc_code(need, &code) != 0) cg = -1;
164 else cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel);
165 omega_gpu_host_free(prog);
166 if (cg != 0 || !kernel.code || kernel.code_size != need ||
167 omega_gpu_session_sync_to_gpu(&code) != 0 ||
168 omega_native_cache_finish(&g.native, (unsigned)index, request, 1) != 0) {
169 omega_gpu_session_free(&code);
170 (void)omega_native_cache_finish(&g.native, (unsigned)index, request, 0);
171 return OMEGA_GPU_FP8GEMM_CHIP_FAIL;
172 }
173 if (s->valid) omega_gpu_session_free(&s->code);
174 memset(s, 0, sizeof *s);
175 s->valid = true; s->spec = *spec; s->kernel = kernel; s->code = code;
176 *out = s; *hit = false;
177 return OMEGA_GPU_FP8GEMM_OK;
178}
179
180OmegaGpuNativeCache *omega_gpu_fp8gemm_native_cache(void) { return &g.native; }
181
182int omega_gpu_fp8gemm_prepare_native(const OmegaGpuNativeArtifact *a, OmegaGpuNativeLease *lease) {
183 uint8_t key[32]; uint64_t req[16], tok[16] = {0};
184 if (!lease || omega_native_admitted(a, key) != 0) return -1;
185 if (!dev_open_locked()) return -1;
186 int fresh = omega_native_cache_refresh(&g.native, CACHE_SLOTS);
187 if (fresh < 0) return -1;
188 if (fresh) memset(g.cache, 0, sizeof g.cache);
189 omega_native_context(req); req[2] = omega_gpu_code_alloc_bytes((size_t)a->sizes[1]);
190 memcpy(req + 3, key, 32);
191 int64_t found = omega_native_cache_call(&g.native, 12, req, tok);
192 if (found < 0) return -1;
193 int index = found ? (int)(found - 1) : omega_native_cache_reserve(&g.native, key, (size_t)req[2], req);
194 if (index < 0) return -1;
195 Slot *s = &g.cache[index];
196 if (found) {
197 if (!s->valid || !s->exact || memcmp(s->manifest, a->manifest, 32)) return -1;
198 } else {
199 OmegaGpuMem code = {0};
200 if (omega_gpu_session_alloc_code((size_t)a->sizes[1], &code) != 0) goto abort;
201 memcpy(code.cpu, a->code, (size_t)a->sizes[1]);
202 if (omega_gpu_session_sync_to_gpu(&code) != 0 ||
203 omega_native_cache_finish(&g.native, (unsigned)index, req, 1) != 0) {
204 omega_gpu_session_free(&code); goto abort;
205 }
206 if (s->valid) omega_gpu_session_free(&s->code);
207 memset(s, 0, sizeof *s);
208 s->valid = s->exact = true; s->code = code;
209 s->kernel.code = code.cpu; s->kernel.code_size = (size_t)a->sizes[1];
210 memcpy(s->manifest, a->manifest, 32);
211 }
212 *lease = omega_native_cache_lease(&g.native, (unsigned)index);
213 return omega_native_lease_op(lease, 5, 1);
214abort:
215 (void)omega_native_cache_finish(&g.native, (unsigned)index, req, 0);
216 return -1;
217}
218
219int omega_gpu_fp8gemm_prepare(uint32_t k) {
220 /* both variants: many experts (4 rows per pass) and dense (8 rows per pass) */
221 OmegaFp8GemmSpec sp[2]; spec_for(k, 64u, &sp[0]); spec_for(k, 1u, &sp[1]);
222 omega_gpu_session_lock();
223 int rc = OMEGA_GPU_FP8GEMM_OK;
224 if (!dev_open_locked()) rc = OMEGA_GPU_FP8GEMM_CHIP_FAIL;
225 else for (int v = 0; v < 2 && rc == OMEGA_GPU_FP8GEMM_OK; v++) {
226 Slot *s = NULL; bool hit = false; rc = kernel_for(&sp[v], &s, &hit);
227 if (rc == OMEGA_GPU_FP8GEMM_OK && !s->pinned) {
228 OmegaGpuNativeLease lease = omega_native_cache_lease(&g.native, (unsigned)(s - g.cache));
229 if (omega_native_lease_op(&lease, 5, 1) != 0) rc = OMEGA_GPU_FP8GEMM_TOO_LARGE;
230 else s->pinned = true;
231 }
232 }
233 omega_gpu_session_unlock();
234 return rc;
235}
236
237/* ---- the stroke's record path (L312 v2): one launch appended to a command list, every buffer the caller's ---- */
238static void spec_for_stroke(uint32_t k, uint32_t segs, uint32_t mt, int rowmap, OmegaFp8GemmSpec *spec) {
239#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
240 spec_for(k, segs, spec);
241 if (mt) spec->mt = mt;
242 spec->rowmap = rowmap ? 1u : 0u;
243#else
244 const uint32_t cfg[8] = {k, segs, g.mt, g.warps, (uint32_t)g.mutant, mt, (uint32_t)rowmap};
245 uint64_t result = 0;
246 ar_check(omega_argspec(&result, cfg, 8, spec, 6));
247#endif
248}
249int omega_gpu_fp8gemm_prepare_shape(uint32_t k, uint32_t segs, uint32_t mt, int rowmap) {
250 OmegaFp8GemmSpec spec; spec_for_stroke(k, segs, mt, rowmap, &spec);
251 if (omega_fp8gemm_shared_bytes(&spec) == 0) { omega_gpu_session_set_error("fp8 gemm prepare: shape refused (rowmap needs mt <= 4)"); return OMEGA_GPU_FP8GEMM_BAD_ARGS; }
252 omega_gpu_session_lock();
253 int rc = OMEGA_GPU_FP8GEMM_OK;
254 if (!dev_open_locked()) rc = OMEGA_GPU_FP8GEMM_CHIP_FAIL;
255 else {
256 Slot *s = NULL; bool hit = false; rc = kernel_for(&spec, &s, &hit);
257 if (rc == OMEGA_GPU_FP8GEMM_OK && !s->pinned) {
258 OmegaGpuNativeLease lease = omega_native_cache_lease(&g.native, (unsigned)(s - g.cache));
259 if (omega_native_lease_op(&lease, 5, 1) != 0) rc = OMEGA_GPU_FP8GEMM_TOO_LARGE;
260 else s->pinned = true;
261 }
262 }
263 omega_gpu_session_unlock();
264 return rc;
265}
266uint32_t omega_gpu_fp8gemm_cta_budget_for(uint32_t segs) { return omega_api_ctabud(segs, g.ctas); }
267
268int omega_gpu_fp8gemm_record(OmegaGpuCmdList *cl, const OmegaGpuFp8Tensor *t, uint32_t rows, uint32_t segs, uint64_t seg_va, uint64_t xp_va,
269 uint64_t y_va, uint64_t xmap_va, uint64_t ymap_va, uint32_t mt, uint32_t cta_budget, OmegaGpuFp8GemmInfo *info) {
270 if (info) ar_words(info, sizeof *info / sizeof(uint32_t), NULL, 0);
271 uint32_t experts = 0, n = 0, k = 0;
272 omega_gpu_fp8_tensor_shape(t, &experts, &n, &k);
273 if (!cl || !t || !experts || !seg_va || !xp_va || !y_va || rows == 0 || segs == 0 || (!xmap_va) != (!ymap_va)) return OMEGA_GPU_FP8GEMM_BAD_ARGS;
274 if (rows > OMEGA_GPU_FP8GEMM_MAX_ROWS || segs > OMEGA_GPU_FP8GEMM_MAX_SEGMENTS) return OMEGA_GPU_FP8GEMM_TOO_LARGE;
275 OmegaFp8GemmSpec spec; spec_for_stroke(k, segs, mt, xmap_va != 0, &spec);
276 if (omega_fp8gemm_shared_bytes(&spec) == 0) { omega_gpu_session_set_error("fp8 gemm record: shape refused (rowmap needs mt <= 4)"); return OMEGA_GPU_FP8GEMM_BAD_ARGS; }
277 const uint32_t W = spec.warps;
278 const uint32_t budget = cta_budget ? cta_budget : omega_gpu_fp8gemm_cta_budget_for(segs);
279 uint64_t plan[32] = {8, rows, n, k, spec.mt, W, budget};
280 (void)ar_plan(plan);
281 const uint32_t grid_x = (uint32_t)plan[16];
282 /* caller holds the session lock */
283 if (!dev_open_locked()) return OMEGA_GPU_FP8GEMM_CHIP_FAIL;
284 Slot *ks = NULL; bool hit = false;
285 g_rec = 1;
286 int rc = kernel_for(&spec, &ks, &hit);
287 g_rec = 0;
288 if (rc != OMEGA_GPU_FP8GEMM_OK) return rc;
289#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
290 uint32_t args[OMEGA_GEMM_ARGS_WORDS_MAP]; memset(args, 0, sizeof args);
291 args[0] = (uint32_t)seg_va; args[1] = (uint32_t)(seg_va >> 32);
292 args[2] = (uint32_t)xp_va; args[3] = (uint32_t)(xp_va >> 32);
293 args[4] = (uint32_t)y_va; args[5] = (uint32_t)(y_va >> 32);
294 args[6] = n; args[7] = grid_x * W;
295 args[8] = (uint32_t)xmap_va; args[9] = (uint32_t)(xmap_va >> 32);
296 args[10] = (uint32_t)ymap_va; args[11] = (uint32_t)(ymap_va >> 32);
297#else
298 uint32_t args[OMEGA_GEMM_ARGS_WORDS_MAP];
299 const uint64_t va[5] = {seg_va, xp_va, y_va, xmap_va, ymap_va};
300 const uint32_t scalar[2] = {n, (uint32_t)plan[17]};
301 write_args(args, OMEGA_GEMM_ARGS_WORDS_MAP, va, scalar);
302#endif
303 if (info) {
304#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
305 info->rows = rows; info->segments = segs; info->n = n; info->k = k; info->mt = spec.mt; info->warps_per_cta = W; info->lane_bytes = spec.lane_bytes;
306 info->grid_x = grid_x; info->grid_y = segs; info->kernel_cache_hit = hit;
307#else
308 const uint64_t cfg[16] = {rows, segs, n, k, spec.mt, W, spec.lane_bytes, grid_x, segs, 0, 0, 0, hit};
309 write_info(info, cfg);
310#endif
311 }
312#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
313 OmegaGpuLaunch L = {
314 .code_va = ks->code.va, .gpr_count = gpr_declared(ks->kernel.gpr_count),
315 .threads_x = 32u * W, .threads_y = 1, .grid_x = grid_x, .grid_y = segs,
316 .num_elements = (size_t)rows * n,
317 .args = args, .n_args = spec.rowmap ? OMEGA_GEMM_ARGS_WORDS_MAP : OMEGA_GEMM_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS,
318 .shared_bytes = omega_fp8gemm_shared_bytes(&spec),
319 };
320#else
321 OmegaGpuLaunch L;
322 const uint64_t launch[16] = {ks->code.va, gpr_declared(ks->kernel.gpr_count), plan[18], 1,
323 grid_x, segs, plan[19], (uintptr_t)args, spec.rowmap ? OMEGA_GEMM_ARGS_WORDS_MAP : OMEGA_GEMM_ARGS_WORDS,
324 MARKER_TIMEOUT_MS, omega_fp8gemm_shared_bytes(&spec)};
325 ar_launch(&L, launch);
326#endif
327 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"); return OMEGA_GPU_FP8GEMM_CHIP_FAIL; }
328 OmegaGpuNativeLease lease = omega_native_cache_lease(&g.native, (unsigned)(ks - g.cache));
329 if (omega_gpu_cmdlist_add_native(cl, &L, &lease) < 0) return OMEGA_GPU_FP8GEMM_CHIP_FAIL;
330 return OMEGA_GPU_FP8GEMM_OK;
331}
332
333/* x' = x * 2^64 in the kernel's lane layout: column c = s*32*LB + L*LB + w*4 + e -> index s*32*LB + (w*32 + L)*4 + e */
334void omega_fp8gemm_permute_x(const float *x, uint32_t k, uint32_t lb, float *out) {
335#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
336 const uint32_t row = 32u * lb, nw = lb / 4u;
337 for (uint32_t s0 = 0; s0 + row <= k; s0 += row)
338 for (uint32_t L = 0; L < 32u; L++)
339 for (uint32_t w = 0; w < nw; w++) {
340 const float *src = x + s0 + L * lb + w * 4u; float *dst = out + s0 + (w * 32u + L) * 4u;
341 dst[0] = src[0] * 18446744073709551616.0f; dst[1] = src[1] * 18446744073709551616.0f;
342 dst[2] = src[2] * 18446744073709551616.0f; dst[3] = src[3] * 18446744073709551616.0f;
343 }
344#else
345 uint64_t result = 0;
346 ar_check(omega_arperm(&result, lb, x, k, out, k));
347 if (result) abort();
348#endif
349}
350
351typedef struct { uint64_t w_va, s_va; uint32_t row0, cnt; uint32_t pad[2]; } SegEntry;
352_Static_assert(sizeof(SegEntry) == OMEGA_GEMM_SEG_BYTES, "segment entry layout");
353
354/* General fallback (GemmGeneral, 2026-10-27): a call the kernel refuses (rows past OMEGA_GPU_FP8GEMM_MAX_ROWS, segments
355 * past OMEGA_GPU_FP8GEMM_MAX_SEGMENTS, a k the codegen refuses) runs each segment through omega_gpu_fp8_general_rows:
356 * the segment's x rows gathered by token_rows into a host buffer, y rows row0..row0+cnt written directly. */
357static int general_gemm(const OmegaGpuFp8Tensor *t, uint32_t M, const uint32_t *ids, const uint32_t *trow, const float *x32,
358 const uint16_t *x16, float *y, uint32_t n, uint32_t k, uint32_t x_rows, uint32_t segs, OmegaGpuFp8GemmInfo *info, int refused, uint64_t t0) {
359#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
360 (void)x_rows;
361#endif
362 int rc = OMEGA_GPU_FP8GEMM_OK;
363 for (uint32_t i = 0; i < M && rc == OMEGA_GPU_FP8GEMM_OK;) {
364#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
365 uint32_t j = i; while (j < M && ids[j] == ids[i]) j++;
366 const uint32_t cnt = j - i;
367 void *xs = omega_gpu_host_calloc((size_t)cnt * k, x32 ? 4u : 2u);
368 if (!xs) return OMEGA_GPU_FP8GEMM_BAD_ARGS;
369 for (uint32_t r = 0; r < cnt; r++) {
370 if (x32) memcpy((float *)xs + (size_t)r * k, x32 + (size_t)trow[i + r] * k, (size_t)k * 4u);
371 else memcpy((uint16_t *)xs + (size_t)r * k, x16 + (size_t)trow[i + r] * k, (size_t)k * 2u);
372 }
373#else
374 uint64_t plan[32] = {8, M, n, k, 1, 1, UINT32_MAX, x_rows, i}, end = 0;
375 ar_check(omega_arrun(&end, i, ids, M));
376 plan[9] = end;
377 (void)ar_plan(plan);
378 const uint32_t j = (uint32_t)end, cnt = (uint32_t)plan[28];
379 void *xs = omega_gpu_host_calloc(plan[29], x32 ? sizeof(float) : sizeof(uint16_t));
380 if (!xs) return OMEGA_GPU_FP8GEMM_BAD_ARGS;
381 uint64_t gathered = 0;
382 if (x32) ar_check(omega_argath32(&gathered, trow + i, cnt, x32, plan[23], xs, plan[29]));
383 else ar_check(omega_argath16(&gathered, trow + i, cnt, x16, plan[23], xs, plan[29]));
384 if (gathered) abort();
385#endif
386 rc = omega_gpu_fp8_general_rows(t, ids[i], x32 ? xs : NULL, x32 ? NULL : xs, cnt,
387#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
388 y + (size_t)i * n,
389#else
390 y + plan[30],
391#endif
392 refused);
393 omega_gpu_host_free(xs);
394 i = j;
395 }
396 if (info) {
397 const uint64_t cfg[16] = {M, segs, n, k, 0, 0, 0, 0, 0, 0, 0, 0, 0, rc == OMEGA_GPU_FP8GEMM_OK};
398 write_info(info, cfg);
399 info->call_ns = omega_gpu_session_now_ns() - t0;
400 }
401 return rc;
402}
403
404static int gemm_core(const OmegaGpuFp8Tensor *t, uint32_t M, const uint32_t *ids, const uint32_t *trow, const float *x32,
405 const uint16_t *x16, uint32_t x_rows, float *y, OmegaGpuFp8GemmInfo *info) {
406 uint64_t t0 = omega_gpu_session_now_ns();
407 if (info) ar_words(info, sizeof *info / sizeof(uint32_t), NULL, 0);
408 uint32_t experts = 0, n = 0, k = 0;
409 omega_gpu_fp8_tensor_shape(t, &experts, &n, &k);
410 if (!t || !experts || !ids || !trow || (!x32 && !x16) || !y || M == 0 || x_rows == 0) return OMEGA_GPU_FP8GEMM_BAD_ARGS;
411 /* a call with a test override of the kernel shape or a kernel mutant keeps the refusal: those calls test the kernel */
412 const int general_ok = !g.mt && !g.warps && !g.ctas && !g.mutant;
413 if (M > OMEGA_GPU_FP8GEMM_MAX_ROWS && !general_ok) return OMEGA_GPU_FP8GEMM_TOO_LARGE;
414 uint32_t segs = 0;
415#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
416 for (uint32_t i = 0; i < M; i++) {
417 if (ids[i] >= experts || trow[i] >= x_rows || (i && ids[i] < ids[i - 1])) return OMEGA_GPU_FP8GEMM_BAD_ARGS;
418 if (i == 0 || ids[i] != ids[i - 1]) segs++;
419 }
420#else
421 const uint64_t group_cfg[4] = {experts, x_rows, 1};
422 uint64_t grouped = 0;
423 ar_check(omega_argroups(&grouped, group_cfg, 4, ids, M, trow, M));
424 if (grouped == UINT64_MAX) return OMEGA_GPU_FP8GEMM_BAD_ARGS;
425 segs = (uint32_t)grouped;
426#endif
427 if (M > OMEGA_GPU_FP8GEMM_MAX_ROWS || segs > OMEGA_GPU_FP8GEMM_MAX_SEGMENTS)
428 return general_ok ? general_gemm(t, M, ids, trow, x32, x16, y, n, k, x_rows, segs, info, OMEGA_GPU_FP8GEMM_TOO_LARGE, t0) : OMEGA_GPU_FP8GEMM_TOO_LARGE;
429 OmegaFp8GemmSpec spec; spec_for(k, segs, &spec);
430 if (general_ok && omega_fp8gemm_shared_bytes(&spec) == 0) return general_gemm(t, M, ids, trow, x32, x16, y, n, k, x_rows, segs, info, OMEGA_GPU_FP8GEMM_CODEGEN_FAIL, t0);
431 const uint32_t MT = spec.mt, W = spec.warps;
432 const uint32_t budget = omega_api_ctabud(segs, g.ctas);
433 uint64_t plan[32] = {8, M, n, k, MT, W, budget, x_rows};
434 (void)ar_plan(plan);
435 const uint32_t grid_x = (uint32_t)plan[16];
436
437 omega_gpu_session_lock();
438 int rc = OMEGA_GPU_FP8GEMM_OK;
439 if (!dev_open_locked()) { omega_gpu_session_unlock(); return OMEGA_GPU_FP8GEMM_CHIP_FAIL; }
440 int src = omega_gpu_session_scratch(&g.x_buf, plan[21]);
441 if (src == 0) src = omega_gpu_session_scratch(&g.y_buf, plan[22]);
442 if (src == 0) src = omega_gpu_session_scratch(&g.seg_buf, (size_t)OMEGA_GPU_FP8GEMM_MAX_SEGMENTS * OMEGA_GEMM_SEG_BYTES);
443 if (src != 0) { omega_gpu_session_unlock(); return src == OMEGA_GPU_SESSION_SCRATCH_RESERVED ? OMEGA_GPU_FP8GEMM_TOO_LARGE : OMEGA_GPU_FP8GEMM_CHIP_FAIL; }
444 Slot *ks = NULL; bool hit = false;
445 rc = kernel_for(&spec, &ks, &hit);
446 if (rc != OMEGA_GPU_FP8GEMM_OK) { omega_gpu_session_unlock(); return rc; }
447
448 /* segment table, permuted x' rows, poisoned y */
449 SegEntry *tab = g.seg_buf.mem.cpu;
450 uint64_t streamed = 0, distinct = 0; uint32_t sg = 0, passes_max = 0;
451#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
452 for (uint32_t i = 0; i < M;) {
453 uint32_t j = i; while (j < M && ids[j] == ids[i]) j++;
454 memset(&tab[sg], 0, sizeof tab[sg]);
455 (void)omega_gpu_fp8_tensor_entry(t, ids[i], &tab[sg].w_va, &tab[sg].s_va);
456 tab[sg].row0 = i; tab[sg].cnt = j - i;
457 uint32_t passes = (j - i + MT - 1) / MT; if (passes > passes_max) passes_max = passes;
458 streamed += (uint64_t)passes * n * k; distinct += (uint64_t)n * k;
459 sg++; i = j;
460 }
461#else
462 uint64_t segment[16] = {0, 0, MT, n, k};
463 while (segment[0] < M) {
464 uint64_t result = 0;
465 ar_check(omega_arrun(&segment[1], segment[0], ids, M));
466 (void)omega_gpu_fp8_tensor_entry(t, ids[segment[0]], &segment[8], &segment[9]);
467 ar_check(omega_argseg(&result, segment, 16, tab, (uint64_t)OMEGA_GPU_FP8GEMM_MAX_SEGMENTS * 4));
468 if (result) abort();
469 }
470 streamed = segment[6]; distinct = segment[7]; sg = (uint32_t)segment[10]; passes_max = (uint32_t)segment[5];
471#endif
472 float *xd = g.x_buf.mem.cpu, *yd = g.y_buf.mem.cpu;
473 {
474#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
475 float tmp[OMEGA_FP8_MAX_K], tp[OMEGA_FP8_MAX_K]; /* permute in cached memory, then one sequential copy: element-wise writes into the uncached buffer cost 2.8 ms per 4 MB */
476 /* a source row routed to several experts (top-k) is permuted once and copied after: the permuted rows are kept in cached memory */
477 float *pc = ((size_t)x_rows * k * 4u <= (16u << 20)) ? malloc((size_t)x_rows * k * 4u) : NULL;
478 unsigned char *have = pc ? calloc(x_rows, 1) : NULL;
479 for (uint32_t i = 0; i < M; i++) {
480 const uint32_t r = trow[i];
481 if (have && have[r]) { memcpy(xd + (size_t)i * k, pc + (size_t)r * k, (size_t)k * 4u); continue; }
482 if (x32) memcpy(tmp, x32 + (size_t)r * k, (size_t)k * 4u);
483 else for (uint32_t c = 0; c < k; c++) { uint32_t b = (uint32_t)x16[(size_t)r * k + c] << 16; memcpy(&tmp[c], &b, 4); }
484 float *dst = have ? pc + (size_t)r * k : tp;
485 omega_fp8gemm_permute_x(tmp, k, spec.lane_bytes, dst);
486 if (have) have[r] = 1;
487 memcpy(xd + (size_t)i * k, dst, (size_t)k * 4u);
488 }
489 free(pc); free(have);
490 memset(xd + (size_t)M * k, 0, (size_t)MT * k * 4u);
491#else
492 float tmp[OMEGA_FP8_MAX_K], tp[OMEGA_FP8_MAX_K];
493 float *pc = plan[24] <= (16u << 20) ? malloc(plan[24]) : NULL;
494 unsigned char *have = pc ? calloc(x_rows, 1) : NULL;
495 uint64_t cfg[16] = {0, k, have != NULL};
496 for (;;) {
497 uint64_t more = 0;
498 ar_check(omega_arxnext(&more, cfg, 16, trow, M, have, have ? x_rows : 0));
499 if (!more) break;
500 float *dst = have ? pc + cfg[8] : tp;
501 if (cfg[7]) {
502 uint64_t result = 0;
503 const float *row = x32 ? x32 + cfg[8] : tmp;
504 if (!x32) {
505 ar_check(omega_arwiden(&result, x16 + cfg[8], cfg[10], tmp, cfg[10]));
506 if (result) abort();
507 }
508 ar_check(omega_arperm(&result, spec.lane_bytes, row, cfg[10], dst, cfg[10]));
509 if (result) abort();
510 }
511 ar_copy(xd + cfg[9], dst, cfg[11]);
512 }
513 free(pc); free(have);
514 ar_fill((uint32_t *)(xd + plan[26]), plan[27], 0);
515#endif
516 }
517#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
518 for (size_t i = 0; i < (size_t)M * n; i++) yd[i] = POISON_F32;
519#else
520 uint32_t poison; memcpy(&poison, &(float){POISON_F32}, sizeof poison);
521 ar_fill((uint32_t *)yd, plan[19], poison);
522#endif
523 if (omega_gpu_session_sync_to_gpu(&g.seg_buf.mem) != 0 || 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_FP8GEMM_CHIP_FAIL;
524
525 uint32_t args[OMEGA_GEMM_ARGS_WORDS];
526#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
527 args[0] = (uint32_t)g.seg_buf.mem.va; args[1] = (uint32_t)(g.seg_buf.mem.va >> 32);
528 args[2] = (uint32_t)g.x_buf.mem.va; args[3] = (uint32_t)(g.x_buf.mem.va >> 32);
529 args[4] = (uint32_t)g.y_buf.mem.va; args[5] = (uint32_t)(g.y_buf.mem.va >> 32);
530 args[6] = n; args[7] = grid_x * W;
531#else
532 const uint64_t va[5] = {g.seg_buf.mem.va, g.x_buf.mem.va, g.y_buf.mem.va};
533 const uint32_t scalar[2] = {n, (uint32_t)plan[17]};
534 write_args(args, OMEGA_GEMM_ARGS_WORDS, va, scalar);
535#endif
536 if (info) {
537#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
538 info->rows = M; info->segments = sg; info->n = n; info->k = k; info->mt = MT; info->warps_per_cta = W; info->lane_bytes = spec.lane_bytes;
539 info->grid_x = grid_x; info->grid_y = sg; info->passes_max = passes_max;
540 info->weight_bytes = distinct; info->streamed_bytes = streamed; info->kernel_cache_hit = hit;
541#else
542 const uint64_t cfg[16] = {M, sg, n, k, MT, W, spec.lane_bytes, grid_x, sg, passes_max, distinct, streamed, hit};
543 write_info(info, cfg);
544#endif
545 }
546 if (rc == OMEGA_GPU_FP8GEMM_OK) {
547#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
548 OmegaGpuLaunch L = {
549 .code_va = ks->code.va, .gpr_count = gpr_declared(ks->kernel.gpr_count),
550 .threads_x = 32u * W, .threads_y = 1, .grid_x = grid_x, .grid_y = sg,
551 .num_elements = (size_t)M * n,
552 .args = args, .n_args = OMEGA_GEMM_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS,
553 .shared_bytes = omega_fp8gemm_shared_bytes(&spec),
554 };
555#else
556 OmegaGpuLaunch L;
557 const uint64_t launch[16] = {ks->code.va, gpr_declared(ks->kernel.gpr_count), plan[18], 1,
558 grid_x, sg, plan[19], (uintptr_t)args, OMEGA_GEMM_ARGS_WORDS, MARKER_TIMEOUT_MS, omega_fp8gemm_shared_bytes(&spec)};
559 ar_launch(&L, launch);
560#endif
561 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_FP8GEMM_CHIP_FAIL; }
562 else if (omega_gpu_session_launch(&L, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL) != 0) rc = OMEGA_GPU_FP8GEMM_CHIP_FAIL;
563 }
564 if (rc == OMEGA_GPU_FP8GEMM_OK && omega_gpu_session_sync_to_cpu(&g.y_buf.mem) != 0) rc = OMEGA_GPU_FP8GEMM_CHIP_FAIL;
565 if (rc == OMEGA_GPU_FP8GEMM_OK) {
566 size_t unchanged = 0;
567#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
568 for (size_t i = 0; i < (size_t)M * n; i++) if (yd[i] == POISON_F32) unchanged++;
569#else
570 unchanged = ar_count((const uint32_t *)yd, plan[19], poison);
571#endif
572 if (unchanged) { omega_gpu_session_set_error("output poison survived"); rc = OMEGA_GPU_FP8GEMM_CHIP_FAIL; }
573 else ar_copy(y, yd, plan[22]);
574 }
575 omega_gpu_session_unlock();
576 if (info) info->call_ns = omega_gpu_session_now_ns() - t0;
577 return rc;
578}
579
580int omega_gpu_fp8gemm_f32(const OmegaGpuFp8Tensor *t, uint32_t M, const uint32_t *ids, const uint32_t *trow, const float *x, uint32_t x_rows, float *y, OmegaGpuFp8GemmInfo *info) {
581 return gemm_core(t, M, ids, trow, x, NULL, x_rows, y, info);
582}
583int omega_gpu_fp8gemm_bf16(const OmegaGpuFp8Tensor *t, uint32_t M, const uint32_t *ids, const uint32_t *trow, const uint16_t *x, uint32_t x_rows, float *y, OmegaGpuFp8GemmInfo *info) {
584 return gemm_core(t, M, ids, trow, NULL, x, x_rows, y, info);
585}
586
587int omega_gpu_fp8gemm_native_code_observe(const OmegaGpuNativeLease *lease, OmegaGpuMem *out) {
588 if (!out) return -1;
589 *out = (OmegaGpuMem){0};
590 if (!lease || lease->cache != &g.native || omega_native_lease_op(lease, 16, 0) != 0)
591 return -1;
592 const Slot *s = &g.cache[lease->slot];
593 if (!s->exact) return -1;
594 *out = s->code;
595 return 0;
596}
597
598int omega_gpu_fp8gemm_record_native(OmegaGpuCmdList *cl, const OmegaGpuFp8Tensor *t,
599 const OmegaGpuNativeArtifact *a, const OmegaGpuNativeLease *lease,
600 uint32_t rows, uint32_t segs, uint64_t table, uint64_t xp, uint64_t y,
601 uint64_t xmap, uint64_t ymap) {
602 OmegaGpuNativeTensorObservation owner;
603 if (omega_gpu_fp8_native_observe(t, &owner) != 0) return -1;
604 const uint32_t n = owner.n, k = owner.k;
605 if (!cl || !t || !rows || !segs || !table || !xp || !y || (!!xmap != !!ymap) ||
606 !lease || lease->cache != &g.native || omega_native_bound(a, lease) != 0) return -1;
607 uint64_t call[12] = {2, rows, n, k, segs, xmap ? 12u : 8u, xmap ? 1u : 0u, 0,
608 owner.allocation_epoch, omega_gpu_session_epoch(), owner.owner_generation, 0}, launch[32];
609 if (omega_native_launch(a, call, launch) != 0) return -1;
610 Slot *s = &g.cache[lease->slot];
611 if (!s->exact || memcmp(s->manifest, a->manifest, 32)) return -1;
612#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
613 uint32_t args[12] = {(uint32_t)table, (uint32_t)(table >> 32),
614 (uint32_t)xp, (uint32_t)(xp >> 32), (uint32_t)y, (uint32_t)(y >> 32),
615 n, (uint32_t)launch[29], (uint32_t)xmap, (uint32_t)(xmap >> 32),
616 (uint32_t)ymap, (uint32_t)(ymap >> 32)};
617 OmegaGpuLaunch L = {.code_va = s->code.va, .gpr_count = (uint32_t)launch[8],
618 .threads_x = (uint32_t)launch[4], .threads_y = (uint32_t)launch[5],
619 .grid_x = (uint32_t)launch[1], .grid_y = (uint32_t)launch[2],
620 .num_elements = (size_t)rows * n, .args = args, .n_args = (uint32_t)call[5],
621 .timeout_ms = MARKER_TIMEOUT_MS, .shared_bytes = (uint32_t)launch[7]};
622#else
623 uint32_t args[12];
624 const uint64_t va[5] = {table, xp, y, xmap, ymap};
625 const uint32_t scalar[2] = {n, (uint32_t)launch[29]};
626 write_args(args, 12, va, scalar);
627 uint64_t plan[32] = {8, rows, n, k, 1, 1, UINT32_MAX};
628 (void)ar_plan(plan);
629 OmegaGpuLaunch L;
630 const uint64_t cfg[16] = {s->code.va, launch[8], launch[4], launch[5], launch[1], launch[2],
631 plan[19], (uintptr_t)args, call[5], MARKER_TIMEOUT_MS, launch[7]};
632 ar_launch(&L, cfg);
633#endif
634 return omega_gpu_cmdlist_add_native(cl, &L, lease);
635}