okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
36ef9d6e6
main
1#!/usr/bin/env python3 2"""Matched fresh/follow-up prefill probe against an otherwise idle local Strata server. 3 4Does not start/stop services. Use a dedicated server and its engine log, restart 5between build/configuration arms, and keep model/template/settings identical. 6""" 7import argparse 8import json 9import os 10from pathlib import Path 11import re 12import time 13import urllib .request 14 15PATTERN = re .compile (r'prompt (\d+) tokens = (\d+) reused \+ (\d+) read in (\d+) ms \(([0-9.]+) tok/s\), (\d+) generated in (\d+) ms \(([0-9.]+) tok/s\)' ) 16FIELDS = ('prompt_tokens' ,'reused' ,'fresh' ,'prefill_ms' ,'prefill_tps' ,'generated' ,'decode_ms' ,'decode_tps' ) 17 18 19def main (): 20p = argparse .ArgumentParser (description = __doc__ ) 21p .add_argument ('--url' ,default = 'http://127.0.0.1:8080' ) 22p .add_argument ('--model' ,required = True ) 23p .add_argument ('--engine-log' ,type = Path ,required = True ) 24p .add_argument ('--output' ,type = Path ,required = True ) 25p .add_argument ('--label' ,required = True ) 26args = p .parse_args () 27results = [] 28 29def request (messages ,kind ,trial ): 30offset = args .engine_log .stat ().st_size 31body = dict (model = args .model ,messages = messages ,max_tokens = 128 , 32temperature = 0 ,top_k = 1 ,top_p = 1 ,min_p = 0 ,seed = 42 , 33reasoning_effort = 'none' ) 34headers = {'Content-Type' :'application/json' } 35if os .environ .get ('STRATA_API_KEY' ): 36headers ['Authorization' ]= 'Bearer ' + os .environ ['STRATA_API_KEY' ] 37req = urllib .request .Request (args .url .rstrip ('/' )+ '/v1/chat/completions' , 38data = json .dumps (body ).encode (),headers = headers ) 39start = time .monotonic () 40with urllib .request .urlopen (req ,timeout = 360 )as response : 41data = json .load (response ) 42elapsed = time .monotonic ()- start 43# The API response may arrive just before stderr's completion line is flushed. 44deadline = time .monotonic ()+ 2 45while True : 46with args .engine_log .open ('rb' )as log : 47log .seek (offset ) 48matches = PATTERN .findall (log .read ().decode (errors = 'replace' )) 49if matches or time .monotonic ()>= deadline : 50break 51time .sleep (0.02 ) 52if len (matches )!= 1 : 53raise RuntimeError ('Expected exactly one completed engine timing line; check idle server and log path' ) 54metrics = dict (zip (FIELDS ,map (float ,matches [0 ]))) 55if kind == 'fresh' and metrics ['reused' ]!= 0 : 56raise RuntimeError ('Fresh prompt reused cached tokens; restart this arm before comparison' ) 57choice = data ['choices' ][0 ] 58results .append (dict (label = args .label ,kind = kind ,trial = trial ,wall_s = elapsed , 59metrics = metrics ,usage = data .get ('usage' ), 60finish_reason = choice .get ('finish_reason' ),message = choice ['message' ])) 61args .output .write_text (json .dumps (results ,indent = 2 )+ '\n' ) 62json .dumps (results [- 1 ]),flush = True ) 63return choice ['message' ] 64 65request ([{'role' :'user' ,'content' :'Reply READY.' }],'warmup' ,0 ) 66for trial ,n in enumerate ((140 ,280 ,140 ,280 ),1 ): 67code = '\n' .join (f'export function rule { i } (x) {{ return x === { i } ? x + { i + 1 } : x - { i } ; }}' for i in range (n )) 68messages = [{'role' :'user' ,'content' :f'CASE { trial } : Review this source and describe its behavior precisely in a paragraph.\n' + code }] 69reply = request (messages ,'fresh' ,trial ) 70messages += [reply , {'role' :'user' ,'content' :'Explain the most relevant boundary case and the smallest useful regression test. ' + 'Focus on integer equality, zero, negative input, unexpected types, and caller assumptions. ' * 5 }] 71request (messages ,'followup' ,trial ) 72 73 74if __name__ == '__main__' : 75main ()