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_pfhost.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception */ |
| 2 | /* omega_gpu_pfhost.c: launchers for the whole-prompt router logits and MoE combine (W-L1G-D). See omega_gpu_pfhost.h. */ |
| 3 | #include "omega_gpu_pfhost.h" |
| 4 | #include "omega_blackwell_codegen_pfhost.h" |
| 5 | #include "omega_api_shells.h" |
| 6 | #include "omega_api_views_rest.h" |
| 7 | #include <string.h> |
| 8 | |
| 9 | #define KIND_RL 0x50u |
| 10 | #define KIND_MC 0x51u |
| 11 | static int rl_mutant, mc_mutant; |
| 12 | static uint32_t rl_rows; |
| 13 | void omega_gpu_pf_router_test_set_mutant(int m) { rl_mutant = m; } |
| 14 | void omega_gpu_pf_combine_test_set_mutant(int m) { mc_mutant = m; } |
| 15 | void omega_gpu_pf_router_set_rows(uint32_t t) { rl_rows = t; } |
| 16 | uint32_t omega_gpu_pf_router_rows(void) { return omega_api_rrows(rl_rows); } |
| 17 | |
| 18 | size_t omega_gpu_pf_router_packed_bytes(uint32_t e, uint32_t k) { return omega_api_pkbytes(e, k); } |
| 19 | void omega_gpu_pf_router_pack(uint32_t e, uint32_t k, const float *w, float *wq) { |
| 20 | if (!w || !wq) return; |
| 21 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 22 | for (uint32_t g = 0; g < k / 4u; g++) |
| 23 | for (uint32_t ex = 0; ex < e; ex++) |
| 24 | for (uint32_t q = 0; q < 4u; q++) wq[omega_api_rtdst(g, ex, q, e)] = w[omega_api_rtsrc(ex, k, g, q)]; |
| 25 | memset(wq + omega_api_rtdst(k / 4u, 0, 0, e), 0, (size_t)e * 16u); |
| 26 | #else |
| 27 | uint64_t r = 0; |
| 28 | ar_check(omega_arroute(&r, e, w, (uint64_t)e * k, wq, omega_api_pkbytes(e, k) / sizeof *wq)); |
| 29 | #endif |
| 30 | } |
| 31 | |
| 32 | static int gen_rl(const OmegaPfKey *key, void *prog) { OmegaRlSpec s; memcpy(&s, key->words, sizeof s); return omega_blackwell_codegen_rl_prog(&s, prog); } |
| 33 | static int gen_mc(const OmegaPfKey *key, void *prog) { OmegaMcSpec s; memcpy(&s, key->words, sizeof s); return omega_blackwell_codegen_mc_prog(&s, prog); } |
| 34 | |
| 35 | int omega_gpu_pf_router_logits(uint32_t k, uint32_t e, uint32_t rows, uint64_t x_va, uint64_t wq_va, uint64_t logits_va, uint64_t *ns) { |
| 36 | OmegaRlSpec s; |
| 37 | const uint32_t desc[] = {k, e, omega_gpu_pf_router_rows(), (uint32_t)rl_mutant}; |
| 38 | ar_words(&s, sizeof s / sizeof(uint32_t), desc, sizeof desc / sizeof desc[0]); |
| 39 | if (!omega_rl_spec_ok(&s) || !x_va || !wq_va || !logits_va || (x_va & 15u) || (wq_va & 15u) || (logits_va & 3u)) return OMEGA_GPU_PF_BAD_ARGS; |
| 40 | if (rows == 0) return OMEGA_GPU_PF_OK; |
| 41 | _Static_assert(sizeof(OmegaRlSpec) <= sizeof(((OmegaPfKey *)0)->words), "spec fits the key"); |
| 42 | OmegaPfKey key; |
| 43 | ar_key(&key, sizeof key / sizeof(uint32_t), KIND_RL, &s, sizeof s / sizeof(uint32_t)); |
| 44 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 45 | const uint32_t args[OMEGA_RL_ARGS_WORDS] = { (uint32_t)x_va, (uint32_t)(x_va >> 32), (uint32_t)wq_va, (uint32_t)(wq_va >> 32), (uint32_t)logits_va, (uint32_t)(logits_va >> 32) }; |
| 46 | #else |
| 47 | uint32_t args[OMEGA_RL_ARGS_WORDS]; |
| 48 | const uint64_t va[] = {x_va, wq_va, logits_va}; |
| 49 | ar_args(args, OMEGA_RL_ARGS_WORDS, va, 3); |
| 50 | #endif |
| 51 | omega_gpu_session_lock(); |
| 52 | int rc = omega_gpu_pf_open_locked(); |
| 53 | OmegaPfKernel kern; |
| 54 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_kernel_locked(&key, gen_rl, &kern); |
| 55 | uint64_t plan[32] = {1, rows, s.t, 0}; |
| 56 | ar_check((uint32_t)ar_plan(plan)); |
| 57 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_launch_locked(&kern, e, (uint32_t)plan[16], 1, omega_rl_shared_bytes(&s), args, OMEGA_RL_ARGS_WORDS, ns); |
| 58 | omega_gpu_session_unlock(); |
| 59 | return rc; |
| 60 | } |
| 61 | |
| 62 | int omega_gpu_pf_moe_combine(uint32_t h, uint32_t top_k, uint32_t rows, uint64_t down_va, uint64_t inv_va, uint64_t wt_va, uint64_t out_va, uint64_t *ns) { |
| 63 | OmegaMcSpec s; |
| 64 | const uint32_t desc[] = {h, top_k, (uint32_t)mc_mutant}; |
| 65 | ar_words(&s, sizeof s / sizeof(uint32_t), desc, sizeof desc / sizeof desc[0]); |
| 66 | if (!omega_mc_spec_ok(&s) || !down_va || !inv_va || !wt_va || !out_va) return OMEGA_GPU_PF_BAD_ARGS; |
| 67 | if ((down_va | inv_va | wt_va | out_va) & 15u) return OMEGA_GPU_PF_BAD_ARGS; |
| 68 | if (rows == 0) return OMEGA_GPU_PF_OK; |
| 69 | _Static_assert(sizeof(OmegaMcSpec) <= sizeof(((OmegaPfKey *)0)->words), "spec fits the key"); |
| 70 | OmegaPfKey key; |
| 71 | ar_key(&key, sizeof key / sizeof(uint32_t), KIND_MC, &s, sizeof s / sizeof(uint32_t)); |
| 72 | #if CUDAPP_MIGRATION_PENDING_API_VIEWS_REST |
| 73 | const uint32_t args[OMEGA_MC_ARGS_WORDS] = { (uint32_t)down_va, (uint32_t)(down_va >> 32), (uint32_t)inv_va, (uint32_t)(inv_va >> 32), |
| 74 | (uint32_t)wt_va, (uint32_t)(wt_va >> 32), (uint32_t)out_va, (uint32_t)(out_va >> 32) }; |
| 75 | #else |
| 76 | uint32_t args[OMEGA_MC_ARGS_WORDS]; |
| 77 | const uint64_t va[] = {down_va, inv_va, wt_va, out_va}; |
| 78 | ar_args(args, OMEGA_MC_ARGS_WORDS, va, 4); |
| 79 | #endif |
| 80 | omega_gpu_session_lock(); |
| 81 | int rc = omega_gpu_pf_open_locked(); |
| 82 | OmegaPfKernel kern; |
| 83 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_kernel_locked(&key, gen_mc, &kern); |
| 84 | uint64_t plan[32] = {1, 0, 1, h}; |
| 85 | ar_check((uint32_t)ar_plan(plan)); |
| 86 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_launch_locked(&kern, (uint32_t)plan[17], rows, 1, 0, args, OMEGA_MC_ARGS_WORDS, ns); |
| 87 | omega_gpu_session_unlock(); |
| 88 | return rc; |
| 89 | } |