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 / bridge / core / abi_ggml.om

256 lines · 5982 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note bridge/core/abi_ggml.om: pure-Omega port of omega-bridge Native ABI v2 validator
3note (slot budget, layout bounds, aliasing/single-edit, trap status mapping) and
4note ggml-omega tensor-bridge descriptor, stride, alignment, tiling, and op dispatch.
5note Compose after si/directives/om2.om.
6
7const BR_OK ulong 0
8const BR_ERR_SLOT_OVERFLOW ulong 1
9const BR_ERR_BAD_LAYOUT_SIZE ulong 2
10const BR_ERR_BAD_FIELD_COUNT ulong 3
11const BR_ERR_FIELD_OOB ulong 4
12const BR_ERR_FIELD_OVERLAP ulong 5
13const BR_ERR_ALIAS_OVERLAP ulong 6
14const BR_ERR_BAD_DTYPE ulong 7
15const BR_ERR_BAD_BLOCK_ALIGN ulong 8
16const BR_ERR_BAD_STRIDE ulong 9
17const BR_ERR_UNSUPPORTED_OP ulong 10
18const BR_ERR_BAD_TILE ulong 11
19const BR_ERR_OMEGA_TRAP ulong 12
20const BR_ERR_HOST_FAILURE ulong 13
21
22const BR_MAX_SLOTS ulong 6
23const BR_MAX_LAYOUT_BYTES ulong 4096
24
25const GGML_F32 ulong 0
26const GGML_F16 ulong 1
27const GGML_Q8_0 ulong 8
28
29const GGML_OP_ADD ulong 1
30const GGML_OP_MUL ulong 2
31const GGML_OP_SUB ulong 3
32const GGML_OP_DIV ulong 4
33const GGML_OP_RMS_NORM ulong 5
34const GGML_OP_SCALE ulong 6
35const GGML_OP_SILU ulong 7
36const GGML_OP_SOFT_MAX ulong 8
37const GGML_OP_MUL_MAT ulong 9
38const GGML_OP_CPY ulong 10
39
40note --- Basic I/O helper ---
41
42fn br_write_all fd int buf view uchar * to bool
43n = len buf
44if eq n ulong 0
45ret true
46end
47if gt n ulong 1048576
48ret false
49end
50off = ulong 0
51while lt off n bound 1048576
52remn = sub n off
53k = write fd buf off remn
54if le k long 0
55ret false
56end
57off = add off ulong k
58end
59ret true
60end
61
62note --- omega-bridge Native ABI v2 Validators ---
63note OM-2 parameter slot budget: scalar=1, sized array/struct=1, unsized view/edit=2; total <= 6.
64
65fn br_check_param_slots n_scalars ulong n_sized ulong n_unsized ulong to ulong
66if or gt n_scalars ulong 16 or gt n_sized ulong 16 gt n_unsized ulong 16
67ret BR_ERR_SLOT_OVERFLOW
68end
69slots = add add n_scalars n_sized mul n_unsized ulong 2
70if gt slots BR_MAX_SLOTS
71ret BR_ERR_SLOT_OVERFLOW
72end
73ret BR_OK
74end
75
76note OM-2 layout bounds: size in 1..4096, count in 1..4096, field fits within layout size.
77fn br_validate_layout_field layout_size ulong field_off ulong elem_size ulong count ulong to ulong
78if or eq layout_size ulong 0 gt layout_size BR_MAX_LAYOUT_BYTES
79ret BR_ERR_BAD_LAYOUT_SIZE
80end
81if or eq count ulong 0 gt count BR_MAX_LAYOUT_BYTES
82ret BR_ERR_BAD_FIELD_COUNT
83end
84if or eq elem_size ulong 0 gt elem_size ulong 64
85ret BR_ERR_FIELD_OOB
86end
87flen = mul elem_size count
88if gt field_off layout_size
89ret BR_ERR_FIELD_OOB
90end
91if gt flen sub layout_size field_off
92ret BR_ERR_FIELD_OOB
93end
94ret BR_OK
95end
96
97note Two half-open byte intervals [off1, off1+len1) and [off2, off2+len2) must not overlap.
98fn br_spans_disjoint off1 ulong len1 ulong off2 ulong len2 ulong to bool
99if or eq len1 ulong 0 eq len2 ulong 0
100ret true
101end
102if le add off1 len1 off2
103ret true
104end
105if le add off2 len2 off1
106ret true
107end
108ret false
109end
110
111fn br_check_single_edit dst_base ulong dst_len ulong src_base ulong src_len ulong to ulong
112if not br_spans_disjoint dst_base dst_len src_base src_len
113ret BR_ERR_ALIAS_OVERLAP
114end
115ret BR_OK
116end
117
118note Trap-to-status mapping: exit 0 -> BR_OK, exit 101..114 -> BR_ERR_OMEGA_TRAP, other -> BR_ERR_HOST_FAILURE.
119fn br_check_exit_status code ulong to ulong
120if eq code ulong 0
121ret BR_OK
122end
123if and ge code ulong 101 le code ulong 114
124ret BR_ERR_OMEGA_TRAP
125end
126ret BR_ERR_HOST_FAILURE
127end
128
129note --- ggml-omega Tensor-Bridge Descriptor, Stride, Tiling & Op Dispatch ---
130
131fn ggml_dtype_valid dt ulong to bool
132if eq dt GGML_F32
133ret true
134end
135if eq dt GGML_F16
136ret true
137end
138if eq dt GGML_Q8_0
139ret true
140end
141ret false
142end
143
144note Exact row byte size for a ggml tensor row of ne0 elements.
145note F32 = 4*ne0, F16 = 2*ne0, Q8_0 = 34*(ne0/32) with ne0 % 32 == 0. Returns 0 on invalid/misaligned.
146fn ggml_row_bytes dt ulong ne0 ulong to ulong
147if eq ne0 ulong 0
148ret ulong 0
149end
150if gt ne0 ulong 1073741824
151ret ulong 0
152end
153if eq dt GGML_F32
154ret mul ne0 ulong 4
155end
156if eq dt GGML_F16
157ret mul ne0 ulong 2
158end
159if eq dt GGML_Q8_0
160if ne rem ne0 ulong 32 ulong 0
161ret ulong 0
162end
163ret mul div ne0 ulong 32 ulong 34
164end
165ret ulong 0
166end
167
168fn ggml_validate_tensor dt ulong ne0 ulong ne1 ulong nb0 ulong nb1 ulong to ulong
169if not ggml_dtype_valid dt
170ret BR_ERR_BAD_DTYPE
171end
172if or eq ne0 ulong 0 eq ne1 ulong 0
173ret BR_ERR_BAD_STRIDE
174end
175rbytes = ggml_row_bytes dt ne0
176if eq rbytes ulong 0
177ret BR_ERR_BAD_BLOCK_ALIGN
178end
179min_nb0 = ulong 4
180if eq dt GGML_F16
181min_nb0 = ulong 2
182end
183if eq dt GGML_Q8_0
184min_nb0 = ulong 34
185end
186if lt nb0 min_nb0
187ret BR_ERR_BAD_STRIDE
188end
189if lt nb1 rbytes
190ret BR_ERR_BAD_STRIDE
191end
192ret BR_OK
193end
194
195note Exact ggml-omega supports_op claim table (mirrors ggml-omega.c).
196fn ggml_supports_op op ulong src0_dt ulong src1_dt ulong dst_dt ulong ne0 ulong to ulong
197if eq ne0 ulong 0
198ret BR_ERR_BAD_STRIDE
199end
200if and ge op GGML_OP_ADD le op GGML_OP_DIV
201if and eq src0_dt GGML_F32 and eq src1_dt GGML_F32 eq dst_dt GGML_F32
202ret BR_OK
203end
204ret BR_ERR_UNSUPPORTED_OP
205end
206if and ge op GGML_OP_RMS_NORM le op GGML_OP_SOFT_MAX
207if and eq src0_dt GGML_F32 eq dst_dt GGML_F32
208ret BR_OK
209end
210ret BR_ERR_UNSUPPORTED_OP
211end
212if eq op GGML_OP_MUL_MAT
213if or ne src1_dt GGML_F32 ne dst_dt GGML_F32
214ret BR_ERR_UNSUPPORTED_OP
215end
216if not ggml_dtype_valid src0_dt
217ret BR_ERR_BAD_DTYPE
218end
219if eq src0_dt GGML_Q8_0
220if ne rem ne0 ulong 32 ulong 0
221ret BR_ERR_BAD_BLOCK_ALIGN
222end
223end
224ret BR_OK
225end
226if eq op GGML_OP_CPY
227if not ggml_dtype_valid src0_dt
228ret BR_ERR_BAD_DTYPE
229end
230if eq dst_dt GGML_Q8_0
231ret BR_ERR_UNSUPPORTED_OP
232end
233if or eq dst_dt GGML_F32 eq dst_dt GGML_F16
234if eq src0_dt GGML_Q8_0
235if ne rem ne0 ulong 32 ulong 0
236ret BR_ERR_BAD_BLOCK_ALIGN
237end
238end
239ret BR_OK
240end
241ret BR_ERR_UNSUPPORTED_OP
242end
243ret BR_ERR_UNSUPPORTED_OP
244end
245
246note Computes fixed-width tiling plan (plan[0]=full_tiles, plan[1]=tail) for ggml-omega row kernels.
247fn ggml_tile_plan ne0 ulong tile_w ulong plan edit ulong 2 to ulong
248if eq tile_w ulong 0
249ret BR_ERR_BAD_TILE
250end
251full = div ne0 tile_w
252tail = rem ne0 tile_w
253put plan 0 full
254put plan 1 tail
255ret BR_OK
256end