okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
049ae1c67
main
1"""Round 0213 TUICFG, step 4: choosing the engine on a PC with cards of several makes (#4971, #5111). 2 3Every case is a temporary /sys/class/drm tree (a real two-vendor sysfs cannot be made on a test box), in both card orders.""" 4from __future__import annotations 5 6import os 7import tempfile 8import unittest 9from pathlib import Path 10from unittest import mock 11 12from strata_cli .launch import catalog as C 13from strata_control import engine_component as ec 14 15AMD ,INTEL ,NVIDIA = "0x1002" ,"0x8086" ,"0x10de" 16REAL_ARC = C .intel_arc_cards 17 18 19def build (root :Path ,* cards )-> None : 20"""cards: (name, vendor, device id or None, own memory in MiB or None); a name like card0 / card10 / card1-DP-1 is the directory.""" 21for name ,vendor ,did ,mib in cards : 22dev = root / name / "device" 23dev .mkdir (parents = True ) 24if vendor : 25 (dev / "vendor" ).write_text (vendor + "\n" ,encoding = "utf-8" ) 26if did : 27 (dev / "device" ).write_text (did + "\n" ,encoding = "utf-8" ) 28if mib is not None : 29 (dev / "mem_info_vram_total" ).write_text (str (mib * 1024 * 1024 )+ "\n" ,encoding = "utf-8" ) 30 31 32class Tree (unittest .TestCase ): 33def setUp (self ): 34self .tmp = tempfile .TemporaryDirectory () 35self .addCleanup (self .tmp .cleanup ) 36self .root = Path (self .tmp .name ) 37 38def backend (self ,* cards ): 39build (self .root ,* cards ) 40return ec ._drm_vendor_backend (str (self .root )) 41 42def arcs (self ,* cards ): 43build (self .root ,* cards ) 44return REAL_ARC (str (self .root )) 45 46 47APU = ("card0" ,AMD ,"0x164e" ,512 ) 48ARC = ("card1" ,INTEL ,"0xe211" ,None ) 49 50 51class Backend (Tree ): 52def test_an_apu_beside_an_arc_is_not_an_amd_pc_in_either_order (self ): 53self .assertEqual (self .backend (APU ,ARC ),"sycl" ) 54self .tearDown ();self .setUp () 55self .assertEqual (self .backend (("card0" ,INTEL ,"0xe211" ,None ), ("card1" ,AMD ,"0x164e" ,512 )),"sycl" ) 56 57def test_an_arc_of_unknown_size_beside_an_apu (self ): 58self .assertEqual (self .backend (APU , ("card1" ,INTEL ,"0xe2f0" ,None )),"sycl" ) 59 60def test_a_sole_apu_stays_amd (self ): 61self .assertEqual (self .backend (APU ),"hip" ) 62 63def test_a_sole_arc_is_intel (self ): 64self .assertEqual (self .backend (ARC ),"sycl" ) 65 66def test_a_discrete_amd_card_and_nvidia_keep_their_order (self ): 67self .assertEqual (self .backend (("card0" ,AMD ,"0x7550" ,16384 ),ARC ),"hip" ) 68self .tearDown ();self .setUp () 69self .assertEqual (self .backend (APU , ("card1" ,NVIDIA ,"0x2204" ,None )),"cuda" ) 70 71def test_a_large_unified_memory_apu_is_a_card (self ): 72self .assertEqual (self .backend (("card0" ,AMD ,"0x1586" ,32768 )),"hip" ) 73self .tearDown ();self .setUp () 74self .assertEqual (self .backend (("card0" ,AMD ,"0x1586" ,32768 ),ARC ),"hip" ,"the older order; an explicit backend decides otherwise" ) 75 76def test_connectors_and_unreadable_entries_are_ignored (self ): 77self .assertEqual (self .backend (("card0-DP-1" ,None ,None ,None ),APU ,ARC ),"sycl" ) 78 79def test_no_card_is_none (self ): 80self .assertIsNone (ec ._drm_vendor_backend (str (self .root / "missing" ))) 81 82 83class ArcCards (Tree ): 84def test_both_orders_and_a_card_beside_a_connector (self ): 85for cards in ([APU ,ARC ], [("card0" ,INTEL ,"0xe211" ,None ), ("card1" ,AMD ,"0x164e" ,512 )], [("card0-DP-1" ,None ,None ,None ),APU ,ARC ]): 86with self .subTest (cards = [c [0 ]for c in cards ]): 87self .tearDown ();self .setUp () 88got = self .arcs (* cards ) 89self .assertEqual ([(g ["name" ],g ["vram_gb" ],g ["index" ])for g in got ], [("Intel Arc Pro B60" ,24.0 ,0 )]) 90 91def test_a_card_number_above_nine_and_an_unreadable_neighbour (self ): 92got = self .arcs (("card0" ,INTEL ,None ,None ), ("card2" ,INTEL ,"0xe20b" ,None ), ("card10" ,INTEL ,"0xe211" ,None )) 93self .assertEqual ([g ["name" ]for g in got ], ["Intel Arc B580" ,"Intel Arc Pro B60" ],"a card without a device file does not hide the rest" ) 94 95def test_an_unlisted_battlemage_or_alchemist_id_still_counts_the_processor_graphics_do_not (self ): 96got = self .arcs (("card0" ,INTEL ,"0xe2f0" ,None ), ("card1" ,INTEL ,"0x56b1" ,None )) 97self .assertEqual (len (got ),2 ) 98self .assertTrue (all (g ["usable" ]and g ["vram_gb" ]>= 8.0 for g in got )) 99self .tearDown ();self .setUp () 100self .assertEqual (self .arcs (("card0" ,INTEL ,"0x7d55" ,None ), ("card1" ,INTEL ,"0x46a6" ,None ), ("card2" ,INTEL ,"0xa7a0" ,None )), []) 101 102def test_a_sole_apu_has_no_arc (self ): 103self .assertEqual (self .arcs (APU ), []) 104 105 106class LauncherChoice (Tree ): 107"""The launcher's own view: the cards, the engine's make, and what is listed as usable.""" 108 109def computer (self ,shared_gpus ,env = None ): 110from tests .terminal .launch_support import Home 111with Home (): 112cat = C .Catalog () 113hw = {"ram_gb" :64.0 ,"os" :"Linux" ,"cpu" : {"avx2" :True },"gpus" :shared_gpus ,"disk_free_gb" :500 } 114fake = lambda root = "/sys/class/drm" :REAL_ARC (str (self .root ))# noqa: E731 115with mock .patch .dict (os .environ ,env or {},clear = False ),mock .patch .object (cat .strata ,"hardware" ,return_value = hw ), \ 116mock .patch .object (C ,"intel_arc_cards" ,fake ),mock .patch ("strata_control.engine_component.status" ,return_value = {"usable" :False }): 117if not env : 118os .environ .pop ("STRATA_ENGINE_BACKEND" ,None ) 119comp = cat .computer () 120return comp .engine_vendor , [g ["name" ]for g in comp .usable_gpus ], [g ["name" ]for g in comp .ignored_gpus ] 121 122IGPU = {"index" :0 ,"name" :"AMD Radeon Graphics" ,"vram_gb" :0.5 ,"usable" :False ,"vendor" :"amd" } 123RTX = {"index" :0 ,"name" :"NVIDIA GeForce RTX 3090" ,"vram_gb" :24.0 ,"usable" :True ,"vendor" :"nvidia" } 124 125def test_the_arc_is_used_beside_an_apu_igpu (self ): 126build (self .root ,APU ,ARC ) 127vendor ,usable ,ignored = self .computer ([self .IGPU ]) 128self .assertEqual ((vendor ,usable ), ("intel" , ["Intel Arc Pro B60" ])) 129 130def test_an_explicit_sycl_beside_nvidia_uses_the_arc_also_when_its_model_is_not_listed (self ): 131for did in ("0xe211" ,"0xe2f0" ): 132with self .subTest (did = did ): 133self .tearDown ();self .setUp () 134build (self .root , ("card0" ,NVIDIA ,"0x2204" ,None ), ("card1" ,INTEL ,did ,None )) 135vendor ,usable ,ignored = self .computer ([self .RTX ], {"STRATA_ENGINE_BACKEND" :"sycl" }) 136self .assertEqual (vendor ,"intel" ) 137self .assertEqual (len (usable ),1 ) 138self .assertIn ("NVIDIA GeForce RTX 3090" ,ignored ) 139 140def test_without_a_choice_nvidia_stays_the_first_make (self ): 141build (self .root , ("card0" ,NVIDIA ,"0x2204" ,None ),ARC ) 142vendor ,usable ,_ = self .computer ([self .RTX ]) 143self .assertEqual ((vendor ,usable ), ("nvidia" , ["NVIDIA GeForce RTX 3090" ])) 144 145 146if __name__ == "__main__" : 147unittest .main ()