okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b0fc432ed
main
1"""harness.py - drives strata-q36 / `strata` (qwen35moe) --serve over the engine protocol, for the q36-vp gates. 2 3Eng(exe, args) starts `exe --serve args`, waits for READY (INFO lines kept) 4.solo(ids, n, keys, emb) GEN / GENI one request -> (tokens, DONE line) 5.batch(reqs) reqs = [{"ids":[..], "n":N, "keys":"", "slot":b, "emb":path}] admitted one after the 6other (BGEN, as the server does: one admission at a time), then every slot decodes 7together -> dict(tokens=[..per req], wall, steady_tok_s, ...) 8Run on the lent box; the box's lock / memory cap wrap the whole python call (see run.sh). 9""" 10import os ,queue ,subprocess ,threading ,time 11 12 13def load_prompts (path ): 14return [[int (x )for x in l .strip ().split ("," )if x ]for l in open (path )if l .strip ()] 15 16 17class Eng : 18def __init__ (self ,exe ,args ,env = None ,log = None ,quiet = False ): 19e = dict (os .environ ) 20e .update (env or {}) 21self .logf = open (log ,"w" )if log else subprocess .DEVNULL 22self .p = subprocess .Popen ([exe ,"--serve" ,* args ],stdin = subprocess .PIPE ,stdout = subprocess .PIPE , 23stderr = self .logf ,text = True ,bufsize = 1 ,env = e ) 24self .q = queue .Queue () 25self .info = {} 26threading .Thread (target = self ._pump ,daemon = True ).start () 27t0 = time .time () 28while True : 29l = self .line (600 ) 30if l is None : 31raise SystemExit ("engine ended before READY (see %s)" % log ) 32if l .startswith ("INFO " ): 33for kv in l .split ()[1 :]: 34k ,_ ,v = kv .partition ("=" ) 35self .info [k ]= v 36if l .startswith ("READY" ): 37self .ready = l 38break 39self .load_s = time .time ()- t0 40 41def _pump (self ): 42for l in self .p .stdout : 43self .q .put (l .rstrip ("\n" )) 44self .q .put (None ) 45 46def line (self ,timeout = 600 ): 47try : 48return self .q .get (timeout = timeout ) 49except queue .Empty : 50raise SystemExit ("engine silent for %ds" % timeout ) 51 52def send (self ,s ): 53self .p .stdin .write (s + "\n" ) 54self .p .stdin .flush () 55 56def solo (self ,ids ,n ,keys = "" ,emb = None ): 57head = ("GENI %d" % n if emb else "GEN %d" % n )+ (" " + keys if keys else "" )+ (" " + emb if emb else "" ) 58t0 = time .time () 59self .send (head + " " + "," .join (map (str ,ids ))) 60toks ,ttft ,pp = [],None , [] 61while True : 62l = self .line () 63if l is None : 64raise SystemExit ("engine died" ) 65if l .startswith ("T " ): 66if ttft is None : 67ttft = time .time ()- t0 68toks .append (int (l .split ()[1 ])) 69elif l .startswith ("PP " ): 70pp .append (l ) 71elif l .startswith ("DONE" ): 72return toks ,l ,ttft ,time .time ()- t0 73elif l .startswith ("ERR" ): 74raise SystemExit ("ERR: " + l ) 75 76def batch (self ,reqs ,hold_s = 0.0 ): 77"""Admit every request (BGEN) in order, then read until every slot is done.""" 78toks = [[]for _ in reqs ] 79by_slot = {r ["slot" ]:i for i ,r in enumerate (reqs )} 80done = [False ]* len (reqs ) 81t0 = time .time () 82first_bt = None 83admitted_at = None 84stamps = [] 85for i ,r in enumerate (reqs ): 86head = ("BGENI %d %d" if r .get ("emb" )else "BGEN %d %d" )% (r ["slot" ],r ["n" ]) 87keys = r .get ("keys" ,"" ) 88self .send (head + (" " + keys if keys else "" )+ (" " + r ["emb" ]if r .get ("emb" )else "" )+ " " + 89"," .join (map (str ,r ["ids" ]))) 90while True : 91l = self .line () 92if l is None : 93raise SystemExit ("engine died" ) 94if l .startswith ("T " ): 95toks [i ].append (int (l .split ()[1 ])) 96elif l .startswith ("BT " ): 97f = l .split () 98j = by_slot [int (f [1 ])] 99toks [j ].append (int (f [2 ])) 100if first_bt is None : 101first_bt = time .time () 102elif l .startswith ("BDONE " ): 103done [by_slot [int (l .split ()[1 ])]]= True 104elif l .startswith ("BADM " ): 105f = l .split () 106if f [2 ]== "0" : 107done [i ]= True 108break 109elif l .startswith ("ERR" ): 110raise SystemExit ("ERR: " + l ) 111stamps .append (time .time ()- t0 ) 112admitted_at = time .time () 113n_at_admit = sum (len (t )for t in toks ) 114while not all (done ): 115l = self .line () 116if l is None : 117raise SystemExit ("engine died" ) 118if l .startswith ("BT " ): 119f = l .split () 120toks [by_slot [int (f [1 ])]].append (int (f [2 ])) 121elif l .startswith ("BDONE " ): 122done [by_slot [int (l .split ()[1 ])]]= True 123elif l .startswith ("ERR" ): 124raise SystemExit ("ERR: " + l ) 125t1 = time .time () 126tot = sum (len (t )for t in toks ) 127return {"tokens" :toks ,"wall" :t1 - t0 ,"admit_s" :admitted_at - t0 ,"admit_stamps" :stamps , 128"tail_tokens" :tot - n_at_admit ,"tail_s" :t1 - admitted_at , 129"tail_tok_s" : (tot - n_at_admit )/ max (1e-9 ,t1 - admitted_at )} 130 131def close (self ): 132try : 133self .send ("STOP" ) 134except Exception : 135pass 136try : 137self .p .stdin .close () 138except Exception : 139pass 140try : 141self .p .wait (timeout = 30 ) 142except Exception : 143self .p .kill ()