okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0351ecb11
main
1"""#3688: the update path never crosses GPU makes. An AMD R9700 PC (hip rx9070 installed) was given the cuda13 engine because a leftover 2nvidia-smi made detection say cuda. Three guards, all tested here with faked hardware: 31. detection: on Windows the display adapters decide first, and a tool for a make with no adapter is ignored; 42. the update decision: an installed engine of another make is never replaced by one of the detected make (unless the person asks); 53. the stage step refuses a package of another make even if a decision slipped it through.""" 6from __future__import annotations 7 8import subprocess 9import sys 10import tempfile 11import unittest 12from pathlib import Path 13from types import SimpleNamespace 14from unittest import mock 15 16from strata_control import engine_component as ec ,feed as feedmod ,updater 17 18WIN = "windows-x86_64" 19 20R9700_ONLY = [{"vendor" :"1002" ,"device" :0x7551 ,"name" :"AMD Radeon AI PRO R9700" ,"vram_gb" :32.0 }] 21R9700_AND_NVIDIA = R9700_ONLY + [{"vendor" :"10de" ,"device" :0x2504 ,"name" :"NVIDIA GeForce RTX 3060" ,"vram_gb" :12.0 }] 22AMD_IGPU_ONLY = [{"vendor" :"1002" ,"device" :0x164E ,"name" :"AMD Radeon(TM) Graphics" ,"vram_gb" :0.5 }] 23NVIDIA_ONLY = [{"vendor" :"10de" ,"device" :0x2504 ,"name" :"NVIDIA GeForce RTX 3060" ,"vram_gb" :12.0 }] 24 25 26def package (backend ,variant ,archs ,version = "0.1.47" ): 27return feedmod ._engine_package ({ 28"version" :version ,"platform" :WIN ,"backend" :backend ,"variant" :variant ,"archs" :list (archs ), 29"min_driver" : {"nvidia" :"580.65" }if backend == "cuda" else {},"min_runtime" :None ,"build_id" :f"b- { variant } " , 30"format" :"zip" ,"url" :f"e- { variant } .zip" ,"size" :10 ,"sha256" :"a" * 64 ,"manifest_sha256" :"b" * 64 , 31"installed_bytes" :20 ,"notes" :"" }) 32 33 34FEED = SimpleNamespace (engine = [package ("cuda" ,"cuda13" , ("sm_75" ,"sm_86" ,"sm_120" )), 35package ("hip" ,"rdna3" , ("gfx1100" ,"gfx1151" )), 36package ("hip" ,"rx9070" , ("gfx1200" ,"gfx1201" ))]) 37 38 39class Detection (unittest .TestCase ): 40def detect (self ,adapters ,tools = ("nvidia-smi" ,)): 41def fake_run (command ,** kw ): 42return subprocess .CompletedProcess (command ,0 ,stdout = "NVIDIA GeForce RTX 3060\n" ,stderr = "" ) 43with mock .patch .object (ec .os ,"name" ,"nt" ),mock .patch .object (ec ,"host_platform" ,return_value = WIN ), \ 44mock .patch .object (ec .shutil ,"which" ,lambda name :name if name in tools else None ), \ 45mock .patch .object (ec .subprocess ,"run" ,fake_run ),mock .patch .object (ec ,"windows_display_adapters" ,return_value = adapters ): 46return ec .detect_backend ({}) 47 48def test_a_leftover_nvidia_smi_does_not_make_an_amd_pc_cuda (self ): 49self .assertEqual (self .detect (R9700_ONLY ), ("hip" ,"display adapters" )) 50 51def test_a_leftover_nvidia_smi_beside_only_an_integrated_radeon_is_not_believed_either (self ): 52self .assertEqual (self .detect (AMD_IGPU_ONLY ), (None ,"no GPU tool answered" )) 53 54def test_an_nvidia_pc_is_still_cuda (self ): 55self .assertEqual (self .detect (NVIDIA_ONLY ), ("cuda" ,"display adapters" )) 56 57def test_both_makes_keep_the_usual_order (self ): 58self .assertEqual (self .detect (R9700_AND_NVIDIA )[0 ],"cuda" ) 59self .assertEqual (ec .adapter_makes (R9700_AND_NVIDIA ), {"cuda" ,"hip" }) 60 61def test_adapters_that_cannot_be_read_leave_the_tools_in_charge (self ): 62self .assertEqual (self .detect ([]), ("cuda" ,"nvidia-smi" )) 63 64def test_the_environment_override_still_wins (self ): 65with mock .patch .object (ec ,"windows_display_adapters" ,return_value = R9700_ONLY ): 66self .assertEqual (ec .detect_backend ({"STRATA_ENGINE_BACKEND" :"cuda" }), ("cuda" ,"environment" )) 67 68 69class Decision (unittest .TestCase ): 70"""Faked hardware and a faked installed engine (a HIP package cannot be built on a machine without the HIP tuning files).""" 71 72def setUp (self ): 73self .temp = tempfile .TemporaryDirectory (prefix = "strata-makeguard-" ) 74self .addCleanup (self .temp .cleanup ) 75self .components = Path (self .temp .name )/ "components" 76self .state = updater .State (self .components ) 77 78def installed (self ,backend ,variant ,version = "0.1.46" ): 79"""Pretend this engine is installed: what the folder scan and the start path would report.""" 80engine = SimpleNamespace (version = version ,platform = WIN ,backend = backend ,variant = variant ,build_id = "b" ,root = Path ("x" )/ f" { version } - { backend } " ,signature = "s" ) 81for patch in (mock .patch .object (ec ,"scan" ,return_value = ([engine ], [])), 82mock .patch .object (updater ,"installed_engine" ,lambda root ,hardware : (engine if hardware .get ("backend" )== backend else None ,None ))): 83patch .start () 84self .addCleanup (patch .stop ) 85 86def hardware (self ,backend ,makes ,source = "nvidia-smi" ,** extra ): 87return {"platform" :WIN ,"backend" :backend ,"backend_source" :source ,"runtime" :None ,"nvidia_driver" :"610.47" , 88"cuda_archs" : ["sm_86" ]if backend == "cuda" else [],"gfx" : ["gfx1201" ]if backend == "hip" else [],"gpu_names" : [], 89"makes" :list (makes ),** extra } 90 91def ctx (self ,hardware ): 92return updater .Context (components = self .components ,source = "" ,trusted = {},hardware = hardware ,environ = {}) 93 94def test_the_r9700_case_detection_says_cuda_but_the_adapters_list_only_the_amd_card_so_the_update_stays_hip (self ): 95self .installed ("hip" ,"rx9070" ) 96ctx = self .ctx (self .hardware ("cuda" , ["hip" ]))# what a leftover nvidia-smi said 97with mock .patch .object (updater ,"detect_hardware" ,return_value = self .hardware ("hip" , ["hip" ])): 98report ,pkg = updater ._choose_engine (FEED ,ctx ,self .state ) 99self .assertEqual (ctx .hardware ["backend" ],"hip" ) 100self .assertEqual (ctx .hardware ["backend_source" ],"installed engine" ) 101self .assertEqual ((pkg .backend ,pkg .variant ), ("hip" ,"rx9070" )) 102self .assertEqual (report ["status" ],"available" ) 103 104def test_with_no_card_of_the_installed_make_the_update_is_refused_in_plain_words (self ): 105self .installed ("hip" ,"rx9070" ) 106report ,pkg = updater ._choose_engine (FEED ,self .ctx (self .hardware ("cuda" , ["cuda" ])),self .state ) 107self .assertIsNone (pkg ) 108self .assertEqual (report ["status" ],"wrong-make" ) 109for word in ("AMD" ,"NVIDIA" ,"Nothing was changed" ): 110self .assertIn (word ,report ["note" ]) 111self .assertIsNone (report ["offered" ]) 112self .assertEqual (report ["installed" ]["backend" ],"hip" ) 113 114def test_cards_unreadable_and_a_different_make_installed_also_refuses (self ): 115self .installed ("hip" ,"rx9070" ) 116report ,pkg = updater ._choose_engine (FEED ,self .ctx (self .hardware ("cuda" , [])),self .state ) 117self .assertEqual ((report ["status" ],pkg ), ("wrong-make" ,None )) 118 119def test_nothing_installed_yet_is_not_blocked (self ): 120with mock .patch .object (ec ,"scan" ,return_value = ([], [])): 121report ,pkg = updater ._choose_engine (FEED ,self .ctx (self .hardware ("cuda" , ["cuda" ])),self .state ) 122self .assertEqual ((report ["status" ],pkg .backend ), ("install" ,"cuda" )) 123 124def test_the_same_make_updates_as_before (self ): 125self .installed ("cuda" ,"cuda13" ,version = "0.1.46" ) 126report ,pkg = updater ._choose_engine (FEED ,self .ctx (self .hardware ("cuda" , ["cuda" ])),self .state ) 127self .assertEqual ((report ["status" ],pkg .backend ,pkg .variant ), ("available" ,"cuda" ,"cuda13" )) 128 129def test_the_cuda_pc_with_an_nvidia_engine_does_not_flip_to_amd (self ): 130self .installed ("cuda" ,"cuda13" ) 131ctx = self .ctx (self .hardware ("cuda" , ["cuda" ,"hip" ])) 132report ,pkg = updater ._choose_engine (FEED ,ctx ,self .state ) 133self .assertEqual (pkg .backend ,"cuda" ) 134 135def test_an_explicit_backend_is_the_persons_decision_and_passes (self ): 136self .installed ("hip" ,"rx9070" ) 137for source in ("requested" ,"environment" ): 138self .assertIsNone (updater .make_guard (self .ctx (self .hardware ("cuda" , ["cuda" ],source = source ))),source ) 139 140def test_the_stage_step_refuses_a_package_of_another_make_even_if_a_decision_slipped_it_through (self ): 141self .installed ("hip" ,"rx9070" ) 142ctx = self .ctx (self .hardware ("cuda" , ["cuda" ])) 143cuda = package ("cuda" ,"cuda13" , ("sm_86" ,)) 144with mock .patch .object (updater ,"_choose_engine" ,return_value = ({"status" :"available" ,"installed" :None },cuda )),mock .patch .object (updater ,"_download" ,side_effect = AssertionError ("downloaded" )): 145with self .assertRaises (updater .UpdateError )as caught : 146updater .update_engine (ctx ,FEED ,self .state ) 147self .assertIn ("another make" ,str (caught .exception )) 148 149 150class Pascal (unittest .TestCase ): 151"""#3655: a GTX 1080 (sm_61) is a working machine on the CUDA 12 engine; the CUDA 13 build has no code for it and is never chosen.""" 152 153FEED = [package ("cuda" ,"cuda13" , ("sm_75" ,"sm_86" ,"sm_89" ,"sm_120" )), 154feedmod ._engine_package ({"version" :"0.1.47" ,"platform" :WIN ,"backend" :"cuda" ,"variant" :"cuda12" ,"min_driver" : {"nvidia" :"525.60" }, 155"archs" : ["sm_60" ,"sm_61" ,"sm_70" ,"sm_75" ,"sm_80" ,"sm_86" ,"sm_89" ],"min_runtime" :None ,"build_id" :"b-12" , 156"format" :"zip" ,"url" :"e-12.zip" ,"size" :10 ,"sha256" :"a" * 64 ,"manifest_sha256" :"b" * 64 , 157"installed_bytes" :20 ,"notes" :"" })] 158 159def hardware (self ,archs ): 160return {"platform" :WIN ,"backend" :"cuda" ,"runtime" :None ,"nvidia_driver" :"581.15" ,"cuda_archs" :archs ,"gfx" : [],"gpu_names" : []} 161 162def test_two_gtx_1080_get_the_cuda_12_engine (self ): 163best ,reasons = feedmod .select_engine (self .FEED ,self .hardware (["sm_61" ,"sm_61" ])) 164self .assertEqual (best .variant ,"cuda12" ) 165self .assertTrue (any ("cuda13" in r or "sm_75" in r for r in reasons ),"the CUDA 13 build is refused for the card" ) 166 167def test_a_newer_card_still_gets_the_newest_build (self ): 168best ,_ = feedmod .select_engine (self .FEED ,self .hardware (["sm_86" ])) 169self .assertEqual (best .variant ,"cuda13" ) 170 171 172class Host (unittest .TestCase ): 173def test_the_app_shows_the_sentence_not_the_no_engine_for_this_card_text (self ): 174text = (Path (__file__ ).resolve ().parents [2 ]/ "apps" / "host" / "server" / "updates.mjs" ).read_text (encoding = "utf-8" ) 175self .assertIn ("engine.status === 'wrong-make'" ,text ) 176 177 178if __name__ == "__main__" : 179unittest .main ()