okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ba8745e13
main
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 4predsim.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 (): 18ap = argparse .ArgumentParser () 19ap .add_argument ('trace' ) 20ap .add_argument ('prompt' ) 21ap .add_argument ('out' ) 22ap .add_argument ('--cap' ,type = float ,default = 36.0 ) 23ap .add_argument ('--warm' ,type = int ,default = 150 ) 24ap .add_argument ('--budget' ,type = float ,default = 400.0 ,help = 'MB of reads ahead per step' ) 25ap .add_argument ('--k' ,type = int ,default = 2 ) 26ap .add_argument ('--sizes' ,default = r'C:\ds-ista-work\sizes.json' ) 27a = ap .parse_args () 28size = cs .Sizes (a .sizes ) 29st = cs .read_trace (a .trace ) 30p = [int (x )for x in open (a .prompt ).read ().strip ().split (',' )] 31o = [int (x )for x in open (a .out ).read ().strip ().strip (',' ).split (',' )] 32S = p + o 33cap = a .cap * 1e9 34routes = {} 35for i ,s in enumerate (st ): 36if i == 0 : 37continue 38routes [i ]= {layer :list (dict .fromkeys (rows [0 ]))for layer ,rows ,_ in s } 39pos = lambda i :len (p )+ i - 1 # noqa: E731 40for mode in ('lru' ,'lru+hist' ): 41od = collections .OrderedDict () 42used = 0 43last = {} 44n = hit = 0 45demand_b = pref_b = wasted_b = 0.0 46rec = tot = prec = ptot = 0 47steps = 0 48for i in range (1 ,len (st )): 49key = tuple (S [pos (i )- a .k + 1 :pos (i )+ 1 ]) 50pred = None 51if mode == 'lru+hist' and key in last : 52pred = routes [last [key ]] 53last [key ]= i 54counted = i >= a .warm 55pend = {} 56if pred is not None : 57budget = a .budget * 1e6 58for l in range (40 ): 59for e in pred [l ]: 60kk = (l ,e ) 61if kk in od or kk in pend : 62continue 63if budget < size (l ): 64continue 65budget -= size (l ) 66pend [kk ]= size (l ) 67if counted : 68pref_b += sum (pend .values ()) 69for l in range (40 ): 70a_ = set (routes [i ][l ]) 71b_ = set (pred [l ]) 72rec += len (a_ & b_ ) 73tot += len (a_ ) 74prec += len (a_ & b_ ) 75ptot += len (b_ ) 76steps += counted 77for l in range (40 ): 78sz = size (l ) 79for e in routes [i ][l ]: 80kk = (l ,e ) 81if counted : 82n += 1 83if kk in od : 84od .move_to_end (kk ) 85if counted : 86hit += 1 87continue 88if kk in pend :# read ahead: arrived in time 89del pend [kk ] 90if counted : 91hit += 1 92elif counted : 93demand_b += sz 94od [kk ]= sz 95used += sz 96while used > cap : 97_ ,s0 = od .popitem (last = False ) 98used -= s0 99if counted : 100wasted_b += sum (pend .values ())# read ahead, not used this step (they stay in the cache only if inserted: not here) 101'%-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%%' % ( 102mode ,a .cap ,a .k ,100 * hit / max (1 ,n ),demand_b / steps / 1e6 ,pref_b / steps / 1e6 ,wasted_b / steps / 1e6 , 103100 * rec / max (1 ,tot ),100 * prec / max (1 ,ptot ))) 104 105 106if __name__ == '__main__' : 107main ()