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_moe_plan.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 | #include "omega_gpu_moe_plan.h" |
| 18 | #include "omega_gpu_pfhost.h" |
| 19 | #include "omega_gpu_fp8gemm_api.h" |
| 20 | #include "omega_native.h" |
| 21 | #include <string.h> |
| 22 | |
| 23 | int omega_moe_queue_init(OmegaMoeQueue *q, uint32_t cap) { |
| 24 | if (!q) return -1; |
| 25 | uint64_t rc = 1; |
| 26 | if (omega2_moe_queue_init(&rc, q->words, OMEGA_MOE_QUEUE_WORDS, cap) != OMEGA_OK || rc != 0) return -1; |
| 27 | return 0; |
| 28 | } |
| 29 | |
| 30 | int omega_moe_queue_enqueue(OmegaMoeQueue *q, uint32_t req_id, uint32_t tokens, uint32_t layer, uint32_t flags, uint32_t *slot_out) { |
| 31 | if (!q) return -1; |
| 32 | uint64_t slot = 0xffffffffu; |
| 33 | if (omega2_moe_queue_enqueue(&slot, q->words, OMEGA_MOE_QUEUE_WORDS, req_id, tokens, layer, flags) != OMEGA_OK || slot == 0xffffffffu) |
| 34 | return -1; |
| 35 | if (slot_out) *slot_out = (uint32_t)slot; |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | int omega_moe_queue_claim_batch(OmegaMoeQueue *q, uint32_t max_tokens, uint32_t out_slots[64], uint32_t *claimed_out) { |
| 40 | if (!q || !out_slots || !claimed_out) return -1; |
| 41 | uint64_t n = 0; |
| 42 | if (omega2_moe_queue_claim_batch(&n, q->words, OMEGA_MOE_QUEUE_WORDS, max_tokens, out_slots, 64u) != OMEGA_OK) |
| 43 | return -1; |
| 44 | *claimed_out = (uint32_t)n; |
| 45 | return 0; |
| 46 | } |
| 47 | |
| 48 | int omega_moe_queue_complete_batch(OmegaMoeQueue *q, uint32_t n, const uint32_t slots[64], uint32_t status) { |
| 49 | if (!q || !slots) return -1; |
| 50 | uint64_t rc = 1; |
| 51 | if (omega2_moe_queue_complete_batch(&rc, q->words, OMEGA_MOE_QUEUE_WORDS, n, slots, 64u, status) != OMEGA_OK || rc != 0) |
| 52 | return -1; |
| 53 | return 0; |
| 54 | } |
| 55 | |
| 56 | static void pack_cfg(const OmegaMoePlanConfig *c, uint64_t out[8]) { |
| 57 | out[0] = c->tokens; |
| 58 | out[1] = c->hidden; |
| 59 | out[2] = c->inter; |
| 60 | out[3] = c->experts; |
| 61 | out[4] = c->top_k; |
| 62 | out[5] = c->flags; |
| 63 | out[6] = 0; |
| 64 | out[7] = 0; |
| 65 | } |
| 66 | |
| 67 | int omega_moe_plan_layer(const OmegaMoePlanConfig *cfg, OmegaMoePlanOp out_ops[OMEGA_MOE_PLAN_MAX_OPS], uint32_t *n_ops) { |
| 68 | if (!cfg || !out_ops || !n_ops) return -1; |
| 69 | uint64_t cv[8]; |
| 70 | uint32_t raw[128]; |
| 71 | memset(raw, 0, sizeof raw); |
| 72 | pack_cfg(cfg, cv); |
| 73 | uint64_t k = 0; |
| 74 | if (omega2_moe_plan_layer(&k, cv, 8u, raw, 128u) != OMEGA_OK || k == 0 || k > OMEGA_MOE_PLAN_MAX_OPS) |
| 75 | return -1; |
| 76 | for (uint64_t i = 0; i < k; i++) { |
| 77 | out_ops[i].op = raw[4u * i + 0u]; |
| 78 | out_ops[i].mark_independent = raw[4u * i + 1u]; |
| 79 | out_ops[i].arg0 = raw[4u * i + 2u]; |
| 80 | out_ops[i].arg1 = raw[4u * i + 3u]; |
| 81 | } |
| 82 | *n_ops = (uint32_t)k; |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | int omega_moe_plan_traffic(const OmegaMoePlanConfig *cfg, uint32_t distinct_experts, OmegaMoeTraffic *out) { |
| 87 | if (!cfg || !out) return -1; |
| 88 | uint64_t cv[8], bytes[8], rc = 1; |
| 89 | memset(bytes, 0, sizeof bytes); |
| 90 | pack_cfg(cfg, cv); |
| 91 | if (omega2_moe_plan_traffic(&rc, cv, 8u, distinct_experts, bytes, 8u) != OMEGA_OK || rc != 0) |
| 92 | return -1; |
| 93 | out->router_bytes = bytes[0]; |
| 94 | out->gateup_bytes = bytes[1]; |
| 95 | out->down_bytes = bytes[2]; |
| 96 | out->combine_bytes = bytes[3]; |
| 97 | out->total_bytes = bytes[4]; |
| 98 | return 0; |
| 99 | } |
| 100 | |
| 101 | int omega_gpu_moe_plan_exec(OmegaGpuCmdList *cl, const OmegaGpuMoeExecArgs *args, OmegaGpuMoeExecStats *stats) { |
| 102 | if (!cl || !args) return -1; |
| 103 | OmegaMoePlanOp ops[OMEGA_MOE_PLAN_MAX_OPS]; |
| 104 | uint32_t n_ops = 0; |
| 105 | if (omega_moe_plan_layer(&args->cfg, ops, &n_ops) != 0) return -1; |
| 106 | |
| 107 | OmegaMoeTraffic traffic; |
| 108 | memset(&traffic, 0, sizeof traffic); |
| 109 | (void)omega_moe_plan_traffic(&args->cfg, args->distinct_experts, &traffic); |
| 110 | |
| 111 | if (stats) memset(stats, 0, sizeof *stats); |
| 112 | |
| 113 | const uint32_t rows = args->cfg.tokens * args->cfg.top_k; |
| 114 | omega_gpu_session_lock(); |
| 115 | omega_gpu_cmdlist_reset(cl); |
| 116 | |
| 117 | int rc = 0; |
| 118 | uint32_t recorded = 0; |
| 119 | for (uint32_t i = 0; i < n_ops && rc == 0; i++) { |
| 120 | const OmegaMoePlanOp *op = &ops[i]; |
| 121 | switch (op->op) { |
| 122 | case OMEGA_MOP_ROUTER_LOGITS: |
| 123 | rc = omega_gpu_pf_router_logits_record(cl, args->cfg.hidden, args->cfg.experts, args->cfg.tokens, |
| 124 | args->x_va, args->router_wq_va, args->logits_va); |
| 125 | break; |
| 126 | case OMEGA_MOP_EXPERT_GATEUP_GEMM: |
| 127 | if (!args->gateup_t) { rc = -1; break; } |
| 128 | rc = omega_gpu_fp8gemm_record(cl, args->gateup_t, rows, args->gateup_segs, args->gateup_seg_va, |
| 129 | args->gateup_xp_va, args->gateup_y_va, 0, 0, 0, 0, NULL); |
| 130 | break; |
| 131 | case OMEGA_MOP_EXPERT_DOWN_GEMM: |
| 132 | if (!args->down_t) { rc = -1; break; } |
| 133 | rc = omega_gpu_fp8gemm_record(cl, args->down_t, rows, args->down_segs, args->down_seg_va, |
| 134 | args->down_xp_va, args->down_y_va, 0, 0, 0, 0, NULL); |
| 135 | break; |
| 136 | case OMEGA_MOP_MOE_COMBINE: |
| 137 | rc = omega_gpu_pf_moe_combine_record(cl, args->cfg.hidden, args->cfg.top_k, args->cfg.tokens, |
| 138 | args->down_y_va, args->inv_va, args->wt_va, args->out_va); |
| 139 | break; |
| 140 | default: |
| 141 | rc = -1; |
| 142 | break; |
| 143 | } |
| 144 | if (rc == 0) { |
| 145 | if (op->mark_independent) omega_gpu_cmdlist_mark_independent(cl); |
| 146 | if (stats && recorded < OMEGA_MOE_PLAN_MAX_OPS) stats->stage_op[recorded] = op->op; |
| 147 | recorded++; |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | OmegaGpuCmdListStats cst; |
| 152 | memset(&cst, 0, sizeof cst); |
| 153 | uint64_t stamps[OMEGA_MOE_PLAN_MAX_OPS + 1]; |
| 154 | memset(stamps, 0, sizeof stamps); |
| 155 | int n_stamps = 0; |
| 156 | if (rc == 0) { |
| 157 | OmegaGpuCmdListOpts opts; |
| 158 | memset(&opts, 0, sizeof opts); |
| 159 | opts.mode = OMEGA_GPU_CMDLIST_PCAS; |
| 160 | opts.barrier = args->barrier; |
| 161 | opts.stamps = 1; |
| 162 | rc = omega_gpu_cmdlist_submit(cl, &opts, &cst); |
| 163 | if (rc == 0) n_stamps = omega_gpu_cmdlist_stamps(cl, stamps, OMEGA_MOE_PLAN_MAX_OPS + 1u); |
| 164 | } |
| 165 | omega_gpu_session_unlock(); |
| 166 | |
| 167 | if (rc != 0) return -1; |
| 168 | if (stats) { |
| 169 | stats->planned_ops = n_ops; |
| 170 | stats->launches = cst.launches; |
| 171 | stats->submits = cst.submits; |
| 172 | stats->chip_ns = cst.elapsed_ns; |
| 173 | stats->host_build_ns = cst.host_ns; |
| 174 | stats->traffic = traffic; |
| 175 | if (n_stamps == (int)(recorded + 1u)) { |
| 176 | for (uint32_t i = 0; i < recorded; i++) |
| 177 | stats->stage_ns[i] = stamps[i + 1u] >= stamps[i] ? (stamps[i + 1u] - stamps[i]) : 0u; |
| 178 | } |
| 179 | if (cst.elapsed_ns > 0) |
| 180 | stats->effective_gbps = (double)traffic.total_bytes / (double)cst.elapsed_ns; |
| 181 | } |
| 182 | return 0; |
| 183 | } |