okami.mom/strata

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

Niko1221setup: UD-* on a card of about 8 GB - estimate the VRAM the engine needs, write the least harmful config that starts (4-bit KV, smaller context, then no draft layer on the GPU), say so, warn that UD-Q4 is very slow there and suggest IQ3_XXS / IQ3_S; testscaac71139

main
6.4 KiB131 linesraw
1"""Tests for setup's UD-* start on a card of about 8 GB (0.1.44): the VRAM estimate, the plan that picks the least harmful
2config that starts, and the words (UD-Q4 is slow here: pick IQ3_XXS / IQ3_S).  Measured on an 8 GB Arc A750: setup's
38192 / int8 / reserve 400 start stopped with "no VRAM is left for the expert cache, short 473 MiB".
4
5    python -m unittest tools.test_setup_small_card
6"""
7from __future__ import annotations
8
9import sys
10import unittest
11from pathlib import Path
12from unittest import mock
13
14ROOT = Path(__file__).resolve().parents[1]
15sys.path.insert(0, str(ROOT))
16sys.path.insert(0, str(ROOT / "tools"))
17import setup  # noqa: E402
18from test_setup_unsloth import Base  # noqa: E402
19
20
21class Estimate(unittest.TestCase):
22    def test_the_a750_measurement(self):
23        # 8192 / int8 / reserve 400 was 473 MiB short; 4608 / q4_0 / reserve 0 started (without the draft layer's slack)
24        self.assertAlmostEqual(setup.unsloth_vram_slack_mib(8.0, 8192, "int8", 400), -473, delta=2)
25        self.assertGreaterEqual(setup.unsloth_vram_slack_mib(8.0, 4608, "q4_0", 0), 0)
26
27    def test_a_bigger_card_has_room(self):
28        self.assertGreater(setup.unsloth_vram_slack_mib(12.0, 32768, "int8", 700), setup.UD_FIT_MIN_SLACK_MIB)
29
30    def test_cuda_and_hip_pay_for_the_runtime_sycl_does_not(self):
31        self.assertEqual(setup.unsloth_runtime_mib({"driver": "i915"}), 0)
32        self.assertEqual(setup.unsloth_runtime_mib({"driver": "xe"}), 0)
33        self.assertEqual(setup.unsloth_runtime_mib({"driver": "580.97"}), setup.UD_FIT_RUNTIME_MIB)
34        self.assertEqual(setup.unsloth_runtime_mib({"driver": "amdgpu"}), setup.UD_FIT_RUNTIME_MIB)
35
36
37class Plan(unittest.TestCase):
38    def test_a_card_that_fits_is_not_changed(self):
39        self.assertEqual(setup.unsloth_small_card_plan(10.0, 8192, "fp16", None)[:3], (8192, "fp16", True))
40
41    def test_8gb_drops_the_draft_layer_first(self):
42        # nothing else frees enough on 8 GB: the context and the KV alone do not fit with the draft layer
43        for kv in ("fp16", "int8"):
44            plan = setup.unsloth_small_card_plan(8.0, 8192, kv, 300)
45            self.assertIsNotNone(plan)
46            self.assertFalse(plan[2])
47            self.assertEqual(plan[:2], (8192, kv))        # the context and the KV the user would get are kept
48            self.assertGreaterEqual(plan[3], setup.UD_FIT_MIN_SLACK_MIB)
49
50    def test_a_card_just_short_changes_the_kv_before_the_context_before_the_draft(self):
51        # ~8.9 GB: the 4-bit KV is enough with the draft layer
52        vg = (setup.UD_FIT_FIXED_MIB + setup.UD_FIT_MTP_MIB + 8192 * 13 * 576 / 2 ** 20 + 300 + 80) / 1024
53        plan = setup.unsloth_small_card_plan(vg, 8192, "fp16", 300)
54        self.assertEqual(plan[:3], (8192, "q4_0", True))
55        # ... and a smaller context when the user fixed the KV
56        vg = (setup.UD_FIT_FIXED_MIB + setup.UD_FIT_MTP_MIB + 4608 * 13 * 2112 / 2 ** 20 + 300 + 80) / 1024
57        plan = setup.unsloth_small_card_plan(vg, 8192, "fp16", 300, kv_fixed=True)
58        self.assertEqual(plan[:3], (4608, "fp16", True))
59
60    def test_what_the_user_chose_is_not_changed(self):
61        plan = setup.unsloth_small_card_plan(8.0, 32768, "int8", 300, ctx_fixed=True, kv_fixed=True)
62        self.assertEqual(plan[:2], (32768, "int8"))
63
64    def test_a_6gb_card_has_no_plan(self):
65        self.assertIsNone(setup.unsloth_small_card_plan(6.0, 8192, "int8", None))
66
67    def test_a_reserve_the_user_gave_is_counted(self):
68        a = setup.unsloth_small_card_plan(8.0, 8192, "int8", 0)
69        b = setup.unsloth_small_card_plan(8.0, 8192, "int8", 1500)
70        self.assertGreater(a[3], b[3] if b else -1e9)
71
72
73class Words(unittest.TestCase):
74    def test_always_says_to_pick_iq3(self):
75        lines = setup.unsloth_small_card_lines("UD-Q4_K_XL", 8.0, (8192, "int8", False, 400.0), False)
76        text = " ".join(lines)
77        self.assertIn("very slow", text)
78        self.assertIn("IQ3_XXS or IQ3_S", text)
79
80    def test_no_plan_says_the_start_will_probably_stop(self):
81        lines = setup.unsloth_small_card_lines("UD-Q4_K_XL", 6.0, None, False)
82        self.assertTrue(any("will probably" in x for x in lines))
83
84
85class Main8GB(Base):
86    """setup.main() for UD-Q4_K_XL on an 8 GB NVIDIA card: the written config is the one that starts."""
87
88    def arg(self, cfg, name):
89        return cfg["args"][cfg["args"].index(name) + 1]
90
91    def test_nvidia_8gb_writes_a_config_that_fits_and_says_so(self):
92        code, out, cfg = self.main(["--context", "8192"], vram=8.0)
93        self.assertEqual(code, 0, out)
94        self.assertNotIn("--mtp", cfg["args"])               # no draft layer on the GPU
95        self.assertEqual(self.arg(cfg, "--max-context"), "8192")   # the context the user asked for
96        self.assertIn("would not start on this 8 GB card", out)
97        self.assertIn("no MTP draft layer", out)
98        self.assertIn("IQ3_XXS or IQ3_S", out)
99        self.assertIn("very slow", out)
100
101    def test_a_context_and_kv_the_user_chose_are_kept(self):
102        code, out, cfg = self.main(["--context", "16384", "--kv", "int8"], vram=8.0)
103        self.assertEqual(code, 0, out)
104        self.assertEqual(self.arg(cfg, "--max-context"), "16384")
105        self.assertEqual(self.arg(cfg, "--kv"), "int8")
106
107    def test_the_default_context_on_8gb_starts_without_asking(self):
108        code, out, cfg = self.main([], vram=8.0)
109        self.assertEqual(code, 0, out)
110        self.assertEqual(self.arg(cfg, "--max-context"), "8192")
111        self.assertNotIn("--mtp", cfg["args"])
112
113    def test_a_12gb_card_is_untouched(self):
114        code, out, cfg = self.main(["--context", "8192"], vram=11.9)
115        self.assertEqual(code, 0, out)
116        self.assertIn("--mtp", cfg["args"])
117        self.assertNotIn("would not start", out)
118        self.assertNotIn("very slow", out)
119
120    def test_amd_8gb(self):
121        r = [{"index": 0, "name": "AMD Radeon RX 7600", "vram_gb": 8.0, "arch": "gfx1102", "driver": "amdgpu"}]
122        with mock.patch.object(setup, "get_prebuilt_hip", lambda *a, **k: self.t / "engine"), \
123                mock.patch.object(setup, "build_engine_hip", lambda *a, **k: self.t / "engine"),                 mock.patch.object(setup, "hip_card", lambda eng, gpu, listed: gpu):
124            code, out, cfg = self.main(["--context", "8192", "--backend", "hip", "--vision", "no"], amd=r)
125        self.assertEqual(code, 0, out)
126        self.assertNotIn("--mtp", cfg["args"])
127        self.assertIn("IQ3_XXS or IQ3_S", out)
128
129
130if __name__ == "__main__":
131    unittest.main()