interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / omega_blackwell_codegen_router.c

68 lines · 2834 bytes Raw Blame History
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_blackwell_codegen_router.c: the one-launch MoE router kernel.
18 * Implementation lives in pure OM-2 (om/gpu/cg_router.om); this file is the thin fail-closed C ABI wrapper. */
19#include "omega_blackwell_codegen_router.h"
20#include "omega_bw_om_ir.h"
21#include "omega_native.h"
22#include <string.h>
23
24static void router_pack_spec(const OmegaRouterSpec *s, uint64_t sp[8]) {
25 memset(sp, 0, 8u * sizeof(uint64_t));
26 sp[0] = s->warps;
27 sp[1] = s->depth;
28 sp[2] = (uint64_t)(int64_t)s->mutant;
29 sp[3] = s->experts;
30 sp[4] = s->shared;
31 sp[5] = s->xstride;
32 sp[6] = s->lbstride;
33}
34
35int omega_router_smem(const OmegaRouterSpec *spec, OmegaRouterSmem *m) {
36 if (!spec || !m) return -1;
37 const uint32_t ex = spec->experts ? spec->experts : OMEGA_ROUTER_EXPERTS;
38 if ((ex != 128u && ex != 256u) || spec->shared > 1u) return -1;
39 if (ex == OMEGA_ROUTER_EXPERTS && !spec->shared) {
40 m->logit = OMEGA_ROUTER_SM_LOGIT; m->red = OMEGA_ROUTER_SM_RED; m->bc = OMEGA_ROUTER_SM_BC;
41 m->sel = OMEGA_ROUTER_SM_SEL; m->selv = OMEGA_ROUTER_SM_SELV; m->total = OMEGA_ROUTER_SHARED_BYTES;
42 return 0;
43 }
44 m->logit = OMEGA_ROUTER_SM_XN + OMEGA_ROUTER_HIDDEN * 4u;
45 m->red = m->logit + (((ex + spec->shared) * 4u + 15u) & 0xfffffff0u);
46 m->bc = m->red + 128u; m->sel = m->bc + 16u; m->selv = m->sel + 48u; m->total = m->selv + 32u;
47 return 0;
48}
49
50int omega_blackwell_codegen_router_prog(const OmegaRouterSpec *spec, BlackwellIRProgram *prog) {
51 if (!prog) return -1;
52 omega_bw_ir_init(prog);
53 if (!spec) return -1;
54 uint64_t sp[8], ins[8192], meta[512], rc = 1;
55 memset(ins, 0, sizeof ins);
56 memset(meta, 0, sizeof meta);
57 router_pack_spec(spec, sp);
58 if (omega2_cg_router_emit(&rc, sp, 8u, ins, 8192u, meta, 512u) != 0 || rc != 0) return -1;
59 return omega_bw_ir_from_om(ins, meta, prog);
60}
61
62int omega_router_prog_stats(const OmegaRouterSpec *spec, uint32_t *insns, uint32_t *gprs) {
63 static BlackwellIRProgram prog;
64 if (omega_blackwell_codegen_router_prog(spec, &prog) != 0) return -1;
65 if (insns) *insns = (uint32_t)prog.count;
66 if (gprs) *gprs = prog.regalloc.peak_gpr_usage;
67 return 0;
68}