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_gdn.c
| 1 | /* SPDX-License-Identifier: Apache-2.0 |
| 2 | * Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | * |
| 4 | * omega_blackwell_codegen_gdn.c: thin C ABI wrapper over the pure OM-2 |
| 5 | * Gated DeltaNet emitter in om/gpu/cg_gdn.om. |
| 6 | */ |
| 7 | #include "omega_blackwell_codegen_gdn.h" |
| 8 | #include "omega_bw_om_ir.h" |
| 9 | #include "omega_native.h" |
| 10 | |
| 11 | int omega_gdn_shape(const OmegaGdnSpec *spec, OmegaGdnShape *s) { |
| 12 | if (!spec || !s) return -1; |
| 13 | uint32_t r = spec->ratio ? spec->ratio : 3u; |
| 14 | if (r < 1u || r > 3u) return -1; |
| 15 | s->r = r; |
| 16 | s->vh = OMEGA_GDN_KEY_HEADS * r; |
| 17 | s->vdim = s->vh * OMEGA_GDN_HEAD_DIM; |
| 18 | s->conv = 2u * OMEGA_GDN_KEY_HEADS * OMEGA_GDN_HEAD_DIM + s->vdim; |
| 19 | s->par_scal = s->conv * OMEGA_GDN_TAPS * 4u; |
| 20 | s->par_norm = s->par_scal + s->vh * 8u; |
| 21 | s->par_bytes = s->par_norm + OMEGA_GDN_HEAD_DIM * 4u; |
| 22 | s->tail_bytes = s->vh * OMEGA_GDN_TAIL_HEAD_BYTES; |
| 23 | s->state_bytes = spec->state_bf16 ? s->vh * (OMEGA_GDN_STATE_HEAD_BYTES / 2u) : s->vh * OMEGA_GDN_STATE_HEAD_BYTES; |
| 24 | s->post_grid_y = s->vh / 8u; |
| 25 | return 0; |
| 26 | } |
| 27 | |
| 28 | static int gdn_emit_kind(uint64_t kind, const OmegaGdnSpec *spec, BlackwellIRProgram *prog, uint32_t gpr_limit) { |
| 29 | if (!prog) return -1; |
| 30 | omega_bw_ir_init(prog); |
| 31 | if (!spec) return -1; |
| 32 | static uint64_t ins[8192], meta[512]; |
| 33 | uint64_t sp[8] = { |
| 34 | kind, |
| 35 | (uint64_t)(uint32_t)spec->mutant, |
| 36 | (uint64_t)(uint32_t)spec->copy, |
| 37 | (uint64_t)spec->ratio, |
| 38 | (uint64_t)spec->state_bf16, |
| 39 | 0, 0, 0 |
| 40 | }; |
| 41 | uint64_t rc = 1; |
| 42 | if (omega2_cg_gdn_emit(&rc, sp, 8u, ins, 8192u, meta, 512u) != 0 || rc != 0) return -1; |
| 43 | return omega_bw_ir_from_om_limit(ins, meta, prog, gpr_limit); |
| 44 | } |
| 45 | |
| 46 | int omega_blackwell_codegen_gdn_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { |
| 47 | return gdn_emit_kind(0u, spec, prog, 0u); |
| 48 | } |
| 49 | |
| 50 | int omega_gdn_prog_stats(const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs) { |
| 51 | static BlackwellIRProgram prog; |
| 52 | if (omega_blackwell_codegen_gdn_prog(spec, &prog) != 0) return -1; |
| 53 | if (insns) *insns = (uint32_t)prog.count; |
| 54 | if (gprs) *gprs = prog.regalloc.peak_gpr_usage; |
| 55 | return 0; |
| 56 | } |
| 57 | |
| 58 | int omega_blackwell_codegen_gdn_copy_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { |
| 59 | return gdn_emit_kind(1u, spec, prog, 0u); |
| 60 | } |
| 61 | |
| 62 | int omega_blackwell_codegen_gdn_prep_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { |
| 63 | return gdn_emit_kind(2u, spec, prog, 0u); |
| 64 | } |
| 65 | |
| 66 | int omega_blackwell_codegen_gdn_scan_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { |
| 67 | return gdn_emit_kind(3u, spec, prog, 0u); |
| 68 | } |
| 69 | |
| 70 | int omega_blackwell_codegen_gdn_post_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { |
| 71 | return gdn_emit_kind(4u, spec, prog, 0u); |
| 72 | } |
| 73 | |
| 74 | int omega_gdn_pf_prog_stats(unsigned kind, const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs) { |
| 75 | static BlackwellIRProgram prog; |
| 76 | int rc = kind == 0 ? omega_blackwell_codegen_gdn_prep_prog(spec, &prog) |
| 77 | : kind == 1 ? omega_blackwell_codegen_gdn_scan_prog(spec, &prog) |
| 78 | : kind == 2 ? omega_blackwell_codegen_gdn_post_prog(spec, &prog) : -1; |
| 79 | if (rc != 0) return -1; |
| 80 | if (insns) *insns = (uint32_t)prog.count; |
| 81 | if (gprs) *gprs = prog.regalloc.peak_gpr_usage; |
| 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | int omega_blackwell_codegen_gdn_multi_prog(const OmegaGdnSpec *spec, BlackwellIRProgram *prog) { |
| 86 | return gdn_emit_kind(5u, spec, prog, BW_PHYS_GPR_HW_LIMIT); |
| 87 | } |
| 88 | |
| 89 | int omega_gdn_multi_prog_stats(const OmegaGdnSpec *spec, uint32_t *insns, uint32_t *gprs) { |
| 90 | static BlackwellIRProgram prog; |
| 91 | if (omega_blackwell_codegen_gdn_multi_prog(spec, &prog) != 0) return -1; |
| 92 | if (insns) *insns = (uint32_t)prog.count; |
| 93 | if (gprs) *gprs = prog.regalloc.peak_gpr_usage; |
| 94 | return 0; |
| 95 | } |