okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
db4dd65e5
main
1"""par_bench.py - aggregate decode throughput of N requests at once: strata-q36 (--batch) and llama.cpp (llama-server -np N), 2same GGUF, same prompt ids, greedy, 128 new tokens each. One JSON line per (engine, N, round). 3 4python q36_par_bench.py strata --exe E --args "--model M --max-context 8192 --batch 8 --mtp 0" --ns 1,2,4,8 --rounds 3 --out F 5python q36_par_bench.py llama --server BIN --model M --ns 1,2,4,8 --rounds 3 --out F [--port 8197] 6 7The strata wall time runs from the first BGEN to the last BDONE (admissions included); the llama one from the first request 8sent to the last answer (prompt reading included); tok/s = tokens produced / wall. `tail_tok_s` (strata): the window phase 9only, after the last admission.""" 10from pathlib import Path 11import argparse ,json ,os ,shlex ,subprocess ,sys ,threading ,time ,urllib .request 12sys .path .insert (0 ,__file__ .rsplit ("/" ,1 )[0 ]if "/" in __file__ else "." ) 13from q36_harness import Eng ,load_prompts 14 15ORDER = [0 ,1 ,2 ,3 ,5 ,8 ,0 ,1 ]# the prompts that run the full 128 tokens, repeated to fill 8 slots 16 17 18def strata (a ): 19prompts = load_prompts (a .prompts ) 20eng = Eng (a .exe ,shlex .split (a .args ),log = a .log ) 21out = open (a .out ,"a" ) 22for n in [int (x )for x in a .ns .split ("," )]: 23for rnd in range (a .rounds ): 24if n == 1 and not eng .info .get ("batch_slots" ):# an engine without slots: the request alone (GEN) 25toks ,done ,ttft ,wall = eng .solo (prompts [ORDER [0 ]],a .n ) 26row = {"engine" :"strata" ,"n" :1 ,"round" :rnd ,"tokens" :len (toks ),"wall" :wall , 27"tok_s" :len (toks )/ wall ,"tail_tok_s" :len (toks )/ max (1e-9 ,wall - (ttft or 0 ))} 28else : 29reqs = [{"ids" :prompts [ORDER [i ]],"n" :a .n ,"slot" :i }for i in range (n )] 30r = eng .batch (reqs ) 31tot = sum (len (t )for t in r ["tokens" ]) 32row = {"engine" :"strata" ,"n" :n ,"round" :rnd ,"tokens" :tot ,"wall" :r ["wall" ],"tok_s" :tot / r ["wall" ], 33"tail_tok_s" :r ["tail_tok_s" ]} 34json .dumps (row ),flush = True ) 35out .write (json .dumps (row )+ "\n" ) 36out .flush () 37eng .close () 38 39 40def post (port ,body ): 41return json .loads (urllib .request .urlopen (urllib .request .Request (f"http://127.0.0.1: { port } /completion" ,json .dumps (body ).encode (), 42 {"Content-Type" :"application/json" }),timeout = 900 ).read ()) 43 44 45def llama (a ): 46prompts = load_prompts (a .prompts ) 47out = open (a .out ,"a" ) 48for n in [int (x )for x in a .ns .split ("," )]: 49cmd = [a .server ,"-m" ,a .model ,"-ngl" ,"99" ,"-fa" ,"on" ,"-c" ,str (8192 * n ),"-np" ,str (n ),"--port" ,str (a .port ), 50"--no-webui" ,"-t" ,"16" ]+ shlex .split (a .llama_args ) 51log = open (a .log or os .devnull ,"a" ) 52p = subprocess .Popen (cmd ,stdout = log ,stderr = log ) 53try : 54for _ in range (240 ): 55try : 56urllib .request .urlopen (f"http://127.0.0.1: { a . port } /health" ,timeout = 2 ) 57break 58except Exception :# noqa: BLE001 59time .sleep (1 ) 60for rnd in range (a .rounds + 1 ):# round 0: the warm-up 61res = [None ]* n 62 63def one (i ): 64res [i ]= post (a .port , {"prompt" :prompts [ORDER [i ]],"n_predict" :a .n ,"temperature" :0 ,"top_k" :1 , 65"cache_prompt" :False ,"id_slot" :i }) 66ts = [threading .Thread (target = one ,args = (i ,))for i in range (n )] 67t0 = time .time () 68for t in ts : 69t .start () 70for t in ts : 71t .join () 72wall = time .time ()- t0 73tot = sum (r ["tokens_predicted" ]for r in res ) 74if rnd == 0 : 75continue 76row = {"engine" :"llama" ,"n" :n ,"round" :rnd - 1 ,"tokens" :tot ,"wall" :wall ,"tok_s" :tot / wall } 77json .dumps (row ),flush = True ) 78out .write (json .dumps (row )+ "\n" ) 79out .flush () 80finally : 81p .terminate () 82try : 83p .wait (timeout = 60 ) 84except subprocess .TimeoutExpired : 85p .kill () 86 87 88ap = argparse .ArgumentParser () 89ap .add_argument ("mode" ,choices = ["strata" ,"llama" ]) 90ap .add_argument ("--exe" ) 91ap .add_argument ("--args" ,default = "" ) 92ap .add_argument ("--server" ) 93ap .add_argument ("--model" ) 94ap .add_argument ("--llama-args" ,default = "" ) 95ap .add_argument ("--ns" ,default = "1,2,4,8" ) 96ap .add_argument ("--rounds" ,type = int ,default = 3 ) 97ap .add_argument ("--n" ,type = int ,default = 128 ) 98ap .add_argument ("--prompts" ,default = str (Path (__file__ ).resolve ().parent / "parity_prompts.ids" )) 99ap .add_argument ("--port" ,type = int ,default = 8197 ) 100ap .add_argument ("--out" ,required = True ) 101ap .add_argument ("--log" ,default = None ) 102a = ap .parse_args () 103(strata if a .mode == "strata" else llama )(a )