okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1"""ARCFIX (0.2.13): the app's model setup on an Intel Arc card (Linux, the SYCL engine). 2 3Found by QAGUI-LIN on an Arc A750: the app's install job ran setup.py with no `--backend`, so setup's NVIDIA / AMD path stopped with "no NVIDIA GPU found 4(nvidia-smi did not answer)". tools/strata_mcp.py now picks `sycl` for an Intel PC and passes `--backend sycl`; setup.py's CONTROL mode answers that with the 5Terminal's own two steps (the pack, then the run config of strata_cli/launch/adopt.py). NVIDIA and AMD are what they were. Offline: nothing runs but Python.""" 6 7from __future__import annotations 8 9import json 10import sys 11import tempfile 12import unittest 13from pathlib import Path 14from unittest import mock 15 16ROOT = Path (__file__ ).resolve ().parents [2 ] 17sys .path .insert (0 ,str (ROOT )) 18sys .path .insert (0 ,str (ROOT / "tools" )) 19import setup # noqa: E402 20import strata_mcp as M # noqa: E402 21from strata_control import intel_app # noqa: E402 22 23A750 = {"vendor" :"intel" ,"name" :"Intel Arc A750" ,"vram_gb" :8.0 ,"usable" :True ,"index" :0 } 24NVIDIA = {"vendor" :"nvidia" ,"name" :"NVIDIA GeForce RTX 5070" ,"vram_gb" :11.9 ,"usable" :True ,"index" :0 ,"arch" :"120" } 25AMD = {"vendor" :"amd" ,"name" :"AMD Radeon RX 9070 XT" ,"vram_gb" :15.9 ,"usable" :True ,"index" :0 ,"arch" :"gfx1201" } 26AMD_IGPU = {"vendor" :"amd" ,"name" :"AMD Radeon Graphics" ,"vram_gb" :0.5 ,"usable" :False ,"index" :0 ,"arch" :"gfx1036" ,"uma" :True } 27 28 29def shards (folder :Path ,model :str = "Q2_0" ,family :str = "qwen" )-> list [Path ]: 30fam = setup .FAMILIES [family ] 31folder .mkdir (parents = True ,exist_ok = True ) 32out = [folder / setup .model_file (fam ,model ,i )for i in range (1 ,setup .model_shards (fam ,model )+ 1 )] 33for p in out : 34p .write_bytes (b"x" ) 35return out 36 37 38class CardList (unittest .TestCase ): 39def add (self ,nvidia ,amd ,* ,platform = "linux" ,cards = (A750 ,),intel = True ): 40return intel_app .cards_to_add (nvidia ,amd ,platform = platform ,cards = lambda : [dict (c )for c in cards ],intel_engine = lambda :intel ) 41 42def test_an_arc_only_pc_gets_its_card (self ): 43self .assertEqual ([c ["name" ]for c in self .add ([], [])], ["Intel Arc A750" ]) 44self .assertEqual ([c ["name" ]for c in self .add ([], [AMD_IGPU ])], ["Intel Arc A750" ],"an integrated Radeon that is not usable does not hide the Arc" ) 45 46def test_nvidia_and_amd_pcs_are_what_they_were (self ): 47self .assertEqual (self .add ([NVIDIA ], []), []) 48self .assertEqual (self .add ([], [AMD ]), []) 49self .assertEqual (self .add ([NVIDIA ], [AMD ]), []) 50 51def test_only_linux_and_only_when_the_engine_is_the_intel_one (self ): 52self .assertEqual (self .add ([], [],platform = "win32" ), [])# no Windows Intel engine in the feed 53self .assertEqual (self .add ([], [],platform = "darwin" ), []) 54self .assertEqual (self .add ([], [],intel = False ), [])# the engine Control would pick is not SYCL (STRATA_ENGINE_BACKEND, a saved choice) 55self .assertEqual (self .add ([], [],cards = ()), []) 56 57def test_a_probe_that_fails_adds_nothing (self ): 58def boom (): 59raise OSError ("no sysfs" ) 60self .assertEqual (intel_app .cards_to_add ([], [],platform = "linux" ,cards = boom ,intel_engine = lambda :True ), []) 61self .assertEqual (intel_app .cards_to_add ([], [],platform = "linux" ,cards = lambda : [A750 ],intel_engine = boom ), []) 62 63def test_the_hardware_report_carries_the_arc_with_its_own_id (self ): 64s = M .Strata (ROOT ) 65S = s .setup_module () 66with mock .patch .object (M .sys ,"platform" ,"linux" ),mock .patch .object (M ,"WIN" ,False ), \ 67mock .patch .object (S ,"gpus" ,lambda : []),mock .patch .object (S ,"amd_gpus" ,lambda : []), \ 68mock .patch .object (M ,"rocm_smi_gpus" ,lambda : []),mock .patch .object (M ,"kfd_gpus" ,lambda : []), \ 69mock .patch .object (intel_app ,"arc_cards" ,lambda root = "" : [dict (A750 )]),mock .patch .object (intel_app ,"engine_is_intel" ,lambda :True ), \ 70mock .patch ("strata_control.engine_component.status" ,lambda : {"usable" :False }): 71hw = s ._hardware () 72self .assertEqual ([(g ["vendor" ],g ["name" ],g ["vram_gb" ],g ["id" ])for g in hw ["gpus" ]], [("intel" ,"Intel Arc A750" ,8.0 ,"intel:0" )]) 73self .assertEqual (hw ["engine_vendor" ],"intel" ) 74self .assertEqual ([g ["name" ]for g in s .engine_gpus (hw )], ["Intel Arc A750" ]) 75 76 77class Recommendation (unittest .TestCase ): 78def test_an_arc_is_a_sycl_pc_and_says_there_is_no_download (self ): 79s = M .Strata (ROOT ) 80rec = s .recommend ({"ram_gb" :64.0 ,"gpus" : [dict (A750 )],"engine_vendor" :"intel" ,"cpu" : {"avx2" :True }}) 81self .assertEqual (rec ["backend" ],"sycl" ) 82self .assertIn (intel_app .NO_DOWNLOAD ,rec ["notes" ]) 83self .assertTrue (rec ["setup_command" ].endswith (" --backend sycl" )) 84 85def test_nvidia_and_amd_recommendations_are_unchanged (self ): 86s = M .Strata (ROOT ) 87rec = s .recommend ({"ram_gb" :64.0 ,"gpus" : [dict (NVIDIA )],"cpu" : {"avx2" :True }}) 88self .assertEqual (rec ["backend" ],"cuda" ) 89self .assertNotIn ("--backend" ,rec ["setup_command" ]) 90self .assertNotIn (intel_app .NO_DOWNLOAD ,rec ["notes" ]) 91rec = s .recommend ({"ram_gb" :64.0 ,"gpus" : [dict (AMD )],"cpu" : {"avx2" :True }}) 92self .assertEqual (rec ["backend" ],"hip" ) 93self .assertTrue (rec ["setup_command" ].endswith (" --backend hip" )) 94 95 96class InstallPlan (unittest .TestCase ): 97def plan (self ,gpus ,* ,backend = None ,family = "qwen" ,model = "Q2_0" ,gguf_dir = None ,data_dir = None ,drafter = "dflash2" ,vision = "no" ,engine_vendor = None ): 98s = M .Strata (ROOT ) 99hw = {"ram_gb" :64.0 ,"gpus" : [dict (g )for g in gpus ],"cpu" : {"avx2" :True },** ({"engine_vendor" :engine_vendor }if engine_vendor else {})} 100s .hardware = lambda fresh = False :hw 101s .disk_free = lambda p :1000.0 102with mock .patch .object (M .sys ,"platform" ,"linux" ),mock .patch .object (M ,"WIN" ,False ): 103return M .Tools (s ).install_plan (family ,model ,32768 ,vision ,"int8" ,None ,None ,backend ,None ,None ,data_dir ,gguf_dir ,drafter ) 104 105def test_the_apps_request_on_an_arc_passes_backend_sycl (self ): 106"""The exact request QAGUI-LIN saw the app make (own folder, 32K, int8, DFlash2): now with --backend sycl and without the drafter.""" 107with tempfile .TemporaryDirectory ()as t : 108folder = Path (t )/ "Q2_0" 109shards (folder ) 110plan ,args = self .plan ([A750 ],gguf_dir = str (folder ),engine_vendor = "intel" ) 111self .assertEqual (args , ["--yes" ,"--no-start" ,"--family" ,"qwen" ,"--model" ,"Q2_0" ,"--context" ,"32768" ,"--vision" ,"no" , 112"--experimental-speed-projection" ,"off" ,"--kv" ,"int8" ,"--gguf-dir" ,str (folder ),"--backend" ,"sycl" ]) 113self .assertTrue (plan ["partly_downloaded" ]) 114 115def test_pictures_are_off_on_an_arc_even_when_asked_for (self ): 116with tempfile .TemporaryDirectory ()as t : 117folder = Path (t )/ "Q2_0" 118shards (folder ) 119_plan ,args = self .plan ([A750 ],gguf_dir = str (folder ),vision = "yes" ) 120self .assertEqual (args [args .index ("--vision" )+ 1 ],"no" ) 121 122def test_the_backend_can_be_named (self ): 123with tempfile .TemporaryDirectory ()as t : 124folder = Path (t )/ "Q2_0" 125shards (folder ) 126_plan ,args = self .plan ([A750 ],backend = "sycl" ,gguf_dir = str (folder )) 127self .assertEqual (args [- 2 :], ["--backend" ,"sycl" ]) 128with self .assertRaises (M .ToolError )as raised :# sycl on a PC with no Arc card 129self .plan ([NVIDIA ],backend = "sycl" ) 130self .assertIn ("no Intel Arc card" ,str (raised .exception )) 131 132def test_without_the_files_there_is_no_download_and_the_words_say_so (self ): 133with tempfile .TemporaryDirectory ()as t : 134data = Path (t )/ "Strata-data" 135data .mkdir () 136with self .assertRaises (M .ToolError )as raised : 137self .plan ([A750 ],data_dir = str (data )) 138self .assertEqual (str (raised .exception ),intel_app .NO_DOWNLOAD ) 139shards (data / "models" / "Q2_0" )# the files are where the Terminal looks (<data>/models/<tag>) 140plan ,args = self .plan ([A750 ],data_dir = str (data )) 141self .assertEqual (args [- 2 :], ["--backend" ,"sycl" ]) 142self .assertNotIn ("--gguf-dir" ,args ) 143self .assertTrue (plan ["partly_downloaded" ]) 144 145def test_nvidia_and_amd_requests_are_byte_identical_to_before (self ): 146with tempfile .TemporaryDirectory ()as t : 147folder = Path (t )/ "Q2_0" 148shards (folder ) 149_plan ,nvidia = self .plan ([NVIDIA ],gguf_dir = str (folder )) 150_plan ,amd = self .plan ([AMD ],gguf_dir = str (folder )) 151_plan ,amd_named = self .plan ([AMD ],backend = "hip" ,gguf_dir = str (folder )) 152_plan ,cuda_named = self .plan ([NVIDIA ],backend = "cuda" ,gguf_dir = str (folder )) 153base = ["--yes" ,"--no-start" ,"--family" ,"qwen" ,"--model" ,"Q2_0" ,"--context" ,"32768" ,"--vision" ,"no" , 154"--experimental-speed-projection" ,"off" ,"--kv" ,"int8" ,"--gguf-dir" ,str (folder )] 155self .assertEqual (nvidia ,base + ["--drafter" ,"dflash2" ]) 156self .assertEqual (amd ,base + ["--drafter" ,"dflash2" ]) 157self .assertEqual (amd_named ,base + ["--backend" ,"hip" ,"--drafter" ,"dflash2" ]) 158self .assertEqual (cuda_named ,base + ["--backend" ,"cuda" ,"--drafter" ,"dflash2" ]) 159 160def test_an_arc_beside_an_nvidia_card_is_an_nvidia_install (self ): 161"""The card list only holds an Arc when no usable NVIDIA / AMD card is there, so a mixed PC takes the old path.""" 162with tempfile .TemporaryDirectory ()as t : 163folder = Path (t )/ "Q2_0" 164shards (folder ) 165_plan ,args = self .plan ([NVIDIA ],gguf_dir = str (folder )) 166self .assertNotIn ("sycl" ,args ) 167 168def test_the_tool_schema_offers_sycl (self ): 169s = M .Strata (ROOT ) 170tools = {t ["name" ]:t for t in M .Tools (s ).definitions ()}if hasattr (M .Tools ,"definitions" )else {} 171if tools : 172self .assertEqual (tools ["strata_install" ]["inputSchema" ]["properties" ]["backend" ]["enum" ], ["auto" ,"cuda" ,"hip" ,"sycl" ]) 173else : 174self .assertIn ('"enum": ["auto", "cuda", "hip", "sycl"]' , (ROOT / "tools" / "strata_mcp.py" ).read_text (encoding = "utf-8" )) 175 176 177class SetupControlMode (unittest .TestCase ): 178def args (self ,folder ,** kw ): 179base = dict (family = "qwen" ,model = "Q2_0" ,gguf_dir = str (folder ),models_dir = None ,data_dir = None ,context = 32768 ,kv = "int8" ,port = None ,host = None , 180api_key = None ,browser = None ,check = False ,yes = True ,move_data = False ,set_default_data_dir = False ,prepare_pack = None ) 181base .update (kw ) 182return mock .Mock (** base ) 183 184def run_setup (self ,a ,data ,home ,* ,driver = "xe" ,prepared = True ): 185def fake_pack (args ): 186pack = Path (args .prepare_pack ) 187 (pack / "tokenizer" ).mkdir (parents = True ,exist_ok = True ) 188 (pack / "tokenizer" / "vocab.json" ).write_text ("{}" ) 189 (pack / "index.txt" ).write_text ("x" ) 190self .packed = args .prepare_pack 191return 0 192self .packed = None 193with mock .patch .object (setup ,"CONTROL" ,True ),mock .patch .object (setup ,"_CONTROL_HOME" ,home ), \ 194mock .patch .object (setup ,"data_folder_for" ,lambda a : (data , [])), \ 195mock .patch .object (setup ,"prepare_pack_only" ,fake_pack ), \ 196mock .patch .object (intel_app ,"arc_cards" ,lambda root = "" : [dict (A750 )]), \ 197mock .patch ("strata_cli.launch.adopt.intel_driver" ,lambda :driver ), \ 198mock .patch .object (intel_app ,"supported_here" ,lambda platform = None :True ): 199return setup .intel_control_setup (a ) 200 201def test_the_pack_and_the_run_config_are_written_as_the_terminal_writes_them (self ): 202with tempfile .TemporaryDirectory ()as t : 203t = Path (t ) 204folder ,data ,home = t / "Q2_0" ,t / "Strata-data" ,t / "install" 205files = shards (folder ) 206home .mkdir () 207data .mkdir () 208self .assertEqual (self .run_setup (self .args (folder ),data ,home ,driver = "i915" ),0 ) 209self .assertEqual (self .packed ,str (data / "packs" / "q2_0" )) 210cfg = json .loads ((home / "strata-q2_0.json" ).read_text (encoding = "utf-8" )) 211self .assertEqual ((cfg ["exe" ],cfg ["backend" ],cfg ["port" ]), ("component:strata-engine" ,"sycl" ,8080 )) 212a = cfg ["args" ] 213self .assertEqual (a [a .index ("--pack" )+ 1 ],str (data / "packs" / "q2_0" )) 214self .assertEqual (a [a .index ("--native" )+ 1 ],str (files [0 ])) 215self .assertEqual (a [a .index ("--ple-gguf" )+ 1 ],str (files [- 1 ])) 216self .assertEqual (a [a .index ("--ple-io" )+ 1 ],"ram" )# the A750 runs on i915 217self .assertEqual (a [a .index ("--kv" )+ 1 ],"int8" ) 218self .assertNotIn ("--vision" ,a ) 219self .assertEqual (cfg ["tokenizer" ],str (data / "packs" / "q2_0" / "tokenizer" )) 220 221def test_a_b_series_card_streams_its_experts (self ): 222with tempfile .TemporaryDirectory ()as t : 223t = Path (t ) 224folder ,data ,home = t / "Q2_0" ,t / "Strata-data" ,t / "install" 225shards (folder ) 226home .mkdir () 227data .mkdir () 228self .run_setup (self .args (folder ,port = 8123 ),data ,home ,driver = "xe" ) 229cfg = json .loads ((home / "strata-q2_0.json" ).read_text (encoding = "utf-8" )) 230self .assertIn ("--stream-experts" ,cfg ["args" ]) 231self .assertEqual (cfg ["port" ],8123 ) 232 233def test_no_files_no_download (self ): 234with tempfile .TemporaryDirectory ()as t : 235t = Path (t ) 236 (t / "empty" ).mkdir () 237home = t / "install" 238home .mkdir () 239with self .assertRaises (SystemExit ): 240self .run_setup (self .args (t / "empty" ),t / "Strata-data" ,home ) 241self .assertIsNone (self .packed ) 242self .assertEqual (list (home .glob ("strata-*.json" )), []) 243 244def test_the_dispatch_in_main_is_by_mode (self ): 245"""CONTROL mode (the app, the Terminal's installer mode) prepares and adopts; a checkout keeps the from-source Intel setup.""" 246for control ,expect in ((True ,"intel_control_setup" ), (False ,"sycl_setup" )): 247with mock .patch .object (setup ,"CONTROL" ,control ),mock .patch .object (setup ,"intel_control_setup" ,return_value = 11 )as ctl , \ 248mock .patch .object (setup ,"sycl_setup" ,return_value = 22 )as old ,mock .patch .object (sys ,"argv" , ["setup.py" ,"--backend" ,"sycl" ,"--yes" ]): 249self .assertEqual (setup .main (),11 if control else 22 ) 250self .assertEqual ((ctl .called ,old .called ), (control ,not control ),expect ) 251 252 253if __name__ == "__main__" : 254unittest .main ()