okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b0fc432ed
main
1"""batch_gate.py - the parallel-requests gate for strata-q36 (the tools/batch_test.py idea over the serve protocol). 2 3python q36_batch_gate.py --exe E --args "--model M --max-context 8192 --batch 4" --slots 4 --n 128 [--golden G.json] 4[--long 3000] [--keys "temperature=0.7 seed=3"] [--out F.json] 5 61. every prompt alone (GEN) on the same engine -> solo tokens (and, with --golden, equal to the base engine's); 72. the first --slots prompts through BGEN, admitted one after the other, then decoding together -> every slot's tokens 8must equal its solo tokens; the decode rate of the window phase; 93. --waves: the 10 prompts in waves of --slots (slot reuse: the cached state of a finished slot is taken over); 104. --long L: a prompt of about L tokens admitted into the last slot while the others decode (its prompt chunks run 11between their windows); the others' tokens must still equal their solo tokens, and so must the long one's. 12""" 13from pathlib import Path 14import argparse ,json ,shlex ,sys 15sys .path .insert (0 ,__file__ .rsplit ("/" ,1 )[0 ]if "/" in __file__ else "." ) 16from q36_harness import Eng ,load_prompts 17 18 19def same (a ,b ): 20return a == b 21 22 23ap = argparse .ArgumentParser () 24ap .add_argument ("--exe" ,required = True ) 25ap .add_argument ("--args" ,default = "" ) 26ap .add_argument ("--slots" ,type = int ,default = 4 ) 27ap .add_argument ("--n" ,type = int ,default = 128 ) 28ap .add_argument ("--prompts" ,default = str (Path (__file__ ).resolve ().parent / "parity_prompts.ids" )) 29ap .add_argument ("--golden" ,default = None ) 30ap .add_argument ("--keys" ,default = "" ) 31ap .add_argument ("--long" ,type = int ,default = 0 ) 32ap .add_argument ("--waves" ,action = "store_true" ) 33ap .add_argument ("--skip-solo-bench" ,action = "store_true" ) 34ap .add_argument ("--out" ,default = None ) 35ap .add_argument ("--log" ,default = None ) 36ap .add_argument ("--env" ,default = "" ) 37a = ap .parse_args () 38env = dict (kv .split ("=" ,1 )for kv in a .env .split ()if "=" in kv ) 39eng = Eng (a .exe ,shlex .split (a .args ),env = env ,log = a .log ) 40"ready:" ,eng .ready ,"info:" ,eng .info ,"load %.1fs" % eng .load_s ,flush = True ) 41prompts = load_prompts (a .prompts ) 42ok = True 43res = {"args" :a .args ,"slots" :a .slots } 44 45# 1. solo 46solo ,solo_rate = [], [] 47for i ,ids in enumerate (prompts ): 48toks ,done ,ttft ,wall = eng .solo (ids ,a .n ,a .keys ) 49solo .append (toks ) 50solo_rate .append (len (toks )/ max (1e-9 ,wall - (ttft or 0 ))) 51"solo: %d prompts, decode %.1f tok/s median" % (len (solo ),sorted (solo_rate )[len (solo_rate )// 2 ]),flush = True ) 52if a .golden : 53g = json .load (open (a .golden ))["tokens" ] 54eq = [same (x ,y )for x ,y in zip (solo ,g )] 55"solo vs golden (base engine):" ,"identical %d of %d" % (sum (eq ),len (eq )),flush = True ) 56ok &= all (eq ) 57res ["solo_tok_s_median" ]= sorted (solo_rate )[len (solo_rate )// 2 ] 58 59# 2. batch 60S = min (a .slots ,len (prompts )) 61reqs = [{"ids" :prompts [i ],"n" :a .n ,"slot" :i ,"keys" :a .keys }for i in range (S )] 62r = eng .batch (reqs ) 63eq = [same (r ["tokens" ][i ],solo [i ])for i in range (S )] 64"batch %d: %d of %d slots identical to solo | wall %.2fs, window phase %d tokens in %.2fs = %.1f tok/s (aggregate)" 65% (S ,sum (eq ),S ,r ["wall" ],r ["tail_tokens" ],r ["tail_s" ],r ["tail_tok_s" ]),flush = True ) 66for i ,e in enumerate (eq ): 67if not e : 68x ,y = r ["tokens" ][i ],solo [i ] 69k = next ((k for k in range (min (len (x ),len (y )))if x [k ]!= y [k ]),min (len (x ),len (y ))) 70" slot %d differs at token %d (lens %d / %d)" % (i ,k ,len (x ),len (y ))) 71ok &= all (eq ) 72res ["batch" ]= {"slots" :S ,"identical" :sum (eq ),"wall" :r ["wall" ],"tail_tok_s" :r ["tail_tok_s" ],"tail_tokens" :r ["tail_tokens" ]} 73 74# 3. waves (slot reuse) 75if a .waves : 76wave_ok = True 77for w0 in range (0 ,len (prompts ),S ): 78idx = list (range (w0 ,min (w0 + S ,len (prompts )))) 79rq = [{"ids" :prompts [i ],"n" :a .n ,"slot" :j ,"keys" :a .keys }for j ,i in enumerate (idx )] 80rr = eng .batch (rq ) 81for j ,i in enumerate (idx ): 82if not same (rr ["tokens" ][j ],solo [i ]): 83wave_ok = False 84" wave %d prompt %d differs" % (w0 // S ,i )) 85"waves of %d: %s" % (S ,"all identical" if wave_ok else "DIFFERENT" ),flush = True ) 86ok &= wave_ok 87 88# 4. a long prompt admitted while the others decode 89if a .long and S >= 2 : 90base = prompts [0 ] 91filler = [] 92while len (filler )< a .long : 93for p in prompts : 94filler += p [3 :- 8 ]if len (p )> 12 else p 95longids = filler [:a .long ]+ prompts [1 ][- 8 :] 96toks ,done ,ttft ,wall = eng .solo (longids ,48 ,a .keys ) 97reqs = [{"ids" :prompts [i ],"n" :a .n * 2 ,"slot" :i ,"keys" :a .keys }for i in range (S - 1 )] 98# the first S-1 are admitted first; then the long one is admitted while they decode 99longreq = {"ids" :longids ,"n" :48 ,"slot" :S - 1 ,"keys" :a .keys } 100solo2 = [] 101for i in range (S - 1 ): 102t2 ,_ ,_ ,_ = eng .solo (prompts [i ],a .n * 2 ,a .keys ) 103solo2 .append (t2 ) 104rr = eng .batch (reqs + [longreq ]) 105eq = [same (rr ["tokens" ][i ],solo2 [i ])for i in range (S - 1 )]+ [same (rr ["tokens" ][S - 1 ],toks )] 106"long prompt (%d tokens) admitted beside %d decoding slots: %s | admission stamps %s" 107% (len (longids ),S - 1 ,"%d of %d identical" % (sum (eq ),len (eq )), ["%.2f" % x for x in rr ["admit_stamps" ]]),flush = True ) 108ok &= all (eq ) 109 110res ["ok" ]= ok 111if a .out : 112json .dump (res ,open (a .out ,"w" )) 113eng .close () 114"GATE" ,"PASS" if ok else "FAIL" ,flush = True ) 115sys .exit (0 if ok else 1 )