okami.mom/strata

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

Niko1221Merge f-028-app-p13b8dc8fa3

main
7.7 KiB130 linesraw
1"""The CUDA 12 driver floor comes from the engine package, not from a constant of setup's own (#4247, #4188 part 1). Nothing here starts an
2engine, downloads anything or reads the real profile."""
3from __future__ import annotations
4
5import time
6import unittest
7from types import SimpleNamespace
8from unittest import mock
9
10import setup as S
11from strata_control import driver_floor as DF
12
13
14def package(version: str, need: str | None, *, variant: str = "cuda12", platform: str = "windows-x86_64", backend: str = "cuda"):
15    return SimpleNamespace(version=version, platform=platform, backend=backend, variant=variant, min_driver={"nvidia": need} if need else {})
16
17
18class DriverFloor(unittest.TestCase):
19    def test_the_floor_is_the_newest_cuda12_package_of_this_platform(self):
20        packages = [package("0.1.49", "570.00"), package("0.1.50", "576.02"), package("0.1.50", "580.65", variant="cuda13"),
21                    package("0.1.51", "575.51", platform="linux-x86_64"), package("0.1.52", None)]
22        self.assertEqual(DF._floor_of(packages, "windows-x86_64", "cuda12"), "576.02")
23        self.assertEqual(DF._floor_of(packages, "linux-x86_64", "cuda12"), "575.51")
24        self.assertEqual(DF._floor_of(packages, "windows-x86_64", "cuda13"), "580.65")
25        self.assertIsNone(DF._floor_of([package("0.1.50", "580.65", variant="cuda13")], "windows-x86_64", "cuda12"))
26        self.assertIsNone(DF._floor_of([package("0.1.50", "580.65", backend="hip")], "windows-x86_64", "cuda13"))
27
28    def test_an_installed_package_answers_before_the_feed_is_asked(self):
29        with mock.patch("strata_control.engine_component.scan", return_value=([package("0.1.50", "576.02")], [])), \
30                mock.patch.object(DF, "from_feed", side_effect=AssertionError("the feed must not be asked")):
31            self.assertEqual(DF.cuda12_floor("windows-x86_64", roots=["/x"], feed_source="https://feed"),
32                             ("576.02", "as the installed engine package says"))
33
34    def test_the_feed_answers_when_no_package_is_installed(self):
35        feed = SimpleNamespace(engine=(package("0.1.50", "576.02"), package("0.1.50", "580.65", variant="cuda13")))
36        with mock.patch("strata_control.engine_component.scan", return_value=([], [])), \
37                mock.patch("strata_control.feed.fetch", return_value=feed), \
38                mock.patch("strata_control.signing.load_trusted_keys", return_value={"k": b"x"}):
39            self.assertEqual(DF.cuda12_floor("windows-x86_64", roots=["/x"], feed_source="https://feed"),
40                             ("576.02", "as the signed update feed says"))
41
42    def test_nothing_readable_gives_the_published_figure_and_never_an_error(self):
43        with mock.patch("strata_control.engine_component.scan", side_effect=OSError("no folder")), \
44                mock.patch("strata_control.feed.fetch", side_effect=RuntimeError("offline")):
45            got = DF.cuda12_floor("windows-x86_64", roots=["/x"], feed_source="https://feed")
46        self.assertEqual(got[0], DF.FALLBACK)
47        self.assertEqual(DF.major(DF.FALLBACK), 575)
48        self.assertEqual(DF.cuda12_floor("linux-x86_64"), (DF.FALLBACK, got[1]), "no roots and no feed: the figure, without touching anything")
49
50    def test_a_feed_that_hangs_does_not_hold_setup_up(self):
51        def hang(*a, **k):
52            time.sleep(3)
53        t0 = time.monotonic()
54        with mock.patch("strata_control.feed.fetch", side_effect=hang), mock.patch("strata_control.signing.load_trusted_keys", return_value={"k": b"x"}):
55            self.assertIsNone(DF.from_feed("https://feed", "windows-x86_64", seconds=0.2))
56        self.assertLess(time.monotonic() - t0, 2.0)
57
58    def test_major(self):
59        self.assertEqual([DF.major(v) for v in ("576.02", "560", " 575.51 ", "", None, "x")], [576, 560, 575, 0, 0, 0])
60
61
62class SetupUsesThePackagesFloor(unittest.TestCase):
63    def floor(self, figure: str, where: str = "as the installed engine package says"):
64        return mock.patch.object(S, "cuda12_min_driver", return_value=(DF.major(figure), figure, where))
65
66    def test_setup_no_longer_says_the_cuda12_engine_works_from_528(self):
67        """#4247 #4188: setup said 'it works with driver 528 or newer' while the feed package needed 576.02 and the update refused it."""
68        with self.floor("576.02"):
69            tk, why = S.cuda_choice([86], None, 556)
70        self.assertEqual(tk, 12, "the choice itself is as before: a driver too old for CUDA 13 takes the CUDA 12 engine")
71        self.assertNotIn("528", why)
72        self.assertNotIn("works with driver", why)
73
74    def test_a_driver_under_the_packages_floor_gets_a_warning_with_the_packages_own_number(self):
75        with self.floor("576.02"):
76            note = S.cuda12_driver_warning(556)
77            self.assertIn("576.02", note)
78            self.assertIn("as the installed engine package says", note)
79            self.assertIn("556", note)
80            self.assertIn("strata update", note)
81            self.assertIsNone(S.cuda12_driver_warning(576))
82            self.assertIsNone(S.cuda12_driver_warning(591))
83            self.assertIsNone(S.cuda12_driver_warning(0), "driver unknown: nothing to say")
84
85    def test_a_package_with_a_lower_floor_lowers_what_setup_says_too(self):
86        with self.floor("550.54"):
87            self.assertIsNone(S.cuda12_driver_warning(556))
88            self.assertIn("550.54", S.cuda12_driver_warning(535))
89
90    def test_the_linux_warning_adds_that_a_locally_built_engine_can_run_older(self):
91        with self.floor("575.51"), mock.patch.object(S, "WIN", False):
92            self.assertIn("compiles the engine here", S.cuda12_driver_warning(535))
93        with self.floor("575.51"), mock.patch.object(S, "WIN", True):
94            self.assertNotIn("compiles the engine here", S.cuda12_driver_warning(535))
95
96    def test_the_choice_between_the_engines_is_unchanged(self):
97        with self.floor("576.02"):
98            self.assertEqual(S.cuda_choice([86], None, 580), (13, None))
99            self.assertEqual(S.cuda_choice([86], None, 0), (13, None))
100            self.assertEqual(S.cuda_choice([86], None, 500), (13, None), "under NVIDIA's own CUDA 12 line: setup stops with the driver message")
101            self.assertEqual(S.cuda_choice([70], None, 0)[0], 12)
102            self.assertEqual(S.cuda_choice([86], "12")[0], 12)
103
104    def test_what_stops_setup_is_nvidias_own_line_not_a_promise(self):
105        self.assertEqual(S.CUDA12_COMPAT_DRIVER, 528 if S.WIN else 525)
106        self.assertFalse(hasattr(S, "CUDA12_MIN_DRIVER"))
107
108    def test_cuda12_min_driver_reads_the_installed_package_and_remembers_it(self):
109        with mock.patch.dict(S._CUDA12_FLOORS, clear=True), \
110                mock.patch("strata_control.engine_component.scan", return_value=([package("0.1.50", "576.02", platform=_host())], [])):
111            self.assertEqual(S.cuda12_min_driver(), (576, "576.02", "as the installed engine package says"))
112            self.assertEqual(S.cuda12_min_driver(), (576, "576.02", "as the installed engine package says"))
113
114    def test_a_plain_call_never_asks_the_feed_but_a_setup_run_may(self):
115        with mock.patch.dict(S._CUDA12_FLOORS, clear=True), mock.patch("strata_control.engine_component.scan", return_value=([], [])), \
116                mock.patch.object(DF, "from_feed", return_value="576.02") as feed, mock.patch.object(S.acquisition, "is_forbidden", return_value=False):
117            self.assertEqual(S.cuda12_min_driver()[1], DF.FALLBACK)
118            feed.assert_not_called()
119            self.assertEqual(S.cuda12_min_driver(feed=True)[1], "576.02")
120            self.assertEqual(S.cuda12_min_driver()[1], "576.02", "the answer from the feed is the one kept")
121            self.assertEqual(feed.call_count, 1)
122
123
124def _host() -> str:
125    from strata_control import targets
126    return targets.host_platform()
127
128
129if __name__ == "__main__":
130    unittest.main()