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

440 lines · 21646 bytes Raw Blame History
1/* SPDX-License-Identifier: Apache-2.0
2 * Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17/*
18 * omega_gpu_moe_api.c: column windows over a resident bf16 tensor (OWN-30B cut 2). See the header.
19 *
20 * Same submission path, buffers, poison check and latch rules as omega_gpu_matmul_api.c (FB-1 cut 1b
21 * on the shared session), restated here rather than shared because that file is owned by the 8B
22 * decode-speed lane and its tensor type is opaque; this file only ADDS exports. Every buffer both
23 * sides touch is GPU-uncached (nvos.h NVOS32_ATTR2_GPU_CACHEABLE: "For system memory this will not
24 * be coherent with direct CPU mappings").
25 *
26 * The window rule: B is in fragment order (omega_matmul_frag_pack_b), column tile nt = kp x 8
27 * elements contiguous at element nt * kp * 8. A window starting at column tile t0 of width wnp
28 * (padded to 8) is therefore the chip-proven kernel for (kp, wnp) with b_va advanced by
29 * t0 * kp * 8 * 2 bytes. Nothing else about the launch changes.
30 */
31#include "omega_gpu_moe_api.h"
32#include "omega_gpu_session.h"
33#include "omega_gpu_fmt.h"
34#include "omega_blackwell_codegen.h"
35#include "omega_blackwell_matmul.h"
36#include "omega_blackwell_qmd.h"
37#include <string.h>
38
39#define MOE_CACHE_SLOTS 16 /* default kernel cache slots; raised only by omega_gpu_moe_reserve */
40#define MOE_CACHE_SLOTS_MAX 128
41#define FRAG_PACK_TILES 64u /* column tiles per host packing chunk at upload */
42#define MARKER_TIMEOUT_MS 20000ull
43#define POISON_F32 (-999.0f)
44
45struct OmegaGpuMoeTensor {
46 uint32_t k, n, kp, np;
47 OmegaGpuMem mem; /* kp x np bf16 in fragment order */
48};
49
50typedef struct {
51 bool valid, pinned;
52 uint32_t kp, np, grid_x, rt, ct;
53 int mutant;
54 OmegaBlackwellKernel kernel;
55 OmegaGpuMem code;
56} MoeCacheSlot;
57
58static struct {
59 OmegaGpuScratch a_buf, c_buf;
60 bool a_row0_ready; uint32_t a_row0_kp; const void *a_row0_cpu;
61 uint64_t a_row0_hits;
62 uint32_t cta_budget;
63 int mutant;
64 int32_t skew; /* test only: column tiles added to every window's B base */
65 bool hooked;
66 MoeCacheSlot cache[MOE_CACHE_SLOTS_MAX];
67 unsigned cache_next, slots;
68 int sealed;
69} g = { .cta_budget = OMEGA_GPU_MATMUL_MAX_CTAS, .slots = MOE_CACHE_SLOTS };
70
71#define FAILAT(s) (omega_gpu_session_set_error(s), 1)
72static uint64_t now_ns(void) { return omega_gpu_session_now_ns(); }
73static uint32_t round_up(uint32_t v, uint32_t q) { return (v + q - 1) / q * q; }
74
75uint64_t omega_gpu_moe_window_byte_offset(uint32_t kp, uint32_t col0) {
76 return (uint64_t)(col0 / OMEGA_GPU_MATMUL_TILE_N) * kp * OMEGA_GPU_MATMUL_TILE_N * 2u;
77}
78
79/* ---- device-side state (caller holds the session lock) ---- */
80
81static void cache_clear_locked(void) {
82 for (unsigned i = 0; i < MOE_CACHE_SLOTS_MAX; i++) {
83 MoeCacheSlot *s = &g.cache[i];
84 if (!s->valid) continue;
85 omega_gpu_session_free(&s->code);
86 memset(s, 0, sizeof *s);
87 }
88 g.cache_next = 0;
89}
90
91static void on_session_close(void) {
92 cache_clear_locked();
93 g.a_row0_ready = false;
94 omega_gpu_session_scratch_free(&g.a_buf);
95 omega_gpu_session_scratch_free(&g.c_buf);
96 g.slots = MOE_CACHE_SLOTS;
97 g.sealed = 0;
98}
99
100static OmegaGpuSession *dev_open_locked(void) {
101 if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; }
102 return omega_gpu_session_open();
103}
104
105void omega_gpu_moe_cache_clear(void) {
106 omega_gpu_session_lock();
107 cache_clear_locked();
108 omega_gpu_session_unlock();
109}
110
111void omega_gpu_moe_set_cta_budget(uint32_t ctas) {
112 omega_gpu_session_lock();
113 g.cta_budget = ctas ? ctas : OMEGA_GPU_MATMUL_MAX_CTAS;
114 cache_clear_locked();
115 omega_gpu_session_unlock();
116}
117
118uint32_t omega_gpu_moe_cta_budget(void) { return g.cta_budget; }
119
120void omega_gpu_moe_test_set_mutant(int on) {
121 omega_gpu_session_lock();
122 g.mutant = on ? 1 : 0;
123 cache_clear_locked();
124 omega_gpu_session_unlock();
125}
126
127void omega_gpu_moe_test_set_window_skew(int32_t tiles) {
128 omega_gpu_session_lock();
129 g.skew = tiles;
130 omega_gpu_session_unlock();
131}
132
133uint64_t omega_gpu_moe_test_a_row0_hits(void) { return g.a_row0_hits; }
134
135/* ---- launch shape and kernel cache (the matmul API's rules, restated) ---- */
136
137static void launch_shape(uint32_t mp, uint32_t np, uint32_t *rows_per_launch, uint32_t *grid_x, uint32_t *rt, uint32_t *ct) {
138 const uint32_t budget = g.cta_budget ? g.cta_budget : OMEGA_GPU_MATMUL_MAX_CTAS, mt = mp / 16u, nt = np / 8u;
139 omega_matmul_frag_block_shape(mt, nt, rt, ct);
140 const uint32_t rb = mt / *rt, nb = nt / *ct;
141 *rows_per_launch = rb <= budget ? mp : budget * 16u * *rt;
142 uint32_t gx = budget / (*rows_per_launch / 16u / *rt);
143 if (gx == 0) gx = 1;
144 if (gx > nb) gx = nb;
145 *grid_x = gx;
146}
147
148static int kernel_for(uint32_t kp, uint32_t np, uint32_t grid_x, uint32_t rt, uint32_t ct, MoeCacheSlot **out, bool *hit) {
149 for (unsigned i = 0; i < g.slots; i++) {
150 MoeCacheSlot *s = &g.cache[i];
151 if (s->valid && s->kp == kp && s->np == np && s->grid_x == grid_x && s->rt == rt && s->ct == ct && s->mutant == g.mutant) {
152 *out = s; *hit = true;
153 return OMEGA_GPU_MATMUL_OK;
154 }
155 }
156 if (g.sealed) {
157 char e[160];
158 omega_gpu_fmt(e, sizeof e, "serving reservation exceeded: moe window kernel kp=%u np=%u grid_x=%u rt=%u ct=%u was not prepared", kp, np, grid_x, rt, ct);
159 omega_gpu_session_set_error(e);
160 return OMEGA_GPU_MATMUL_TOO_LARGE;
161 }
162 MoeCacheSlot *s = NULL;
163 for (unsigned i = 0; i < g.slots && !s; i++) {
164 MoeCacheSlot *c = &g.cache[(g.cache_next + i) % g.slots];
165 if (!c->valid || !c->pinned) { s = c; g.cache_next = (unsigned)((c - g.cache) + 1) % g.slots; }
166 }
167 if (!s) {
168 omega_gpu_session_set_error("moe window kernel cache: every slot holds a prepared kernel (raise kernel_slots)");
169 return OMEGA_GPU_MATMUL_TOO_LARGE;
170 }
171 OmegaMatMulSpec spec;
172 if (omega_matmul_spec_init(&spec, 16, kp, np, OMEGA_MATMUL_PRECISION_BF16) != 0) return OMEGA_GPU_MATMUL_TOO_LARGE;
173 OmegaBlackwellKernel kernel;
174 memset(&kernel, 0, sizeof kernel);
175 const bool blocked = rt > 1 || ct > 1;
176 uint32_t unroll = blocked ? omega_matmul_frag_block_unroll(kp, rt, ct) : omega_matmul_frag_unroll(kp);
177 if (g.mutant) while (unroll > 1 && kp / 16u / unroll < 2) unroll /= 2;
178 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog);
179 if (!prog) { omega_gpu_session_set_error("moe window codegen: host memory"); return OMEGA_GPU_MATMUL_CODEGEN_FAIL; }
180 int cg = blocked ? omega_blackwell_codegen_matmul_frag_block_prog(&spec, grid_x, unroll, rt, ct, g.mutant, prog)
181 : omega_blackwell_codegen_matmul_frag_prog(&spec, grid_x, unroll, g.mutant, prog);
182 const size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0;
183 if (cg != 0 || need == 0) { omega_gpu_host_free(prog); return OMEGA_GPU_MATMUL_CODEGEN_FAIL; }
184 OmegaGpuMem code;
185 if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_MATMUL_CHIP_FAIL; }
186 cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel);
187 omega_gpu_host_free(prog);
188 if (cg != 0 || !kernel.code || kernel.code_size != need) { omega_gpu_session_free(&code); return OMEGA_GPU_MATMUL_CODEGEN_FAIL; }
189 if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_MATMUL_CHIP_FAIL; }
190 if (s->valid) omega_gpu_session_free(&s->code);
191 s->valid = true; s->pinned = false; s->kp = kp; s->np = np; s->grid_x = grid_x; s->rt = rt; s->ct = ct; s->mutant = g.mutant;
192 s->kernel = kernel; s->code = code;
193 *out = s; *hit = false;
194 return OMEGA_GPU_MATMUL_OK;
195}
196
197static int launch(const MoeCacheSlot *ks, uint64_t a_va, uint64_t b_va, uint64_t c_va,
198 uint32_t grid_x, uint32_t grid_y, uint64_t *elapsed_ns, uint32_t *marker_out) {
199 uint32_t cbank_args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS];
200 omega_blackwell_build_cbank_args_matmul(cbank_args, a_va, b_va, c_va, grid_y * 16u * ks->rt, ks->kp, ks->np);
201 OmegaGpuLaunch L = {
202 .code_va = ks->code.va, .gpr_count = omega_bw_gpr_declared(ks->kernel.gpr_count), /* peak + 2 rounded up to 8 (the session passed the raw peak before) */
203 .threads_x = 32, .threads_y = 1, .grid_x = grid_x, .grid_y = grid_y,
204 .num_elements = (size_t)grid_x * grid_y * 128u,
205 .args = cbank_args, .n_args = OMEGA_BW_CBANK_MATMUL_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS,
206 };
207 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_MATMUL_CHIP_FAIL; }
208 return omega_gpu_session_launch(&L, elapsed_ns, marker_out) == 0 ? OMEGA_GPU_MATMUL_OK : OMEGA_GPU_MATMUL_CHIP_FAIL;
209}
210
211/* ---- serving reservation ---- */
212
213int omega_gpu_moe_reserve(uint32_t max_rows, uint32_t max_k, uint32_t max_cols, uint32_t kernel_slots) {
214 if (max_rows == 0 || max_k == 0 || max_cols == 0) { omega_gpu_session_set_error("moe reserve: zero bound"); return OMEGA_GPU_MATMUL_BAD_ARGS; }
215 if (max_rows > OMEGA_BW_MATMUL_MAX_M || max_k > OMEGA_BW_MATMUL_MAX_K || max_cols > OMEGA_BW_MATMUL_MAX_N || kernel_slots > MOE_CACHE_SLOTS_MAX) {
216 omega_gpu_session_set_error("moe reserve: bound past the matmul envelope");
217 return OMEGA_GPU_MATMUL_TOO_LARGE;
218 }
219 const size_t mp = round_up(max_rows, OMEGA_GPU_MATMUL_TILE_M), kp = round_up(max_k, OMEGA_GPU_MATMUL_TILE_K);
220 const size_t c_bytes = mp * round_up(max_cols, OMEGA_GPU_MATMUL_TILE_N) * 4u;
221 omega_gpu_session_lock();
222 int rc = OMEGA_GPU_MATMUL_OK;
223 if (!dev_open_locked()) rc = OMEGA_GPU_MATMUL_CHIP_FAIL;
224 else if (omega_gpu_session_scratch_reserve(&g.a_buf, mp * kp * 2u) != 0 || omega_gpu_session_scratch_reserve(&g.c_buf, c_bytes) != 0) {
225 omega_gpu_session_scratch_unreserve(&g.a_buf); omega_gpu_session_scratch_unreserve(&g.c_buf);
226 rc = OMEGA_GPU_MATMUL_CHIP_FAIL;
227 } else if (kernel_slots > g.slots) {
228 cache_clear_locked();
229 g.slots = kernel_slots;
230 }
231 g.a_row0_ready = false;
232 omega_gpu_session_unlock();
233 return rc;
234}
235
236void omega_gpu_moe_unreserve(void) {
237 omega_gpu_session_lock();
238 omega_gpu_session_scratch_unreserve(&g.a_buf);
239 omega_gpu_session_scratch_unreserve(&g.c_buf);
240 omega_gpu_session_unlock();
241}
242
243int omega_gpu_moe_prepare(uint32_t m, uint32_t k, uint32_t cols) {
244 if (m == 0 || k == 0 || cols == 0) { omega_gpu_session_set_error("moe prepare: zero dimension"); return OMEGA_GPU_MATMUL_BAD_ARGS; }
245 if (m > OMEGA_BW_MATMUL_MAX_M || k > OMEGA_BW_MATMUL_MAX_K || cols > OMEGA_BW_MATMUL_MAX_N) {
246 omega_gpu_session_set_error("moe prepare: shape past the matmul envelope");
247 return OMEGA_GPU_MATMUL_TOO_LARGE;
248 }
249 const uint32_t mp = round_up(m, OMEGA_GPU_MATMUL_TILE_M), kp = round_up(k, OMEGA_GPU_MATMUL_TILE_K), np = round_up(cols, OMEGA_GPU_MATMUL_TILE_N);
250 uint32_t rows_per_launch, grid_x, rt, ct;
251 omega_gpu_session_lock();
252 int rc = OMEGA_GPU_MATMUL_OK;
253 if (!dev_open_locked()) rc = OMEGA_GPU_MATMUL_CHIP_FAIL;
254 else {
255 launch_shape(mp, np, &rows_per_launch, &grid_x, &rt, &ct);
256 MoeCacheSlot *s = NULL; bool hit = false;
257 rc = kernel_for(kp, np, grid_x, rt, ct, &s, &hit);
258 if (rc == OMEGA_GPU_MATMUL_OK) s->pinned = true;
259 }
260 omega_gpu_session_unlock();
261 return rc;
262}
263
264void omega_gpu_moe_seal(int on) {
265 omega_gpu_session_lock();
266 g.sealed = on ? 1 : 0;
267 omega_gpu_session_unlock();
268}
269
270/* ---- tensors ---- */
271
272void omega_gpu_moe_tensor_shape(const OmegaGpuMoeTensor *t, uint32_t *k, uint32_t *n) {
273 if (k) *k = t ? t->k : 0;
274 if (n) *n = t ? t->n : 0;
275}
276
277int omega_gpu_moe_tensor_upload_bf16(uint32_t k, uint32_t n, const uint16_t *b, OmegaGpuMoeTensor **out) {
278 if (out) *out = NULL;
279 if (!b || !out || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS;
280 if (k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) return OMEGA_GPU_MATMUL_TOO_LARGE;
281 OmegaGpuMoeTensor *t = omega_gpu_host_calloc(1, sizeof *t);
282 if (!t) return OMEGA_GPU_MATMUL_BAD_ARGS;
283 t->k = k; t->n = n; t->kp = round_up(k, OMEGA_GPU_MATMUL_TILE_K); t->np = round_up(n, OMEGA_GPU_MATMUL_TILE_N);
284 const uint32_t nt = t->np / 8u;
285 uint16_t *h = omega_gpu_host_alloc((size_t)FRAG_PACK_TILES * t->kp * 8u * sizeof *h);
286 if (!h) { omega_gpu_host_free(t); return OMEGA_GPU_MATMUL_BAD_ARGS; }
287 int rc = OMEGA_GPU_MATMUL_OK;
288 omega_gpu_session_lock();
289 if (!dev_open_locked() || omega_gpu_session_alloc((size_t)t->kp * t->np * 2u, &t->mem) != 0) rc = OMEGA_GPU_MATMUL_CHIP_FAIL;
290 omega_gpu_session_unlock();
291 if (rc != OMEGA_GPU_MATMUL_OK) { omega_gpu_host_free(h); omega_gpu_host_free(t); return rc; }
292 uint16_t *d = t->mem.cpu;
293 for (uint32_t nt0 = 0; nt0 < nt; nt0 += FRAG_PACK_TILES) {
294 const uint32_t nt1 = nt - nt0 < FRAG_PACK_TILES ? nt : nt0 + FRAG_PACK_TILES;
295 omega_matmul_frag_pack_b(b, k, n, t->kp, nt0, nt1, h);
296 memcpy(&d[(size_t)nt0 * t->kp * 8u], h, (size_t)(nt1 - nt0) * t->kp * 8u * sizeof *h);
297 }
298 omega_gpu_host_free(h);
299 if (omega_gpu_session_sync_to_gpu(&t->mem) != 0) { omega_gpu_moe_tensor_free(t); return OMEGA_GPU_MATMUL_CHIP_FAIL; }
300 *out = t;
301 return OMEGA_GPU_MATMUL_OK;
302}
303
304int omega_gpu_moe_tensor_upload_f32(uint32_t k, uint32_t n, const float *b, OmegaGpuMoeTensor **out) {
305 if (out) *out = NULL;
306 if (!b || !out || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS;
307 if (k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) return OMEGA_GPU_MATMUL_TOO_LARGE;
308 size_t cnt = (size_t)k * n;
309 uint16_t *h = omega_gpu_host_alloc(cnt * sizeof *h);
310 if (!h) return OMEGA_GPU_MATMUL_BAD_ARGS;
311 for (size_t i = 0; i < cnt; i++) h[i] = omega_fp32_to_bf16(b[i]);
312 int rc = omega_gpu_moe_tensor_upload_bf16(k, n, h, out);
313 omega_gpu_host_free(h);
314 return rc;
315}
316
317void omega_gpu_moe_tensor_free(OmegaGpuMoeTensor *t) {
318 if (!t) return;
319 omega_gpu_session_lock();
320 omega_gpu_session_free(&t->mem);
321 omega_gpu_session_unlock();
322 omega_gpu_host_free(t);
323}
324
325/* ---- the window matmul ---- */
326
327/* The alignment rule, fail-closed, with the stage named. */
328static int window_args_ok(const OmegaGpuMoeTensor *t, uint32_t col0, uint32_t cols) {
329 char e[160];
330 if (col0 % OMEGA_GPU_MATMUL_TILE_N != 0) {
331 omega_gpu_fmt(e, sizeof e, "moe window: col0=%u is not a multiple of %u (a window starts on a column tile)", col0, OMEGA_GPU_MATMUL_TILE_N);
332 omega_gpu_session_set_error(e);
333 return 0;
334 }
335 if (cols == 0 || col0 > t->n || cols > t->n - col0) {
336 omega_gpu_fmt(e, sizeof e, "moe window: [%u, %u + %u) is not inside the tensor's %u columns", col0, col0, cols, t->n);
337 omega_gpu_session_set_error(e);
338 return 0;
339 }
340 return 1;
341}
342
343/* a16 or a32 (exactly one non-NULL). nwin windows of `cols` columns at col0[w]; c receives nwin
344 * blocks of m x cols. The activation pack and the kernel are shared by every window of the call. */
345static int windows_core(uint32_t m, const uint16_t *a16, const float *a32, const OmegaGpuMoeTensor *t,
346 uint32_t nwin, const uint32_t *col0, uint32_t cols, float *c, OmegaGpuMatmulInfo *info) {
347 const uint32_t k = t->k, kp = t->kp;
348 const uint32_t mp = round_up(m, OMEGA_GPU_MATMUL_TILE_M), np = round_up(cols, OMEGA_GPU_MATMUL_TILE_N);
349 for (uint32_t w = 0; w < nwin; w++) if (!window_args_ok(t, col0[w], cols)) return OMEGA_GPU_MATMUL_BAD_ARGS;
350 omega_gpu_session_lock();
351 int rc = OMEGA_GPU_MATMUL_OK;
352 if (!dev_open_locked()) { omega_gpu_session_unlock(); return OMEGA_GPU_MATMUL_CHIP_FAIL; }
353 uint32_t rows_per_launch, grid_x, rt, ct;
354 launch_shape(mp, np, &rows_per_launch, &grid_x, &rt, &ct);
355 if (info) {
356 info->m = m; info->k = k; info->n = cols; info->padded_m = mp; info->padded_n = np; info->padded_k = kp;
357 info->k_slices = 1; info->rows_per_call = rows_per_launch; info->grid_x = grid_x; info->grid_y = rows_per_launch / 16u / rt;
358 info->resident = true; info->kernel_cache_hit = true;
359 (void)omega_gpu_fmt(info->target_chip, sizeof info->target_chip, "%s", "NVIDIA DGX Spark (Grace Blackwell GB10)");
360 info->sm_architecture = 121;
361 }
362 if (g.sealed && !(g.a_buf.reserved && g.c_buf.reserved)) {
363 omega_gpu_session_set_error("serving reservation exceeded: moe window sealed without a scratch reservation (call omega_gpu_moe_reserve first)");
364 omega_gpu_session_unlock();
365 return OMEGA_GPU_MATMUL_TOO_LARGE;
366 }
367 int src = omega_gpu_session_scratch(&g.a_buf, (size_t)mp * kp * 2u);
368 if (src == 0) src = omega_gpu_session_scratch(&g.c_buf, (size_t)mp * np * 4u);
369 if (src != 0) {
370 omega_gpu_session_unlock();
371 return src == OMEGA_GPU_SESSION_SCRATCH_RESERVED ? OMEGA_GPU_MATMUL_TOO_LARGE : OMEGA_GPU_MATMUL_CHIP_FAIL;
372 }
373 uint16_t *ad = g.a_buf.mem.cpu;
374 const bool one_row = m == 1 && mp == 16u;
375 if (one_row && g.a_row0_ready && g.a_row0_kp == kp && g.a_row0_cpu == ad) {
376 if (a32) omega_matmul_frag_pack_a_row0_f32(a32, k, kp, ad);
377 else omega_matmul_frag_pack_a_row0(a16, k, kp, ad);
378 g.a_row0_hits++;
379 } else {
380 if (a32) omega_matmul_frag_pack_a_f32(a32, m, k, mp, kp, ad);
381 else omega_matmul_frag_pack_a(a16, m, k, mp, kp, ad);
382 g.a_row0_ready = one_row; g.a_row0_kp = kp; g.a_row0_cpu = ad;
383 }
384 if (omega_gpu_session_sync_to_gpu(&g.a_buf.mem) != 0) rc = OMEGA_GPU_MATMUL_CHIP_FAIL;
385 float *cd = g.c_buf.mem.cpu;
386 for (uint32_t w = 0; w < nwin && rc == OMEGA_GPU_MATMUL_OK; w++) {
387 /* the window's B base: column tile col0 / 8 (plus the test skew, which must be caught) */
388 const int64_t tile0 = (int64_t)(col0[w] / OMEGA_GPU_MATMUL_TILE_N) + g.skew;
389 const uint64_t b_va = t->mem.va + (uint64_t)tile0 * kp * OMEGA_GPU_MATMUL_TILE_N * 2u;
390 omega_matmul_result_sentinels(cd, m, cols, np, POISON_F32, 1);
391 if (omega_gpu_session_sync_to_gpu(&g.c_buf.mem) != 0) { rc = OMEGA_GPU_MATMUL_CHIP_FAIL; break; }
392 for (uint32_t r0 = 0; r0 < mp && rc == OMEGA_GPU_MATMUL_OK; r0 += rows_per_launch) {
393 uint32_t rows = mp - r0 < rows_per_launch ? mp - r0 : rows_per_launch;
394 uint32_t grid_y = rows / 16u / rt;
395 MoeCacheSlot *ks = NULL; bool hit = false;
396 rc = kernel_for(kp, np, grid_x, rt, ct, &ks, &hit);
397 if (rc != OMEGA_GPU_MATMUL_OK) break;
398 if (info && !hit) info->kernel_cache_hit = false;
399 rc = launch(ks, g.a_buf.mem.va + (uint64_t)r0 * kp * 2u, b_va, g.c_buf.mem.va + (uint64_t)r0 * np * 4u,
400 grid_x, grid_y, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL);
401 if (info) info->chip_calls++;
402 }
403 if (rc != OMEGA_GPU_MATMUL_OK) break;
404 if (omega_gpu_session_sync_to_cpu(&g.c_buf.mem) != 0) { rc = OMEGA_GPU_MATMUL_CHIP_FAIL; break; }
405 size_t unchanged = omega_matmul_result_sentinels(cd, m, cols, np, POISON_F32, 0);
406 if (unchanged && FAILAT("output poison survived")) { rc = OMEGA_GPU_MATMUL_CHIP_FAIL; break; }
407 float *cw = c + (size_t)w * m * cols;
408 for (uint32_t r = 0; r < m; r++) memcpy(&cw[(size_t)r * cols], &cd[(size_t)r * np], (size_t)cols * sizeof *c);
409 }
410 omega_gpu_session_unlock();
411 return rc;
412}
413
414static int entry(uint32_t m, const uint16_t *a16, const float *a32, const OmegaGpuMoeTensor *t,
415 uint32_t nwin, const uint32_t *col0, uint32_t cols, float *c, OmegaGpuMatmulInfo *info) {
416 uint64_t t0 = now_ns();
417 if (info) memset(info, 0, sizeof *info);
418 if ((!a16 && !a32) || !t || !c || !col0 || m == 0 || nwin == 0 || !t->mem.cpu) return OMEGA_GPU_MATMUL_BAD_ARGS;
419 if (nwin > OMEGA_GPU_MOE_MAX_WINDOWS) { omega_gpu_session_set_error("moe window: too many windows in one call"); return OMEGA_GPU_MATMUL_BAD_ARGS; }
420 if (m > OMEGA_BW_MATMUL_MAX_M) return OMEGA_GPU_MATMUL_TOO_LARGE;
421 int rc = windows_core(m, a16, a32, t, nwin, col0, cols, c, info);
422 if (info) info->call_ns = now_ns() - t0;
423 return rc;
424}
425
426int omega_gpu_matmul_resident_window_f32(uint32_t m, const float *a, const OmegaGpuMoeTensor *t,
427 uint32_t col0, uint32_t cols, float *c, OmegaGpuMatmulInfo *info) {
428 return entry(m, NULL, a, t, 1, &col0, cols, c, info);
429}
430
431int omega_gpu_matmul_resident_window_bf16(uint32_t m, const uint16_t *a, const OmegaGpuMoeTensor *t,
432 uint32_t col0, uint32_t cols, float *c, OmegaGpuMatmulInfo *info) {
433 return entry(m, a, NULL, t, 1, &col0, cols, c, info);
434}
435
436int omega_gpu_matmul_resident_windows_f32(uint32_t m, const float *a, const OmegaGpuMoeTensor *t,
437 uint32_t nwin, const uint32_t *col0, uint32_t cols, float *c,
438 OmegaGpuMatmulInfo *info) {
439 return entry(m, NULL, a, t, nwin, col0, cols, c, info);
440}