okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge main (87dfca0e7) into f-own-int: the 0.2.14 app branch, fixtures and CI fixes, the Qwen4-readiness tools, the shape-engine build scriptsffb3d9b31

main
6.6 KiB129 linesraw
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"):
26    return {"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 (
33    entry(LINUX, "cuda", "cuda13", ("sm_75", "sm_86", "sm_89", "sm_120", "compute_120")),
34    entry(LINUX, "hip", "rx9070", ("gfx1200", "gfx1201")),
35    entry(WIN, "cuda", "cuda13", ("sm_86",)),
36    entry(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)."""
41    for i, vendor in enumerate(vendors):
42        dev = root / f"card{i}" / "device"
43        dev.mkdir(parents=True)
44        (dev / "vendor").write_text(vendor + "\n")
45        (root / f"card{i}-HDMI-A-1").mkdir()
46    return str(root)
47
48
49class ArcChoice(unittest.TestCase):
50    def 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."""
52        with tempfile.TemporaryDirectory() as t:
53            drm = fake_drm(Path(t), *vendors)
54            real = ec._drm_vendor_backend
55            with mock.patch.object(ec.shutil, "which", return_value=None), \
56                    mock.patch.object(ec, "host_platform", return_value=platform), \
57                    mock.patch.object(updater, "host_platform", return_value=platform), \
58                    mock.patch.object(ec, "_drm_vendor_backend", lambda: real(drm)), \
59                    mock.patch.object(updater, "_gfx_names", return_value=[]), \
60                    mock.patch.object(updater, "_amd_card_names", return_value=[]), \
61                    mock.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
62                return updater.detect_hardware(environ or {})
63
64    def pick(self, hardware):
65        best, reasons = feedmod.select_engine(FEED, hardware)
66        return best, reasons
67
68    def test_a_clean_arc_pc_without_oneapi_gets_the_sycl_package(self):
69        hw = self.detect("0x8086")
70        self.assertEqual((hw["platform"], hw["backend"]), (LINUX, "sycl"))
71        best, _ = self.pick(hw)
72        self.assertEqual((best.backend, best.platform, best.archs), ("sycl", LINUX, ("intel-arc",)))
73
74    def test_nvidia_and_amd_pcs_never_get_it_even_with_an_intel_igpu_beside_the_card(self):
75        for vendors, want in ((("0x10de",), "cuda"), (("0x8086", "0x10de"), "cuda"), (("0x8086", "0x1002"), "hip")):
76            hw = self.detect(*vendors)
77            best, _ = self.pick(hw)
78            self.assertEqual(best.backend, want, vendors)
79
80    def test_windows_has_no_intel_package_and_says_why(self):
81        hw = self.detect("0x8086", platform=WIN)
82        best, why = self.pick(hw)
83        self.assertIsNone(best)
84        self.assertTrue(any("sycl" in r and WIN in r for r in why), why)
85        self.assertEqual(ec.backend_supported("sycl", WIN)[0], False)
86        self.assertEqual(ec.backend_supported("sycl", LINUX)[0], True)
87
88    def test_the_package_is_not_offered_to_a_mac_or_to_a_forced_other_backend(self):
89        self.assertIsNone(self.pick({"platform": "macos-arm64", "backend": "metal", "runtime": "0.2.2"})[0])
90        best, _ = self.pick(self.detect("0x8086", environ={ec.ENV_BACKEND: "cuda"}))
91        self.assertEqual(best.backend, "cuda")
92
93    def test_an_older_sycl_package_does_not_beat_the_new_one_and_a_newer_one_wins(self):
94        packages = [feedmod._engine_package(entry(LINUX, "sycl", "", ("intel-arc",), v)) for v in ("0.1.45", "0.1.47", "0.1.46")]
95        best, _ = feedmod.select_engine(packages, self.detect("0x8086"))
96        self.assertEqual(best.version, "0.1.47")
97
98    def test_no_package_at_all_gives_the_plain_no_engine_reason(self):
99        best, why = feedmod.select_engine([p for p in FEED if p.backend != "sycl"], self.detect("0x8086"))
100        self.assertIsNone(best)
101        self.assertTrue(any("needs sycl" in r for r in why), why)
102
103
104class Recipe(unittest.TestCase):
105    SCRIPT = ROOT / "packaging" / "build_sycl_linux.sh"
106
107    def test_the_recipe_exists_and_is_valid_shell(self):
108        self.assertTrue(self.SCRIPT.is_file())
109        sh = shutil.which("sh")
110        if sh:
111            self.assertEqual(subprocess.run([sh, "-n", str(self.SCRIPT)], capture_output=True).returncode, 0)
112
113    def test_the_recipe_bundles_the_runtime_and_checks_a_clean_start(self):
114        text = self.SCRIPT.read_text(encoding="utf-8")
115        for 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"):
117            self.assertIn(needed, text)
118        self.assertNotIn("\r", text, "a shell script with CRLF endings does not run on Linux")
119
120    def test_a_sycl_package_needs_an_arch_tag_and_only_linux(self):
121        with tempfile.TemporaryDirectory() as t:
122            with self.assertRaises(SystemExit):
123                package_engine.build(Path(t), "sycl", "0.1.47", LINUX, Path(t) / "out")
124        self.assertIn(LINUX, ec.BACKEND_MATRIX["sycl"])
125        self.assertNotIn(WIN, ec.BACKEND_MATRIX["sycl"])
126
127
128if __name__ == "__main__":
129    unittest.main()