okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ba8745e13
main
1"""h100sim.py - replay the H100 routing corpus (1.78M tokens, code/agent/chat/multi/math/long, DS4_TRACE format, see 2phaseA-tests/h100/trace_tools.py) through an LRU expert cache at this PC's sizes, with and without a profile warm start. 3 4h100sim.py <trace.bin[.zst]> <profile.bin> [--tokens N] [--skip S] [--caps 30,40,44] [--sizes sizes.json] 5 6Prints the demand hit rate by bytes after a warm-up of `--warm` tokens, for: empty start LRU; LRU started with the profile's 7hottest experts in (the engine's preload) and the same cold-start window's hit rate (first 2000 tokens), and a static top-N 8(profile only, no adaptation).""" 9import argparse 10import collections 11import io 12import json 13import struct 14import sys 15 16import numpy as np 17 18 19def decode (path ,max_tokens ,skip ): 20if path .endswith ('.zst' ): 21import zstandard 22raw = zstandard .ZstdDecompressor ().stream_reader (open (path ,'rb' )).read () 23else : 24raw = open (path ,'rb' ).read () 25buf = np .frombuffer (raw ,dtype = np .uint8 ) 26pos ,n = 0 ,len (buf ) 27per = {l : []for l in range (40 )} 28got = [0 ]* 40 29while pos < n : 30b0 = int (buf [pos ]) 31if b0 & 0x80 : 32l = b0 & 0x7F 33T = int (buf [pos + 1 ])| (int (buf [pos + 2 ])<< 8 ) 34pos += 3 35else : 36l = b0 37T = int (buf [pos + 1 ])& 0x7F 38pos += 2 39sz = T * 12 40if pos + sz > n : 41break 42per [l ].append (buf [pos :pos + sz ].view (np .int16 ).reshape (T ,6 )) 43pos += sz 44if min (got )>= skip + max_tokens : 45break 46got [l ]+= T 47arr = [] 48for l in range (40 ): 49a = np .concatenate (per [l ])[skip :skip + max_tokens ] 50arr .append (a ) 51m = min (len (a )for a in arr ) 52return np .stack ([a [:m ]for a in arr ],axis = 1 )# [tokens][layer][6] 53 54 55def lru (acc_iter ,size ,cap ,init = None ,warm = 0 ): 56od = collections .OrderedDict () 57used = 0 58if init : 59for k in init : 60od [k ]= size [k [0 ]] 61used += size [k [0 ]] 62hb = tb = 0.0 63first_hb = first_tb = 0.0 64for i , (t ,k )in enumerate (acc_iter ): 65sz = size [k [0 ]] 66cnt = t >= warm 67early = t < 2000 68if k in od : 69od .move_to_end (k ) 70if cnt : 71hb += sz 72if early : 73first_hb += sz 74else : 75od [k ]= sz 76used += sz 77while used > cap : 78_ ,s0 = od .popitem (last = False ) 79used -= s0 80if cnt : 81tb += sz 82if early : 83first_tb += sz 84return 100 * hb / max (tb ,1 ),100 * first_hb / max (first_tb ,1 ) 85 86 87def main (): 88ap = argparse .ArgumentParser () 89ap .add_argument ('trace' ) 90ap .add_argument ('profile' ) 91ap .add_argument ('--tokens' ,type = int ,default = 40000 ) 92ap .add_argument ('--skip' ,type = int ,default = 0 ) 93ap .add_argument ('--warm' ,type = int ,default = 4000 ) 94ap .add_argument ('--caps' ,default = '30,40' ) 95ap .add_argument ('--sizes' ,default = r'C:\ds-ista-work\sizes.json' ) 96a = ap .parse_args () 97d = json .load (open (a .sizes )) 98size = {int (k ):float (v )for k ,v in d ['exp_layer' ].items ()} 99tr = decode (a .trace ,a .tokens ,a .skip ) 100prof = np .fromfile (a .profile ,dtype = np .uint32 ).reshape (- 1 ,384 )[:40 ] 101'trace' ,tr .shape ,'tokens' ,len (tr )) 102 103def acc (): 104for t in range (len (tr )): 105for l in range (40 ): 106for e in tr [t ,l ]: 107yield t , (l ,int (e )) 108dens = sorted (((prof [l ,e ]/ size [l ],l ,e )for l in range (40 )for e in range (384 )),reverse = True ) 109for cg in [float (x )for x in a .caps .split (',' )]: 110cap = cg * 1e9 111r0 ,f0 = lru (acc (),size ,cap ,None ,a .warm ) 112order ,used = [],0.0 113for _ ,l ,e in dens : 114if used + size [l ]> cap : 115break 116order .append ((l ,e )) 117used += size [l ] 118r1 ,f1 = lru (acc (),size ,cap ,list (reversed (order )),a .warm )# hottest last = most recent 119# static: top-N of the profile, no adaptation 120sel = set (order ) 121hb = tb = 0.0 122for t ,k in acc (): 123if t >= a .warm : 124tb += size [k [0 ]] 125if k in sel : 126hb += size [k [0 ]] 127'cap %.0f GB: LRU from empty: steady hit %.1f%% (first 2000 tokens %.1f%%); LRU from the profile\'s hottest: steady %.1f%% (first 2000 tokens %.1f%%); static top-N alone %.1f%%' % ( 128cg ,r0 ,f0 ,r1 ,f1 ,100 * hb / tb )) 129 130 131if __name__ == '__main__' : 132main ()