okami.mom/strata

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

Niko1221Merge f-0213-defaults (a04860fad): DFlash2 per-device defaults - Strix Halo and Blackwell cc 12.0 cards with >= 48 GB draft window 6 -> 8 (all sets), conf 0.55 for IQ3_S only on that Blackwell class; end to end through the Terminal's flags, 3 interleaved rounds: Strix UD-Q4_K_XL x1.052 (output differs, deterministic; owner 11-08: ship as is), Strix UD-IQ4_XS-v31 x1.020 (identical), l620 IQ3_S x1.028 (identical); every other card unchangede3bd5cfde

main
7.9 KiB132 linesraw
1"""The per-card DFlash2 window and confidence of 0.2.13 (owner 11-08, RESULTS-CONF.md): Strix Halo window 8 (conf 0.35), Blackwell cc 12.0 with >= 48 GB window 8 and conf 0.55.
2
3Every other card keeps conf and --spec 6, so its EFFECTIVE window (--spec lowered to the engine's cap, include/strata/core/draft_cap.hpp) is what it was.
4"""
5
6from __future__ import annotations
7
8import hashlib
9import tempfile
10import unittest
11from pathlib import Path
12from unittest import mock
13
14from strata_control import drafting
15
16IQ3S = "qwen3.8-flash-next-iq3_s"
17Q4 = "qwen3.8-flash-next-unsloth-ud-q4_k_xl"
18V31 = "qwen3.8-flash-next-unsloth-ud-iq4_xs-v31"
19
20GIB = 1 << 30
21TUNED = b"tuned-pack-" * 200_000
22
23
24def old_cap(uma, major):       # include/strata/core/draft_cap.hpp before 11-08
25    return 5 if uma else 6 if major == 12 else 0
26
27
28def new_cap(uma, major, minor, vram_bytes):      # the same file after (mirrored here; src/core/draft_cap_test.cpp tests the C++)
29    if uma:
30        return 8
31    if major == 12:
32        return 8 if (minor == 0 and vram_bytes >= 48_000_000_000) else 6
33    return 0
34
35
36def effective(spec, cap):
37    return min(spec, cap) if cap > 0 else spec
38
39
40class CardWindow(unittest.TestCase):
41    def setUp(self):
42        self.tmp = tempfile.TemporaryDirectory()
43        self.addCleanup(self.tmp.cleanup)
44        self.data = Path(self.tmp.name)
45        (self.data / "dflash2").mkdir()
46        (self.data / "dflash2" / drafting.PACK_NAME).write_bytes(b"s" * (2 << 20))
47        (self.data / "dflash2" / drafting.VOCAB_NAME).write_bytes(b"v" * 100)
48        self.exe = self.data / "strata"
49        self.exe.write_bytes(b"--mtp-draft-vocab FILE (or --mtp DIR ... --draft-fallback ... selector codebooks: Q4_0 rows")
50        for patcher in (mock.patch.object(drafting, "ENGINE_SERVES_DFLASH2", True),
51                        mock.patch.multiple(drafting, TUNED_BYTES=len(TUNED), TUNED_SHA256=hashlib.sha256(TUNED).hexdigest())):
52            patcher.start()
53            self.addCleanup(patcher.stop)
54
55    def flags(self, name, backend, archs, vram, extra=(), user_args=None, tuned=False):
56        tuned_file = self.data / "dflash2" / drafting.TUNED_NAME
57        if tuned:
58            tuned_file.write_bytes(TUNED)
59        else:
60            tuned_file.unlink(missing_ok=True)
61        cfg = {"model_name": name, "args": ["--native", "m.gguf", *extra], "drafting": "dflash2"}
62        if user_args:
63            cfg["user_args"] = list(user_args)
64        with mock.patch.object(drafting, "detect_hardware", return_value=(backend, list(archs))):
65            d = drafting.apply(cfg, data_dir=self.data, backend=backend, archs=list(archs), engine=self.exe, vram_mib=vram)
66        self.assertEqual(d.drafter, "dflash2")
67        a = cfg["args"]
68        return a[a.index("--spec") + 1], a[a.index("--draft-conf") + 1]
69
70    def test_the_constants(self):
71        self.assertEqual((drafting.SPEC_WINDOW, drafting.WIDE_WINDOW, drafting.DRAFT_CONF), (6, 8, 0.35))
72        self.assertEqual(drafting.BIG_CARD_BYTES, 48_000_000_000)
73
74    def test_strix_halo_window_8_conf_unchanged(self):
75        for name in (IQ3S, Q4, V31):
76            self.assertEqual(self.flags(name, "hip", ["gfx1151"], None), ("8", "0.35"), name)
77
78    def test_blackwell_cc_12_0_with_48_gb_or_more(self):
79        """Window 8 for every set; conf 0.55 for IQ3_S only (the set measured there) and only with the tuned pack."""
80        for mib in (73728, 73415, 49152, 49140, 98304, 45777):         # 72 GB, the l620 reading, 48 GiB, an RTX A6000-class 48 GB, 96 GB, the 48e9 byte line
81            self.assertEqual(self.flags(IQ3S, "cuda", ["sm_120"], mib, tuned=True), ("8", "0.55"), mib)
82            self.assertEqual(self.flags(IQ3S, "cuda", ["sm_120"], mib), ("8", "0.35"), "the stock pack was not measured at 0.55")
83            for name in (V31, Q4):
84                self.assertEqual(self.flags(name, "cuda", ["sm_120"], mib, tuned=True), ("8", "0.35"), (name, mib))
85                self.assertEqual(self.flags(name, "cuda", ["sm_120"], mib), ("8", "0.35"), (name, mib))
86
87    def test_blackwell_cc_12_0_under_48_gb_keeps_today(self):
88        for mib in (32768, 24576, 16384, 12288, 45776, None):           # RTX 5090 32 GB .. 5070 12 GB, just under the line, memory unknown (several cards)
89            self.assertEqual(self.flags(IQ3S, "cuda", ["sm_120"], mib, tuned=True), ("6", f"{drafting.conf_for('cuda', drafting.slots_of([], mib)):g}"), mib)     # the slot rule, as before
90        self.assertEqual(self.flags(IQ3S, "cuda", ["sm_120"], 32768)[1], "0.35")
91
92    def test_the_spark_is_unchanged(self):
93        for tuned in (False, True):
94            self.assertEqual(self.flags(IQ3S, "cuda", ["sm_121"], None, tuned=tuned), ("6", "0.35"))
95            self.assertEqual(self.flags(V31, "cuda", ["sm_121"], None, tuned=tuned), ("6", "0.35"))
96            self.assertEqual(self.flags(IQ3S, "cuda", ["sm_121"], 131072, tuned=tuned), ("6", "0.35"), "even when a reading of its pool is passed")
97
98    def test_the_big_card_with_a_small_expert_cache_keeps_the_slot_rule(self):
99        self.assertEqual(self.flags(IQ3S, "cuda", ["sm_120"], 73728, extra=["--expert-cache", "1000"], tuned=True), ("6", "0.8"))
100
101    def test_other_cards_are_unchanged(self):
102        for backend, archs, mib in (("cuda", ["sm_86"], 12288), ("cuda", ["sm_89"], 24576), ("cuda", ["sm_90"], 81920), ("cuda", ["sm_86"], 49152), ("hip", ["gfx1201"], 32768),
103                                    ("hip", ["gfx1100"], 24576), ("hip", ["gfx1103"], None), ("sycl", ["arc_a"], None), ("metal", [], None)):
104            spec, conf = self.flags(Q4 if backend == "hip" else V31, backend, archs, mib)
105            self.assertEqual(spec, "6", (backend, archs))
106            self.assertEqual(conf, f"{drafting.conf_for(backend, drafting.slots_of([], mib)):g}", (backend, archs))      # the stock pack here: the backend's own rule
107
108    def test_the_persons_own_flags_win(self):
109        self.assertEqual(self.flags(IQ3S, "cuda", ["sm_120"], 73728, user_args=["--draft-conf", "0.4", "--spec", "5"], extra=["--draft-conf", "0.4", "--spec", "5"], tuned=True)[1], "0.4")
110        cfg = {"model_name": IQ3S, "args": ["--native", "m.gguf", "--spec", "5"], "user_args": ["--spec", "5"], "drafting": "dflash2"}
111        with mock.patch.object(drafting, "detect_hardware", return_value=("hip", ["gfx1151"])):
112            drafting.apply(cfg, data_dir=self.data, backend="hip", archs=["gfx1151"], engine=self.exe)
113        self.assertEqual(cfg["args"].count("--spec"), 1)
114        self.assertEqual(cfg["args"][cfg["args"].index("--spec") + 1], "5")
115
116    def test_no_card_but_the_two_classes_changes_its_effective_window(self):
117        """--spec as written, lowered to the engine's cap: the same number as before for every card but Strix Halo (5 -> 8) and the large Blackwell class (6 -> 8)."""
118        cards = [("hip", ["gfx1151"], None, True, 0, 0, 0)]
119        for mib, major, minor in ((73728, 12, 0), (98304, 12, 0), (49152, 12, 0), (32768, 12, 0), (16384, 12, 0), (12288, 12, 0), (None, 12, 0), (131072, 12, 1), (None, 12, 1),
120                                  (12288, 8, 6), (24576, 8, 9), (81920, 9, 0), (49152, 8, 6), (73728, 10, 0)):
121            cards.append(("cuda", [f"sm_{major}{minor}"], mib, False, major, minor, (mib or 0) * (1 << 20)))
122        for backend, archs, mib, uma, major, minor, vram in (*cards, ("hip", ["gfx1201"], 32768, False, 0, 0, 0), ("hip", ["gfx1100"], 24576, False, 0, 0, 0), ("sycl", ["arc_a"], None, False, 0, 0, 0), ("metal", [], None, False, 0, 0, 0)):
123            spec = int(self.flags(IQ3S if backend != "hip" else Q4, backend, archs, mib)[0])
124            old = effective(6, old_cap(uma, major))
125            new = effective(spec, new_cap(uma, major, minor, vram))
126            big = backend == "cuda" and major == 12 and minor == 0 and vram >= 48_000_000_000
127            want = 8 if (uma or big) else old
128            self.assertEqual(new, want, (backend, archs, mib, "old", old))
129
130
131if __name__ == "__main__":
132    unittest.main()