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