okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1"""Owner test round 8 (7 machines), the Control / TUI / setup fixes: F5, F3, N2 to N9, N11.""" 2 3import argparse 4import io 5import json 6import os 7import sys 8import tempfile 9import unittest 10from contextlib import redirect_stdout 11from pathlib import Path 12from unittest import mock 13 14from strata_cli .launch import adopt ,library ,runner as R 15from strata_cli .launch import settings as S 16from strata_cli .launch .runner import Runner 17from tests .terminal .launch_support import HW_12 ,HW_2X16 ,Home ,catalog 18 19 20def runner_for (** kw )-> Runner : 21return Runner (S .LaunchSettings (family = "qwen" ,model = "IQ3_XXS" ,context = 32768 ,vision = "off" ,** kw ),catalog = catalog (HW_12 )) 22 23 24class EngineExitIsExplained (unittest .TestCase ): 25"""F5 (L3): an Arc engine died with 'No device of requested type available' and the person saw only 'server_exited'.""" 26 27def config_with_log (self ,lines ): 28folder = tempfile .TemporaryDirectory () 29self .addCleanup (folder .cleanup ) 30log = Path (folder .name )/ "engine.log" 31log .write_text ("\n" .join (lines )+ "\n" ,encoding = "utf-8" ) 32cfg = Path (folder .name )/ "run.json" 33cfg .write_text (json .dumps ({"log" :str (log )}),encoding = "utf-8" ) 34return cfg ,log 35 36def test_arc_without_the_render_group_names_the_cause_the_next_step_and_the_log (self ): 37cfg ,log = self .config_with_log (["strata generate: memory guard: limit 61.6 GiB" ,"strata generate: SYCL init" , 38"terminate called after throwing an instance of 'sycl::exception'" , 39" what(): No device of requested type available. -1 (PI_ERROR_DEVICE_NOT_FOUND)" ]) 40text ,nxt = R .explain_engine_exit ({"state" :"failed" ,"error" :"server_exited" ,"engine_exit_code" :134 },cfg ) 41self .assertIn ("stopped while it was loading the model" ,text ) 42self .assertIn ("No device of requested type available" ,text ) 43self .assertNotIn ("server_exited" ,text + nxt ) 44self .assertIn ("render group" ,nxt ) 45self .assertIn (str (log ),nxt ) 46 47def test_a_gpu_runtime_start_failure_gets_a_plain_sentence_and_keeps_the_runtime_text_out_of_it (self ): 48cfg ,log = self .config_with_log (["strata generate: loading" ,"terminate called after throwing an instance of 'rocr::AMD::hsa_exception'" , 49" what(): " ]) 50text ,nxt = R .explain_engine_exit ({"state" :"failed" ,"error" :"server_exited" ,"engine_exit_code" :134 },cfg ) 51self .assertIn ("stopped while it was loading the model (exit code 134)" ,text ) 52self .assertNotIn ("hsa_exception" ,text + nxt ) 53self .assertNotIn ("terminate called" ,text + nxt ) 54self .assertIn ("graphics card was not ready yet" ,nxt ) 55self .assertIn ("wait a few seconds" ,nxt ) 56self .assertIn ("restart the PC" ,nxt ) 57self .assertIn (str (log ),nxt ) 58text ,nxt = R .explain_engine_exit ({"error" :"server_exited" ,"engine_exit_code" :134 , 59"log_tail" :"terminate called after throwing an instance of 'rocr::AMD::hsa_exception' | what():" },None ) 60self .assertNotIn ("hsa_exception" ,text ) 61self .assertIn ("graphics card was not ready yet" ,nxt ) 62 63def test_a_real_cause_in_the_same_log_wins_over_the_gpu_runtime_start_failure (self ): 64cfg ,log = self .config_with_log (["strata generate: CUDA error: out of memory" , 65"terminate called after throwing an instance of 'rocr::AMD::hsa_exception'" ," what(): " ]) 66text ,nxt = R .explain_engine_exit ({"state" :"failed" ,"error" :"server_exited" ,"engine_exit_code" :134 },cfg ) 67self .assertIn ("out of memory" ,text ) 68self .assertIn ("--context 16k" ,nxt ) 69self .assertNotIn ("graphics card was not ready" ,nxt ) 70cfg ,_ = self .config_with_log (["Permission denied: /dev/dri/renderD128" ,"terminate called after throwing an instance of 'rocr::AMD::hsa_exception'" ]) 71self .assertIn ("render group" ,R .explain_engine_exit ({"error" :"server_exited" ,"engine_exit_code" :134 },cfg )[1 ]) 72cfg ,_ = self .config_with_log (["hsaKmtOpenKFD ok" ,"strata generate: CUDA error: out of memory" ]) 73self .assertNotIn ("graphics card was not ready" ,R .explain_engine_exit ({"error" :"server_exited" },cfg )[1 ]) 74 75def test_out_of_memory_and_old_engine_get_their_own_next_step (self ): 76cfg ,_ = self .config_with_log (["strata generate: CUDA error: out of memory" ]) 77self .assertIn ("--context 16k" ,R .explain_engine_exit ({"error" :"server_exited" },cfg )[1 ]) 78cfg ,_ = self .config_with_log (["strata generate: no kernel for TQK6 on this engine" ]) 79self .assertIn ("strata update" ,R .explain_engine_exit ({"error" :"server_exited" },cfg )[1 ]) 80 81def test_an_unreadable_log_still_says_the_stage_and_where_to_look (self ): 82text ,nxt = R .explain_engine_exit ({"state" :"failed" ,"error" :"server_exited" ,"log_tail" :"boom | Error: cannot map the pack" },None ) 83self .assertIn ("cannot map the pack" ,text ) 84self .assertIn ("strata logs" ,nxt ) 85text ,_ = R .explain_engine_exit ({"state" :"starting" ,"error" :"startup_wait_expired" },None ) 86self .assertIn ("did not finish loading in time" ,text ) 87 88 89class NoTuiNeverAsks (unittest .TestCase ): 90def test_no_tui_keeps_the_models_and_says_so_without_input (self ): 91with tempfile .TemporaryDirectory ()as old ,tempfile .TemporaryDirectory ()as new : 92 (Path (old )/ "models" ).mkdir () 93 (Path (old )/ "models" / "a.gguf" ).write_bytes (b"x" * 2048 ) 94from strata_cli .launch import cmd 95a = argparse .Namespace (data_dir = new ,move_data = False ,yes = False ,plan = False ,no_tui = True ) 96cat = mock .Mock () 97cat .data_dir .return_value = Path (old ) 98out = io .StringIO () 99with mock .patch .object (library ,"movable_library" ,return_value = 40.0 ),mock .patch ("builtins.input" ,side_effect = AssertionError ("asked" )), \ 100mock .patch .object (sys ,"stdin" )as stdin ,redirect_stdout (out ): 101stdin .isatty .return_value = True 102self .assertFalse (cmd ._move_data_choice (a ,S .LaunchSettings (),cat ,False )) 103self .assertIn ("Keeping your 40 GB of models where they are" ,out .getvalue ()) 104self .assertIn ("--move-data" ,out .getvalue ()) 105 106def test_a_run_without_a_keyboard_says_the_same (self ): 107from strata_cli .launch import cmd 108a = argparse .Namespace (data_dir = "/new" ,move_data = False ,yes = False ,plan = False ,no_tui = False ) 109out = io .StringIO () 110with mock .patch .object (library ,"movable_library" ,return_value = 3.0 ),mock .patch ("builtins.input" ,side_effect = AssertionError ("asked" )), \ 111mock .patch .object (sys ,"stdin" )as stdin ,redirect_stdout (out ): 112stdin .isatty .return_value = False 113self .assertFalse (cmd ._move_data_choice (a ,S .LaunchSettings (),mock .Mock (),False )) 114self .assertIn ("Keeping your 3.0 GB" ,out .getvalue ()) 115 116 117class SetupArguments (unittest .TestCase ): 118def installed (self ,h :Home ,** cfg ): 119path = h .base / "install" / "strata-iq3_xxs.json" 120path .parent .mkdir (parents = True ,exist_ok = True ) 121path .write_text (json .dumps ({"exe" :"x" ,"args" : [],** cfg }),encoding = "utf-8" ) 122return path 123 124def test_a_context_change_forwards_the_installed_roles (self ): 125# F3: 0=main,1=helper came back from a 32k to 16k change as 0=helper,1=main 126with Home ()as h : 127path = self .installed (h ,gpu = [1 ,0 ],gpus_asked = True ,gpu_roles = {"0" :"main" ,"1" :"helper" }) 128r = runner_for () 129with mock .patch .object (Runner ,"installed_state" ,return_value = (path ,None )): 130a = r .setup_args () 131self .assertEqual (a [a .index ("--gpu-roles" )+ 1 ],"0=main,1=helper" ) 132 133def test_installed_cards_are_forwarded_and_the_automatic_pick_is_not (self ): 134with Home ()as h : 135path = self .installed (h ,gpu = [1 ,0 ],gpus_asked = True ) 136with mock .patch .object (Runner ,"installed_state" ,return_value = (path ,None )): 137a = runner_for ().setup_args () 138self .assertEqual (a [a .index ("--gpus" )+ 1 ],"1,0" ) 139path = self .installed (h ,gpu = 0 )# never asked: setup picks again 140with mock .patch .object (Runner ,"installed_state" ,return_value = (path ,None )): 141a = runner_for ().setup_args () 142self .assertNotIn ("--gpus" ,a ) 143self .assertNotIn ("--gpu" ,a ) 144 145def test_cards_named_on_the_command_line_win_over_the_installed_ones (self ): 146with Home ()as h : 147path = self .installed (h ,gpu = [1 ,0 ],gpus_asked = True ,gpu_roles = {"0" :"main" ,"1" :"helper" }) 148with mock .patch .object (Runner ,"installed_state" ,return_value = (path ,None )): 149a = runner_for (gpus = [0 ]).setup_args () 150self .assertEqual (a [a .index ("--gpu" )+ 1 ],"0" ) 151self .assertNotIn ("--gpu-roles" ,a ) 152 153def test_a_remembered_single_card_is_said_when_the_pc_has_more (self ): 154# NVIDIA: after `--gpu 0` the next launch with nothing named stayed on one card without a word (boston, 2x A6000) 155base = {"exe" :"x" ,"args" : [],"gpu" :0 ,"gpus_asked" :True } 156with Home (): 157two = Runner (S .LaunchSettings (family = "qwen" ,model = "IQ3_XXS" ),catalog = catalog (HW_2X16 )) 158two .say_remembered_cards (base ) 159self .assertEqual (len (two .notices ),1 ) 160self .assertIn ("stays on card 0" ,two .notices [0 ]) 161self .assertIn ("strata launch --gpu 0,1" ,two .notices [0 ]) 162self .assertIn ("remembered" ,two .notices [0 ]) 163one = Runner (S .LaunchSettings (family = "qwen" ,model = "IQ3_XXS" ),catalog = catalog (HW_12 )) 164one .say_remembered_cards (base )# one card in the PC: nothing to say 165self .assertEqual (one .notices , []) 166both = Runner (S .LaunchSettings (family = "qwen" ,model = "IQ3_XXS" ),catalog = catalog (HW_2X16 )) 167both .say_remembered_cards ({** base ,"gpu" : [0 ,1 ]})# all cards are used: nothing to say 168both .say_remembered_cards ({** base ,"gpus_asked" :False })# setup's own pick is not a choice 169self .assertEqual (both .notices , []) 170named = Runner (S .LaunchSettings (family = "qwen" ,model = "IQ3_XXS" ,gpus = [0 ]),catalog = catalog (HW_2X16 )) 171named .say_remembered_cards (base )# named on this launch: the person knows 172self .assertEqual (named .notices , []) 173 174def test_mtp_off_reaches_setup (self ): 175with Home (): 176a = runner_for (mtp = "off" ).setup_args () 177self .assertEqual (a [a .index ("--mtp" )+ 1 ],"off" ) 178self .assertNotIn ("--mtp" ,runner_for ().setup_args ()) 179 180def test_setup_skips_the_draft_layer_download_for_mtp_off (self ): 181# setup.py is a script; its two fetch sites must be guarded by the explicit --mtp off (N3: a 5.2 GB download happened anyway) 182text = (Path (__file__ ).resolve ().parents [2 ]/ "setup.py" ).read_text (encoding = "utf-8" ) 183self .assertIn ('if a.mtp == "off" and not (rt / "experts.bin").exists():' ,text ) 184self .assertIn ('if use_mtp and a.mtp == "off" and not (mtp / "mtp-q2_0.gguf").exists():' ,text ) 185self .assertIn ('"use --data-dir on a bigger drive (strata launch --data-dir)" if CONTROL' ,text )# N2: a launch option, not --models-dir 186 187 188class PlanPredictsEveryDownload (unittest .TestCase ): 189def setUp (self ): 190from tests .terminal .launch_support import not_intel 191not_intel (self ) 192 193def plan_text (self ,h :Home ,own :Path ,** kw )-> dict : 194s = S .LaunchSettings (family = "qwen" ,model = "IQ3_XXS" ,context = 32768 ,vision = "on" ,model_path = str (own ),data_dir = str (h .base / "data" ),** kw ) 195r = Runner (s ,catalog = catalog (HW_12 )) 196with mock .patch .object (Runner ,"check_own_files" ),mock .patch .object (Runner ,"ready_pack_gap" ,return_value = None ): 197return r .download_plan () 198 199def test_a_local_gguf_without_a_reader_shows_the_picture_reader_and_the_switch (self ): 200with Home ()as h : 201own = h .base / "own" 202own .mkdir () 203 (own / "m-00001-of-00002.gguf" ).write_bytes (b"x" ) 204plan = self .plan_text (h ,own ) 205self .assertIn ("picture reader" ,plan ["text" ]) 206self .assertIn ("--vision off" ,plan ["text" ]) 207self .assertIn ("draft layer" ,plan ["text" ]) 208self .assertIn ("--mtp off" ,plan ["text" ]) 209 210def test_a_reader_beside_the_files_is_used_and_not_counted (self ): 211with Home ()as h : 212own = h .base / "own" 213own .mkdir () 214 (own / "m-00001-of-00002.gguf" ).write_bytes (b"x" ) 215 (own / "mmproj-Qwen3.8-Flash-Next-BF16.gguf" ).write_bytes (b"x" ) 216plan = self .plan_text (h ,own ) 217self .assertNotIn ("picture reader" ,plan ["text" ]) 218 219def test_mtp_off_is_not_counted (self ): 220with Home ()as h : 221own = h .base / "own" 222own .mkdir () 223 (own / "m-00001-of-00002.gguf" ).write_bytes (b"x" ) 224self .assertNotIn ("draft layer" ,self .plan_text (h ,own ,mtp = "off" )["text" ]) 225 226 227class ReadyPackPlan (unittest .TestCase ): 228def test_a_ready_made_pack_is_counted_and_the_hint_is_a_launch_option (self ): 229with Home ()as h : 230own = h .base / "own" 231own .mkdir () 232r = Runner (S .LaunchSettings (family = "qwen" ,model = "IQ3_XXS" ,context = 32768 ,vision = "off" ,model_path = str (own )),catalog = catalog (HW_12 )) 233gap = {"gb" :129.0 ,"folder" :str (h .base / "data" / "models" ),"free_gb" :110.0 } 234with mock .patch .object (Runner ,"ready_pack_gap" ,return_value = gap ): 235plan = r .download_plan () 236self .assertGreaterEqual (plan ["gb" ],129.0 ) 237self .assertIn ("ready-made pack" ,plan ["text" ]) 238self .assertIn ("--data-dir" ,plan ["text" ]) 239self .assertNotIn ("Nothing to download" ,plan ["text" ]) 240 241 242class EngineIsFreshEnough (unittest .TestCase ): 243def test_versions_compare_by_numbers (self ): 244self .assertEqual (R .version_tuple ("0.1.45.3" ), (0 ,1 ,45 ,3 )) 245self .assertEqual (R .version_tuple ("junk" ), ()) 246 247def test_an_old_engine_is_named_in_plain_words_and_a_current_one_is_not (self ): 248with Home (): 249r = runner_for () 250r .catalog .strata .setup_module ()# loads setup.py's tables (MIN_ENGINE) 251words = r .stale_engine ("0.1.45.3" ) 252self .assertIn ("older than this model needs" ,words ) 253self .assertIn ("0.1.45.3" ,words ) 254self .assertIsNone (r .stale_engine ("0.1.47" )) 255self .assertIsNone (r .stale_engine ("source" )) 256 257def test_gyro_on_a_mac_needs_engine_0_1_47 (self ): 258with Home (): 259r = Runner (S .LaunchSettings (family = "gyro" ,model = "Gyro-S" ,context = 32768 ,vision = "off" ),catalog = catalog (HW_12 )) 260with mock .patch .object (sys ,"platform" ,"darwin" ): 261self .assertEqual (r .needed_engine (), (0 ,1 ,47 )) 262self .assertIn ("older than this model needs (0.1.47" ,r .stale_engine ("0.1.45.3" )) 263self .assertIsNone (r .stale_engine ("0.1.47" )) 264with mock .patch .object (sys ,"platform" ,"linux" ): 265self .assertLess (r .needed_engine (), (0 ,1 ,47 ))# CUDA, HIP and SYCL had the kernels earlier 266self .assertIsNone (r .stale_engine ("0.1.46" )) 267with mock .patch .object (sys ,"platform" ,"darwin" ): 268q = Runner (S .LaunchSettings (family = "qwen" ,model = "IQ3_XXS" ,context = 32768 ,vision = "off" ),catalog = catalog (HW_12 )) 269self .assertLess (q .needed_engine (), (0 ,1 ,47 ))# only Gyro is held to it 270 271def test_a_stale_engine_is_updated_or_said_never_ignored (self ): 272with Home (): 273r = runner_for () 274events = [] 275r .emit = lambda stage ,text ,pct = None ,level = "info" ,** kw :events .append ((level ,text )) 276before = {"usable" :True ,"engine" : {"version" :"0.1.45.3" ,"backend" :"cuda" }} 277proc = mock .Mock () 278proc .stdout = iter ([]) 279proc .wait .return_value = 0 280from strata_control import engine_component as ec 281with mock .patch .object (ec ,"status" ,return_value = before ),mock .patch .object (Runner ,"_spawn" ,return_value = proc ): 282r .ensure_engine () 283self .assertTrue (any (level == "warn" and "older than this model needs" in text for level ,text in events ),events ) 284self .assertTrue (any ("strata update" in n for n in r .notices ),r .notices ) 285 286 287class InstallIsNotHijacked (unittest .TestCase ): 288def test_an_earlier_install_outside_the_chosen_data_folder_is_named (self ): 289with Home ()as h : 290cat = mock .Mock () 291cat .places = [mock .Mock (path = h .base / "old" / "Strata" ,source = "app" ,configs = 2 )] 292words = R .install_isolation_words (cat ,str (h .base / "scratch" )) 293self .assertEqual (len (words ),1 ) 294self .assertIn ("earlier install" ,words [0 ]) 295self .assertIn (str (h .base / "old" / "Strata" ),words [0 ]) 296cat .places = [mock .Mock (path = h .base / "scratch" / "install" ,source = "app" ,configs = 1 )] 297self .assertEqual (R .install_isolation_words (cat ,str (h .base / "scratch" )), []) 298cat .places = [mock .Mock (path = h .base / "old" ,source = "explicit" ,configs = 1 )] 299self .assertEqual (R .install_isolation_words (cat ,str (h .base / "scratch" )), []) 300self .assertEqual (R .install_isolation_words (cat ,None ), []) 301 302def test_an_install_inside_strata_data_dir_comes_first (self ): 303from strata_control import discovery 304with tempfile .TemporaryDirectory ()as data ,tempfile .TemporaryDirectory ()as other : 305for folder in (Path (data )/ "install" ,Path (other )/ "install" ): 306folder .mkdir () 307 (folder / "strata-q.json" ).write_text ("{}" ,encoding = "utf-8" ) 308env = {"STRATA_DATA_DIR" :data } 309found = discovery .places (environ = env ,here = Path (other )/ "install" ,home = Path (other )) 310self .assertEqual (Path (found [0 ].path ).resolve (), (Path (data )/ "install" ).resolve ()) 311 312 313class IdentityAndAdoption (unittest .TestCase ): 314def test_family_that_does_not_match_the_file_header_is_warned_about (self ): 315with Home ()as h : 316r = Runner (S .LaunchSettings (family = "swift" ,model = "IQ2_XS" ,model_path = str (h .base )),catalog = catalog (HW_12 )) 317plan = {"candidate_selection" : {"family" :"qwen" ,"model" :"IQ3_XXS" },"folder_holds" : [],"issues" : []} 318from strata_control import models 319with mock .patch .object (models ,"own_path_problem" ,return_value = None ),mock .patch .object (models ,"plan_import" ,return_value = plan ), \ 320mock .patch .object (models ,"import_target" ,return_value = str (h .base )): 321r .resolve_own_files () 322self .assertEqual (len (r .identity_notes ),1 ) 323self .assertIn ("look like qwen IQ3_XXS" ,r .identity_notes [0 ]) 324self .assertIn ("not swift IQ2_XS" ,r .identity_notes [0 ]) 325self .assertEqual ((r .s .family ,r .s .model ), ("swift" ,"IQ2_XS" ))# warned, never forced 326 327def test_gyro_is_adopted_like_the_others_and_the_text_has_no_double_period (self ): 328with Home ()as h : 329data = h .base / "data" 330model_dir = data / "models" / "gyro-Gyro-S" 331model_dir .mkdir (parents = True ) 332 (model_dir / "Qwen3.8-Flash-Next-Gyro-S-TQ1_0.gguf" ).write_bytes (b"x" ) 333pack = data / "packs" / "gyro-gyro-s" 334 (pack / "tokenizer" ).mkdir (parents = True ) 335 (pack / "index.txt" ).write_text ("x" ,encoding = "utf-8" ) 336cfg ,problems = adopt .plan (family = "gyro" ,model = "Gyro-S" ,model_dir = model_dir ,data_dir = data ,src = Path (__file__ ).resolve ().parents [2 ], 337context = 8192 ,kv = None ,model_name = "gyro" ,family_def = {},log = h .base / "x.log" ,backend = "sycl" , 338pack_name = "gyro-gyro-s" ) 339self .assertEqual (problems , [],problems ) 340self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--ple-gguf" )+ 1 ],cfg ["args" ][cfg ["args" ].index ("--native" )+ 1 ]) 341_ ,refused = adopt .plan (family = "nvfp4" ,model = "NVFP4" ,model_dir = model_dir ,data_dir = data ,src = Path ("." ),context = 8192 ,kv = None , 342model_name = "x" ,family_def = {},log = h .base / "x.log" ,backend = None ) 343self .assertTrue (refused [0 ].endswith ("not for nvfp4" ))# the caller adds the one full stop 344 345 346class UpdateCheckNamesTheChoice (unittest .TestCase ): 347def test_the_selected_package_is_printed_before_the_rejected_ones (self ): 348from strata_cli import update_cmd 349report = {"feed" : {"sequence" :107 ,"channel" :"stable" ,"key_id" :"k" ,"trusted_by" :"built-in keys" }, 350"engine" : {"status" :"install" ,"installed" :None ,"download_bytes" :5 << 20 , 351"offered" : {"version" :"0.1.47" ,"platform" :"linux-x86_64" ,"backend" :"cuda" ,"variant" :"cuda12" }, 352"reasons" : ["0.1.47 linux-x86_64-cuda-cuda13 needs driver 580" ],"note" :None }, 353"control" : {"status" :"current" ,"installed" :"0.1.47" ,"offered" :None },"app" : {"status" :"not-in-feed" }} 354out = io .StringIO () 355with redirect_stdout (out ): 356update_cmd ._print_check (report ) 357text = out .getvalue () 358self .assertIn ("selected for this PC: 0.1.47 linux-x86_64-cuda-cuda12" ,text ) 359self .assertLess (text .index ("selected for this PC" ),text .index ("not used for this PC" )) 360 361 362if __name__ == "__main__" : 363unittest .main ()