okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
cc3f9142e
main
1"""Round 0213 TUICFG, step 3: one precedence for a launch's engine options (#5178, #5132, #4985). 2 3Explicit launch values, then persisted overrides, then calibrated values, then Auto (strata_control/arg_layers.py). Each case builds the run 4config like a launch does (derive_config, stamp, write, read back), then asks serve/server.py engine_args for the final native argv, and 5launches a second time with no new flags. The Control and the engine are doubles; nothing starts.""" 6from __future__import annotations 7 8import json 9import unittest 10from unittest import mock 11 12from strata_cli .launch import settings as S 13from strata_cli .launch .runner import derive_config 14from strata_control import arg_layers ,drafting ,tune_knobs 15from tests .terminal .test_drafting_single import Base ,flag 16 17 18class Launches (Base ): 19def server_args (self ,cfg ,backend = "cuda" ,archs = ("sm_120" ,),vram = 8000.0 ): 20from serve .server import engine_args 21with mock .patch .object (drafting ,"detect_hardware" ,return_value = (backend ,list (archs ))),mock .patch ("serve.server.drafting_vram_mib" ,return_value = vram ), \ 22mock .patch .object (drafting ,"installed_engine_exe" ,return_value = self .exe ): 23return engine_args (cfg ,str (self .exe ),announce = False ) 24 25def base (self ,** extra ): 26return {"exe" :"component:strata-engine" ,"model_name" :"qwen3.8-flash-next-iq3_s" ,"port" :8080 ,"args" :list (self .args ),** extra } 27 28def launch (self ,base = None ,extra = None ,prev = None ,** settings ): 29"""What `Runner.build_config` writes: derive, stamp, and a round trip through the file.""" 30s = S .LaunchSettings (extra = dict (extra or {}),** settings ) 31with mock .patch .object (drafting ,"detect_hardware" ,return_value = ("cuda" , ["sm_120" ])): 32cfg = derive_config (base or self .base (),s ,bool_flags = tune_knobs .BOOL_FLAGS ,prev = prev ) 33arg_layers .stamp (cfg ) 34return json .loads (json .dumps (cfg )) 35 36# ---- #5178 37def test_an_explicit_draft_conf_reaches_the_engine_and_survives_a_second_launch (self ): 38for mode in ("dflash2" ,"auto" ): 39with self .subTest (mode = mode ): 40first = self .launch (extra = {"--draft-conf" :"0.35" },drafting = mode ) 41argv = self .server_args (first ,vram = 24000.0 )# (an 8 GB card is the 12 GB class, where Auto is the MTP for IQ3_S: drafting.CLASS_DEFAULTS) 42self .assertEqual ((argv .count ("--draft-conf" ),flag (argv ,"--draft-conf" )), (1 ,"0.35" )) 43self .assertEqual (flag (first ["user_args" ],"--draft-conf" ),"0.35" ,"the saved config carries it where the drafter policy keeps it" ) 44second = self .launch (extra = {"--draft-conf" :"0.35" },prev = first ,drafting = mode ) 45self .assertEqual (self .server_args (second ,vram = 24000.0 ),argv ,"a second launch with the same flags is the same argv" ) 46self .assertEqual (second ["args" ].count ("--draft-conf" ),1 ) 47 48def test_an_explicit_value_is_not_remembered_when_the_next_launch_leaves_it_out (self ): 49first = self .launch (extra = {"--draft-conf" :"0.35" },drafting = "dflash2" ) 50second = self .launch (prev = first ,drafting = "dflash2" ) 51argv = self .server_args (second ) 52self .assertNotEqual (flag (argv ,"--draft-conf" ),"0.35" ,"back to Auto" ) 53self .assertNotIn ("--draft-conf" ,second .get ("user_args" )or []) 54self .assertNotIn ("launch_set" ,second ) 55 56def test_the_new_explicit_value_beats_the_earlier_one (self ): 57first = self .launch (extra = {"--draft-conf" :"0.35" },drafting = "dflash2" ) 58second = self .launch (extra = {"--draft-conf" :"0.6" },prev = first ,drafting = "dflash2" ) 59argv = self .server_args (second ) 60self .assertEqual ((argv .count ("--draft-conf" ),flag (argv ,"--draft-conf" )), (1 ,"0.6" )) 61 62def test_an_explicit_spec_survives_the_drafter_policy_too (self ): 63cfg = self .launch (extra = {"--spec" :"3" },drafting = "dflash2" ) 64argv = self .server_args (cfg ) 65self .assertEqual ((argv .count ("--spec" ),flag (argv ,"--spec" )), (1 ,"3" )) 66 67# ---- explicit off 68def test_an_explicit_off_removes_the_option_and_stays_off_for_the_same_command (self ): 69base = self .base (args = [* self .args ,"--mmap-experts" ]) 70first = self .launch (base ,extra = {"--mmap-experts" :"off" }) 71self .assertNotIn ("--mmap-experts" ,first ["args" ]) 72self .assertNotIn ("--mmap-experts" ,self .server_args (first )) 73second = self .launch (base ,extra = {"--mmap-experts" :"off" },prev = first ) 74self .assertNotIn ("--mmap-experts" ,self .server_args (second )) 75third = self .launch (base ,prev = second )# the flag left out: the installed config's own value is back 76self .assertIn ("--mmap-experts" ,self .server_args (third )) 77 78def test_an_explicit_off_beats_a_persisted_override (self ): 79base = self .base (user_args = ["--pcie-frac" ,"0.3" ],args = [* self .args ,"--pcie-frac" ,"0.3" ]) 80cfg = self .launch (base ,extra = {"--pcie-frac" :"off" }) 81argv = self .server_args (cfg ) 82self .assertNotIn ("--pcie-frac" ,argv ) 83self .assertNotIn ("--pcie-frac" ,cfg .get ("user_args" )or []) 84 85# ---- #5132 86def test_a_hand_edit_of_the_launch_config_survives_the_next_launch (self ): 87first = self .launch () 88self .assertNotIn ("--pcie-frac" ,first ["args" ]) 89first ["args" ]+= ["--pcie-frac" ,"0.2" ,"--pool-workers" ,"6" ]# written by hand (or by a tool that does not stamp) 90second = self .launch (prev = first ) 91argv = self .server_args (second ) 92self .assertEqual ((flag (argv ,"--pcie-frac" ),flag (argv ,"--pool-workers" )), ("0.2" ,"6" )) 93self .assertEqual ((argv .count ("--pcie-frac" ),argv .count ("--pool-workers" )), (1 ,1 )) 94third = self .launch (prev = second )# and again with no new flags 95self .assertEqual (self .server_args (third ),argv ) 96self .assertEqual (flag (third ["user_args" ],"--pcie-frac" ),"0.2" ) 97 98def test_the_launch_flags_do_not_come_back_as_hand_edits (self ): 99first = self .launch (extra = {"--pcie-frac" :"0.4" }) 100again = self .launch (prev = first ) 101self .assertNotIn ("--pcie-frac" ,again ["args" ],"given once, not remembered" ) 102again2 = self .launch (extra = {"--pcie-frac" :"0.4" },prev = first ) 103self .assertEqual (flag (again2 ["args" ],"--pcie-frac" ),"0.4" ) 104 105def test_a_config_from_before_the_stamp_is_not_mistaken_for_hand_edits (self ): 106old = self .launch () 107for key in ("args_derived" ,): 108old .pop (key ) 109old ["args_written" ]= ["--unrelated" ]# the installed config's copy, as older launches left it 110old ["args" ]+= ["--cards" ,"0" ] 111new = self .launch (prev = old ) 112self .assertNotIn ("--cards" ,new ["args" ]) 113 114def test_calibrated_values_are_kept_by_the_next_launch (self ): 115from strata_control import calibration 116cfg0 = self .launch ()# the launch config the profile points at 117applied = json .loads (json .dumps (cfg0 )) 118applied ["args" ]= tune_knobs .apply_to_argv (applied ["args" ], {"--pcie-frac" :"0.2" ,"--pool-workers" :"6" }) 119arg_layers .record_calibration (applied ,cfg0 , {"--pcie-frac" :"0.2" ,"--pool-workers" :"6" }) 120self .assertEqual (applied ["calibrated" ]["settings" ], {"--pcie-frac" :"0.2" ,"--pool-workers" :"6" }) 121self .assertEqual (applied ["args_derived" ],applied ["args" ],"its own values are not hand edits" ) 122second = self .launch (prev = applied ) 123argv = self .server_args (second ) 124self .assertEqual ((flag (argv ,"--pcie-frac" ),flag (argv ,"--pool-workers" )), ("0.2" ,"6" )) 125self .assertIn ("calibrated" ,second ) 126self .assertNotIn ("--pcie-frac" ,second .get ("user_args" )or [],"calibrated is not an override: provenance kept" ) 127third = self .launch (prev = second ) 128self .assertEqual (self .server_args (third ),argv ) 129self .assertTrue (hasattr (calibration ,"apply_result" )) 130 131def test_calibrated_values_lose_to_persisted_overrides_and_explicit_values (self ): 132cfg0 = self .launch () 133applied = json .loads (json .dumps (cfg0 )) 134applied ["args" ]= tune_knobs .apply_to_argv (applied ["args" ], {"--pcie-frac" :"0.2" }) 135arg_layers .record_calibration (applied ,cfg0 , {"--pcie-frac" :"0.2" }) 136base = self .base (user_args = ["--pcie-frac" ,"0.3" ]) 137self .assertEqual (flag (self .server_args (self .launch (base ,prev = applied )),"--pcie-frac" ),"0.3" ,"persisted over calibrated" ) 138self .assertEqual (flag (self .server_args (self .launch (base ,extra = {"--pcie-frac" :"0.5" },prev = applied )),"--pcie-frac" ),"0.5" ,"explicit over both" ) 139self .assertEqual (flag (self .server_args (self .launch (prev = applied )),"--pcie-frac" ),"0.2" ,"calibrated over Auto" ) 140 141def test_a_calibration_for_another_context_or_model_is_not_applied (self ): 142cfg0 = self .launch () 143applied = json .loads (json .dumps (cfg0 )) 144applied ["args" ]= tune_knobs .apply_to_argv (applied ["args" ], {"--pcie-frac" :"0.2" }) 145arg_layers .record_calibration (applied ,cfg0 , {"--pcie-frac" :"0.2" }) 146other_ctx = self .base (args = [a if a != "131072" else "65536" for a in self .args ]) 147self .assertNotIn ("--pcie-frac" ,self .launch (other_ctx ,prev = applied )["args" ]) 148other_model = self .base (model_name = "qwen3.8-flash-next-gyro-gyro-s" ) 149self .assertNotIn ("--pcie-frac" ,self .launch (other_model ,prev = applied )["args" ]) 150 151# ---- #4985 and the shape of the file 152def test_one_effective_value_per_option_in_every_case (self ): 153cases = [dict (),dict (extra = {"--draft-conf" :"0.35" }),dict (extra = {"--pcie-frac" :"0.4" ,"--spec" :"3" }),dict (extra = {"--pool-workers" :"off" })] 154prev = None 155for kw in cases : 156cfg = self .launch (prev = prev ,drafting = "dflash2" ,** kw ) 157argv = self .server_args (cfg ) 158for name in set (a for a in argv if a .startswith ("--" )and a not in tune_knobs .BOOL_FLAGS ): 159self .assertEqual (argv .count (name ),1 , (kw ,name )) 160prev = cfg 161 162 163if __name__ == "__main__" : 164unittest .main ()