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 / om / arboris_gated.om
| 1 | note SPDX-License-Identifier: AGPL-3.0-only |
| 2 | note Compose after arboris_arena.om and gpu/nvidia/om/numeric/numeric.om. |
| 3 | note CPU oracle for the Omega gated vector v1 model. Offsets in the working |
| 4 | note descriptor are in ushort elements of the reservation. The immutable |
| 5 | note GraphMeta input is the same snapshot a QMD constant bank would receive. |
| 6 | note W_syn[K,D], bias[B,D], W_gate[N,D], W_pred[N,D] are elementwise FP16. |
| 7 | note Per branch: ReLU(bias + ordered synapse FMAs + ordered child potentials). |
| 8 | note Per soma: g = sigmoid(W_gate*previous), y = ReLU(W_pred*sum(roots)). |
| 9 | note Store roundToFP16(g*y + (1-g)*previous). No header flip in this oracle. |
| 10 | fn ag_layout m view ulong 16 offsets view ulong 6 cells ulong page ulong desc edit ulong 16 to bool |
| 11 | check = new ulong 16 0 |
| 12 | expected = new ulong 6 0 |
| 13 | for i ulong 16 |
| 14 | put check i get m i |
| 15 | end |
| 16 | if gt cells div ulong 0xffffffffffffffff 2 |
| 17 | ret false |
| 18 | end |
| 19 | if ne arboris_plan_arena check mul cells 2 page expected 0 |
| 20 | ret false |
| 21 | end |
| 22 | for i ulong 6 |
| 23 | if ne get expected i get offsets i |
| 24 | ret false |
| 25 | end |
| 26 | end |
| 27 | for i ulong 5 |
| 28 | if ne get m add 9 i get check add 9 i |
| 29 | ret false |
| 30 | end |
| 31 | end |
| 32 | n = get m 1 |
| 33 | if gt n 0x1000000 |
| 34 | ret false |
| 35 | end |
| 36 | rows = add add get m 2 get m 3 mul n 2 |
| 37 | bytes = positive_product rows mul get m 4 2 get m 10 |
| 38 | if or eq bytes 0 ne bytes get m 10 |
| 39 | ret false |
| 40 | end |
| 41 | for i ulong 5 |
| 42 | put desc i get m add i 1 |
| 43 | end |
| 44 | note CSR bases, expressed as ushort indices. |
| 45 | put desc 5 div get offsets 1 2 |
| 46 | put desc 6 add get desc 5 mul add n 1 2 |
| 47 | put desc 7 add get desc 6 mul add get desc 1 1 2 |
| 48 | put desc 8 add get desc 7 mul get desc 2 2 |
| 49 | note Parameter bases and active/inactive bank bases. |
| 50 | put desc 9 div get offsets 2 2 |
| 51 | put desc 10 add get desc 9 mul get desc 2 get desc 3 |
| 52 | put desc 11 add get desc 10 mul get desc 1 get desc 3 |
| 53 | put desc 12 add get desc 11 mul n get desc 3 |
| 54 | put desc 13 div get offsets add 3 get m 6 2 |
| 55 | put desc 14 div get offsets sub ulong 4 get m 6 2 |
| 56 | ret true |
| 57 | end |
| 58 | |
| 59 | fn ag_u32 arena view ushort * base ulong i ulong to ulong |
| 60 | pos = add base mul i 2 |
| 61 | ret or ulong get arena pos shl ulong get arena add pos 1 16 |
| 62 | end |
| 63 | |
| 64 | fn ag_csr arena view ushort * base ulong count ulong last ulong fan ulong to bool |
| 65 | prev = ulong 0 |
| 66 | if ne ag_u32 arena base 0 0 |
| 67 | ret false |
| 68 | end |
| 69 | for i ulong count bound handle_limit |
| 70 | next = ag_u32 arena base add i 1 |
| 71 | if or lt next prev gt next last |
| 72 | ret false |
| 73 | end |
| 74 | if gt sub next prev fan |
| 75 | ret false |
| 76 | end |
| 77 | prev = next |
| 78 | end |
| 79 | ret eq prev last |
| 80 | end |
| 81 | |
| 82 | fn ag_tree arena view ushort * desc view ulong 16 soma ulong to bool |
| 83 | first = ag_u32 arena get desc 5 soma |
| 84 | stop = ag_u32 arena get desc 5 add soma 1 |
| 85 | allowed = ulong 0 |
| 86 | while lt first stop bound handle_limit |
| 87 | word = ag_u32 arena get desc 8 first |
| 88 | note Low 24 bits owner, next 7 depth, top bit 0 selects ReLU. |
| 89 | if or ne and word 0xffffff soma ne shr word 31 0 |
| 90 | ret false |
| 91 | end |
| 92 | depth = shr word 24 |
| 93 | if gt depth allowed |
| 94 | ret false |
| 95 | end |
| 96 | allowed = add depth 1 |
| 97 | first = add first 1 |
| 98 | end |
| 99 | ret true |
| 100 | end |
| 101 | |
| 102 | fn ag_half_span arena view ushort * start ulong count ulong to bool |
| 103 | for i ulong count bound 0x7fffffffffffffff |
| 104 | if eq and get arena add start i 0x7c00 0x7c00 |
| 105 | ret false |
| 106 | end |
| 107 | end |
| 108 | ret true |
| 109 | end |
| 110 | |
| 111 | fn ag_input_ok arena view ushort * d view ulong 16 to bool |
| 112 | if not ag_csr arena get d 5 get d 0 get d 1 get d 4 |
| 113 | ret false |
| 114 | end |
| 115 | if not ag_csr arena get d 6 get d 1 get d 2 get d 2 |
| 116 | ret false |
| 117 | end |
| 118 | for k ulong get d 2 bound handle_limit |
| 119 | if ge ag_u32 arena get d 7 k get d 0 |
| 120 | ret false |
| 121 | end |
| 122 | end |
| 123 | for i ulong get d 0 bound handle_limit |
| 124 | if not ag_tree arena d i |
| 125 | ret false |
| 126 | end |
| 127 | end |
| 128 | state_cells = mul get d 0 get d 3 |
| 129 | params = add sub get d 12 get d 9 state_cells |
| 130 | if not ag_half_span arena get d 9 params |
| 131 | ret false |
| 132 | end |
| 133 | ret ag_half_span arena get d 13 state_cells |
| 134 | end |
| 135 | |
| 136 | fn ag_load arena view ushort * desc view ulong 16 base ulong row ulong dim ulong to f32 |
| 137 | index = add dim mul row get desc 3 |
| 138 | ret f32of f16to32 ulong get arena add get desc base index |
| 139 | end |
| 140 | |
| 141 | fn ag_synapses arena view ushort * d view ulong 16 b ulong dim ulong to f32 |
| 142 | start = ag_u32 arena get d 6 b |
| 143 | stop = ag_u32 arena get d 6 add b 1 |
| 144 | acc = ag_load arena d 10 b dim |
| 145 | k = start |
| 146 | while lt k stop bound handle_limit |
| 147 | src = ag_u32 arena get d 7 k |
| 148 | x = ag_load arena d 13 src dim |
| 149 | w = ag_load arena d 9 k dim |
| 150 | acc = fma x w acc |
| 151 | k = add k 1 |
| 152 | end |
| 153 | ret acc |
| 154 | end |
| 155 | |
| 156 | fn ag_close stack edit f32 129 used ulong keep ulong to void |
| 157 | level = used |
| 158 | while gt level keep bound 128 |
| 159 | parent = sub level 1 |
| 160 | activation = fmax f32 0 get stack level |
| 161 | put stack parent add get stack parent activation |
| 162 | level = parent |
| 163 | end |
| 164 | end |
| 165 | |
| 166 | fn ag_sum arena view ushort * d view ulong 16 soma ulong dim ulong to f32 |
| 167 | stack = new f32 129 0.0f32 |
| 168 | used = ulong 0 |
| 169 | b = ag_u32 arena get d 5 soma |
| 170 | stop = ag_u32 arena get d 5 add soma 1 |
| 171 | while lt b stop bound handle_limit |
| 172 | depth = shr ag_u32 arena get d 8 b 24 |
| 173 | ag_close stack used depth |
| 174 | used = add depth 1 |
| 175 | put stack used ag_synapses arena d b dim |
| 176 | b = add b 1 |
| 177 | end |
| 178 | ag_close stack used 0 |
| 179 | ret get stack 0 |
| 180 | end |
| 181 | |
| 182 | fn ag_sigmoid x f32 to f32 |
| 183 | z = f32of mexp bitsof neg fabs x |
| 184 | denom = add f32 1 z |
| 185 | if lt x f32 0 |
| 186 | ret div z denom |
| 187 | end |
| 188 | ret div f32 1 denom |
| 189 | end |
| 190 | |
| 191 | fn ag_coordinate arena view ushort * d view ulong 16 soma ulong dim ulong to ulong |
| 192 | prev = ag_load arena d 13 soma dim |
| 193 | g = ag_sigmoid mul prev ag_load arena d 11 soma dim |
| 194 | candidate = fmax f32 0 mul ag_sum arena d soma dim ag_load arena d 12 soma dim |
| 195 | y = fma g candidate mul sub f32 1 g prev |
| 196 | ret narrow bitsof y 10 15 31 |
| 197 | end |
| 198 | |
| 199 | note A failure after computation begins leaves the inactive bank disposable. |
| 200 | note No reader may select it until the controller commits a complete tick. |
| 201 | export fn arboris_gated_tick m view ulong 16 offsets view ulong 6 arena edit ushort * page ulong to ulong |
| 202 | desc = new ulong 16 0 |
| 203 | if not ag_layout m offsets len arena page desc |
| 204 | ret 1 |
| 205 | end |
| 206 | if not ag_input_ok arena desc |
| 207 | ret 2 |
| 208 | end |
| 209 | dim = get desc 3 |
| 210 | count = mul get desc 0 dim |
| 211 | for i ulong count bound 0x7fffffffffffffff |
| 212 | v = ag_coordinate arena desc div i dim rem i dim |
| 213 | if eq and v 0x7c00 0x7c00 |
| 214 | ret 3 |
| 215 | end |
| 216 | put arena add get desc 14 i ushort v |
| 217 | end |
| 218 | ret 0 |
| 219 | end |
| 220 | |
| 221 | |
| 222 | note Cold loader workspace is two equal arena images followed by 2*N uint |
| 223 | note remap entries. Input image is first, output second. Only logical slab |
| 224 | note bytes are copied. Each half is bound by the same checked plan. The map |
| 225 | note is new-to-old followed by old-to-new. d[15] holds image length in ushorts. |
| 226 | fn ag_fan a view ushort * d view ulong 16 i ulong to ulong |
| 227 | ret sub ag_u32 a get d 5 add i 1 ag_u32 a get d 5 i |
| 228 | end |
| 229 | fn ag_word a edit ushort * base ulong i ulong value ulong to void |
| 230 | p = add base mul i 2 |
| 231 | put a p ushort and value 65535 |
| 232 | put a add p 1 ushort and shr value 16 65535 |
| 233 | end |
| 234 | fn ag_order a edit ushort * d view ulong 16 threshold ulong count ulong to void |
| 235 | first = ulong 0 |
| 236 | hub = count |
| 237 | ids = mul get d 15 2 |
| 238 | n = get d 0 |
| 239 | for i ulong n bound 0x1000000 |
| 240 | index = first |
| 241 | if le ag_fan a d i threshold |
| 242 | first = add first 1 |
| 243 | else |
| 244 | index = hub |
| 245 | hub = add hub 1 |
| 246 | end |
| 247 | ag_word a ids index i |
| 248 | ag_word a ids add n i index |
| 249 | end |
| 250 | end |
| 251 | fn ag_copy_row a edit ushort * d view ulong 16 base_slot ulong old ulong destination ulong to void |
| 252 | old = add get d base_slot mul old get d 3 |
| 253 | destination = add add get d 15 get d base_slot mul destination get d 3 |
| 254 | for i ulong get d 3 bound 24576 |
| 255 | put a add destination i get a add old i |
| 256 | end |
| 257 | end |
| 258 | |
| 259 | fn ag_copy_syn a edit ushort * d view ulong 16 k ulong new_k ulong to void |
| 260 | old = ag_u32 a get d 7 k |
| 261 | id = ag_u32 a mul get d 15 2 add get d 0 old |
| 262 | ag_word a add get d 15 get d 7 new_k id |
| 263 | ag_copy_row a d 9 k new_k |
| 264 | end |
| 265 | |
| 266 | note Cursor is new soma, old branch, new branch, new synapse. |
| 267 | fn ag_copy_branch a edit ushort * d view ulong 16 cursor edit ulong 4 to void |
| 268 | b = get cursor 1 |
| 269 | new_b = get cursor 2 |
| 270 | meta = ag_u32 a get d 8 b |
| 271 | ag_word a add get d 15 get d 8 new_b or and meta 0xff000000 get cursor 0 |
| 272 | ag_copy_row a d 10 b new_b |
| 273 | k = ag_u32 a get d 6 b |
| 274 | stop = ag_u32 a get d 6 add b 1 |
| 275 | new_k = get cursor 3 |
| 276 | ag_word a add get d 15 get d 6 new_b new_k |
| 277 | while lt k stop bound handle_limit |
| 278 | ag_copy_syn a d k new_k |
| 279 | new_k = add new_k 1 |
| 280 | k = add k 1 |
| 281 | end |
| 282 | put cursor 3 new_k |
| 283 | put cursor 2 add new_b 1 |
| 284 | end |
| 285 | fn ag_copy_graph a edit ushort * d view ulong 16 to void |
| 286 | cursor = new ulong 4 0 |
| 287 | for i ulong get d 0 bound 0x1000000 |
| 288 | old = ag_u32 a mul get d 15 2 i |
| 289 | put cursor 0 i |
| 290 | ag_word a add get d 15 get d 5 i get cursor 2 |
| 291 | b = ag_u32 a get d 5 old |
| 292 | stop = ag_u32 a get d 5 add old 1 |
| 293 | while lt b stop bound handle_limit |
| 294 | put cursor 1 b |
| 295 | ag_copy_branch a d cursor |
| 296 | b = add b 1 |
| 297 | end |
| 298 | for j ulong 4 |
| 299 | ag_copy_row a d add 11 j old i |
| 300 | end |
| 301 | end |
| 302 | ag_word a add get d 15 get d 5 get d 0 get d 1 |
| 303 | ag_word a add get d 15 get d 6 get d 1 get d 2 |
| 304 | end |
| 305 | fn ag_partition a edit ushort * d view ulong 16 smem ulong limits edit ulong 2 to bool |
| 306 | tile = get d 3 |
| 307 | if gt tile 256 |
| 308 | tile = ulong 256 |
| 309 | end |
| 310 | hub = div smem mul tile 4 |
| 311 | threshold = div hub 4 |
| 312 | if and ne get limits 0 0 lt get limits 0 threshold |
| 313 | threshold = get limits 0 |
| 314 | end |
| 315 | count = ulong 0 |
| 316 | for i ulong get d 0 bound 0x1000000 |
| 317 | fan = ag_fan a d i |
| 318 | if gt fan hub |
| 319 | ret false |
| 320 | end |
| 321 | if le fan threshold |
| 322 | count = add count 1 |
| 323 | end |
| 324 | end |
| 325 | ag_order a d threshold count |
| 326 | put limits 1 count |
| 327 | ret true |
| 328 | end |
| 329 | fn ag_image_header m view ulong 16 a edit ushort * cells ulong to void |
| 330 | for i ulong 16 |
| 331 | ag_word a cells mul i 2 get m i |
| 332 | ag_word a cells add mul i 2 1 shr get m i 32 |
| 333 | end |
| 334 | end |
| 335 | |
| 336 | note limits[0] optionally lowers the standard branch cap. limits[1] receives |
| 337 | note the prefix boundary. Run before plan lock, when the arena digest is zero. |
| 338 | export fn arboris_pack_gated m view ulong 16 offsets view ulong 6 a edit ushort * limits edit ulong 2 to ulong |
| 339 | d = new ulong 16 0 |
| 340 | cells = div get m 12 2 |
| 341 | if or ne get m 15 0 gt get m 12 len a |
| 342 | ret 1 |
| 343 | end |
| 344 | if not ag_layout m offsets cells 4096 d |
| 345 | ret 1 |
| 346 | end |
| 347 | if gt get d 3 24576 |
| 348 | ret 2 |
| 349 | end |
| 350 | if ne sub len a get m 12 mul get d 0 4 |
| 351 | ret 2 |
| 352 | end |
| 353 | if not ag_input_ok a d |
| 354 | ret 3 |
| 355 | end |
| 356 | put d 15 cells |
| 357 | if not ag_partition a d get m 13 limits |
| 358 | ret 4 |
| 359 | end |
| 360 | ag_image_header m a cells |
| 361 | ag_copy_graph a d |
| 362 | receipt = add cells div get offsets 5 2 |
| 363 | for i ulong 32 |
| 364 | put a add receipt i 0 |
| 365 | end |
| 366 | ret 0 |
| 367 | end |