okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
004e22c1d
main
1"""A stand-in for `tune_session.WorkerSession`: speeds are computed from the flags it is given, so the tuner's decisions can be 2tested without an engine. The numbers mean nothing about any hardware.""" 3 4from __future__import annotations 5 6import contextlib 7import random 8from typing import Any ,Callable 9 10from strata_control .tune_session import RequestSet ,RunResult ,StartFailed 11 12 13class StubEngine : 14def __init__ (self ,session :"StubSession" ,args :list [str ],env :dict [str ,Any ])-> None : 15self .s = session 16self .args = args 17self .env = env 18self .facts = dict (session .facts ) 19 20def _rate (self ,tune :dict [str ,Any ]| None = None )-> float : 21base = self .s .model (self .args ,self .env ,tune ) 22return base * (1.0 + self .s .rng .gauss (0.0 ,self .s .noise )) 23 24def decode_once (self ,tune :dict [str ,Any ]| None = None ,tokens :int = 200 )-> float | None : 25self .s .decodes += 1 26return self ._rate (tune ) 27 28def measure (self ,spec :RequestSet )-> RunResult : 29d = [self ._rate ()for _ in range (spec .decodes )] 30import statistics 31pp = self .s .pp (self .args ,self .env )* (1.0 + self .s .rng .gauss (0.0 ,self .s .noise / 3 )) 32r = RunResult (load_s = 20.0 ,decode_tok_s = statistics .median (d ),decode_runs = d ,pp_tok_s = pp ,pp_cold_tok_s = pp * 0.8 , 33ttft_s = 4096 / pp ,rss_peak_mib = self .s .rss (self .args ,self .env ),vram_used_mib = self .s .vram (self .args ), 34vram_added_mib = self .s .vram (self .args )- 400 ,min_avail_mib = self .s .avail - self .s .rss (self .args ,self .env ), 35facts = dict (self .facts ),wall_s = self .s .run_s ) 36return r 37 38 39class StubSession : 40def __init__ (self ,* ,model :Callable [...,float ],pp :Callable [...,float ]| None = None ,noise :float = 0.005 , 41seed :int = 1 ,facts :dict [str ,Any ]| None = None ,fail :Callable [[list [str ],dict ],StartFailed | None ]| None = None , 42rss :Callable [...,float ]| None = None ,vram :Callable [...,float ]| None = None ,avail :float = 40000 , 43run_s :float = 90.0 ,clock :Any = None )-> None : 44self .clock = clock 45self .model = model 46self .pp = pp or (lambda a ,e :1500.0 ) 47self .noise = noise 48self .rng = random .Random (seed ) 49self .facts = facts or {"workers" :6 ,"slots" :3000 ,"resident_ram" :False } 50self .fail = fail 51self .rss = rss or (lambda a ,e :12000.0 ) 52self .vram = vram or (lambda a :10000.0 ) 53self .avail = avail 54self .run_s = run_s 55self .runs :list [tuple [list [str ],dict ]]= [] 56self .decodes = 0 57self .mem = {"total_mib" :64000 ,"available_mib" :avail } 58 59def system (self )-> dict [str ,Any ]: 60return {"memory" :dict (self .mem ),"gpus" : [{"vendor" :"nvidia" ,"index" :0 ,"name" :"Stub GPU" ,"vram_total_mib" :12000 , 61"vram_used_mib" :400 ,"driver" :"1" }],"cpu" : {"model" :"stub" ,"logical" :12 ,"hybrid" :False }} 62 63@ contextlib . contextmanager 64def engine (self ,args :list [str ],env :dict [str ,Any ],pre_start = None ): 65if pre_start is not None : 66pre_start () 67self .runs .append ((list (args ),dict (env ))) 68if self .clock is not None : 69self .clock .t += self .run_s 70if self .fail : 71err = self .fail (args ,env ) 72if err : 73raise err 74yield StubEngine (self ,list (args ),dict (env )) 75 76 77def flag (args :list [str ],name :str )-> str | None : 78for i ,a in enumerate (args ): 79if a == name : 80return args [i + 1 ]if i + 1 < len (args )and not str (args [i + 1 ]).startswith ("--" )else "" 81return None 82 83 84class FakeClock : 85def __init__ (self )-> None : 86self .t = 0.0 87 88def __call__ (self )-> float : 89return self .t 90 91def sleep (self ,s :float )-> None : 92self .t += s