okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ea9a58d8a
main
1"""Serve-API test of the DFlash2 drafter: runs `strata --serve` on one arm and a scenario of greedy requests, writes the tokens and the engine's DONE figures. 2 3python serve_test.py --exe ENGINE --arm df2 --scenario scenario.json --out out_df2.json --log arm.log -- <the engine arguments, as in the run config> 4 5The wire is the engine's own: stdin `GEN <max_new> <id>,<id>,...`, stdout `T <id>` ... `DONE <n> <prompt> <prompt_ms> <decode_ms> <finish> <accepted> <offered> <resume> ...` 6(serve/server.py is the Python client). A scenario is {"requests": [{"name", "ids": [...]} | {"name", "after": "<name>", "follow": [...]}, ...], "max_new": N}: 7a request with `after` continues that request's conversation (its prompt + its output + `follow`), which is what a chat client sends for the next turn. 8Pure standard library: it runs on any box that has the engine. 9 10compare mode: python serve_test.py --compare nd.json mtp.json df2.json 11prints, per request, the first token where the arm differs from the no-draft arm (the engine's windows are not bitwise equal to one-token decode at near-ties, 12for the MTP as well; the DFlash2 arm must not be worse than the MTP arm), the drafts accepted / offered, and the figures the park test needs. 13""" 14import json 15import subprocess 16import sys 17import threading 18import time 19 20 21def run (exe ,args ,scenario ,log_path ,ready_timeout = 900 ,req_timeout = 600 ): 22log = open (log_path ,"ab" ) 23proc = subprocess .Popen ([exe ,"--serve" ,* args ],stdin = subprocess .PIPE ,stdout = subprocess .PIPE ,stderr = log ,text = True ,bufsize = 1 ) 24lines = [] 25cv = threading .Condition () 26 27def pump (): 28for ln in proc .stdout : 29with cv : 30lines .append (ln .rstrip ("\n" )) 31cv .notify_all () 32with cv : 33lines .append (None ) 34cv .notify_all () 35 36threading .Thread (target = pump ,daemon = True ).start () 37pos = 0 38 39def next_line (timeout ): 40nonlocal pos 41end = time .time ()+ timeout 42with cv : 43while pos >= len (lines ): 44left = end - time .time () 45if left <= 0 : 46raise TimeoutError ("no line from the engine" ) 47cv .wait (left ) 48ln = lines [pos ] 49pos += 1 50return ln 51 52while True : 53ln = next_line (ready_timeout ) 54if ln is None : 55raise RuntimeError ("the engine ended before READY" ) 56if ln .startswith ("READY" ): 57break 58done = {} 59results = [] 60for req in scenario ["requests" ]: 61if "after" in req : 62prev = done [req ["after" ]] 63ids = prev ["ids" ]+ prev ["out" ]+ req .get ("follow" , []) 64else : 65ids = list (req ["ids" ]) 66proc .stdin .write ("GEN %d %s\n" % (req .get ("max_new" ,scenario .get ("max_new" ,100 )),"," .join (str (int (t ))for t in ids ))) 67proc .stdin .flush () 68out ,stats ,resume = [], {},None 69while True : 70ln = next_line (req_timeout ) 71if ln is None : 72raise RuntimeError ("the engine ended during %s" % req ["name" ]) 73if ln .startswith ("T " ): 74out .append (int (ln .split ()[1 ])) 75elif ln .startswith ("RESUME " ): 76resume = int (ln .split ()[1 ]) 77elif ln .startswith ("ERR" ): 78raise RuntimeError ("engine: " + ln ) 79elif ln .startswith ("DONE" ): 80f = ln .split () 81stats = {"produced" :int (f [1 ]),"prompt_tokens" :int (f [2 ]),"prompt_ms" :float (f [3 ]),"decode_ms" :float (f [4 ]),"finish" :f [5 ], 82"accepted" :int (f [6 ]),"offered" :int (f [7 ]),"resume" :int (f [8 ])} 83break 84done [req ["name" ]]= {"ids" :ids ,"out" :out } 85results .append ({"name" :req ["name" ],"prompt_len" :len (ids ),"out" :out ,"resume" :resume ,** stats }) 86"%-14s prompt %5d resume %5s tokens %3d drafts %s/%s %.1f tok/s" % ( 87req ["name" ],len (ids ),resume ,len (out ),stats .get ("accepted" ),stats .get ("offered" ), 881000.0 * len (out )/ max (1e-9 ,stats .get ("decode_ms" ,0.0 ))),flush = True ) 89try : 90proc .stdin .close () 91proc .wait (60 ) 92except Exception : 93proc .kill () 94return results 95 96 97def compare (nd_path ,mtp_path ,df2_path ): 98nd ,mtp ,df2 = (json .load (open (p ))for p in (nd_path ,mtp_path ,df2_path )) 99worst = 0 100"%-14s %7s %9s %9s %12s %12s" % ("request" ,"tokens" ,"mtp diff@" ,"df2 diff@" ,"mtp drafts" ,"df2 drafts" )) 101for a ,b ,c in zip (nd ["results" ],mtp ["results" ],df2 ["results" ]): 102def first (x ,y ): 103n = min (len (x ),len (y )) 104return next ((i for i in range (n )if x [i ]!= y [i ]),None ) 105fm ,fd = first (a ["out" ],b ["out" ]),first (a ["out" ],c ["out" ]) 106"%-14s %7d %9s %9s %12s %12s" % (a ["name" ],len (a ["out" ]),fm ,fd ,"%d/%d" % (b ["accepted" ],b ["offered" ]),"%d/%d" % (c ["accepted" ],c ["offered" ]))) 107return worst 108 109 110if __name__ == "__main__" : 111argv = sys .argv [1 :] 112if argv and argv [0 ]== "--compare" : 113compare (* argv [1 :4 ]) 114sys .exit (0 ) 115opts = {} 116i = 0 117while i < len (argv )and argv [i ]!= "--" : 118opts [argv [i ].lstrip ("-" )]= argv [i + 1 ] 119i += 2 120engine_args = argv [i + 1 :] 121scenario = json .load (open (opts ["scenario" ])) 122res = run (opts ["exe" ],engine_args ,scenario ,opts .get ("log" ,"serve_test.log" )) 123json .dump ({"arm" :opts .get ("arm" ),"results" :res },open (opts ["out" ],"w" )) 124"SERVE_TEST_DONE" )