okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
2b36e8a02
main
1#!/usr/bin/env python3 2"""Is decode at 56K limited by KV streaming? Raise --kv-resident to the whole context if it fits, and re-measure the same 356K prompt. Writes one row; compare it with the 31.0 tok/s of the shipped 32,768-cell setting. 4 5python dgx_kvresident.py CONFIG OUT.json [--tokens 56000] 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" ).split () 18 19 20def build (tokens ,code ,rng ): 21body = [rng .choice (WORDS )for _ in range (int (tokens / 1.35 ))] 22at = rng .randrange (len (body )// 3 ,2 * len (body )// 3 ) 23body [at :at ]= [f"The access code for the harbor gate is { code } ." ] 24return ("Read the following log carefully, then answer the question at the end.\n\n" + " " .join (body )+ 25"\n\nQuestion: what is the access code for the harbor gate? Answer with the code only." ) 26 27 28def main (): 29cfg_path ,out_path = sys .argv [1 ],sys .argv [2 ] 30tokens = int (sys .argv [3 ])if len (sys .argv )> 3 else 56000 31port = 8091 32cfg = json .loads (Path (cfg_path ).read_text ()) 33cfg ["port" ]= port 34cfg ["log" ]= "/tmp/kvresident.log" 35tmp = Path ("/tmp/kvresident.json" ) 36tmp .write_text (json .dumps (cfg ,indent = 1 )) 37proc = subprocess .Popen ([sys .executable ,str (ROOT / "serve" / "server.py" ),"--engine" ,"strata" ,"--config" , 38str (tmp ),"--port" ,str (port )],cwd = str (ROOT ), 39stdout = subprocess .DEVNULL ,stderr = subprocess .DEVNULL ) 40try : 41t0 = time .time () 42while time .time ()- t0 < 1800 : 43if proc .poll ()is not None : 44raise SystemExit (f"server exited { proc . returncode } (see /tmp/kvresident.log)" ) 45try : 46with urllib .request .urlopen (f"http://127.0.0.1: { port } /v1/models" ,timeout = 5 )as r : 47if r .status == 200 : 48break 49except Exception : 50pass 51time .sleep (3 ) 52f"ready in { time . time () - t0 :.0f } s" ,flush = True ) 53rng = random .Random (7 ) 54code = "481516" 55prompt = build (tokens ,code ,rng ) 56body = json .dumps ({"messages" : [{"role" :"user" ,"content" :prompt }],"max_tokens" :64 ,"temperature" :0 , 57"chat_template_kwargs" : {"enable_thinking" :False }}).encode () 58req = urllib .request .Request (f"http://127.0.0.1: { port } /v1/chat/completions" ,data = body , 59headers = {"Content-Type" :"application/json" }) 60t1 = time .time () 61with urllib .request .urlopen (req ,timeout = 3600 )as r : 62out = json .loads (r .read ()) 63wall = time .time ()- t1 64t = out .get ("timings" )or {} 65answer = out ["choices" ][0 ]["message" ].get ("content" ,"" ) 66row = {"tokens" :tokens ,"kv_resident" :cfg ["args" ][cfg ["args" ].index ("--kv-resident" )+ 1 ], 67"prompt_n" :t .get ("prompt_n" ),"prompt_per_second" :t .get ("prompt_per_second" ), 68"predicted_n" :t .get ("predicted_n" ),"predicted_per_second" :t .get ("predicted_per_second" ), 69"wall_s" :round (wall ,1 ),"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 ()