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