okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
53623e56f
main
1#!/usr/bin/env python3 2"""Strata community-report runs (PREREG_STRATA_194 addendum). Runs ON .194 against a running Strata server. 3Builds ~N-token prompts from Strata's own docs (MIT, at the installed commit), sends each request with a unique nonce 4first line (no prefix reuse), streams, and records client TTFT / total. The engine's own timing line for each request 5is read from Strata's log afterwards (by order). Python stdlib only. 6usage: strata_bench.py --url http://127.0.0.1:8080 --repo ~/strata --lengths 4096,32768 --runs 3 --out bench.jsonl""" 7import argparse ,json ,os ,secrets ,time ,urllib .request 8from pathlib import Path 9 10QUESTION = ("\n\nThe text above is documentation for an inference engine. In a few short paragraphs, explain what " 11"the engine does and how it keeps a large mixture-of-experts model fast on a consumer PC." ) 12 13 14def corpus (repo :Path )-> str : 15parts = [] 16for p in sorted ((repo / "docs" ).glob ("*.md" ))+ [repo / "README.md" ]: 17parts .append (f"===== { p . name } =====\n" + p .read_text (errors = "replace" )) 18return "\n\n" .join (parts ) 19 20 21def tokenize_count (url :str ,text :str )-> int : 22for path in ("/tokenize" ,"/v1/tokenize" ): 23try : 24req = urllib .request .Request (url + path ,json .dumps ({"content" :text }).encode (), {"Content-Type" :"application/json" }) 25with urllib .request .urlopen (req ,timeout = 120 )as r : 26return len (json .load (r ).get ("tokens" , [])) 27except Exception : 28continue 29return int (len (text )/ 3.6 )# fallback estimate, flagged in the output 30 31 32def build (url :str ,text :str ,target :int )-> tuple [str ,int ]: 33lo ,hi = 1000 ,len (text ) 34best = text [:lo ] 35while hi - lo > 2000 :# binary search on characters for ~target tokens 36mid = (lo + hi )// 2 37if tokenize_count (url ,text [:mid ]+ QUESTION )<= target : 38lo ,best = mid ,text [:mid ] 39else : 40hi = mid 41p = best + QUESTION 42return p ,tokenize_count (url ,p ) 43 44 45def run (url :str ,prompt :str )-> dict : 46nonce = secrets .token_hex (16 ) 47body = {"model" :"strata" ,"messages" : [{"role" :"user" ,"content" :f"[bench nonce: { nonce } ]\n" + prompt }], 48"max_tokens" :256 ,"temperature" :0 ,"stream" :True ,"reasoning_effort" :"none" , 49"stream_options" : {"include_usage" :True }} 50req = urllib .request .Request (url + "/v1/chat/completions" ,json .dumps (body ).encode (), {"Content-Type" :"application/json" }) 51t0 = time .monotonic ();first = last = None ;text = [];usage = None ;reasoning = 0 52with urllib .request .urlopen (req ,timeout = 3600 )as r : 53for raw in r : 54line = raw .decode (errors = "replace" ).strip () 55if not line .startswith ("data: " )or line == "data: [DONE]" : 56continue 57ev = json .loads (line [6 :]) 58for ch in ev .get ("choices" )or []: 59d = ch .get ("delta" )or {} 60if d .get ("content" )or d .get ("reasoning_content" ): 61now = time .monotonic ()- t0 62first = now if first is None else first 63last = now 64text .append (d .get ("content" )or "" ) 65reasoning += len (d .get ("reasoning_content" )or "" ) 66usage = ev .get ("usage" )or usage 67total = time .monotonic ()- t0 68return {"nonce" :nonce ,"ttft_s" :round (first ,3 )if first else None ,"total_s" :round (total ,3 ), 69"first_to_last_s" :round (last - first ,3 )if first is not None else None , 70"usage" :usage ,"content_chars" :len ("" .join (text )),"reasoning_chars" :reasoning , 71"content_head" :"" .join (text )[:200 ]} 72 73 74def main (): 75ap = argparse .ArgumentParser () 76ap .add_argument ("--url" ,default = "http://127.0.0.1:8080" );ap .add_argument ("--repo" ,default = os .path .expanduser ("~/strata" )) 77ap .add_argument ("--lengths" ,default = "4096,32768" );ap .add_argument ("--runs" ,type = int ,default = 3 ) 78ap .add_argument ("--out" ,required = True ) 79a = ap .parse_args () 80text = corpus (Path (a .repo )) 81with open (a .out ,"a" )as f : 82for L in [int (x )for x in a .lengths .split ("," )]: 83prompt ,n = build (a .url ,text ,L - 40 ) 84for i in range (a .runs ): 85rec = {"target_tokens" :L ,"prompt_tokens_tokenized" :n ,"run" :i + 1 ,** run (a .url ,prompt )} 86f .write (json .dumps (rec )+ "\n" );f .flush ();os .fsync (f .fileno ()) 87json .dumps ({k :rec [k ]for k in ("target_tokens" ,"run" ,"ttft_s" ,"total_s" ,"content_chars" ,"reasoning_chars" )}),flush = True ) 88 89 90if __name__ == "__main__" : 91main ()