interplane.Get Omega
Read in your language

aien-sovereign-core-mv

AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.

Brought by drakestapleton

branch main Branches Tags History

aien-sovereign-core-mv / om / residency / residency.om

565 lines · 12749 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Expert residency simulator in seeded Omega (sc#630 residency question, sc#633 prefetch hints;
3note host only). Replays a recorded MoE router trace against an expert cache of capacity C and
4note counts hits and misses for two eviction policies: LRU (online) and OPT (Belady, offline bound).
5note Input on stdin, ASCII decimal numbers separated by spaces, tabs or newlines:
6note E L K T, then T*L*K expert ids in step-major order (step t, layer l, slot k), each below E;
7note optionally H, then T*L*H prefetch hint ids in the same row order (row t*L+l holds H ids below
8note E, repeats allowed): the experts a predictor expects layer l of step t to use.
9note Limits: 1 <= E <= 1024, 1 <= K <= 16, 1 <= L, 2 <= T, T*L <= 1000000, T*L*K <= 8388608,
10note H <= 16, T*L*H <= 8388608, hints only when L*E <= 65536, input at most 16 MiB.
11note Ids inside one step and layer must be distinct.
12note Output: one header line, then one line per policy and capacity percent in pc:
13note POLICY cap=C pct=P hits=H miss=M warm_miss=W warm_acc=A
14note lru and opt give every layer its own cache of C = E*P/100 experts. When L*E <= 65536, glru and
15note gopt follow: one cache of C = L*E*P/100 shared by all layers, replaying (t,l) as step t*L+l on
16note virtual expert l*E+e, the order a serving loop needs the experts in.
17note With hints, hlru lines follow, only for C >= K+H: the shared LRU cache where each step first
18note makes its H hinted experts resident and marks them used in the step (a missing one is fetched
19note as a prefetch), then serves its K demands. hits and miss count demands only; hint=H; warm_pf
20note counts prefetches issued at warm steps, and of those warm_used were later demanded before
21note eviction and warm_waste were evicted unused (the rest are still unused at the end).
22note hcold lines follow the hlru lines: the same replay, except that a hint only protects its
23note expert for the step. It never marks it used, and a fetched one enters at the LRU end, as if
24note last used at step 0 (ties: lowest id), so an unused prefetch is the next to go.
25note warm counts only steps t >= T/2 (glru/gopt/hlru/hcold: t*L+l >= T*L/2), so compulsory
26note first-touch misses mostly fall outside them.
27note Eviction never removes an expert already used, hinted or protected in the current step.
28note Exit: 0 ok, 2 read error, 3 input over 16 MiB, 4 header out of range, 5 bad or missing number,
29note 6 trailing bytes, 7 id out of range, 8 repeated id in one step, 9 write error.
30global inb uchar 16777216
31global ids ushort 8388608
32global nxt uint 8388608
33global stp ulong 65536
34global nu ulong 65536
35global lst ulong 65536
36global hin ushort 8388608
37note pfl: 1 while an expert fetched by a hint is still unused; its issue step is then in nu.
38global pfl uchar 65536
39note prt: step+1 of the last step whose hints named the expert; protects it in that step.
40global prt ulong 65536
41note hd: 0 E, 1 L, 2 K, 3 T, 4 parse position, 5 input length, 6 H, 7 hints present.
42global hd ulong 8
43note ac: 0 hits, 1 misses, 2 warm misses, 3 resident count, 4 warm prefetches, 5 warm prefetches
44note used, 6 warm prefetches wasted.
45global ac ulong 8
46global pc ulong 8
47global ob uchar 8192
48global ow ulong 1
49text lit "trace E=~ L=~ K=~ T=~\n~lru ~opt ~cap=~ pct=~ hits=~ miss=~ warm_miss=~ warm_acc=~glru ~gopt ~hlru ~ hint=~ warm_pf=~ warm_used=~ warm_waste=~hcold "
50
51note slurp: read all of stdin into inb. Each inner pass adds at least one byte or ends, so
52note 64 outer passes of 999999 cover the 16777216-byte buffer.
53fn slurp to int
54n = ulong 0
55for r ulong 64
56while lt n len inb bound 999999
57k = read 0 inb n sub len inb n
58if lt k 0
59ret 2
60end
61if eq k 0
62put hd 5 n
63ret 0
64end
65n = add n ulong k
66end
67end
68ret 3
69end
70
71note isws: byte c is a space, tab, carriage return or newline.
72fn isws c uchar to bool
73ret or or eq c 32 eq c 10 or eq c 9 eq c 13
74end
75
76note skip: advance the parse position past white space; return the new position.
77fn skip to ulong
78p = get hd 4
79n = get hd 5
80while lt p n bound 999999
81if not isws get inb p
82break
83end
84p = add p 1
85end
86put hd 4 p
87ret p
88end
89
90note num: next decimal number (at most 20 digits); exit 5 when none is there.
91fn num to ulong
92p = skip
93n = get hd 5
94v = ulong 0
95d = ulong 0
96while lt p n bound 21
97c = get inb p
98if or lt c 48 gt c 57
99break
100end
101v = add mul v 10 ulong sub c 48
102p = add p 1
103d = add d 1
104end
105if eq d 0
106exit 5
107end
108if lt p n
109if not isws get inb p
110exit 5
111end
112end
113put hd 4 p
114ret v
115end
116
117note badd: append byte c to ob.
118fn badd c uchar to void
119w = get ow 0
120if ge w len ob
121exit 9
122end
123put ob w c
124put ow 0 add w 1
125end
126
127note frag: append fragment k of lit (fragments separated by ~, byte 126).
128fn frag k ulong to void
129c = ulong 0
130for i ulong len lit
131b = get lit i
132if eq b 126
133c = add c 1
134else
135if eq c k
136badd b
137end
138end
139end
140end
141
142note dec: append v in decimal.
143fn dec v ulong to void
144p = ulong 1
145while le p div v 10 bound 20
146p = mul p 10
147end
148while ne p 0 bound 21
149badd uchar add div v p 48
150v = rem v p
151p = div p 10
152end
153end
154
155note kv: append fragment k, then v in decimal.
156fn kv k ulong v ulong to void
157frag k
158dec v
159end
160
161note flush: write ob to stdout, looping over partial writes.
162fn flush to int
163o = ulong 0
164w = get ow 0
165while lt o w bound 8193
166k = write 1 ob o sub w o
167if le k 0
168ret 9
169end
170o = add o ulong k
171end
172ret 0
173end
174
175note slot: index of (step t, layer l, slot k) in ids.
176fn slot t ulong l ulong k ulong to ulong
177ret add mul add mul t get hd 1 l get hd 2 k
178end
179
180note prepst: for the K ids at (t,l), nxt[t*K+k] = lst[id], the next step that uses the id;
181note then record step t as the id's next use for earlier steps.
182fn prepst t ulong l ulong to void
183k0 = get hd 2
184for k ulong k0
185id = ulong get ids slot t l k
186put nxt add mul t k0 k uint get lst id
187end
188for k ulong k0
189put lst ulong get ids slot t l k t
190end
191end
192
193note prep: nxt[t*K+k] = next step after t whose layer-l ids contain the id at (t,l,k), else T.
194fn prep l ulong to void
195t0 = get hd 3
196for e ulong get hd 0
197put lst e t0
198end
199for j ulong t0
200prepst sub sub t0 1 j l
201end
202end
203
204note score: eviction preference of resident expert e at step t (larger evicts first).
205note LRU (pol 0): least recently used; OPT (pol 1): next use farthest away.
206fn score e ulong t ulong pol ulong to ulong
207if eq pol 0
208ret sub add t 2 get stp e
209end
210ret get nu e
211end
212
213note evict: drop the resident expert with the largest score, lowest id on ties. An expert used,
214note hinted or protected in step t (stamp or prt t+1) is not evictable. The two tests are nested
215note ifs: one combined condition exceeds the 16 live values (error 12), and a helper call per
216note candidate made the hinted replay 2.4x slower. Dropping an unused prefetch counts as waste.
217fn evict t ulong pol ulong to void
218best = ulong 0
219bv = ulong 0
220fnd = false
221for e ulong get hd 0
222s = get stp e
223if and ne s 0 ne s add t 1
224if ne get prt e add t 1
225v = score e t pol
226if or not fnd gt v bv
227best = e
228bv = v
229fnd = true
230end
231end
232end
233end
234if not fnd
235exit 8
236end
237if ne get pfl best 0
238if ge get nu best div get hd 3 2
239put ac 6 add get ac 6 1
240end
241put pfl best 0
242end
243put stp best 0
244end
245
246note pfuse: the first demand of prefetched expert id; count it if the prefetch was warm.
247fn pfuse id ulong to void
248if ge get nu id div get hd 3 2
249put ac 5 add get ac 5 1
250end
251put pfl id 0
252end
253
254note access: one router choice (t,l,k) against cache capacity c.
255fn access t ulong l ulong k ulong c ulong pol ulong to void
256id = ulong get ids slot t l k
257if ne get stp id 0
258put ac 0 add get ac 0 1
259if ne get pfl id 0
260pfuse id
261end
262else
263put ac 1 add get ac 1 1
264if ge t div get hd 3 2
265put ac 2 add get ac 2 1
266end
267if ge get ac 3 c
268evict t pol
269else
270put ac 3 add get ac 3 1
271end
272end
273put stp id add t 1
274put nu id ulong get nxt add mul t get hd 2 k
275end
276
277note simlayer: replay layer l from an empty cache of capacity c.
278fn simlayer l ulong c ulong pol ulong to void
279for e ulong get hd 0
280put stp e 0
281put nu e 0
282end
283put ac 3 0
284for t ulong get hd 3
285for k ulong get hd 2
286access t l k c pol
287end
288end
289end
290
291note pfstep: before the demands of step s, make the H hinted experts resident and protect them in
292note step s. A missing one is fetched: flagged in pfl, with its issue step s in nu. cd 0 (hlru)
293note also marks every hinted expert used in step s; cd 1 (hcold) puts a fetched one at the LRU end
294note (stamp 1) and leaves the stamp of a resident one alone.
295fn pfstep s ulong c ulong cd ulong to void
296h0 = get hd 6
297for h ulong h0
298v = ulong get hin add mul s h0 h
299if eq get stp v 0
300if ge s div get hd 3 2
301put ac 4 add get ac 4 1
302end
303if ge get ac 3 c
304evict s 0
305else
306put ac 3 add get ac 3 1
307end
308put pfl v 1
309put nu v s
310put stp v 1
311end
312if eq cd 0
313put stp v add s 1
314end
315put prt v add s 1
316end
317end
318
319note simhint: replay the flattened (shared-cache) trace under LRU with the hints at capacity c,
320note hint mode cd as in pfstep.
321fn simhint c ulong cd ulong to void
322for e ulong get hd 0
323put stp e 0
324put nu e 0
325put pfl e 0
326put prt e 0
327end
328put ac 3 0
329for t ulong get hd 3
330pfstep t c cd
331for k ulong get hd 2
332access t 0 k c 0
333end
334end
335end
336
337note run: every layer at capacity c under policy pol; append the result line tagged with
338note fragment tg+pol.
339fn run c ulong p ulong pol ulong tg ulong to void
340put ac 0 0
341put ac 1 0
342put ac 2 0
343for l ulong get hd 1
344prep l
345simlayer l c pol
346end
347frag add tg pol
348kv 7 c
349kv 8 p
350kv 9 get ac 0
351kv 10 get ac 1
352kv 11 get ac 2
353t = get hd 3
354kv 12 mul mul get hd 1 get hd 2 sub t div t 2
355badd 10
356end
357
358note runh: the hinted shared LRU replay at capacity c in hint mode cd; append its hlru (cd 0,
359note fragment 15) or hcold (cd 1, fragment 20) line.
360fn runh c ulong p ulong cd ulong to void
361for i ulong 7
362put ac i 0
363end
364prep 0
365simhint c cd
366frag add 15 mul 5 cd
367kv 7 c
368kv 8 p
369kv 16 get hd 6
370kv 9 get ac 0
371kv 10 get ac 1
372kv 11 get ac 2
373kv 17 get ac 4
374kv 18 get ac 5
375kv 19 get ac 6
376t = get hd 3
377kv 12 mul get hd 2 sub t div t 2
378badd 10
379end
380
381note globst: add o to the K ids of step s of the one-layer view. glob calls it with o = l*E for
382note s = t*L+l, so (t,l,k) keeps its slot (t*L+l)*K+k and its id e becomes virtual expert l*E+e.
383fn globst s ulong o ulong to void
384k0 = get hd 2
385for k ulong k0
386i = add mul s k0 k
387put ids i ushort add ulong get ids i o
388end
389end
390
391note globh: the same rewrite for the H hint ids of step s.
392fn globh s ulong o ulong to void
393h0 = get hd 6
394for h ulong h0
395i = add mul s h0 h
396put hin i ushort add ulong get hin i o
397end
398end
399
400note glob: rewrite the trace in place as one layer of L*E virtual experts and T*L steps.
401note Needs L*E <= 65536 (ids are ushort).
402fn glob to void
403e0 = get hd 0
404l0 = get hd 1
405for s ulong mul get hd 3 l0
406globst s mul rem s l0 e0
407globh s mul rem s l0 e0
408end
409put hd 0 mul e0 l0
410put hd 3 mul get hd 3 l0
411put hd 1 1
412end
413
414note ldstep: parse the K ids of step-major row s (s = t*L+l) into ids; return 0 or exit code 7
415note (id out of range) or 8 (id repeated in the row).
416fn ldstep s ulong to int
417k0 = get hd 2
418b = mul s k0
419for k ulong k0
420v = num
421if ge v get hd 0
422ret 7
423end
424for j ulong k
425if eq ulong get ids add b j v
426ret 8
427end
428end
429put ids add b k ushort v
430end
431ret 0
432end
433
434note ldhrow: parse the H hint ids of row s into hin; return 0 or exit code 7.
435fn ldhrow s ulong to int
436h0 = get hd 6
437for h ulong h0
438v = num
439if ge v get hd 0
440ret 7
441end
442put hin add mul s h0 h ushort v
443end
444ret 0
445end
446
447note ldhint: parse H and the T*L*H hint ids that follow the trace; return 0 or an exit code.
448fn ldhint to int
449h = num
450t = get hd 3
451l = get hd 1
452if gt h 16
453ret 4
454end
455if gt mul l get hd 0 65536
456ret 4
457end
458if gt mul h l div 8388608 t
459ret 4
460end
461put hd 6 h
462put hd 7 1
463for s ulong mul t l
464r = ldhrow s
465if ne r 0
466ret r
467end
468end
469ret 0
470end
471
472note load: parse the header and the ids; return 0 or an exit code. T*L <= 1000000 keeps every
473note loop over steps, rows and flattened steps within the 1000000 passes a for loop may make.
474fn load to int
475e = num
476l = num
477k = num
478t = num
479if or or lt e 1 gt e 1024 or lt k 1 gt k 16
480ret 4
481end
482if or lt l 1 lt t 2
483ret 4
484end
485if or gt t 1000000 gt l div 1000000 t
486ret 4
487end
488if gt l div 8388608 mul t k
489ret 4
490end
491put hd 0 e
492put hd 1 l
493put hd 2 k
494put hd 3 t
495for s ulong mul t l
496r = ldstep s
497if ne r 0
498ret r
499end
500end
501if lt skip get hd 5
502q = ldhint
503if ne q 0
504ret q
505end
506end
507if lt skip get hd 5
508ret 6
509end
510ret 0
511end
512
513fn main to int
514r = slurp
515if ne r 0
516ret r
517end
518r = load
519if ne r 0
520ret r
521end
522put pc 0 25
523put pc 1 40
524put pc 2 50
525put pc 3 60
526put pc 4 64
527put pc 5 70
528put pc 6 80
529put pc 7 90
530kv 0 get hd 0
531kv 1 get hd 1
532kv 2 get hd 2
533kv 3 get hd 3
534frag 4
535for pol ulong 2
536for j ulong 8
537c = div mul get hd 0 get pc j 100
538if ge c get hd 2
539run c get pc j pol 5
540end
541end
542end
543if le mul get hd 0 get hd 1 65536
544glob
545for pol ulong 2
546for j ulong 8
547c = div mul get hd 0 get pc j 100
548if ge c get hd 2
549run c get pc j pol 13
550end
551end
552end
553if ne get hd 7 0
554for cd ulong 2
555for j ulong 8
556c = div mul get hd 0 get pc j 100
557if ge c add get hd 2 get hd 6
558runh c get pc j cd
559end
560end
561end
562end
563end
564ret flush
565end