okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
db4dd65e5
main
1"""proto_edge.py - the protocol's edges for parallel requests (engine level): max_new 1, BSTOP, a busy slot, STOP during an 2admission's prompt read, slot reuse (the held state), sampled requests in a batch against the same request alone, a 3request that reaches the context's end.""" 4from pathlib import Path 5import argparse ,shlex ,sys ,time 6sys .path .insert (0 ,__file__ .rsplit ("/" ,1 )[0 ]if "/" in __file__ else "." ) 7from q36_harness import Eng ,load_prompts 8 9ap = argparse .ArgumentParser () 10ap .add_argument ("--exe" ,required = True ) 11ap .add_argument ("--args" ,default = "" ) 12ap .add_argument ("--log" ,default = None ) 13a = ap .parse_args () 14eng = Eng (a .exe ,shlex .split (a .args ),log = a .log ) 15P = load_prompts (str (Path (__file__ ).resolve ().parent / "parity_prompts.ids" )) 16ok = True 17 18 19def check (name ,cond ,extra = "" ): 20global ok 21ok &= bool (cond ) 22"PASS " if cond else "FAIL " )+ name + (" - " + extra if extra else "" ),flush = True ) 23 24 25def lines_until (pred ,timeout = 120 ): 26out = [] 27t0 = time .time () 28while time .time ()- t0 < timeout : 29l = eng .line (timeout ) 30out .append (l ) 31if pred (l ): 32return out 33return out 34 35 36# 1. max_new 1: T, DONE, BADM slot 0 37eng .send ("BGEN 0 1 " + "," .join (map (str ,P [0 ]))) 38r = lines_until (lambda l :l .startswith ("BADM" )) 39check ("BGEN max_new 1 ends at the admission" ,r [- 1 ]== "BADM 0 0" and any (l .startswith ("T " )for l in r ),str (r [- 3 :])) 40 41# 2. a long answer, BSTOP after a few tokens, then a longer request on the held state 42eng .send ("BGEN 0 200 " + "," .join (map (str ,P [1 ]))) 43r = lines_until (lambda l :l .startswith ("BADM" )) 44check ("BGEN max_new 200 continues" ,r [- 1 ]== "BADM 0 1" ,r [- 1 ]) 45bts = [] 46while len (bts )< 5 : 47l = eng .line () 48if l .startswith ("BT 0" ): 49bts .append (int (l .split ()[2 ])) 50# the slot is busy: another BGEN for it is refused (BT lines of the running slot may come first) 51eng .send ("BGEN 0 5 " + "," .join (map (str ,P [2 ]))) 52seen = lines_until (lambda l :l .startswith ("ERR" )or l .startswith ("BADM" )) 53for l in seen : 54if l .startswith ("BT 0" ): 55bts .append (int (l .split ()[2 ])) 56check ("BGEN into a busy slot is refused" ,seen [- 1 ].startswith ("ERR BGEN" ),seen [- 1 ]) 57eng .send ("BSTOP 0" ) 58done = lines_until (lambda l :l .startswith ("BDONE 0" )) 59for l in done : 60if l .startswith ("BT 0" ): 61bts .append (int (l .split ()[2 ])) 62check ("BSTOP ends the slot with cancel" ,done [- 1 ].split ()[3 ]== "cancel" ,done [- 1 ]) 63first = [int (l .split ()[1 ])for l in r if l .startswith ("T " )] 64# the slot holds the prompt and every token a window fed (all but the last one): a continuation extends exactly that 65eng .send ("BGEN 0 3 " + "," .join (map (str ,P [1 ]+ first + bts ))) 66r2 = lines_until (lambda l :l .startswith ("BADM" )) 67dn = [l for l in r2 if l .startswith ("DONE" )][0 ].split () 68check ("a continuation reuses the slot's held state" ,int (dn [8 ])== len (P [1 ])+ len (first )+ len (bts )- 1 , 69"reused %s of %d" % (dn [8 ],len (P [1 ]+ first + bts ))) 70lines_until (lambda l :l .startswith ("BDONE 0" )) 71 72# 3. STOP during an admission's prompt read 73filler = [] 74while len (filler )< 3000 : 75for p in P : 76filler += p [3 :- 8 ] 77filler = filler [:3000 ] 78eng .send ("BGEN 1 8 " + "," .join (map (str ,filler ))) 79time .sleep (0.15 ) 80eng .send ("STOP" ) 81r3 = lines_until (lambda l :l .startswith ("BADM" )) 82dn = [l for l in r3 if l .startswith ("DONE" )][0 ].split () 83pp = [l for l in r3 if l .startswith ("PP " )] 84check ("STOP during the prompt read ends it with cancel" ,dn [5 ]== "cancel" and r3 [- 1 ]== "BADM 1 0" , 85"%d PP line(s), DONE %s" % (len (pp )," " .join (dn [:6 ]))) 86# the engine is in step: a normal request on the same slot works 87eng .send ("BGEN 1 6 " + "," .join (map (str ,P [3 ]))) 88r4 = lines_until (lambda l :l .startswith ("BADM" )) 89check ("a request after the cancelled read works" ,r4 [- 1 ]== "BADM 1 1" ,r4 [- 1 ]) 90lines_until (lambda l :l .startswith ("BDONE 1" )) 91 92# 4. sampled: batch against alone, same seed 93keys = "temperature=0.8 top_k=40 top_p=0.95 seed=11" 94toks ,_ ,_ ,_ = eng .solo (P [4 ],40 ,keys ) 95res = eng .batch ([{"ids" :P [4 ],"n" :40 ,"slot" :2 ,"keys" :keys }, {"ids" :P [5 ],"n" :40 ,"slot" :3 ,"keys" :"temperature=0.5 seed=3" }]) 96check ("a sampled request in a batch equals the same request alone" ,res ["tokens" ][0 ]== toks , 97"%d / %d tokens" % (len (res ["tokens" ][0 ]),len (toks ))) 98 99# 5. the context's end: a prompt of ctx - 40 tokens and max_new 400 finishes with length 100ctx = int (eng .ready .split ()[1 ]) 101long_prompt = (filler * 4 )[:ctx - 60 ] 102eng .send ("BGEN 3 400 " + "," .join (map (str ,long_prompt ))) 103r5 = lines_until (lambda l :l .startswith ("ERR" )or l .startswith ("BADM" )) 104check ("a request that cannot fit its answer is refused with the engine's message" ,r5 [- 1 ].startswith ("ERR prompt" ),r5 [- 1 ][:80 ]) 105eng .send ("BGEN 3 50 " + "," .join (map (str ,long_prompt [:ctx - 80 ]))) 106r6 = lines_until (lambda l :l .startswith ("BADM" )) 107bd = lines_until (lambda l :l .startswith ("BDONE 3" )) 108check ("a request to the context's end finishes" ,bd [- 1 ].split ()[3 ]in ("length" ,"stop" ),bd [- 1 ]) 109 110eng .close () 111"GATE" ,"PASS" if ok else "FAIL" ) 112sys .exit (0 if ok else 1 )