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

351 lines · 18850 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */
2/* omega_gpu_pfgemm.c: device-resident tensor-core FP8 grouped GEMM v2 (OWN-30B T3b). See omega_gpu_prefill.h. */
3#include "omega_gpu_prefill.h"
4#include "omega_blackwell_codegen_tc2.h"
5#include "omega_gpu_fp8_api.h"
6#include "omega_api_shells.h"
7#include "omega_api_views_rest.h"
8#include "omega_gpu_cmdlist.h"
9#include "omega_gpu_fmt.h"
10#include "cudapp_native.h"
11#include <stdlib.h>
12#include <string.h>
13
14#define KIND_TC2 2u
15static struct { uint32_t rt, warps, aqn; int mutant; } g;
16void omega_gpu_pf_gemm_set_shape(uint32_t rt, uint32_t warps, uint32_t aqn) { g.rt = rt; g.warps = warps; g.aqn = aqn; }
17void omega_gpu_pf_gemm_test_set_mutant(int m) { g.mutant = m; }
18static uint32_t RT(void) { return (uint32_t)(omega_api_pfbm(g.rt) >> 4); }
19uint32_t omega_gpu_pf_gemm_bm(void) { return omega_api_pfbm(g.rt); }
20
21#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
22static uint32_t perm4(uint32_t q) { return omega_api_perm4(q); }
23#endif
24
25int omega_gpu_pf_weights_host_form(uint32_t E, uint32_t n, uint32_t k, const uint8_t *w, const uint16_t *sinv, uint32_t layout, uint8_t *pw, float *ratio) {
26 if (!E || omega_api_pfargs(n, k, layout) || !w || !sinv || !pw || !ratio) return OMEGA_GPU_PF_BAD_ARGS;
27#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
28 const uint32_t KB = k / 128u, NB = n / 128u, I = n / 2u;
29 float *sf = malloc((size_t)NB * KB * 4u);
30 if (!sf) return OMEGA_GPU_PF_TOO_LARGE;
31 int rc = OMEGA_GPU_PF_OK;
32 for (uint32_t e = 0; e < E && rc == OMEGA_GPU_PF_OK; e++) {
33 if (omega_fp8_pack_scales(sinv + (size_t)e * NB * KB, (size_t)NB * KB, sf, 0) != 0) { rc = OMEGA_GPU_PF_BAD_ARGS; break; }
34 for (uint32_t p = 0; p < n; p++) {
35 uint32_t src = p;
36 if (layout == OMEGA_GPU_PF_SWIGLU) { uint32_t G = p / 16u, h = p % 16u, i = perm4(8u * G + h % 8u); src = h < 8u ? i : I + i; }
37 memcpy(pw + ((size_t)e * n + p) * k, w + ((size_t)e * n + src) * k, k);
38 if (p % 8u == 0) {
39 /* pair layout: [e][p / 16][kb 0..KB][p / 8 % 2] (the kernel loads both column tiles of a warp with one LDG.64) */
40 float *rt = ratio + ((size_t)e * (n / 16u) + p / 16u) * (KB + 1u) * 2u + (p / 8u) % 2u;
41 const float *s = sf + (size_t)(src / 128u) * KB;
42 rt[0] = 1.0f;
43 for (uint32_t kb = 1; kb < KB; kb++) rt[2u * kb] = s[kb - 1] / s[kb];
44 rt[2u * KB] = s[KB - 1];
45 }
46 }
47 }
48 free(sf);
49 return rc;
50#else
51 uint64_t plan[32] = {3, E, n, k};
52 if (ar_plan(plan)) return OMEGA_GPU_PF_BAD_ARGS;
53 float *sf = malloc((size_t)plan[25]);
54 if (!sf) return OMEGA_GPU_PF_TOO_LARGE;
55 const uint64_t cfg[4] = {n, k, layout};
56 int rc = OMEGA_GPU_PF_OK;
57 /* Shell sequencing preserves the one-expert scratch allocation. Each Omega
58 * entry receives acquired slices; no host-data transform remains here. */
59 for (uint32_t e = 0; e < E && rc == OMEGA_GPU_PF_OK; e++) {
60 plan[4] = e;
61 if (ar_plan(plan)) { rc = OMEGA_GPU_PF_BAD_ARGS; break; }
62 if (omega_fp8_pack_scales(sinv + plan[22], (size_t)plan[16], sf, 0) != 0) {
63 rc = OMEGA_GPU_PF_BAD_ARGS; break;
64 }
65 uint64_t done = 0;
66 ar_check(omega_arpfcopy(&done, cfg, 4, w + plan[23], plan[17], pw + plan[23], plan[17]));
67 ar_check(omega_arpfrat(&done, cfg, 4, sf, plan[16], ratio + plan[26], plan[27]));
68 }
69 free(sf);
70 return rc;
71#endif
72}
73
74int omega_gpu_pf_weights_upload(uint32_t E, uint32_t n, uint32_t k, const uint8_t *w, const uint16_t *sinv, uint32_t layout, OmegaPfWeights *out) {
75 if (!out) return OMEGA_GPU_PF_BAD_ARGS;
76 memset(out, 0, sizeof *out);
77 uint64_t plan[32] = {3, E, n, k};
78 ar_check((uint32_t)ar_plan(plan));
79 const size_t wb = plan[19], rb16 = plan[21];
80 uint8_t *pw = malloc(wb); float *ratio = malloc(plan[28]);
81 int rc = (!pw || !ratio) ? OMEGA_GPU_PF_TOO_LARGE : omega_gpu_pf_weights_host_form(E, n, k, w, sinv, layout, pw, ratio);
82 if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_alloc_dev(wb, &out->w);
83 if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_alloc_dev(rb16, &out->r);
84 if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_upload(&out->w, 0, pw, plan[29]);
85 if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_upload(&out->r, 0, ratio, rb16);
86 free(pw); free(ratio);
87 if (rc != OMEGA_GPU_PF_OK) { omega_gpu_pf_free(&out->w); omega_gpu_pf_free(&out->r); memset(out, 0, sizeof *out); return rc; }
88 out->experts = E; out->n = n; out->k = k; out->layout = layout;
89 return OMEGA_GPU_PF_OK;
90}
91void omega_gpu_pf_weights_free(OmegaPfWeights *w) { if (!w) return; omega_gpu_pf_free(&w->w); omega_gpu_pf_free(&w->r); memset(w, 0, sizeof *w); }
92
93typedef struct { uint64_t w_va, r_va; uint32_t row0, cnt; uint64_t pad; } Tile;
94_Static_assert(sizeof(Tile) == OMEGA_TC2_TILE_BYTES, "tile entry");
95uint32_t omega_gpu_pf_gemm_tiles(const OmegaPfWeights *w, const uint32_t *counts, void *tab, uint32_t max) {
96 if (!w || !counts || !tab) return 0;
97#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
98 const uint32_t bm = omega_gpu_pf_gemm_bm(), KB = w->k / 128u;
99 Tile *t = tab; uint32_t nt = 0, row = 0;
100 for (uint32_t e = 0; e < w->experts; e++) {
101 for (uint32_t r = 0; r < counts[e]; r += bm) {
102 if (nt >= max) return 0;
103 t[nt].w_va = w->w.va + (uint64_t)e * w->n * w->k;
104 t[nt].r_va = w->r.va + (uint64_t)e * (w->n / 8u) * (KB + 1u) * 4u;
105 t[nt].row0 = row + r; t[nt].cnt = counts[e] - r < bm ? counts[e] - r : bm; t[nt].pad = 0; nt++;
106 }
107 row += counts[e];
108 }
109 return nt;
110#else
111 uint64_t plan[32] = {3, w->experts, w->n, w->k};
112 if (ar_plan(plan)) return 0;
113 const uint64_t cfg[8] = {max, w->w.va, w->r.va, plan[17], plan[18], omega_gpu_pf_gemm_bm()};
114 uint64_t nt = 0;
115 ar_check(omega_artiles(&nt, cfg, 8, counts, w->experts, tab, (uint64_t)max * 4));
116 return (uint32_t)nt;
117#endif
118}
119
120static int gen_tc2(const OmegaPfKey *key, void *prog) { OmegaTc2Spec s; memcpy(&s, key->words, sizeof s); return omega_blackwell_codegen_tc2_prog(&s, prog); }
121
122int omega_gpu_pf_gemm(const OmegaPfWeights *w, uint64_t tiles_va, uint32_t ntiles, uint64_t rowidx_va, uint64_t x_va, uint64_t y_va, uint64_t *ns) {
123 if (!w || !w->w.va || !tiles_va || !x_va || !y_va) return OMEGA_GPU_PF_BAD_ARGS;
124 if (ntiles == 0) return OMEGA_GPU_PF_OK;
125 if (ntiles > 65535u) return OMEGA_GPU_PF_TOO_LARGE;
126 OmegaTc2Spec s; memset(&s, 0, sizeof s);
127 s.k = w->k; s.rt = RT(); s.ct = 2; s.warps = omega_api_tcw(g.warps); s.aqn = g.aqn ? g.aqn : 1u;
128 s.epi = w->layout == OMEGA_GPU_PF_SWIGLU ? OMEGA_TC2_EPI_SWIGLU : OMEGA_TC2_EPI_F32; s.direct = rowidx_va == 0; s.n = w->n; s.mutant = g.mutant;
129 if (!omega_tc2_spec_ok(&s)) return OMEGA_GPU_PF_BAD_ARGS;
130 const uint32_t span = omega_tc2_span(&s);
131 if (w->n % span) return OMEGA_GPU_PF_BAD_ARGS;
132 OmegaPfKey key;
133 ar_key(&key, sizeof key / sizeof(uint32_t), KIND_TC2, &s, sizeof s / sizeof(uint32_t));
134 _Static_assert(sizeof(OmegaTc2Spec) <= sizeof key.words, "spec fits the key");
135 uint32_t args[OMEGA_TC2_ARGS_WORDS];
136 const uint64_t va[4] = { tiles_va, rowidx_va, x_va, y_va };
137#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
138 for (int i = 0; i < 4; i++) { args[2 * i] = (uint32_t)va[i]; args[2 * i + 1] = (uint32_t)(va[i] >> 32); }
139 args[8] = w->n;
140#else
141 const uint32_t tail[] = {w->n};
142 ar_args_extra(args, OMEGA_TC2_ARGS_WORDS, va, 4, tail, 1);
143#endif
144 omega_gpu_session_lock();
145 int rc = omega_gpu_pf_open_locked();
146 OmegaPfKernel k;
147 if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_kernel_locked(&key, gen_tc2, &k);
148 uint64_t launch[32] = {5, s.warps}, grid[32] = {1, w->n, span, 0};
149 ar_check((uint32_t)ar_plan(launch));
150 ar_check((uint32_t)ar_plan(grid));
151 if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_launch_locked(&k, (uint32_t)launch[16], (uint32_t)grid[16], ntiles, omega_tc2_shared_bytes(&s), args, OMEGA_TC2_ARGS_WORDS, ns);
152 omega_gpu_session_unlock();
153 return rc;
154}
155
156/* ---- native FP8 path (W-L1G): activation quantiser (omega_blackwell_codegen_qg.h) ---- */
157#include "omega_blackwell_codegen_qg.h"
158#define KIND_PQ 3u
159static int pq_mutant;
160void omega_gpu_pf_quant_test_set_mutant(int m) { pq_mutant = m; }
161static int gen_pq(const OmegaPfKey *key, void *prog) { OmegaPqSpec s; memcpy(&s, key->words, sizeof s); return omega_blackwell_codegen_pq_prog(&s, prog); }
162/* cl non-NULL: record (caller holds the lock) instead of launch */
163static int pq_core(OmegaGpuCmdList *cl, uint32_t k, uint32_t in_type, uint32_t rows, uint64_t x_va, uint64_t q_va, uint64_t s_va, uint64_t *ns) {
164 OmegaPqSpec s; memset(&s, 0, sizeof s); s.k = k; s.in_type = in_type; s.mutant = pq_mutant;
165 s.groups = omega_api_qset(k);
166 uint64_t shape[32] = {13, k, s.groups};
167 ar_check((uint32_t)ar_plan(shape));
168 if (!omega_pq_spec_ok(&s) || !x_va || !q_va || !s_va) return OMEGA_GPU_PF_BAD_ARGS;
169 if (rows == 0) return OMEGA_GPU_PF_OK;
170 OmegaPfKey key;
171 ar_key(&key, sizeof key / sizeof(uint32_t), KIND_PQ, &s, sizeof s / sizeof(uint32_t));
172 uint32_t args[OMEGA_PQ_ARGS_WORDS];
173 const uint64_t va[] = {x_va, q_va, s_va};
174 ar_args(args, OMEGA_PQ_ARGS_WORDS, va, 3);
175 if (!cl) omega_gpu_session_lock();
176 int rc = omega_gpu_pf_open_locked();
177 OmegaPfKernel kern;
178 if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_kernel_locked(&key, gen_pq, &kern);
179 if (rc == OMEGA_GPU_PF_OK)
180 rc = cl ? omega_gpu_pf_record_locked(cl, &kern, (uint32_t)shape[19], rows, (uint32_t)shape[18], 0, args, OMEGA_PQ_ARGS_WORDS)
181 : omega_gpu_pf_launch_locked(&kern, (uint32_t)shape[19], rows, (uint32_t)shape[18], 0, args, OMEGA_PQ_ARGS_WORDS, ns);
182 if (!cl) omega_gpu_session_unlock();
183 return rc;
184}
185int omega_gpu_pf_quant(uint32_t k, uint32_t in_type, uint32_t rows, uint64_t x_va, uint64_t q_va, uint64_t s_va, uint64_t *ns) {
186 return pq_core(NULL, k, in_type, rows, x_va, q_va, s_va, ns);
187}
188int omega_gpu_pf_quant_record(OmegaGpuCmdList *cl, uint32_t k, uint32_t in_type, uint32_t rows, uint64_t x_va, uint64_t q_va, uint64_t s_va) {
189 if (!cl) return OMEGA_GPU_PF_BAD_ARGS;
190 return pq_core(cl, k, in_type, rows, x_va, q_va, s_va, NULL);
191}
192
193/* ---- native FP8 path (W-L1G): grouped GEMM on QMMA (omega_blackwell_codegen_qg.h) ---- */
194#define KIND_QG 4u
195static struct { uint32_t rt, warps, wait, set; int mutant; uint32_t group; } q;
196void omega_gpu_pf_qgemm_set_group(uint32_t group) { q.group = group; }
197void omega_gpu_pf_qgemm_set_shape(uint32_t rt, uint32_t warps, uint32_t wait) { q.rt = rt; q.warps = warps; q.wait = wait; q.set = rt || warps || wait; }
198void omega_gpu_pf_qgemm_test_set_mutant(int m) { q.mutant = m; }
199static uint32_t QRT(void) { return omega_api_qrt(q.rt); }
200static uint32_t QWARPS(void) { return omega_api_tcw(q.warps); }
201static uint32_t QWAIT(void) { return omega_api_qwait(q.set, q.wait); }
202uint32_t omega_gpu_pf_qgemm_bm(void) { uint64_t p[32] = {5, 0, 16, QRT(), 1}; ar_check((uint32_t)ar_plan(p)); return (uint32_t)p[17]; }
203
204/* tile table from per-expert base addresses (w_va[e], s_va[e]: GEMV-tensor layout, wherever it lives) */
205uint32_t omega_gpu_pf_qgemm_tiles_va(uint32_t E, const uint64_t *w_va, const uint64_t *s_va, const uint32_t *counts, void *tab, uint32_t max) {
206 if (!E || !w_va || !s_va || !counts || !tab) return 0;
207 const uint32_t bm = omega_gpu_pf_qgemm_bm();
208#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
209 Tile *tl = tab; uint32_t nt = 0, row = 0;
210 for (uint32_t e = 0; e < E; e++) {
211 for (uint32_t r = 0; r < counts[e]; r += bm) {
212 if (nt >= max) return 0;
213 tl[nt].w_va = w_va[e]; tl[nt].r_va = s_va[e];
214 tl[nt].row0 = row + r; tl[nt].cnt = counts[e] - r < bm ? counts[e] - r : bm; tl[nt].pad = 0; nt++;
215 }
216 row += counts[e];
217 }
218 return nt;
219#else
220 uint64_t cfg[12] = {0, 0, 0, bm};
221 uint64_t more = 0, nt = 0;
222 for (;;) {
223 ar_check(omega_arnext(&more, cfg, 12, counts, E));
224 if (!more) return (uint32_t)nt;
225 cfg[4] = w_va[cfg[6]]; cfg[5] = s_va[cfg[6]];
226 ar_check(omega_artile(&nt, cfg, 8, tab, (uint64_t)max * 4));
227 if (!nt) return 0;
228 cfg[0] = nt;
229 }
230#endif
231}
232/* the same straight from an FP8 GEMV tensor (omega_gpu_fp8_tensor_entry, read in place, no copy) */
233uint32_t omega_gpu_pf_qgemm_tiles(const OmegaGpuFp8Tensor *t, const uint32_t *counts, void *tab, uint32_t max) {
234 if (!t || !counts || !tab) return 0;
235 uint32_t E = 0, n = 0, k = 0; omega_gpu_fp8_tensor_shape(t, &E, &n, &k);
236 const uint32_t bm = omega_gpu_pf_qgemm_bm();
237#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
238 Tile *tl = tab; uint32_t nt = 0, row = 0;
239 for (uint32_t e = 0; e < E; e++) {
240 if (!counts[e]) continue;
241 uint64_t w_va = 0, s_va = 0;
242 if (omega_gpu_fp8_tensor_entry(t, e, &w_va, &s_va) != 0) return 0;
243 for (uint32_t r = 0; r < counts[e]; r += bm) {
244 if (nt >= max) return 0;
245 tl[nt].w_va = w_va; tl[nt].r_va = s_va;
246 tl[nt].row0 = row + r; tl[nt].cnt = counts[e] - r < bm ? counts[e] - r : bm; tl[nt].pad = 0; nt++;
247 }
248 row += counts[e];
249 }
250 return nt;
251#else
252 uint64_t cfg[12] = {0, 0, 0, bm};
253 uint64_t more = 0, nt = 0;
254 for (;;) {
255 ar_check(omega_arnext(&more, cfg, 12, counts, E));
256 if (!more) return (uint32_t)nt;
257 if (omega_gpu_fp8_tensor_entry(t, (uint32_t)cfg[6], &cfg[4], &cfg[5]) != 0) return 0;
258 ar_check(omega_artile(&nt, cfg, 8, tab, (uint64_t)max * 4));
259 if (!nt) return 0;
260 cfg[0] = nt;
261 }
262#endif
263}
264
265static int gen_qg(const OmegaPfKey *key, void *prog) { OmegaQgSpec s; memcpy(&s, key->words, sizeof s); return omega_blackwell_codegen_qgemm_prog(&s, prog); }
266
267static int qg_core(OmegaGpuCmdList *cl, const OmegaQgSpec *scoped, uint32_t n, uint32_t k, uint32_t layout, uint64_t tiles_va, uint32_t ntiles, uint64_t rowidx_va, uint64_t xq_va,
268 uint64_t xs_va, uint64_t y_va, uint64_t *ns) {
269 if (!tiles_va || !xq_va || !xs_va || !y_va) return OMEGA_GPU_PF_BAD_ARGS;
270 if (layout != OMEGA_GPU_PF_PLAIN && layout != OMEGA_GPU_PF_SWIGLU_NAT) return OMEGA_GPU_PF_BAD_ARGS;
271 if (ntiles == 0) return OMEGA_GPU_PF_OK;
272 if (ntiles > 65535u) return OMEGA_GPU_PF_TOO_LARGE;
273 OmegaQgSpec s; memset(&s, 0, sizeof s);
274 if (scoped) s = *scoped;
275 else { s.rt = QRT(); s.warps = QWARPS(); s.wait = QWAIT(); s.mutant = q.mutant; s.group = q.group; }
276 s.k = k; s.n = n;
277 s.epi = layout == OMEGA_GPU_PF_SWIGLU_NAT ? OMEGA_QG_EPI_SWIGLU : OMEGA_QG_EPI_F32; s.direct = rowidx_va == 0;
278 if (!omega_qg_spec_ok(&s)) return OMEGA_GPU_PF_BAD_ARGS;
279 const uint32_t span = omega_qg_span(&s);
280 OmegaPfKey key;
281 ar_key(&key, sizeof key / sizeof(uint32_t), KIND_QG, &s, sizeof s / sizeof(uint32_t));
282 _Static_assert(sizeof(OmegaQgSpec) <= sizeof key.words, "spec fits the key");
283 _Static_assert(sizeof(Tile) == OMEGA_QG_TILE_BYTES, "tile entry");
284 uint32_t args[OMEGA_QG_ARGS_WORDS];
285 const uint64_t va[5] = { tiles_va, rowidx_va, xq_va, y_va, xs_va };
286#if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST
287 for (int i = 0; i < 5; i++) { args[2 * i] = (uint32_t)va[i]; args[2 * i + 1] = (uint32_t)(va[i] >> 32); }
288 args[10] = n; args[11] = ntiles;
289#else
290 const uint32_t tail[] = {n, ntiles};
291 ar_args_extra(args, OMEGA_QG_ARGS_WORDS, va, 5, tail, 2);
292#endif
293 uint64_t plan[64] = {0}, ok = 0;
294 if (omega_native_qgorder(&ok, plan, 64, s.group, n / span, ntiles, 1) != OMEGA_OK || ok != 1) return OMEGA_GPU_PF_BAD_ARGS;
295 if (!cl) omega_gpu_session_lock();
296 int rc = omega_gpu_pf_open_locked();
297 OmegaPfKernel kern;
298 if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_kernel_locked(&key, gen_qg, &kern);
299 if (rc == OMEGA_GPU_PF_OK)
300 {
301 uint64_t launch[32] = {5, s.warps};
302 ar_check((uint32_t)ar_plan(launch));
303 rc = cl ? omega_gpu_pf_record_locked(cl, &kern, (uint32_t)launch[16], (uint32_t)plan[0], (uint32_t)plan[1], omega_qg_shared_bytes(&s), args, OMEGA_QG_ARGS_WORDS)
304 : omega_gpu_pf_launch_locked(&kern, (uint32_t)launch[16], (uint32_t)plan[0], (uint32_t)plan[1], omega_qg_shared_bytes(&s), args, OMEGA_QG_ARGS_WORDS, ns);
305 }
306 if (!cl) omega_gpu_session_unlock();
307 return rc;
308}
309int omega_gpu_pf_qgemm(uint32_t n, uint32_t k, uint32_t layout, uint64_t tiles_va, uint32_t ntiles, uint64_t rowidx_va, uint64_t xq_va, uint64_t xs_va, uint64_t y_va, uint64_t *ns) {
310 return qg_core(NULL, NULL, n, k, layout, tiles_va, ntiles, rowidx_va, xq_va, xs_va, y_va, ns);
311}
312int omega_gpu_pf_qgemm_record(OmegaGpuCmdList *cl, uint32_t n, uint32_t k, uint32_t layout, uint64_t tiles_va, uint32_t ntiles,
313 uint64_t rowidx_va, uint64_t xq_va, uint64_t xs_va, uint64_t y_va) {
314 if (!cl) return OMEGA_GPU_PF_BAD_ARGS;
315 return qg_core(cl, NULL, n, k, layout, tiles_va, ntiles, rowidx_va, xq_va, xs_va, y_va, NULL);
316}
317
318/* Per-call options: no reads or writes of the experimental process-global knobs.
319 * Shape and work-order admission remain in the checked Omega units. */
320static OmegaQgSpec qg_scoped_spec(uint32_t rt, uint32_t warps, uint32_t wait, uint32_t group) {
321 OmegaQgSpec s; memset(&s, 0, sizeof s);
322 s.rt = rt; s.warps = warps; s.wait = wait; s.group = group;
323 return s;
324}
325uint32_t omega_gpu_pf_qgemm_scoped_bm(uint32_t rt, uint32_t warps, uint32_t wait, uint32_t group) {
326 OmegaQgSpec s = qg_scoped_spec(rt, warps, wait, group);
327 s.n = 256; s.k = 128; s.direct = 1;
328 if (!omega_qg_spec_ok(&s)) return 0;
329 uint64_t p[32] = {5, 0, 16, s.rt, 1};
330 ar_check((uint32_t)ar_plan(p));
331 return (uint32_t)p[17];
332}
333int omega_gpu_pf_qgemm_scoped(uint32_t rt, uint32_t warps, uint32_t wait, uint32_t group,
334 uint32_t n, uint32_t k, uint32_t layout, uint64_t tiles_va, uint32_t ntiles,
335 uint64_t rowidx_va, uint64_t xq_va, uint64_t xs_va, uint64_t y_va, uint64_t *ns) {
336 const OmegaQgSpec s = qg_scoped_spec(rt, warps, wait, group);
337 return qg_core(NULL, &s, n, k, layout, tiles_va, ntiles, rowidx_va, xq_va, xs_va, y_va, ns);
338}
339
340/* Scalar transport for the checked Omega reading profile. */
341uint32_t omega_gpu_pf_qgemm_reading_option(uint32_t rows, uint32_t index) {
342 uint64_t option = UINT32_MAX;
343 if (omega_native_qgreading(&option, rows, index) != OMEGA_OK || option > UINT32_MAX) return UINT32_MAX;
344 return (uint32_t)option;
345}
346
347/* Existing allocation capacities are admitted by Omega before reusing another profile. */
348int omega_gpu_pf_qgemm_workspace_fits(uint64_t capacity, uint64_t padded, uint64_t tiles, uint64_t rows, uint64_t bm) {
349 uint64_t fits = 0;
350 return omega_native_qgworkspace(&fits, capacity, padded, tiles, rows, bm) == OMEGA_OK && fits == 1;
351}