okami.mom/strata

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

Niko1221Merge f-022-int into f-022-gyrocuda: Pascal path kept for cc 6.0, 8-lane trellis rows for every other CUDA card01f8c3255

main
9.0 KiB164 linesraw
1"""strata_control/gpu_fit.py: the one rule for which graphics cards a model uses (app, strata launch and setup share it)."""
2
3from __future__ import annotations
4
5import re
6import unittest
7from pathlib import Path
8
9from strata_control import gpu_fit as G
10
11ROOT = Path(__file__).resolve().parents[2]
12
13
14def card(index, name, vram, **kw):
15    return {"index": index, "name": name, "vram_gb": vram, "usable": True, **kw}
16
17
18class GpuFitRule(unittest.TestCase):
19    def test_one_card_is_the_only_option(self):
20        r = G.options(44.0, 62.0, [card(0, "NVIDIA GeForce RTX 3070 Ti", 8)])
21        self.assertEqual([o["key"] for o in r["options"]], ["single"])
22        self.assertEqual(r["recommended"], "single")
23        self.assertEqual(r["options"][0]["verdict"], "fits")
24        self.assertIn("RTX 3070 Ti + RAM", r["options"][0]["label"])
25
26    def test_two_unequal_cards_and_the_model_fits_one_defaults_to_single_and_offers_the_split(self):
27        r = G.options(44.0, 62.0, [card(1, "NVIDIA GeForce RTX 3060 Ti", 8), card(0, "NVIDIA GeForce RTX 3070 Ti", 16)])
28        keys = [o["key"] for o in r["options"]]
29        self.assertEqual(keys[:2], ["single", "split"])             # the largest card first, then the split
30        self.assertEqual(r["recommended"], "single")
31        self.assertEqual(r["options"][0]["cards"], [0])             # the largest card is the 16 GB one
32        split = r["options"][1]
33        self.assertEqual(split["cards"], [0, 1])
34        self.assertIn("layer split", split["label"])
35        self.assertTrue(any("20% faster" in n for n in split["notes"]))
36        self.assertEqual(sum(1 for o in r["options"] if o["recommended"]), 1)
37
38    def test_two_small_cards_and_the_model_does_not_fit_one_defaults_to_the_split(self):
39        r = G.options(44.0, 32.0, [card(0, "NVIDIA GeForce RTX 3060", 10), card(1, "NVIDIA GeForce RTX 3060", 10)])
40        single, split = r["options"][0], r["options"][1]
41        self.assertEqual(single["verdict"], "spills")
42        self.assertLess(split["spill_gb"], single["spill_gb"])      # the second card takes part of the SSD reading away
43        self.assertEqual(r["recommended"], "split")
44        self.assertEqual(split["cards"], [0, 1])
45
46    def test_a_tight_single_card_moves_the_default_to_the_split_when_that_fits_with_room(self):
47        # 44 GB model, 46 GB of RAM: one 12 GB card + RAM holds it but leaves no RAM spare (tight); two cards leave room
48        r = G.options(44.0, 46.0, [card(0, "RTX 4070", 12), card(1, "RTX 4070", 12)])
49        self.assertEqual(r["options"][0]["verdict"], "tight")
50        self.assertEqual(r["options"][1]["verdict"], "fits")
51        self.assertEqual(r["recommended"], "split")
52
53    def test_an_integrated_chip_and_a_tiny_card_are_ignored(self):
54        cards = [card(0, "NVIDIA GeForce RTX 3070 Ti", 8), card(1, "AMD Radeon Graphics", 16, integrated=True), card(2, "Old display card", 2)]
55        r = G.options(44.0, 62.0, cards)
56        self.assertEqual([o["key"] for o in r["options"]], ["single"])
57        self.assertEqual({c["index"] for c in r["ignored"]}, {1, 2})
58        self.assertTrue(all(c["why_ignored"] for c in r["ignored"]))
59        self.assertEqual(G.recommended_cards(44.0, 62.0, cards), [0])
60
61    def test_a_unified_memory_chip_counts_when_it_is_the_only_device(self):
62        mac = {"index": 0, "name": "Apple M4", "vendor": "apple", "vram_gb": 0.0, "unified_gb": 32.0, "usable": True}
63        keep, ignored = G.usable_cards([mac])
64        self.assertEqual((len(keep), ignored), (1, []))
65        igpu = {"index": 0, "name": "AMD Radeon 8060S", "vram_gb": 64.0, "uma": True, "usable": True}
66        self.assertEqual(len(G.usable_cards([igpu])[0]), 1)                 # a Strix Halo PC: the integrated chip IS the device
67        keep, ignored = G.usable_cards([card(0, "RTX 3070 Ti", 8), igpu])   # beside a real card it adds nothing
68        self.assertEqual([c["index"] for c in keep], [0] if keep[0]["name"] == "RTX 3070 Ti" else [])
69        self.assertEqual(len(ignored), 1)
70
71    def test_a_strix_halo_counts_its_memory_once(self):
72        """The 'shared' pool of an APU is the RAM: a 70 GB model on a 128 GB Strix Halo with a 2 GB carve-out is RAM-resident, not 'VRAM + RAM'."""
73        igpu = {"index": 0, "name": "AMD Radeon 8060S", "vram_gb": 66.0, "dedicated_gb": 2.0, "uma": True, "usable": True}
74        self.assertEqual(G.own_memory_gb(igpu), 2.0)
75        self.assertEqual(G.own_memory_gb({"index": 1, "vram_gb": 32.0}), 32.0)
76        self.assertEqual(G.own_memory_gb({"index": 0, "vram_gb": 66.0, "uma": True}), 0.0)          # no carve-out figure: none counted
77        # 64 GB PC, a 90 GB model: counted twice (66 GB 'VRAM' + 54 GB RAM) it looked like it fit; once, 36 GB come from the SSD
78        r = G.options(90.0, 64.0, [dict(igpu, vram_gb=34.0)])
79        self.assertEqual(r["options"][0]["verdict"], "spills")
80        self.assertGreater(r["options"][0]["spill_gb"], 30.0)
81        # 128 GB, 90 GB model: fits in RAM
82        self.assertEqual(G.options(90.0, 128.0, [dict(igpu, vram_gb=66.0)])["options"][0]["verdict"], "fits")
83
84    def test_an_unusable_card_is_ignored_with_its_own_reason(self):
85        r = G.options(44.0, 62.0, [card(0, "RTX 3070 Ti", 8), card(1, "GTX 1060", 6, usable=False, problem="older than the RTX 20 series")])
86        self.assertEqual([o["key"] for o in r["options"]], ["single"])
87        self.assertIn("older than the RTX 20 series", r["ignored"][0]["why_ignored"])
88
89    def test_a_card_under_the_split_minimum_is_offered_alone_but_not_in_the_split(self):
90        r = G.options(44.0, 32.0, [card(0, "RTX 3060", 12), card(1, "GTX 1660", 6)])
91        keys = [o["key"] for o in r["options"]]
92        self.assertNotIn("split", keys)
93        self.assertEqual(r["recommended"], "single")
94
95    def test_three_cards_offer_the_pair_and_all_of_them(self):
96        r = G.options(70.0, 32.0, [card(i, "RTX 3060", 12) for i in range(3)])
97        keys = [o["key"] for o in r["options"]]
98        self.assertEqual(keys[:3], ["single", "split", "split-all"])
99
100    def test_no_card_gives_no_options(self):
101        r = G.options(44.0, 62.0, [])
102        self.assertEqual((r["options"], r["recommended"]), ([], None))
103
104    def test_unknown_ram_is_unknown_not_a_guess(self):
105        r = G.options(44.0, None, [card(0, "RTX 3070 Ti", 8), card(1, "RTX 3060 Ti", 8)])
106        self.assertEqual(r["options"][0]["verdict"], "unknown")
107        self.assertEqual(r["recommended"], "single")
108
109    def test_plain_numbers_are_in_the_words(self):
110        r = G.options(44.0, 62.0, [card(0, "NVIDIA GeForce RTX 3070 Ti", 8)])
111        self.assertIn("needs 44 GB", r["options"][0]["words"])
112        self.assertIn("RTX 3070 Ti + RAM", r["options"][0]["summary"])
113        r2 = G.options(44.0, 32.0, [card(0, "RTX 3060", 10)])
114        self.assertIn("read from the SSD", r2["options"][0]["words"])
115
116    def test_the_constants_equal_the_apps(self):
117        text = (ROOT / "apps" / "host" / "src" / "lib" / "native-fit.ts").read_text(encoding="utf-8")
118        m = re.search(r"const DENSE_GB = (\d+), SYSTEM_ROOM_GB = (\d+), DENSE_EXTRA_GB = (\d+)", text)
119        self.assertIsNotNone(m)
120        self.assertEqual((G.DENSE_GB, G.SYSTEM_ROOM_GB, G.DENSE_EXTRA_GB), tuple(float(x) for x in m.groups()))
121
122    def test_fit_matches_the_launch_catalog_port_of_native_fit(self):
123        from strata_cli.launch.catalog import fit_on
124        for ram, vram in ((62, 16), (32, 10), (16, 0), (128, 24)):
125            a = fit_on({"memory_gb": 44.0}, ram, vram)
126            b = G.fit(44.0, ram, vram)
127            self.assertAlmostEqual(a["spillGb"], b["spill_gb"])
128            self.assertAlmostEqual(a["vramUseGb"], b["vram_use_gb"])
129            self.assertAlmostEqual(a["ramUseGb"], b["ram_use_gb"])
130
131
132class GpuFitInTheModelList(unittest.TestCase):
133    """The app reads the options from the model list (tools/strata_mcp.py model_table): the same rule, nothing duplicated in the UI."""
134
135    def setUp(self):
136        from tests.terminal.launch_support import Home
137        self.home = Home().__enter__()
138        self.addCleanup(self.home.__exit__, None, None, None)
139
140    def sizes(self, hw):
141        from tests.terminal.launch_support import catalog
142        table = catalog(hw).strata.model_table(hw)
143        return {s["model"]: s for f in table for s in f["sizes"]}
144
145    def test_two_cards_give_options_on_every_model(self):
146        from tests.terminal.launch_support import HW_2X16
147        s = self.sizes(HW_2X16)["IQ3_XXS"]
148        res = s["gpu_options"]
149        self.assertEqual([o["key"] for o in res["options"]][:2], ["single", "split"])
150        self.assertEqual(res["recommended"], "single")
151
152    def test_one_card_has_no_choice_to_show(self):
153        from tests.terminal.launch_support import HW_12
154        self.assertIsNone(self.sizes(HW_12)["IQ3_XXS"]["gpu_options"])
155
156    def test_an_integrated_chip_beside_one_card_is_no_choice(self):
157        from tests.terminal.launch_support import HW_2X16
158        hw = dict(HW_2X16)
159        hw["gpus"] = [dict(HW_2X16["gpus"][0]), {"index": 1, "name": "AMD Radeon Graphics", "vram_gb": 16.0, "usable": False, "uma": True, "problem": "integrated graphics"}]
160        self.assertIsNone(self.sizes(hw)["IQ3_XXS"]["gpu_options"])
161
162
163if __name__ == "__main__":
164    unittest.main()