okami.mom/strata

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

Niko1221Merge f-clean-1c (3c32937a2): launcher speed settings become engine defaults (parallel stack, GDN stack, gfx12 chunked scan, HC_Q8 on UD files, Metal fp16 for resident IQ3_S/Q2_0, hipBLASLt table on gfx1151 only); row 8 held (cleanup step 1c)ddbfa1d5c

main
13.7 KiB254 linesraw
1"""Tests for setup.py's model menu (0.1.44): the family order, NVFP4 hidden on AMD and Intel, the NVFP4 line's card note
2(RTX 50 native FP4, older RTX slower than UD-IQ4_XS), the Unsloth sizes in order with images on, and the prebuilt
3install removing a stale strata_kq library.
4
5    python -m unittest tools.test_setup_menu
6"""
7from __future__ import annotations
8
9import re
10import sys
11import tempfile
12import unittest
13from unittest import mock
14from pathlib import Path
15
16ROOT = Path(__file__).resolve().parents[1]
17sys.path.insert(0, str(ROOT))
18sys.path.insert(0, str(ROOT / "tools"))
19import setup  # noqa: E402
20
21
22class FamilyOrder(unittest.TestCase):
23    def test_the_menu_order_on_nvidia(self):
24        fams = setup.menu_families(False) + list(setup.OWN_PATH_FAMILIES)
25        self.assertEqual(fams, ["qwen", "swift", "coder", "unsloth", "gyro", "nvfp4", "qwen36"])
26
27    def test_the_default_stays_the_first_family(self):
28        self.assertEqual(setup.menu_families(False)[0], "qwen")
29        self.assertIn("GSQ-RCO", setup.FAMILIES["qwen"]["by"])
30
31    def test_nvfp4_is_not_listed_on_amd_and_intel(self):
32        fams = setup.menu_families(True)
33        self.assertNotIn("nvfp4", fams)
34        self.assertEqual(fams, ["qwen", "swift", "coder", "unsloth", "gyro"])
35        self.assertIn("nvfp4", setup.FAMILIES)             # --family nvfp4 still exists, and says why not
36
37    def test_the_unsloth_line_names_the_alternative_on_amd_and_intel(self):
38        self.assertIn("UD-IQ4_XS", setup.family_menu_note("unsloth", True, None))
39        self.assertEqual(setup.family_menu_note("unsloth", False, {"arch": 86}), "")
40        self.assertEqual(setup.family_menu_note("coder", True, None), "")
41
42
43class Nvfp4Note(unittest.TestCase):
44    def test_rtx_50_has_native_fp4(self):
45        line = setup.family_menu_note("nvfp4", False, {"arch": 120, "name": "RTX 5070"})
46        self.assertIn("native FP4 on this card", line)
47
48    def test_older_rtx_runs_but_is_slower_than_ud_iq4_xs(self):
49        for arch in (75, 86, 89):
50            line = setup.family_menu_note("nvfp4", False, {"arch": arch})
51            self.assertIn("runs, but UD-IQ4_XS is faster on this card", line, arch)
52            self.assertNotIn("native FP4", line)
53
54    def test_an_avx2_cpu_is_told_its_rows_but_not_refused(self):      # #2347
55        line = setup.family_menu_note("nvfp4", False, {"arch": 120}, avx2=True, avx512=False)
56        self.assertIn("no AVX-512", line)
57        self.assertIn("AVX2", line)
58        self.assertIn("native FP4 on this card", line)               # the card part is unchanged
59        self.assertTrue(setup.nvfp4_cpu_note(True, False))
60
61    def test_an_avx512_cpu_and_a_cpu_without_avx2_get_no_cpu_note(self):
62        self.assertEqual(setup.nvfp4_cpu_note(True, True), "")
63        self.assertEqual(setup.nvfp4_cpu_note(False, False), "")     # the older-CPU build says its own piece
64        line512 = setup.family_menu_note("nvfp4", False, {"arch": 120})
65        self.assertNotIn("AVX", line512)
66        self.assertEqual(line512, setup.family_menu_note("nvfp4", False, {"arch": 120}, avx2=True, avx512=True))
67
68    def test_the_line_gives_the_download_and_the_ram_need(self):
69        line = setup.family_menu_note("nvfp4", False, {"arch": 120})
70        d = setup.MODELS["NVFP4"]
71        self.assertIn(f"{d['download_gb']:.0f} GB download", line)
72        self.assertIn(f"~{d['ram_gb']} GB of RAM", line)
73
74
75class UnslothSizes(unittest.TestCase):
76    def test_the_sizes_in_menu_order(self):
77        names = [m for m in setup.MODELS if "unsloth" in setup.MODELS[m].get("families", ())]
78        self.assertEqual(names, ["UD-IQ4_XS", "UD-Q4_K_XL", "UD-Q5_K_XL", "UD-Q6_K_XL", "Q8_0", "UD-IQ1_M", "UD-Q2_0", "UD-IQ2_XS", "UD-Q2_K_XL",
79                                 "UD-IQ3_XXS", "UD-Q3_K_XL", "UD-IQ4_XS-v31"])       # (round 0214 adds the v3.1 sets after the pinned five)
80
81    def test_nothing_is_experimental_and_images_are_on(self):
82        for m in ("UD-IQ4_XS", "UD-Q4_K_XL", "UD-Q5_K_XL", "UD-Q6_K_XL", "Q8_0"):
83            d = setup.MODELS[m]
84            self.assertFalse(d.get("experimental"), m)
85            self.assertFalse(d.get("nvidia_only"), m)
86            self.assertIs(d.get("vision"), True, m)
87            self.assertIn("download_gb", d)
88            self.assertIn("ram_gb", d)
89        self.assertNotIn("experimental", setup.MODELS["UD-Q4_K_XL"]["about"].lower())
90        self.assertTrue(setup.FAMILIES["unsloth"]["vision"])
91
92    def test_the_family_line_lists_every_size(self):
93        about = setup.FAMILIES["unsloth"]["about"]
94        for m in ("UD-IQ4_XS", "UD-Q4_K_XL", "UD-Q5_K_XL", "UD-Q6_K_XL", "Q8_0"):
95            self.assertIn(m, about)
96
97
98class RecommendedSizes(unittest.TestCase):
99    """0.1.44 (owner): IQ3_S, UD-Q4_K_XL and UD-IQ4_XS are the recommended sizes; recommend, never force."""
100
101    def names(self, family):
102        return [m for m in setup.MODELS if family in setup.MODELS[m].get("families", ("qwen", "swift"))]
103
104    def test_qwen_and_swift_default_to_iq3_s_when_it_fits(self):
105        for fam in ("qwen", "swift"):
106            n = self.names(fam)
107            self.assertIn("IQ3_S", n)
108            self.assertEqual(setup.recommended_size(fam, n, 67.7), "IQ3_S")
109            self.assertEqual(setup.recommended_size(fam, n, 191.0), "IQ3_S")      # not the largest: IQ3_S stays the pick
110            self.assertEqual([m for m in n if setup.size_tag(fam, m)], ["IQ3_S"])  # the others are choices, unmarked
111
112    def test_without_the_ram_the_largest_that_fits_and_iq3_s_stays_marked(self):
113        n = self.names("qwen")
114        self.assertEqual(setup.recommended_size("qwen", n, 61.0), "IQ3_XXS")      # IQ3_S needs 62 GB
115        self.assertEqual(setup.recommended_size("qwen", n, 52.0), "IQ3_XXS")      # 48 GB tier (RAM + VRAM - 5 GB)
116        self.assertEqual(setup.recommended_size("qwen", n, 40.0, 11.0), "IQ3_XXS")  # 40 + 11 - 5 = 46 GB together
117        self.assertEqual(setup.recommended_size("qwen", n, 32.0, 12.0), "IQ2_XS")  # 44 - 5 = 39 GB: the 40 GB tier
118        self.assertEqual(setup.recommended_size("swift", self.names("swift"), 32.0, 12.0), "IQ2_XS")
119        self.assertEqual(setup.recommended_size("qwen", n, 32.0, 35.0), "IQ3_S")   # 67 - 5 = 62 GB together
120        self.assertEqual(setup.size_tag("qwen", "IQ3_S"), " (recommended)")
121
122    def test_3425_the_tiers_leave_5_gb_for_windows(self):
123        # the reporter: RTX 5080 (16 GB) + 32 GB of RAM summed to 48 GB and got IQ3_XXS, which read ~11 GB from the SSD
124        n = self.names("qwen")
125        self.assertEqual(setup.TIER_SYSTEM_RESERVE_GB, 5.0)
126        self.assertEqual(setup.recommended_size("qwen", n, 31.9, 15.9), "IQ2_XS")
127        self.assertEqual(setup.tier_pick(31.9, 15.9), ("swift", "IQ2_XS"))
128        # boundaries: floors 38 / 46 / 62 plus the 5 GB
129        self.assertEqual(setup.tier_pick(43.0), ("swift", "IQ2_XS"))
130        self.assertIsNone(setup.tier_pick(42.9))
131        self.assertEqual(setup.tier_pick(51.0), ("qwen", "IQ3_XXS"))
132        self.assertEqual(setup.tier_pick(50.9), ("swift", "IQ2_XS"))
133        self.assertEqual(setup.tier_pick(67.0), ("qwen", "IQ3_S"))
134        self.assertEqual(setup.tier_pick(66.9), ("qwen", "IQ3_XXS"))
135        self.assertEqual(setup.tier_pick(40.0, 3.0), ("swift", "IQ2_XS"))      # VRAM counts the same as RAM
136
137    def test_coder_has_nothing_marked(self):
138        n = self.names("coder")
139        self.assertEqual([setup.size_tag("coder", m) for m in n], [""] * len(n))
140        self.assertEqual(setup.recommended_size("coder", n, 64.0), n[0])
141
142
143class UnslothDefaultEnv(unittest.TestCase):
144    """0.1.44 (owner): the env setup writes for Unsloth's files (review-quants.md section 5)."""
145
146    def test_q5_and_q6_turn_the_k_quant_prompt_kernels_on_q4_does_not(self):
147        f = setup.unsloth_default_env
148        self.assertEqual(f("unsloth", "UD-Q5_K_XL", False, False)["STRATA_MMQ_KQ"], "1")
149        self.assertEqual(f("unsloth", "UD-Q6_K_XL", True, True)["STRATA_MMQ_KQ"], "1")
150        for m in ("UD-Q4_K_XL", "UD-IQ4_XS", "Q8_0"):
151            self.assertNotIn("STRATA_MMQ_KQ", f("unsloth", m, False, False), m)
152
153    def test_hc_q8_is_the_engines_own_default_not_setups(self):
154        f = setup.unsloth_default_env
155        for m in ("UD-IQ4_XS", "UD-Q4_K_XL", "UD-Q5_K_XL", "UD-Q6_K_XL", "Q8_0"):
156            for hip in (False, True):
157                self.assertNotIn("STRATA_HC_Q8", f("unsloth", m, hip, True), (m, hip))
158
159    def test_tail_skip_off_only_when_every_expert_is_in_ram(self):
160        f = setup.unsloth_default_env
161        self.assertEqual(f("unsloth", "UD-Q4_K_XL", False, False)["STRATA_ROUTE_TAIL_SKIP"], "0")
162        self.assertNotIn("STRATA_ROUTE_TAIL_SKIP", f("unsloth", "UD-Q4_K_XL", False, True))   # budget mode keeps the skip
163
164    @mock.patch.object(setup, "WIN", False)       # "Linux" gfx12: setup asks the host OS (setup.WIN), so the test names Linux itself
165    def test_tail_skip_stays_on_for_linux_gfx12(self):
166        # owner 11-03 ("3 Yes"): on Linux gfx12 (R9700, RX 9000) setup no longer turns the skip off for the UD files; the engine's own default (rank 7) runs
167        f = setup.unsloth_default_env
168        for arch in ("gfx1201", "gfx1200", "gfx1201:sramecc-:xnack-"):
169            for m in ("UD-IQ4_XS", "UD-Q4_K_XL", "UD-Q5_K_XL", "UD-Q6_K_XL"):
170                self.assertNotIn("STRATA_ROUTE_TAIL_SKIP", f("unsloth", m, True, False, arch), (arch, m))
171        # every other card keeps the 0: other AMD (gfx1100, gfx1151), NVIDIA, no arch given (the old signature)
172        for arch in ("gfx1100", "gfx1151", "gfx1030", "", "sm_120"):
173            self.assertEqual(f("unsloth", "UD-Q4_K_XL", True, False, arch)["STRATA_ROUTE_TAIL_SKIP"], "0", arch)
174        self.assertEqual(f("unsloth", "UD-Q4_K_XL", False, False, "gfx1201")["STRATA_ROUTE_TAIL_SKIP"], "0")    # not hip
175        self.assertEqual(f("unsloth", "UD-Q4_K_XL", True, False)["STRATA_ROUTE_TAIL_SKIP"], "0")                 # the old call
176        # Windows gfx12 was not measured: it keeps the 0
177        old = setup.WIN
178        try:
179            setup.WIN = True
180            self.assertEqual(f("unsloth", "UD-Q4_K_XL", True, False, "gfx1201")["STRATA_ROUTE_TAIL_SKIP"], "0")
181        finally:
182            setup.WIN = old
183
184    def test_other_families_are_untouched(self):
185        for fam, m in (("qwen", "IQ3_S"), ("swift", "Q2_0"), ("coder", "IQ1_M"), ("nvfp4", "NVFP4")):
186            self.assertEqual(setup.unsloth_default_env(fam, m, False, False), {}, fam)
187
188
189class EnginePlaceholders(unittest.TestCase):
190    def test_every_engine_minimum_is_this_release(self):
191        self.assertEqual(setup.NVFP4_ENGINE, (0, 1, 44))
192        self.assertEqual(setup.MIN_ENGINE, (0, 1, 46))
193        for m in ("UD-Q5_K_XL", "UD-Q6_K_XL", "NVFP4", "NVFP4-NVIDIA"):
194            self.assertEqual(setup.MODELS[m]["engine"], (0, 1, 44), m)
195        # No minimum may name an engine newer than this tree (a placeholder for a release that does not exist yet would make setup
196        # ask for an engine nobody can download). The tree's version is read from CMakeLists.txt, so this does not go stale each release.
197        text = (ROOT / "CMakeLists.txt").read_text(encoding="utf-8")
198        tree = tuple(int(n) for n in re.search(r"project\(strata VERSION ([0-9.]+)", text).group(1).split("."))
199        for what, minimum in [("MIN_ENGINE", setup.MIN_ENGINE), ("NVFP4_ENGINE", setup.NVFP4_ENGINE)] + [(m, setup.MODELS[m]["engine"]) for m in setup.MODELS if "engine" in setup.MODELS[m]]:
200            self.assertLessEqual(tuple(minimum), tree[:len(minimum)], f"{what} {minimum} is ahead of the tree {tree}")
201
202
203class KqLibrary(unittest.TestCase):
204    def test_a_zip_without_strata_kq_does_not_leave_an_old_one(self):
205        with tempfile.TemporaryDirectory() as d:
206            eng, tmp = Path(d) / "engine", Path(d) / "new"
207            eng.mkdir()
208            tmp.mkdir()
209            (eng / setup.EXE).write_bytes(b"old")
210            (eng / setup.KQ_LIB).write_bytes(b"old kq")
211            (tmp / setup.EXE).write_bytes(b"new")
212            setup.install_unpacked(tmp, eng)
213            self.assertEqual((eng / setup.EXE).read_bytes(), b"new")
214            self.assertFalse((eng / setup.KQ_LIB).exists())
215            self.assertEqual((eng / setup.PREVIOUS_ENGINE / setup.KQ_LIB).read_bytes(), b"old kq")   # a rollback gets it back
216
217    def test_a_zip_with_strata_kq_replaces_it(self):
218        with tempfile.TemporaryDirectory() as d:
219            eng, tmp = Path(d) / "engine", Path(d) / "new"
220            eng.mkdir()
221            tmp.mkdir()
222            (eng / setup.KQ_LIB).write_bytes(b"old kq")
223            (tmp / setup.EXE).write_bytes(b"new")
224            (tmp / setup.KQ_LIB).write_bytes(b"new kq")
225            setup.install_unpacked(tmp, eng)
226            self.assertEqual((eng / setup.KQ_LIB).read_bytes(), b"new kq")
227
228
229class ImageEncoderDefault(unittest.TestCase):
230    """#2406: "images: yes" gets the CPU encoder on an NVIDIA card under 24 GB (the GPU encoder cost 3-5% of text speed on
231    16 GB cards, 35-60% on an RTX 4080 SUPER under Windows); --vision gpu is kept with a note."""
232
233    def test_yes_follows_the_card(self):
234        f = setup.flash_vision_encoder
235        self.assertEqual(f("yes", {"vram_gb": 12.0}), "cpu")
236        self.assertEqual(f("yes", {"vram_gb": 15.9}), "cpu")
237        self.assertEqual(f("yes", {"vram_gb": 24.0}), "gpu")
238        self.assertEqual(f("yes", {"vram_gb": 32.0}), "gpu")
239        self.assertEqual(f("yes", {}), "gpu")                    # unknown VRAM: the old default
240        self.assertEqual(f("cpu", {"vram_gb": 32.0}), "cpu")
241        self.assertEqual(f("none", {"vram_gb": 16.0}), "none")
242
243    def test_gpu_asked_is_kept_with_a_note(self):
244        import contextlib, io
245        out = io.StringIO()
246        with contextlib.redirect_stdout(out):
247            self.assertEqual(setup.flash_vision_encoder("gpu", {"vram_gb": 16.0}), "gpu")
248            self.assertEqual(setup.flash_vision_encoder("gpu", {"vram_gb": 32.0}), "gpu")
249        self.assertEqual(out.getvalue().count("kept as you chose"), 1)
250        self.assertIn("#2406", out.getvalue())
251
252
253if __name__ == "__main__":
254    unittest.main()