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_gemm.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_gemm.c: FP8 e4m3 block-scaled grouped GEMM for prefill. |
| 18 | * Spec validation, IR emission (om/gpu/cg_gemm.om), and stall scheduling |
| 19 | * (om/gpu/bw_sched.om) all run in pure seeded OM-2. */ |
| 20 | #include "omega_blackwell_codegen_gemm.h" |
| 21 | #include "omega_bw_om_ir.h" |
| 22 | #include "omega_native.h" |
| 23 | |
| 24 | static void gemm_pack_spec(const OmegaFp8GemmSpec *s, uint64_t sp[8]) { |
| 25 | sp[0] = s->k; |
| 26 | sp[1] = s->lane_bytes; |
| 27 | sp[2] = s->mt; |
| 28 | sp[3] = s->warps; |
| 29 | sp[4] = (uint64_t)(uint32_t)s->mutant; |
| 30 | sp[5] = s->rowmap; |
| 31 | sp[6] = 0; |
| 32 | sp[7] = 0; |
| 33 | } |
| 34 | |
| 35 | static int gemm_spec_ok(const OmegaFp8GemmSpec *s) { |
| 36 | if (!s) return 0; |
| 37 | uint64_t sp[8], ok = 0; |
| 38 | gemm_pack_spec(s, sp); |
| 39 | if (omega2_cg_gemm_ok(&ok, sp, 8u) != 0) return 0; |
| 40 | return ok != 0; |
| 41 | } |
| 42 | |
| 43 | uint32_t omega_fp8gemm_steps(const OmegaFp8GemmSpec *spec) { |
| 44 | return gemm_spec_ok(spec) ? spec->k / (32u * spec->lane_bytes) : 0u; |
| 45 | } |
| 46 | |
| 47 | uint32_t omega_fp8gemm_shared_bytes(const OmegaFp8GemmSpec *spec) { |
| 48 | return gemm_spec_ok(spec) ? spec->mt * 128u * spec->lane_bytes : 0u; |
| 49 | } |
| 50 | |
| 51 | int omega_blackwell_codegen_fp8gemm_prog(const OmegaFp8GemmSpec *spec, BlackwellIRProgram *prog) { |
| 52 | if (!spec || !prog) return -1; |
| 53 | omega_bw_ir_init(prog); |
| 54 | uint64_t sp[8], ins[8192], meta[512], rc = 1; |
| 55 | memset(ins, 0, sizeof ins); |
| 56 | memset(meta, 0, sizeof meta); |
| 57 | gemm_pack_spec(spec, sp); |
| 58 | if (omega2_cg_gemm_emit(&rc, sp, 8u, ins, 8192u, meta, 512u) != 0 || rc != 0) return -1; |
| 59 | if (omega_bw_ir_from_om(ins, meta, prog) != 0) { omega_bw_ir_init(prog); return -1; } |
| 60 | const size_t n = prog->count; |
| 61 | uint64_t phys[BW_MAX_VREGS], bsz[BW_MAX_VREGS]; |
| 62 | for (int v = 0; v < BW_MAX_VREGS; v++) { |
| 63 | phys[v] = (v < prog->regalloc.num_vregs) ? (uint64_t)(int64_t)prog->regalloc.vreg_to_phys[v] : (uint64_t)-1; |
| 64 | bsz[v] = (v < prog->regalloc.num_vregs && prog->regalloc.intervals[v].bundle_size) ? prog->regalloc.intervals[v].bundle_size : 1u; |
| 65 | } |
| 66 | uint64_t res = 1; |
| 67 | if (omega2_bw_sched_stalls(&res, ins, 8192u, phys, BW_MAX_VREGS, bsz, BW_MAX_VREGS, (uint64_t)n) != OMEGA_OK || res != 0) { |
| 68 | omega_bw_ir_init(prog); |
| 69 | return -1; |
| 70 | } |
| 71 | for (size_t i = 0; i < n; i++) prog->insns[i].control = (uint32_t)(ins[i * 8u + 6u] >> 32); |
| 72 | return 0; |
| 73 | } |
| 74 | |
| 75 | int omega_fp8gemm_prog_stats(const OmegaFp8GemmSpec *spec, uint32_t *insns, uint32_t *gprs) { |
| 76 | static BlackwellIRProgram prog; |
| 77 | if (omega_blackwell_codegen_fp8gemm_prog(spec, &prog) != 0) return -1; |
| 78 | if (insns) *insns = (uint32_t)prog.count; |
| 79 | if (gprs) *gprs = prog.regalloc.peak_gpr_usage; |
| 80 | return 0; |
| 81 | } |