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_blackwell_codegen_moe.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_blackwell_codegen_moe.c: FP8 e4m3 block-scaled GEMV kernel (OWN-30B Cut 6 / Cut 5). |
| 18 | * Spec validation, IR emission (om/gpu/cg_moe.om), and sched=1 stall scheduling |
| 19 | * (om/gpu/bw_sched.om) all run in pure seeded OM-2. */ |
| 20 | #include "omega_blackwell_codegen_moe.h" |
| 21 | #include "omega_bw_om_ir.h" |
| 22 | #include "omega_native.h" |
| 23 | #ifndef OMEGA_GPU_NO_POSIX |
| 24 | #include <stdlib.h> |
| 25 | #endif |
| 26 | |
| 27 | static void moe_pack_spec(const OmegaFp8GemvSpec *s, uint64_t sp[8]) { |
| 28 | sp[0] = s->k; |
| 29 | sp[1] = s->lane_bytes; |
| 30 | sp[2] = s->rows; |
| 31 | sp[3] = s->warps; |
| 32 | sp[4] = (uint64_t)(uint32_t)s->mutant; |
| 33 | sp[5] = s->depth; |
| 34 | sp[6] = s->sched; |
| 35 | sp[7] = (s->coal > 1u || s->wfirst > 1u) ? 4u : ((uint64_t)s->coal | ((uint64_t)s->wfirst << 1)); |
| 36 | } |
| 37 | |
| 38 | static int moe_spec_ok(const OmegaFp8GemvSpec *s) { |
| 39 | if (!s) return 0; |
| 40 | uint64_t sp[8], ok = 0; |
| 41 | moe_pack_spec(s, sp); |
| 42 | if (omega2_cg_moe_ok(&ok, sp, 8u) != 0) return 0; |
| 43 | return ok != 0; |
| 44 | } |
| 45 | |
| 46 | uint32_t omega_fp8_gemv_steps(const OmegaFp8GemvSpec *spec) { |
| 47 | return moe_spec_ok(spec) ? spec->k / (32u * spec->lane_bytes) : 0u; |
| 48 | } |
| 49 | |
| 50 | uint32_t omega_fp8_gemv_shared_bytes(const OmegaFp8GemvSpec *spec) { |
| 51 | if (!moe_spec_ok(spec)) return 0u; |
| 52 | uint32_t xsb = spec->depth ? spec->k * 4u + 16u : 0u; |
| 53 | uint32_t osb = spec->warps * spec->rows * 4u; |
| 54 | return xsb + (spec->coal ? 2u * osb : 0u); |
| 55 | } |
| 56 | |
| 57 | int omega_blackwell_codegen_fp8_gemv_prog(const OmegaFp8GemvSpec *spec, BlackwellIRProgram *prog) { |
| 58 | if (!spec || !prog) return -1; |
| 59 | omega_bw_ir_init(prog); |
| 60 | uint64_t sp[8], ins[8192], meta[512], rc = 1; |
| 61 | memset(ins, 0, sizeof ins); |
| 62 | memset(meta, 0, sizeof meta); |
| 63 | moe_pack_spec(spec, sp); |
| 64 | if (omega2_cg_moe_emit(&rc, sp, 8u, ins, 8192u, meta, 512u) != 0 || rc != 0) return -1; |
| 65 | if (omega_bw_ir_from_om(ins, meta, prog) != 0) { omega_bw_ir_init(prog); return -1; } |
| 66 | if (spec->sched) { |
| 67 | const size_t n = prog->count; |
| 68 | uint64_t phys[BW_MAX_VREGS], bsz[BW_MAX_VREGS]; |
| 69 | for (int v = 0; v < BW_MAX_VREGS; v++) { |
| 70 | phys[v] = (v < prog->regalloc.num_vregs) ? (uint64_t)(int64_t)prog->regalloc.vreg_to_phys[v] : (uint64_t)-1; |
| 71 | bsz[v] = (v < prog->regalloc.num_vregs && prog->regalloc.intervals[v].bundle_size) ? prog->regalloc.intervals[v].bundle_size : 1u; |
| 72 | } |
| 73 | uint64_t res = 1; |
| 74 | if (omega2_bw_sched_stalls(&res, ins, 8192u, phys, BW_MAX_VREGS, bsz, BW_MAX_VREGS, (uint64_t)n) != OMEGA_OK || res != 0) { |
| 75 | omega_bw_ir_init(prog); |
| 76 | return -1; |
| 77 | } |
| 78 | for (size_t i = 0; i < n; i++) prog->insns[i].control = (uint32_t)(ins[i * 8u + 6u] >> 32); |
| 79 | } |
| 80 | return 0; |
| 81 | } |
| 82 | |
| 83 | int omega_fp8_gemv_prog_stats(const OmegaFp8GemvSpec *spec, uint32_t *insns, uint32_t *gprs) { |
| 84 | static BlackwellIRProgram prog; |
| 85 | if (omega_blackwell_codegen_fp8_gemv_prog(spec, &prog) != 0) return -1; |
| 86 | if (insns) *insns = (uint32_t)prog.count; |
| 87 | if (gprs) *gprs = prog.regalloc.peak_gpr_usage; |
| 88 | return 0; |
| 89 | } |
| 90 | |
| 91 | #ifndef OMEGA_GPU_NO_POSIX |
| 92 | int omega_blackwell_codegen_fp8_gemv(const OmegaFp8GemvSpec *spec, OmegaBlackwellKernel *kernel) { |
| 93 | if (!kernel) return -1; |
| 94 | BlackwellIRProgram *prog = calloc(1, sizeof *prog); |
| 95 | if (!prog) return -1; |
| 96 | int rc = omega_blackwell_codegen_fp8_gemv_prog(spec, prog); |
| 97 | size_t need = rc == 0 ? omega_bw_program_code_bytes(prog) : 0; |
| 98 | uint8_t *buf = need ? malloc(need) : NULL; |
| 99 | if (!buf) { free(prog); return -1; } |
| 100 | rc = omega_blackwell_kernel_encode_into(prog, buf, need, kernel); |
| 101 | free(prog); |
| 102 | if (rc != 0) { free(buf); return -1; } |
| 103 | return 0; |
| 104 | } |
| 105 | #endif |