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