okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6e1085c1d
main
1#!/usr/bin/env python3 2"""Two simultaneous, distinct ~19.6K prompts for issue #1356 (not its unpublished original prompts). 3 4Reuse the public V100 report's synthetic-Python filler/template and sampling. 5Save exact requests, partial SSE, usage, elapsed time and server metrics. 6""" 7import argparse 8import concurrent .futures 9import json 10import pathlib 11import sys 12import time 13import urllib .request 14 15ROOT = pathlib .Path (__file__ ).resolve ().parents [4 ] 16sys .path [:0 ]= [str (ROOT ),str (ROOT / 'tools' )] 17from strata_tokenizer import Tokenizer 18from serve .frontend import ChatTemplate ,openai_to_messages 19 20 21def main (): 22ap = argparse .ArgumentParser () 23ap .add_argument ('--pack' ,type = pathlib .Path ,required = True ) 24ap .add_argument ('--url' ,required = True ) 25ap .add_argument ('--out' ,type = pathlib .Path ,required = True ) 26a = ap .parse_args () 27a .out .mkdir (parents = True ,exist_ok = True ) 28d = a .pack / 'tokenizer' ;vocab = json .loads ((d / 'vocab.json' ).read_text ()) 29tokens = [None ]* len (vocab ) 30for token ,number in vocab .items ():tokens [number ]= token 31tok = Tokenizer (tokens ,(d / 'merges.txt' ).read_text ().splitlines (),json .loads ((d / 'token_type.json' ).read_text ())) 32template = ChatTemplate (d / 'chat_template.jinja' ) 33filler = '\n' .join (f'def task_ { i :05d } (value: int) -> int: return (value * { ( i % 97 ) + 1 } + { i } ) % 100003' for i in range (12000 )) 34ending = '\n\nWrite a detailed explanation of the code above. Discuss deterministic integer transforms, modulo arithmetic, testing, naming, complexity, and maintainability. Write at least 600 words.' 35def payload (text ): 36return dict (model = 'strata' ,messages = [dict (role = 'user' ,content = text )],temperature = 0 , 37reasoning_effort = 'none' ,max_tokens = 256 ,stream = True ,stream_options = dict (include_usage = True )) 38requests = [] 39for n in range (2 ): 40prefix = f'Benchmark nonce: parallel-1356-trial- { n } .\nReview this synthetic Python module:\n' 41lo ,hi = 0 ,len (filler ) 42def count (body ): 43m ,t ,k = openai_to_messages (body );return len (tok .encode (template .render (m ,t ,** k ),parse_special = True )) 44while lo < hi : 45mid = (lo + hi + 1 )// 2 46if count (payload (prefix + filler [:mid ]+ ending ))<= 19600 :lo = mid 47else :hi = mid - 1 48body = payload (prefix + filler [:lo ]+ ending );requests .append (body ) 49 (a .out / f'request- { n } .json' ).write_text (json .dumps (body )) 50 (a .out / f'count- { n } .json' ).write_text (json .dumps ({'prompt_tokens' :count (body )})) 51import threading 52barrier = threading .Barrier (2 ) 53def ask (n ): 54chunks = [];row = {'slot' :n };barrier .wait ();start = time .monotonic () 55try : 56req = urllib .request .Request (a .url + '/v1/chat/completions' ,data = json .dumps (requests [n ]).encode (),headers = {'Content-Type' :'application/json' }) 57with urllib .request .urlopen (req ,timeout = 600 )as response , (a .out / f'sse- { n } .jsonl' ).open ('w' )as f : 58for line in response : 59if not line .startswith (b'data:' ):continue 60data = line [5 :].strip () 61if data == b'[DONE]' :break 62chunk = json .loads (data );chunks .append (chunk );f .write (json .dumps (chunk )+ '\n' );f .flush () 63if 'first_token_s' not in row and any (c .get ('delta' ,{}).get ('content' )for c in chunk .get ('choices' ,[])): 64row ['first_token_s' ]= time .monotonic ()- start 65row ['text' ]= '' .join (c .get ('delta' ,{}).get ('content' )or '' for ch in chunks for c in ch .get ('choices' ,[])) 66row ['usage' ]= next ((ch ['usage' ]for ch in reversed (chunks )if ch .get ('usage' )),None ) 67row ['finish_reason' ]= next ((c ['finish_reason' ]for ch in reversed (chunks )for c in ch .get ('choices' ,[])if c .get ('finish_reason' )),None ) 68except Exception as ex : 69row ['error' ]= f' { type ( ex ). __name__ } : { ex } ' 70row ['elapsed_s' ]= time .monotonic ()- start 71 (a .out / f'result- { n } .json' ).write_text (json .dumps (row ,indent = 2 )) 72json .dumps (row ),flush = True ) 73return row 74with concurrent .futures .ThreadPoolExecutor (max_workers = 2 )as ex :rows = list (ex .map (ask ,range (2 ))) 75with urllib .request .urlopen (a .url + '/metrics' ,timeout = 10 )as response :metrics = json .load (response ) 76 (a .out / 'metrics.json' ).write_text (json .dumps (metrics ,indent = 2 )) 77return int (any (r .get ('error' )or not r .get ('text' )for r in rows )) 78 79 80if __name__ == '__main__' :sys .exit (main ())