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 / fixtures / oracle.wl

128 lines · 6370 bytes Raw Blame History
1(* SPDX-License-Identifier: Apache-2.0 *)
2(* Independent LRU and Belady (OPT) oracle for om/residency/residency.om, in the Wolfram Language.
3 residencyOracle[e, l, k, t, ids] returns the simulator's expected stdout for a trace with header
4 E L K T and the flat step-major id list ids (step t, layer l, slot k);
5 residencyOracle[e, l, k, t, ids, h, hids] the same with the hint section H = h and the flat
6 row-major hint ids hids. It is written from the residency.om contract in the file header, not
7 from its code:
8 - lru/opt: each layer replays alone from an empty cache of c = Floor[E p / 100] experts;
9 - glru/gopt (only when L E <= 65536): one cache of c = Floor[L E p / 100] for all layers, steps
10 taken in serving order (t, l) -> t L + l, virtual expert l E + e;
11 - a line exists only when c >= K; a miss with c experts resident evicts the resident expert
12 with the largest score that was not already used in the current step, lowest id on ties;
13 LRU score: steps since last use; OPT score: step of next use (number of steps when none);
14 - warm counts misses at steps s >= Floor[steps / 2]; warm_acc = accesses at those steps;
15 - hlru and hcold (with hints, after glru/gopt, only when c >= K + H): see hintReplay. *)
16
17replay[steps_, n_, c_, pol_] := Module[{nT = Length[steps], nK = Length[First[steps]], last, nxt,
18 stamp, nextUse, hits = 0, miss = 0, warm = 0, resident = 0, id, cand, sc, best},
19 (* nxt[[s, j]]: 0-based step of the next use of steps[[s, j]] after step s, else nT *)
20 last = ConstantArray[nT, n];
21 nxt = ConstantArray[0, {nT, nK}];
22 Do[
23 Do[nxt[[s, j]] = last[[steps[[s, j]] + 1]], {j, nK}];
24 Do[last[[steps[[s, j]] + 1]] = s - 1, {j, nK}],
25 {s, nT, 1, -1}];
26 stamp = ConstantArray[0, n]; (* 0: not resident; else 1-based step of last use *)
27 nextUse = ConstantArray[0, n];
28 Do[
29 Do[
30 id = steps[[s, j]] + 1;
31 If[stamp[[id]] != 0, hits++,
32 miss++;
33 If[s - 1 >= Floor[nT/2], warm++];
34 If[resident >= c,
35 cand = Select[Range[n], stamp[[#]] != 0 && stamp[[#]] != s &];
36 sc = If[pol == 0, s - stamp[[cand]], nextUse[[cand]]];
37 best = cand[[First[FirstPosition[sc, Max[sc]]]]];
38 stamp[[best]] = 0,
39 resident++]];
40 stamp[[id]] = s;
41 nextUse[[id]] = nxt[[s, j]],
42 {j, nK}],
43 {s, nT}];
44 {hits, miss, warm}];
45
46line[tag_, c_, p_, {h_, m_, w_}, acc_] := StringJoin[tag, " cap=", ToString[c], " pct=", ToString[p],
47 " hits=", ToString[h], " miss=", ToString[m], " warm_miss=", ToString[w], " warm_acc=", ToString[acc]];
48
49(* hintReplay[steps, hsteps, n, c, cold]: the shared-cache LRU replay of replay[] with prefetch
50 hints. Before the demands of step s, every hinted expert of hsteps[[s]] is made resident (a
51 missing one is fetched as a prefetch, making room exactly like a miss) and protected from
52 eviction during step s; then the demands of steps[[s]] are served.
53 cold = False (hlru): a hinted expert also counts as used in step s.
54 cold = True (hcold): a hint does not count as a use; a fetched expert gets last-use step 1, the
55 oldest possible, so ties with it go to the lowest id.
56 Returns the demand counts {hits, miss, warm} and the counts of prefetches issued at warm steps
57 {issued, demanded before eviction, evicted undemanded}. *)
58hintReplay[steps_, hsteps_, n_, c_, cold_] := Module[{nT = Length[steps], half, stamp, prot, pf,
59 issue, hits = 0, miss = 0, warm = 0, wpf = 0, wused = 0, wwaste = 0, resident = 0, makeRoom, id},
60 half = Floor[nT/2];
61 stamp = ConstantArray[0, n];
62 prot = ConstantArray[0, n];
63 pf = ConstantArray[False, n];
64 issue = ConstantArray[0, n];
65 makeRoom[s_] := If[resident >= c,
66 Module[{cand, sc, best},
67 cand = Select[Range[n], stamp[[#]] != 0 && stamp[[#]] != s && prot[[#]] != s &];
68 sc = s - stamp[[cand]];
69 best = cand[[First[FirstPosition[sc, Max[sc]]]]];
70 If[pf[[best]], If[issue[[best]] >= half, wwaste++]; pf[[best]] = False];
71 stamp[[best]] = 0],
72 resident++];
73 Do[
74 Do[
75 id = hsteps[[s, j]] + 1;
76 If[stamp[[id]] == 0,
77 If[s - 1 >= half, wpf++];
78 makeRoom[s];
79 pf[[id]] = True;
80 issue[[id]] = s - 1;
81 stamp[[id]] = 1];
82 If[! cold, stamp[[id]] = s];
83 prot[[id]] = s,
84 {j, Length[hsteps[[s]]]}];
85 Do[
86 id = steps[[s, j]] + 1;
87 If[stamp[[id]] != 0,
88 hits++;
89 If[pf[[id]], If[issue[[id]] >= half, wused++]; pf[[id]] = False],
90 miss++;
91 If[s - 1 >= half, warm++];
92 makeRoom[s]];
93 stamp[[id]] = s,
94 {j, Length[steps[[s]]]}],
95 {s, nT}];
96 {{hits, miss, warm}, {wpf, wused, wwaste}}];
97
98hline[tag_, c_, p_, h_, {{hh_, m_, w_}, {f_, u_, x_}}, acc_] := StringJoin[tag, " cap=", ToString[c],
99 " pct=", ToString[p], " hint=", ToString[h], " hits=", ToString[hh], " miss=", ToString[m],
100 " warm_miss=", ToString[w], " warm_pf=", ToString[f], " warm_used=", ToString[u],
101 " warm_waste=", ToString[x], " warm_acc=", ToString[acc]];
102
103residencyOracle[e_, l_, k_, t_, ids_, h_ : None, hids_ : {}] := Module[{pcts = {25, 40, 50, 60,
104 64, 70, 80, 90}, tr = ArrayReshape[ids, {t, l, k}], out, g, gsteps, htr, hsteps},
105 out = {StringJoin["trace E=", ToString[e], " L=", ToString[l], " K=", ToString[k], " T=", ToString[t]]};
106 Do[
107 With[{c = Floor[e p/100]},
108 If[c >= k, AppendTo[out, line[{"lru", "opt"}[[pol + 1]], c, p,
109 Total[Table[replay[tr[[All, ll]], e, c, pol], {ll, l}]], l k (t - Floor[t/2])]]]],
110 {pol, {0, 1}}, {p, pcts}];
111 If[l e <= 65536,
112 g = l e;
113 gsteps = Flatten[Table[tr[[tt, ll]] + (ll - 1) e, {tt, t}, {ll, l}], 1];
114 Do[
115 With[{c = Floor[g p/100]},
116 If[c >= k, AppendTo[out, line[{"glru", "gopt"}[[pol + 1]], c, p,
117 replay[gsteps, g, c, pol], k (t l - Floor[t l/2])]]]],
118 {pol, {0, 1}}, {p, pcts}];
119 If[IntegerQ[h],
120 hsteps = If[h == 0, ConstantArray[{}, t l],
121 htr = ArrayReshape[hids, {t, l, h}];
122 Flatten[Table[htr[[tt, ll]] + (ll - 1) e, {tt, t}, {ll, l}], 1]];
123 Do[
124 With[{c = Floor[g p/100]},
125 If[c >= k + h, AppendTo[out, hline[{"hlru", "hcold"}[[cold + 1]], c, p, h,
126 hintReplay[gsteps, hsteps, g, c, cold == 1], k (t l - Floor[t l/2])]]]],
127 {cold, {0, 1}}, {p, pcts}]]];
128 StringJoin[Riffle[out, FromCharacterCode[10]], FromCharacterCode[10]]];