okami.mom/strata

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

Niko1221Merge f-0213-qatui into f-0213-int5d363f11e

main
2.7 KiB76 linesraw
1"""Round 0213 QATUI (final pass, owner PC): with an engine installed, one `strata launch --plan` on Windows started `nvidia-smi` 19 times (10 x compute_cap, 9 x
2driver_version): every caller asked again. The answer is kept for 30 s per field now.
3
4    python -m pytest tests/terminal/test_smi_cache_0213_qatui.py
5"""
6from __future__ import annotations
7
8import subprocess
9import sys
10import unittest
11from pathlib import Path
12from unittest import mock
13
14ROOT = Path(__file__).resolve().parents[2]
15sys.path.insert(0, str(ROOT))
16
17from strata_control import engine_component as ec  # noqa: E402
18
19
20class SmiCache(unittest.TestCase):
21    def setUp(self):
22        ec._SMI_CACHE.clear()
23        self.addCleanup(ec._SMI_CACHE.clear)
24
25    def _smi(self, run):
26        return (mock.patch.object(ec.shutil, "which", lambda name: "nvidia-smi"), mock.patch.object(ec.subprocess, "run", run))
27
28    def test_many_callers_start_nvidia_smi_once_per_field(self):
29        calls = []
30
31        def run(cmd, **kw):
32            calls.append(cmd[1])
33            return subprocess.CompletedProcess(cmd, 0, "12.0\n" if "compute_cap" in cmd[1] else "610.47\n", "")
34        p1, p2 = self._smi(run)
35        with p1, p2:
36            for _ in range(10):
37                self.assertEqual(ec.cuda_archs_present(), ["sm_120"])
38                self.assertEqual(ec.nvidia_driver_version(), "610.47")
39        self.assertEqual(sorted(calls), ["--query-gpu=compute_cap", "--query-gpu=driver_version"])
40
41    def test_a_stuck_nvidia_smi_costs_its_timeout_once(self):
42        calls = []
43
44        def run(cmd, **kw):
45            calls.append(kw.get("timeout"))
46            raise subprocess.TimeoutExpired(cmd, kw.get("timeout"))
47        p1, p2 = self._smi(run)
48        with p1, p2:
49            for _ in range(10):
50                self.assertEqual(ec.cuda_archs_present(), [])
51        self.assertEqual(calls, [8])
52
53    def test_it_is_asked_again_after_the_time_is_up(self):
54        calls = []
55
56        def run(cmd, **kw):
57            calls.append(1)
58            return subprocess.CompletedProcess(cmd, 0, "8.6\n", "")
59        p1, p2 = self._smi(run)
60        with p1, p2:
61            ec.cuda_archs_present()
62            stamp, found = ec._SMI_CACHE["compute_cap"]
63            ec._SMI_CACHE["compute_cap"] = (stamp - ec.SMI_CACHE_SECONDS - 1, found)
64            ec.cuda_archs_present()
65        self.assertEqual(len(calls), 2)
66
67    def test_a_caller_cannot_change_the_next_answer(self):
68        p1, p2 = self._smi(lambda cmd, **kw: subprocess.CompletedProcess(cmd, 0, "8.6\n8.9\n", ""))
69        with p1, p2:
70            first = ec._smi_query("compute_cap")
71            first.append("x")
72            self.assertEqual(ec._smi_query("compute_cap"), ["8.6", "8.9"])
73
74
75if __name__ == "__main__":
76    unittest.main()