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 / evidence / OWN30B-REQUAL-MAIN-710d438c-20261015 / gates / cut4b / FB1-CUT4B-710d438c / omega_gpu_matmul_api.c

538 lines · 27827 bytes Raw Blame History
1/*
2 * omega_gpu_matmul_api.c: FB-1 cut 1b, on the shared session since cut 4b.
3 * See the header for the contract.
4 *
5 * Submission path: omega_gpu_session_launch (src/omega_gpu_session.c), the same
6 * words this file emitted in cut 1b (chip PASS FB1-CUT1B-4345406): SETUP_WORDS,
7 * shader-cache invalidate, constant bank upload, QMD0/QMD1 launch, first marker
8 * release, then the C3 tail (L2_FLUSH_DIRTY and a second release on uncached
9 * memory the host waits for before reading). Every buffer both sides touch is
10 * GPU-uncached (nvos.h NVOS32_ATTR2_GPU_CACHEABLE: "For system memory this will
11 * not be coherent with direct CPU mappings"). The device, channel, pushbuffer,
12 * constant bank, marker page and QMD page now belong to the session and are
13 * shared with the elementwise and attention APIs; this file keeps the resident
14 * tensors, the kernel cache (host and device copies) and its staging buffers.
15 */
16#include "omega_gpu_matmul_api.h"
17#include "omega_gpu_session.h"
18#include "omega_gpu_fmt.h"
19#include "omega_blackwell_codegen.h"
20#include "omega_blackwell_matmul.h"
21#include "omega_blackwell_qmd.h"
22#include "omega_blackwell_submit.h"
23#include <string.h>
24
25#define CACHE_SLOTS 8 /* default kernel cache slots; raised only by omega_gpu_matmul_reserve */
26#define CACHE_SLOTS_MAX 128 /* storage for the opt-in larger cache (a code buffer is a few KB); Qwen3-4B serving needs 95 */
27#define FRAG_PACK_TILES 64u /* column tiles per host packing chunk at upload */
28#define MARKER_TIMEOUT_MS 20000ull /* a 1x2048x128256 call is ~0.1 s; 20 s is a stall, not a slow run */
29#define POISON_F32 (-999.0f)
30#define ORACLE_TOL 1e-5
31
32struct OmegaGpuTensor {
33 uint32_t k, n, kp, np;
34 OmegaGpuMem mem; /* kp x np bf16 in fragment order (omega_matmul_frag_pack_b) */
35};
36
37typedef struct {
38 bool valid;
39 bool pinned; /* built by omega_gpu_matmul_prepare: never evicted */
40 uint32_t kp, np, grid_x;
41 uint32_t rt, ct; /* tiles per warp (omega_matmul_frag_block_shape); 1, 1 = the single-tile kernel */
42 int mutant;
43 OmegaMatMulSpec spec;
44 OmegaBlackwellKernel kernel;
45 OmegaGpuMem code; /* resident copy of kernel.code */
46} CacheSlot;
47
48static struct {
49 OmegaGpuScratch a_buf, c_buf; /* activation and result staging, grown to the high-water mark */
50 /* a_buf holds a full one-row pack (rows 1..15 of every block zero) at width a_row0_kp in the
51 * allocation a_row0_cpu: the next one-row call of that width writes row 0 only. Cleared by any
52 * other pack and whenever the buffer is freed. */
53 bool a_row0_ready; uint32_t a_row0_kp; const void *a_row0_cpu;
54 uint64_t a_row0_hits; /* one-row calls that wrote row 0 only (test hook) */
55 uint32_t cta_budget;
56 int oracle, mutant;
57 uint32_t unroll; /* test override of the fragment kernel unroll; 0 = omega_matmul_frag_unroll */
58 bool hooked;
59 CacheSlot cache[CACHE_SLOTS_MAX];
60 unsigned cache_next;
61 unsigned slots; /* active kernel cache slots, <= CACHE_SLOTS_MAX */
62 int sealed; /* omega_gpu_matmul_seal: a kernel cache miss is refused, never built */
63} g = { .cta_budget = OMEGA_GPU_MATMUL_MAX_CTAS, .oracle = 1, .slots = CACHE_SLOTS };
64
65#define FAILAT(s) (omega_gpu_session_set_error(s), 1)
66const char *omega_gpu_matmul_last_error(void) { return omega_gpu_session_last_error(); }
67
68static uint64_t now_ns(void) { return omega_gpu_session_now_ns(); } /* the platform clock (CLOCK_NS) */
69static inline double abs_d(double x) { return x < 0.0 ? -x : x; }
70
71static uint32_t round_up(uint32_t v, uint32_t q) { return (v + q - 1) / q * q; }
72
73const char *omega_gpu_matmul_rc_name(int rc) {
74 switch (rc) {
75 case OMEGA_GPU_MATMUL_OK: return "OK";
76 case OMEGA_GPU_MATMUL_BAD_ARGS: return "BAD_ARGS";
77 case OMEGA_GPU_MATMUL_TOO_LARGE: return "TOO_LARGE";
78 case OMEGA_GPU_MATMUL_CODEGEN_FAIL: return "CODEGEN_FAIL";
79 case OMEGA_GPU_MATMUL_CHIP_FAIL: return "CHIP_FAIL";
80 case OMEGA_GPU_MATMUL_PARITY_FAIL: return "PARITY_FAIL";
81 default: return "UNKNOWN";
82 }
83}
84
85int omega_gpu_matmul_is_blocked(void) { return omega_gpu_session_is_blocked(); }
86
87/* ---- device-side state (caller holds the session lock) ---- */
88
89static void cache_clear_locked(void) {
90 for (unsigned i = 0; i < CACHE_SLOTS_MAX; i++) {
91 CacheSlot *s = &g.cache[i];
92 if (!s->valid) continue;
93 omega_gpu_session_free(&s->code); /* kernel.code points into it */
94 memset(s, 0, sizeof *s);
95 }
96 g.cache_next = 0;
97}
98
99/* Called by the session (lock held, device still open) right before it closes. */
100static void on_session_close(void) {
101 cache_clear_locked();
102 g.a_row0_ready = false;
103 omega_gpu_session_scratch_free(&g.a_buf);
104 omega_gpu_session_scratch_free(&g.c_buf);
105 g.slots = CACHE_SLOTS; /* a serving reservation ends with the device */
106 g.sealed = 0;
107}
108
109static OmegaGpuSession *dev_open_locked(void) {
110 if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; }
111 return omega_gpu_session_open();
112}
113
114void omega_gpu_device_close(void) { omega_gpu_session_close(); }
115
116void omega_gpu_matmul_cache_clear(void) {
117 omega_gpu_session_lock();
118 cache_clear_locked();
119 omega_gpu_session_unlock();
120}
121
122void omega_gpu_matmul_set_cta_budget(uint32_t ctas) {
123 omega_gpu_session_lock();
124 g.cta_budget = ctas ? ctas : OMEGA_GPU_MATMUL_MAX_CTAS;
125 cache_clear_locked();
126 omega_gpu_session_unlock();
127}
128
129uint32_t omega_gpu_matmul_cta_budget(void) { return g.cta_budget; }
130
131/* ---- opt-in serving reservation (see the header) ---- */
132
133int omega_gpu_matmul_reserve(uint32_t max_rows, uint32_t max_k, uint32_t max_n, uint32_t max_n_one_row, uint32_t kernel_slots) {
134 if (max_rows == 0 || max_k == 0 || max_n == 0) { omega_gpu_session_set_error("matmul reserve: zero bound"); return OMEGA_GPU_MATMUL_BAD_ARGS; }
135 if (max_rows > OMEGA_BW_MATMUL_MAX_M || max_k > OMEGA_BW_MATMUL_MAX_K || max_n > OMEGA_BW_MATMUL_MAX_N || max_n_one_row > OMEGA_BW_MATMUL_MAX_N ||
136 kernel_slots > CACHE_SLOTS_MAX) {
137 omega_gpu_session_set_error("matmul reserve: bound past the matmul envelope");
138 return OMEGA_GPU_MATMUL_TOO_LARGE;
139 }
140 const size_t mp = round_up(max_rows, OMEGA_GPU_MATMUL_TILE_M), kp = round_up(max_k, OMEGA_GPU_MATMUL_TILE_K);
141 size_t c_bytes = mp * round_up(max_n, OMEGA_GPU_MATMUL_TILE_N) * 4u;
142 if (max_n_one_row) { /* a call of up to 16 rows (one row tile) may be wider than the batch width: lm_head */
143 size_t one = (size_t)OMEGA_GPU_MATMUL_TILE_M * round_up(max_n_one_row, OMEGA_GPU_MATMUL_TILE_N) * 4u;
144 if (one > c_bytes) c_bytes = one;
145 }
146 omega_gpu_session_lock();
147 int rc = OMEGA_GPU_MATMUL_OK;
148 if (!dev_open_locked()) rc = OMEGA_GPU_MATMUL_CHIP_FAIL;
149 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) {
150 /* roll back this API's own buffers: a failed reserve leaves nothing flagged */
151 omega_gpu_session_scratch_unreserve(&g.a_buf); omega_gpu_session_scratch_unreserve(&g.c_buf);
152 rc = OMEGA_GPU_MATMUL_CHIP_FAIL;
153 }
154 else if (kernel_slots > g.slots) { /* grow only; the cache restarts empty */
155 cache_clear_locked();
156 g.slots = kernel_slots;
157 }
158 omega_gpu_session_unlock();
159 return rc;
160}
161
162void omega_gpu_matmul_unreserve(void) {
163 omega_gpu_session_lock();
164 omega_gpu_session_scratch_unreserve(&g.a_buf);
165 omega_gpu_session_scratch_unreserve(&g.c_buf);
166 omega_gpu_session_unlock();
167}
168
169static int kernel_for(uint32_t kp, uint32_t np, uint32_t grid_x, uint32_t rt, uint32_t ct, CacheSlot **out, bool *hit);
170static void launch_shape(uint32_t mp, uint32_t np, uint32_t *rows_per_launch, uint32_t *grid_x, uint32_t *rt, uint32_t *ct);
171
172int omega_gpu_matmul_prepare(uint32_t m, uint32_t k, uint32_t n) {
173 if (m == 0 || k == 0 || n == 0) { omega_gpu_session_set_error("matmul prepare: zero dimension"); return OMEGA_GPU_MATMUL_BAD_ARGS; }
174 if (m > OMEGA_BW_MATMUL_MAX_M || k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) {
175 omega_gpu_session_set_error("matmul prepare: shape past the matmul envelope");
176 return OMEGA_GPU_MATMUL_TOO_LARGE;
177 }
178 const uint32_t mp = round_up(m, OMEGA_GPU_MATMUL_TILE_M), kp = round_up(k, OMEGA_GPU_MATMUL_TILE_K), np = round_up(n, OMEGA_GPU_MATMUL_TILE_N);
179 uint32_t rows_per_launch, grid_x, rt, ct;
180 omega_gpu_session_lock();
181 int rc = OMEGA_GPU_MATMUL_OK;
182 if (!dev_open_locked()) rc = OMEGA_GPU_MATMUL_CHIP_FAIL;
183 else {
184 launch_shape(mp, np, &rows_per_launch, &grid_x, &rt, &ct);
185 CacheSlot *s = NULL; bool hit = false;
186 rc = kernel_for(kp, np, grid_x, rt, ct, &s, &hit);
187 if (rc == OMEGA_GPU_MATMUL_OK) s->pinned = true; /* a prepared kernel is never evicted */
188 }
189 omega_gpu_session_unlock();
190 return rc;
191}
192
193/* Anything that clears the kernel cache while sealed (cache_clear, set_cta_budget, the test knobs, a
194 * reserve that grows the slot count) also drops the pinned kernels; the seal stays, so every later
195 * call is refused by name ("was not prepared") until the server prepares again or releases. */
196void omega_gpu_matmul_seal(int on) {
197 omega_gpu_session_lock();
198 g.sealed = on ? 1 : 0;
199 omega_gpu_session_unlock();
200}
201
202void omega_gpu_matmul_set_oracle(int on) { g.oracle = on ? 1 : 0; }
203
204void omega_gpu_matmul_test_set_mutant(int on) {
205 omega_gpu_session_lock();
206 g.mutant = on ? 1 : 0;
207 cache_clear_locked();
208 omega_gpu_session_unlock();
209}
210
211uint64_t omega_gpu_matmul_test_a_row0_hits(void) { return g.a_row0_hits; }
212
213void omega_gpu_matmul_test_set_unroll(uint32_t unroll) {
214 omega_gpu_session_lock();
215 g.unroll = unroll;
216 cache_clear_locked();
217 omega_gpu_session_unlock();
218}
219
220/* ---- one launch shape (the kernel cache key bakes in grid_x) ---- */
221
222/* rows per launch: all of them unless the row blocks alone exceed the CTA budget; grid_x fills the rest of
223 * the budget. A CTA (one warp) owns rt row tiles x ct column tiles (omega_matmul_frag_block_shape, 2026-10-12):
224 * rt = ct = 1 for decode rows (the single-tile kernel), 4 x 4 at m = 128. Every launch of a call has a row
225 * count divisible by 16*rt (rows_per_launch is, and so is the remainder, since rt divides mp/16). */
226static void launch_shape(uint32_t mp, uint32_t np, uint32_t *rows_per_launch, uint32_t *grid_x, uint32_t *rt, uint32_t *ct) {
227 const uint32_t budget = g.cta_budget ? g.cta_budget : OMEGA_GPU_MATMUL_MAX_CTAS, mt = mp / 16u, nt = np / 8u;
228 omega_matmul_frag_block_shape(mt, nt, rt, ct);
229 const uint32_t rb = mt / *rt, nb = nt / *ct;
230 *rows_per_launch = rb <= budget ? mp : budget * 16u * *rt;
231 uint32_t gx = budget / (*rows_per_launch / 16u / *rt);
232 if (gx == 0) gx = 1;
233 if (gx > nb) gx = nb;
234 *grid_x = gx;
235}
236
237/* ---- kernel cache (caller holds the lock, device open) ---- */
238
239static int kernel_for(uint32_t kp, uint32_t np, uint32_t grid_x, uint32_t rt, uint32_t ct, CacheSlot **out, bool *hit) {
240 for (unsigned i = 0; i < g.slots; i++) {
241 CacheSlot *s = &g.cache[i];
242 if (s->valid && s->kp == kp && s->np == np && s->grid_x == grid_x && s->rt == rt && s->ct == ct && s->mutant == g.mutant) {
243 *out = s; *hit = true;
244 return OMEGA_GPU_MATMUL_OK;
245 }
246 }
247 if (g.sealed) {
248 char e[160];
249 omega_gpu_fmt(e, sizeof e, "serving reservation exceeded: matmul kernel kp=%u np=%u grid_x=%u rt=%u ct=%u was not prepared", kp, np, grid_x, rt, ct);
250 omega_gpu_session_set_error(e);
251 return OMEGA_GPU_MATMUL_TOO_LARGE;
252 }
253 /* choose the slot before any build or driver call: round-robin, skipping prepared (pinned) kernels; all pinned = refused, nothing evicted */
254 CacheSlot *s = NULL;
255 for (unsigned i = 0; i < g.slots && !s; i++) {
256 CacheSlot *c = &g.cache[(g.cache_next + i) % g.slots];
257 if (!c->valid || !c->pinned) { s = c; g.cache_next = (unsigned)((c - g.cache) + 1) % g.slots; }
258 }
259 if (!s) {
260 omega_gpu_session_set_error("matmul kernel cache: every slot holds a prepared kernel (raise kernel_slots)");
261 return OMEGA_GPU_MATMUL_TOO_LARGE;
262 }
263 OmegaMatMulSpec spec;
264 /* the kernel ignores M; 16 keeps the spec inside the tile rule */
265 if (omega_matmul_spec_init(&spec, 16, kp, np, OMEGA_MATMUL_PRECISION_BF16) != 0) return OMEGA_GPU_MATMUL_TOO_LARGE;
266 OmegaBlackwellKernel kernel;
267 memset(&kernel, 0, sizeof kernel);
268 const bool blocked = rt > 1 || ct > 1;
269 /* the test unroll knob applies to the single-tile kernel; the blocked kernel keeps its own rule */
270 uint32_t unroll = blocked ? omega_matmul_frag_block_unroll(kp, rt, ct) : g.unroll ? g.unroll : omega_matmul_frag_unroll(kp);
271 /* the mutant drops the last pass, so it needs two: halve the unroll of a one-pass shape */
272 if (g.mutant && (blocked || !g.unroll)) while (unroll > 1 && kp / 16u / unroll < 2) unroll /= 2;
273 /* P07 cut C: the program is built in host memory and encoded straight into the device code buffer, sized
274 * exactly (omega_bw_program_code_bytes: the bytes the allocating generators produce, cut A.2 proves them
275 * identical). kernel.code then points into that buffer and owns nothing. */
276 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog);
277 if (!prog) { omega_gpu_session_set_error("matmul codegen: host memory"); return OMEGA_GPU_MATMUL_CODEGEN_FAIL; }
278 int cg = blocked ? omega_blackwell_codegen_matmul_frag_block_prog(&spec, grid_x, unroll, rt, ct, g.mutant, prog)
279 : omega_blackwell_codegen_matmul_frag_prog(&spec, grid_x, unroll, g.mutant, prog);
280 const size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0;
281 if (cg != 0 || need == 0) { omega_gpu_host_free(prog); return OMEGA_GPU_MATMUL_CODEGEN_FAIL; }
282 OmegaGpuMem code;
283 if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_MATMUL_CHIP_FAIL; }
284 cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel);
285 omega_gpu_host_free(prog);
286 if (cg != 0 || !kernel.code || kernel.code_size != need) { omega_gpu_session_free(&code); return OMEGA_GPU_MATMUL_CODEGEN_FAIL; }
287 if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_MATMUL_CHIP_FAIL; }
288 if (s->valid) omega_gpu_session_free(&s->code);
289 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;
290 s->spec = spec; s->kernel = kernel; s->code = code;
291 *out = s; *hit = false;
292 return OMEGA_GPU_MATMUL_OK;
293}
294
295/* ---- one launch (caller holds the lock, device open, not blocked) ---- */
296
297static int launch(const CacheSlot *ks, uint64_t a_va, uint64_t b_va, uint64_t c_va,
298 uint32_t grid_x, uint32_t grid_y, uint64_t *elapsed_ns, uint32_t *marker_out) {
299 uint32_t cbank_args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS];
300 omega_blackwell_build_cbank_args_matmul(cbank_args, a_va, b_va, c_va, grid_y * 16u * ks->rt, ks->kp, ks->np);
301 OmegaGpuLaunch L = {
302 .code_va = ks->code.va, .gpr_count = ks->kernel.gpr_count,
303 .threads_x = 32, .threads_y = 1, .grid_x = grid_x, .grid_y = grid_y,
304 .num_elements = (size_t)grid_x * grid_y * 128u,
305 .args = cbank_args, .n_args = OMEGA_BW_CBANK_MATMUL_ARGS_WORDS, .timeout_ms = MARKER_TIMEOUT_MS,
306 };
307 return omega_gpu_session_launch(&L, elapsed_ns, marker_out) == 0 ? OMEGA_GPU_MATMUL_OK : OMEGA_GPU_MATMUL_CHIP_FAIL;
308}
309
310/* ---- tensors ---- */
311
312void omega_gpu_tensor_shape(const OmegaGpuTensor *t, uint32_t *k, uint32_t *n) {
313 if (k) *k = t ? t->k : 0;
314 if (n) *n = t ? t->n : 0;
315}
316
317int omega_gpu_tensor_upload_bf16(uint32_t k, uint32_t n, const uint16_t *b, OmegaGpuTensor **out) {
318 if (out) *out = NULL;
319 if (!b || !out || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS;
320 if (k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) return OMEGA_GPU_MATMUL_TOO_LARGE;
321 OmegaGpuTensor *t = omega_gpu_host_calloc(1, sizeof *t);
322 if (!t) return OMEGA_GPU_MATMUL_BAD_ARGS;
323 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);
324 /* fragment order (omega_matmul_frag_pack_b), packed on the host FRAG_PACK_TILES column
325 * tiles at a time and copied in order into the uncached device buffer */
326 const uint32_t nt = t->np / 8u;
327 uint16_t *h = omega_gpu_host_alloc((size_t)FRAG_PACK_TILES * t->kp * 8u * sizeof *h);
328 if (!h) { omega_gpu_host_free(t); return OMEGA_GPU_MATMUL_BAD_ARGS; }
329 int rc = OMEGA_GPU_MATMUL_OK;
330 omega_gpu_session_lock();
331 if (!dev_open_locked() || omega_gpu_session_alloc((size_t)t->kp * t->np * 2u, &t->mem) != 0) rc = OMEGA_GPU_MATMUL_CHIP_FAIL;
332 omega_gpu_session_unlock();
333 if (rc != OMEGA_GPU_MATMUL_OK) { omega_gpu_host_free(h); omega_gpu_host_free(t); return rc; }
334 uint16_t *d = t->mem.cpu;
335 for (uint32_t nt0 = 0; nt0 < nt; nt0 += FRAG_PACK_TILES) {
336 const uint32_t nt1 = nt - nt0 < FRAG_PACK_TILES ? nt : nt0 + FRAG_PACK_TILES;
337 omega_matmul_frag_pack_b(b, k, n, t->kp, nt0, nt1, h);
338 memcpy(&d[(size_t)nt0 * t->kp * 8u], h, (size_t)(nt1 - nt0) * t->kp * 8u * sizeof *h);
339 }
340 omega_gpu_host_free(h);
341 if (omega_gpu_session_sync_to_gpu(&t->mem) != 0) { omega_gpu_tensor_free(t); return OMEGA_GPU_MATMUL_CHIP_FAIL; }
342 *out = t;
343 return OMEGA_GPU_MATMUL_OK;
344}
345
346int omega_gpu_tensor_upload_f32(uint32_t k, uint32_t n, const float *b, OmegaGpuTensor **out) {
347 if (out) *out = NULL;
348 if (!b || !out || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS;
349 if (k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) return OMEGA_GPU_MATMUL_TOO_LARGE;
350 size_t cnt = (size_t)k * n;
351 uint16_t *h = omega_gpu_host_alloc(cnt * sizeof *h);
352 if (!h) return OMEGA_GPU_MATMUL_BAD_ARGS;
353 for (size_t i = 0; i < cnt; i++) h[i] = omega_fp32_to_bf16(b[i]);
354 int rc = omega_gpu_tensor_upload_bf16(k, n, h, out);
355 omega_gpu_host_free(h);
356 return rc;
357}
358
359void omega_gpu_tensor_free(OmegaGpuTensor *t) {
360 if (!t) return;
361 omega_gpu_session_lock();
362 omega_gpu_session_free(&t->mem);
363 omega_gpu_session_unlock();
364 omega_gpu_host_free(t);
365}
366
367/* ---- the matmul ---- */
368
369/* a16 or a32 (exactly one non-NULL): the activation rows as bf16 or as f32, packed into fragment
370 * order in one pass either way (f32 rows are converted while packing, no staging copy) */
371static int matmul_core(uint32_t m, const uint16_t *a16, const float *a32, const OmegaGpuTensor *b, float *c, OmegaGpuMatmulInfo *info) {
372 const uint32_t k = b->k, n = b->n, kp = b->kp, np = b->np;
373 const uint32_t mp = round_up(m, OMEGA_GPU_MATMUL_TILE_M);
374 omega_gpu_session_lock();
375 int rc = OMEGA_GPU_MATMUL_OK;
376 if (!dev_open_locked()) { omega_gpu_session_unlock(); return OMEGA_GPU_MATMUL_CHIP_FAIL; }
377 uint32_t rows_per_launch, grid_x, rt, ct;
378 launch_shape(mp, np, &rows_per_launch, &grid_x, &rt, &ct);
379 uint32_t grid_y_full = rows_per_launch / 16u / rt;
380 if (info) {
381 info->m = m; info->k = k; info->n = n; info->padded_m = mp; info->padded_n = np; info->padded_k = kp;
382 info->k_slices = 1; info->rows_per_call = rows_per_launch; info->grid_x = grid_x; info->grid_y = grid_y_full;
383 info->resident = true; info->kernel_cache_hit = true;
384 (void)omega_gpu_fmt(info->target_chip, sizeof info->target_chip, "%s", "NVIDIA DGX Spark (Grace Blackwell GB10)");
385 info->sm_architecture = 121;
386 }
387 if (g.sealed && !(g.a_buf.reserved && g.c_buf.reserved)) {
388 /* sealed means no driver allocation on the call path; without a scratch reservation the
389 * buffers below could still grow, so refuse before touching them */
390 omega_gpu_session_set_error("serving reservation exceeded: matmul sealed without a scratch reservation (call omega_gpu_reserve_serving first)");
391 omega_gpu_session_unlock();
392 return OMEGA_GPU_MATMUL_TOO_LARGE;
393 }
394 int src = omega_gpu_session_scratch(&g.a_buf, (size_t)mp * kp * 2u);
395 if (src == 0) src = omega_gpu_session_scratch(&g.c_buf, (size_t)mp * np * 4u);
396 if (src != 0) {
397 omega_gpu_session_unlock();
398 /* a serving reservation refuses growth loudly (TOO_LARGE, error text names the sizes); anything else is a driver failure */
399 return src == OMEGA_GPU_SESSION_SCRATCH_RESERVED ? OMEGA_GPU_MATMUL_TOO_LARGE : OMEGA_GPU_MATMUL_CHIP_FAIL;
400 }
401 /* activation rows in, fragment order (omega_matmul_frag_pack_a / _f32: f32 rows are converted
402 * while packing, one pass), zero padded, packed on the host straight into the uncached buffer
403 * (the CPU side of that memory is ordinary cached memory; dsb below orders it before the launch);
404 * result rows poisoned. A one-row call (decode) writes row 0 only when the buffer already holds
405 * a full one-row pack of this width (rows 1..15 of every block zero): the 15 zero rows are not
406 * rewritten per token. */
407 uint16_t *ad = g.a_buf.mem.cpu;
408 const bool one_row = m == 1 && mp == 16u;
409 if (one_row && g.a_row0_ready && g.a_row0_kp == kp && g.a_row0_cpu == ad) {
410 if (a32) omega_matmul_frag_pack_a_row0_f32(a32, k, kp, ad);
411 else omega_matmul_frag_pack_a_row0(a16, k, kp, ad);
412 g.a_row0_hits++;
413 } else {
414 if (a32) omega_matmul_frag_pack_a_f32(a32, m, k, mp, kp, ad);
415 else omega_matmul_frag_pack_a(a16, m, k, mp, kp, ad);
416 g.a_row0_ready = one_row; g.a_row0_kp = kp; g.a_row0_cpu = ad;
417 }
418 /* result sentinels: one poison word per 16 x 8 fragment tile holding a real output word, at the tile's
419 * last real row and column (omega_matmul_result_sentinels). The kernel stores a tile as one straight-line
420 * run of STG.E words, so a tile whose sentinel survives was never stored. Poisoning and scanning every
421 * word was three passes over m x np (12.6 MB at m = 128, n = 24576: about 2 ms per prefill call, 2026-10-12). */
422 float *cd = g.c_buf.mem.cpu;
423 omega_matmul_result_sentinels(cd, m, n, np, POISON_F32, 1);
424 if (omega_gpu_session_sync_to_gpu(&g.a_buf.mem) != 0 || omega_gpu_session_sync_to_gpu(&g.c_buf.mem) != 0) rc = OMEGA_GPU_MATMUL_CHIP_FAIL;
425
426 for (uint32_t r0 = 0; r0 < mp && rc == OMEGA_GPU_MATMUL_OK; r0 += rows_per_launch) {
427 uint32_t rows = mp - r0 < rows_per_launch ? mp - r0 : rows_per_launch;
428 uint32_t grid_y = rows / 16u / rt;
429 CacheSlot *ks = NULL; bool hit = false;
430 rc = kernel_for(kp, np, grid_x, rt, ct, &ks, &hit);
431 if (rc != OMEGA_GPU_MATMUL_OK) break;
432 if (info && !hit) info->kernel_cache_hit = false;
433 rc = launch(ks, g.a_buf.mem.va + (uint64_t)r0 * kp * 2u, b->mem.va, g.c_buf.mem.va + (uint64_t)r0 * np * 4u,
434 grid_x, grid_y, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL);
435 if (info) info->chip_calls++;
436 }
437 if (rc == OMEGA_GPU_MATMUL_OK && omega_gpu_session_sync_to_cpu(&g.c_buf.mem) != 0) rc = OMEGA_GPU_MATMUL_CHIP_FAIL;
438 if (rc == OMEGA_GPU_MATMUL_OK) {
439 /* the chip must have stored every fragment tile with a real output word (sentinel check) */
440 size_t unchanged = omega_matmul_result_sentinels(cd, m, n, np, POISON_F32, 0);
441 if (unchanged && FAILAT("output poison survived")) rc = OMEGA_GPU_MATMUL_CHIP_FAIL;
442 else for (uint32_t r = 0; r < m; r++) memcpy(&c[(size_t)r * n], &cd[(size_t)r * np], (size_t)n * sizeof *c);
443 }
444 omega_gpu_session_unlock();
445 return rc;
446}
447
448static int resident_entry(uint32_t m, const uint16_t *a, const OmegaGpuTensor *b, float *c, OmegaGpuMatmulInfo *info) {
449 uint64_t t0 = now_ns();
450 if (info) memset(info, 0, sizeof *info);
451 if (!a || !b || !c || m == 0 || !b->mem.cpu) return OMEGA_GPU_MATMUL_BAD_ARGS;
452 if (m > OMEGA_BW_MATMUL_MAX_M) return OMEGA_GPU_MATMUL_TOO_LARGE;
453 int rc = matmul_core(m, a, NULL, b, c, info);
454 if (info) info->call_ns = now_ns() - t0;
455 return rc;
456}
457
458int omega_gpu_matmul_resident_bf16(uint32_t m, const uint16_t *a, const OmegaGpuTensor *b, float *c,
459 OmegaGpuMatmulInfo *info) {
460 return resident_entry(m, a, b, c, info);
461}
462
463int omega_gpu_matmul_resident_f32(uint32_t m, const float *a, const OmegaGpuTensor *b, float *c,
464 OmegaGpuMatmulInfo *info) {
465 uint64_t t0 = now_ns();
466 if (info) memset(info, 0, sizeof *info);
467 if (!a || !b || !c || m == 0 || !b->mem.cpu) return OMEGA_GPU_MATMUL_BAD_ARGS;
468 if (m > OMEGA_BW_MATMUL_MAX_M) return OMEGA_GPU_MATMUL_TOO_LARGE;
469 int rc = matmul_core(m, NULL, a, b, c, info);
470 if (info) info->call_ns = now_ns() - t0;
471 return rc;
472}
473
474/* Host oracle: double accumulation of the bf16 inputs; an element mismatches when
475 * its error exceeds ORACLE_TOL of the accumulation scale (sum of |terms|). */
476static void oracle(uint32_t m, uint32_t k, uint32_t n, const uint16_t *a, const uint16_t *b, const float *c, OmegaGpuMatmulInfo *info) {
477 float max_abs = 0.0f, max_rel = 0.0f;
478 size_t mism = 0;
479 for (uint32_t i = 0; i < m; i++) {
480 for (uint32_t j = 0; j < n; j++) {
481 double acc = 0.0, scale = 0.0;
482 for (uint32_t t = 0; t < k; t++) {
483 double p = (double)omega_bf16_to_fp32(a[(size_t)i * k + t]) * (double)omega_bf16_to_fp32(b[(size_t)t * n + j]);
484 acc += p; scale += abs_d(p);
485 }
486 double d = abs_d(acc - (double)c[(size_t)i * n + j]);
487 double rel = d / (scale > 1e-6 ? scale : 1e-6);
488 if ((float)d > max_abs) max_abs = (float)d;
489 if ((float)rel > max_rel) max_rel = (float)rel;
490 if (rel > ORACLE_TOL) mism++;
491 }
492 }
493 info->max_abs_err = max_abs; info->max_rel_err = max_rel; info->mismatch_count = mism;
494 info->oracle_ran = true; info->parity_verified = (mism == 0);
495}
496
497int omega_gpu_matmul_bf16(uint32_t m, uint32_t k, uint32_t n,
498 const uint16_t *a, const uint16_t *b, float *c,
499 OmegaGpuMatmulInfo *info) {
500 uint64_t t0 = now_ns();
501 OmegaGpuMatmulInfo local;
502 if (!info) info = &local;
503 memset(info, 0, sizeof *info);
504 if (!a || !b || !c || m == 0 || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS;
505 if (m > OMEGA_BW_MATMUL_MAX_M || k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N)
506 return OMEGA_GPU_MATMUL_TOO_LARGE;
507 OmegaGpuTensor *t = NULL;
508 int rc = omega_gpu_tensor_upload_bf16(k, n, b, &t);
509 if (rc != OMEGA_GPU_MATMUL_OK) return rc;
510 rc = matmul_core(m, a, NULL, t, c, info);
511 omega_gpu_tensor_free(t);
512 info->resident = false;
513 if (rc == OMEGA_GPU_MATMUL_OK && g.oracle) {
514 oracle(m, k, n, a, b, c, info);
515 if (!info->parity_verified) rc = OMEGA_GPU_MATMUL_PARITY_FAIL;
516 }
517 info->call_ns = now_ns() - t0;
518 return rc;
519}
520
521int omega_gpu_matmul_f32(uint32_t m, uint32_t k, uint32_t n,
522 const float *a, const float *b, float *c,
523 OmegaGpuMatmulInfo *info) {
524 if (info) memset(info, 0, sizeof *info);
525 if (!a || !b || !c || m == 0 || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS;
526 if (m > OMEGA_BW_MATMUL_MAX_M || k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N)
527 return OMEGA_GPU_MATMUL_TOO_LARGE;
528 size_t na = (size_t)m * k, nb = (size_t)k * n;
529 uint16_t *ab = omega_gpu_host_alloc(na * sizeof *ab);
530 uint16_t *bb = omega_gpu_host_alloc(nb * sizeof *bb);
531 if (!ab || !bb) { omega_gpu_host_free(ab); omega_gpu_host_free(bb); return OMEGA_GPU_MATMUL_BAD_ARGS; }
532 for (size_t i = 0; i < na; i++) ab[i] = omega_fp32_to_bf16(a[i]);
533 for (size_t i = 0; i < nb; i++) bb[i] = omega_fp32_to_bf16(b[i]);
534 int rc = omega_gpu_matmul_bf16(m, k, n, ab, bb, c, info);
535 omega_gpu_host_free(ab);
536 omega_gpu_host_free(bb);
537 return rc;
538}