okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
538acd098
main
1"""Tests for setup.py's AMD card detection on a mocked KFD topology (/sys/class/kfd + /sys/class/drm): the arch 2names from gfx_target_version, the CPU node skipped, HIP numbering, product names, which cards are supported and 3which TheRock index each family installs from. No GPU, no ROCm, no downloads. 4 5python -m unittest tools.test_setup_amd 6""" 7from __future__import annotations 8 9import sys 10import tempfile 11import unittest 12from unittest import mock 13from pathlib import Path 14 15ROOT = Path (__file__ ).resolve ().parents [1 ] 16sys .path .insert (0 ,str (ROOT )) 17import setup # noqa: E402 18 19 20def fake_sysfs (root :Path ,nodes :list )-> None : 21"""nodes: (gfx_target_version, simd_count, render_minor, product_name or None, vram_bytes)""" 22for i , (ver ,simd ,minor ,name ,vram )in enumerate (nodes ): 23n = root / "class/kfd/kfd/topology/nodes" / str (i ) 24n .mkdir (parents = True ) 25 (n / "properties" ).write_text (f"cpu_cores_count { 0 if simd else 12 } \nsimd_count { simd } \n" 26f"gfx_target_version { ver } \ndrm_render_minor { minor } \n" ) 27if simd : 28d = root / f"class/drm/renderD { minor } /device" 29d .mkdir (parents = True ) 30 (d / "mem_info_vram_total" ).write_text (str (vram )) 31if name is not None : 32 (d / "product_name" ).write_text (name + "\n" ) 33 34 35class KfdDetection (unittest .TestCase ): 36def setUp (self ): 37self .tmp = tempfile .TemporaryDirectory () 38self .root = Path (self .tmp .name ) 39self .win = setup .WIN 40setup .WIN = False 41 42def tearDown (self ): 43setup .WIN = self .win 44self .tmp .cleanup () 45 46def test_every_family (self ): 47fake_sysfs (self .root , [ 48 (0 ,0 ,0 ,None ,0 ),# the CPU node: skipped 49 (110001 ,120 ,128 ,"" ,16 << 30 ),# gfx1101 without a product name 50 (120000 ,64 ,129 ,None ,16 << 30 ),# gfx1200, no product_name file 51 (120001 ,128 ,130 ,"AMD Radeon AI PRO R9700" ,32 << 30 ), 52 (110002 ,64 ,131 ,None ,8 << 30 ),# gfx1102: supported, unvalidated (#938) 53 (100306 ,4 ,132 ,None ,512 << 20 ),# an integrated gfx1036: listed, not supported 54 (110000 ,192 ,133 ,"Radeon RX 7900 XTX" ,24 << 30 ), 55 ]) 56g = setup .amd_gpus (str (self .root )) 57self .assertEqual ([x ["arch" ]for x in g ], ["gfx1101" ,"gfx1200" ,"gfx1201" ,"gfx1102" ,"gfx1036" ,"gfx1100" ]) 58self .assertEqual ([x ["index" ]for x in g ], [0 ,1 ,2 ,3 ,4 ,5 ])# HIP numbers: GPU nodes only 59self .assertEqual (g [0 ]["name" ],setup .AMD_NAMES ["gfx1101" ]) 60self .assertEqual (g [1 ]["name" ],setup .AMD_NAMES ["gfx1200" ]) 61self .assertEqual (g [2 ]["name" ],"AMD Radeon AI PRO R9700" ) 62self .assertEqual (g [3 ]["name" ],setup .AMD_NAMES ["gfx1102" ]) 63self .assertAlmostEqual (g [2 ]["vram_gb" ],32.0 ) 64ok = [x ["arch" ]for x in g if setup .amd_problem (x )is None ] 65self .assertEqual (ok , ["gfx1101" ,"gfx1200" ,"gfx1201" ,"gfx1102" ,"gfx1100" ]) 66self .assertIn ("gfx1036" ,setup .amd_problem (g [4 ])) 67 68def test_no_kfd (self ): 69self .assertEqual (setup .amd_gpus (str (self .root )), []) 70 71def test_rocm_index_per_family (self ): 72for arch in setup .AMD_ARCHS : 73self .assertIn (arch ,setup .ROCM_INDEXES ) 74self .assertTrue (setup .ROCM_INDEXES ["gfx1101" ].endswith ("/gfx110X-dgpu/" )) 75self .assertTrue (setup .ROCM_INDEXES ["gfx1200" ].endswith ("/gfx120X-all/" )) 76self .assertEqual (setup .ROCM_INDEXES ["gfx1101" ],setup .ROCM_INDEXES ["gfx1100" ]) 77self .assertEqual (setup .ROCM_INDEXES ["gfx1200" ],setup .ROCM_INDEXES ["gfx1201" ]) 78# #524: the RX 6700 XT (gfx1031) takes the RDNA2 wheels, as the RX 6800 / 6900 (gfx1030) 79self .assertEqual (setup .ROCM_INDEXES ["gfx1031" ],setup .ROCM_INDEXES ["gfx1030" ]) 80self .assertIsNone (setup .amd_problem ({"arch" :"gfx1031" })) 81 82 83class Rx6000Detection (KfdDetection ): 84"""0.2.2: the RX 6000 engine package (gfx1030 / gfx1031). Setup names the cards, says the package is built and not tested on a real 85RX 6000 card, and says it for these two architectures only.""" 86 87def test_rx6000_cards_by_gfx_target_version (self ): 88fake_sysfs (self .root , [ 89 (100300 ,80 ,128 ,"AMD Radeon RX 6900 XT" ,16 << 30 ),# gfx1030 90 (100301 ,40 ,129 ,None ,12 << 30 ),# gfx1031 (RX 6700 XT), no product name 91 (100302 ,28 ,130 ,None ,8 << 30 ),# gfx1032 (RX 6600): not in the package 92 ]) 93g = setup .amd_gpus (str (self .root )) 94self .assertEqual ([x ["arch" ]for x in g ], ["gfx1030" ,"gfx1031" ,"gfx1032" ]) 95self .assertEqual (g [1 ]["name" ],setup .AMD_NAMES ["gfx1031" ]) 96self .assertEqual ([setup .is_rdna2 (x )for x in g ], [True ,True ,False ]) 97self .assertIsNone (setup .amd_problem (g [0 ])) 98self .assertIsNone (setup .amd_problem (g [1 ])) 99 100def test_the_note_is_for_gfx103x_only (self ): 101for arch in ("gfx1030" ,"gfx1031" ,"gfx1030:sramecc-:xnack-" ): 102lines = setup .rdna2_notes ({"arch" :arch }) 103self .assertEqual (len (lines ),1 ,arch ) 104self .assertIn ("built, not tested on a real RX 6000 card" ,lines [0 ]) 105self .assertIn (arch .split (":" )[0 ],lines [0 ]) 106for arch in ("gfx1100" ,"gfx1101" ,"gfx1102" ,"gfx1200" ,"gfx1201" ,"gfx1151" ,"gfx1032" ,"gfx103" ,"gfx10300" ,"" ,None ): 107self .assertEqual (setup .rdna2_notes ({"arch" :arch }), [],arch ) 108self .assertEqual (setup .rdna2_notes (None ), []) 109self .assertFalse (setup .is_rdna2 ({"arch" :"gfx1030x" })) 110 111def test_windows_ids_and_names (self ): 112for device ,arch in ((0x73BF ,"gfx1030" ), (0x73AF ,"gfx1030" ), (0x73A5 ,"gfx1030" ), (0x73DF ,"gfx1031" )): 113self .assertEqual (setup .win_amd_arch (device ,"" ),arch ) 114self .assertEqual (setup .win_amd_arch (None ,"AMD Radeon RX 6900 XT" ),"gfx1030" ) 115self .assertEqual (setup .win_amd_arch (0x744C ,"AMD Radeon RX 7900 XTX" ),"gfx1100" )# an RX 7000 card is not RDNA2 116 117 118class GpuLists (unittest .TestCase ): 119"""--gpus with AMD cards: every chosen card must be supported; the first is the main one.""" 120AMD = [{"index" :0 ,"name" :"AMD Radeon RX 9070 XT" ,"vram_gb" :16.0 ,"arch" :"gfx1201" }, 121 {"index" :1 ,"name" :"AMD Radeon AI PRO R9700" ,"vram_gb" :32.0 ,"arch" :"gfx1201" }, 122 {"index" :2 ,"name" :"AMD Radeon (gfx1036)" ,"vram_gb" :0.5 ,"arch" :"gfx1036" }, 123 {"index" :3 ,"name" :"AMD Radeon RX 7900 XTX" ,"vram_gb" :24.0 ,"arch" :"gfx1100" }] 124 125def setUp (self ): 126self .say = setup .say 127setup .say = lambda * a ,** k :None 128 129def tearDown (self ): 130setup .say = self .say 131 132def test_list_in_order (self ): 133self .assertEqual ([g ["index" ]for g in setup .amd_parse_gpus ("1,0" ,self .AMD )], [1 ,0 ]) 134self .assertEqual ([g ["index" ]for g in setup .amd_parse_gpus (" 0, 3 " ,self .AMD )], [0 ,3 ]) 135 136def test_all_is_every_supported_card_most_vram_first (self ): 137self .assertEqual ([g ["index" ]for g in setup .amd_parse_gpus ("all" ,self .AMD )], [1 ,3 ,0 ]) 138 139def test_all_leaves_out_the_apu_beside_two_cards (self ): 140"""Strix Halo beside two discrete cards: "all" is the two cards (the APU shares the RAM and draws the desktop); with ONE card it is both 141(two are needed for a split) and a number list still takes the APU as asked.""" 142apu = {"index" :0 ,"name" :"AMD Radeon 8060S" ,"vram_gb" :64.0 ,"arch" :"gfx1151" ,"uma" :True } 143r9700 = {"index" :1 ,"name" :"AMD Radeon AI PRO R9700" ,"vram_gb" :32.0 ,"arch" :"gfx1201" } 144r9070 = {"index" :2 ,"name" :"AMD Radeon RX 9070 XT" ,"vram_gb" :16.0 ,"arch" :"gfx1201" } 145self .assertEqual ([g ["index" ]for g in setup .amd_parse_gpus ("all" , [apu ,r9700 ,r9070 ])], [1 ,2 ]) 146self .assertEqual ([g ["index" ]for g in setup .amd_parse_gpus ("all" , [apu ,r9700 ])], [1 ,0 ]) 147self .assertEqual ([g ["index" ]for g in setup .amd_parse_gpus ("0,1" , [apu ,r9700 ,r9070 ])], [0 ,1 ]) 148 149def test_an_unused_apu_with_a_big_carve_out_is_said (self ): 150apu = {"index" :0 ,"name" :"AMD Radeon 8060S" ,"vram_gb" :96.0 ,"arch" :"gfx1151" ,"uma" :True ,"dedicated_gb" :64.0 } 151r9700 = {"index" :1 ,"name" :"AMD Radeon AI PRO R9700" ,"vram_gb" :32.0 ,"arch" :"gfx1201" } 152out = setup .unused_apu_notes ([r9700 ], [apu ,r9700 ],62.0 ) 153self .assertEqual (len (out ),1 ) 154self .assertTrue (out [0 ].startswith ("!" )and "64 GB" in out [0 ]and "62 GB" in out [0 ]) 155self .assertEqual (setup .unused_apu_notes ([apu ,r9700 ], [apu ,r9700 ],62.0 ), [])# in the run: not "unused" 156self .assertEqual (setup .unused_apu_notes ([r9700 ], [dict (apu ,dedicated_gb = 2.0 ),r9700 ],120.0 ), [])# a small carve-out: nothing to say 157self .assertEqual (setup .unused_apu_notes ([r9700 ], [r9700 ],120.0 ), []) 158 159def test_refusals (self ): 160for text in ("1,2" ,"1,7" ,"1" ,"1,1" ,"x,y" ): 161with self .assertRaises (SystemExit ,msg = text ): 162setup .amd_parse_gpus (text ,self .AMD ) 163 164def test_wheels_hold_one_family (self ): 165with tempfile .TemporaryDirectory ()as d :# no system ROCm there: the wheels would be needed 166old = setup .os .environ .get ("ROCM_PATH" ) 167setup .os .environ ["ROCM_PATH" ]= d 168try : 169with self .assertRaises (SystemExit ): 170setup .rocm_root (["gfx1100" ,"gfx1201" ]) 171finally : 172if old is None : 173del setup .os .environ ["ROCM_PATH" ] 174else : 175setup .os .environ ["ROCM_PATH" ]= old 176 177def test_runtime_only_system_rocm_falls_back_to_the_wheels (self ): 178"""#446: a system ROCm 7 with hipcc and libhipblas but no HIP development files (no hip-lang CMake package, no 179hip_runtime.h) is not used for the build: a warning says what is missing and the wheels path follows (here 180it stops at the two-family check, which only the wheels path makes); with the files it is used as before.""" 181said = [] 182setup .say = lambda msg = "" :said .append (msg )# tearDown puts the real one back 183with tempfile .TemporaryDirectory ()as d : 184sysroot = Path (d ) 185for rel ,text in (("bin/hipcc" ,"" ), ("lib/libhipblas.so.3" ,"" ), 186 ("include/rocm-core/rocm_version.h" , 187"#define ROCM_VERSION_MAJOR 7\n#define ROCM_VERSION_MINOR 14\n" )): 188 (sysroot / rel ).parent .mkdir (parents = True ,exist_ok = True ) 189 (sysroot / rel ).write_text (text ) 190with mock .patch .dict (setup .os .environ , {"ROCM_PATH" :d }): 191with self .assertRaises (SystemExit ): 192setup .rocm_root (["gfx1100" ,"gfx1201" ]) 193self .assertIn (f"the ROCm in { sysroot } has no HIP development files (lib/cmake/hip-lang/hip-lang-" 194"config.cmake, include/hip/hip_runtime.h): using AMD's wheels" ,"\n" .join (said )) 195self .assertIn ("two GPU families" ,"\n" .join (said )) 196for lib in ("lib64" ,"lib" ):# either place CMake looks 197with self .subTest (lib = lib ): 198cfg = sysroot / lib / "cmake/hip-lang/hip-lang-config.cmake" 199cfg .parent .mkdir (parents = True ,exist_ok = True ) 200cfg .write_text ("" ) 201said .clear () 202with self .assertRaises (SystemExit ): 203setup .rocm_root (["gfx1100" ,"gfx1201" ]) 204self .assertIn ("(include/hip/hip_runtime.h)" ,"\n" .join (said )) 205 (sysroot / "include/hip" ).mkdir (parents = True ,exist_ok = True ) 206 (sysroot / "include/hip/hip_runtime.h" ).write_text ("" ) 207self .assertEqual (setup .rocm_root (["gfx1100" ,"gfx1201" ]), (sysroot , [str (sysroot / "lib" )])) 208cfg .unlink () 209 (sysroot / "include/hip/hip_runtime.h" ).unlink () 210 211def test_build_for_every_arch (self ): 212"""build_engine_hip compiles for the set of the chosen cards' archs and records it in BUILD.json.""" 213calls = {} 214saved = {k :getattr (setup ,k )for k in ("ROOT" ,"rocm_root" ,"cmake_build" ,"source_hash" ,"source_version" , 215"ok" ,"shutil" )} 216with tempfile .TemporaryDirectory ()as d : 217root = Path (d ) 218 219def fake_build (src ,bdir ,target ,defs ,vcvars ,bat ): 220calls ["defs" ]= defs 221 (bdir ).mkdir (parents = True ,exist_ok = True ) 222 (bdir / setup .EXE ).write_text ("engine" ) 223 224class Sh : 225which = staticmethod (lambda name :"/usr/bin/" + name ) 226copy2 = staticmethod (lambda a ,b :Path (b ).write_text (Path (a ).read_text ())) 227setup .ROOT ,setup .cmake_build ,setup .ok ,setup .shutil = root ,fake_build ,lambda * a :None ,Sh 228setup .source_hash ,setup .source_version = (lambda * a :"h" ), (lambda :"0.1.31" ) 229setup .rocm_root = lambda archs : (calls .setdefault ("archs" ,archs )and root , [str (root / "lib" )]) 230try : 231setup .build_engine_hip ({"arch" :"gfx1201" ,"archs" : ["gfx1201" ,"gfx1100" ,"gfx1201" ]},root ) 232self .assertEqual (calls ["archs" ], ["gfx1100" ,"gfx1201" ]) 233self .assertIn ("-DCMAKE_HIP_ARCHITECTURES=gfx1100;gfx1201" ,calls ["defs" ]) 234import json 235self .assertEqual (json .loads ((root / "engine" / "BUILD.json" ).read_text ())["archs" ], 236 ["gfx1100" ,"gfx1201" ]) 237calls .clear ()# one of those cards alone: already built, no compile 238setup .build_engine_hip ({"arch" :"gfx1100" },root ) 239self .assertNotIn ("defs" ,calls ) 240setup .build_engine_hip ({"arch" :"gfx1101" },root )# another arch: compiled again 241self .assertIn ("-DCMAKE_HIP_ARCHITECTURES=gfx1101" ,calls ["defs" ]) 242finally : 243for k ,v in saved .items (): 244setattr (setup ,k ,v ) 245 246 247class WindowsDetection (unittest .TestCase ): 248"""Windows: the AMD cards from the display adapters (mocked Win32_VideoController rows and display-class registry 249values), the HIP runtime's numbering from `strata-device --list-devices`, and the card setup chose matched to its 250HIP ordinal (#325: an integrated Radeon is HIP's device 0).""" 251ADAPTERS = [# Win32_VideoController: name, PNPDeviceID, AdapterRAM (32-bit: at most 4 GB) 252 {"name" :"NVIDIA GeForce RTX 5070" ,"pnp" :r"PCI\VEN_10DE&DEV_2F04&SUBSYS_1234&REV_A1\4&1" ,"ram" :4293918720 }, 253 {"name" :"AMD Radeon RX 9070 XT" ,"pnp" :r"PCI\VEN_1002&DEV_7550&SUBSYS_0E3B1002&REV_C0\6&2" ,"ram" :4293918720 }, 254 {"name" :"AMD Radeon(TM) Graphics" ,"pnp" :r"PCI\VEN_1002&DEV_164E&SUBSYS_88771043&REV_C1\4&3" ,"ram" :536870912 }, 255 {"name" :"AMD Radeon RX 7800 XT" ,"pnp" :r"PCI\VEN_1002&DEV_7499&SUBSYS_0000&REV_C8\6&4" ,"ram" :4293918720 }, 256 ] 257REGISTRY = [# the display class's driver instances: 64-bit VRAM size (as int, or as bytes) 258 {"DriverDesc" :"NVIDIA GeForce RTX 5070" ,"MatchingDeviceId" :r"PCI\VEN_10DE&DEV_2F04" , 259"HardwareInformation.qwMemorySize" :12 << 30 }, 260 {"DriverDesc" :"AMD Radeon RX 6600" ,"MatchingDeviceId" :r"PCI\VEN_1002&DEV_73FF" ,# a removed card 261"HardwareInformation.qwMemorySize" :8 << 30 }, 262 {"DriverDesc" :"AMD Radeon RX 9070 XT" ,"MatchingDeviceId" :r"PCI\VEN_1002&DEV_7550&REV_C0" , 263"HardwareInformation.qwMemorySize" : (16 << 30 ).to_bytes (8 ,"little" ),"DriverVersion" :"32.0.21013.1000" }, 264 {"DriverDesc" :"AMD Radeon(TM) Graphics" ,"MatchingDeviceId" :r"PCI\VEN_1002&DEV_164E&REV_C1" , 265"HardwareInformation.qwMemorySize" :512 << 20 }, 266 {"DriverDesc" :"AMD Radeon RX 7800 XT" ,"MatchingDeviceId" :r"PCI\VEN_1002&DEV_7499" , 267"HardwareInformation.qwMemorySize" :16 << 30 }, 268 ] 269LIST = ("device 0: AMD Radeon(TM) Graphics\n arch gfx1036, 28.1 GiB, wave32\n" 270" cannot run: GPU 0 (AMD Radeon(TM) Graphics, gfx1036) is not an architecture this Strata engine was " 271"compiled for (gfx1100,gfx1101,gfx1102,gfx1200,gfx1201,gfx1030); ...\n" 272"device 1: AMD Radeon RX 9070 XT\n arch gfx1201, 15.9 GiB, wave32\n" 273"device 2: AMD Radeon RX 7800 XT\n arch gfx1101, 16.0 GiB, wave32\n" ) 274 275def test_adapters (self ): 276g = setup .amd_gpus_windows (self .ADAPTERS ,self .REGISTRY ) 277self .assertEqual ([x ["name" ]for x in g ], ["AMD Radeon RX 9070 XT" ,"AMD Radeon(TM) Graphics" , 278"AMD Radeon RX 7800 XT" ])# AMD only, present only, in order 279self .assertEqual ([x ["index" ]for x in g ], [0 ,1 ,2 ]) 280self .assertEqual (g [0 ]["arch" ],"gfx1201" )# by PCI device id 281self .assertEqual (g [2 ]["arch" ],"gfx1101" )# an unlisted id: by its name 282self .assertTrue (g [1 ]["arch" ].startswith ("unknown" ))# the iGPU: listed, not supported 283self .assertAlmostEqual (g [0 ]["vram_gb" ],16.0 )# the registry's 64-bit size 284self .assertEqual (g [0 ]["driver" ],"32.0.21013.1000" ) 285self .assertEqual ([setup .amd_problem (x )is None for x in g ], [True ,False ,True ]) 286 287def test_rx_6800m_is_gfx1031 (self ): 288"""#881: PCI 73DF (RX 6700 XT / 6750 XT / 6800M) is gfx1031, not an unknown id.""" 289self .assertEqual (setup .win_amd_arch (0x73DF ,"AMD Radeon RX 6800M" ),"gfx1031" ) 290self .assertIsNone (setup .amd_problem ({"arch" :"gfx1031" })) 291 292def test_cpu_vision_build_uses_the_visual_studio_environment (self ): 293"""#881: build_vision_cpu on Windows hands cmake_build the vcvars file and a real .bat name (it passed None and 294an empty name before).""" 295calls = [] 296with tempfile .TemporaryDirectory ()as d ,mock .patch .object (setup ,"WIN" ,True ),mock .patch .object (setup ,"find_vcvars" ,return_value = Path ("C:/vs/vcvars64.bat" )),mock .patch .object (setup ,"cmake_build" ,lambda * a :calls .append (a )),mock .patch .object (setup ,"ROOT" ,Path (d )),mock .patch .object (setup .shutil ,"copy2" ): 297eng = Path (d )/ "engine" 298eng .mkdir () 299setup .build_vision_cpu (eng ,eng / "BUILD.json" , {},Path ("llama" ),"src1" ) 300self .assertEqual (calls [0 ][4 ],Path ("C:/vs/vcvars64.bat" )) 301self .assertTrue (calls [0 ][5 ].endswith (".bat" )) 302 303def test_registry_alone (self ): 304"""No WMI answer: the registry's own list (which can hold a removed card).""" 305g = setup .amd_gpus_windows ([],self .REGISTRY ) 306self .assertEqual ([x ["arch" ]for x in g if not x ["arch" ].startswith ("unknown" )], ["gfx1201" ,"gfx1101" ]) 307 308def test_arch_names (self ): 309for name ,arch in (("AMD Radeon RX 9070 GRE" ,"gfx1201" ), ("AMD Radeon AI PRO R9700" ,"gfx1201" ), 310 ("AMD Radeon RX 9060 XT" ,"gfx1200" ), ("AMD Radeon RX 7900 GRE" ,"gfx1100" ), 311 ("AMD Radeon PRO W7800" ,"gfx1100" ), ("AMD Radeon RX 7700 XT" ,"gfx1101" ), 312 ("AMD Radeon RX 7600" ,"gfx1102" ), ("AMD Radeon RX 6950 XT" ,"gfx1030" ), 313 ("AMD Radeon RX 6800M" ,"" ), ("AMD Radeon 780M Graphics" ,"gfx1103" ), ("AMD Radeon RX 7700S" ,"" )): 314self .assertEqual (setup .win_amd_arch (None ,name ),arch ,name ) 315self .assertEqual (setup .win_amd_arch (0x744C ,"whatever" ),"gfx1100" ) 316 317def test_list_devices (self ): 318h = setup .hip_devices (text = self .LIST ) 319self .assertEqual ([(x ["index" ],x ["arch" ])for x in h ], [(0 ,"gfx1036" ), (1 ,"gfx1201" ), (2 ,"gfx1101" )]) 320self .assertAlmostEqual (h [1 ]["vram_gb" ],15.9 ) 321self .assertIn ("not an architecture" ,h [0 ]["cannot_run" ]) 322self .assertIsNotNone (setup .amd_problem (h [0 ])) 323self .assertIsNone (setup .amd_problem (h [1 ])) 324cannot = setup .hip_devices (text = "device 0: AMD Radeon RX 9070 XT\n arch gfx1201, 15.9 GiB, wave32\n" 325" cannot run: GPU 0 runs wave64\n" ) 326self .assertEqual (setup .amd_problem (cannot [0 ]),"GPU 0 runs wave64" )# the engine's own check counts 327self .assertEqual (setup .hip_devices (text = "(no GPU device)\n" ), []) 328 329def test_the_card_by_its_hip_ordinal (self ): 330"""#325: setup's GPU 0 (the 9070 XT, display order) is HIP's device 1 behind the iGPU.""" 331listed = setup .amd_gpus_windows (self .ADAPTERS ,self .REGISTRY ) 332hip = setup .hip_devices (text = self .LIST ) 333self .assertEqual (setup .hip_match (listed [0 ],listed ,hip )["index" ],1 ) 334self .assertEqual (setup .hip_match (listed [2 ],listed ,hip )["index" ],2 ) 335two = [{"index" :0 ,"arch" :"gfx1201" }, {"index" :1 ,"arch" :"gfx1201" }]# two of a kind: by rank 336self .assertEqual (setup .hip_match (two [1 ],two , [{"index" :0 ,"arch" :"gfx1036" }, {"index" :1 ,"arch" :"gfx1201" }, 337 {"index" :2 ,"arch" :"gfx1201" }])["index" ],2 ) 338self .assertIsNone (setup .hip_match ({"index" :0 ,"arch" :"gfx1100" }, [{"index" :0 ,"arch" :"gfx1100" }],hip )) 339 340def test_hip_card (self ): 341listed = setup .amd_gpus_windows (self .ADAPTERS ,self .REGISTRY ) 342with mock .patch .object (setup ,"hip_devices" ,lambda probe = None ,text = None :setup_hip (self .LIST )), \ 343mock .patch .object (setup ,"ok" ,lambda * a :None ): 344g = setup .hip_card (Path ("engine" ),listed [0 ],listed ) 345self .assertEqual ((g ["index" ],g ["count" ],g ["arch" ],g ["vram_gb" ]), (1 ,3 ,"gfx1201" ,15.9 )) 346with mock .patch .object (setup ,"hip_devices" ,lambda probe = None ,text = None : []), \ 347mock .patch .object (setup ,"say" ,lambda * a ,** k :None ),self .assertRaises (SystemExit ): 348setup .hip_card (Path ("engine" ),listed [0 ],listed )# no AMD driver: stops before the download 349with mock .patch .object (setup ,"hip_devices" ,lambda probe = None ,text = None :setup_hip (self .LIST )[:1 ]), \ 350mock .patch .object (setup ,"say" ,lambda * a ,** k :None ),self .assertRaises (SystemExit ): 351setup .hip_card (Path ("engine" ),listed [0 ],listed )# HIP does not list the card 352 353def test_windows_dispatch (self ): 354"""amd_gpus() on Windows: HIP's numbering once the HIP engine answers, the display adapters before.""" 355with mock .patch .object (setup ,"WIN" ,True ), \ 356mock .patch .object (setup ,"hip_devices" ,lambda probe = None ,text = None :None ), \ 357mock .patch .object (setup ,"amd_gpus_windows" ,lambda adapters = None ,registry = None : ["display" ]): 358self .assertEqual (setup .amd_gpus (), ["display" ]) 359with mock .patch .object (setup ,"WIN" ,True ), \ 360mock .patch .object (setup ,"hip_devices" ,lambda probe = None ,text = None : ["hip" ]), \ 361mock .patch .object (setup ,"amd_gpus_windows" ,lambda adapters = None ,registry = None : ["display" ]): 362self .assertEqual (setup .amd_gpus (), ["hip" ]) 363 364def test_probe_in_the_component_folder (self ): 365"""#3418: the app keeps the engine in the component folder, not in control/engine; the cards must be numbered by the 366HIP runtime there too (an iGPU on ordinal 0 made the display-order "GPU 0" the wrong card).""" 367from strata_control import engine_component as EC 368name = "strata-device.exe" if setup .WIN else "strata-device" 369with tempfile .TemporaryDirectory ()as d : 370root ,comp = Path (d )/ "control" ,Path (d )/ "components" / "0.2-hip" 371root .mkdir () 372comp .mkdir (parents = True ) 373 (comp / name ).write_text ("x" ) 374 (comp / "engine-component.json" ).write_text ('{"backend": "hip"}' )# no BUILD.json in this package 375found = mock .Mock (root = comp ) 376with mock .patch .object (setup ,"ROOT" ,root ),mock .patch .object (EC ,"resolve" ,return_value = found )as res : 377self .assertEqual (setup .hip_probe_path (),comp / name ) 378self .assertEqual (res .call_args .kwargs ["backend" ],"hip" ) 379ran = mock .Mock (returncode = 0 ,stdout = self .LIST ) 380with mock .patch .object (setup .subprocess ,"run" ,return_value = ran )as run , \ 381mock .patch .object (setup ,"hip_runtime_beside_exe" ,lambda * a :None ): 382cards = setup .hip_devices () 383self .assertEqual (run .call_args .args [0 ][0 ],str (comp / name )) 384self .assertEqual ([c ["index" ]for c in cards ], [0 ,1 ,2 ]) 385 (root / "engine" ).mkdir () 386 (root / "engine" / name ).write_text ("x" ) 387with mock .patch .object (setup ,"ROOT" ,root ),mock .patch .object (EC ,"resolve" ,side_effect = AssertionError ): 388self .assertEqual (setup .hip_probe_path (),root / "engine" / name )# a checkout's own engine wins 389with mock .patch .object (setup ,"ROOT" ,Path (d )/ "none" ), \ 390mock .patch .object (EC ,"resolve" ,side_effect = EC .EngineMissing ("none" )): 391self .assertFalse (setup .hip_probe_path ().exists ())# no engine anywhere: no probe 392 393def test_no_hip_engine_no_probe (self ): 394with tempfile .TemporaryDirectory ()as d : 395eng = Path (d ) 396 (eng / "strata-device.exe" ).write_text ("x" ) 397 (eng / "BUILD.json" ).write_text ('{"backend": "cuda"}' )# an NVIDIA engine is never asked 398self .assertIsNone (setup .hip_devices (eng / "strata-device.exe" )) 399 400def test_prebuilt_hip_zip (self ): 401"""get_prebuilt_hip: a published zip (here a local folder) is unpacked into engine/; one without code for the 402card, or older than the first Windows HIP release, is refused.""" 403import json 404import zipfile 405with tempfile .TemporaryDirectory ()as d : 406root = Path (d ) 407pub = root / "pub" 408pub .mkdir () 409 410def publish (meta ): 411with zipfile .ZipFile (pub / setup .WIN_HIP_ASSET ,"w" )as z : 412z .writestr (setup .EXE ,"engine" )# #975: "strata" on Linux 413z .writestr ("strata-device.exe" ,"probe" ) 414z .writestr ("rocm/bin/amdhip64_7.dll" ,"dll" ) 415z .writestr ("BUILD.json" ,json .dumps (meta )) 416ver = "." .join (map (str ,setup .WIN_HIP_MIN_ENGINE )) 417good = {"source" :"prebuilt" ,"backend" :"hip" ,"version" :ver ,"archs" : ["gfx1100" ,"gfx1201" ], 418"lib_dirs" : ["rocm/bin" ]} 419with mock .patch .object (setup ,"ROOT" ,root ),mock .patch .object (setup ,"say" ,lambda * a ,** k :None ), \ 420mock .patch .object (setup ,"ok" ,lambda * a :None ),mock .patch .object (setup ,"warn" ,lambda * a :None ): 421publish ({** good ,"archs" : ["gfx1100" ]}) 422self .assertIsNone (setup .get_prebuilt_hip (str (pub )+ "/" , {"arch" :"gfx1201" })) 423publish ({** good ,"version" :"0.1.30" }) 424self .assertIsNone (setup .get_prebuilt_hip (str (pub )+ "/" , {"arch" :"gfx1201" })) 425publish (good ) 426eng = setup .get_prebuilt_hip (str (pub )+ "/" , {"arch" :"gfx1201" }) 427self .assertEqual (eng ,root / "engine" ) 428self .assertTrue ((eng / "rocm" / "bin" / "amdhip64_7.dll" ).exists ()) 429self .assertEqual (setup .hip_lib_dirs (eng ), [eng / "rocm" / "bin" ]) 430 (pub / setup .WIN_HIP_ASSET ).unlink ()# installed: kept, nothing downloaded again 431self .assertEqual (setup .get_prebuilt_hip (str (pub )+ "/" , {"arch" :"gfx1201" }),eng ) 432 433def test_prebuilt_hip_zip_archs_as_list_or_string (self ): 434"""#3810 review: gfx names are names, not numbers - an installed ready-made engine is recognised (no download again) and a zip 435is accepted whether BUILD.json writes archs as a list or as the ';'-separated string.""" 436import json 437import zipfile 438for archs in (["gfx1100" ,"gfx1151" ],"gfx1100;gfx1151" ): 439with tempfile .TemporaryDirectory ()as d : 440root = Path (d ) 441pub = root / "pub" 442pub .mkdir () 443ver = "." .join (map (str ,setup .WIN_HIP_MIN_ENGINE )) 444meta = {"source" :"prebuilt" ,"backend" :"hip" ,"version" :ver ,"archs" :archs ,"lib_dirs" : ["rocm/bin" ]} 445with zipfile .ZipFile (pub / setup .WIN_HIP_ASSET ,"w" )as z : 446z .writestr (setup .EXE ,"engine" ) 447z .writestr ("strata-device.exe" ,"probe" ) 448z .writestr ("rocm/bin/amdhip64_7.dll" ,"dll" ) 449z .writestr ("BUILD.json" ,json .dumps (meta )) 450with mock .patch .object (setup ,"ROOT" ,root ),mock .patch .object (setup ,"say" ,lambda * a ,** k :None ),mock .patch .object (setup ,"ok" ,lambda * a :None ),mock .patch .object (setup ,"warn" ,lambda * a :None ): 451eng = setup .get_prebuilt_hip (str (pub )+ "/" , {"arch" :"gfx1151" }) 452self .assertEqual (eng ,root / "engine" ,archs ) 453 (pub / setup .WIN_HIP_ASSET ).unlink ()# installed: recognised, nothing downloaded again 454self .assertEqual (setup .get_prebuilt_hip (str (pub )+ "/" , {"arch" :"gfx1151" }),eng ,archs ) 455self .assertEqual (setup .engine_archs_hip (), ["gfx1100" ,"gfx1151" ],archs ) 456 457 458_HIP_DEVICES = setup .hip_devices # the real parser, for the tests that mock setup.hip_devices 459 460 461def setup_hip (text ): 462return _HIP_DEVICES (text = text ) 463 464 465class CalibrationKey (unittest .TestCase ): 466"""#566: a calibration is saved and found again per PC and model (hardware_key). A HIP config's cards are AMD's, 467in HIP's numbering - nvidia-smi named them "?" (or another card with that number) before - and setup reuses a 468saved calibration on Linux HIP. The NVIDIA key and the settings file's format are unchanged.""" 469AMD = [{"index" :0 ,"name" :"AMD Radeon RX 7900 XTX" ,"vram_gb" :23.98 ,"arch" :"gfx1100" }, 470 {"index" :1 ,"name" :"AMD Radeon RX 7900 XT" ,"vram_gb" :19.98 ,"arch" :"gfx1100" }, 471 {"index" :2 ,"name" :"AMD Radeon (gfx1201)" ,"vram_gb" :15.92 ,"arch" :"gfx1201" }, 472 {"index" :3 ,"name" :"AMD Radeon (gfx1036)" ,"vram_gb" :0.5 ,"arch" :"gfx1036" }] 473 474def setUp (self ): 475self .patches = [mock .patch .object (setup ,"amd_gpus" ,lambda * a ,** k : [dict (g )for g in self .AMD ]), 476mock .patch .object (setup ,"cpu_info" ,lambda : ("AMD Ryzen 9 7950X" ,16 )), 477mock .patch .object (setup ,"ram_gb" ,lambda :63.6 )] 478for p in self .patches : 479p .start () 480 481def tearDown (self ): 482for p in self .patches : 483p .stop () 484 485@staticmethod 486def cfg (gpu ,backend = "hip" ,ctx = "32768" ): 487c = {"model_name" :"qwen3.8-flash-next-iq3_xxs" ,"args" : ["--max-context" ,ctx ,"--kv" ,"int8" ],"gpu" :gpu } 488if backend : 489c ["backend" ]= backend 490return c 491 492def test_hip_key_names_the_amd_card_and_its_arch (self ): 493def no_nvidia (* a ,** k ):# a HIP config never asks nvidia-smi (a PC with both kinds) 494raise AssertionError ("gpu_info called for a HIP config" ) 495with mock .patch .object (setup ,"gpu_info" ,no_nvidia ): 496self .assertEqual (setup .hardware_key (self .cfg (0 )), 497"AMD Radeon RX 7900 XTX (gfx1100)|24GB|AMD Ryzen 9 7950X|64GB|qwen3.8-flash-next-iq3_xxs|" 498"32768|text" ) 499# the arch is not repeated when the name already holds it (no product name in sysfs) 500self .assertTrue (setup .hardware_key (self .cfg (2 )).startswith ("AMD Radeon (gfx1201)|16GB|" )) 501 502def test_two_cards_of_one_arch_are_two_keys (self ): 503keys = {setup .hardware_key (self .cfg (i ))for i in (0 ,1 ,2 )} 504self .assertEqual (len (keys ),3 ) 505 506def test_a_split_names_every_card (self ): 507self .assertTrue (setup .hardware_key (self .cfg ([1 ,0 ])).startswith ( 508"AMD Radeon RX 7900 XT (gfx1100) + AMD Radeon RX 7900 XTX (gfx1100)|44GB|" )) 509 510def test_no_gpu_in_the_config_is_the_supported_card_with_the_most_vram (self ): 511self .assertTrue (setup .hardware_key (self .cfg (None )).startswith ("AMD Radeon RX 7900 XTX (gfx1100)|24GB|" )) 512 513def test_a_card_that_is_gone_is_unknown (self ): 514self .assertTrue (setup .hardware_key (self .cfg (7 )).startswith ("?|0GB|" )) 515 516def test_nvidia_key_unchanged (self ): 517def amd (* a ,** k ): 518raise AssertionError ("amd_gpus called for an NVIDIA config" ) 519with mock .patch .object (setup ,"amd_gpus" ,amd ), \ 520mock .patch .object (setup ,"gpu_info" ,lambda i = None : {"name" :"NVIDIA GeForce RTX 5070" ,"vram_gb" :11.94 }): 521self .assertEqual (setup .hardware_key (self .cfg (0 ,backend = None )), 522"NVIDIA GeForce RTX 5070|12GB|AMD Ryzen 9 7950X|64GB|qwen3.8-flash-next-iq3_xxs|32768|text" ) 523self .assertEqual (setup .hardware_key (self .cfg ([0 ,1 ],backend = "cuda" )), 524"NVIDIA GeForce RTX 5070 + NVIDIA GeForce RTX 5070|24GB|AMD Ryzen 9 7950X|64GB|" 525"qwen3.8-flash-next-iq3_xxs|32768|text" ) 526 527def test_nvidia_string_dual_keys_like_the_list (self ): 528"""A dual config's string "0,1" keys like [0, 1] (calibrate died with "there is no GPU 0,1").""" 529def amd (* a ,** k ): 530raise AssertionError ("amd_gpus called for an NVIDIA config" ) 531with mock .patch .object (setup ,"amd_gpus" ,amd ), \ 532mock .patch .object (setup ,"gpu_info" ,lambda i = None : {"name" :"NVIDIA GeForce RTX 5070" ,"vram_gb" :11.94 }): 533self .assertEqual (setup .hardware_key (self .cfg ("0,1" ,backend = "cuda" )), 534setup .hardware_key (self .cfg ([0 ,1 ],backend = "cuda" ))) 535self .assertEqual (setup .gpu_sel ("0,1" ), [0 ,1 ]) 536self .assertEqual (setup .gpu_sel ("0" ), [0 ]) 537asked = [] 538with mock .patch .object (setup ,"amd_gpus" ,amd ), \ 539mock .patch .object (setup ,"gpu_info" ,lambda i = None :asked .append (i )or {"name" :"NVIDIA GeForce RTX 5070" ,"vram_gb" :11.94 }): 540setup .hardware_key (self .cfg (1 ,backend = "cuda" ))# a single card given as a number still asks for that card 541self .assertEqual (asked , [1 ]) 542 543def test_setup_reuses_a_saved_calibration_on_linux_hip (self ): 544cfg = self .cfg (1 ) 545saved = {"settings" : {"pcie_frac" :0.0 },"tok_s" :40.1 ,"date" :"2026-10-03" } 546other = {"settings" : {"pcie_frac" :0.5 },"tok_s" :50.0 ,"date" :"2026-10-02" } 547store = {"calibration" : {setup .hardware_key (cfg ):saved ,setup .hardware_key (self .cfg (0 )):other }} 548with mock .patch .object (setup ,"load_settings" ,lambda :store ): 549with mock .patch .object (setup ,"WIN" ,False ): 550self .assertEqual (setup .setup_calibration (cfg ,hip = True ),saved )# its own card's, not the XTX's 551self .assertIsNone (setup .setup_calibration (self .cfg (2 ),hip = True ))# never tuned on that card 552with mock .patch .object (setup ,"WIN" ,True ): 553self .assertIsNone (setup .setup_calibration (cfg ,hip = True ))# Windows HIP: defaults for now 554 555 556class HipVisionWish (unittest .TestCase ): 557"""AMD pictures are on by default, on Linux and Windows: the wish is "gpu" (hip_reader() settles it against the readers the 558engine folder carries: the HIP one, else the CPU one), "cpu" when asked for, "none" for no.""" 559 560def test_wish (self ): 561for win in (True ,False ): 562with mock .patch .object (setup ,"WIN" ,win ): 563self .assertEqual (setup .hip_vision (None ),"gpu" ) 564self .assertEqual (setup .hip_vision ("yes" ),"gpu" ) 565self .assertEqual (setup .hip_vision ("gpu" ),"gpu" ) 566self .assertEqual (setup .hip_vision ("cpu" ),"cpu" ) 567self .assertEqual (setup .hip_vision ("no" ),"none" ) 568self .assertEqual (setup .hip_vision ("none" ),"none" ) 569# owner decision 2026-10-18: nothing asked / yes = the CPU reader under 24 GB, the GPU reader from 24 GB; gpu / cpu are kept 570self .assertEqual (setup .hip_vision (None ,16.0 ),"cpu" ) 571self .assertEqual (setup .hip_vision ("yes" ,16.0 ),"cpu" ) 572self .assertEqual (setup .hip_vision ("yes" ,23.9 ),"gpu" ) 573self .assertEqual (setup .hip_vision (None ,32.0 ),"gpu" ) 574self .assertEqual (setup .hip_vision ("gpu" ,16.0 ),"gpu" ) 575self .assertEqual (setup .hip_vision ("cpu" ,32.0 ),"cpu" ) 576 577def test_reader_settled_against_the_engine_folder (self ): 578for win ,suffix in ((False ,"" ), (True ,".exe" )): 579with tempfile .TemporaryDirectory ()as d ,mock .patch .object (setup ,"WIN" ,win ),mock .patch .object (setup ,"warn" ,lambda * a :None ): 580eng = Path (d ) 581 (eng / ("strata-vision" + suffix )).write_bytes (b"x" ) 582self .assertEqual (setup .hip_reader (eng ,"gpu" , {"arch" :"gfx1201" })[0 ],"cpu" )# no HIP reader: the CPU one 583 (eng / ("strata-vision-hip" + suffix )).write_bytes (b"x" ) 584mode ,r = setup .hip_reader (eng ,"gpu" , {"arch" :"gfx1201" }) 585self .assertEqual ((mode ,r .exe .name ,r .cpu_exe .name ), 586 ("gpu" ,"strata-vision-hip" + suffix ,"strata-vision" + suffix )) 587 (eng / "BUILD.json" ).write_text ('{"vision_archs": ["gfx1100"]}' ) 588self .assertEqual (setup .hip_reader (eng ,"gpu" , {"arch" :"gfx1201" })[0 ],"cpu" )# the card is not in its build 589self .assertEqual (setup .hip_reader (eng ,"gpu" , {"arch" :"gfx1100" })[0 ],"gpu" ) 590self .assertEqual (setup .hip_reader (eng ,"cpu" , {"arch" :"gfx1100" })[0 ],"cpu" ) 591 (eng / ("strata-vision" + suffix )).unlink () 592 (eng / ("strata-vision-hip" + suffix )).unlink () 593self .assertEqual (setup .hip_reader (eng ,"gpu" , {"arch" :"gfx1100" })[0 ],"none" ) 594 595 596class HipRuntimeBesideExe (unittest .TestCase ): 597"""#468 #461: the bundled HIP runtime (and amd_comgr) goes next to strata.exe, so an AMD driver's System32 copy is 598not found first; rocBLAS and the rest stay in rocm/bin.""" 599 600def test_copies_only_the_runtime (self ): 601import json 602with tempfile .TemporaryDirectory ()as d : 603eng = Path (d ) 604rb = eng / "rocm" / "bin" 605rb .mkdir (parents = True ) 606 (eng / "BUILD.json" ).write_text (json .dumps ({"backend" :"hip" ,"lib_dirs" : ["rocm/bin" ]})) 607for n ,data in (("amdhip64_7.dll" ,b"hip-3686" ), ("amd_comgr.dll" ,b"comgr" ), ("rocblas.dll" ,b"blas" )): 608 (rb / n ).write_bytes (data ) 609setup .hip_runtime_beside_exe (eng ) 610self .assertEqual ((eng / "amdhip64_7.dll" ).read_bytes (),b"hip-3686" ) 611self .assertEqual ((eng / "amd_comgr.dll" ).read_bytes (),b"comgr" ) 612self .assertFalse ((eng / "rocblas.dll" ).exists ())# finds its kernels relative to rocm/bin 613 (rb / "amdhip64_7.dll" ).write_bytes (b"hip-3690-newer" )# a newer zip: replaced on the next start 614setup .hip_runtime_beside_exe (eng ) 615self .assertEqual ((eng / "amdhip64_7.dll" ).read_bytes (),b"hip-3690-newer" ) 616 617def test_no_rocm_bin_is_a_no_op (self ): 618with tempfile .TemporaryDirectory ()as d : 619setup .hip_runtime_beside_exe (Path (d ))# no BUILD.json (a CUDA or Linux engine) 620self .assertEqual (list (Path (d ).iterdir ()), []) 621 622 623class DeviceAccess (unittest .TestCase ): 624"""Linux AMD: /dev/kfd and the render nodes must be openable by the user; setup warns, never refuses.""" 625 626def dev (self ,d ,kfd = True ,nodes = ("renderD128" ,)): 627root = Path (d ) 628 (root / "dri" ).mkdir () 629if kfd : 630 (root / "kfd" ).write_text ("" ) 631for n in nodes : 632 (root / "dri" / n ).write_text ("" ) 633return str (root ) 634 635def test_accessible_is_silent (self ): 636with tempfile .TemporaryDirectory ()as d : 637self .assertIsNone (setup .amd_device_access_problem (self .dev (d ),access = lambda p ,m :True )) 638 639def test_no_kfd_is_not_this_problem (self ): 640with tempfile .TemporaryDirectory ()as d : 641self .assertIsNone (setup .amd_device_access_problem (self .dev (d ,kfd = False ),access = lambda p ,m :False )) 642 643def test_kfd_denied_names_the_fix (self ): 644with tempfile .TemporaryDirectory ()as d : 645msg = setup .amd_device_access_problem (self .dev (d ),access = lambda p ,m :not p .endswith ("kfd" )) 646self .assertIn ("kfd" ,msg ) 647self .assertNotIn ("renderD128" ,msg ) 648self .assertIn ("sudo usermod -aG render,video $USER" ,msg ) 649self .assertIn ("log out and in" ,msg ) 650 651def test_render_node_denied (self ): 652with tempfile .TemporaryDirectory ()as d : 653msg = setup .amd_device_access_problem (self .dev (d ),access = lambda p ,m :"renderD" not in p ) 654self .assertIn ("renderD128" ,msg ) 655self .assertNotIn ("/kfd" ,msg ) 656 657def test_engine_names_the_permission_not_another_program (self ): 658src = (Path (setup .__file__ ).resolve ().parent / "src/program/generate.cpp" ).read_text (encoding = "utf-8" ) 659i = src .index ("cannot open /dev/kfd" ) 660guard = src [src .rindex ("#if" ,0 ,i ):i ] 661self .assertIn ("cudaGetDeviceCount" ,guard ) 662self .assertIn ('access("/dev/kfd", R_OK | W_OK)' ,guard ) 663self .assertIn ("STRATA_USE_HIP" ,guard )# CUDA builds keep the "another program" text 664self .assertIn ("another program (or an engine that is still exiting)" ,src ) 665 666 667class EngineLaunchEnv (unittest .TestCase ): 668"""#2372: every engine program setup runs gets the ready-made HIP engine's rocm/bin on PATH, or Windows shows a 669"hipblas.dll not found" dialog per DLL and `strata --families` prints nothing (setup then said the engine does 670not run Qwen3.6).""" 671 672def engine (self ,tmp :Path )-> Path : 673eng = tmp / "engine" 674 (eng / "rocm" / "bin" ).mkdir (parents = True ) 675 (eng / "BUILD.json" ).write_text ('{"backend": "hip", "lib_dirs": ["rocm/bin"]}' ,encoding = "utf-8" ) 676return eng 677 678def test_engine_env_puts_rocm_bin_first_on_windows (self ): 679with tempfile .TemporaryDirectory ()as t : 680eng = self .engine (Path (t )) 681with mock .patch .dict ("os.environ" , {"PATH" :"C:\\Windows" }), \ 682mock .patch .object (setup .os ,"name" ,"nt" ): 683env = setup .engine_env (eng ) 684self .assertEqual (env ["PATH" ].split (setup .os .pathsep )[0 ],str (eng / "rocm" / "bin" )) 685self .assertTrue (env ["PATH" ].endswith ("C:\\Windows" )) 686 687def test_engine_env_puts_rocm_bin_first_on_ld_library_path (self ): 688with tempfile .TemporaryDirectory ()as t : 689eng = self .engine (Path (t )) 690with mock .patch .dict ("os.environ" , {"PATH" :"/usr/bin" ,"LD_LIBRARY_PATH" :"/usr/lib" }), \ 691mock .patch .object (setup .os ,"name" ,"posix" ): 692env = setup .engine_env (eng ) 693self .assertEqual (env ["LD_LIBRARY_PATH" ].split (setup .os .pathsep )[0 ],str (eng / "rocm" / "bin" )) 694self .assertTrue (env ["LD_LIBRARY_PATH" ].endswith ("/usr/lib" )) 695self .assertEqual (env ["PATH" ],"/usr/bin" ) 696 697def test_engine_without_lib_dirs_keeps_the_environment (self ): 698with tempfile .TemporaryDirectory ()as t : 699with mock .patch .dict ("os.environ" , {"PATH" :"/usr/bin" }): 700self .assertEqual (setup .engine_env (Path (t ))["PATH" ],"/usr/bin" ) 701 702def test_families_probe_runs_with_that_env (self ): 703seen = {} 704 705def fake_run (cmd ,** kw ): 706seen ["cmd" ],seen ["env" ]= cmd ,kw .get ("env" ) 707return mock .Mock (stdout = "qwen4exp qwen35moe\n" ) 708with tempfile .TemporaryDirectory ()as t : 709eng = self .engine (Path (t )) 710with mock .patch .object (setup .subprocess ,"run" ,fake_run ), \ 711mock .patch .object (setup .os ,"name" ,"nt" ): 712names = setup .engine_families (eng ) 713self .assertEqual (names , {"qwen4exp" ,"qwen35moe" }) 714self .assertEqual (seen ["cmd" ][1 ],"--families" ) 715self .assertEqual (seen ["env" ]["PATH" ].split (setup .os .pathsep )[0 ],str (eng / "rocm" / "bin" )) 716 717def test_device_probe_runs_with_that_env (self ): 718seen = {} 719 720def fake_run (cmd ,** kw ): 721seen ["env" ]= kw .get ("env" ) 722return mock .Mock (returncode = 0 ,stdout = "device 0: AMD Radeon RX 7900 XT\n arch gfx1100, 20.0 GiB, wave32\n" ) 723with tempfile .TemporaryDirectory ()as t : 724eng = self .engine (Path (t )) 725probe = eng / ("strata-device.exe" if setup .WIN else "strata-device" ) 726probe .write_text ("x" ) 727with mock .patch .object (setup .subprocess ,"run" ,fake_run ),mock .patch .object (setup ,"hip_runtime_beside_exe" ,lambda d :None ),mock .patch .object (setup .os ,"name" ,"nt" ): 728found = setup .hip_devices (probe ) 729self .assertEqual (found [0 ]["arch" ],"gfx1100" ) 730self .assertEqual (seen ["env" ]["PATH" ].split (setup .os .pathsep )[0 ],str (eng / "rocm" / "bin" )) 731 732 733class TdrPointer (unittest .TestCase ): 734def test_setup_points_windows_gfx12_to_the_entry (self ): 735src = Path (setup .__file__ ).read_text (encoding = "utf-8" ) 736self .assertIn ('WIN and str(gpu.get("arch") or "").startswith("gfx12")' ,src ) 737doc = (Path (setup .__file__ ).resolve ().parent / "docs/TROUBLESHOOTING.md" ).read_text (encoding = "utf-8" ) 738self .assertIn ("Windows AMD: the driver resets" ,doc ) 739for env in ("STRATA_PF_STEP_SYNC" ,"STRATA_KV_HOST_DMA" ): 740self .assertIn (env ,doc ) 741self .assertIn (env , (Path (setup .__file__ ).resolve ().parent / "src/prefill/prefill.cpp" ).read_text (encoding = "utf-8" )) 742 743 744if __name__ == "__main__" : 745unittest .main ()