Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/CUDApp/blob/main/src/omega_gpu_moe_api.c # 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 Sign in to star 0 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `CUDApp / src / omega_gpu_moe_api.c` 520 lines · 24081 bytes Raw Blame History 1 /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ 2 /* 3 * omega_gpu_moe_api.c: column windows over a resident bf16 tensor (OWN-30B cut 2). See the header. 4 * 5 * Same submission path, buffers, poison check and latch rules as omega_gpu_matmul_api.c (FB-1 cut 1b 6 * on the shared session), restated here rather than shared because that file is owned by the 8B 7 * decode-speed lane and its tensor type is opaque; this file only ADDS exports. Every buffer both 8 * sides touch is GPU-uncached (nvos.h NVOS32_ATTR2_GPU_CACHEABLE: "For system memory this will not 9 * be coherent with direct CPU mappings"). 10 * 11 * The window rule: B is in fragment order (omega_matmul_frag_pack_b), column tile nt = kp x 8 12 * elements contiguous at element nt * kp * 8. A window starting at column tile t0 of width wnp 13 * (padded to 8) is therefore the chip-proven kernel for (kp, wnp) with b_va advanced by 14 * t0 * kp * 8 * 2 bytes. Nothing else about the launch changes. 15 */ 16 #include "omega_gpu_moe_api.h" 17 #include "omega_api_shells.h" 18 #include "omega_api_views_rest.h" 19 #include "omega_gpu_session.h" 20 #include "omega_gpu_fmt.h" 21 #include "omega_blackwell_codegen.h" 22 #include "omega_blackwell_matmul.h" 23 #include "omega_blackwell_qmd.h" 24 #include 25 26 #define MOE_CACHE_SLOTS 16 /* default kernel cache slots; raised only by omega_gpu_moe_reserve */ 27 #define MOE_CACHE_SLOTS_MAX 128 28 #define FRAG_PACK_TILES 64u /* column tiles per host packing chunk at upload */ 29 #define MARKER_TIMEOUT_MS 20000ull 30 #define POISON_F32 (-999.0f) 31 32 struct OmegaGpuMoeTensor { 33 uint32_t k, n, kp, np; 34 OmegaGpuMem mem; /* kp x np bf16 in fragment order */ 35 }; 36 37 typedef struct { 38 bool valid, pinned; 39 uint32_t kp, np, grid_x, rt, ct; 40 int mutant; 41 OmegaBlackwellKernel kernel; 42 OmegaGpuMem code; 43 } MoeCacheSlot; 44 _Static_assert(sizeof(int) == sizeof(uint32_t), "cache mutant is one key word"); 45 _Static_assert(offsetof(MoeCacheSlot, mutant) == offsetof(MoeCacheSlot, kp) + 5 * sizeof(uint32_t), "cache key is six contiguous words"); 46 47 static struct { 48 OmegaGpuScratch a_buf, c_buf; 49 bool a_row0_ready; uint32_t a_row0_kp; const void *a_row0_cpu; 50 uint64_t a_row0_hits; 51 uint32_t cta_budget; 52 int mutant; 53 int32_t skew; /* test only: column tiles added to every window's B base */ 54 bool hooked; 55 MoeCacheSlot cache[MOE_CACHE_SLOTS_MAX]; 56 unsigned cache_next, slots; 57 int sealed; 58 } g = { .cta_budget = OMEGA_GPU_MATMUL_MAX_CTAS, .slots = MOE_CACHE_SLOTS }; 59 60 #define FAILAT(s) (omega_gpu_session_set_error(s), 1) 61 static uint64_t now_ns(void) { return omega_gpu_session_now_ns(); } 62 63 uint64_t omega_gpu_moe_window_byte_offset(uint32_t kp, uint32_t col0) { 64 return omega_api_woff(kp, col0); 65 } 66 67 /* ---- device-side state (caller holds the session lock) ---- */ 68 69 static void cache_clear_locked(void) { 70 for (unsigned i = 0; i < MOE_CACHE_SLOTS_MAX; i++) { 71 MoeCacheSlot *s = &g.cache[i]; 72 if (!s->valid) continue; 73 omega_gpu_session_free(&s->code); 74 memset(s, 0, sizeof *s); 75 } 76 g.cache_next = 0; 77 } 78 79 static void on_session_close(void) { 80 cache_clear_locked(); 81 g.a_row0_ready = false; 82 omega_gpu_session_scratch_free(&g.a_buf); 83 omega_gpu_session_scratch_free(&g.c_buf); 84 g.slots = MOE_CACHE_SLOTS; 85 g.sealed = 0; 86 } 87 88 static OmegaGpuSession *dev_open_locked(void) { 89 if (!g.hooked) { (void)omega_gpu_session_on_close(on_session_close); g.hooked = true; } 90 return omega_gpu_session_open(); 91 } 92 93 void omega_gpu_moe_cache_clear(void) { 94 omega_gpu_session_lock(); 95 cache_clear_locked(); 96 omega_gpu_session_unlock(); 97 } 98 99 void omega_gpu_moe_set_cta_budget(uint32_t ctas) { 100 omega_gpu_session_lock(); 101 g.cta_budget = ctas ? ctas : OMEGA_GPU_MATMUL_MAX_CTAS; 102 cache_clear_locked(); 103 omega_gpu_session_unlock(); 104 } 105 106 uint32_t omega_gpu_moe_cta_budget(void) { return g.cta_budget; } 107 108 void omega_gpu_moe_test_set_mutant(int on) { 109 omega_gpu_session_lock(); 110 g.mutant = on ? 1 : 0; 111 cache_clear_locked(); 112 omega_gpu_session_unlock(); 113 } 114 115 void omega_gpu_moe_test_set_window_skew(int32_t tiles) { 116 omega_gpu_session_lock(); 117 g.skew = tiles; 118 omega_gpu_session_unlock(); 119 } 120 121 uint64_t omega_gpu_moe_test_a_row0_hits(void) { return g.a_row0_hits; } 122 123 /* ---- launch shape and kernel cache (the matmul API's rules, restated) ---- */ 124 125 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, uint32_t *grid_y) { 126 #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST 127 const uint32_t budget = g.cta_budget ? g.cta_budget : OMEGA_GPU_MATMUL_MAX_CTAS, mt = mp / 16u, nt = np / 8u; 128 omega_matmul_frag_block_shape(mt, nt, rt, ct); 129 const uint32_t rb = mt / *rt, nb = nt / *ct; 130 *rows_per_launch = rb <= budget ? mp : budget * 16u * *rt; 131 uint32_t gx = budget / (*rows_per_launch / 16u / *rt); 132 if (gx == 0) gx = 1; 133 if (gx > nb) gx = nb; 134 *grid_x = gx; 135 *grid_y = *rows_per_launch / 16u / *rt; 136 #else 137 uint64_t q[32] = {9, mp, np, g.cta_budget ? g.cta_budget : OMEGA_GPU_MATMUL_MAX_CTAS}; 138 ar_check((uint32_t)ar_plan(q)); 139 omega_matmul_frag_block_shape((uint32_t)q[16], (uint32_t)q[17], rt, ct); 140 q[4] = *rt; q[5] = *ct; 141 ar_check((uint32_t)ar_plan(q)); 142 *rows_per_launch = (uint32_t)q[18]; *grid_x = (uint32_t)q[19]; *grid_y = (uint32_t)q[20]; 143 #endif 144 } 145 146 static int kernel_for(uint32_t kp, uint32_t np, uint32_t grid_x, uint32_t rt, uint32_t ct, MoeCacheSlot **out, bool *hit) { 147 #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST 148 for (unsigned i = 0; i < g.slots; i++) { 149 MoeCacheSlot *s = &g.cache[i]; 150 if (s->valid && s->kp == kp && s->np == np && s->grid_x == grid_x && s->rt == rt && s->ct == ct && s->mutant == g.mutant) { 151 *out = s; *hit = true; 152 return OMEGA_GPU_MATMUL_OK; 153 } 154 } 155 #else 156 const uint32_t key[6] = {kp, np, grid_x, rt, ct, (uint32_t)g.mutant}; 157 size_t found = ar_cache(g.cache, sizeof g.cache, g.slots, sizeof g.cache[0], 158 offsetof(MoeCacheSlot, valid), offsetof(MoeCacheSlot, kp), key, sizeof key); 159 if (found != SIZE_MAX) { 160 *out = &g.cache[found]; *hit = true; 161 return OMEGA_GPU_MATMUL_OK; 162 } 163 #endif 164 if (g.sealed) { 165 char e[160]; 166 omega_gpu_fmt(e, sizeof e, "serving reservation exceeded: moe window kernel kp=%u np=%u grid_x=%u rt=%u ct=%u was not prepared", kp, np, grid_x, rt, ct); 167 omega_gpu_session_set_error(e); 168 return OMEGA_GPU_MATMUL_TOO_LARGE; 169 } 170 MoeCacheSlot *s = NULL; 171 #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST 172 for (unsigned i = 0; i < g.slots && !s; i++) { 173 MoeCacheSlot *c = &g.cache[(g.cache_next + i) % g.slots]; 174 if (!c->valid || !c->pinned) { s = c; g.cache_next = (unsigned)((c - g.cache) + 1) % g.slots; } 175 } 176 #else 177 uint64_t vq[8] = {g.slots, sizeof g.cache[0], offsetof(MoeCacheSlot, valid), 178 offsetof(MoeCacheSlot, pinned), g.cache_next}; 179 uint64_t vi = UINT64_MAX; 180 ar_check(omega_arvictim(&vi, vq, 8, g.cache, sizeof g.cache)); 181 if (vi != UINT64_MAX) { s = &g.cache[vi]; g.cache_next = (unsigned)vq[4]; } 182 #endif 183 if (!s) { 184 omega_gpu_session_set_error("moe window kernel cache: every slot holds a prepared kernel (raise kernel_slots)"); 185 return OMEGA_GPU_MATMUL_TOO_LARGE; 186 } 187 OmegaMatMulSpec spec; 188 if (omega_matmul_spec_init(&spec, 16, kp, np, OMEGA_MATMUL_PRECISION_BF16) != 0) return OMEGA_GPU_MATMUL_TOO_LARGE; 189 OmegaBlackwellKernel kernel; 190 memset(&kernel, 0, sizeof kernel); 191 const bool blocked = rt > 1 || ct > 1; 192 uint32_t unroll = blocked ? omega_matmul_frag_block_unroll(kp, rt, ct) : omega_matmul_frag_unroll(kp); 193 #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST 194 if (g.mutant) while (unroll > 1 && kp / 16u / unroll < 2) unroll /= 2; 195 #else 196 uint64_t uq[32] = {11, kp, unroll, (uint64_t)g.mutant}; 197 ar_check((uint32_t)ar_plan(uq)); 198 unroll = (uint32_t)uq[16]; 199 #endif 200 BlackwellIRProgram *prog = omega_gpu_host_calloc(1, sizeof *prog); 201 if (!prog) { omega_gpu_session_set_error("moe window codegen: host memory"); return OMEGA_GPU_MATMUL_CODEGEN_FAIL; } 202 int cg = blocked ? omega_blackwell_codegen_matmul_frag_block_prog(&spec, grid_x, unroll, rt, ct, g.mutant, prog) 203 : omega_blackwell_codegen_matmul_frag_prog(&spec, grid_x, unroll, g.mutant, prog); 204 const size_t need = cg == 0 ? omega_bw_program_code_bytes(prog) : 0; 205 if (cg != 0 || need == 0) { omega_gpu_host_free(prog); return OMEGA_GPU_MATMUL_CODEGEN_FAIL; } 206 OmegaGpuMem code; 207 if (omega_gpu_session_alloc_code(need, &code) != 0) { omega_gpu_host_free(prog); return OMEGA_GPU_MATMUL_CHIP_FAIL; } 208 cg = omega_blackwell_kernel_encode_into(prog, code.cpu, need, &kernel); 209 omega_gpu_host_free(prog); 210 if (cg != 0 || !kernel.code || kernel.code_size != need) { omega_gpu_session_free(&code); return OMEGA_GPU_MATMUL_CODEGEN_FAIL; } 211 if (omega_gpu_session_sync_to_gpu(&code) != 0) { omega_gpu_session_free(&code); return OMEGA_GPU_MATMUL_CHIP_FAIL; } 212 if (s->valid) omega_gpu_session_free(&s->code); 213 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; 214 s->kernel = kernel; s->code = code; 215 *out = s; *hit = false; 216 return OMEGA_GPU_MATMUL_OK; 217 } 218 219 static int launch(const MoeCacheSlot *ks, uint64_t a_va, uint64_t b_va, uint64_t c_va, 220 uint32_t grid_x, uint32_t grid_y, uint64_t *elapsed_ns, uint32_t *marker_out) { 221 uint32_t cbank_args[OMEGA_BW_CBANK_MATMUL_ARGS_WORDS]; 222 uint64_t p[32] = {10, grid_x, grid_y, ks->rt}; 223 ar_check((uint32_t)ar_plan(p)); 224 omega_blackwell_build_cbank_args_matmul(cbank_args, a_va, b_va, c_va, (uint32_t)p[16], ks->kp, ks->np); 225 OmegaGpuLaunch L; 226 const uint64_t lq[16] = {ks->code.va, omega_bw_gpr_declared(ks->kernel.gpr_count), 227 32, 1, grid_x, grid_y, p[17], (uintptr_t)cbank_args, 228 OMEGA_BW_CBANK_MATMUL_ARGS_WORDS, MARKER_TIMEOUT_MS}; 229 ar_launch(&L, lq); 230 if (!omega_bw_gpr_declared_ok(L.gpr_count, ks->kernel.gpr_count)) { omega_gpu_session_set_error("declared registers below peak + 2: refusing to launch"); return OMEGA_GPU_MATMUL_CHIP_FAIL; } 231 return omega_gpu_session_launch(&L, elapsed_ns, marker_out) == 0 ? OMEGA_GPU_MATMUL_OK : OMEGA_GPU_MATMUL_CHIP_FAIL; 232 } 233 234 /* ---- serving reservation ---- */ 235 236 int omega_gpu_moe_reserve(uint32_t max_rows, uint32_t max_k, uint32_t max_cols, uint32_t kernel_slots) { 237 if (max_rows == 0 || max_k == 0 || max_cols == 0) { omega_gpu_session_set_error("moe reserve: zero bound"); return OMEGA_GPU_MATMUL_BAD_ARGS; } 238 if (max_rows > OMEGA_BW_MATMUL_MAX_M || max_k > OMEGA_BW_MATMUL_MAX_K || max_cols > OMEGA_BW_MATMUL_MAX_N || kernel_slots > MOE_CACHE_SLOTS_MAX) { 239 omega_gpu_session_set_error("moe reserve: bound past the matmul envelope"); 240 return OMEGA_GPU_MATMUL_TOO_LARGE; 241 } 242 uint64_t dims[32] = {12, max_rows, max_k, max_cols}; 243 ar_check((uint32_t)ar_plan(dims)); 244 const size_t c_bytes = dims[20]; 245 omega_gpu_session_lock(); 246 int rc = OMEGA_GPU_MATMUL_OK; 247 if (!dev_open_locked()) rc = OMEGA_GPU_MATMUL_CHIP_FAIL; 248 else if (omega_gpu_session_scratch_reserve(&g.a_buf, dims[19]) != 0 || omega_gpu_session_scratch_reserve(&g.c_buf, c_bytes) != 0) { 249 omega_gpu_session_scratch_unreserve(&g.a_buf); omega_gpu_session_scratch_unreserve(&g.c_buf); 250 rc = OMEGA_GPU_MATMUL_CHIP_FAIL; 251 } else if (kernel_slots > g.slots) { 252 cache_clear_locked(); 253 g.slots = kernel_slots; 254 } 255 g.a_row0_ready = false; 256 omega_gpu_session_unlock(); 257 return rc; 258 } 259 260 void omega_gpu_moe_unreserve(void) { 261 omega_gpu_session_lock(); 262 omega_gpu_session_scratch_unreserve(&g.a_buf); 263 omega_gpu_session_scratch_unreserve(&g.c_buf); 264 omega_gpu_session_unlock(); 265 } 266 267 int omega_gpu_moe_prepare(uint32_t m, uint32_t k, uint32_t cols) { 268 if (m == 0 || k == 0 || cols == 0) { omega_gpu_session_set_error("moe prepare: zero dimension"); return OMEGA_GPU_MATMUL_BAD_ARGS; } 269 if (m > OMEGA_BW_MATMUL_MAX_M || k > OMEGA_BW_MATMUL_MAX_K || cols > OMEGA_BW_MATMUL_MAX_N) { 270 omega_gpu_session_set_error("moe prepare: shape past the matmul envelope"); 271 return OMEGA_GPU_MATMUL_TOO_LARGE; 272 } 273 uint64_t dims[32] = {12, m, k, cols}; 274 ar_check((uint32_t)ar_plan(dims)); 275 const uint32_t mp = (uint32_t)dims[16], kp = (uint32_t)dims[17], np = (uint32_t)dims[18]; 276 uint32_t rows_per_launch, grid_x, rt, ct, grid_y; 277 omega_gpu_session_lock(); 278 int rc = OMEGA_GPU_MATMUL_OK; 279 if (!dev_open_locked()) rc = OMEGA_GPU_MATMUL_CHIP_FAIL; 280 else { 281 launch_shape(mp, np, &rows_per_launch, &grid_x, &rt, &ct, &grid_y); 282 MoeCacheSlot *s = NULL; bool hit = false; 283 rc = kernel_for(kp, np, grid_x, rt, ct, &s, &hit); 284 if (rc == OMEGA_GPU_MATMUL_OK) s->pinned = true; 285 } 286 omega_gpu_session_unlock(); 287 return rc; 288 } 289 290 void omega_gpu_moe_seal(int on) { 291 omega_gpu_session_lock(); 292 g.sealed = on ? 1 : 0; 293 omega_gpu_session_unlock(); 294 } 295 296 /* ---- tensors ---- */ 297 298 void omega_gpu_moe_tensor_shape(const OmegaGpuMoeTensor *t, uint32_t *k, uint32_t *n) { 299 if (k) *k = t ? t->k : 0; 300 if (n) *n = t ? t->n : 0; 301 } 302 303 int omega_gpu_moe_tensor_upload_bf16(uint32_t k, uint32_t n, const uint16_t *b, OmegaGpuMoeTensor **out) { 304 if (out) *out = NULL; 305 if (!b || !out || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS; 306 if (k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) return OMEGA_GPU_MATMUL_TOO_LARGE; 307 OmegaGpuMoeTensor *t = omega_gpu_host_calloc(1, sizeof *t); 308 if (!t) return OMEGA_GPU_MATMUL_BAD_ARGS; 309 uint64_t dims[32] = {12, 1, k, n}; 310 ar_check((uint32_t)ar_plan(dims)); 311 t->k = k; t->n = n; t->kp = (uint32_t)dims[17]; t->np = (uint32_t)dims[18]; 312 const uint32_t nt = (uint32_t)dims[24]; 313 uint16_t *h = omega_gpu_host_alloc(dims[25]); 314 if (!h) { omega_gpu_host_free(t); return OMEGA_GPU_MATMUL_BAD_ARGS; } 315 int rc = OMEGA_GPU_MATMUL_OK; 316 omega_gpu_session_lock(); 317 if (!dev_open_locked() || omega_gpu_session_alloc(dims[21], &t->mem) != 0) rc = OMEGA_GPU_MATMUL_CHIP_FAIL; 318 omega_gpu_session_unlock(); 319 if (rc != OMEGA_GPU_MATMUL_OK) { omega_gpu_host_free(h); omega_gpu_host_free(t); return rc; } 320 uint16_t *d = t->mem.cpu; 321 #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST 322 for (uint32_t nt0 = 0; nt0 < nt; nt0 += FRAG_PACK_TILES) { 323 const uint32_t nt1 = nt - nt0 < FRAG_PACK_TILES ? nt : nt0 + FRAG_PACK_TILES; 324 omega_matmul_frag_pack_b(b, k, n, t->kp, nt0, nt1, h); 325 memcpy(&d[(size_t)nt0 * t->kp * 8u], h, (size_t)(nt1 - nt0) * t->kp * 8u * sizeof *h); 326 } 327 #else 328 uint64_t step[16] = {0, nt, t->kp, FRAG_PACK_TILES}; 329 uint64_t more = 0, packed = 0; 330 for (;;) { 331 ar_check(omega_armstep(&more, step, 16, NULL, 0)); 332 if (!more) break; 333 uint64_t p[12] = {2, 0, k, 0, t->kp, n, step[8], step[9]}; 334 ar_check(omega_cg_pklen(&packed, p, 12)); 335 if (packed) abort(); 336 ar_check(omega_cg_pkh(&packed, p, 12, b, p[8], h, p[9])); 337 if (packed) abort(); 338 ar_copy(&d[step[10]], h, step[12]); 339 } 340 #endif 341 omega_gpu_host_free(h); 342 if (omega_gpu_session_sync_to_gpu(&t->mem) != 0) { omega_gpu_moe_tensor_free(t); return OMEGA_GPU_MATMUL_CHIP_FAIL; } 343 *out = t; 344 return OMEGA_GPU_MATMUL_OK; 345 } 346 347 int omega_gpu_moe_tensor_upload_f32(uint32_t k, uint32_t n, const float *b, OmegaGpuMoeTensor **out) { 348 if (out) *out = NULL; 349 if (!b || !out || k == 0 || n == 0) return OMEGA_GPU_MATMUL_BAD_ARGS; 350 if (k > OMEGA_BW_MATMUL_MAX_K || n > OMEGA_BW_MATMUL_MAX_N) return OMEGA_GPU_MATMUL_TOO_LARGE; 351 uint64_t dims[32] = {12, 1, k, n}; 352 ar_check((uint32_t)ar_plan(dims)); 353 size_t cnt = dims[22]; 354 uint16_t *h = omega_gpu_host_alloc(dims[23]); 355 if (!h) return OMEGA_GPU_MATMUL_BAD_ARGS; 356 #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST 357 for (size_t i = 0; i < cnt; i++) h[i] = omega_fp32_to_bf16(b[i]); 358 #else 359 uint64_t converted = 0; 360 ar_check(omega_ar_bf16(&converted, b, cnt, h, cnt)); 361 #endif 362 int rc = omega_gpu_moe_tensor_upload_bf16(k, n, h, out); 363 omega_gpu_host_free(h); 364 return rc; 365 } 366 367 void omega_gpu_moe_tensor_free(OmegaGpuMoeTensor *t) { 368 if (!t) return; 369 omega_gpu_session_lock(); 370 omega_gpu_session_free(&t->mem); 371 omega_gpu_session_unlock(); 372 omega_gpu_host_free(t); 373 } 374 375 /* ---- the window matmul ---- */ 376 377 /* The alignment rule, fail-closed, with the stage named. */ 378 static int window_args_ok(const OmegaGpuMoeTensor *t, uint32_t col0, uint32_t cols) { 379 char e[160]; 380 if (omega_api_wok(col0, cols, t->n)) return 1; 381 if (col0 % OMEGA_GPU_MATMUL_TILE_N != 0) { 382 omega_gpu_fmt(e, sizeof e, "moe window: col0=%u is not a multiple of %u (a window starts on a column tile)", col0, OMEGA_GPU_MATMUL_TILE_N); 383 omega_gpu_session_set_error(e); 384 return 0; 385 } 386 omega_gpu_fmt(e, sizeof e, "moe window: [%u, %u + %u) is not inside the tensor's %u columns", col0, col0, cols, t->n); 387 omega_gpu_session_set_error(e); 388 return 0; 389 } 390 391 /* a16 or a32 (exactly one non-NULL). nwin windows of `cols` columns at col0[w]; c receives nwin 392 * blocks of m x cols. The activation pack and the kernel are shared by every window of the call. */ 393 static int windows_core(uint32_t m, const uint16_t *a16, const float *a32, const OmegaGpuMoeTensor *t, 394 uint32_t nwin, const uint32_t *col0, uint32_t cols, float *c, OmegaGpuMatmulInfo *info) { 395 const uint32_t k = t->k, kp = t->kp; 396 uint64_t dims[32] = {12, m, k, cols}; 397 ar_check((uint32_t)ar_plan(dims)); 398 const uint32_t mp = (uint32_t)dims[16], np = (uint32_t)dims[18]; 399 uint64_t valid[16] = {0, nwin, cols, t->n}, bad = 0; 400 valid[15] = 3; 401 ar_check(omega_armstep(&bad, valid, 16, col0, nwin)); 402 if (bad != nwin) { 403 (void)window_args_ok(t, col0[bad], cols); 404 return OMEGA_GPU_MATMUL_BAD_ARGS; 405 } 406 omega_gpu_session_lock(); 407 int rc = OMEGA_GPU_MATMUL_OK; 408 if (!dev_open_locked()) { omega_gpu_session_unlock(); return OMEGA_GPU_MATMUL_CHIP_FAIL; } 409 uint32_t rows_per_launch, grid_x, rt, ct, grid_y; 410 launch_shape(mp, np, &rows_per_launch, &grid_x, &rt, &ct, &grid_y); 411 if (info) { 412 info->m = m; info->k = k; info->n = cols; info->padded_m = mp; info->padded_n = np; info->padded_k = kp; 413 info->k_slices = 1; info->rows_per_call = rows_per_launch; info->grid_x = grid_x; info->grid_y = grid_y; 414 info->resident = true; info->kernel_cache_hit = true; 415 (void)omega_gpu_fmt(info->target_chip, sizeof info->target_chip, "%s", "NVIDIA DGX Spark (Grace Blackwell GB10)"); 416 info->sm_architecture = 121; 417 } 418 if (g.sealed && !(g.a_buf.reserved && g.c_buf.reserved)) { 419 omega_gpu_session_set_error("serving reservation exceeded: moe window sealed without a scratch reservation (call omega_gpu_moe_reserve first)"); 420 omega_gpu_session_unlock(); 421 return OMEGA_GPU_MATMUL_TOO_LARGE; 422 } 423 int src = omega_gpu_session_scratch(&g.a_buf, dims[19]); 424 if (src == 0) src = omega_gpu_session_scratch(&g.c_buf, dims[20]); 425 if (src != 0) { 426 omega_gpu_session_unlock(); 427 return src == OMEGA_GPU_SESSION_SCRATCH_RESERVED ? OMEGA_GPU_MATMUL_TOO_LARGE : OMEGA_GPU_MATMUL_CHIP_FAIL; 428 } 429 uint16_t *ad = g.a_buf.mem.cpu; 430 const bool one_row = m == 1 && mp == 16u; 431 const bool reuse = one_row && g.a_row0_ready && g.a_row0_kp == kp && g.a_row0_cpu == ad; 432 #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST 433 if (reuse) { 434 if (a32) omega_matmul_frag_pack_a_row0_f32(a32, k, kp, ad); 435 else omega_matmul_frag_pack_a_row0(a16, k, kp, ad); 436 } else { 437 if (a32) omega_matmul_frag_pack_a_f32(a32, m, k, mp, kp, ad); 438 else omega_matmul_frag_pack_a(a16, m, k, mp, kp, ad); 439 } 440 #else 441 uint64_t ap[12] = {a32 ? (reuse ? 4 : 3) : (reuse ? 1 : 0), m, k, mp, kp}; 442 uint64_t packed = 0; 443 ar_check(omega_cg_pklen(&packed, ap, 12)); 444 if (packed) abort(); 445 ar_check(omega_cg_pkh(&packed, ap, 12, a32 ? (const void *)a32 : (const void *)a16, ap[8], ad, ap[9])); 446 if (packed) abort(); 447 #endif 448 if (reuse) g.a_row0_hits++; 449 else { g.a_row0_ready = one_row; g.a_row0_kp = kp; g.a_row0_cpu = ad; } 450 if (omega_gpu_session_sync_to_gpu(&g.a_buf.mem) != 0) rc = OMEGA_GPU_MATMUL_CHIP_FAIL; 451 float *cd = g.c_buf.mem.cpu; 452 uint64_t ws[16] = {0, nwin, kp, m, cols, t->mem.va, (uint64_t)(int64_t)g.skew}; 453 ws[15] = 1; 454 while (rc == OMEGA_GPU_MATMUL_OK) { 455 uint64_t more = 0; 456 ar_check(omega_armstep(&more, ws, 16, col0, nwin)); 457 if (!more) break; 458 const uint64_t b_va = ws[9]; 459 omega_matmul_result_sentinels(cd, m, cols, np, POISON_F32, 1); 460 if (omega_gpu_session_sync_to_gpu(&g.c_buf.mem) != 0) { rc = OMEGA_GPU_MATMUL_CHIP_FAIL; break; } 461 uint64_t rs[16] = {0, mp, rows_per_launch, rt, kp, np, g.a_buf.mem.va, g.c_buf.mem.va}; 462 rs[15] = 2; 463 while (rc == OMEGA_GPU_MATMUL_OK) { 464 uint64_t more = 0; 465 ar_check(omega_armstep(&more, rs, 16, NULL, 0)); 466 if (!more) break; 467 uint32_t grid_y = (uint32_t)rs[9]; 468 MoeCacheSlot *ks = NULL; bool hit = false; 469 rc = kernel_for(kp, np, grid_x, rt, ct, &ks, &hit); 470 if (rc != OMEGA_GPU_MATMUL_OK) break; 471 if (info && !hit) info->kernel_cache_hit = false; 472 rc = launch(ks, rs[10], b_va, rs[11], 473 grid_x, grid_y, info ? &info->elapsed_ns : NULL, info ? &info->completion_marker : NULL); 474 if (info) info->chip_calls++; 475 } 476 if (rc != OMEGA_GPU_MATMUL_OK) break; 477 if (omega_gpu_session_sync_to_cpu(&g.c_buf.mem) != 0) { rc = OMEGA_GPU_MATMUL_CHIP_FAIL; break; } 478 size_t unchanged = omega_matmul_result_sentinels(cd, m, cols, np, POISON_F32, 0); 479 if (unchanged && FAILAT("output poison survived")) { rc = OMEGA_GPU_MATMUL_CHIP_FAIL; break; } 480 float *cw = c + ws[10]; 481 #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST 482 for (uint32_t r = 0; r < m; r++) memcpy(&cw[(size_t)r * cols], &cd[(size_t)r * np], (size_t)cols * sizeof *c); 483 #endif 484 #if !CUDAPP_MIGRATION_PENDING_API_VIEWS_REST 485 uint64_t rq[3] = {m, cols, np}, compacted = 0; 486 ar_check(omega_mrow(&compacted, rq, 3, cd, (uint64_t)m * np, cw, (uint64_t)m * cols)); 487 if (compacted) abort(); 488 #endif 489 } 490 omega_gpu_session_unlock(); 491 return rc; 492 } 493 494 static int entry(uint32_t m, const uint16_t *a16, const float *a32, const OmegaGpuMoeTensor *t, 495 uint32_t nwin, const uint32_t *col0, uint32_t cols, float *c, OmegaGpuMatmulInfo *info) { 496 uint64_t t0 = now_ns(); 497 if (info) memset(info, 0, sizeof *info); 498 if ((!a16 && !a32) || !t || !c || !col0 || m == 0 || nwin == 0 || !t->mem.cpu) return OMEGA_GPU_MATMUL_BAD_ARGS; 499 if (nwin > OMEGA_GPU_MOE_MAX_WINDOWS) { omega_gpu_session_set_error("moe window: too many windows in one call"); return OMEGA_GPU_MATMUL_BAD_ARGS; } 500 if (m > OMEGA_BW_MATMUL_MAX_M) return OMEGA_GPU_MATMUL_TOO_LARGE; 501 int rc = windows_core(m, a16, a32, t, nwin, col0, cols, c, info); 502 if (info) info->call_ns = now_ns() - t0; 503 return rc; 504 } 505 506 int omega_gpu_matmul_resident_window_f32(uint32_t m, const float *a, const OmegaGpuMoeTensor *t, 507 uint32_t col0, uint32_t cols, float *c, OmegaGpuMatmulInfo *info) { 508 return entry(m, NULL, a, t, 1, &col0, cols, c, info); 509 } 510 511 int omega_gpu_matmul_resident_window_bf16(uint32_t m, const uint16_t *a, const OmegaGpuMoeTensor *t, 512 uint32_t col0, uint32_t cols, float *c, OmegaGpuMatmulInfo *info) { 513 return entry(m, a, NULL, t, 1, &col0, cols, c, info); 514 } 515 516 int omega_gpu_matmul_resident_windows_f32(uint32_t m, const float *a, const OmegaGpuMoeTensor *t, 517 uint32_t nwin, const uint32_t *col0, uint32_t cols, float *c, 518 OmegaGpuMatmulInfo *info) { 519 return entry(m, NULL, a, t, nwin, col0, cols, c, info); 520 }