okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
34e7c5cb1
main
1"""The engine options a person set by hand survive setup, the app and every start (#3216 control vectors, #3215 and #3270 2--vram-reserve-mib). strata_control/user_args.py is the rule; setup.carry_over, serve/server.py engine_args, serve/runconfig.py 3and strata_control/tune_apply.py use it. Every outside effect is mocked (tools/test_setup_golden.py's install()). 4 5python -m unittest tools.test_user_args 6""" 7from __future__import annotations 8 9import contextlib 10import io 11import sys 12import unittest 13from pathlib import Path 14 15ROOT = Path (__file__ ).resolve ().parents [1 ] 16sys .path .insert (0 ,str (ROOT )) 17sys .path .insert (0 ,str (ROOT / "tools" )) 18import setup # noqa: E402 19import strata_mcp # noqa: E402 20from serve import runconfig # noqa: E402 21from serve .server import engine_args # noqa: E402 22from strata_control import tune_apply ,user_args # noqa: E402 23from test_setup_golden import PROFILES ,install # noqa: E402 24 25CVEC = ["--control-vector-scaled" ,"C:/vectors/mine.gguf:1.0" ,"--control-vector-layer-range" ,"4" ,"44" , 26"--cvec-mode" ,"project" ,"--cvec-dir" ,"per-layer" ] 27 28 29def value (args ,flag ): 30return args [args .index (flag )+ 1 ]if flag in args [:- 1 ]else None 31 32 33class Rule (unittest .TestCase ): 34def test_groups_and_merge (self ): 35self .assertEqual (user_args .groups (["--a" ,"1" ,"--b" ,"--c" ,"x" ,"y" ]), [["--a" ,"1" ], ["--b" ], ["--c" ,"x" ,"y" ]]) 36self .assertEqual (user_args .merge (["--a" ,"1" ,"--b" ,"2" ,"--a" ,"3" ], ["--a" ,"9" ,"--z" ]), ["--b" ,"2" ,"--a" ,"9" ,"--z" ]) 37self .assertEqual (user_args .merge (["--a" ,"1" ], []), ["--a" ,"1" ]) 38 39def test_capture_tells_hand_edits_from_what_was_written (self ): 40written = ["--max-context" ,"32768" ,"--vram-reserve-mib" ,"700" ,"--spec" ,"4" ] 41cfg = {"args" :written + CVEC ,"args_written" :written } 42self .assertEqual (user_args .capture (cfg ),CVEC ) 43cfg = {"args" : ["--max-context" ,"32768" ,"--vram-reserve-mib" ,"0" ,"--spec" ,"4" ],"args_written" :written } 44self .assertEqual (user_args .capture (cfg ), ["--vram-reserve-mib" ,"0" ]) 45self .assertEqual (user_args .capture ({** cfg ,"args" :written }), [])# nothing changed 46# the context is setup's own choice: a hand edit of it is not taken as an override 47self .assertEqual (user_args .capture ({"args" : ["--max-context" ,"1" ],"args_written" : ["--max-context" ,"2" ]}, 48setup .SETUP_CHOICE_FLAGS ), []) 49# an option taken out of "args" by hand is the person's no more 50self .assertEqual (user_args .capture ({"args" :written [:4 ],"args_written" :written ,"user_args" : ["--spec" ,"2" ]}), []) 51self .assertEqual (user_args .capture ({"args" :written ,"args_written" :written ,"user_args" : ["--spec" ,"2" ]}), ["--spec" ,"2" ])# user_args stands 52# no copy of what was written: only user_args is known 53self .assertEqual (user_args .capture ({"args" :CVEC ,"user_args" : ["--a" ,"1" ]}), ["--a" ,"1" ]) 54 55 56class SetupRerun (unittest .TestCase ): 57"""The app's Load runs setup again whenever the context, KV type or pictures differ from the installed ones.""" 58 59def setUp (self ): 60self .ram ,self .found = PROFILES ["64GB-1x32GB" ] 61 62def run_setup (self ,* extra ,configs = ()): 63return install (self .ram ,self .found , ["--family" ,"qwen" ,"--model" ,"Q2_0" ,"--no-start" ,* extra ],configs = configs ) 64 65def first (self ): 66code ,out ,first ,_ = self .run_setup ("--context" ,"65536" ) 67self .assertEqual (code ,0 ,out ) 68return first 69 70def test_3216_control_vector_arguments_survive_a_load (self ): 71first = self .first () 72old = {** first ,"args" :first ["args" ]+ CVEC ,"args_written" :first ["args" ]}# typed into the JSON editor 73code ,out ,cfg ,_ = self .run_setup ("--context" ,"131072" ,configs = [("strata-q2_0.json" ,old )]) 74self .assertEqual (code ,0 ,out ) 75a = cfg ["args" ] 76self .assertEqual (a [a .index ("--control-vector-scaled" ):][:9 ],CVEC ) 77self .assertEqual (value (a ,"--max-context" ),"131072" )# setup's own choice still applies 78self .assertEqual (cfg ["user_args" ],CVEC ) 79# the second Load: the options are in user_args, so they stay 80code ,out ,again ,_ = self .run_setup ("--context" ,"65536" ,configs = [("strata-q2_0.json" ,cfg )]) 81self .assertEqual (again ["user_args" ],CVEC ) 82self .assertEqual (value (again ["args" ],"--cvec-mode" ),"project" ) 83 84def test_3216_a_config_without_args_written_keeps_them_too (self ): 85first = self .first () 86old = {** first ,"args" :first ["args" ]+ CVEC }# 0.2.0-beta: no copy of what was written 87code ,out ,cfg ,_ = self .run_setup ("--context" ,"131072" ,configs = [("strata-q2_0.json" ,old )]) 88self .assertEqual (code ,0 ,out ) 89self .assertEqual (cfg ["user_args" ],CVEC ) 90self .assertEqual (cfg ["args" ][- len (CVEC ):],CVEC ) 91 92def test_3215_a_reserve_of_zero_is_kept (self ): 93first = self .first () 94written = list (first ["args" ]) 95edited = [* written ] 96edited [edited .index ("--vram-reserve-mib" )+ 1 ]= "0" 97for old in ({** first ,"args" :edited ,"args_written" :written },# with the copy of what was written 98 {** first ,"args" :edited }):# a 0.2.0-beta file 99code ,out ,cfg ,_ = self .run_setup ("--context" ,"131072" ,configs = [("strata-q2_0.json" ,old )]) 100self .assertEqual (code ,0 ,out ) 101self .assertEqual (value (cfg ["args" ],"--vram-reserve-mib" ),"0" ) 102self .assertEqual (cfg ["user_args" ], ["--vram-reserve-mib" ,"0" ]) 103 104def test_3270_a_lowered_reserve_is_not_reset (self ): 105first = self .first () 106written = list (first ["args" ]) 107for mib in ("1024" ,"256" ): 108edited = [* written ] 109edited [edited .index ("--vram-reserve-mib" )+ 1 ]= mib 110code ,out ,cfg ,_ = self .run_setup ("--context" ,"131072" ,configs = [("strata-q2_0.json" , {** first ,"args" :edited ,"args_written" :written })]) 111self .assertEqual (value (cfg ["args" ],"--vram-reserve-mib" ),mib ) 112 113def test_the_vision_default_is_not_taken_for_a_choice (self ): 114first = self .first ()# pictures on the GPU write 700 themselves 115self .assertEqual (value (first ["args" ],"--vram-reserve-mib" ),"700" ) 116code ,out ,cfg ,_ = self .run_setup ("--context" ,"131072" ,"--vision" ,"cpu" ,configs = [("strata-q2_0.json" ,first )]) 117self .assertNotIn ("user_args" ,cfg ) 118self .assertNotIn ("--vram-reserve-mib" ,cfg ["args" ])# the CPU reader needs no reserve 119 120def test_an_option_given_on_the_command_line_wins_and_is_kept (self ): 121first = self .first () 122old = {** first ,"args" :first ["args" ]+ ["--vram-reserve-mib" ,"0" ],"user_args" : ["--vram-reserve-mib" ,"0" ]} 123code ,out ,cfg ,_ = self .run_setup ("--context" ,"131072" ,"--vram-reserve-mib" ,"256" ,configs = [("strata-q2_0.json" ,old )]) 124self .assertEqual (code ,0 ,out ) 125self .assertEqual (cfg ["user_args" ], ["--vram-reserve-mib" ,"256" ]) 126self .assertEqual (value (cfg ["args" ],"--vram-reserve-mib" ),"256" ) 127 128def test_a_first_install_with_a_reserve_records_it (self ): 129code ,out ,cfg ,_ = self .run_setup ("--vram-reserve-mib" ,"300" ) 130self .assertEqual (cfg ["user_args" ], ["--vram-reserve-mib" ,"300" ]) 131 132def test_setup_writes_args_written_and_a_rerun_with_no_edit_makes_no_user_args (self ): 133first = self .first () 134code ,out ,cfg ,_ = self .run_setup ("--context" ,"131072" ,configs = [("strata-q2_0.json" , {** first ,"args_written" :first ["args" ]})]) 135self .assertNotIn ("user_args" ,cfg ) 136 137 138class Start (unittest .TestCase ): 139def test_every_start_puts_the_persons_options_over_the_generated_ones (self ): 140cfg = {"args" : ["--pack" ,"/p" ,"--vram-reserve-mib" ,"1536" ,"--spec" ,"4" ], 141"user_args" : ["--vram-reserve-mib" ,"0" ]+ CVEC } 142with contextlib .redirect_stdout (io .StringIO ()): 143args = engine_args (cfg ) 144self .assertEqual (value (args ,"--vram-reserve-mib" ),"0" ) 145self .assertEqual (args .count ("--vram-reserve-mib" ),1 ) 146self .assertEqual (args [args .index ("--control-vector-scaled" ):][:9 ],CVEC ) 147self .assertEqual (cfg ["args" ][3 ],"1536" )# the config in memory is untouched 148 149def test_the_settings_page_records_the_reserve_as_the_persons (self ): 150cfg = {"args" : ["--pack" ,"/p" ,"--vram-reserve-mib" ,"1536" ],"args_written" : ["--pack" ,"/p" ,"--vram-reserve-mib" ,"1536" ]} 151new ,changed = runconfig .apply (cfg , {"vram_reserve_mib" :0 }) 152self .assertEqual (changed , ["vram_reserve_mib" ]) 153self .assertEqual (new ["user_args" ], ["--vram-reserve-mib" ,"0" ]) 154self .assertEqual (new ["args_written" ],new ["args" ]) 155back ,_ = runconfig .apply (new , {"vram_reserve_mib" :None }) 156self .assertNotIn ("user_args" ,back ) 157self .assertNotIn ("--vram-reserve-mib" ,back ["args" ]) 158 159def test_a_tuning_replaces_only_the_flags_it_sets (self ): 160cfg = {"args" : ["--pack" ,"/p" ,"--vram-reserve-mib" ,"0" ,* CVEC ],"args_written" : ["--pack" ,"/p" ], 161"user_args" : ["--vram-reserve-mib" ,"0" ,* CVEC ]} 162out = tune_apply .apply_args_to_config (cfg , ["--vram-reserve-mib" ,"512" ,"--pcie-frac" ,"0.5" ]) 163self .assertEqual (value (out ["args" ],"--vram-reserve-mib" ),"512" ) 164self .assertEqual (out ["user_args" ],CVEC )# the tuned flag is no longer the old pin 165self .assertEqual (out ["args_written" ],out ["args" ]) 166 167 168class DisplayCards (unittest .TestCase ): 169"""The app keeps the safety reserve for a card that drives a display and less on one that does not (#3215).""" 170 171def test_nvidia_by_nvidia_smi (self ): 172gpus = [{"vendor" :"nvidia" ,"index" :0 }, {"vendor" :"nvidia" ,"index" :1 }, {"vendor" :"nvidia" ,"index" :2 }] 173strata_mcp .mark_display (gpus , {0 :False ,1 :True }, []) 174self .assertIs (gpus [0 ]["drives_display" ],False ) 175self .assertIs (gpus [1 ]["drives_display" ],True ) 176self .assertNotIn ("drives_display" ,gpus [2 ])# not told: unknown 177 178def test_amd_by_the_windows_adapter_with_a_desktop (self ): 179amd = {"vendor" :"amd" ,"index" :1 ,"pci_id" :0x744C } 180gpus = [{"vendor" :"amd" ,"index" :0 ,"pci_id" :0x164E },amd ] 181strata_mcp .mark_display (gpus , {}, ["PCI\\VEN_1002&DEV_164E&SUBSYS_1" ]) 182self .assertIs (gpus [0 ]["drives_display" ],True ) 183self .assertIs (amd ["drives_display" ],False ) 184twins = [{"vendor" :"amd" ,"pci_id" :0x744C }, {"vendor" :"amd" ,"pci_id" :0x744C }] 185strata_mcp .mark_display (twins , {}, ["PCI\\VEN_1002&DEV_744C" ]) 186self .assertNotIn ("drives_display" ,twins [0 ])# two of one model: not told apart 187strata_mcp .mark_display (gpus [:1 ]+ [{"vendor" :"amd" ,"pci_id" :0x1 }], {}, []) 188self .assertTrue (True ) 189 190 191if __name__ == "__main__" : 192unittest .main ()