okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
58169905b
main
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 4python -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 8byte 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 : 31return hashlib .sha256 (Path (p ).read_bytes ()).hexdigest () 32 33 34def shipped (choice :str )-> Path : 35return ROOT / "data" / setup .DRAFT_VOCABS [choice ] 36 37 38def vocab_arg (cfg :dict )-> Path : 39a = cfg ["args" ] 40return 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 ): 45with_mtp = False 46 47def setUp (self ): 48self ._tmp = tempfile .TemporaryDirectory () 49self .addCleanup (self ._tmp .cleanup ) 50self .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) 54self .rt = self .data / "mtp" / "rt" 55if self .with_mtp : 56self .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 61def 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.""" 63takes_choice = "choice" in inspect .signature (setup .provide_dflash2_vocab ).parameters 64if takes_choice : 65setup .provide_dflash2_vocab (self .data ,choice ) 66else : 67setup .provide_dflash2_vocab (self .data )# the code before this branch 68if (self .rt / "experts.bin" ).exists (): 69setup .refresh_draft_vocab (self .rt ,choice ) 70 71def config (self ,mode :str = "dflash2" ,choice :str | None = None )-> dict : 72args = ["--pack" ,str (self .data / "packs" / "m" ),"--native" ,"x.gguf" ,"--mtp" ,str (self .rt ),"--spec" ,"4" ,"--spec-min-p" ,"0.5" ] 73cfg = {"args" :args ,"model_name" :"qwen3.8-flash-next-iq3_s" ,"drafting" :mode } 74if choice : 75cfg ["draft_vocab" ]= choice 76return cfg 77 78def applied (self ,mode :str = "dflash2" )-> dict : 79cfg = self .config (mode ) 80d = drafting .apply (cfg ,data_dir = self .data ,family = "qwen" ,backend = "cuda" ,archs = ["sm_121" ],slots = 0 ) 81self .assertEqual (d .drafter ,"dflash2" ,d ) 82return cfg 83 84 85class DFlash2Only (Base ): 86with_mtp = False 87 88def test_each_choice_reaches_the_engine (self ): 89for choice in CHOICES : 90with self .subTest (choice = choice ): 91self .setup_step (choice ) 92self .assertEqual (sha (vocab_arg (self .applied ())),sha (shipped (choice )),f"--mtp-draft-vocab is not the { choice } subset" ) 93 94def test_auto_uses_the_choice (self ): 95for choice in CHOICES : 96with self .subTest (choice = choice ): 97self .setup_step (choice ) 98self .assertEqual (sha (vocab_arg (self .applied ("auto" ))),sha (shipped (choice ))) 99 100def test_a_later_choice_replaces_a_shipped_subset_and_a_hand_made_one_stays (self ): 101self .setup_step ("fr" ) 102self .assertEqual (sha (vocab_arg (self .applied ())),sha (shipped ("fr" ))) 103self .setup_step ("en" ) 104self .assertEqual (sha (vocab_arg (self .applied ())),sha (shipped ("en" ))) 105self .setup_step ("cjk" ) 106self .assertEqual (sha (vocab_arg (self .applied ())),sha (shipped ("cjk" ))) 107mine = drafting .vocab_target (self .data ) 108mine .write_bytes (bytes ([1 ,0 ,0 ,0 ])) 109self .setup_step ("fr" ) 110self .assertEqual (mine .read_bytes (),bytes ([1 ,0 ,0 ,0 ]),"a subset made by hand is kept" ) 111 112def test_an_update_and_the_drafters_fetch_keep_the_choice (self ): 113self .setup_step ("fr" ) 114cfg = self .config (choice = "fr" ) 115setup .refresh_shared_draft_vocab (cfg ,set ())# setup.py --update, the saved config names fr 116self .assertEqual (sha (vocab_arg (self .applied ())),sha (shipped ("fr" ))) 117drafting .provide_vocab (self .data )# the fetch (Load, `strata drafting fetch`) never overwrites a copy that is there 118self .assertEqual (sha (vocab_arg (self .applied ())),sha (shipped ("fr" ))) 119 120def test_an_update_applies_the_saved_choice_to_an_older_copy (self ): 121drafting .provide_vocab (self .data )# an install made before the choice reached DFlash2: the default copy 122self .assertEqual (sha (drafting .vocab_target (self .data )),sha (shipped ("cjk" ))) 123setup .refresh_shared_draft_vocab (self .config (choice = "cyrillic" ),set ()) 124self .assertEqual (sha (vocab_arg (self .applied ())),sha (shipped ("cyrillic" ))) 125 126def test_no_choice_means_the_default_file (self ): 127setup .refresh_shared_draft_vocab (self .config (),set ())# no "draft_vocab" key: nothing to apply, nothing broken 128drafting .provide_vocab (self .data ) 129self .assertEqual (sha (vocab_arg (self .applied ())),sha (shipped ("cjk" ))) 130 131 132class WithMtpFolder (Base ): 133with_mtp = True 134 135def test_each_choice_reaches_the_engine (self ): 136for choice in CHOICES : 137with self .subTest (choice = choice ): 138self .setup_step (choice ) 139self .assertEqual (sha (vocab_arg (self .applied ())),sha (shipped (choice ))) 140self .assertEqual (sha (self .rt / "draft_vocab.bin" ),sha (shipped (choice )),"the MTP layer's file follows too" ) 141if drafting .vocab_target (self .data ).exists (): 142self .assertEqual (sha (drafting .vocab_target (self .data )),sha (shipped (choice )),"the DFlash2 copy agrees with the folder" ) 143 144def test_auto_uses_the_choice (self ): 145self .setup_step ("fr" ) 146self .assertEqual (sha (vocab_arg (self .applied ("auto" ))),sha (shipped ("fr" ))) 147 148def test_the_two_files_never_disagree_after_an_update (self ): 149self .setup_step ("en" ) 150setup .refresh_shared_draft_vocab (self .config (choice = "fr" ),set ()) 151self .assertEqual (sha (self .rt / "draft_vocab.bin" ),sha (shipped ("fr" ))) 152if drafting .vocab_target (self .data ).exists (): 153self .assertEqual (sha (drafting .vocab_target (self .data )),sha (shipped ("fr" ))) 154self .assertEqual (sha (vocab_arg (self .applied ())),sha (shipped ("fr" ))) 155 156 157if __name__ == "__main__" : 158unittest .main ()