okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ef8071303
main
1#!/usr/bin/env python3 2"""Serial, fresh-prompt Strata benchmark. Run inside its prepared environment. 3 4This is the script from bench/results/2026-09-30-community-rtx-5090/benchmark.py, 5unchanged except for the defaults, so the numbers stay comparable with that 6report. Only --root, --pack, --url and --out defaults were repointed at the 7Docker image layout (/opt/strata, /data/packs/coder-iq1_m, port 8080). 8""" 9import argparse 10import hashlib 11import json 12import statistics 13import sys 14import time 15import urllib .request 16from pathlib import Path 17 18 19def get (url ): 20with urllib .request .urlopen (url ,timeout = 30 )as response : 21return json .load (response ) 22 23 24def main (): 25parser = argparse .ArgumentParser (description = __doc__ ) 26parser .add_argument ("--root" ,type = Path ,default = Path ("/opt/strata" )) 27parser .add_argument ("--pack" ,type = Path ,default = Path ("/data/packs/coder-iq1_m" )) 28parser .add_argument ("--url" ,default = "http://127.0.0.1:8080" ) 29parser .add_argument ("--out" ,type = Path ,default = Path ("/data/bench" )) 30parser .add_argument ("--targets" ,default = "4096,32768,128000" ) 31parser .add_argument ("--runs" ,type = int ,default = 3 ) 32args = parser .parse_args () 33args .out .mkdir (parents = True ,exist_ok = True ) 34sys .path [:0 ]= [str (args .root ),str (args .root / "tools" )] 35from strata_tokenizer import Tokenizer 36from serve .frontend import ChatTemplate ,openai_to_messages 37directory = args .pack / "tokenizer" 38vocab = json .loads ((directory / "vocab.json" ).read_text (encoding = "utf-8" )) 39tokens = [None ]* len (vocab ) 40for token ,number in vocab .items (): 41tokens [number ]= token 42tokenizer = Tokenizer (tokens , (directory / "merges.txt" ).read_text (encoding = "utf-8" ).splitlines (), 43json .loads ((directory / "token_type.json" ).read_text (encoding = "utf-8" ))) 44template = ChatTemplate (directory / "chat_template.jinja" ) 45initial = get (args .url + "/v1/status" ) 46 (args .out / "initial-status.json" ).write_text (json .dumps (initial ,indent = 2 )+ "\n" ) 47rows = [] 48 49def request (content ,maximum = 256 ): 50return {"model" :"strata" ,"messages" : [{"role" :"user" ,"content" :content }], 51"temperature" :0 ,"reasoning_effort" :"none" ,"max_tokens" :maximum , 52"stream" :True ,"stream_options" : {"include_usage" :True }} 53 54def count (req ): 55messages ,tools ,kwargs = openai_to_messages (req ) 56return len (tokenizer .encode (template .render (messages ,tools ,** kwargs ),parse_special = True )) 57 58def perform (label ,req ,expected = None ): 59body = json .dumps (req ).encode () 60 (args .out / (label + "-request.json" )).write_bytes (body + b"\n" ) 61"start" ,label ,"tokens" ,expected ,flush = True ) 62chunks ,texts ,first ,finish = [], [],None ,None 63started = time .perf_counter () 64wire = urllib .request .Request (args .url + "/v1/chat/completions" ,data = body , 65headers = {"Content-Type" :"application/json" }) 66with urllib .request .urlopen (wire ,timeout = 1800 )as response : 67for line in response : 68if not line .startswith (b"data: " ): 69continue 70payload = line [6 :].strip () 71if payload == b"[DONE]" : 72break 73chunk = json .loads (payload ) 74chunks .append (chunk ) 75for choice in chunk .get ("choices" , []): 76delta = choice .get ("delta" , {}) 77text = (delta .get ("content" )or "" )+ (delta .get ("reasoning_content" )or "" ) 78if text : 79first = first if first is not None else time .perf_counter ()- started 80texts .append (text ) 81finish = choice .get ("finish_reason" )or finish 82elapsed = time .perf_counter ()- started 83metrics = get (args .url + "/metrics" ) 84engine = metrics ["requests" ][0 ] 85usage = next ((chunk ["usage" ]for chunk in reversed (chunks )if "usage" in chunk ), {}) 86row = {"label" :label ,"expected_prompt_tokens" :expected , 87"request_sha256" :hashlib .sha256 (body ).hexdigest (), 88"client_ttft_s" :first ,"client_elapsed_s" :elapsed ,"finish_reason" :finish , 89"usage" :usage ,"engine" :engine ,"text" :"" .join (texts )} 90if expected is not None and engine ["prompt_tokens" ]!= expected : 91raise RuntimeError (f"token count mismatch: { expected } vs { engine [ 'prompt_tokens' ] } " ) 92if not texts : 93raise RuntimeError (f"no text received for { label } " ) 94 (args .out / (label + "-raw.json" )).write_text (json .dumps ({"chunks" :chunks ,"row" :row },indent = 2 )+ "\n" ) 95rows .append (row ) 96 (args .out / "results.json" ).write_text (json .dumps (rows ,indent = 2 )+ "\n" ) 97"done" ,label ,json .dumps ({"ttft_s" :first ,"elapsed_s" :elapsed ,"engine" :engine }),flush = True ) 98return row 99 100perform ("warmup" ,request ("Reply with exactly the word READY." ,16 )) 101# Each nonce occurs before the filler so previous long prompts cannot be reused. 102filler = "\n" .join (f"def task_ { i :05d } (value: int) -> int: return (value * { ( i % 97 ) + 1 } + { i } ) % 100003" 103for i in range (12000 )) 104ending = ("\n\nWrite a detailed explanation of the code above. Discuss deterministic integer transforms, " 105"modulo arithmetic, testing, naming, complexity, and maintainability. Write at least 600 words." ) 106for target in map (int ,args .targets .split ("," )): 107for run in range (1 ,args .runs + 1 ): 108prefix = f"Benchmark nonce: series- { target } -trial- { run } .\nReview this synthetic Python module:\n" 109lo ,hi = 0 ,len (filler ) 110while lo < hi : 111middle = (lo + hi + 1 )// 2 112if count (request (prefix + filler [:middle ]+ ending ))<= target : 113lo = middle 114else : 115hi = middle - 1 116req = request (prefix + filler [:lo ]+ ending ) 117actual = count (req ) 118if actual < target - 20 or actual > target : 119raise RuntimeError (f"could not construct target { target } : { actual } " ) 120perform (f"tokens- { target } -run- { run } " ,req ,actual ) 121measured = [row for row in rows if row ["label" ]!= "warmup" ] 122groups = {} 123for target in map (int ,args .targets .split ("," )): 124subset = [r for r in measured if r ["label" ].startswith (f"tokens- { target } -" )] 125values = { 126"prompt_tokens" : [r ["engine" ]["prompt_tokens" ]for r in subset ], 127"cached_tokens" : [r ["engine" ].get ("reused" ,0 )for r in subset ], 128"output_tokens" : [r ["engine" ]["engine_generated" ]for r in subset ], 129"client_ttft_s" : [r ["client_ttft_s" ]for r in subset ], 130"client_elapsed_s" : [r ["client_elapsed_s" ]for r in subset ], 131"prefill_tok_s" : [(r ["engine" ]["prompt_tokens" ]- (r ["engine" ].get ("reused" )or 0 ))/ (r ["engine" ]["prompt_ms" ]/ 1000 )for r in subset ], 132"decode_tok_s" : [r ["engine" ]["engine_generated" ]/ (r ["engine" ]["decode_ms" ]/ 1000 )for r in subset ], 133 } 134groups [str (target )]= {key : {"median" :statistics .median (v ),"min" :min (v ),"max" :max (v )} 135for key ,v in values .items ()} 136 (args .out / "summary.json" ).write_text (json .dumps (groups ,indent = 2 )+ "\n" ) 137"summary" ,json .dumps (groups ),flush = True ) 138 139 140if __name__ == "__main__" : 141main ()