okami.mom/strata

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

Niko1221rc1 merge df2-vocab (1063d6fa4)58169905b

main
7.4 KiB158 linesraw
1"""The draft-vocabulary subset a person chose in setup (--draft-vocab en / fr / cyrillic) reaches DFlash2, which is the default drafter: on an install that has NO MTP folder
2(a fresh DFlash2-only install) as well as on one that has it.  Pure file work in temporary folders, no GPU, no downloads, no prompts.
3
4    python -m unittest tools.test_dflash2_vocab -v
5
6What is checked, per choice (cjk = the default file, en, cyrillic, fr):
7  - setup's vocab step (provide_dflash2_vocab / refresh_draft_vocab / refresh_shared_draft_vocab) and then strata_control.drafting.apply: the file `--mtp-draft-vocab` points at is the chosen subset
8    byte for byte, for an explicit `dflash2` and for `auto`;
9  - a later choice replaces a shipped subset, a subset made by hand stays;
10  - an update (refresh_shared_draft_vocab) and the drafter's own fetch (provide_vocab without a choice) keep the person's choice.
11"""
12from __future__ import annotations
13
14import hashlib
15import inspect
16import json
17import sys
18import tempfile
19import unittest
20from pathlib import Path
21
22ROOT = Path(__file__).resolve().parents[1]
23sys.path.insert(0, str(ROOT))
24import setup  # noqa: E402
25from strata_control import drafting  # noqa: E402
26
27CHOICES = ("cjk", "en", "cyrillic", "fr")
28
29
30def sha(p) -> str:
31    return hashlib.sha256(Path(p).read_bytes()).hexdigest()
32
33
34def shipped(choice: str) -> Path:
35    return ROOT / "data" / setup.DRAFT_VOCABS[choice]
36
37
38def vocab_arg(cfg: dict) -> Path:
39    a = cfg["args"]
40    return Path(a[a.index("--mtp-draft-vocab") + 1])
41
42
43@unittest.skipUnless(all(shipped(c).exists() for c in CHOICES), "the shipped draft subsets are not in this checkout")
44class Base(unittest.TestCase):
45    with_mtp = False
46
47    def setUp(self):
48        self._tmp = tempfile.TemporaryDirectory()
49        self.addCleanup(self._tmp.cleanup)
50        self.data = Path(self._tmp.name) / "data"
51        (self.data / "packs" / "m").mkdir(parents=True)
52        (self.data / "dflash2").mkdir(parents=True)
53        (self.data / "dflash2" / drafting.PACK_NAME).write_bytes(b"\0" * ((1 << 20) + 16))   # "downloaded" (size is all drafting.pack_ready looks at)
54        self.rt = self.data / "mtp" / "rt"
55        if self.with_mtp:
56            self.rt.mkdir(parents=True)
57            (self.rt / "experts.bin").write_bytes(b"e")
58            (self.rt / "draft_vocab.bin").write_bytes(shipped("cjk").read_bytes())      # what an install of the MTP layer holds first
59
60    # ---- what setup does for a model with this draft_vocab choice
61    def setup_step(self, choice: str) -> None:
62        """setup.py's vocab step on an install with a DFlash2 drafter: the DFlash2 copy (beside the pack) and, when there is an MTP folder, the folder's file."""
63        takes_choice = "choice" in inspect.signature(setup.provide_dflash2_vocab).parameters
64        if takes_choice:
65            setup.provide_dflash2_vocab(self.data, choice)
66        else:
67            setup.provide_dflash2_vocab(self.data)               # the code before this branch
68        if (self.rt / "experts.bin").exists():
69            setup.refresh_draft_vocab(self.rt, choice)
70
71    def config(self, mode: str = "dflash2", choice: str | None = None) -> dict:
72        args = ["--pack", str(self.data / "packs" / "m"), "--native", "x.gguf", "--mtp", str(self.rt), "--spec", "4", "--spec-min-p", "0.5"]
73        cfg = {"args": args, "model_name": "qwen3.8-flash-next-iq3_s", "drafting": mode}
74        if choice:
75            cfg["draft_vocab"] = choice
76        return cfg
77
78    def applied(self, mode: str = "dflash2") -> dict:
79        cfg = self.config(mode)
80        d = drafting.apply(cfg, data_dir=self.data, family="qwen", backend="cuda", archs=["sm_121"], slots=0)
81        self.assertEqual(d.drafter, "dflash2", d)
82        return cfg
83
84
85class DFlash2Only(Base):
86    with_mtp = False
87
88    def test_each_choice_reaches_the_engine(self):
89        for choice in CHOICES:
90            with self.subTest(choice=choice):
91                self.setup_step(choice)
92                self.assertEqual(sha(vocab_arg(self.applied())), sha(shipped(choice)), f"--mtp-draft-vocab is not the {choice} subset")
93
94    def test_auto_uses_the_choice(self):
95        for choice in CHOICES:
96            with self.subTest(choice=choice):
97                self.setup_step(choice)
98                self.assertEqual(sha(vocab_arg(self.applied("auto"))), sha(shipped(choice)))
99
100    def test_a_later_choice_replaces_a_shipped_subset_and_a_hand_made_one_stays(self):
101        self.setup_step("fr")
102        self.assertEqual(sha(vocab_arg(self.applied())), sha(shipped("fr")))
103        self.setup_step("en")
104        self.assertEqual(sha(vocab_arg(self.applied())), sha(shipped("en")))
105        self.setup_step("cjk")
106        self.assertEqual(sha(vocab_arg(self.applied())), sha(shipped("cjk")))
107        mine = drafting.vocab_target(self.data)
108        mine.write_bytes(bytes([1, 0, 0, 0]))
109        self.setup_step("fr")
110        self.assertEqual(mine.read_bytes(), bytes([1, 0, 0, 0]), "a subset made by hand is kept")
111
112    def test_an_update_and_the_drafters_fetch_keep_the_choice(self):
113        self.setup_step("fr")
114        cfg = self.config(choice="fr")
115        setup.refresh_shared_draft_vocab(cfg, set())                       # setup.py --update, the saved config names fr
116        self.assertEqual(sha(vocab_arg(self.applied())), sha(shipped("fr")))
117        drafting.provide_vocab(self.data)                                  # the fetch (Load, `strata drafting fetch`) never overwrites a copy that is there
118        self.assertEqual(sha(vocab_arg(self.applied())), sha(shipped("fr")))
119
120    def test_an_update_applies_the_saved_choice_to_an_older_copy(self):
121        drafting.provide_vocab(self.data)                                  # an install made before the choice reached DFlash2: the default copy
122        self.assertEqual(sha(drafting.vocab_target(self.data)), sha(shipped("cjk")))
123        setup.refresh_shared_draft_vocab(self.config(choice="cyrillic"), set())
124        self.assertEqual(sha(vocab_arg(self.applied())), sha(shipped("cyrillic")))
125
126    def test_no_choice_means_the_default_file(self):
127        setup.refresh_shared_draft_vocab(self.config(), set())             # no "draft_vocab" key: nothing to apply, nothing broken
128        drafting.provide_vocab(self.data)
129        self.assertEqual(sha(vocab_arg(self.applied())), sha(shipped("cjk")))
130
131
132class WithMtpFolder(Base):
133    with_mtp = True
134
135    def test_each_choice_reaches_the_engine(self):
136        for choice in CHOICES:
137            with self.subTest(choice=choice):
138                self.setup_step(choice)
139                self.assertEqual(sha(vocab_arg(self.applied())), sha(shipped(choice)))
140                self.assertEqual(sha(self.rt / "draft_vocab.bin"), sha(shipped(choice)), "the MTP layer's file follows too")
141                if drafting.vocab_target(self.data).exists():
142                    self.assertEqual(sha(drafting.vocab_target(self.data)), sha(shipped(choice)), "the DFlash2 copy agrees with the folder")
143
144    def test_auto_uses_the_choice(self):
145        self.setup_step("fr")
146        self.assertEqual(sha(vocab_arg(self.applied("auto"))), sha(shipped("fr")))
147
148    def test_the_two_files_never_disagree_after_an_update(self):
149        self.setup_step("en")
150        setup.refresh_shared_draft_vocab(self.config(choice="fr"), set())
151        self.assertEqual(sha(self.rt / "draft_vocab.bin"), sha(shipped("fr")))
152        if drafting.vocab_target(self.data).exists():
153            self.assertEqual(sha(drafting.vocab_target(self.data)), sha(shipped("fr")))
154        self.assertEqual(sha(vocab_arg(self.applied())), sha(shipped("fr")))
155
156
157if __name__ == "__main__":
158    unittest.main()