Files
leleka/engine/strata/tools/dsv41/win/predsim.py
T
Pavlo RatushnyiandClaude Sonnet 5.5 87e2633afe engine/strata: bring the Strata subtree to upstream 41cf05a0 (engine 0.1.50) with the local patches re-applied
Documented tree replacement (the history has no git-subtree metadata): 3-way merge of the old subtree
(570201a:engine/strata) against upstream 41cf05a06a25dd29103b7b27a2279a3f60c21876 (2026-10-25), merge base =
the old pin d9ab8435 (tree-identical to upstream 5b734e42; d9ab8435 is not an ancestor of upstream main).

Local patches re-applied by hand where they conflicted (15 files): STRATA_CUDA_STATIC profile, experimental KAR
experts and KAR PLE arms, keyless V-only arm (now beside upstream's K8V4 hybrid KV and fused norm+rope). Details,
conflict table and left-out files are in docs/upstream.md.

Left out (weights, captures, logs, media): new bench/results (2022 files), binary media and app assets (61),
docs/work/evidence (24), *.bin / *.gguf data files, and the 7 files the first import already left out.

scripts/build-cpu.sh: build the 11 CPU tests upstream registered since the old pin (they ran as "Not Run").
Checked: build-cpu.sh 16/16 ctest in WSL; sm_86 CUDA objects of the patched files compile, also with the KAR
macros on. No GPU run, no quality gate.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-25 12:49:15 +01:00

108 lines
4.5 KiB
Python

"""predsim.py - token-level prediction of a decode step's experts from the token history (n-gram match), as a prefetch into an LRU
cache, on DS4_TRACE decode traces.
predsim.py <trace> <prompt ids file> <output ids file> [--cap GB] [--warm N] [--budget MB] [--k 2]
Before step i (its token known: the one the last step sampled) the experts of the step after the previous occurrence of the same
k-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
is only the ones the guess missed. Reports demand hit rate, SSD MB a step (demand + wasted guesses) and the guess's recall /
precision. The engine's lead time for these reads is a whole step (the SSD is idle while the CPU lane computes)."""
import argparse
import collections
import sys
sys.path.insert(0, r'C:\Users\AI-Server\Desktop\Strata\GitHub\wt-dsv41-win\tools\dsv41\win')
import cachesim as cs
def main():
ap = argparse.ArgumentParser()
ap.add_argument('trace')
ap.add_argument('prompt')
ap.add_argument('out')
ap.add_argument('--cap', type=float, default=36.0)
ap.add_argument('--warm', type=int, default=150)
ap.add_argument('--budget', type=float, default=400.0, help='MB of reads ahead per step')
ap.add_argument('--k', type=int, default=2)
ap.add_argument('--sizes', default=r'C:\ds-ista-work\sizes.json')
a = ap.parse_args()
size = cs.Sizes(a.sizes)
st = cs.read_trace(a.trace)
p = [int(x) for x in open(a.prompt).read().strip().split(',')]
o = [int(x) for x in open(a.out).read().strip().strip(',').split(',')]
S = p + o
cap = a.cap * 1e9
routes = {}
for i, s in enumerate(st):
if i == 0:
continue
routes[i] = {layer: list(dict.fromkeys(rows[0])) for layer, rows, _ in s}
pos = lambda i: len(p) + i - 1 # noqa: E731
for mode in ('lru', 'lru+hist'):
od = collections.OrderedDict()
used = 0
last = {}
n = hit = 0
demand_b = pref_b = wasted_b = 0.0
rec = tot = prec = ptot = 0
steps = 0
for i in range(1, len(st)):
key = tuple(S[pos(i) - a.k + 1:pos(i) + 1])
pred = None
if mode == 'lru+hist' and key in last:
pred = routes[last[key]]
last[key] = i
counted = i >= a.warm
pend = {}
if pred is not None:
budget = a.budget * 1e6
for l in range(40):
for e in pred[l]:
kk = (l, e)
if kk in od or kk in pend:
continue
if budget < size(l):
continue
budget -= size(l)
pend[kk] = size(l)
if counted:
pref_b += sum(pend.values())
for l in range(40):
a_ = set(routes[i][l])
b_ = set(pred[l])
rec += len(a_ & b_)
tot += len(a_)
prec += len(a_ & b_)
ptot += len(b_)
steps += counted
for l in range(40):
sz = size(l)
for e in routes[i][l]:
kk = (l, e)
if counted:
n += 1
if kk in od:
od.move_to_end(kk)
if counted:
hit += 1
continue
if kk in pend: # read ahead: arrived in time
del pend[kk]
if counted:
hit += 1
elif counted:
demand_b += sz
od[kk] = sz
used += sz
while used > cap:
_, s0 = od.popitem(last=False)
used -= s0
if counted:
wasted_b += sum(pend.values()) # read ahead, not used this step (they stay in the cache only if inserted: not here)
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%%' % (
mode, a.cap, a.k, 100 * hit / max(1, n), demand_b / steps / 1e6, pref_b / steps / 1e6, wasted_b / steps / 1e6,
100 * rec / max(1, tot), 100 * prec / max(1, ptot)))
if __name__ == '__main__':
main()