interplane.Get Omega
Read in your language

omega-bridge

Omega-first language interoperability: checked conversion backends and native language bindings

Brought by drakestapleton

branch main Branches Tags History

omega-bridge / max-cpu / kernels / __init__.mojo

52 lines · 2271 bytes Raw Blame History
1# MAX CPU custom op "omega_matmul": glue only. The matmul is the Omega unit max-cpu/omatmul.om,
2# compiled by tools/native into libomega_native.so (export omega2_omatmul, native ABI v2,
3# docs/NATIVE-ABI.md).
4#
5# c output float32 [M, N] written by Omega as f32 bit patterns
6# ab input bfloat16 [M*K + K*N] A row-major, then B row-major (one view: six-slot limit)
7# shape input int64 [3] [M, K, N]
8#
9# Symbol resolution: the caller loads libomega_native.so with RTLD_GLOBAL before the graph runs
10# (max-cpu/omega_max.py). MAX 26.7's kernel JIT does not search the process image, so a plain
11# external_call["omega2_omatmul", ...] fails at model load with "Symbols not found"
12# (max-cpu/run.sh keeps that as a control). The call therefore resolves the same C symbol through
13# dlopen(NULL)/dlsym (OwnedDLHandle() with no path), and a missing symbol raises.
14#
15# Omega refuses a bad shape with a nonzero result and leaves C untouched; the bridge refuses a bad
16# pointer or length with a nonzero status. Either is raised here, so MAX fails the execution
17# instead of returning C.
18
19import extensibility
20from extensibility import InputTensor, OutputTensor
21from std.ffi import OwnedDLHandle, external_call
22
23
24@extensibility.register("omega_matmul", type="cpu")
25struct OmegaMatmul:
26 @staticmethod
27 def execute[
28 target: StaticString,
29 ](
30 c: OutputTensor[dtype=DType.float32, rank=2, ...],
31 ab: InputTensor[dtype=DType.bfloat16, rank=1, ...],
32 shape: InputTensor[dtype=DType.int64, rank=1, ...],
33 ) raises:
34 comptime assert target == "cpu", "omega_matmul is a CPU op"
35 var result: UInt64 = 0
36 var process = OwnedDLHandle()
37 var status = process.get_function[UInt32]("omega2_omatmul")(
38 Pointer(to=result),
39 ab.unsafe_ptr[DType.uint16](),
40 UInt64(ab.size()),
41 c.unsafe_ptr[DType.uint32](),
42 UInt64(c.size()),
43 shape.unsafe_ptr[DType.uint64](),
44 UInt64(shape.size()),
45 )
46 if status != 0 or result != 0:
47 raise Error(
48 "omega_matmul refused: status "
49 + String(status)
50 + " result "
51 + String(result)
52 )