okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2b36e8a02
main
1#!/usr/bin/env python3 2"""Coverage past 32K: one 60K-token prompt on the 64K-context configuration, with a hidden code, so the run measures prompt 3throughput at that length and checks recall in the same request. 4 5python long_context.py CONFIG OUT.json [--tokens 60000] 6""" 7import json 8import random 9import subprocess 10import sys 11import time 12import urllib .request 13from pathlib import Path 14 15ROOT = Path (__file__ ).resolve ().parents [3 ] 16WORDS = ("river stone market lantern copper winter garden signal harbor needle orbit velvet canyon ember ledger quartz " 17"meadow anchor violet thunder cedar marble piston ribbon cobalt falcon glacier hollow ivory juniper kernel " 18"lattice mirror nectar oasis pepper quarry raven saddle tundra umber vessel willow xenon yellow zephyr" ).split () 19 20 21def build (tokens ,code ,rng ): 22body = [rng .choice (WORDS )for _ in range (int (tokens / 1.35 ))] 23at = rng .randrange (len (body )// 3 ,2 * len (body )// 3 ) 24body [at :at ]= [f"The access code for the harbor gate is { code } ." ] 25return ("Read the following log carefully, then answer the question at the end.\n\n" + " " .join (body )+ 26"\n\nQuestion: what is the access code for the harbor gate? Answer with the code only." ) 27 28 29def main (): 30cfg_path ,out_path = sys .argv [1 ],sys .argv [2 ] 31tokens = int (sys .argv [3 ])if len (sys .argv )> 3 else 60000 32port = 8091 33cfg = json .loads (Path (cfg_path ).read_text ()) 34cfg ["port" ]= port 35cfg ["log" ]= "/tmp/long-context.log" 36tmp = Path ("/tmp/long-context.json" ) 37tmp .write_text (json .dumps (cfg ,indent = 1 )) 38proc = subprocess .Popen ([sys .executable ,str (ROOT / "serve" / "server.py" ),"--engine" ,"strata" ,"--config" , 39str (tmp ),"--port" ,str (port )],cwd = str (ROOT ), 40stdout = subprocess .DEVNULL ,stderr = subprocess .DEVNULL ) 41try : 42t0 = time .time () 43while time .time ()- t0 < 1800 : 44if proc .poll ()is not None : 45raise SystemExit (f"server exited { proc . returncode } " ) 46try : 47with urllib .request .urlopen (f"http://127.0.0.1: { port } /v1/models" ,timeout = 5 )as r : 48if r .status == 200 : 49break 50except Exception : 51pass 52time .sleep (3 ) 53f"ready in { time . time () - t0 :.0f } s" ,flush = True ) 54rng = random .Random (7 ) 55code = "481516" 56prompt = build (tokens ,code ,rng ) 57body = json .dumps ({"messages" : [{"role" :"user" ,"content" :prompt }],"max_tokens" :32 ,"temperature" :0 , 58"chat_template_kwargs" : {"enable_thinking" :False }}).encode () 59req = urllib .request .Request (f"http://127.0.0.1: { port } /v1/chat/completions" ,data = body , 60headers = {"Content-Type" :"application/json" }) 61t1 = time .time () 62with urllib .request .urlopen (req ,timeout = 3600 )as r : 63out = json .loads (r .read ()) 64wall = time .time ()- t1 65t = out .get ("timings" )or {} 66answer = out ["choices" ][0 ]["message" ].get ("content" ,"" ) 67row = {"tokens" :tokens ,"prompt_n" :t .get ("prompt_n" ),"prompt_per_second" :t .get ("prompt_per_second" ), 68"predicted_per_second" :t .get ("predicted_per_second" ),"wall_s" :round (wall ,1 ), 69"answer" :answer [:80 ],"recalled" :code in answer } 70Path (out_path ).write_text (json .dumps (row ,indent = 1 )) 71json .dumps (row )) 72finally : 73proc .terminate () 74try : 75proc .wait (180 ) 76except subprocess .TimeoutExpired : 77proc .kill () 78 79 80if __name__ == "__main__" : 81main ()