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_bf16.c

86 lines · 3117 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_bf16.c: bf16 streaming GEMV kernel (the lm_head).
18 * Spec validation and IR emission run in pure seeded OM-2 (om/gpu/cg_bf16.om). */
19#include "omega_blackwell_codegen_bf16.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
26static void bf16_pack_spec(const OmegaBf16GemvSpec *s, uint64_t sp[8]) {
27 sp[0] = s->k;
28 sp[1] = s->rows;
29 sp[2] = s->warps;
30 sp[3] = s->depth;
31 sp[4] = (uint64_t)(uint32_t)s->mutant;
32 sp[5] = s->ystride;
33 sp[6] = s->xbatch;
34 sp[7] = s->mask;
35}
36
37static int bf16_spec_ok(const OmegaBf16GemvSpec *s) {
38 if (!s) return 0;
39 uint64_t sp[8], ok = 0;
40 bf16_pack_spec(s, sp);
41 if (omega2_cg_bf16_ok(&ok, sp, 8u) != 0) return 0;
42 return ok != 0;
43}
44
45uint32_t omega_bf16_gemv_steps(const OmegaBf16GemvSpec *spec) {
46 return bf16_spec_ok(spec) ? spec->k / 256u : 0u;
47}
48
49uint32_t omega_bf16_gemv_shared_bytes(const OmegaBf16GemvSpec *spec) {
50 return bf16_spec_ok(spec) ? spec->k * 4u * (spec->xbatch ? spec->xbatch : 1u) : 0u;
51}
52
53int omega_blackwell_codegen_bf16_gemv_prog(const OmegaBf16GemvSpec *spec, BlackwellIRProgram *prog) {
54 if (!spec || !prog) return -1;
55 omega_bw_ir_init(prog);
56 uint64_t sp[8], ins[8192], meta[512], rc = 1;
57 memset(ins, 0, sizeof ins);
58 memset(meta, 0, sizeof meta);
59 bf16_pack_spec(spec, sp);
60 if (omega2_cg_bf16_emit(&rc, sp, 8u, ins, 8192u, meta, 512u) != 0 || rc != 0) return -1;
61 return omega_bw_ir_from_om(ins, meta, prog);
62}
63
64int omega_bf16_gemv_prog_stats(const OmegaBf16GemvSpec *spec, uint32_t *insns, uint32_t *gprs) {
65 static BlackwellIRProgram prog;
66 if (omega_blackwell_codegen_bf16_gemv_prog(spec, &prog) != 0) return -1;
67 if (insns) *insns = (uint32_t)prog.count;
68 if (gprs) *gprs = prog.regalloc.peak_gpr_usage;
69 return 0;
70}
71
72#ifndef OMEGA_GPU_NO_POSIX
73int omega_blackwell_codegen_bf16_gemv(const OmegaBf16GemvSpec *spec, OmegaBlackwellKernel *kernel) {
74 if (!kernel) return -1;
75 BlackwellIRProgram *prog = calloc(1, sizeof *prog);
76 if (!prog) return -1;
77 int rc = omega_blackwell_codegen_bf16_gemv_prog(spec, prog);
78 size_t need = rc == 0 ? omega_bw_program_code_bytes(prog) : 0;
79 uint8_t *buf = need ? malloc(need) : NULL;
80 if (!buf) { free(prog); return -1; }
81 rc = omega_blackwell_kernel_encode_into(prog, buf, need, kernel);
82 free(prog);
83 if (rc != 0) { free(buf); return -1; }
84 return 0;
85}
86#endif