okami.mom/strata

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

Niko1221Merge f-022-gpuroles: Main / Helper / Off per graphics card in the app, TUI, launch, setup, server and Dockere28889d12

main
3.4 KiB63 linesraw
1"""serve/server.py: the run settings' "gpu_roles" (Main / Helper / Off) become the engine's card list, Main LAST, Off left out."""
2from __future__ import annotations
3
4import sys
5import unittest
6from pathlib import Path
7
8sys.path.insert(0, str(Path(__file__).resolve().parents[1]))
9from serve.server import card_roles_lines, child_env, gpu_list, ordered_gpus  # noqa: E402
10
11
12class Roles(unittest.TestCase):
13    def test_roles_alone_give_the_order_main_last_and_off_left_out(self):
14        cfg = {"gpu_roles": {"0": "helper", "1": "main", "2": "off"}, "args": []}
15        self.assertEqual(gpu_list(cfg), [0, 1])
16        self.assertEqual(gpu_list({"gpu_roles": {"0": "main", "1": "helper"}, "args": []}), [1, 0])
17        env = child_env({"gpu_roles": {"0": "main", "1": "helper", "2": "off"}, "args": []})
18        self.assertEqual(env["CUDA_VISIBLE_DEVICES"], "1,0")
19
20    def test_the_mirror_is_used_and_never_re_sorted_by_speed(self):
21        cfg = {"gpu_roles": {"0": "main", "1": "helper"}, "gpu": [1, 0], "gpu_order": "as_given", "args": []}
22        self.assertEqual(ordered_gpus(cfg, {0: 1.0, 1: 9.0}), [1, 0])             # the faster card is Helper on purpose
23        cfg2 = {"gpu_roles": {"0": "main", "1": "helper"}, "gpu": [1, 0], "args": []}      # even without gpu_order
24        self.assertEqual(ordered_gpus(cfg2, {0: 1.0, 1: 9.0}), [1, 0])
25
26    def test_one_card_on(self):
27        cfg = {"gpu_roles": {"0": "off", "1": "main"}, "gpu": 1, "gpu_order": "as_given", "args": []}
28        self.assertEqual(gpu_list(cfg), [1])
29        self.assertEqual(child_env(cfg)["CUDA_VISIBLE_DEVICES"], "1")
30
31    def test_a_hand_edited_gpu_wins(self):
32        cfg = {"gpu_roles": {"0": "helper", "1": "main"}, "gpu": [1, 0], "gpu_order": "as_given", "args": []}
33        self.assertEqual(gpu_list(cfg), [1, 0])
34        self.assertTrue(any("edited by hand" in l for l in card_roles_lines(cfg)))
35        self.assertEqual(ordered_gpus({**cfg, "gpu_order": None}, {0: 1.0, 1: 9.0}), [0, 1])       # the legacy rule again
36
37    def test_bad_roles_are_ignored(self):
38        for bad in ({"0": "boss"}, {"0": "main", "1": "main"}, "x", {}):
39            self.assertEqual(gpu_list({"gpu_roles": bad, "gpu": 1, "args": []}), [1])
40
41    def test_legacy_settings_are_untouched(self):
42        self.assertEqual(gpu_list({"gpu": "0,2", "args": []}), [0, 2])
43        self.assertEqual(gpu_list({"args": []}), [])
44        self.assertEqual(card_roles_lines({"gpu": [1, 0], "args": []}), [])
45
46    def test_the_log_line_names_every_role(self):
47        lines = card_roles_lines({"gpu_roles": {"0": "helper", "1": "main", "2": "off"}, "gpu": [0, 1], "args": []})
48        self.assertEqual(lines, ["[strata] card roles: Main GPU 1, Helper GPU 0, Off GPU 2 (numbers as nvidia-smi gives them)"])
49
50    def test_every_card_off(self):
51        cfg = {"gpu_roles": {"0": "off", "1": "off"}, "gpu": [], "gpu_none": True, "args": []}
52        self.assertEqual(gpu_list(cfg), [])
53        env = child_env(cfg)
54        self.assertEqual((env["CUDA_VISIBLE_DEVICES"], env["HIP_VISIBLE_DEVICES"]), ("", ""))
55        with self.assertRaises(SystemExit) as e:
56            card_roles_lines(cfg)                                                   # a CUDA engine cannot start without a card: said plainly
57        self.assertIn("Turn a card on", str(e.exception))
58        cpu = card_roles_lines({**cfg, "backend": "cpu"})
59        self.assertIn("Performance is degraded on CPU only.", cpu[0])
60
61
62if __name__ == "__main__":
63    unittest.main()