okami.mom/strata

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

Niko1221update: --backend amd on a PC with an NVIDIA and an AMD card picks the AMD card's packagefdfca1d9d

main
9.8 KiB166 linesraw
1"""A PC with an NVIDIA card and an AMD card (the lent boxia: RTX 3080 + RX 6700 XT gfx1031, 11-15).
2
3Before: `strata update --backend amd` said "The feed has no engine package for this PC" (the word amd was never turned into the hip family, and the
4hardware the choice read stayed the NVIDIA card's), `strata components engine --backend amd` exited 5 ("this PC needs amd (requested)"), and
5`strata components engines` marked the installed hip-rx6000 engine "not for this computer" although the PC has the card it is for.
6
7Proves, with a faked two-card detection: amd / hip / hip-rx6000 / nvidia name their family; --backend amd reads the AMD card (gfx1031) and picks
8the rx6000 package from a feed that also has the CUDA, rx9070, rx7900 and halo builds; resolve / status accept the make's word; the engines list marks
9the AMD engine as one this computer can run (for its other card) and keeps "newest" on the engine of the make in use; switching to it keeps the
10AMD choice the way `strata launch --backend amd` does."""
11import argparse
12import io
13import json
14import shutil
15import unittest
16from contextlib import redirect_stdout
17from unittest import mock
18
19from strata_cli import update_cmd
20from strata_control import engine_component as ec, engine_library as lib, feed as feedmod, updater
21from tests.terminal.test_updater import PLATFORM, UpdaterCase
22
23
24def _detect_backend(environ=None):
25    forced = ec.split_backend((environ or {}).get(ec.ENV_BACKEND, ""))[0]
26    if forced:
27        return forced, "environment"
28    return "cuda", "nvidia-smi"                       # the NVIDIA card answers first, as on boxia
29
30
31def _pkg(backend, variant, archs):
32    return feedmod.EnginePackage(version="0.1.60", platform="linux-x86_64", backend=backend, variant=variant, archs=tuple(archs), min_driver={},
33                                 min_runtime=None, build_id="x", url=f"strata-engine-0.1.60-linux-x86_64-{backend}-{variant}.tar.gz", format="tar.gz",
34                                 size=1, sha256="0" * 64, manifest_sha256="0" * 64, installed_bytes=1)
35
36
37FEED = [_pkg("cuda", "cuda13", ("sm_86", "sm_120")), _pkg("hip", "rx6000", ("gfx1030", "gfx1031", "gfx1032")),
38        _pkg("hip", "rx9070", ("gfx1200", "gfx1201")), _pkg("hip", "rx7900", ("gfx1100", "gfx1101", "gfx1102")),
39        _pkg("hip", "halo", ("gfx1151",))]
40
41
42class MixedPcHardware(unittest.TestCase):
43    def setUp(self):
44        for target, name, value in ((ec, "detect_backend", None), (ec, "gpu_makes_present", {"cuda", "hip"}), (updater, "host_platform", "linux-x86_64"),
45                                    (updater, "_gfx_names", ["gfx1031"]), (updater, "_amd_card_names", []), (ec, "runtime_version", None),
46                                    (ec, "nvidia_driver_version", "580.95"), (ec, "cuda_archs_present", ["sm_86"])):
47            patch = mock.patch.object(target, name, _detect_backend) if value is None else mock.patch.object(target, name, return_value=value)
48            patch.start()
49            self.addCleanup(patch.stop)
50
51    def test_the_make_words_name_their_family(self):
52        self.assertEqual(ec.split_backend("amd"), ("hip", ""))
53        self.assertEqual(ec.split_backend(" AMD "), ("hip", ""))
54        self.assertEqual(ec.split_backend("nvidia"), ("cuda", ""))
55        self.assertEqual(ec.split_backend("intel"), ("sycl", ""))
56        self.assertEqual(ec.split_backend("hip-rx6000"), ("hip", "rx6000"))
57        self.assertEqual(ec.split_backend("cuda"), ("cuda", ""))
58
59    def test_without_a_choice_the_nvidia_card_decides_as_before(self):
60        hw = updater.detect_hardware({})
61        self.assertEqual((hw["backend"], hw["gfx"], hw["cuda_archs"]), ("cuda", [], ["sm_86"]))
62        pick, _ = feedmod.select_engine(FEED, hw)
63        self.assertEqual((pick.backend, pick.variant), ("cuda", "cuda13"))
64
65    def test_backend_amd_reads_the_amd_card_and_picks_the_rx6000_package(self):
66        hw = update_cmd.requested_hardware("amd", environ={})
67        self.assertEqual((hw["backend"], hw["gfx"], hw["backend_source"]), ("hip", ["gfx1031"], "requested"))
68        pick, reasons = feedmod.select_engine(FEED, hw)
69        self.assertIsNotNone(pick, reasons)
70        self.assertEqual((pick.backend, pick.variant), ("hip", "rx6000"))
71
72    def test_backend_hip_and_the_package_name_pick_the_same(self):
73        for word in ("hip", "hip-rx6000", "HIP"):
74            hw = update_cmd.requested_hardware(word, environ={})
75            pick, reasons = feedmod.select_engine(FEED, hw)
76            self.assertEqual((pick.backend, pick.variant), ("hip", "rx6000"), (word, reasons))
77        self.assertEqual(update_cmd.requested_hardware("hip-rx6000", environ={})["variant_wanted"], "rx6000")
78
79    def test_backend_nvidia_stays_on_the_nvidia_card(self):
80        hw = update_cmd.requested_hardware("nvidia", environ={})
81        self.assertEqual((hw["backend"], hw["cuda_archs"], hw["gfx"]), ("cuda", ["sm_86"], []))
82
83    def test_a_word_that_names_no_family_is_still_answered_by_the_feed(self):
84        hw = update_cmd.requested_hardware("vulkan", environ={})
85        pick, reasons = feedmod.select_engine(FEED, hw)
86        self.assertIsNone(pick)
87        self.assertTrue(any("needs vulkan" in r for r in reasons))
88
89    def test_the_make_guard_lets_the_named_family_through(self):
90        ctx = mock.Mock(hardware=update_cmd.requested_hardware("amd", environ={}))
91        self.assertIsNone(updater.make_guard(ctx))
92
93
94class MixedPcEngines(UpdaterCase):
95    """The installed engines on the two-card PC: the CUDA engine the PC picked first, and the hip-rx6000 one installed with --backend amd."""
96
97    def setUp(self):
98        super().setUp()
99        cuda = self.install_current("0.1.60")
100        # the hip-rx6000 package: the stub CUDA package with an AMD manifest (the packager wants a real AMD stage, its hipBLASLt version)
101        self.amd = cuda.parent / f"0.1.60-{PLATFORM}-hip-rx6000"
102        shutil.copytree(cuda, self.amd)
103        manifest = json.loads((self.amd / ec.MANIFEST).read_text())
104        manifest.update(backend="hip", variant="rx6000", archs=["gfx1030", "gfx1031", "gfx1032"], min_driver={})
105        (self.amd / ec.MANIFEST).write_text(json.dumps(manifest))
106        for target, name, value in ((lib, "_process_uses", False), (ec, "hip_archs_present", ["gfx1031"]), (ec, "gpu_makes_present", {"cuda", "hip"})):
107            patch = mock.patch.object(target, name, return_value=value)
108            patch.start()
109            self.addCleanup(patch.stop)
110        self.settings = self.root / "settings"
111        patch = mock.patch("strata_control.discovery.own_settings_file", return_value=self.settings / "settings.json")
112        patch.start()
113        self.addCleanup(patch.stop)
114
115    def rows(self, hardware):
116        return {e["folder"]: e for e in lib.rows_of(lib.list_engines(self.ctx(hardware=hardware)), lib.QWEN)}
117
118    def test_the_amd_engine_is_one_this_computer_can_run(self):
119        rows = self.rows({**self.hardware, "makes": ["cuda", "hip"]})
120        amd = rows[self.amd.name]
121        self.assertTrue(amd["fits"] and amd["other_card"] and amd["why_not"] is None, amd)
122        self.assertFalse(amd["active"] or amd["newest"], "the newest is the one of the make a start uses")
123        cuda = next(r for r in rows.values() if r["backend"] == "cuda")
124        self.assertTrue(cuda["active"] and cuda["newest"] and not cuda["other_card"])
125
126    def test_on_a_pc_without_an_amd_card_it_stays_for_a_different_kind_of_card(self):
127        rows = self.rows({**self.hardware, "makes": ["cuda"]})
128        amd = rows[self.amd.name]
129        self.assertFalse(amd["fits"])
130        self.assertEqual(amd["why_code"], "gpu_make")
131
132    def test_with_backend_amd_the_amd_engine_is_the_one_in_use(self):
133        rows = self.rows({**self.hardware, "backend": "hip", "gfx": ["gfx1031"], "makes": ["cuda", "hip"]})
134        self.assertTrue(rows[self.amd.name]["active"] and rows[self.amd.name]["newest"] and not rows[self.amd.name]["other_card"])
135
136    def test_resolve_and_status_take_the_make_word(self):
137        self.assertEqual(ec.resolve(self.components, backend="amd").root.name, self.amd.name)
138        self.assertEqual(ec.resolve(self.components, backend="hip-rx6000").root.name, self.amd.name)
139        report = ec.status(self.components, backend="amd")
140        self.assertTrue(report["usable"], report["notice"])
141        self.assertEqual((report["backend"], report["engine"]["backend"]), ("hip", "hip"))
142
143    def test_switching_to_the_amd_engine_keeps_the_amd_choice(self):
144        hardware = {**self.hardware, "makes": ["cuda", "hip"]}
145        with mock.patch.object(updater, "detect_hardware", side_effect=lambda environ=None: {**hardware, "backend": ec.saved_backend() or "cuda"}):
146            out = lib.use_engine(self.ctx(hardware=hardware), "qwen", self.amd.name)
147        self.assertEqual(out.get("backend_saved"), "hip")
148        self.assertEqual(json.loads((self.settings / "settings.json").read_text())[ec.SAVED_KEY], "hip")
149        self.assertEqual(ec.resolve(self.components, backend="hip").root.name, self.amd.name)
150
151    def test_the_engines_listing_with_backend_amd_from_the_terminal(self):
152        args = argparse.Namespace(components_dir=str(self.components), backend="amd", wait=None, force=False, model_config=None, output="json",
153                                  components_command="engines")
154        out = io.StringIO()
155        with mock.patch.object(updater, "detect_hardware", side_effect=lambda environ=None: {**self.hardware, "makes": ["cuda", "hip"],
156                                                                                             "backend": ec.split_backend((environ or {}).get(ec.ENV_BACKEND, "cuda"))[0]}), \
157                redirect_stdout(out):
158            code = update_cmd.run(args)
159        self.assertEqual(code, 0)
160        rows = {e["folder"]: e for f in json.loads(out.getvalue())["families"] for e in f["engines"]}
161        self.assertTrue(rows[self.amd.name]["active"] and rows[self.amd.name]["fits"])
162        shutil.rmtree(self.settings, ignore_errors=True)
163
164
165if __name__ == "__main__":
166    unittest.main()