okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
c69872aa2
main
1#!/usr/bin/env python3 2"""Spec-Sweep fuer Strata: pro N den Server neu starten, 3 Laeufe je Prompt, 256 Token, greedy. 3Liest tok/s aus dem Serverlog ("generated in X ms").""" 4import json ,os ,re ,signal ,subprocess ,sys ,time ,urllib .request ,random ,pathlib ,statistics 5ROOT = pathlib .Path .home ()/ "ai/Strata" 6OUT = pathlib .Path .home ()/ "messungen/2026-10-01-strata-spec-sweep" 7BASE = json .load (open (ROOT / "strata-swift-iq3_xxs.json.bak-20261001-vorsweep" )) 8NS = [int (x )for x in sys .argv [1 ].split ("," )] 9RUNS = 3 10def corpus (tokens ): 11files = sorted ((ROOT / "src" ).rglob ("*.cpp" ))+ sorted ((ROOT / "serve" ).glob ("*.py" ))+ sorted ((ROOT / "docs" ).glob ("*.md" )) 12txt = "" 13for f in files : 14txt += f"\n### { f . name } \n" + f .read_text (errors = "ignore" ) 15if len (txt )> tokens * 3.2 :break 16return txt [:int (tokens * 3.2 )] 17PROMPTS = { 18"kurz-code" : ("Write a Python class LRUCache with get/put, type hints and unit tests using pytest." ,0 ), 19"4k-code" : ("Here is source code:\n" + corpus (4000 )+ "\n\nExplain what this code does and propose three concrete refactorings with code." ,4000 ), 20"32k-code" : ("Here is source code:\n" + corpus (32000 )+ "\n\nExplain what this code does and propose three concrete refactorings with code." ,32000 ), 21} 22def stop (): 23me = os .getpid () 24for sig in (signal .SIGINT ,signal .SIGTERM ,signal .SIGKILL ): 25for d in os .listdir ("/proc" ): 26if d .isdigit ()and int (d )!= me : 27try :cmd = open (f"/proc/ { d } /cmdline" ).read ().replace (chr (0 )," " ) 28except OSError :continue 29if "serve/server.py --engine strata" in cmd or "engine/strata" in cmd and "python3 sweep" not in cmd : 30try :os .kill (int (d ),sig ) 31except OSError :pass 32time .sleep (8 ) 33for _ in range (60 ): 34port = subprocess .run ("ss -ltn | grep -q ':8080 '" ,shell = True ).returncode == 0 35mem = max (int (x )for x in subprocess .run ("nvidia-smi --query-gpu=memory.used --format=csv,noheader,nounits" ,shell = True ,capture_output = True ,text = True ).stdout .split ()) 36if not port and mem < 1000 :return 37time .sleep (3 ) 38raise SystemExit ("Port/GPU wurde nicht frei" ) 39def start (n ): 40cfg = json .loads (json .dumps (BASE )) 41a = cfg ["args" ];a [a .index ("--spec" )+ 1 ]= str (n ) 42cfg ["log" ]= str (OUT / f"server-spec { n } .log" ) 43p = OUT / f"cfg-spec { n } .json" ;json .dump (cfg ,open (p ,"w" ),indent = 1 ) 44proc = subprocess .Popen (["setsid" ,str (ROOT / ".venv/bin/python" ),str (ROOT / "serve/server.py" ),"--engine" ,"strata" ,"--config" ,str (p ),"--port" ,"8080" ,"--open" ], 45cwd = ROOT ,stdout = open (OUT / f"stdout-spec { n } .txt" ,"w" ),stderr = subprocess .STDOUT ) 46for _ in range (300 ): 47time .sleep (3 ) 48if proc .poll ()is not None :raise SystemExit (f"Server fuer spec= { n } sofort beendet, siehe stdout-spec { n } .txt" ) 49try : 50urllib .request .urlopen ("http://127.0.0.1:8080/v1/models" ,timeout = 3 ).read ();return cfg ["log" ] 51except Exception :pass 52raise SystemExit (f"Server fuer spec= { n } nicht hochgekommen" ) 53def ask (text ,nonce ): 54body = {"model" :BASE ["model_name" ],"messages" : [{"role" :"user" ,"content" :f"[ { nonce } ]\n" + text }], 55"max_tokens" :256 ,"temperature" :0 ,"stream" :False } 56r = urllib .request .Request ("http://127.0.0.1:8080/v1/chat/completions" ,json .dumps (body ).encode (), {"Content-Type" :"application/json" }) 57return json .load (urllib .request .urlopen (r ,timeout = 900 )) 58res = [] 59for n in NS : 60stop ();log = start (n ) 61for name , (text ,_ )in PROMPTS .items (): 62ask (text ,f"warm { n } { name } { random . random () } " )# Aufwaermung, zaehlt nicht 63for i in range (RUNS ): 64before = open (log ).read () 65ask (text ,f"r { n } { name } { i } { random . random () } " ) 66time .sleep (1 ) 67new = open (log ).read ()[len (before ):] 68m = re .findall (r"prompt (\d+) tokens = .*?, (\d+) generated in (\d+) ms \(([\d.]+) tok/s\), drafts accepted (\d+) of (\d+)" ,new ) 69if not m :"kein Logtreffer" ,n ,name ,i );continue 70pt ,gen ,ms ,tps ,acc ,tot = m [- 1 ] 71res .append (dict (spec = n ,prompt = name ,run = i ,prompt_tokens = int (pt ),generated = int (gen ),tok_s = float (tps ),accepted = int (acc ),drafted = int (tot ))) 72n ,name ,i ,tps ,f" { acc } / { tot } " ,flush = True ) 73json .dump (res ,open (OUT / "sweep.json" ,"w" ),indent = 1 ) 74"\nspec prompt median min-max accept" ) 75for n in NS : 76for name in PROMPTS : 77r = [x for x in res if x ["spec" ]== n and x ["prompt" ]== name ] 78if r : 79t = [x ["tok_s" ]for x in r ] 80f" { n :>4 } { name :<10 } { statistics . median ( t ):7.1f } { min ( t ):.0f } - { max ( t ):.0f } { sum ( x [ 'accepted' ] for x in r ) / max ( 1 , sum ( x [ 'drafted' ] for x in r )):.0% } " ) 81 82stop ()