okami.mom/strata

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

Niko1221Two GTX 1080s are a working PC: an 8 GB card starts Off only beside a bigger card, and a PC whose only NVIDIA cards are Pascal uses them on the CUDA 12 engine (#3655)0351ecb11

main
10.3 KiB179 linesraw
1"""#3688: the update path never crosses GPU makes. An AMD R9700 PC (hip rx9070 installed) was given the cuda13 engine because a leftover
2nvidia-smi made detection say cuda. Three guards, all tested here with faked hardware:
3  1. detection: on Windows the display adapters decide first, and a tool for a make with no adapter is ignored;
4  2. the update decision: an installed engine of another make is never replaced by one of the detected make (unless the person asks);
5  3. the stage step refuses a package of another make even if a decision slipped it through."""
6from __future__ import annotations
7
8import subprocess
9import sys
10import tempfile
11import unittest
12from pathlib import Path
13from types import SimpleNamespace
14from unittest import mock
15
16from strata_control import engine_component as ec, feed as feedmod, updater
17
18WIN = "windows-x86_64"
19
20R9700_ONLY = [{"vendor": "1002", "device": 0x7551, "name": "AMD Radeon AI PRO R9700", "vram_gb": 32.0}]
21R9700_AND_NVIDIA = R9700_ONLY + [{"vendor": "10de", "device": 0x2504, "name": "NVIDIA GeForce RTX 3060", "vram_gb": 12.0}]
22AMD_IGPU_ONLY = [{"vendor": "1002", "device": 0x164E, "name": "AMD Radeon(TM) Graphics", "vram_gb": 0.5}]
23NVIDIA_ONLY = [{"vendor": "10de", "device": 0x2504, "name": "NVIDIA GeForce RTX 3060", "vram_gb": 12.0}]
24
25
26def package(backend, variant, archs, version="0.1.47"):
27    return feedmod._engine_package({
28        "version": version, "platform": WIN, "backend": backend, "variant": variant, "archs": list(archs),
29        "min_driver": {"nvidia": "580.65"} if backend == "cuda" else {}, "min_runtime": None, "build_id": f"b-{variant}",
30        "format": "zip", "url": f"e-{variant}.zip", "size": 10, "sha256": "a" * 64, "manifest_sha256": "b" * 64,
31        "installed_bytes": 20, "notes": ""})
32
33
34FEED = SimpleNamespace(engine=[package("cuda", "cuda13", ("sm_75", "sm_86", "sm_120")),
35                               package("hip", "rdna3", ("gfx1100", "gfx1151")),
36                               package("hip", "rx9070", ("gfx1200", "gfx1201"))])
37
38
39class Detection(unittest.TestCase):
40    def detect(self, adapters, tools=("nvidia-smi",)):
41        def fake_run(command, **kw):
42            return subprocess.CompletedProcess(command, 0, stdout="NVIDIA GeForce RTX 3060\n", stderr="")
43        with mock.patch.object(ec.os, "name", "nt"), mock.patch.object(ec, "host_platform", return_value=WIN), \
44                mock.patch.object(ec.shutil, "which", lambda name: name if name in tools else None), \
45                mock.patch.object(ec.subprocess, "run", fake_run), mock.patch.object(ec, "windows_display_adapters", return_value=adapters):
46            return ec.detect_backend({})
47
48    def test_a_leftover_nvidia_smi_does_not_make_an_amd_pc_cuda(self):
49        self.assertEqual(self.detect(R9700_ONLY), ("hip", "display adapters"))
50
51    def test_a_leftover_nvidia_smi_beside_only_an_integrated_radeon_is_not_believed_either(self):
52        self.assertEqual(self.detect(AMD_IGPU_ONLY), (None, "no GPU tool answered"))
53
54    def test_an_nvidia_pc_is_still_cuda(self):
55        self.assertEqual(self.detect(NVIDIA_ONLY), ("cuda", "display adapters"))
56
57    def test_both_makes_keep_the_usual_order(self):
58        self.assertEqual(self.detect(R9700_AND_NVIDIA)[0], "cuda")
59        self.assertEqual(ec.adapter_makes(R9700_AND_NVIDIA), {"cuda", "hip"})
60
61    def test_adapters_that_cannot_be_read_leave_the_tools_in_charge(self):
62        self.assertEqual(self.detect([]), ("cuda", "nvidia-smi"))
63
64    def test_the_environment_override_still_wins(self):
65        with mock.patch.object(ec, "windows_display_adapters", return_value=R9700_ONLY):
66            self.assertEqual(ec.detect_backend({"STRATA_ENGINE_BACKEND": "cuda"}), ("cuda", "environment"))
67
68
69class Decision(unittest.TestCase):
70    """Faked hardware and a faked installed engine (a HIP package cannot be built on a machine without the HIP tuning files)."""
71
72    def setUp(self):
73        self.temp = tempfile.TemporaryDirectory(prefix="strata-makeguard-")
74        self.addCleanup(self.temp.cleanup)
75        self.components = Path(self.temp.name) / "components"
76        self.state = updater.State(self.components)
77
78    def installed(self, backend, variant, version="0.1.46"):
79        """Pretend this engine is installed: what the folder scan and the start path would report."""
80        engine = SimpleNamespace(version=version, platform=WIN, backend=backend, variant=variant, build_id="b", root=Path("x") / f"{version}-{backend}", signature="s")
81        for patch in (mock.patch.object(ec, "scan", return_value=([engine], [])),
82                      mock.patch.object(updater, "installed_engine", lambda root, hardware: (engine if hardware.get("backend") == backend else None, None))):
83            patch.start()
84            self.addCleanup(patch.stop)
85
86    def hardware(self, backend, makes, source="nvidia-smi", **extra):
87        return {"platform": WIN, "backend": backend, "backend_source": source, "runtime": None, "nvidia_driver": "610.47",
88                "cuda_archs": ["sm_86"] if backend == "cuda" else [], "gfx": ["gfx1201"] if backend == "hip" else [], "gpu_names": [],
89                "makes": list(makes), **extra}
90
91    def ctx(self, hardware):
92        return updater.Context(components=self.components, source="", trusted={}, hardware=hardware, environ={})
93
94    def test_the_r9700_case_detection_says_cuda_but_the_adapters_list_only_the_amd_card_so_the_update_stays_hip(self):
95        self.installed("hip", "rx9070")
96        ctx = self.ctx(self.hardware("cuda", ["hip"]))           # what a leftover nvidia-smi said
97        with mock.patch.object(updater, "detect_hardware", return_value=self.hardware("hip", ["hip"])):
98            report, pkg = updater._choose_engine(FEED, ctx, self.state)
99        self.assertEqual(ctx.hardware["backend"], "hip")
100        self.assertEqual(ctx.hardware["backend_source"], "installed engine")
101        self.assertEqual((pkg.backend, pkg.variant), ("hip", "rx9070"))
102        self.assertEqual(report["status"], "available")
103
104    def test_with_no_card_of_the_installed_make_the_update_is_refused_in_plain_words(self):
105        self.installed("hip", "rx9070")
106        report, pkg = updater._choose_engine(FEED, self.ctx(self.hardware("cuda", ["cuda"])), self.state)
107        self.assertIsNone(pkg)
108        self.assertEqual(report["status"], "wrong-make")
109        for word in ("AMD", "NVIDIA", "Nothing was changed"):
110            self.assertIn(word, report["note"])
111        self.assertIsNone(report["offered"])
112        self.assertEqual(report["installed"]["backend"], "hip")
113
114    def test_cards_unreadable_and_a_different_make_installed_also_refuses(self):
115        self.installed("hip", "rx9070")
116        report, pkg = updater._choose_engine(FEED, self.ctx(self.hardware("cuda", [])), self.state)
117        self.assertEqual((report["status"], pkg), ("wrong-make", None))
118
119    def test_nothing_installed_yet_is_not_blocked(self):
120        with mock.patch.object(ec, "scan", return_value=([], [])):
121            report, pkg = updater._choose_engine(FEED, self.ctx(self.hardware("cuda", ["cuda"])), self.state)
122        self.assertEqual((report["status"], pkg.backend), ("install", "cuda"))
123
124    def test_the_same_make_updates_as_before(self):
125        self.installed("cuda", "cuda13", version="0.1.46")
126        report, pkg = updater._choose_engine(FEED, self.ctx(self.hardware("cuda", ["cuda"])), self.state)
127        self.assertEqual((report["status"], pkg.backend, pkg.variant), ("available", "cuda", "cuda13"))
128
129    def test_the_cuda_pc_with_an_nvidia_engine_does_not_flip_to_amd(self):
130        self.installed("cuda", "cuda13")
131        ctx = self.ctx(self.hardware("cuda", ["cuda", "hip"]))
132        report, pkg = updater._choose_engine(FEED, ctx, self.state)
133        self.assertEqual(pkg.backend, "cuda")
134
135    def test_an_explicit_backend_is_the_persons_decision_and_passes(self):
136        self.installed("hip", "rx9070")
137        for source in ("requested", "environment"):
138            self.assertIsNone(updater.make_guard(self.ctx(self.hardware("cuda", ["cuda"], source=source))), source)
139
140    def test_the_stage_step_refuses_a_package_of_another_make_even_if_a_decision_slipped_it_through(self):
141        self.installed("hip", "rx9070")
142        ctx = self.ctx(self.hardware("cuda", ["cuda"]))
143        cuda = package("cuda", "cuda13", ("sm_86",))
144        with mock.patch.object(updater, "_choose_engine", return_value=({"status": "available", "installed": None}, cuda)),                 mock.patch.object(updater, "_download", side_effect=AssertionError("downloaded")):
145            with self.assertRaises(updater.UpdateError) as caught:
146                updater.update_engine(ctx, FEED, self.state)
147        self.assertIn("another make", str(caught.exception))
148
149
150class Pascal(unittest.TestCase):
151    """#3655: a GTX 1080 (sm_61) is a working machine on the CUDA 12 engine; the CUDA 13 build has no code for it and is never chosen."""
152
153    FEED = [package("cuda", "cuda13", ("sm_75", "sm_86", "sm_89", "sm_120")),
154            feedmod._engine_package({"version": "0.1.47", "platform": WIN, "backend": "cuda", "variant": "cuda12", "min_driver": {"nvidia": "525.60"},
155                                     "archs": ["sm_60", "sm_61", "sm_70", "sm_75", "sm_80", "sm_86", "sm_89"], "min_runtime": None, "build_id": "b-12",
156                                     "format": "zip", "url": "e-12.zip", "size": 10, "sha256": "a" * 64, "manifest_sha256": "b" * 64,
157                                     "installed_bytes": 20, "notes": ""})]
158
159    def hardware(self, archs):
160        return {"platform": WIN, "backend": "cuda", "runtime": None, "nvidia_driver": "581.15", "cuda_archs": archs, "gfx": [], "gpu_names": []}
161
162    def test_two_gtx_1080_get_the_cuda_12_engine(self):
163        best, reasons = feedmod.select_engine(self.FEED, self.hardware(["sm_61", "sm_61"]))
164        self.assertEqual(best.variant, "cuda12")
165        self.assertTrue(any("cuda13" in r or "sm_75" in r for r in reasons), "the CUDA 13 build is refused for the card")
166
167    def test_a_newer_card_still_gets_the_newest_build(self):
168        best, _ = feedmod.select_engine(self.FEED, self.hardware(["sm_86"]))
169        self.assertEqual(best.variant, "cuda13")
170
171
172class Host(unittest.TestCase):
173    def test_the_app_shows_the_sentence_not_the_no_engine_for_this_card_text(self):
174        text = (Path(__file__).resolve().parents[2] / "apps" / "host" / "server" / "updates.mjs").read_text(encoding="utf-8")
175        self.assertIn("engine.status === 'wrong-make'", text)
176
177
178if __name__ == "__main__":
179    unittest.main()