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_moe2.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_moe2.c: FP8 e4m3 block-scaled GEMV, variant 2 (row-pipelined). |
| 18 | * Spec validation and IR emission run in pure seeded OM-2 (om/gpu/cg_moe2.om). */ |
| 19 | #include "omega_blackwell_codegen_moe2.h" |
| 20 | #include "omega_bw_om_ir.h" |
| 21 | #include "omega_native.h" |
| 22 | #ifndef OMEGA_GPU_NO_POSIX |
| 23 | #include <stdlib.h> |
| 24 | #endif |
| 25 | |
| 26 | static void moe2_pack_spec(const OmegaFp8GemvSpec *s, uint64_t sp[8]) { |
| 27 | sp[0] = s->k; |
| 28 | sp[1] = s->lane_bytes; |
| 29 | sp[2] = s->rows; |
| 30 | sp[3] = s->warps; |
| 31 | sp[4] = (uint64_t)(uint32_t)s->mutant; |
| 32 | sp[5] = 0; |
| 33 | sp[6] = 0; |
| 34 | sp[7] = 0; |
| 35 | } |
| 36 | |
| 37 | int omega_fp8_gemv2_spec_ok(const OmegaFp8GemvSpec *s) { |
| 38 | if (!s) return 0; |
| 39 | uint64_t sp[8], ok = 0; |
| 40 | moe2_pack_spec(s, sp); |
| 41 | if (omega2_cg_moe2_ok(&ok, sp, 8u) != 0) return 0; |
| 42 | return ok != 0; |
| 43 | } |
| 44 | |
| 45 | uint32_t omega_fp8_gemv2_shared_bytes(const OmegaFp8GemvSpec *spec) { |
| 46 | if (!omega_fp8_gemv2_spec_ok(spec)) return 0u; |
| 47 | return spec->k * 4u + (spec->k % (32u * spec->warps) ? 16u : 0u); |
| 48 | } |
| 49 | |
| 50 | int omega_blackwell_codegen_fp8_gemv2_prog(const OmegaFp8GemvSpec *spec, BlackwellIRProgram *prog) { |
| 51 | if (!spec || !prog) return -1; |
| 52 | omega_bw_ir_init(prog); |
| 53 | uint64_t sp[8], ins[8192], meta[512], rc = 1; |
| 54 | memset(ins, 0, sizeof ins); |
| 55 | memset(meta, 0, sizeof meta); |
| 56 | moe2_pack_spec(spec, sp); |
| 57 | if (omega2_cg_moe2_emit(&rc, sp, 8u, ins, 8192u, meta, 512u) != 0 || rc != 0) return -1; |
| 58 | return omega_bw_ir_from_om(ins, meta, prog); |
| 59 | } |
| 60 | |
| 61 | int omega_fp8_gemv2_prog_stats(const OmegaFp8GemvSpec *spec, uint32_t *insns, uint32_t *gprs) { |
| 62 | static BlackwellIRProgram prog; |
| 63 | if (omega_blackwell_codegen_fp8_gemv2_prog(spec, &prog) != 0) return -1; |
| 64 | if (insns) *insns = (uint32_t)prog.count; |
| 65 | if (gprs) *gprs = prog.regalloc.peak_gpr_usage; |
| 66 | return 0; |
| 67 | } |
| 68 | |
| 69 | #ifndef OMEGA_GPU_NO_POSIX |
| 70 | int omega_blackwell_codegen_fp8_gemv2(const OmegaFp8GemvSpec *spec, OmegaBlackwellKernel *kernel) { |
| 71 | if (!kernel) return -1; |
| 72 | BlackwellIRProgram *prog = calloc(1, sizeof *prog); |
| 73 | if (!prog) return -1; |
| 74 | int rc = omega_blackwell_codegen_fp8_gemv2_prog(spec, prog); |
| 75 | size_t need = rc == 0 ? omega_bw_program_code_bytes(prog) : 0; |
| 76 | uint8_t *buf = need ? malloc(need) : NULL; |
| 77 | if (!buf) { free(prog); return -1; } |
| 78 | rc = omega_blackwell_kernel_encode_into(prog, buf, need, kernel); |
| 79 | free(prog); |
| 80 | if (rc != 0) { free(buf); return -1; } |
| 81 | return 0; |
| 82 | } |
| 83 | #endif |