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 / omatmul.om

51 lines · 1656 bytes Raw Blame History
1note omatmul.om: the Omega kernel behind the MAX CPU custom op "omega_matmul" (max-cpu/README.md).
2note C[M,N] = A[M,K] @ B[K,N], row-major. A and B arrive as bfloat16 bit patterns in ONE view ab
3note (A's M*K elements, then B's K*N): bf16 to f32 is exact (the 16 bits become the high half), each
4note product of two bf16 values is exact in f32, and the K-term sum accumulates in f32 in order
5note p = 0..K-1, one rounding per add (RNE, no fused multiply-add). C is written as f32 bit patterns.
6note s holds the shape [M, K, N]. A, B and C cannot be three separate views because OM-2 function
7note mode has six parameter slots and an unsized view takes two (spec/omegese-om2.md section 4).
8note Result codes (status 0): 0 done; 1 a dimension is 0 or above 4096; 2 len ab is not M*K + K*N;
9note 3 len c is not M*N. C is untouched unless the result is 0. Every index is also checked (trap 3).
10om2
11fn bdot ab view ushort * ai ulong bj ulong k ulong n ulong to uint
12acc = f32 0.0
13for p ulong k
14x = bitcast f32 shl uint get ab add ai p 16
15y = bitcast f32 shl uint get ab add bj mul p n 16
16acc = add acc mul x y
17end
18ret bitcast uint acc
19end
20fn brow ab view ushort * c edit uint * s view ulong 3 i ulong to void
21k = get s 1
22n = get s 2
23ka = mul get s 0 k
24ai = mul i k
25ci = mul i n
26for j ulong n
27put c add ci j bdot ab ai add ka j k n
28end
29end
30export fn omatmul ab view ushort * c edit uint * s view ulong 3 to ulong
31m = get s 0
32k = get s 1
33n = get s 2
34if or or eq m 0 eq k 0 eq n 0
35ret 1
36end
37if or or gt m 4096 gt k 4096 gt n 4096
38ret 1
39end
40ka = mul m k
41if ne len ab add ka mul k n
42ret 2
43end
44if ne len c mul m n
45ret 3
46end
47for i ulong m
48brow ab c s i
49end
50ret 0
51end