okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
d5d5b0837
main
1"""setup.py and a feed model of its own shape (strata_control/shape_engine.py, strata_control/feed_models.py). 2 3python -m pytest tools/test_setup_shape.py 4 5A Qwen4 model whose geometry is not Flash-Next's runs on its own engine component. The model list puts it in the same tables as every feed model, with the shape; setup's engine 6step fetches that engine (not the Strata engine's) and the run config names it ("component:strata-q4-lite"); the Terminal's launch checks it in its first step. Every other model 7is exactly what it was. The engine fetch, downloads and processes are mocked; nothing is fetched from the network. 8""" 9from __future__import annotations 10 11import argparse 12import json 13import sys 14import unittest 15from pathlib import Path 16from unittest import mock 17 18ROOT = Path (__file__ ).resolve ().parents [1 ] 19sys .path .insert (0 ,str (ROOT )) 20sys .path .insert (0 ,str (ROOT / "tools" )) 21 22from strata_control import engine_component as ec ,feed_models ,shape_engine # noqa: E402 23import test_setup_feed_models as base # noqa: E402 24 25 26def shaped (** kw ): 27kw = {"id" :"q4lite-test" ,"family" :"qwen4exp_q4_lite" ,"drafter" :"none" ,"pictures" :False ,"mmproj" :None ,"min_engine" :"" ,** kw } 28return base .flash_next (** kw ) 29 30 31def clean (entry :dict )-> dict : 32entry .pop ("mmproj" ,None ) 33return entry 34 35 36class Registered (base .Base ): 37def setUp (self ): 38super ().setUp () 39self .result = feed_models .apply (self .setup ,base .parsed (clean (shaped ())), {"qwen4exp" })# the Strata engine runs qwen4exp only: not what decides 40self .fam = self .setup .FAMILIES ["q4lite-test" ] 41 42def test_it_is_listed_and_installable_though_the_strata_engine_does_not_list_its_family (self ): 43self .assertEqual ((self .result .registered ,self .result .unavailable ), (["q4lite-test" ], {})) 44self .assertEqual (self .fam ["shape" ],"q4_lite" ) 45self .assertTrue (self .fam ["feed" ]) 46self .assertNotIn ("q4lite-test" ,self .setup .FEED_UNAVAILABLE ) 47 48def test_its_min_engine_is_not_compared_with_the_strata_engine (self ): 49"""The app marks a size "needs a newer engine" when the Strata engine is older than the size's `engine`; a shape model runs on its own engine.""" 50feed_models .apply (self .setup ,base .parsed (clean (shaped (id = "q4lite-min" ,min_engine = "0.1.59" ))),None ) 51sizes = [m for m in self .setup .MODELS .values ()if "q4lite-min" in m .get ("families" , ())] 52self .assertTrue (sizes ) 53self .assertTrue (all ("engine" not in m for m in sizes )) 54feed_models .apply (self .setup ,base .parsed (base .flash_next (id = "plain-min" ,min_engine = "0.1.59" )),None ) 55self .assertTrue (all (m .get ("engine" )== (0 ,1 ,59 )for m in self .setup .MODELS .values ()if "plain-min" in m .get ("families" , ()))) 56 57def test_a_plain_qwen4_model_has_no_shape (self ): 58feed_models .apply (self .setup ,base .parsed (base .flash_next (id = "plain-one" ,title = "Plain One" )), {"qwen4exp" }) 59self .assertNotIn ("shape" ,self .setup .FAMILIES ["plain-one" ]) 60 61def test_it_is_offered_on_a_mac_like_the_two_file_sizes (self ): 62from strata_control import mac_fit 63self .assertIn ("q4lite-test" ,mac_fit .FEED_FAMILIES ) 64rows = {k :v for k ,v in mac_fit .MAC_MODELS .items ()if v .get ("family" )== "q4lite-test" } 65self .assertEqual (len (rows ),1 ) 66 (key ,row ),= rows .items () 67self .assertTrue (row ["no_mtp" ],"its entry says drafter none" ) 68self .assertEqual (mac_fit .app_sizes ("q4lite-test" ), [key ]) 69 70def test_the_mac_arguments_for_a_one_file_model_without_a_draft_layer (self ): 71from strata_control import mac_fit 72 (key ,)= [k for k ,v in mac_fit .MAC_MODELS .items ()if v .get ("family" )== "q4lite-test" ] 73shard = Path ("model.gguf" ) 74args = mac_fit .engine_args (Path ("pack" ), [shard ],Path ("rt" ),8192 ,False ,128.0 ,"profile.bin" ,key ) 75self .assertEqual (args [args .index ("--ple-gguf" )+ 1 ],"model.gguf" ,"one file: its own PLE table" ) 76self .assertNotIn ("--mtp" ,args ) 77plain = mac_fit .engine_args (Path ("pack" ), [Path ("a-1.gguf" ),Path ("a-2.gguf" )],Path ("rt" ),8192 ,False ,128.0 ,"profile.bin" ,"IQ3_S" ) 78self .assertEqual (plain [plain .index ("--ple-gguf" )+ 1 ],"a-2.gguf" ) 79self .assertIn ("--mtp" ,plain ,"a built-in model is exactly what it was" ) 80 81def test_the_same_list_without_an_engine_to_ask_gives_the_same_registration (self ): 82other = base .fresh_setup ("setup_other" ) 83feed_models .apply (other ,base .parsed (clean (shaped ())),None ) 84self .assertEqual (other .FAMILIES ["q4lite-test" ]["shape" ],"q4_lite" ) 85 86def test_a_family_older_controls_do_not_know_is_unavailable_for_them (self ): 87"""What the Control before this does with the same entry: its install table lacks the family, so the model is listed as unavailable and never offered.""" 88from strata_control import feed as feedmod 89self .assertNotIn ("qwen4exp_q4_lite" , ("qwen4exp" ,"qwen35moe" )) 90self .assertIsNone (feedmod .FeedModel .shape .fget (base .parsed (base .flash_next ())[0 ])) 91 92 93class EngineStep (base .Base ): 94def setUp (self ): 95super ().setUp () 96feed_models .apply (self .setup ,base .parsed (clean (shaped ())),None ) 97self .addCleanup (self .setup .SHAPE_ENGINE .clear ) 98 99def test_the_run_config_names_the_shape_engine_only_for_a_model_of_that_shape (self ): 100self .assertEqual (self .setup .engine_exe (Path ("engine" )),"component:strata-engine" if self .setup .CONTROL else str (Path ("engine" )/ self .setup .EXE )) 101self .setup .SHAPE_ENGINE ["shape" ]= "q4_lite" 102self .assertEqual (self .setup .engine_exe (Path ("engine" )),"component:strata-q4-lite" ) 103self .setup .SHAPE_ENGINE .clear () 104self .assertNotEqual (self .setup .engine_exe (Path ("engine" )),"component:strata-q4-lite" ) 105 106def test_engine_step_fetches_the_shape_engine_and_not_the_strata_engine (self ): 107self .setup .SHAPE_ENGINE ["shape" ]= "q4_lite" 108folder = self .root / "components" / "shapes" / "q4_lite" / "engine" / "0.1.59-linux-x86_64-cuda" 109folder .mkdir (parents = True ) 110found = mock .Mock (root = str (folder ),version = "0.1.59" ,backend = "cuda" ,platform = "linux-x86_64" ) 111with mock .patch .object (shape_engine ,"ensure" ,return_value = folder / "strata" )as ensure , \ 112mock .patch .object (shape_engine ,"find" ,return_value = found ), \ 113mock .patch .object (self .setup ,"llama_cpp_if_here" ,return_value = Path ("llama" )), \ 114mock .patch .object (self .setup ,"get_llama_cpp" ,return_value = Path ("llama" )), \ 115mock .patch .object (self .setup ,"control_engine_step" ,side_effect = AssertionError ("the Strata engine step must not run" )), \ 116mock .patch .object (ec ,"resolve" ,side_effect = AssertionError ("the Strata engine must not be resolved" )): 117llama ,eng ,meta ,lib_dirs ,gpu ,vision = self .setup .engine_step (argparse .Namespace (feed = "/some/feed" ), {"index" :0 },False ,False ,13 ,"none" ) 118self .assertEqual (ensure .call_args .args [:1 ], ("q4_lite" ,)) 119self .assertEqual (ensure .call_args .args [1 ],"/some/feed" ) 120self .assertEqual ((eng ,meta ["version" ],meta ["shape" ],lib_dirs ,vision ), (folder ,"0.1.59" ,"q4_lite" , [],"none" )) 121 122def test_the_lines_and_the_run_config_name_the_models_title (self ): 123"""A person reads the model's title, not the geometry's name; the run config keeps the title for the refusal at a start (strata_control/shape_engine.py USE_TITLE).""" 124self .setup .SHAPE_ENGINE .update (shape = "q4_lite" ,title = "Q4 Lite Shape" ) 125folder = self .root / "components" / "shapes" / "q4_lite" / "engine" / "0.1.59-linux-x86_64-cuda" 126folder .mkdir (parents = True ) 127found = mock .Mock (root = str (folder ),version = "0.1.59" ,backend = "cuda" ,platform = "linux-x86_64" ) 128said = [] 129with mock .patch .object (shape_engine ,"ensure" ,return_value = folder / "strata" )as ensure ,mock .patch .object (shape_engine ,"find" ,return_value = found ),mock .patch .object (self .setup ,"llama_cpp_if_here" ,return_value = Path ("llama" )),mock .patch .object (self .setup ,"get_llama_cpp" ,return_value = Path ("llama" )),mock .patch .object (self .setup ,"ok" ,side_effect = said .append ): 130self .setup .engine_step (argparse .Namespace (feed = None ), {"index" :0 },False ,False ,13 ,"none" ) 131self .assertEqual (ensure .call_args .kwargs ["title" ],"Q4 Lite Shape" ) 132self .assertTrue (any (line .startswith ("engine for Q4 Lite Shape: 0.1.59" )for line in said ),said ) 133self .assertFalse (any ("q4_lite" in line .split (" in " )[0 ]for line in said ),said ) 134cfg = {"exe" :"component:strata-q4-lite" ,"args" : [],"model_name" :"x" } 135target = self .root / "strata-q4lite.json" 136self .setup .write_setup_config (target ,cfg ) 137self .assertEqual (json .loads (target .read_text (encoding = "utf-8" ))["shape_title" ],"Q4 Lite Shape" ) 138plain = {"exe" :"component:strata-engine" ,"args" : [],"model_name" :"y" }# another model's config is exactly what it was 139self .setup .write_setup_config (self .root / "strata-other.json" ,plain ) 140self .assertNotIn ("shape_title" ,json .loads ((self .root / "strata-other.json" ).read_text (encoding = "utf-8" ))) 141 142def test_without_a_shape_the_strata_engine_step_runs_as_before (self ): 143self .assertEqual (self .setup .SHAPE_ENGINE , {}) 144with mock .patch .object (self .setup ,"control_engine_step" ,return_value = ("L" ,"E" , {}, [], {},"none" ))as step ,mock .patch .object (self .setup ,"CONTROL" ,True ): 145self .assertEqual (self .setup .engine_step (argparse .Namespace (), {},False ,False ,13 ,"none" )[1 ],"E" ) 146step .assert_called_once () 147 148def test_no_engine_for_the_shape_stops_before_the_model_is_downloaded (self ): 149self .setup .SHAPE_ENGINE ["shape" ]= "q4_lite" 150with mock .patch .object (shape_engine ,"ensure" ,return_value = None ), \ 151mock .patch .object (self .setup ,"llama_cpp_if_here" ,return_value = Path ("llama" )),mock .patch .object (self .setup ,"get_llama_cpp" ,return_value = Path ("llama" )), \ 152mock .patch .dict ("os.environ" , {"STRATA_ALLOW_NO_ENGINE" :"" }): 153with self .assertRaises (SystemExit ): 154self .setup .engine_step (argparse .Namespace (feed = None ), {"index" :0 },False ,False ,13 ,"none" ) 155 156def test_the_person_may_go_on_without_the_engine_and_set_it_up_later (self ): 157self .setup .SHAPE_ENGINE ["shape" ]= "q4_lite" 158with mock .patch .object (shape_engine ,"ensure" ,return_value = None ), \ 159mock .patch .object (self .setup ,"llama_cpp_if_here" ,return_value = Path ("llama" )),mock .patch .object (self .setup ,"get_llama_cpp" ,return_value = Path ("llama" )), \ 160mock .patch .dict ("os.environ" , {"STRATA_ALLOW_NO_ENGINE" :"1" }): 161out = self .setup .engine_step (argparse .Namespace (feed = None ), {"index" :0 },False ,False ,13 ,"none" ) 162self .assertEqual (out [2 ]["component" ],"missing" ) 163 164 165class Launch (unittest .TestCase ): 166"""The Terminal's first step reads the shape from setup's table and checks that engine, not the Strata engine's.""" 167 168def runner (self ,families ): 169from strata_cli .launch import runner as R 170fake = mock .Mock () 171fake .s .family = "q4lite-test" 172fake .catalog .strata .setup_module .return_value = mock .Mock (FAMILIES = families ) 173fake .multi_file .return_value = False 174return R ,fake 175 176def test_model_shape_reads_the_family_entry (self ): 177R ,fake = self .runner ({"q4lite-test" : {"shape" :"q4_lite" }}) 178self .assertEqual (R .Runner .model_shape (fake ),"q4_lite" ) 179R ,fake = self .runner ({"q4lite-test" : {}}) 180self .assertIsNone (R .Runner .model_shape (fake )) 181R ,fake = self .runner ({}) 182self .assertIsNone (R .Runner .model_shape (fake )) 183fake .catalog .strata .setup_module .side_effect = RuntimeError ("no table" ) 184self .assertIsNone (R .Runner .model_shape (fake )) 185 186def test_step_one_checks_the_shape_engine_and_never_the_strata_engine (self ): 187R ,fake = self .runner ({"q4lite-test" : {"shape" :"q4_lite" }}) 188fake .model_shape .return_value = "q4_lite" 189with mock .patch .object (ec ,"status" ,side_effect = AssertionError ("the Strata engine is not looked at" )): 190R .Runner .ensure_engine (fake ) 191fake .ensure_shape_engine .assert_called_once_with ("q4_lite" ) 192 193def test_a_missing_engine_is_the_launchs_no_engine_question (self ): 194R ,fake = self .runner ({}) 195fake .components_dir = None 196fake .feed = "/feed" 197with mock .patch .object (shape_engine ,"ensure" ,side_effect = lambda shape ,source ,** kw : (kw ["emit" ]("There is no engine for this model (q4_lite)." ),None )[1 ]): 198R .Runner .ensure_shape_engine (fake ,"q4_lite" ) 199fake .no_engine .assert_called_once_with ("There is no engine for this model (q4_lite)." ) 200 201def test_the_launch_lines_name_the_title (self ): 202R ,fake = self .runner ({"q4lite-test" : {"shape" :"q4_lite" ,"title" :"Q4 Lite Shape" }}) 203self .assertEqual (R .Runner .model_title (fake ),"Q4 Lite Shape" ) 204fake .model_title .return_value = "Q4 Lite Shape" 205fake .components_dir = None 206fake .feed = "/feed" 207events = [] 208fake .emit .side_effect = lambda stage ,text ,* a ,** k :events .append (text ) 209found = mock .Mock (version = "0.1.59" ,backend = "cuda" ) 210with mock .patch .object (shape_engine ,"ensure" ,return_value = Path ("/x/strata" ))as ensure ,mock .patch .object (shape_engine ,"find" ,return_value = found ): 211R .Runner .ensure_shape_engine (fake ,"q4_lite" ) 212self .assertEqual (ensure .call_args .kwargs ["title" ],"Q4 Lite Shape" ) 213self .assertIn ("Checking the engine for Q4 Lite Shape…" ,events ) 214self .assertIn ("Engine for Q4 Lite Shape 0.1.59 (cuda) is installed." ,events ) 215self .assertFalse ([e for e in events if "q4_lite" in e ],events ) 216 217 218if __name__ == "__main__" : 219unittest .main ()