okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
6e1085c1d
main
1"""Continue the fixed-clock run while retaining completed preparation trials.""" 2import argparse 3import importlib .util 4import json 5from pathlib import Path 6import time 7import traceback 8 9spec = importlib .util .spec_from_file_location ("benchmark" ,Path (__file__ ).with_name ("strata_bench_run.py" )) 10b = importlib .util .module_from_spec (spec ) 11spec .loader .exec_module (b ) 12 13 14class Continuation (b .Runner ): 15def __init__ (self ,resident ): 16super ().__init__ () 17self .resident = set (resident ) 18self .rows = [json .loads (s )for s in (b .OUT / "requests.jsonl" ).read_text (encoding = "utf-8" ).splitlines ()if s .strip ()] 19configs = list ((b .OUT / "configs" ).glob ("*.json" )) 20self .server_number = max ([int (p .name .split ("-" )[0 ])for p in configs ]+ [0 ]) 21gates = b .OUT / "262k-gates.json" 22self .gates = json .loads (gates .read_text (encoding = "utf-8" ))if gates .exists ()else {} 23self .start_failures = {} 24 25def start (self ,model ,ctx = 131072 ,parallel = 1 ,mtp = True ,cache = False ,cpu_off = False ): 26self .engine_environment = {"STRATA_RESIDENT_HEADROOM_GIB" :"12" ,"STRATA_UNBUFFERED_LOAD" :"1" }if model in self .resident else {} 27key = (model ,ctx ,parallel ,mtp ,cache ,cpu_off ) 28if not self .monitor .thread .is_alive (): 29raise b .CaseError ("resource monitor is unavailable" ) 30if self .start_failures .get (key ,0 )>= 2 : 31self .event ("startup_condition_skipped" ,requested_model = model ,context = ctx , 32reason = "same startup condition stopped twice on memory threshold" ) 33raise b .CaseError ("repeated startup memory threshold" ) 34try : 35return super ().start (model ,ctx ,parallel ,mtp ,cache ,cpu_off ) 36except b .CaseError as exc : 37if "memory threshold" in str (exc ): 38self .start_failures [key ]= self .start_failures .get (key ,0 )+ 1 39raise 40 41def config (self ,model ,* args ,** kwargs ): 42c = super ().config (model ,* args ,** kwargs ) 43if model in self .resident : 44pack = Path (c ["args" ][c ["args" ].index ("--pack" )+ 1 ]) 45if not (pack / "experts.bin" ).is_file (): 46raise b .CaseError ("resident pack has not been prepared" ) 47c ["args" ].append ("--resident-experts" ) 48c ["args" ].extend (["--resident-budget-gib" ,"20" ]) 49return c 50 51def perform (self ,label ,* args ,** kwargs ): 52group = kwargs .get ("group" ,"speed" ) 53if group in ("speed" ,"bridge" ,"language" ): 54prior = next ((r for r in reversed (self .rows ) 55if r ["model" ]== self .model and r ["context" ]== self .ctx and r ["label" ]== label 56and r .get ("weights_mode" ,"native" )== self .weights_mode 57and r .get ("engine_environment" , {})== self .engine_environment 58and r ["group" ]== group and r .get ("ok" )and r .get ("token_count_matches" ) 59and not r .get ("engine" , {}).get ("reused" )),None ) 60if prior : 61self .event ("request_already_completed" ,label = label ,request_file = prior ["request_file" ]) 62return prior 63row = super ().perform (label ,* args ,** kwargs ) 64return row 65 66def inventory (self ): 67# The original run hashed all source files before these continuation trials. 68identity = json .loads ((b .OUT / "engine-identity.json" ).read_text (encoding = "utf-8" )) 69engine = Path (self .config ("q2_0" ,131072 )["exe" ]) 70with engine .open ("rb" )as f : 71checksum = b .hashlib .file_digest (f ,"sha256" ).hexdigest () 72if checksum != identity ["engine_sha256" ]: 73raise b .CaseError ("Engine changed since the original inventory" ) 74self .event ("inventory_retained" ,manifest = "model-manifest.json" , 75resident_models = sorted (self .resident ),engine_sha256 = checksum ) 76 77def completed_speed (self ,model ,target ): 78env = {"STRATA_RESIDENT_HEADROOM_GIB" :"12" ,"STRATA_UNBUFFERED_LOAD" :"1" }if model in self .resident else {} 79return sum (bool (r .get ("ok" ))and r .get ("token_count_matches" )and not r .get ("engine" , {}).get ("reused" ) 80for r in self .rows if r ["model" ]== model and r ["group" ]== "speed" 81and r .get ("target" )== target and r .get ("engine_environment" , {})== env ) 82 83def run (self ): 84supervisor = b .subprocess .Popen ([b .sys .executable ,str (b .SCRIPT ),"--watchdog" ,str (b .os .getpid ()), 85str (self .clock ["measurement_stop_epoch" ]+ 300 )],stdout = b .subprocess .DEVNULL , 86stderr = b .subprocess .DEVNULL ,creationflags = b .FLAGS ) 87try : 88self .budget ("resident_preparation" ,min (20 * 60 ,self .hard - time .monotonic ())) 89self .init_tokenizer () 90self .inventory () 91for model in sorted (self .resident ): 92def smoke (): 93self .start (model ) 94self .perform ("resident-preparation-4096" ,self .make (4096 ,rep = 0 ),4096 ,group = "preparation" ) 95self .safe_block (smoke ) 96self .stop () 97# Retain time for every authorized phase after the approval wait. 98stages = [("speed" ,190 ,self .speed ), ("recall" ,55 ,self .recall ), 99 ("quality" ,55 ,self .quality ), ("features" ,30 ,self .features ), 100 ("soak" ,30 ,self .soak ), ("concurrency" ,15 ,self .concurrency )] 101allocations = [] 102for i , (phase ,cap_minutes ,fn )in enumerate (stages ): 103later = sum (s [1 ]for s in stages [i + 1 :])* 60 104seconds = min (cap_minutes * 60 ,max (0 ,self .hard - time .monotonic ()- later )) 105self .budget (phase ,seconds ) 106allocations .append ({"phase" :phase ,"maximum_seconds" :seconds }) 107b .write (b .OUT / "continuation-phase-budgets.json" ,allocations ) 108try : 109fn () 110except b .CaseError as exc : 111self .event ("phase_stopped" ,reason = str (exc )) 112except Exception as exc : 113# A phase failure is retained; independent later phases still run. 114self .event ("phase_error" ,error_type = type (exc ).__name__ ,reason = str (exc )) 115 (b .OUT / f" { phase } -error.txt" ).write_text (traceback .format_exc (),encoding = "utf-8" ) 116finally : 117if self .active_worker and self .active_worker .poll ()is None : 118b .kill_tree (self .active_worker ) 119self .active_worker = None 120for job in self .child_jobs : 121b .kill_tree (job ) 122self .child_jobs .clear () 123self .stop () 124self .event ("phase_finished" ) 125self .budget ("reserve" ,max (0 ,self .hard - time .monotonic ())) 126if (b .OUT / "code" ).exists (): 127self .evaluate (min (720 ,self .left ())) 128for model in b .MODELS : 129missing = [t for t in (128000 ,32768 ,64000 ,4096 ,1024 ,512 ,128 ) 130if self .completed_speed (model ,t )< 3 ] 131if not missing or self .left ()< 30 : 132continue 133def fill_missing (): 134self .start (model ) 135for target in missing : 136completed = self .completed_speed (model ,target ) 137for rep in range (completed + 1 ,4 ): 138self .perform (f"reserve- { target } -r { rep } " ,self .make (target ,rep = rep ),target , 139expected = f"amber-quartz- { target } - { rep } -50" if target >= 32768 else None ) 140self .safe_block (fill_missing ) 141self .report () 142finally : 143if self .active_worker and self .active_worker .poll ()is None : 144b .kill_tree (self .active_worker ) 145for job in self .child_jobs : 146b .kill_tree (job ) 147self .stop () 148self .monitor .done .set () 149if supervisor .poll ()is None : 150supervisor .terminate () 151 152 153def main (): 154p = argparse .ArgumentParser () 155p .add_argument ("--resident" ,nargs = "*" ,choices = b .MODELS ,default = []) 156a = p .parse_args () 157with b .socket .socket ()as sock : 158if sock .connect_ex (("127.0.0.1" ,b .PORT ))== 0 : 159raise RuntimeError ("Benchmark service is still occupied; it was preserved" ) 160if a .resident : 161approval = b .OUT / "resident-approval.json" 162allowed = json .loads (approval .read_text (encoding = "utf-8" ))if approval .exists ()else {} 163if not set (a .resident ).issubset (set (allowed .get ("models" , []))): 164raise RuntimeError ("Explicit approval for resident benchmark conditions is required" ) 165 (b .OUT / "runner.pid" ).write_text (str (b .os .getpid ())) 166Continuation (a .resident ).run () 167 168 169if __name__ == "__main__" : 170main ()