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

489 lines · 12554 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note Shared Arboris online co-activation prefetcher core (zero globals, zero heap).
3note Caller supplies all state buffers; fits any layer/expert/top-k/loop geometry.
4note
5note Config layout (`cfg view ulong 6`):
6note 0: n_trans transition tables (0..n_trans-2 for L->L+1; n_trans-1 for loop wrap)
7note 1: n_exp experts per layer (1..65535; packed into 16-bit edge target field)
8note 2: top_k active experts per layer per stream (1..n_exp)
9note 3: n_slots Space-Saving slots per source expert row (>= 1)
10note 4: loop_from source layer for looped wrap transition, or AO_NONE if non-looped
11note 5: loop_to target layer for looped wrap transition, or AO_NONE if non-looped
12note
13note Edge packing: `(count << 16) | target_eid` in `uint`, decayed by halving at AO_DECAY_AT.
14note Single-stream `prev_state` (`len >= 2 * top_k + 1`):
15note [0 .. top_k - 1] prev_ids
16note [top_k] prev_layer (n_trans = token-reset sentinel)
17note [top_k + 1 .. 2 * top_k] cur_ids scratch staged by ao_stage_cur
18
19const AO_NONE ulong 0xffffffff
20const AO_DECAY_AT ulong 60000
21
22fn ao_cfg_valid cfg view ulong 6 to bool
23 n_tr = get cfg 0
24 n_ex = get cfg 1
25 t_k = get cfg 2
26 n_sl = get cfg 3
27 l_f = get cfg 4
28 l_t = get cfg 5
29 if or eq n_tr 0 or eq n_ex 0 gt n_ex 65535
30 ret false
31 end
32 if or eq t_k 0 gt t_k n_ex
33 ret false
34 end
35 if or eq n_sl 0 gt n_tr div 0x7fffffff n_ex
36 ret false
37 end
38 if eq l_f AO_NONE
39 ret eq l_t AO_NONE
40 end
41 if or lt n_tr 2 or ge l_f n_tr ge l_t n_tr
42 ret false
43 end
44 ret true
45end
46
47fn ao_table_words cfg view ulong 6 to ulong
48 if not ao_cfg_valid cfg
49 ret 0
50 end
51 prod = mul get cfg 0 get cfg 1
52 ret mul prod get cfg 3
53end
54
55fn ao_pack cnt ulong tgt ulong to uint
56 ret or shl uint cnt 16 uint tgt
57end
58
59fn ao_resolve_step n_tr ulong l_f ulong l_t ulong prev_l ulong layer ulong to ulong
60 if ge layer n_tr
61 ret n_tr
62 end
63 if and gt layer 0 eq add prev_l 1 layer
64 ret prev_l
65 end
66 if ne l_f AO_NONE
67 if and eq prev_l l_f eq layer l_t
68 ret sub n_tr 1
69 end
70 end
71 ret n_tr
72end
73
74fn ao_resolve_trans cfg view ulong 6 prev_l ulong layer ulong to ulong
75 if not ao_cfg_valid cfg
76 ret AO_NONE
77 end
78 ret ao_resolve_step get cfg 0 get cfg 4 get cfg 5 prev_l layer
79end
80
81fn ao_reset_token cfg view ulong 6 prev_state edit uint * to bool
82 if not ao_cfg_valid cfg
83 ret false
84 end
85 n_tr = get cfg 0
86 t_k = get cfg 2
87 need = add mul 2 t_k 1
88 if lt len prev_state need
89 ret false
90 end
91 for i ulong mul 2 t_k bound 131072
92 put prev_state i uint AO_NONE
93 end
94 put prev_state t_k uint n_tr
95 ret true
96end
97
98fn ao_reset_all cfg view ulong 6 edges edit uint * prev_state edit uint * to bool
99 words = ao_table_words cfg
100 if or eq words 0 lt len edges words
101 ret false
102 end
103 if not ao_reset_token cfg prev_state
104 ret false
105 end
106 for i ulong words bound 16777216
107 put edges i 0
108 end
109 ret true
110end
111
112fn ao_halve_row_span edges edit uint * row_base ulong n_slots ulong to void
113 for h ulong n_slots bound 65536
114 idx = add row_base h
115 e2 = get edges idx
116 c2 = ulong shr e2 16
117 if gt c2 1
118 put edges idx ao_pack shr c2 1 ulong and e2 65535
119 else
120 put edges idx 0
121 end
122 end
123end
124
125fn ao_find_or_bump edges edit uint * row_base ulong n_slots ulong tgt ulong to bool
126 for j ulong n_slots bound 65536
127 idx = add row_base j
128 ent = get edges idx
129 c = ulong shr ent 16
130 if and gt c 0 eq ulong and ent 65535 tgt
131 nc = add c 1
132 if ge nc AO_DECAY_AT
133 ao_halve_row_span edges row_base n_slots
134 nc = shr AO_DECAY_AT 1
135 end
136 put edges idx ao_pack nc tgt
137 ret true
138 end
139 end
140 ret false
141end
142
143fn ao_bump_miss edges edit uint * row_base ulong n_slots ulong tgt ulong to void
144 empty_slot = n_slots
145 min_slot = ulong 0
146 min_cnt = ulong 65536
147 for j ulong n_slots bound 65536
148 ent = get edges add row_base j
149 c = ulong shr ent 16
150 if eq c 0
151 if eq empty_slot n_slots
152 empty_slot = j
153 end
154 else
155 if lt c min_cnt
156 min_cnt = c
157 min_slot = j
158 end
159 end
160 end
161 if lt empty_slot n_slots
162 put edges add row_base empty_slot ao_pack 1 tgt
163 ret
164 end
165 if le min_cnt 1
166 put edges add row_base min_slot ao_pack 1 tgt
167 else
168 old_tgt = ulong and get edges add row_base min_slot 65535
169 put edges add row_base min_slot ao_pack sub min_cnt 1 old_tgt
170 end
171end
172
173fn ao_bump_row edges edit uint * row_base ulong n_slots ulong tgt ulong to void
174 if not ao_find_or_bump edges row_base n_slots tgt
175 ao_bump_miss edges row_base n_slots tgt
176 end
177end
178
179fn ao_bump_edge cfg view ulong 6 edges edit uint * trans ulong src ulong tgt ulong to void
180 if not ao_cfg_valid cfg
181 ret
182 end
183 n_tr = get cfg 0
184 n_ex = get cfg 1
185 if or ge trans n_tr or ge src n_ex ge tgt n_ex
186 ret
187 end
188 n_sl = get cfg 3
189 row_base = mul add mul trans n_ex src n_sl
190 if gt add row_base n_sl len edges
191 ret
192 end
193 ao_bump_row edges row_base n_sl tgt
194end
195
196fn ao_stage_cur cfg view ulong 6 prev_state edit uint * cur_ids view uint * to bool
197 if not ao_cfg_valid cfg
198 ret false
199 end
200 t_k = get cfg 2
201 need = add mul 2 t_k 1
202 if or lt len cur_ids t_k lt len prev_state need
203 ret false
204 end
205 cur_off = add t_k 1
206 for d ulong t_k bound 65536
207 put prev_state add cur_off d get cur_ids d
208 end
209 ret true
210end
211
212fn ao_bump_from_row cfg view ulong 6 edges edit uint * prev_state edit uint * meta view ulong 2 to void
213 n_ex = get cfg 1
214 t_k = get cfg 2
215 cur_off = add t_k 1
216 trans = get meta 0
217 src = get meta 1
218 for d ulong t_k bound 65536
219 tgt = ulong get prev_state add cur_off d
220 if lt tgt n_ex
221 ao_bump_edge cfg edges trans src tgt
222 end
223 end
224end
225
226fn ao_observe cfg view ulong 6 edges edit uint * prev_state edit uint * layer ulong to bool
227 if not ao_cfg_valid cfg
228 ret false
229 end
230 t_k = get cfg 2
231 if or ge layer get cfg 0 lt len prev_state add mul 2 t_k 1
232 ret false
233 end
234 if lt len edges ao_table_words cfg
235 ret false
236 end
237 trans = ao_resolve_trans cfg ulong get prev_state t_k layer
238 rm = new ulong 2 0
239 put rm 0 trans
240 if lt trans get cfg 0
241 for s ulong t_k bound 65536
242 src = ulong get prev_state s
243 if lt src get cfg 1
244 put rm 1 src
245 ao_bump_from_row cfg edges prev_state rm
246 end
247 end
248 end
249 for k ulong t_k bound 65536
250 put prev_state k get prev_state add add t_k 1 k
251 end
252 put prev_state t_k uint layer
253 ret true
254end
255
256fn ao_scores_zero cfg view ulong 6 scores edit ulong * to bool
257 if not ao_cfg_valid cfg
258 ret false
259 end
260 n_ex = get cfg 1
261 if lt len scores n_ex
262 ret false
263 end
264 for i ulong n_ex bound 65536
265 put scores i 0
266 end
267 ret true
268end
269
270note Add counts from one edge row (`meta = [row_base, n_slots, n_exp, weight]`) into `scores`.
271fn ao_score_add edges view uint * meta view ulong 4 scores edit ulong * to void
272 if or gt add get meta 0 get meta 1 len edges lt len scores get meta 2
273 ret
274 end
275 for j ulong get meta 1 bound 65536
276 ent = get edges add get meta 0 j
277 c = ulong shr ent 16
278 if gt c 0
279 tgt = ulong and ent 65535
280 if lt tgt get meta 2
281 delta = mul c get meta 3
282 put scores tgt add get scores tgt delta
283 end
284 end
285 end
286end
287
288fn ao_score_src cfg view ulong 6 edges view uint * meta view ulong 3 scores edit ulong * to bool
289 if not ao_cfg_valid cfg
290 ret false
291 end
292 n_ex = get cfg 1
293 trans = get meta 0
294 src = get meta 1
295 if or ge trans get cfg 0 or ge src n_ex lt len scores n_ex
296 ret false
297 end
298 if gt add mul add mul trans n_ex src get cfg 3 get cfg 3 len edges
299 ret false
300 end
301 rm = new ulong 4 0
302 put rm 0 mul add mul trans n_ex src get cfg 3
303 put rm 1 get cfg 3
304 put rm 2 n_ex
305 put rm 3 get meta 2
306 ao_score_add edges rm scores
307 ret true
308end
309
310fn ao_score_work_valid edges view uint * scores_work edit ulong * to bool
311 if lt len scores_work 7
312 ret false
313 end
314 cfg = new ulong 6 0
315 for k ulong 6
316 put cfg k get scores_work k
317 end
318 if not ao_cfg_valid cfg
319 ret false
320 end
321 if or ge get scores_work 6 get cfg 0 lt len scores_work add 7 get cfg 1
322 ret false
323 end
324 if lt len edges ao_table_words cfg
325 ret false
326 end
327 ret true
328end
329
330fn ao_score_batch_step edges view uint * rm edit ulong 4 scores_work edit ulong * src ulong to void
331 n_ex = get scores_work 1
332 pos = mul get scores_work 6 n_ex
333 pos = add pos src
334 put rm 0 mul pos get scores_work 3
335 ao_score_add edges rm part scores_work 7 n_ex
336end
337
338note Single-call sum-over-paths scoring for a slice of unique or per-stream source IDs.
339note `scores_work` layout (`len >= 7 + n_exp`): `[0..5]=cfg, [6]=trans, [7..7+n_exp-1]=scores`.
340fn ao_score_batch edges view uint * src_ids view uint * scores_work edit ulong * to bool
341 if not ao_score_work_valid edges scores_work
342 ret false
343 end
344 n_ex = get scores_work 1
345 for e ulong n_ex bound 65536
346 put scores_work add 7 e 0
347 end
348 rm = new ulong 4 0
349 put rm 1 get scores_work 3
350 put rm 2 n_ex
351 put rm 3 1
352 for i ulong len src_ids bound 1048576
353 src = ulong get src_ids i
354 if ne src AO_NONE
355 if ge src n_ex
356 ret false
357 end
358 ao_score_batch_step edges rm scores_work src
359 end
360 end
361 ret true
362end
363
364note Linear probe insert for small cross-batch expert set (`dedup_out[0..n_unique-1]`).
365fn ao_dedup_insert dedup_out edit uint * n_unique ulong eid ulong to ulong
366 for u ulong n_unique bound 1048576
367 if eq ulong get dedup_out u eid
368 ret u
369 end
370 end
371 put dedup_out n_unique uint eid
372 ret n_unique
373end
374
375fn ao_dedup_validate meta view ulong 4 batch_ids view uint * dedup_cap ulong to long
376 n_ex = get meta 0
377 n_st = get meta 1
378 t_k = get meta 2
379 if or eq n_ex 0 or gt n_ex 65535 or eq n_st 0 eq t_k 0
380 ret long -1
381 end
382 if gt n_st div 349525 t_k
383 ret long -1
384 end
385 n_items = mul n_st t_k
386 if or lt len batch_ids n_items lt dedup_cap mul 3 n_items
387 ret long -1
388 end
389 for i ulong n_items bound 1048576
390 eid = ulong get batch_ids i
391 if and ne eid AO_NONE ge eid n_ex
392 ret long -1
393 end
394 end
395 ret long n_items
396end
397
398fn ao_inc_u32 arr edit uint * pos ulong to void
399 put arr pos add get arr pos 1
400end
401
402note Deduplicate routed experts across a multi-stream batch.
403note `meta = [n_exp, n_streams, top_k, unused]`; `n_items = n_streams * top_k`.
404note `dedup_out` (`len >= 3 * n_items`) receives:
405note [0 .. n_items - 1] unique expert IDs (prefix [0 .. n_unique - 1] valid)
406note [n_items .. 2*n_items - 1] batch multiplicity count per unique expert
407note [2*n_items .. 3*n_items - 1] inverse slot map (batch index -> unique slot, or AO_NONE)
408fn ao_dedup_batch meta view ulong 4 batch_ids view uint * dedup_out edit uint * to long
409 if lt ao_dedup_validate meta batch_ids len dedup_out 0
410 ret long -1
411 end
412 n_items = mul get meta 1 get meta 2
413 n_uniq = ulong 0
414 for i ulong n_items bound 1048576
415 eid = ulong get batch_ids i
416 if eq eid AO_NONE
417 put dedup_out add mul 2 n_items i uint AO_NONE
418 else
419 idx = ao_dedup_insert dedup_out n_uniq eid
420 if eq idx n_uniq
421 put dedup_out add n_items idx 1
422 n_uniq = add n_uniq 1
423 else
424 ao_inc_u32 dedup_out add n_items idx
425 end
426 put dedup_out add mul 2 n_items i uint idx
427 end
428 end
429 ret long n_uniq
430end
431
432fn ao_mask_resident cfg view ulong 6 next_layer ulong resident view long * scores edit ulong * to long
433 if not ao_cfg_valid cfg
434 ret long -1
435 end
436 n_tr = get cfg 0
437 n_ex = get cfg 1
438 if or ge next_layer n_tr lt len scores n_ex
439 ret long -1
440 end
441 if eq len resident 0
442 ret long 0
443 end
444 base = mul next_layer n_ex
445 if lt len resident add base n_ex
446 ret long -1
447 end
448 for eid ulong n_ex bound 65536
449 if ge get resident add base eid 0
450 put scores eid 0
451 end
452 end
453 ret long 0
454end
455
456fn ao_pick_best n_ex ulong scores view ulong * to ulong
457 best = n_ex
458 best_sc = ulong 0
459 for eid ulong n_ex bound 65536
460 sc = get scores eid
461 if gt sc best_sc
462 best_sc = sc
463 best = eid
464 end
465 end
466 ret best
467end
468
469fn ao_pick_op3 cfg view ulong 6 scores edit ulong * out_buf edit uint * to long
470 if not ao_cfg_valid cfg
471 ret long -1
472 end
473 n_ex = get cfg 1
474 if or lt len scores n_ex ne and len out_buf 1 0
475 ret long -1
476 end
477 budget = div len out_buf 2
478 for slot ulong budget bound 65536
479 best = ao_pick_best n_ex scores
480 if eq best n_ex
481 ret long slot
482 end
483 pos = mul slot 2
484 put out_buf pos 3
485 put out_buf add pos 1 uint best
486 put scores best 0
487 end
488 ret long budget
489end