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_tc.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_tc.c: tensor-core kernels for the OWN-30B one-pass prefill (T3). |
| 18 | * Kernel emitters live in pure OM-2 (om/gpu/cg_tc.om); this file is the thin fail-closed C ABI wrapper. */ |
| 19 | #include "omega_blackwell_codegen_tc.h" |
| 20 | #include "omega_bw_om_ir.h" |
| 21 | #include "omega_native.h" |
| 22 | #include <string.h> |
| 23 | |
| 24 | int omega_blackwell_codegen_tc_probe_prog(int mutant, BlackwellIRProgram *prog) { |
| 25 | if (!prog) return -1; |
| 26 | omega_bw_ir_init(prog); |
| 27 | uint64_t sp[4] = { (uint64_t)(int64_t)mutant, 0, 0, 0 }; |
| 28 | uint64_t ins[8192], meta[512], rc = 1; |
| 29 | memset(ins, 0, sizeof ins); |
| 30 | memset(meta, 0, sizeof meta); |
| 31 | if (omega2_cg_tc_probe_emit(&rc, sp, 4u, ins, 8192u, meta, 512u) != 0 || rc != 0) return -1; |
| 32 | return omega_bw_ir_from_om(ins, meta, prog); |
| 33 | } |
| 34 | |
| 35 | static void tcgemm_pack_spec(const OmegaTcGemmSpec *s, uint64_t sp[8]) { |
| 36 | memset(sp, 0, 8u * sizeof(uint64_t)); |
| 37 | sp[0] = s->k; |
| 38 | sp[1] = s->rt; |
| 39 | sp[2] = s->ct; |
| 40 | sp[3] = s->warps; |
| 41 | sp[4] = (uint64_t)(int64_t)s->mutant; |
| 42 | } |
| 43 | |
| 44 | int omega_tcgemm_spec_ok(const OmegaTcGemmSpec *s) { |
| 45 | if (!s) return 0; |
| 46 | uint64_t sp[8], ok = 0; |
| 47 | tcgemm_pack_spec(s, sp); |
| 48 | if (omega2_cg_tcgemm_spec_ok(&ok, sp, 8u) != 0) return 0; |
| 49 | return ok != 0 ? 1 : 0; |
| 50 | } |
| 51 | |
| 52 | uint32_t omega_tcgemm_shared_bytes(const OmegaTcGemmSpec *s) { return omega_tcgemm_spec_ok(s) ? 2u * 16u * s->rt * 128u : 0u; } |
| 53 | uint32_t omega_tcgemm_span(const OmegaTcGemmSpec *s) { return omega_tcgemm_spec_ok(s) ? s->warps * 8u * s->ct : 0u; } |
| 54 | |
| 55 | static uint16_t bf16_rne(float f) { |
| 56 | uint32_t u; memcpy(&u, &f, 4); |
| 57 | if ((u & 0x7fffffffu) > 0x7f800000u) return (uint16_t)((u >> 16) | 0x40u); /* NaN stays NaN */ |
| 58 | u += 0x7fffu + ((u >> 16) & 1u); |
| 59 | return (uint16_t)(u >> 16); |
| 60 | } |
| 61 | void omega_tcgemm_permute_x(const float *x, uint32_t k, uint16_t *out) { |
| 62 | static const unsigned ord[4] = { 0, 2, 1, 3 }; |
| 63 | for (uint32_t q = 0; q + 4 <= k; q += 4) |
| 64 | for (unsigned i = 0; i < 4; i++) out[q + i] = bf16_rne(x[q + ord[i]] * 18446744073709551616.0f); |
| 65 | } |
| 66 | uint16_t omega_tcgemm_w_bf16(uint8_t c) { return (uint16_t)(((c & 0x7Fu) << 4) | ((c & 0x80u) << 8)); } |
| 67 | |
| 68 | int omega_blackwell_codegen_tcgemm_prog(const OmegaTcGemmSpec *spec, BlackwellIRProgram *prog) { |
| 69 | if (!prog) return -1; |
| 70 | omega_bw_ir_init(prog); |
| 71 | if (!spec) return -1; |
| 72 | uint64_t sp[8], ins[8192], meta[512], rc = 1; |
| 73 | memset(ins, 0, sizeof ins); |
| 74 | memset(meta, 0, sizeof meta); |
| 75 | tcgemm_pack_spec(spec, sp); |
| 76 | if (omega2_cg_tcgemm_emit(&rc, sp, 8u, ins, 8192u, meta, 512u) != 0 || rc != 0) return -1; |
| 77 | return omega_bw_ir_from_om(ins, meta, prog); |
| 78 | } |
| 79 | |
| 80 | int omega_blackwell_codegen_tc_peak_prog(uint32_t iters, uint32_t chains, uint32_t stall, uint32_t threads, BlackwellIRProgram *prog) { |
| 81 | if (!prog) return -1; |
| 82 | omega_bw_ir_init(prog); |
| 83 | uint64_t sp[4] = { iters, chains, stall, threads }; |
| 84 | uint64_t ins[8192], meta[512], rc = 1; |
| 85 | memset(ins, 0, sizeof ins); |
| 86 | memset(meta, 0, sizeof meta); |
| 87 | if (omega2_cg_tc_peak_emit(&rc, sp, 4u, ins, 8192u, meta, 512u) != 0 || rc != 0) return -1; |
| 88 | return omega_bw_ir_from_om(ins, meta, prog); |
| 89 | } |
| 90 | |
| 91 | int omega_blackwell_codegen_tc_bw_prog(uint32_t iters, uint32_t groups, uint32_t threads, BlackwellIRProgram *prog) { |
| 92 | if (!prog) return -1; |
| 93 | omega_bw_ir_init(prog); |
| 94 | uint64_t sp[4] = { iters, groups, threads, 0 }; |
| 95 | uint64_t ins[8192], meta[512], rc = 1; |
| 96 | memset(ins, 0, sizeof ins); |
| 97 | memset(meta, 0, sizeof meta); |
| 98 | if (omega2_cg_tc_bw_emit(&rc, sp, 4u, ins, 8192u, meta, 512u) != 0 || rc != 0) return -1; |
| 99 | return omega_bw_ir_from_om(ins, meta, prog); |
| 100 | } |