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_tgemm_api.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* omega_gpu_tgemm_api.c: the trainer's device-resident bf16 GEMM (lane E1, cut 1). See the header. */ |
| 3 | #include "omega_gpu_tgemm_api.h" |
| 4 | #include "omega_gpu_prefill.h" |
| 5 | #include "omega_api_shells.h" |
| 6 | #include "omega_api_views_rest.h" |
| 7 | #include <string.h> |
| 8 | |
| 9 | #define TG_TIMEOUT_MS 20000u /* the prefill module's launch limits (private there; same values) */ |
| 10 | #define TG_SPIN_US 200000u |
| 11 | static struct { uint32_t rt, warps, aqn; int mutant; OmegaGpuMem tab; } g; |
| 12 | void omega_gpu_tgemm_set_shape(uint32_t rt, uint32_t warps, uint32_t aqn) { g.rt = rt; g.warps = warps; g.aqn = aqn; } |
| 13 | void omega_gpu_tgemm_test_set_mutant(int m) { g.mutant = m; } |
| 14 | uint32_t omega_gpu_tgemm_bm(uint32_t n) { return omega_api_tgbm(g.rt, g.warps, n); } |
| 15 | _Static_assert(sizeof(OmegaTgemmTile) == OMEGA_TGEMM_TILE_BYTES, "tile entry"); |
| 16 | |
| 17 | int omega_gpu_tgemm_spec(const OmegaTgemmCall *c, OmegaTgemmSpec *out) { |
| 18 | if (!c || !out) return OMEGA_GPU_PF_BAD_ARGS; |
| 19 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 20 | OmegaTgemmSpec s; memset(&s, 0, sizeof s); |
| 21 | s.k = c->k; s.n = c->n; s.ct = 2; s.kc = 32; s.aqn = g.aqn ? g.aqn : 1u; s.layout = c->layout; s.epi = c->epi; s.mutant = g.mutant; |
| 22 | omega_api_tgspec(g.rt, g.warps, c->n, &s.rt, &s.warps); |
| 23 | #else |
| 24 | OmegaTgemmSpec s; |
| 25 | const uint32_t cfg[8] = {c->k, c->n, g.rt, g.warps, g.aqn, c->layout, c->epi, (uint32_t)g.mutant}; |
| 26 | uint64_t filled = 0; |
| 27 | ar_check(omega_artspec(&filled, cfg, 8, &s, 10)); |
| 28 | #endif |
| 29 | if (!omega_tgemm_spec_ok(&s)) return OMEGA_GPU_PF_BAD_ARGS; |
| 30 | if (!c->a_va || !c->b_va || !c->c_va) return OMEGA_GPU_PF_BAD_ARGS; |
| 31 | { |
| 32 | uint32_t sz = omega_api_tgsz(c->m, c->n, omega_tgemm_bm(&s)); |
| 33 | if (sz == 1u) return OMEGA_GPU_PF_BAD_ARGS; |
| 34 | if (sz == 2u) return OMEGA_GPU_PF_TOO_LARGE; |
| 35 | } |
| 36 | *out = s; |
| 37 | return OMEGA_GPU_PF_OK; |
| 38 | } |
| 39 | |
| 40 | uint32_t omega_gpu_tgemm_tiles(const OmegaTgemmCall *c, OmegaTgemmTile *tab, uint32_t max) { |
| 41 | OmegaTgemmSpec s; |
| 42 | if (!tab || omega_gpu_tgemm_spec(c, &s) != OMEGA_GPU_PF_OK) return 0; |
| 43 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 44 | const uint32_t bm = omega_tgemm_bm(&s), nt = c->m / bm; |
| 45 | if (nt > max) return 0; |
| 46 | for (uint32_t t = 0; t < nt; t++) { tab[t].c_va = c->c_va; tab[t].row0 = t * bm; tab[t].kb0 = 0; tab[t].nkb = c->k / 128u; tab[t].pad = 0; tab[t].pad2 = 0; } |
| 47 | return nt; |
| 48 | #else |
| 49 | uint64_t plan[32] = {2, c->m, c->n, c->k, omega_tgemm_bm(&s), omega_tgemm_span(&s), s.warps}; |
| 50 | if (ar_plan(plan) || plan[16] > max) return 0; |
| 51 | const uint64_t cfg[8] = {plan[16], c->c_va, omega_tgemm_bm(&s), plan[21]}; |
| 52 | uint64_t nt = 0; |
| 53 | ar_check(omega_artg(&nt, cfg, 8, tab, (uint64_t)max * 4)); |
| 54 | return (uint32_t)nt; |
| 55 | #endif |
| 56 | } |
| 57 | |
| 58 | static int gen_tgemm(const OmegaPfKey *key, void *prog) { OmegaTgemmSpec s; memcpy(&s, key->words, sizeof s); return omega_blackwell_codegen_tgemm_prog(&s, prog); } |
| 59 | |
| 60 | int omega_gpu_tgemm_fill_launch_locked(const OmegaTgemmCall *c, uint64_t tiles_va, uint32_t ntiles, OmegaGpuLaunch *L, uint32_t *args) { |
| 61 | OmegaTgemmSpec s; |
| 62 | if (!L || !args || !tiles_va || !ntiles) return OMEGA_GPU_PF_BAD_ARGS; |
| 63 | int rc = omega_gpu_tgemm_spec(c, &s); |
| 64 | if (rc != OMEGA_GPU_PF_OK) return rc; |
| 65 | OmegaPfKey key; |
| 66 | ar_key(&key, sizeof key / sizeof(uint32_t), OMEGA_TGEMM_KIND, &s, sizeof s / sizeof(uint32_t)); |
| 67 | _Static_assert(sizeof(OmegaTgemmSpec) <= sizeof key.words, "spec fits the key"); |
| 68 | OmegaPfKernel k; |
| 69 | rc = omega_gpu_pf_kernel_locked(&key, gen_tgemm, &k); |
| 70 | if (rc != OMEGA_GPU_PF_OK) return rc; |
| 71 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 72 | const uint32_t threads = 32u * s.warps; |
| 73 | #else |
| 74 | uint64_t plan[32] = {2, c->m, c->n, c->k, omega_tgemm_bm(&s), omega_tgemm_span(&s), s.warps}; |
| 75 | if (ar_plan(plan)) return OMEGA_GPU_PF_BAD_ARGS; |
| 76 | #endif |
| 77 | if (!omega_api_regfit(s.warps, k.gpr_declared)) return OMEGA_GPU_PF_TOO_LARGE; |
| 78 | if (!omega_bw_gpr_declared_ok(k.gpr_declared, k.gpr_peak)) return OMEGA_GPU_PF_CHIP_FAIL; |
| 79 | const uint64_t va[3] = { tiles_va, c->a_va, c->b_va }; |
| 80 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 81 | for (int i = 0; i < 3; i++) { args[2 * i] = (uint32_t)va[i]; args[2 * i + 1] = (uint32_t)(va[i] >> 32); } |
| 82 | #else |
| 83 | ar_args(args, OMEGA_TGEMM_ARGS_WORDS, va, 3); |
| 84 | #endif |
| 85 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 86 | memset(L, 0, sizeof *L); |
| 87 | L->code_va = k.code_va; L->gpr_count = k.gpr_declared; L->threads_x = threads; L->threads_y = 1; |
| 88 | L->grid_x = c->n / omega_tgemm_span(&s); L->grid_y = ntiles; L->num_elements = (size_t)threads * L->grid_x * L->grid_y; |
| 89 | L->args = args; L->n_args = OMEGA_TGEMM_ARGS_WORDS; L->timeout_ms = TG_TIMEOUT_MS; L->shared_bytes = omega_tgemm_shared_bytes(&s); L->spin_us = TG_SPIN_US; |
| 90 | #else |
| 91 | uint64_t elements[32] = {5, plan[18], plan[17], ntiles, 1}; |
| 92 | if (ar_plan(elements)) return OMEGA_GPU_PF_BAD_ARGS; |
| 93 | const uint64_t launch[16] = {k.code_va, k.gpr_declared, plan[18], 1, plan[17], ntiles, elements[17], |
| 94 | (uintptr_t)args, OMEGA_TGEMM_ARGS_WORDS, TG_TIMEOUT_MS, |
| 95 | omega_tgemm_shared_bytes(&s), TG_SPIN_US}; |
| 96 | ar_launch(L, launch); |
| 97 | #endif |
| 98 | return OMEGA_GPU_PF_OK; |
| 99 | } |
| 100 | |
| 101 | int omega_gpu_tgemm(const OmegaTgemmCall *c, uint64_t *ns) { |
| 102 | OmegaTgemmSpec s; |
| 103 | int rc = omega_gpu_tgemm_spec(c, &s); |
| 104 | if (rc != OMEGA_GPU_PF_OK) return rc; |
| 105 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 106 | const uint32_t nt = c->m / omega_tgemm_bm(&s); |
| 107 | const size_t need = ((size_t)nt * OMEGA_TGEMM_TILE_BYTES + 4095u) & ~(size_t)4095u; |
| 108 | #else |
| 109 | uint64_t plan[32] = {2, c->m, c->n, c->k, omega_tgemm_bm(&s), omega_tgemm_span(&s), s.warps}; |
| 110 | if (ar_plan(plan)) return OMEGA_GPU_PF_BAD_ARGS; |
| 111 | const uint32_t nt = (uint32_t)plan[16]; |
| 112 | const size_t need = (size_t)plan[20]; |
| 113 | #endif |
| 114 | OmegaGpuLaunch L; uint32_t args[OMEGA_TGEMM_ARGS_WORDS]; |
| 115 | /* the module table is shared state: (re)built only under the session lock, which also serializes the launch that reads it */ |
| 116 | omega_gpu_session_lock(); |
| 117 | rc = omega_gpu_pf_open_locked(); |
| 118 | if (rc == OMEGA_GPU_PF_OK && g.tab.size < need) { omega_gpu_session_free(&g.tab); if (omega_gpu_session_alloc(need, &g.tab) != 0) rc = OMEGA_GPU_PF_CHIP_FAIL; } |
| 119 | if (rc == OMEGA_GPU_PF_OK && omega_gpu_tgemm_tiles(c, g.tab.cpu, nt) != nt) rc = OMEGA_GPU_PF_BAD_ARGS; |
| 120 | if (rc == OMEGA_GPU_PF_OK && omega_gpu_session_sync_to_gpu(&g.tab) != 0) rc = OMEGA_GPU_PF_CHIP_FAIL; |
| 121 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_tgemm_fill_launch_locked(c, g.tab.va, nt, &L, args); |
| 122 | if (rc == OMEGA_GPU_PF_OK) { |
| 123 | uint64_t t = 0; |
| 124 | if (omega_gpu_session_launch(&L, &t, NULL) != 0) rc = OMEGA_GPU_PF_CHIP_FAIL; |
| 125 | else if (ns) *ns += t; |
| 126 | } |
| 127 | omega_gpu_session_unlock(); |
| 128 | return rc; |
| 129 | } |