From d672a5f44380689fb2574ead8225f496830a44fa Mon Sep 17 00:00:00 2001 From: Niko1221 Date: Thu, 1 Oct 2026 08:14:00 +0200 Subject: [PATCH] #287 follow-up: setup run again without --draft-vocab keeps the model's earlier choice The full setup rewrites the model's config and refreshed the MTP folder's subset with `a.draft_vocab or "cjk"`, so running it again without the flag put the CJK subset back over a cyrillic (or en) choice, and dropped draft_vocab from the config. saved_draft_vocab() reads the choice the config holds; the flag still wins. tools/test_setup_draft_vocab.py. --- setup.py | 18 +++++++++-- tools/test_setup_draft_vocab.py | 53 +++++++++++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 3 deletions(-) create mode 100644 tools/test_setup_draft_vocab.py diff --git a/setup.py b/setup.py index 060edab4..a5cbb38b 100644 --- a/setup.py +++ b/setup.py @@ -1814,6 +1814,16 @@ OLD_DRAFT_VOCABS = {"369151522226a5edaa5f12cfd1e2ae7db8f4fbdbd222f3dcf327dced959 DRAFT_VOCABS = {"cjk": "draft_vocab.bin", "en": "draft_vocab_en.bin", "cyrillic": "draft_vocab_cyrillic.bin"} +def saved_draft_vocab(cfg_path: Path) -> str | None: + """The draft subset a model's config chose earlier (--draft-vocab), or None: a setup run again without the flag + rewrites the config, and would otherwise put the default subset back.""" + try: + v = json.loads(cfg_path.read_text(encoding="utf-8-sig")).get("draft_vocab") + except (OSError, ValueError, AttributeError): + return None + return v if v in DRAFT_VOCABS else None + + def refresh_draft_vocab(rt: Path, choice: str = "cjk") -> None: """The draft layer's token subset in the MTP folder: `cjk` (data/draft_vocab.bin, since 0.1.27, #137), `en` (data/draft_vocab_en.bin, the English/code subset before it: ~110 MiB less VRAM, English answers 1-2% faster) or @@ -2393,7 +2403,9 @@ def main() -> int: "--out", str(mtp / "mtp-q2_0.gguf")], env=env) run([sys.executable, str(ROOT / "tools" / "mtp_rt.py"), "--gguf", str(mtp / "mtp-q2_0.gguf"), "--out", str(rt)], env=env) - refresh_draft_vocab(rt, a.draft_vocab or "cjk") + # a setup run again without --draft-vocab keeps the subset this model's config chose before (cyrillic, en) + draft_vocab = a.draft_vocab or saved_draft_vocab(ROOT / f"strata-{tag.lower()}.json") + refresh_draft_vocab(rt, draft_vocab or "cjk") ok(f"MTP draft layer: {rt}") # ---- 7. the start script @@ -2463,8 +2475,8 @@ def main() -> int: cfg["host"] = a.host if a.api_key: cfg["api_key"] = a.api_key - if a.draft_vocab: - cfg["draft_vocab"] = a.draft_vocab + if draft_vocab: + cfg["draft_vocab"] = draft_vocab if vision != "none": cfg["vision"] = {"exe": str(eng / VEXE), "mmproj": str(mmproj), "model": str(shards[0]), "gpu": vision == "gpu", "max_tokens": VISION[vision]["max_tokens"]} diff --git a/tools/test_setup_draft_vocab.py b/tools/test_setup_draft_vocab.py new file mode 100644 index 00000000..f0b75692 --- /dev/null +++ b/tools/test_setup_draft_vocab.py @@ -0,0 +1,53 @@ +"""Tests for setup.py's draft-vocabulary choice (#287): a setup run again without --draft-vocab keeps the subset the +model's config chose before (cyrillic, en), and refresh_draft_vocab copies the chosen shipped subset. Pure file work +in a temporary folder - no GPU, no downloads, no prompts. + + python -m unittest tools.test_setup_draft_vocab +""" +from __future__ import annotations + +import hashlib +import json +import sys +import tempfile +import unittest +from pathlib import Path + +ROOT = Path(__file__).resolve().parents[1] +sys.path.insert(0, str(ROOT)) +import setup # noqa: E402 + + +class SavedChoice(unittest.TestCase): + def test_a_config_keeps_its_choice(self): + with tempfile.TemporaryDirectory() as d: + for choice in setup.DRAFT_VOCABS: + p = Path(d) / "strata-x.json" + p.write_text(json.dumps({"args": [], "draft_vocab": choice}), encoding="utf-8") + self.assertEqual(setup.saved_draft_vocab(p), choice) + + def test_no_choice_without_a_valid_config(self): + with tempfile.TemporaryDirectory() as d: + self.assertIsNone(setup.saved_draft_vocab(Path(d) / "missing.json")) + for text in ("{}", "[1]", "not json", json.dumps({"draft_vocab": "klingon"})): + p = Path(d) / "strata-y.json" + p.write_text(text, encoding="utf-8") + self.assertIsNone(setup.saved_draft_vocab(p), text) + + +class Refresh(unittest.TestCase): + def test_cyrillic_replaces_a_shipped_subset_and_stays(self): + if not (ROOT / "data" / setup.DRAFT_VOCABS["cyrillic"]).exists(): + self.skipTest("data/draft_vocab_cyrillic.bin is not in this checkout") + want = hashlib.sha256((ROOT / "data" / setup.DRAFT_VOCABS["cyrillic"]).read_bytes()).hexdigest() + with tempfile.TemporaryDirectory() as d: + rt = Path(d) + (rt / "draft_vocab.bin").write_bytes((ROOT / "data" / setup.DRAFT_VOCABS["cjk"]).read_bytes()) + setup.refresh_draft_vocab(rt, "cyrillic") + self.assertEqual(hashlib.sha256((rt / "draft_vocab.bin").read_bytes()).hexdigest(), want) + setup.refresh_draft_vocab(rt, "cyrillic") # again: nothing changes + self.assertEqual(hashlib.sha256((rt / "draft_vocab.bin").read_bytes()).hexdigest(), want) + + +if __name__ == "__main__": + unittest.main()