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