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_pfhost.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 | /* omega_gpu_pfhost.c: launchers for the whole-prompt router logits and MoE combine (W-L1G-D). See omega_gpu_pfhost.h. */ |
| 18 | #include "omega_gpu_pfhost.h" |
| 19 | #include "omega_blackwell_codegen_pfhost.h" |
| 20 | #include <string.h> |
| 21 | |
| 22 | #define KIND_RL 0x50u |
| 23 | #define KIND_MC 0x51u |
| 24 | static int rl_mutant, mc_mutant; |
| 25 | static uint32_t rl_rows; |
| 26 | void omega_gpu_pf_router_test_set_mutant(int m) { rl_mutant = m; } |
| 27 | void omega_gpu_pf_combine_test_set_mutant(int m) { mc_mutant = m; } |
| 28 | void omega_gpu_pf_router_set_rows(uint32_t t) { rl_rows = t; } |
| 29 | uint32_t omega_gpu_pf_router_rows(void) { return rl_rows ? rl_rows : 16u; } |
| 30 | |
| 31 | size_t omega_gpu_pf_router_packed_bytes(uint32_t e, uint32_t k) { return (size_t)(k / 4u + 1u) * e * 16u; } |
| 32 | void omega_gpu_pf_router_pack(uint32_t e, uint32_t k, const float *w, float *wq) { |
| 33 | if (!w || !wq) return; |
| 34 | for (uint32_t g = 0; g < k / 4u; g++) |
| 35 | for (uint32_t ex = 0; ex < e; ex++) |
| 36 | for (uint32_t q = 0; q < 4u; q++) wq[((size_t)g * e + ex) * 4u + q] = w[(size_t)ex * k + 4u * g + q]; |
| 37 | memset(wq + (size_t)(k / 4u) * e * 4u, 0, (size_t)e * 16u); |
| 38 | } |
| 39 | |
| 40 | 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); } |
| 41 | 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); } |
| 42 | |
| 43 | 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) { |
| 44 | OmegaRlSpec s; memset(&s, 0, sizeof s); s.k = k; s.e = e; s.t = omega_gpu_pf_router_rows(); s.mutant = rl_mutant; |
| 45 | 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; |
| 46 | if (rows == 0) return OMEGA_GPU_PF_OK; |
| 47 | _Static_assert(sizeof(OmegaRlSpec) <= sizeof(((OmegaPfKey *)0)->words), "spec fits the key"); |
| 48 | OmegaPfKey key; memset(&key, 0, sizeof key); key.kind = KIND_RL; memcpy(key.words, &s, sizeof s); |
| 49 | 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) }; |
| 50 | omega_gpu_session_lock(); |
| 51 | int rc = omega_gpu_pf_open_locked(); |
| 52 | OmegaPfKernel kern; |
| 53 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_kernel_locked(&key, gen_rl, &kern); |
| 54 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_launch_locked(&kern, e, (rows + s.t - 1u) / s.t, 1, omega_rl_shared_bytes(&s), args, OMEGA_RL_ARGS_WORDS, ns); |
| 55 | omega_gpu_session_unlock(); |
| 56 | return rc; |
| 57 | } |
| 58 | |
| 59 | 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) { |
| 60 | OmegaMcSpec s; memset(&s, 0, sizeof s); s.h = h; s.top_k = top_k; s.mutant = mc_mutant; |
| 61 | if (!omega_mc_spec_ok(&s) || !down_va || !inv_va || !wt_va || !out_va) return OMEGA_GPU_PF_BAD_ARGS; |
| 62 | if ((down_va | inv_va | wt_va | out_va) & 15u) return OMEGA_GPU_PF_BAD_ARGS; |
| 63 | if (rows == 0) return OMEGA_GPU_PF_OK; |
| 64 | _Static_assert(sizeof(OmegaMcSpec) <= sizeof(((OmegaPfKey *)0)->words), "spec fits the key"); |
| 65 | OmegaPfKey key; memset(&key, 0, sizeof key); key.kind = KIND_MC; memcpy(key.words, &s, sizeof s); |
| 66 | 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), |
| 67 | (uint32_t)wt_va, (uint32_t)(wt_va >> 32), (uint32_t)out_va, (uint32_t)(out_va >> 32) }; |
| 68 | omega_gpu_session_lock(); |
| 69 | int rc = omega_gpu_pf_open_locked(); |
| 70 | OmegaPfKernel kern; |
| 71 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_kernel_locked(&key, gen_mc, &kern); |
| 72 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_launch_locked(&kern, h / 4u, rows, 1, 0, args, OMEGA_MC_ARGS_WORDS, ns); |
| 73 | omega_gpu_session_unlock(); |
| 74 | return rc; |
| 75 | } |
| 76 | |
| 77 | int omega_gpu_pf_router_logits_record(struct OmegaGpuCmdList *cl, uint32_t k, uint32_t e, uint32_t rows, |
| 78 | uint64_t x_va, uint64_t wq_va, uint64_t logits_va) { |
| 79 | OmegaRlSpec s; memset(&s, 0, sizeof s); s.k = k; s.e = e; s.t = omega_gpu_pf_router_rows(); s.mutant = rl_mutant; |
| 80 | if (!cl || !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; |
| 81 | if (rows == 0) return OMEGA_GPU_PF_OK; |
| 82 | OmegaPfKey key; memset(&key, 0, sizeof key); key.kind = KIND_RL; memcpy(key.words, &s, sizeof s); |
| 83 | 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) }; |
| 84 | int rc = omega_gpu_pf_open_locked(); |
| 85 | OmegaPfKernel kern; |
| 86 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_kernel_locked(&key, gen_rl, &kern); |
| 87 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_record_locked(cl, &kern, e, (rows + s.t - 1u) / s.t, 1, omega_rl_shared_bytes(&s), args, OMEGA_RL_ARGS_WORDS); |
| 88 | return rc; |
| 89 | } |
| 90 | |
| 91 | int omega_gpu_pf_moe_combine_record(struct OmegaGpuCmdList *cl, uint32_t h, uint32_t top_k, uint32_t rows, |
| 92 | uint64_t down_va, uint64_t inv_va, uint64_t wt_va, uint64_t out_va) { |
| 93 | OmegaMcSpec s; memset(&s, 0, sizeof s); s.h = h; s.top_k = top_k; s.mutant = mc_mutant; |
| 94 | if (!cl || !omega_mc_spec_ok(&s) || !down_va || !inv_va || !wt_va || !out_va) return OMEGA_GPU_PF_BAD_ARGS; |
| 95 | if ((down_va | inv_va | wt_va | out_va) & 15u) return OMEGA_GPU_PF_BAD_ARGS; |
| 96 | if (rows == 0) return OMEGA_GPU_PF_OK; |
| 97 | OmegaPfKey key; memset(&key, 0, sizeof key); key.kind = KIND_MC; memcpy(key.words, &s, sizeof s); |
| 98 | 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), |
| 99 | (uint32_t)wt_va, (uint32_t)(wt_va >> 32), (uint32_t)out_va, (uint32_t)(out_va >> 32) }; |
| 100 | int rc = omega_gpu_pf_open_locked(); |
| 101 | OmegaPfKernel kern; |
| 102 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_kernel_locked(&key, gen_mc, &kern); |
| 103 | if (rc == OMEGA_GPU_PF_OK) rc = omega_gpu_pf_record_locked(cl, &kern, h / 4u, rows, 1, 0, args, OMEGA_MC_ARGS_WORDS); |
| 104 | return rc; |
| 105 | } |