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
6.1 KiB127 linesraw
1"""strata_control/gpu_roles.py: Main / Helper / Off for every graphics card (the rules the app, `strata launch`, setup and the server share)."""
2
3from __future__ import annotations
4
5import json
6import unittest
7from pathlib import Path
8
9from strata_control import gpu_roles as R
10
11ROOT = Path(__file__).resolve().parents[2]
12V = json.loads((ROOT / "tests" / "data" / "gpu_roles_vectors.json").read_text(encoding="utf-8"))
13
14
15def roles(d):
16    return None if d is None else {int(k): v for k, v in d.items()}
17
18
19class Vectors(unittest.TestCase):
20    """tests/data/gpu_roles_vectors.json is also run by apps/host/tests/gpu-roles.test.mjs against gpu-roles.ts."""
21
22    def test_recommend(self):
23        for c in V["recommend"]:
24            with self.subTest(c["hw"]):
25                r = R.recommend(V["hw"][c["hw"]])
26                self.assertEqual(r["roles"], roles(c["roles"]))
27                self.assertEqual((r["main"], r["why"]), (c["main"], c["why"]))
28
29    def test_set_role(self):
30        for c in V["set_role"]:
31            with self.subTest(c):
32                out, note = R.set_role(roles(c["roles"]), V["hw"][c["hw"]], c["card"], c["role"])
33                self.assertEqual(out, roles(c["after"]))
34                self.assertEqual(note, c["note"])
35
36    def test_order(self):
37        for c in V["order"]:
38            with self.subTest(c):
39                self.assertEqual(R.order(roles(c["roles"]), V["hw"][c["hw"]]), c["gpu"])
40
41    def test_warnings(self):
42        for c in V["warnings"]:
43            with self.subTest(c):
44                w = R.warnings(roles(c["roles"]), V["hw"][c["hw"]], spill_gb=c.get("spill_gb", 0.0))
45                self.assertEqual([x["code"] for x in w], c["codes"])
46                if c.get("fix"):
47                    self.assertEqual(w[0]["fix"], c["fix"])
48
49    def test_saved(self):
50        for c in V["saved"]:
51            with self.subTest(c):
52                got, problem = R.saved_roles(c["cfg"])
53                self.assertEqual(got, roles(c["roles"]))
54                if c.get("problem"):
55                    self.assertIn(c["problem"], problem or "")
56
57    def test_legacy(self):
58        for c in V["legacy"]:
59            with self.subTest(c):
60                self.assertEqual(R.from_legacy(c["gpu"], V["hw"][c["hw"]], c.get("gpu_order")), roles(c["roles"]))
61
62
63class Words(unittest.TestCase):
64    def test_warnings_are_one_short_line_and_say_only_what_a_person_cannot_know(self):
65        two = V["hw"]["two"]
66        # a slower card as Main is NOT warned about: people understand it (owner rule)
67        self.assertEqual(R.warnings({0: "main", 1: "helper"}, two), [])
68        for case in V["warnings"]:
69            for w in R.warnings(roles(case["roles"]), V["hw"][case["hw"]], spill_gb=case.get("spill_gb", 0.0)):
70                self.assertLess(len(w["text"]), 90, w["text"])
71                self.assertNotIn("slower", w["text"].replace("is slow as Main", ""))
72
73    def test_cpu_only_line(self):
74        w = R.warnings({0: "off", 1: "off"}, V["hw"]["two"], engine_runs_cpu_only=True)
75        self.assertEqual(w[0]["text"], "Performance is degraded on CPU only.")
76        w = R.warnings({0: "off", 1: "off"}, V["hw"]["two"])
77        self.assertEqual(w[0]["text"], R.NO_CARD_LINE)
78
79    def test_sentence_and_describe(self):
80        self.assertEqual(R.sentence(V["hw"]["one"]), "Strata will use your RTX 4070 Ti.")
81        s = R.sentence(V["hw"]["three"])
82        self.assertIn("RTX 4070 Ti is Main: it is faster.", s)
83        self.assertIn("RTX 2060 starts Off: 8 GB or less.", s)
84        self.assertEqual(R.describe({0: "helper", 1: "main"}, V["hw"]["two"]), "Main: GPU 2 RTX 4070 Ti. Helper: GPU 1 RTX 3060.")
85
86    def test_cards_with_8_gb_or_less_start_off_but_a_lone_8_gb_card_is_main(self):
87        self.assertEqual(R.recommend([{"index": 0, "name": "RTX 4060", "vram_gb": 8}])["roles"], {0: "main"})
88        r = R.recommend([{"index": 0, "name": "A", "vram_gb": 12, "speed": 9}, {"index": 1, "name": "B", "vram_gb": 8, "speed": 5}, {"index": 2, "name": "C", "vram_gb": 10, "speed": 5}])
89        self.assertEqual(r["roles"], {0: "main", 1: "off", 2: "helper"})
90
91
92class MirrorAndParse(unittest.TestCase):
93    def test_mirror_writes_roles_and_the_legacy_fields_with_main_last(self):
94        m = R.mirror({0: "helper", 1: "main"}, V["hw"]["two"])
95        self.assertEqual(m["gpu_roles"], {"0": "helper", "1": "main"})
96        self.assertEqual((m["gpu"], m["gpu_order"]), ([0, 1], "as_given"))
97        self.assertNotIn("gpu_none", m)
98        one = R.mirror({0: "off", 1: "main"}, V["hw"]["two"])
99        self.assertEqual(one["gpu"], 1)                                   # one card is a number, never a list (#3203)
100        none = R.mirror({0: "off", 1: "off"}, V["hw"]["two"])
101        self.assertEqual((none["gpu"], none["gpu_none"]), ([], True))
102
103    def test_parse_roles(self):
104        cards = V["hw"]["three"]
105        self.assertEqual(R.parse_roles("1=main, 0=helper,2=off", cards), {1: "main", 0: "helper", 2: "off"})
106        self.assertEqual(R.roles_from_text("1=main,0=helper", cards), {0: "helper", 1: "main", 2: "off"})      # unnamed cards are Off
107        self.assertEqual(R.roles_from_text("0=helper", cards), {0: "main", 1: "off", 2: "off"})                 # a lone card on is Main
108        for bad in ("", "1", "1=boss", "x=main", "0=main,1=main", "7=main"):
109            with self.subTest(bad), self.assertRaises(ValueError):
110                R.parse_roles(bad, cards)
111
112    def test_normalize(self):
113        two = V["hw"]["two"]
114        self.assertEqual(R.normalize({0: "helper", 1: "helper"}, two), {0: "helper", 1: "main"})
115        self.assertEqual(R.normalize({0: "main", 1: "main"}, two), {0: "main", 1: "helper"})
116        self.assertEqual(R.normalize({0: "helper", 1: "off"}, two), {0: "main", 1: "off"})
117
118    def test_spill_uses_the_cards_that_are_on(self):
119        two = [{"index": 0, "name": "A", "vram_gb": 12}, {"index": 1, "name": "B", "vram_gb": 12}]
120        a = R.spill_for({0: "main", 1: "helper"}, two, 44.0, 32.0)
121        b = R.spill_for({0: "main", 1: "off"}, two, 44.0, 32.0)
122        self.assertLess(a, b)
123        self.assertEqual(R.spill_for({0: "main", 1: "off"}, two, None, 32.0), 0.0)
124
125
126if __name__ == "__main__":
127    unittest.main()