okami.mom/strata

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

Niko1221Merge f-0213-tuicfg (final c6dda74bf) into f-0213-int049ae1c67

main
6.8 KiB147 linesraw
1"""Round 0213 TUICFG, step 4: choosing the engine on a PC with cards of several makes (#4971, #5111).
2
3Every case is a temporary /sys/class/drm tree (a real two-vendor sysfs cannot be made on a test box), in both card orders."""
4from __future__ import annotations
5
6import os
7import tempfile
8import unittest
9from pathlib import Path
10from unittest import mock
11
12from strata_cli.launch import catalog as C
13from strata_control import engine_component as ec
14
15AMD, INTEL, NVIDIA = "0x1002", "0x8086", "0x10de"
16REAL_ARC = C.intel_arc_cards
17
18
19def build(root: Path, *cards) -> None:
20    """cards: (name, vendor, device id or None, own memory in MiB or None); a name like card0 / card10 / card1-DP-1 is the directory."""
21    for name, vendor, did, mib in cards:
22        dev = root / name / "device"
23        dev.mkdir(parents=True)
24        if vendor:
25            (dev / "vendor").write_text(vendor + "\n", encoding="utf-8")
26        if did:
27            (dev / "device").write_text(did + "\n", encoding="utf-8")
28        if mib is not None:
29            (dev / "mem_info_vram_total").write_text(str(mib * 1024 * 1024) + "\n", encoding="utf-8")
30
31
32class Tree(unittest.TestCase):
33    def setUp(self):
34        self.tmp = tempfile.TemporaryDirectory()
35        self.addCleanup(self.tmp.cleanup)
36        self.root = Path(self.tmp.name)
37
38    def backend(self, *cards):
39        build(self.root, *cards)
40        return ec._drm_vendor_backend(str(self.root))
41
42    def arcs(self, *cards):
43        build(self.root, *cards)
44        return REAL_ARC(str(self.root))
45
46
47APU = ("card0", AMD, "0x164e", 512)
48ARC = ("card1", INTEL, "0xe211", None)
49
50
51class Backend(Tree):
52    def test_an_apu_beside_an_arc_is_not_an_amd_pc_in_either_order(self):
53        self.assertEqual(self.backend(APU, ARC), "sycl")
54        self.tearDown(); self.setUp()
55        self.assertEqual(self.backend(("card0", INTEL, "0xe211", None), ("card1", AMD, "0x164e", 512)), "sycl")
56
57    def test_an_arc_of_unknown_size_beside_an_apu(self):
58        self.assertEqual(self.backend(APU, ("card1", INTEL, "0xe2f0", None)), "sycl")
59
60    def test_a_sole_apu_stays_amd(self):
61        self.assertEqual(self.backend(APU), "hip")
62
63    def test_a_sole_arc_is_intel(self):
64        self.assertEqual(self.backend(ARC), "sycl")
65
66    def test_a_discrete_amd_card_and_nvidia_keep_their_order(self):
67        self.assertEqual(self.backend(("card0", AMD, "0x7550", 16384), ARC), "hip")
68        self.tearDown(); self.setUp()
69        self.assertEqual(self.backend(APU, ("card1", NVIDIA, "0x2204", None)), "cuda")
70
71    def test_a_large_unified_memory_apu_is_a_card(self):
72        self.assertEqual(self.backend(("card0", AMD, "0x1586", 32768)), "hip")
73        self.tearDown(); self.setUp()
74        self.assertEqual(self.backend(("card0", AMD, "0x1586", 32768), ARC), "hip", "the older order; an explicit backend decides otherwise")
75
76    def test_connectors_and_unreadable_entries_are_ignored(self):
77        self.assertEqual(self.backend(("card0-DP-1", None, None, None), APU, ARC), "sycl")
78
79    def test_no_card_is_none(self):
80        self.assertIsNone(ec._drm_vendor_backend(str(self.root / "missing")))
81
82
83class ArcCards(Tree):
84    def test_both_orders_and_a_card_beside_a_connector(self):
85        for cards in ([APU, ARC], [("card0", INTEL, "0xe211", None), ("card1", AMD, "0x164e", 512)], [("card0-DP-1", None, None, None), APU, ARC]):
86            with self.subTest(cards=[c[0] for c in cards]):
87                self.tearDown(); self.setUp()
88                got = self.arcs(*cards)
89                self.assertEqual([(g["name"], g["vram_gb"], g["index"]) for g in got], [("Intel Arc Pro B60", 24.0, 0)])
90
91    def test_a_card_number_above_nine_and_an_unreadable_neighbour(self):
92        got = self.arcs(("card0", INTEL, None, None), ("card2", INTEL, "0xe20b", None), ("card10", INTEL, "0xe211", None))
93        self.assertEqual([g["name"] for g in got], ["Intel Arc B580", "Intel Arc Pro B60"], "a card without a device file does not hide the rest")
94
95    def test_an_unlisted_battlemage_or_alchemist_id_still_counts_the_processor_graphics_do_not(self):
96        got = self.arcs(("card0", INTEL, "0xe2f0", None), ("card1", INTEL, "0x56b1", None))
97        self.assertEqual(len(got), 2)
98        self.assertTrue(all(g["usable"] and g["vram_gb"] >= 8.0 for g in got))
99        self.tearDown(); self.setUp()
100        self.assertEqual(self.arcs(("card0", INTEL, "0x7d55", None), ("card1", INTEL, "0x46a6", None), ("card2", INTEL, "0xa7a0", None)), [])
101
102    def test_a_sole_apu_has_no_arc(self):
103        self.assertEqual(self.arcs(APU), [])
104
105
106class LauncherChoice(Tree):
107    """The launcher's own view: the cards, the engine's make, and what is listed as usable."""
108
109    def computer(self, shared_gpus, env=None):
110        from tests.terminal.launch_support import Home
111        with Home():
112            cat = C.Catalog()
113            hw = {"ram_gb": 64.0, "os": "Linux", "cpu": {"avx2": True}, "gpus": shared_gpus, "disk_free_gb": 500}
114            fake = lambda root="/sys/class/drm": REAL_ARC(str(self.root))      # noqa: E731
115            with mock.patch.dict(os.environ, env or {}, clear=False), mock.patch.object(cat.strata, "hardware", return_value=hw), \
116                    mock.patch.object(C, "intel_arc_cards", fake), mock.patch("strata_control.engine_component.status", return_value={"usable": False}):
117                if not env:
118                    os.environ.pop("STRATA_ENGINE_BACKEND", None)
119                comp = cat.computer()
120                return comp.engine_vendor, [g["name"] for g in comp.usable_gpus], [g["name"] for g in comp.ignored_gpus]
121
122    IGPU = {"index": 0, "name": "AMD Radeon Graphics", "vram_gb": 0.5, "usable": False, "vendor": "amd"}
123    RTX = {"index": 0, "name": "NVIDIA GeForce RTX 3090", "vram_gb": 24.0, "usable": True, "vendor": "nvidia"}
124
125    def test_the_arc_is_used_beside_an_apu_igpu(self):
126        build(self.root, APU, ARC)
127        vendor, usable, ignored = self.computer([self.IGPU])
128        self.assertEqual((vendor, usable), ("intel", ["Intel Arc Pro B60"]))
129
130    def test_an_explicit_sycl_beside_nvidia_uses_the_arc_also_when_its_model_is_not_listed(self):
131        for did in ("0xe211", "0xe2f0"):
132            with self.subTest(did=did):
133                self.tearDown(); self.setUp()
134                build(self.root, ("card0", NVIDIA, "0x2204", None), ("card1", INTEL, did, None))
135                vendor, usable, ignored = self.computer([self.RTX], {"STRATA_ENGINE_BACKEND": "sycl"})
136                self.assertEqual(vendor, "intel")
137                self.assertEqual(len(usable), 1)
138                self.assertIn("NVIDIA GeForce RTX 3090", ignored)
139
140    def test_without_a_choice_nvidia_stays_the_first_make(self):
141        build(self.root, ("card0", NVIDIA, "0x2204", None), ARC)
142        vendor, usable, _ = self.computer([self.RTX])
143        self.assertEqual((vendor, usable), ("nvidia", ["NVIDIA GeForce RTX 3090"]))
144
145
146if __name__ == "__main__":
147    unittest.main()