okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
fdfca1d9d
main
1"""A PC with an NVIDIA card and an AMD card (the lent boxia: RTX 3080 + RX 6700 XT gfx1031, 11-15). 2 3Before: `strata update --backend amd` said "The feed has no engine package for this PC" (the word amd was never turned into the hip family, and the 4hardware the choice read stayed the NVIDIA card's), `strata components engine --backend amd` exited 5 ("this PC needs amd (requested)"), and 5`strata components engines` marked the installed hip-rx6000 engine "not for this computer" although the PC has the card it is for. 6 7Proves, with a faked two-card detection: amd / hip / hip-rx6000 / nvidia name their family; --backend amd reads the AMD card (gfx1031) and picks 8the rx6000 package from a feed that also has the CUDA, rx9070, rx7900 and halo builds; resolve / status accept the make's word; the engines list marks 9the AMD engine as one this computer can run (for its other card) and keeps "newest" on the engine of the make in use; switching to it keeps the 10AMD choice the way `strata launch --backend amd` does.""" 11import argparse 12import io 13import json 14import shutil 15import unittest 16from contextlib import redirect_stdout 17from unittest import mock 18 19from strata_cli import update_cmd 20from strata_control import engine_component as ec ,engine_library as lib ,feed as feedmod ,updater 21from tests .terminal .test_updater import PLATFORM ,UpdaterCase 22 23 24def _detect_backend (environ = None ): 25forced = ec .split_backend ((environ or {}).get (ec .ENV_BACKEND ,"" ))[0 ] 26if forced : 27return forced ,"environment" 28return "cuda" ,"nvidia-smi" # the NVIDIA card answers first, as on boxia 29 30 31def _pkg (backend ,variant ,archs ): 32return feedmod .EnginePackage (version = "0.1.60" ,platform = "linux-x86_64" ,backend = backend ,variant = variant ,archs = tuple (archs ),min_driver = {}, 33min_runtime = None ,build_id = "x" ,url = f"strata-engine-0.1.60-linux-x86_64- { backend } - { variant } .tar.gz" ,format = "tar.gz" , 34size = 1 ,sha256 = "0" * 64 ,manifest_sha256 = "0" * 64 ,installed_bytes = 1 ) 35 36 37FEED = [_pkg ("cuda" ,"cuda13" , ("sm_86" ,"sm_120" )),_pkg ("hip" ,"rx6000" , ("gfx1030" ,"gfx1031" ,"gfx1032" )), 38_pkg ("hip" ,"rx9070" , ("gfx1200" ,"gfx1201" )),_pkg ("hip" ,"rx7900" , ("gfx1100" ,"gfx1101" ,"gfx1102" )), 39_pkg ("hip" ,"halo" , ("gfx1151" ,))] 40 41 42class MixedPcHardware (unittest .TestCase ): 43def setUp (self ): 44for target ,name ,value in ((ec ,"detect_backend" ,None ), (ec ,"gpu_makes_present" , {"cuda" ,"hip" }), (updater ,"host_platform" ,"linux-x86_64" ), 45 (updater ,"_gfx_names" , ["gfx1031" ]), (updater ,"_amd_card_names" , []), (ec ,"runtime_version" ,None ), 46 (ec ,"nvidia_driver_version" ,"580.95" ), (ec ,"cuda_archs_present" , ["sm_86" ])): 47patch = mock .patch .object (target ,name ,_detect_backend )if value is None else mock .patch .object (target ,name ,return_value = value ) 48patch .start () 49self .addCleanup (patch .stop ) 50 51def test_the_make_words_name_their_family (self ): 52self .assertEqual (ec .split_backend ("amd" ), ("hip" ,"" )) 53self .assertEqual (ec .split_backend (" AMD " ), ("hip" ,"" )) 54self .assertEqual (ec .split_backend ("nvidia" ), ("cuda" ,"" )) 55self .assertEqual (ec .split_backend ("intel" ), ("sycl" ,"" )) 56self .assertEqual (ec .split_backend ("hip-rx6000" ), ("hip" ,"rx6000" )) 57self .assertEqual (ec .split_backend ("cuda" ), ("cuda" ,"" )) 58 59def test_without_a_choice_the_nvidia_card_decides_as_before (self ): 60hw = updater .detect_hardware ({}) 61self .assertEqual ((hw ["backend" ],hw ["gfx" ],hw ["cuda_archs" ]), ("cuda" , [], ["sm_86" ])) 62pick ,_ = feedmod .select_engine (FEED ,hw ) 63self .assertEqual ((pick .backend ,pick .variant ), ("cuda" ,"cuda13" )) 64 65def test_backend_amd_reads_the_amd_card_and_picks_the_rx6000_package (self ): 66hw = update_cmd .requested_hardware ("amd" ,environ = {}) 67self .assertEqual ((hw ["backend" ],hw ["gfx" ],hw ["backend_source" ]), ("hip" , ["gfx1031" ],"requested" )) 68pick ,reasons = feedmod .select_engine (FEED ,hw ) 69self .assertIsNotNone (pick ,reasons ) 70self .assertEqual ((pick .backend ,pick .variant ), ("hip" ,"rx6000" )) 71 72def test_backend_hip_and_the_package_name_pick_the_same (self ): 73for word in ("hip" ,"hip-rx6000" ,"HIP" ): 74hw = update_cmd .requested_hardware (word ,environ = {}) 75pick ,reasons = feedmod .select_engine (FEED ,hw ) 76self .assertEqual ((pick .backend ,pick .variant ), ("hip" ,"rx6000" ), (word ,reasons )) 77self .assertEqual (update_cmd .requested_hardware ("hip-rx6000" ,environ = {})["variant_wanted" ],"rx6000" ) 78 79def test_backend_nvidia_stays_on_the_nvidia_card (self ): 80hw = update_cmd .requested_hardware ("nvidia" ,environ = {}) 81self .assertEqual ((hw ["backend" ],hw ["cuda_archs" ],hw ["gfx" ]), ("cuda" , ["sm_86" ], [])) 82 83def test_a_word_that_names_no_family_is_still_answered_by_the_feed (self ): 84hw = update_cmd .requested_hardware ("vulkan" ,environ = {}) 85pick ,reasons = feedmod .select_engine (FEED ,hw ) 86self .assertIsNone (pick ) 87self .assertTrue (any ("needs vulkan" in r for r in reasons )) 88 89def test_the_make_guard_lets_the_named_family_through (self ): 90ctx = mock .Mock (hardware = update_cmd .requested_hardware ("amd" ,environ = {})) 91self .assertIsNone (updater .make_guard (ctx )) 92 93 94class MixedPcEngines (UpdaterCase ): 95"""The installed engines on the two-card PC: the CUDA engine the PC picked first, and the hip-rx6000 one installed with --backend amd.""" 96 97def setUp (self ): 98super ().setUp () 99cuda = self .install_current ("0.1.60" ) 100# the hip-rx6000 package: the stub CUDA package with an AMD manifest (the packager wants a real AMD stage, its hipBLASLt version) 101self .amd = cuda .parent / f"0.1.60- { PLATFORM } -hip-rx6000" 102shutil .copytree (cuda ,self .amd ) 103manifest = json .loads ((self .amd / ec .MANIFEST ).read_text ()) 104manifest .update (backend = "hip" ,variant = "rx6000" ,archs = ["gfx1030" ,"gfx1031" ,"gfx1032" ],min_driver = {}) 105 (self .amd / ec .MANIFEST ).write_text (json .dumps (manifest )) 106for target ,name ,value in ((lib ,"_process_uses" ,False ), (ec ,"hip_archs_present" , ["gfx1031" ]), (ec ,"gpu_makes_present" , {"cuda" ,"hip" })): 107patch = mock .patch .object (target ,name ,return_value = value ) 108patch .start () 109self .addCleanup (patch .stop ) 110self .settings = self .root / "settings" 111patch = mock .patch ("strata_control.discovery.own_settings_file" ,return_value = self .settings / "settings.json" ) 112patch .start () 113self .addCleanup (patch .stop ) 114 115def rows (self ,hardware ): 116return {e ["folder" ]:e for e in lib .rows_of (lib .list_engines (self .ctx (hardware = hardware )),lib .QWEN )} 117 118def test_the_amd_engine_is_one_this_computer_can_run (self ): 119rows = self .rows ({** self .hardware ,"makes" : ["cuda" ,"hip" ]}) 120amd = rows [self .amd .name ] 121self .assertTrue (amd ["fits" ]and amd ["other_card" ]and amd ["why_not" ]is None ,amd ) 122self .assertFalse (amd ["active" ]or amd ["newest" ],"the newest is the one of the make a start uses" ) 123cuda = next (r for r in rows .values ()if r ["backend" ]== "cuda" ) 124self .assertTrue (cuda ["active" ]and cuda ["newest" ]and not cuda ["other_card" ]) 125 126def test_on_a_pc_without_an_amd_card_it_stays_for_a_different_kind_of_card (self ): 127rows = self .rows ({** self .hardware ,"makes" : ["cuda" ]}) 128amd = rows [self .amd .name ] 129self .assertFalse (amd ["fits" ]) 130self .assertEqual (amd ["why_code" ],"gpu_make" ) 131 132def test_with_backend_amd_the_amd_engine_is_the_one_in_use (self ): 133rows = self .rows ({** self .hardware ,"backend" :"hip" ,"gfx" : ["gfx1031" ],"makes" : ["cuda" ,"hip" ]}) 134self .assertTrue (rows [self .amd .name ]["active" ]and rows [self .amd .name ]["newest" ]and not rows [self .amd .name ]["other_card" ]) 135 136def test_resolve_and_status_take_the_make_word (self ): 137self .assertEqual (ec .resolve (self .components ,backend = "amd" ).root .name ,self .amd .name ) 138self .assertEqual (ec .resolve (self .components ,backend = "hip-rx6000" ).root .name ,self .amd .name ) 139report = ec .status (self .components ,backend = "amd" ) 140self .assertTrue (report ["usable" ],report ["notice" ]) 141self .assertEqual ((report ["backend" ],report ["engine" ]["backend" ]), ("hip" ,"hip" )) 142 143def test_switching_to_the_amd_engine_keeps_the_amd_choice (self ): 144hardware = {** self .hardware ,"makes" : ["cuda" ,"hip" ]} 145with mock .patch .object (updater ,"detect_hardware" ,side_effect = lambda environ = None : {** hardware ,"backend" :ec .saved_backend ()or "cuda" }): 146out = lib .use_engine (self .ctx (hardware = hardware ),"qwen" ,self .amd .name ) 147self .assertEqual (out .get ("backend_saved" ),"hip" ) 148self .assertEqual (json .loads ((self .settings / "settings.json" ).read_text ())[ec .SAVED_KEY ],"hip" ) 149self .assertEqual (ec .resolve (self .components ,backend = "hip" ).root .name ,self .amd .name ) 150 151def test_the_engines_listing_with_backend_amd_from_the_terminal (self ): 152args = argparse .Namespace (components_dir = str (self .components ),backend = "amd" ,wait = None ,force = False ,model_config = None ,output = "json" , 153components_command = "engines" ) 154out = io .StringIO () 155with mock .patch .object (updater ,"detect_hardware" ,side_effect = lambda environ = None : {** self .hardware ,"makes" : ["cuda" ,"hip" ], 156"backend" :ec .split_backend ((environ or {}).get (ec .ENV_BACKEND ,"cuda" ))[0 ]}), \ 157redirect_stdout (out ): 158code = update_cmd .run (args ) 159self .assertEqual (code ,0 ) 160rows = {e ["folder" ]:e for f in json .loads (out .getvalue ())["families" ]for e in f ["engines" ]} 161self .assertTrue (rows [self .amd .name ]["active" ]and rows [self .amd .name ]["fits" ]) 162shutil .rmtree (self .settings ,ignore_errors = True ) 163 164 165if __name__ == "__main__" : 166unittest .main ()