okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ffb3d9b31
main
1"""Intel Arc engine package (owner test round 8, track L3): the stable feed had no `linux-x86_64-sycl` package, so a clean Arc PC got a 2Terminal that could not run a model and `strata update` kept saying the feed had no engine for it. The feed is whatever packages the release 3builds (packaging/feed_tool.py takes every `*.entry.json`), so the gap was the missing build recipe, not the choice code. These tests keep the 4choice honest once the package exists: an Arc PC (found by its PCI vendor, with no oneAPI tools installed) gets it by itself, nobody else does, 5and the recipe that makes it carries the checks that a clean PC needs.""" 6from __future__import annotations 7 8import shutil 9import subprocess 10import sys 11import tempfile 12import unittest 13from pathlib import Path 14from unittest import mock 15 16from strata_control import engine_component as ec ,feed as feedmod ,updater 17 18ROOT = Path (__file__ ).resolve ().parents [2 ] 19sys .path .insert (0 ,str (ROOT / "packaging" )) 20import package_engine # noqa: E402 21 22LINUX ,WIN = "linux-x86_64" ,"windows-x86_64" 23 24 25def entry (platform ,backend ,variant ,archs ,version = "0.1.47" ): 26return {"version" :version ,"platform" :platform ,"backend" :backend ,"variant" :variant ,"archs" :list (archs ),"min_driver" : {}, 27"min_runtime" :None ,"build_id" :f"rel- { platform } - { backend } - { variant } " ,"format" :"tar.gz" , 28"url" :f"strata-engine- { platform } - { backend } - { variant } .tar.gz" ,"size" :1000 ,"sha256" :"a" * 64 ,"manifest_sha256" :"b" * 64 , 29"installed_bytes" :2000 ,"notes" :"" } 30 31 32FEED = [feedmod ._engine_package (e )for e in ( 33entry (LINUX ,"cuda" ,"cuda13" , ("sm_75" ,"sm_86" ,"sm_89" ,"sm_120" ,"compute_120" )), 34entry (LINUX ,"hip" ,"rx9070" , ("gfx1200" ,"gfx1201" )), 35entry (WIN ,"cuda" ,"cuda13" , ("sm_86" ,)), 36entry (LINUX ,"sycl" ,"" , ("intel-arc" ,)))] 37 38 39def fake_drm (root :Path ,* vendors :str )-> str : 40"""A /sys/class/drm with one card per vendor id (plus a connector entry, which must be skipped).""" 41for i ,vendor in enumerate (vendors ): 42dev = root / f"card { i } " / "device" 43dev .mkdir (parents = True ) 44 (dev / "vendor" ).write_text (vendor + "\n" ) 45 (root / f"card { i } -HDMI-A-1" ).mkdir () 46return str (root ) 47 48 49class ArcChoice (unittest .TestCase ): 50def detect (self ,* vendors ,platform = LINUX ,environ = None ): 51"""What `updater.detect_hardware` says on a PC with these display vendors and none of the vendor tools installed.""" 52with tempfile .TemporaryDirectory ()as t : 53drm = fake_drm (Path (t ),* vendors ) 54real = ec ._drm_vendor_backend 55with mock .patch .object (ec .shutil ,"which" ,return_value = None ), \ 56mock .patch .object (ec ,"host_platform" ,return_value = platform ), \ 57mock .patch .object (updater ,"host_platform" ,return_value = platform ), \ 58mock .patch .object (ec ,"_drm_vendor_backend" ,lambda :real (drm )), \ 59mock .patch .object (updater ,"_gfx_names" ,return_value = []), \ 60mock .patch .object (updater ,"_amd_card_names" ,return_value = []), \ 61mock .patch .object (ec ,"windows_display_adapters" ,return_value = []),mock .patch .object (ec ,"_windows_vendor_backend" ,lambda :real (drm )):# the test PC may be Windows: either probe answers 62return updater .detect_hardware (environ or {}) 63 64def pick (self ,hardware ): 65best ,reasons = feedmod .select_engine (FEED ,hardware ) 66return best ,reasons 67 68def test_a_clean_arc_pc_without_oneapi_gets_the_sycl_package (self ): 69hw = self .detect ("0x8086" ) 70self .assertEqual ((hw ["platform" ],hw ["backend" ]), (LINUX ,"sycl" )) 71best ,_ = self .pick (hw ) 72self .assertEqual ((best .backend ,best .platform ,best .archs ), ("sycl" ,LINUX , ("intel-arc" ,))) 73 74def test_nvidia_and_amd_pcs_never_get_it_even_with_an_intel_igpu_beside_the_card (self ): 75for vendors ,want in ((("0x10de" ,),"cuda" ), (("0x8086" ,"0x10de" ),"cuda" ), (("0x8086" ,"0x1002" ),"hip" )): 76hw = self .detect (* vendors ) 77best ,_ = self .pick (hw ) 78self .assertEqual (best .backend ,want ,vendors ) 79 80def test_windows_has_no_intel_package_and_says_why (self ): 81hw = self .detect ("0x8086" ,platform = WIN ) 82best ,why = self .pick (hw ) 83self .assertIsNone (best ) 84self .assertTrue (any ("sycl" in r and WIN in r for r in why ),why ) 85self .assertEqual (ec .backend_supported ("sycl" ,WIN )[0 ],False ) 86self .assertEqual (ec .backend_supported ("sycl" ,LINUX )[0 ],True ) 87 88def test_the_package_is_not_offered_to_a_mac_or_to_a_forced_other_backend (self ): 89self .assertIsNone (self .pick ({"platform" :"macos-arm64" ,"backend" :"metal" ,"runtime" :"0.2.2" })[0 ]) 90best ,_ = self .pick (self .detect ("0x8086" ,environ = {ec .ENV_BACKEND :"cuda" })) 91self .assertEqual (best .backend ,"cuda" ) 92 93def test_an_older_sycl_package_does_not_beat_the_new_one_and_a_newer_one_wins (self ): 94packages = [feedmod ._engine_package (entry (LINUX ,"sycl" ,"" , ("intel-arc" ,),v ))for v in ("0.1.45" ,"0.1.47" ,"0.1.46" )] 95best ,_ = feedmod .select_engine (packages ,self .detect ("0x8086" )) 96self .assertEqual (best .version ,"0.1.47" ) 97 98def test_no_package_at_all_gives_the_plain_no_engine_reason (self ): 99best ,why = feedmod .select_engine ([p for p in FEED if p .backend != "sycl" ],self .detect ("0x8086" )) 100self .assertIsNone (best ) 101self .assertTrue (any ("needs sycl" in r for r in why ),why ) 102 103 104class Recipe (unittest .TestCase ): 105SCRIPT = ROOT / "packaging" / "build_sycl_linux.sh" 106 107def test_the_recipe_exists_and_is_valid_shell (self ): 108self .assertTrue (self .SCRIPT .is_file ()) 109sh = shutil .which ("sh" ) 110if sh : 111self .assertEqual (subprocess .run ([sh ,"-n" ,str (self .SCRIPT )],capture_output = True ).returncode ,0 ) 112 113def test_the_recipe_bundles_the_runtime_and_checks_a_clean_start (self ): 114text = self .SCRIPT .read_text (encoding = "utf-8" ) 115for needed in ("collect_libs.py" ,"libur_adapter_level_zero" ,"libur_adapter_opencl" ,"patchelf --set-rpath" , 116"package_engine.py --verify" ,"binary_deps.py" ,"env -i" ,"--version" ,"--backend sycl" ,"intel-arc" ): 117self .assertIn (needed ,text ) 118self .assertNotIn ("\r" ,text ,"a shell script with CRLF endings does not run on Linux" ) 119 120def test_a_sycl_package_needs_an_arch_tag_and_only_linux (self ): 121with tempfile .TemporaryDirectory ()as t : 122with self .assertRaises (SystemExit ): 123package_engine .build (Path (t ),"sycl" ,"0.1.47" ,LINUX ,Path (t )/ "out" ) 124self .assertIn (LINUX ,ec .BACKEND_MATRIX ["sycl" ]) 125self .assertNotIn (WIN ,ec .BACKEND_MATRIX ["sycl" ]) 126 127 128if __name__ == "__main__" : 129unittest .main ()