Files
strata/tools/test_setup_draft_vocab.py
T
Niko1221 5c50fbbcf2 setup: --draft-vocab fr - the English/code subset plus the tokens of French text (#597)
French answers drafted poorly with the shipped subsets: 23.6% of the token occurrences of French text fall outside
draft_vocab_en.bin, and the draft head cannot propose those (no subset adds the accented words and word pieces).

- tools/draft_vocab.py: a coverage mode. --corpus FILE... adds to the base the most frequent tokens of the corpus that
  together cover --coverage (default 0.99) of its occurrences, and prints the share outside the base and the new list.
- data/draft_vocab_fr.bin: draft_vocab_en.bin plus 5,686 tokens (46,211 ids), from 10 French Wikipedia articles
  (France, Paris, Napoleon Ier, Langue francaise, Union europeenne, Litterature francaise, Cuisine francaise,
  Economie de la France, Revolution francaise, Seconde Guerre mondiale; 1.24 M characters, 326,926 tokens) at 0.99:
    python tools/draft_vocab.py --gguf <IQ3_XXS shard 1> --base data/draft_vocab_en.bin --corpus <the articles> \
        --coverage 0.99 --out data/draft_vocab_fr.bin
  Outside the list on that corpus: 0.74% (23.65% with en).
- setup.py: "fr" in DRAFT_VOCABS / --draft-vocab (~151 MiB head on IQ3_S, between en and cyrillic). The sha256 check
  knows it as a shipped subset, so a later --draft-vocab cjk/en replaces it and a subset made by hand stays.
- tests: tools/test_draft_vocab.py (the coverage mode), tools/test_setup_draft_vocab.py (fr replaces a shipped subset
  and back, keeps the en ids first in their order, its MiB estimate).

The default subset (cjk) is unchanged.
2026-10-03 18:19:49 +02:00

110 lines
5.4 KiB
Python

"""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, fr, 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)
def test_fr_replaces_a_shipped_subset_and_back(self):
# #597: the French subset is a shipped one too - a later --draft-vocab cjk (or en) replaces it again, while a
# subset made by hand stays
data = ROOT / "data"
if not (data / setup.DRAFT_VOCABS["fr"]).exists():
self.skipTest("data/draft_vocab_fr.bin is not in this checkout")
sha = lambda p: hashlib.sha256(p.read_bytes()).hexdigest() # noqa: E731
with tempfile.TemporaryDirectory() as d:
rt = Path(d)
(rt / "draft_vocab.bin").write_bytes((data / setup.DRAFT_VOCABS["en"]).read_bytes())
setup.refresh_draft_vocab(rt, "fr")
self.assertEqual(sha(rt / "draft_vocab.bin"), sha(data / setup.DRAFT_VOCABS["fr"]))
setup.refresh_draft_vocab(rt, "cjk")
self.assertEqual(sha(rt / "draft_vocab.bin"), sha(data / setup.DRAFT_VOCABS["cjk"]))
(rt / "draft_vocab.bin").write_bytes(bytes([1, 0, 0, 0])) # made by hand: kept
setup.refresh_draft_vocab(rt, "fr")
self.assertEqual((rt / "draft_vocab.bin").read_bytes(), bytes([1, 0, 0, 0]))
def test_fr_holds_the_en_subset_in_its_order(self):
# built with tools/draft_vocab.py --base data/draft_vocab_en.bin --corpus ...: the base first, unchanged
from array import array
data = ROOT / "data"
if not (data / setup.DRAFT_VOCABS["fr"]).exists():
self.skipTest("data/draft_vocab_fr.bin is not in this checkout")
en = list(array("i", (data / setup.DRAFT_VOCABS["en"]).read_bytes()))
fr = list(array("i", (data / setup.DRAFT_VOCABS["fr"]).read_bytes()))
self.assertEqual(fr[:len(en)], en)
self.assertEqual(len(set(fr)), len(fr))
self.assertEqual(fr[len(en):], sorted(fr[len(en):]))
class SmallCardNote(unittest.TestCase):
"""#474: a card under 14 GB is told about a smaller draft subset - a note only, and not when one was chosen."""
def test_note_on_a_small_card_only(self):
note = setup.draft_vocab_note(12.0, None)
self.assertTrue(note)
self.assertIn("--draft-vocab en", " ".join(note))
self.assertIn("the draft head does not fit", " ".join(note))
for vram, chosen in ((16.0, None), (24.0, None), (12.0, "en"), (12.0, "cyrillic"), (12.0, "fr"), (12.0, "cjk"),
(0.0, None)):
self.assertEqual(setup.draft_vocab_note(vram, chosen), [], (vram, chosen))
def test_sizes_follow_the_shipped_subsets(self):
# the MiB the note gives scale with the subsets' token counts (4 bytes per token id in data/)
sizes = {}
for choice, name in setup.DRAFT_VOCABS.items():
p = ROOT / "data" / name
if not p.exists():
self.skipTest(f"data/{name} is not in this checkout")
sizes[choice] = p.stat().st_size // 4
for choice in ("en", "cyrillic", "fr"):
want = setup.DRAFT_VOCAB_MIB["cjk"] * sizes[choice] / sizes["cjk"]
self.assertAlmostEqual(setup.DRAFT_VOCAB_MIB[choice], want, delta=3)
if __name__ == "__main__":
unittest.main()