okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
09fadfee6
main
1#!/usr/bin/env python3 2"""Strata benchmark harness with pluggable model name. 3 4For each configured --max-context, finds a chat content that lands the prompt 5near 92 % of capacity, runs: 6- 1 cold-prefill probe (max_tokens=1, non-stream) for the real prefill 7throughput number (the number the README's 2,000+ t/s headlines measure) 8- 3 SSE stream /v1/chat/completions calls (run once each, with `reasoning_effort=minimal`) 9- no extra reference non-stream call 10 11Why two prefill numbers: 12- SSE's `timings.prompt_per_second` is "time to FIRST decode chunk after 13prefilled chunk" (first-chunk-completion throughput). With --prefill auto 14doing 8192-token chunks, the engine starts streaming before the entire 15prompt is in. So SSE reports a smaller denominator than the cold prefill. 16- Cold-prefill non-stream (max_tokens=1) reports end-of-prompt throughput. 17Useful for apples-to-apples comparison with the README's stated numbers. 18""" 19from __future__import annotations 20 21import argparse 22import json 23import statistics 24import time 25from dataclasses import dataclass ,asdict ,field 26from pathlib import Path 27from typing import List ,Optional 28import requests 29 30TARGET_FRACTION = 0.92 31CHAR_PER_TOKEN = 4.0 32PEER_WINDOW = 32 33 34FILLER = ( 35"Strata models a pagoda of experts and policies; during a long context " 36"session the persistent graph speculatively drafts the next token while " 37"the verifier follows. The verifier commits the draft when the " 38"distributions agree, else resamples and re-aligns. On a workstation with " 39"a single 16 GB card, the expert cache holds about a thousand experts " 40"and the README warns that context past 65 k degrades without KV-cache " 41"compression. The MTP draft layer is a five-token head tuned from the " 42"1.3 B Qwen3 base and imported unchanged. " 43) 44 45 46def render_prompt (target_chars :int )-> str : 47out ,cur = [],0 48while cur < target_chars : 49out .append (FILLER ) 50cur += len (FILLER ) 51return "" .join (out )[:target_chars ] 52 53 54@dataclass 55class Run : 56seq :int 57target_ctx :int 58target_fill_chars :int 59prompt_tokens_actual :int 60completion_tokens :int 61content_chunks :int 62reasoning_chunks :int 63ttft_ms :float 64decode_avg_tps :float 65decode_peak_tps :float 66eng_predicted_per_second :float 67eng_predicted_per_token_ms :float 68eng_prompt_per_second :float 69sse_prompt_per_second :float 70eng_draft_n :int 71eng_draft_n_accepted :int 72eng_predicted_ms :float 73eng_total_request_ms :float 74finish_reason :str 75elapsed_ms :float 76 77 78@dataclass 79class ColdPrefill : 80prompt_tokens_actual :int 81prompt_total_ms :float 82prompt_per_second :float 83completion_tokens :int 84 85 86def lookup_v1_models (base_url :str ,model_name :str )-> str : 87r = requests .get (f" { base_url } /v1/models" ,timeout = 20 ) 88r .raise_for_status () 89data = r .json ().get ("data" , []) 90for m in data : 91if m ["id" ]== model_name : 92return model_name 93if data : 94f" WARN: { model_name } not in /v1/models; server reports: { data [ 0 ][ 'id' ] } " ) 95return data [0 ]["id" ]if data else model_name 96 97 98def calibration (base_url :str ,fill_chars :int ,model_name :str )-> int : 99body = { 100"model" :model_name , 101"stream" :False , 102"reasoning_effort" :"minimal" , 103"messages" : [{"role" :"user" ,"content" :render_prompt (fill_chars )}], 104"max_tokens" :4 , 105"temperature" :0 , 106 } 107r = requests .post (f" { base_url } /v1/chat/completions" ,json = body ,timeout = 900 ) 108r .raise_for_status () 109return r .json ()["usage" ]["prompt_tokens" ] 110 111 112def find_sweet_fill_chars (base_url :str ,target_ctx :int ,model_name :str ): 113goal = int (target_ctx * TARGET_FRACTION ) 114lo = int (target_ctx * 0.50 / CHAR_PER_TOKEN ) 115hi = int (target_ctx * 0.97 / CHAR_PER_TOKEN ) 116n_lo = calibration (base_url ,lo ,model_name ) 117n_hi = calibration (base_url ,hi ,model_name ) 118if n_hi == n_lo : 119return hi ,n_hi 120slope = (n_hi - n_lo )/ (hi - lo ) 121fill = max (1 ,int (lo + (goal - n_lo )/ slope )) 122best_c ,best_n = fill ,calibration (base_url ,fill ,model_name ) 123for d in (- 2048 ,- 1024 ,0 ,+ 1024 ,+ 2048 ): 124c = max (1 ,fill + d ) 125if c < lo or c > hi : 126continue 127n = calibration (base_url ,c ,model_name ) 128if abs (n - goal )< abs (best_n - goal ): 129best_c ,best_n = c ,n 130return best_c ,best_n 131 132 133def force_cold_prefill_state (base_url :str )-> None : 134# Upstream (>= 6f32ec07) requires Content-Type: application/json on POST 135# /unload; the server returns HTTP 415 without it. Older (rc/0.1.30) was 136# tolerant. Send an explicit JSON header. 137try : 138r = requests .post ( 139f" { base_url } /unload" , 140data = b"{}" , 141headers = {"Content-Type" :"application/json" }, 142timeout = 60 , 143 ) 144r .raise_for_status () 145except Exception as e : 146f" warn: /unload failed: { e !r } " ) 147try : 148for _ in range (60 ): 149r = requests .get (f" { base_url } /health" ,timeout = 5 ) 150if r .status_code == 200 : 151j = r .json () 152if j .get ("loaded" ): 153break 154time .sleep (1 ) 155except Exception : 156pass 157 158 159def cold_prefill (base_url :str ,fill_chars :int ,model_name :str , 160target_ctx :int )-> ColdPrefill : 161force_cold_prefill_state (base_url ) 162body = { 163"model" :model_name , 164"stream" :False , 165"reasoning_effort" :"minimal" , 166"messages" : [{"role" :"user" ,"content" :render_prompt (fill_chars )}], 167"max_tokens" :1 , 168"temperature" :0 , 169 } 170r = requests .post (f" { base_url } /v1/chat/completions" ,json = body ,timeout = 1800 ) 171r .raise_for_status () 172data = r .json () 173usage = data .get ("usage" , {})or {} 174timings = data .get ("timings" , {})or {} 175prompt_n = timings .get ("prompt_n" )or usage .get ("prompt_tokens" ,0 ) 176prompt_ms = timings .get ("prompt_ms" ,0.0 ) 177prompt_per_s = timings .get ("prompt_per_second" ,float (prompt_n )* 1000.0 / max (1.0 ,prompt_ms ))if prompt_ms else 0.0 178return ColdPrefill ( 179prompt_tokens_actual = prompt_n , 180prompt_total_ms = round (prompt_ms ,1 ), 181prompt_per_second = round (prompt_per_s ,1 ), 182completion_tokens = timings .get ("predicted_n" )or usage .get ("completion_tokens" ,0 ), 183 ) 184 185 186def stream_run (base_url :str ,fill_chars :int ,seq :int ,target_ctx :int , 187model_name :str )-> Run : 188body = { 189"model" :model_name , 190"stream" :True , 191"reasoning_effort" :"minimal" , 192"messages" : [{"role" :"user" ,"content" :render_prompt (fill_chars )}], 193"max_tokens" :192 , 194"temperature" :0 , 195 } 196t_req = time .perf_counter () 197content_ts :List [float ]= [] 198n_content ,n_reasoning = 0 ,0 199final_event = None 200finish_reason = None 201 202with requests .post (f" { base_url } /v1/chat/completions" ,json = body , 203stream = True ,timeout = 1800 )as r : 204r .raise_for_status () 205# first chunk timestamp — proxy for server "received by client" latency 206first_remote = None 207for raw in r .iter_lines (decode_unicode = True ): 208if not raw or not raw .startswith ("data:" ): 209continue 210payload = raw [5 :].strip () 211if payload == "[DONE]" :break 212try : 213c = json .loads (payload ) 214except json .JSONDecodeError : 215continue 216t = time .perf_counter () 217delta = (c ["choices" ][0 ].get ("delta" , {})if c .get ("choices" )else {}) 218if "content" in delta and delta ["content" ]: 219content_ts .append (t ) 220n_content += 1 221if "reasoning_content" in delta and delta ["reasoning_content" ]: 222n_reasoning += 1 223if c .get ("choices" )and c ["choices" ][0 ].get ("finish_reason" ): 224finish_reason = c ["choices" ][0 ]["finish_reason" ] 225if c .get ("timings" ): 226final_event = c 227last_remote = time .perf_counter () 228 229usage = (final_event or {}).get ("usage" , {})or {} 230timings = (final_event or {}).get ("timings" , {})or {} 231prompt_tokens = usage .get ("prompt_tokens" ,0 ) 232completion_tokens = usage .get ("completion_tokens" ,0 ) 233 234ttft_ms = ((content_ts [0 ]- t_req )* 1000 )if content_ts else 0.0 235decode_avg_tps ,decode_peak_tps = 0.0 ,0.0 236if content_ts and completion_tokens > 0 : 237decode_dt = max (0.001 ,content_ts [- 1 ]- content_ts [0 ]) 238decode_avg_tps = completion_tokens / decode_dt 239if len (content_ts )> PEER_WINDOW : 240for i in range (len (content_ts )- PEER_WINDOW ): 241w = content_ts [i + PEER_WINDOW - 1 ]- content_ts [i ] 242if w > 0 : 243rate = PEER_WINDOW / w 244if rate > decode_peak_tps : 245decode_peak_tps = rate 246elapsed = ((content_ts [- 1 ]- t_req )* 1000 )if content_ts else 0.0 247total_request_ms = (last_remote - t_req )* 1000 248 249return Run ( 250seq = seq , 251target_ctx = target_ctx , 252target_fill_chars = fill_chars , 253prompt_tokens_actual = prompt_tokens , 254completion_tokens = completion_tokens , 255content_chunks = n_content , 256reasoning_chunks = n_reasoning , 257ttft_ms = round (ttft_ms ,2 ), 258decode_avg_tps = round (decode_avg_tps ,2 ), 259decode_peak_tps = round (decode_peak_tps ,2 ), 260eng_predicted_per_second = round (timings .get ("predicted_per_second" ,0.0 ),2 ), 261eng_predicted_per_token_ms = round (timings .get ("predicted_per_token_ms" ,0.0 ),2 ), 262eng_prompt_per_second = round (timings .get ("prompt_per_second" ,0.0 ),2 ), 263sse_prompt_per_second = round (timings .get ("prompt_per_second" ,0.0 ),2 ), 264eng_draft_n = timings .get ("draft_n" ,0 ), 265eng_draft_n_accepted = timings .get ("draft_n_accepted" ,0 ), 266eng_predicted_ms = round (timings .get ("predicted_ms" ,0.0 ),2 ), 267eng_total_request_ms = round (total_request_ms ,1 ), 268finish_reason = str (finish_reason or "stop" ), 269elapsed_ms = round (elapsed ,1 ), 270 ) 271 272 273def measure_window (base_url :str ,target_ctx :int ,model_name :str )-> dict : 274f"\n=== { model_name } @ ctx { target_ctx } ===" ) 275lookup_v1_models (base_url ,model_name ) 276fill_chars ,prompt_tokens_actual = find_sweet_fill_chars (base_url ,target_ctx ,model_name ) 277f" calibration: fill_chars= { fill_chars } , prompt_tokens= { prompt_tokens_actual } " ) 278 279f" cold-prefill probe (max_tokens=1, non-stream) ..." ) 280cp = cold_prefill (base_url ,fill_chars ,model_name ,target_ctx ) 281f" coldfill prompt_n= { cp . prompt_tokens_actual :5d } " 282f"prompt_total_ms= { cp . prompt_total_ms :8.1f } prompt_per_second= { cp . prompt_per_second :7.1f } t/s" ) 283 284runs :List [Run ]= [] 285for i in range (1 ,4 ): 286time .sleep (1.5 ) 287r = stream_run (base_url ,fill_chars ,seq = i ,target_ctx = target_ctx , 288model_name = model_name ) 289runs .append (r ) 290f" seq= { r . seq } prompt_tok= { r . prompt_tokens_actual :5d } " 292f"completion_tok= { r . completion_tokens :3d } ttft= { r . ttft_ms :7.0f } ms " 293f"avg= { r . decode_avg_tps :6.2f } t/s peak= { r . decode_peak_tps :6.2f } t/s " 294f"(eng_pred= { r . eng_predicted_per_second :6.2f } , " 295f"sse-prompt-rpt= { r . sse_prompt_per_second :6.1f } , " 296f"draft+= { r . eng_draft_n_accepted } / { r . eng_draft_n } ) " 297f"finish= { r . finish_reason } " 298 ) 299 300avgs = [r .decode_avg_tps for r in runs if r .decode_avg_tps ] 301peaks = [r .decode_peak_tps for r in runs if r .decode_peak_tps ] 302engs = [r .eng_predicted_per_second for r in runs if r .eng_predicted_per_second ] 303return { 304"target_ctx" :target_ctx , 305"model_name" :model_name , 306"target_prompt_tokens" :prompt_tokens_actual , 307"fill_chars" :fill_chars , 308"cold_prefill" :asdict (cp ), 309"runs" : [asdict (r )for r in runs ], 310"avg_decode_avg_tps" :round (statistics .mean (avgs ),2 )if avgs else 0.0 , 311"avg_peak_decode_tps" :round (statistics .mean (peaks ),2 )if peaks else 0.0 , 312"peak_decode_tps_overall" :round (max (peaks ),2 )if peaks else 0.0 , 313"engine_predicted_avg_tps" :round (statistics .mean (engs ),2 )if engs else 0.0 , 314"engine_predicted_per_run" :engs , 315"ttft_ms_per_run" : [r .ttft_ms for r in runs ], 316"engine_prefill_per_run" : [r .eng_prompt_per_second for r in runs ], 317"finish_reasons" : [r .finish_reason for r in runs ], 318 } 319 320 321def main (): 322ap = argparse .ArgumentParser () 323ap .add_argument ("--base-url" ,default = "http://127.0.0.1:8090" ) 324ap .add_argument ("--model" ,required = True ) 325ap .add_argument ("--contexts" ,type = int ,nargs = "+" ,default = [32768 ]) 326ap .add_argument ("--out" ,required = True ) 327ap .add_argument ("--note" ,default = "" ) 328args = ap .parse_args () 329 330out = { 331"engine" :"Strata" , 332"model_name" :args .model , 333"host" :"Shin-BlackMamba" , 334"started_at_utc" :int (time .time ()), 335"contexts" :args .contexts , 336"note" :args .note , 337"results" : [], 338 } 339for ctx in args .contexts : 340out ["results" ].append (measure_window (args .base_url ,ctx ,args .model )) 341 342out ["finished_at_utc" ]= int (time .time ()) 343Path (args .out ).parent .mkdir (parents = True ,exist_ok = True ) 344Path (args .out ).write_text (json .dumps (out ,indent = 2 )) 345"\nWrote" ,args .out ) 346 347 348if __name__ == "__main__" : 349main ()