okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
99b9311c1
main
1"""Strata speed test following docs/COMMUNITY_BENCHMARKS.md. 2 3Three conditions, 3 measured runs each after one unmeasured warm-up, output capped at 256 tokens: 4short 104-token prompt, fully processed (a unique first line defeats prefix reuse) 5long-cold ~21K-token prompt, fully processed (unique first line) 6long-reuse the same ~21K prefix as the previous request, new question at the end 7Throughput comes from the engine's own timings (prompt_ms / predicted_ms); TTFT and total 8latency are measured at the client. The engine's log lines for each request are kept. 9 10Usage: python speed_bench.py <folder of .ts files used as filler text> <path to the strata serve log> 11""" 12import json ,pathlib ,subprocess ,sys ,time ,urllib .request ,uuid 13 14URL = "http://127.0.0.1:8080/v1/chat/completions" 15if len (sys .argv )!= 3 : 16sys .exit (__doc__ ) 17SRC = pathlib .Path (sys .argv [1 ])# a folder of .ts files, used as filler text 18LOG = pathlib .Path (sys .argv [2 ])# the log file named in the run configuration 19OUT = pathlib .Path (__file__ ).with_name ("runs.json" ) 20MAX_TOKENS = 256 21 22 23def filler (chars ): 24parts ,n = [],0 25for p in sorted (SRC .rglob ("*.ts" )): 26t = p .read_text (encoding = "utf-8" ,errors = "replace" ) 27parts .append (f"// ===== { p . name } =====\n { t } " ) 28n += len (t ) 29if n > chars : 30break 31return "\n" .join (parts ) 32 33 34def vram (): 35q = subprocess .run (["nvidia-smi" ,"--query-gpu=index,memory.used" ,"--format=csv,noheader,nounits" ], 36capture_output = True ,text = True ).stdout .split ("\n" ) 37return {f"gpu { l . split ( ',' )[ 0 ]. strip () } _mib" :int (l .split ("," )[1 ])for l in q if l .strip ()} 38 39 40def call (label ,messages ): 41log_pos = LOG .stat ().st_size 42body = {"model" :"x" ,"messages" :messages ,"max_tokens" :MAX_TOKENS ,"stream" :True ,"temperature" :0.6 , 43"stream_options" : {"include_usage" :True }} 44req = urllib .request .Request (URL ,json .dumps (body ).encode (), {"Content-Type" :"application/json" }) 45t0 = time .perf_counter ();first = None ;first_kind = None ;text = [];reasoning = [];timings = usage = None 46with urllib .request .urlopen (req ,timeout = 1800 )as r : 47for raw in r : 48line = raw .decode ("utf-8" ,"replace" ).strip () 49if not line .startswith ("data:" )or line == "data: [DONE]" : 50continue 51ev = json .loads (line [5 :]) 52timings = ev .get ("timings" )or timings 53usage = ev .get ("usage" )or usage 54for ch in ev .get ("choices" , []): 55d = ch .get ("delta" , {}) 56rc = d .get ("reasoning_content" )or d .get ("reasoning" ) 57if (d .get ("content" )or rc )and first is None : 58first ,first_kind = time .perf_counter (), ("reasoning" if rc else "answer" ) 59if rc :reasoning .append (rc ) 60if d .get ("content" ):text .append (d ["content" ]) 61t1 = time .perf_counter () 62time .sleep (1.0 ) 63with LOG .open ("rb" )as f : 64f .seek (log_pos ) 65log_lines = [l for l in f .read ().decode ("utf-8" ,"replace" ).splitlines ()if l .startswith ("strata serve:" )] 66t = timings or {} 67row = { 68"label" :label , 69"prompt_tokens" : (usage or {}).get ("prompt_tokens" ), 70"reused_tokens" :t .get ("cache_n" ),"read_tokens" :t .get ("prompt_n" ),"prompt_ms" :t .get ("prompt_ms" ), 71"prompt_tok_s" :t .get ("prompt_per_second" ), 72"generated_tokens" :t .get ("predicted_n" ),"decode_ms" :t .get ("predicted_ms" ), 73"decode_tok_s" :t .get ("predicted_per_second" ), 74"draft_n" :t .get ("draft_n" ),"draft_accepted" :t .get ("draft_n_accepted" ), 75"ttft_s" :round ((first or t1 )- t0 ,3 ),"first_token_kind" :first_kind , 76"total_latency_s" :round (t1 - t0 ,3 ), 77"vram_after" :vram (), 78"engine_log" :log_lines , 79"reasoning_chars" :len ("" .join (reasoning )),"answer" :"" .join (text ), 80 } 81f" { label :14s } read { row [ 'read_tokens' ]:>6 } reused { row [ 'reused_tokens' ]:>6 } | prompt { row [ 'prompt_tok_s' ]:7.1f } t/s" 82f" | decode { row [ 'generated_tokens' ] } tok { row [ 'decode_tok_s' ]:5.1f } t/s | TTFT { row [ 'ttft_s' ]:6.2f } s total { row [ 'total_latency_s' ]:6.2f } s" , 83flush = True ) 84return row 85 86 87def main (): 88ask = "Write a TypeScript function that parses a unified diff into hunks. Code only." 89big = filler (80_000 ) 90q = "Above is part of a codebase. In 5 bullet points, name the main modules and what each does." 91runs = [] 92call ("warm-up" , [{"role" :"user" ,"content" :f"run { uuid . uuid4 () } \n" + ask }]) 93for i in range (3 ): 94runs .append (call (f"short- { i + 1 } " , [{"role" :"user" ,"content" :f"run { uuid . uuid4 () } \n" + ask }])) 95for i in range (3 ): 96prefix = f"run { uuid . uuid4 () } \n" + big 97runs .append (call (f"long-cold- { i + 1 } " , [{"role" :"user" ,"content" :prefix + "\n\n" + q }])) 98runs .append (call (f"long-reuse- { i + 1 } " , [{"role" :"user" ,"content" :prefix + "\n\nList the 3 largest files you saw." }])) 99OUT .write_text (json .dumps ({"date" :time .strftime ("%Y-%m-%d %H:%M" ),"max_tokens" :MAX_TOKENS , 100"temperature" :0.6 ,"runs" :runs },indent = 1 ),encoding = "utf-8" ) 101 102def summary (prefix ,key ): 103v = sorted (r [key ]for r in runs if r ["label" ].startswith (prefix )and r [key ]is not None ) 104return f"median { v [ len ( v ) // 2 ]:.1f } (range { v [ 0 ]:.1f } - { v [ - 1 ]:.1f } , n= { len ( v ) } )" if v else "n/a" 105for c in ("short" ,"long-cold" ,"long-reuse" ): 106f"\n { c } : prompt t/s { summary ( c , 'prompt_tok_s' ) } | decode t/s { summary ( c , 'decode_tok_s' ) } " 107f" | TTFT s { summary ( c , 'ttft_s' ) } | total s { summary ( c , 'total_latency_s' ) } " ) 108 109 110if __name__ == "__main__" : 111main ()