okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

waveringanaserve: restore the DeepSeek V4.1 server paths, tools and headers removed upstream, keeping the native picture tower firstba8745e13

main
4.5 KiB107 linesraw
1"""predsim.py - token-level prediction of a decode step's experts from the token history (n-gram match), as a prefetch into an LRU
2cache, on DS4_TRACE decode traces.
3
4  predsim.py <trace> <prompt ids file> <output ids file> [--cap GB] [--warm N] [--budget MB] [--k 2]
5
6Before step i (its token known: the one the last step sampled) the experts of the step after the previous occurrence of the same
7k-gram are the guess for every layer.  They are read ahead (up to --budget MB a step, hottest layers first), so a miss at use time
8is only the ones the guess missed.  Reports demand hit rate, SSD MB a step (demand + wasted guesses) and the guess's recall /
9precision.  The engine's lead time for these reads is a whole step (the SSD is idle while the CPU lane computes)."""
10import argparse
11import collections
12import sys
13sys.path.insert(0, r'C:\Users\AI-Server\Desktop\Strata\GitHub\wt-dsv41-win\tools\dsv41\win')
14import cachesim as cs
15
16
17def main():
18    ap = argparse.ArgumentParser()
19    ap.add_argument('trace')
20    ap.add_argument('prompt')
21    ap.add_argument('out')
22    ap.add_argument('--cap', type=float, default=36.0)
23    ap.add_argument('--warm', type=int, default=150)
24    ap.add_argument('--budget', type=float, default=400.0, help='MB of reads ahead per step')
25    ap.add_argument('--k', type=int, default=2)
26    ap.add_argument('--sizes', default=r'C:\ds-ista-work\sizes.json')
27    a = ap.parse_args()
28    size = cs.Sizes(a.sizes)
29    st = cs.read_trace(a.trace)
30    p = [int(x) for x in open(a.prompt).read().strip().split(',')]
31    o = [int(x) for x in open(a.out).read().strip().strip(',').split(',')]
32    S = p + o
33    cap = a.cap * 1e9
34    routes = {}
35    for i, s in enumerate(st):
36        if i == 0:
37            continue
38        routes[i] = {layer: list(dict.fromkeys(rows[0])) for layer, rows, _ in s}
39    pos = lambda i: len(p) + i - 1  # noqa: E731
40    for mode in ('lru', 'lru+hist'):
41        od = collections.OrderedDict()
42        used = 0
43        last = {}
44        n = hit = 0
45        demand_b = pref_b = wasted_b = 0.0
46        rec = tot = prec = ptot = 0
47        steps = 0
48        for i in range(1, len(st)):
49            key = tuple(S[pos(i) - a.k + 1:pos(i) + 1])
50            pred = None
51            if mode == 'lru+hist' and key in last:
52                pred = routes[last[key]]
53            last[key] = i
54            counted = i >= a.warm
55            pend = {}
56            if pred is not None:
57                budget = a.budget * 1e6
58                for l in range(40):
59                    for e in pred[l]:
60                        kk = (l, e)
61                        if kk in od or kk in pend:
62                            continue
63                        if budget < size(l):
64                            continue
65                        budget -= size(l)
66                        pend[kk] = size(l)
67                if counted:
68                    pref_b += sum(pend.values())
69                    for l in range(40):
70                        a_ = set(routes[i][l])
71                        b_ = set(pred[l])
72                        rec += len(a_ & b_)
73                        tot += len(a_)
74                        prec += len(a_ & b_)
75                        ptot += len(b_)
76            steps += counted
77            for l in range(40):
78                sz = size(l)
79                for e in routes[i][l]:
80                    kk = (l, e)
81                    if counted:
82                        n += 1
83                    if kk in od:
84                        od.move_to_end(kk)
85                        if counted:
86                            hit += 1
87                        continue
88                    if kk in pend:   # read ahead: arrived in time
89                        del pend[kk]
90                        if counted:
91                            hit += 1
92                    elif counted:
93                        demand_b += sz
94                    od[kk] = sz
95                    used += sz
96                    while used > cap:
97                        _, s0 = od.popitem(last=False)
98                        used -= s0
99            if counted:
100                wasted_b += sum(pend.values())   # read ahead, not used this step (they stay in the cache only if inserted: not here)
101        print('%-9s cap %.0f GB k=%d: demand hit %.1f%%; SSD per step: demand %.0f MB + guesses %.0f MB (wasted %.0f); guess recall %.1f%% precision %.1f%%' % (
102            mode, a.cap, a.k, 100 * hit / max(1, n), demand_b / steps / 1e6, pref_b / steps / 1e6, wasted_b / steps / 1e6,
103            100 * rec / max(1, tot), 100 * prec / max(1, ptot)))
104
105
106if __name__ == '__main__':
107    main()