okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b0e897b41
main
1"""Things a user of the Terminal archive (no setup.py, no tools/calibrate.py, an engine package) hit on real boxes.""" 2 3from __future__import annotations 4 5import json 6import os 7import stat 8import sys 9import tempfile 10import unittest 11from pathlib import Path 12from unittest import mock 13 14ROOT = Path (__file__ ).resolve ().parents [2 ] 15sys .path .insert (0 ,str (ROOT )) 16 17from strata_control import calibration ,tune_apply ,tuning # noqa: E402 18from strata_control .profiles import ProfileError # noqa: E402 19from strata_control .targets import host_platform # noqa: E402 20 21 22class BooleanFlags (unittest .TestCase ): 23def test_a_boolean_flag_is_on_or_absent_never_given_a_value (self ): 24self .assertEqual (tuning .flag_argv ("--kv-grow" ,"1" ), ["--kv-grow" ]) 25self .assertEqual (tuning .flag_argv ("--kv-grow" ,"0" ), []) 26self .assertEqual (tuning .flag_argv ("--kv-grow" ,True ), ["--kv-grow" ]) 27self .assertEqual (tuning .flag_argv ("--kv-grow" ,False ), []) 28self .assertEqual (tuning .flag_argv ("--spec-min-p" ,"0.5" ), ["--spec-min-p" ,"0.5" ]) 29self .assertEqual (tuning .flag_argv ("--spec" ,"1" ), ["--spec" ,"1" ])# a numeric flag keeps its 1 30 31def test_a_candidate_with_kv_grow_off_does_not_become_kv_grow_0 (self ): 32self .assertEqual (tune_apply .args_to_argv ({"--kv-grow" :"0" ,"--kv" :"int8" }), ["--kv" ,"int8" ]) 33self .assertEqual (tune_apply .args_to_argv ({"--kv-grow" :"1" }), ["--kv-grow" ]) 34 35def test_the_harness_argv_follows_the_same_rule (self ): 36from strata_control import tune_engine 37harness = tune_engine .EngineHarness (Path ("engine" ), {"--kv-grow" :"0" ,"--pool-workers" :"0" ,"--host-core" :"first" }) 38argv = harness ._argv () 39self .assertNotIn ("--kv-grow" ,argv ) 40self .assertIn ("--pool-workers" ,argv ) 41self .assertEqual (argv [argv .index ("--pool-workers" )+ 1 ],"0" ) 42 43 44class EnginePackageEnv (unittest .TestCase ): 45def test_a_package_folder_goes_first_on_the_library_path (self ): 46with tempfile .TemporaryDirectory ()as d : 47folder = Path (d ) 48 (folder / "engine-component.json" ).write_text ("{}" ,encoding = "utf-8" ) 49 (folder / "strata" ).write_text ("x" ,encoding = "utf-8" ) 50with mock .patch .dict (os .environ , {"LD_LIBRARY_PATH" :"/old" ,"PATH" :"/old" }): 51env = tuning .engine_env (folder / "strata" ) 52var = "PATH" if os .name == "nt" else "LD_LIBRARY_PATH" 53self .assertTrue (env [var ].startswith (str (folder .resolve ()))) 54self .assertTrue (env [var ].endswith ("/old" )or env [var ].endswith ("old" )) 55 56def test_a_plain_engine_keeps_the_callers_environment (self ): 57with tempfile .TemporaryDirectory ()as d : 58 (Path (d )/ "strata" ).write_text ("x" ,encoding = "utf-8" ) 59self .assertIsNone (tuning .engine_env (Path (d )/ "strata" )) 60 61 62class CalibrateWithoutAFullFolder (unittest .TestCase ): 63def test_missing_setup_is_a_refusal_in_words_not_a_traceback (self ): 64with mock .patch .dict (os .environ , {},clear = False ): 65os .environ .pop ("STRATA_INSTALL_DIR" ,None ) 66with mock .patch ("importlib.import_module" ,side_effect = ImportError ("no module" )): 67with self .assertRaises (ProfileError )as ctx : 68calibration ._full_folder ("tools.calibrate" ) 69self .assertEqual (ctx .exception .code if hasattr (ctx .exception ,"code" )else 5 ,5 ) 70self .assertIn ("STRATA_INSTALL_DIR" ,str (ctx .exception )) 71self .assertIn ("tune run --mode api" ,str (ctx .exception )) 72 73def test_tool_availability_is_reported_for_the_plan (self ): 74with mock .patch .dict (os .environ , {},clear = False ): 75os .environ .pop ("STRATA_INSTALL_DIR" ,None ) 76with mock .patch ("importlib.util.find_spec" ,return_value = None ): 77self .assertFalse (calibration ._tools_available ()) 78with tempfile .TemporaryDirectory ()as d : 79 (Path (d )/ "tools" ).mkdir () 80 (Path (d )/ "setup.py" ).write_text ("" ,encoding = "utf-8" ) 81 (Path (d )/ "tools" / "calibrate.py" ).write_text ("" ,encoding = "utf-8" ) 82os .environ ["STRATA_INSTALL_DIR" ]= d 83with mock .patch ("importlib.util.find_spec" ,return_value = None ): 84self .assertTrue (calibration ._tools_available ()) 85 86 87@ unittest . skipIf (host_platform() == "linux-aarch64" , "there is no HIP engine for arm64 Linux (BACKEND_MATRIX)" ) 88class HipPackageForAnotherCard (unittest .TestCase ): 89"""A HIP package built for gfx1200/gfx1201 must not be chosen for a Phoenix iGPU (gfx1103): P2006 resolved it as usable.""" 90 91def _kfd (self ,root :Path ,versions ): 92for number ,value in enumerate (versions ): 93node = root / str (number ) 94node .mkdir () 95 (node / "properties" ).write_text ("cpu_cores_count 1\ngfx_target_version " + value + "\n" ,encoding = "utf-8" ) 96 97def test_gfx_target_version_becomes_the_gfx_name (self ): 98from strata_control import engine_component as ec 99with tempfile .TemporaryDirectory ()as d : 100self ._kfd (Path (d ), ["0" ,"110003" ,"120001" ,"90010" ,"100300" ]) 101self .assertEqual (ec .hip_archs_present (d ), ["gfx1103" ,"gfx1201" ,"gfx90a" ,"gfx1030" ]) 102self .assertEqual (ec .hip_archs_present ("/nonexistent/kfd" ), []) 103 104def test_a_package_without_the_cards_arch_is_not_chosen_and_one_with_it_is (self ): 105from strata_control import engine_component as ec 106from tests .terminal .test_engine_component import make_package ,rewrite 107with tempfile .TemporaryDirectory ()as d : 108root = Path (d ) 109folder = make_package (root ,backend = "hip" ) 110rewrite (folder ,schema_version = 2 ,archs = ["gfx1200" ,"gfx1201" ],build_id = "test" ) 111with mock .patch .object (ec ,"hip_archs_present" ,return_value = ["gfx1103" ]): 112with self .assertRaises (ec .EngineMissing )as ctx : 113ec .resolve (root ,backend = "hip" ) 114self .assertIn ("gfx1103" ,str (ctx .exception )) 115self .assertIn ("gfx1201" ,str (ctx .exception )) 116with mock .patch .object (ec ,"hip_archs_present" ,return_value = ["gfx1201" ]): 117self .assertEqual (ec .resolve (root ,backend = "hip" ).backend ,"hip" ) 118with mock .patch .object (ec ,"hip_archs_present" ,return_value = []):# cannot tell: not a refusal 119self .assertEqual (ec .resolve (root ,backend = "hip" ).backend ,"hip" ) 120 121 122class DeviceAccess (unittest .TestCase ): 123def test_a_render_node_the_user_cannot_open_is_named (self ): 124from strata_control import doctor 125if not sys .platform .startswith ("linux" ): 126self .skipTest ("Linux device nodes" ) 127with tempfile .TemporaryDirectory ()as d : 128 (Path (d )/ "dri" ).mkdir () 129node = Path (d )/ "dri" / "renderD128" 130node .write_text ("" ,encoding = "utf-8" ) 131os .chmod (node ,0 ) 132if os .access (node ,os .R_OK ): 133self .skipTest ("running as a user that can open anything" ) 134issue = doctor ._device_access_issue ("sycl" ,d ) 135self .assertEqual (issue ["code" ],"GPU_DEVICE_ACCESS" ) 136self .assertIn ("render" ,issue ["message" ]) 137os .chmod (node ,0o666 ) 138self .assertIsNone (doctor ._device_access_issue ("sycl" ,d )) 139self .assertIsNone (doctor ._device_access_issue ("cuda" ,d )) 140self .assertIsNone (doctor ._device_access_issue ("hip" ,tempfile .gettempdir ()+ "/none" )) 141 142 143class ArchiveContents (unittest .TestCase ): 144def test_the_fingerprint_table_models_import_reads_is_in_the_payload (self ): 145sys .path .insert (0 ,str (ROOT / "packaging" )) 146import package_terminal 147self .assertIn ("data/gguf_fingerprints.json" ,package_terminal .PAYLOAD ) 148self .assertTrue ((ROOT / "data" / "gguf_fingerprints.json" ).is_file ()) 149 150 151if __name__ == "__main__" : 152unittest .main ()