okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1306424be
main
1"""0.2.2: "use my own model files" accepts every layout 0.1.45's `setup.py --gguf-dir` accepted (issues #3316 #3292 #3190 #3209 #3331). 2 3The checker in front of setup (strata_control/models.plan_import) used to be stricter than setup itself: a split set whose first shard 4holds only the metadata (Unsloth's), a link to the file (Hugging Face cache, Ollama), a folder holding several sizes, a folder above the 5model folder (a whole repository, a 0.1.45 data folder), more than 128 files in a folder. Tiny GGUF files with the real shard names 6stand for the real ones (the shard names and the expert types are what the table checks).""" 7 8from __future__import annotations 9 10import json 11import os 12import struct 13import tempfile 14import unittest 15from pathlib import Path 16 17from strata_control import gguf_preflight ,models 18from strata_control .profiles import ProfileError 19from tools .gguf_reader import BLOCK_GEOMETRY 20 21TYPE_IDS = {"Q8_0" :8 ,"IQ3_S" :21 ,"IQ4_NL" :20 ,"IQ4_XS" :23 ,"IQ3_XXS" :18 ,"Q2_0" :42 ,"IQ2_S" :22 ,"IQ2_XS" :17 , 22"I8" :24 ,"I16" :25 ,"I32" :26 ,"I64" :27 } 23# One value per block. Kept here rather than read from the reader's table: the test has to build such a file whether or 24# not that table sizes the type, and a wrong row there must fail the test instead of moving the fixture with it. 25INTEGER_BYTES = {"I8" :1 ,"I16" :2 ,"I32" :4 ,"I64" :8 } 26 27 28def string (value :str )-> bytes : 29raw = value .encode () 30return struct .pack ("<Q" ,len (raw ))+ raw 31 32 33def gguf_bytes (experts :list [str ],* ,metadata :bool = True )-> bytes : 34"""A tiny qwen4exp GGUF with one routed-expert tensor per type named; `experts=[]` is the metadata-only first shard of a split set.""" 35pairs = [("general.architecture" ,8 ,string ("qwen4exp" )), ("tokenizer.ggml.model" ,8 ,string ("gpt2" )), 36 ("tokenizer.ggml.tokens" ,9 ,struct .pack ("<IQ" ,8 ,1 )+ string ("a" )), 37 ("tokenizer.ggml.merges" ,9 ,struct .pack ("<IQ" ,8 ,1 )+ string ("a b" ))]if metadata else [] 38header = b"GGUF" + struct .pack ("<IQQ" ,3 ,len (experts ),len (pairs ))+ \ 39b"" .join (string (k )+ struct .pack ("<I" ,kind )+ value for k ,kind ,value in pairs ) 40offset = 0 41for i ,name in enumerate (experts ): 42block ,size = (1 ,INTEGER_BYTES [name ])if name in INTEGER_BYTES else BLOCK_GEOMETRY [name ] 43header += string (f"blk. { i } .ffn_gate_exps.weight" )+ struct .pack ("<IQIQ" ,1 ,block ,TYPE_IDS [name ],offset ) 44offset += - (- size // 32 )* 32 45header += b"\x00" * (- len (header )% 32 ) 46return header + b"\x00" * offset 47 48 49UNSLOTH_Q4 = ["Qwen3.8-Flash-Next-UD-Q4_K_XL-0000%d-of-00004.gguf" % i for i in (1 ,2 ,3 ,4 )] 50UNSLOTH_IQ4 = ["Qwen3.8-Flash-Next-UD-IQ4_XS-0000%d-of-00003.gguf" % i for i in (1 ,2 ,3 )] 51Q2 = ["Qwen3.8-Flash-Next-GSQ-RCO-Q2_0-0000%d-of-00002.gguf" % i for i in (1 ,2 )] 52IQ3XXS = ["Qwen3.8-Flash-Next-GSQ-RCO-IQ3_XXS-0000%d-of-00002.gguf" % i for i in (1 ,2 )] 53 54 55class Base (unittest .TestCase ): 56def setUp (self ): 57self .tmp = tempfile .TemporaryDirectory (prefix = "strata-own-" ) 58self .addCleanup (self .tmp .cleanup ) 59self .base = Path (self .tmp .name ) 60 61def put (self ,folder :Path ,names :list [str ],experts :list [str ])-> Path : 62folder .mkdir (parents = True ,exist_ok = True ) 63for n in names : 64 (folder / n ).write_bytes (gguf_bytes (experts )) 65return folder 66 67def gsq (self ,folder :Path ,names :list [str ],kind :str )-> Path : 68return self .put (folder ,names , [kind ]) 69 70def unsloth (self ,folder :Path ,names :list [str ],kinds :list [str ])-> Path : 71"""The first shard has only the metadata (what llama.cpp's gguf-split makes), the others carry tensors.""" 72folder .mkdir (parents = True ,exist_ok = True ) 73 (folder / names [0 ]).write_bytes (gguf_bytes ([])) 74for n in names [1 :]: 75 (folder / n ).write_bytes (gguf_bytes (kinds ,metadata = False )) 76return folder 77 78 79class MetadataOnlyFirstShard (Base ): 80def test_unsloth_split_set_is_recognised (self ): 81folder = self .unsloth (self .base / "u" ,UNSLOTH_Q4 , ["IQ4_NL" ]) 82plan = models .plan_import (str (folder )) 83# the tiny files carry IQ4_NL experts, which UD-Q4_K_XL does not list: the set is found, the quantization is what is flagged 84self .assertNotIn ("unsupported_or_missing_architecture" ,plan ["issues" ]) 85self .assertNotIn ("unsupported_or_incomplete_tokenizer_metadata" ,plan ["issues" ]) 86self .assertNotIn ("GGUF tensor or metadata count" ," " .join (plan ["issues" ])) 87self .assertEqual ((plan ["candidate_selection" ]["family" ],plan ["candidate_selection" ]["model" ]), ("unsloth" ,"UD-Q4_K_XL" )) 88 89def test_unsloth_iq4_xs_passes_with_its_real_expert_types (self ): 90folder = self .unsloth (self .base / "u" ,UNSLOTH_IQ4 , ["IQ3_S" ,"IQ4_NL" ,"IQ4_XS" ,"Q8_0" ]) 91plan = models .plan_import (str (folder )) 92self .assertEqual (plan ["issues" ], []) 93self .assertEqual (plan ["candidate_selection" ]["model" ],"UD-IQ4_XS" ) 94self .assertEqual (plan ["model_dir" ],str (folder )) 95 96def test_a_shard_with_no_tensors_and_no_metadata_is_still_refused (self ): 97f = self .base / "x.gguf" 98f .write_bytes (gguf_bytes ([],metadata = False )) 99plan = models .plan_import (str (f )) 100self .assertIn ("unsupported_or_missing_architecture" ,plan ["issues" ]) 101 102 103class Links (Base ): 104def make_links (self ): 105blobs = self .base / "blobs" 106snap = self .base / "snapshots" / "abc" / "Q2_0" 107blobs .mkdir () 108snap .mkdir (parents = True ) 109for i ,n in enumerate (Q2 ): 110 (blobs / f"sha { i } " ).write_bytes (gguf_bytes (["Q2_0" ])) 111try : 112os .symlink (blobs / f"sha { i } " ,snap / n ) 113except (OSError ,NotImplementedError ): 114self .skipTest ("this account cannot create symbolic links" ) 115return snap 116 117def test_a_hugging_face_cache_snapshot_is_used_through_its_links (self ): 118snap = self .make_links () 119plan = models .plan_import (str (snap )) 120self .assertEqual (plan ["issues" ], []) 121self .assertEqual (plan ["candidate_selection" ]["model" ],"Q2_0" ) 122self .assertEqual (plan ["file_count" ],2 ) 123 124def test_a_link_to_the_folder_is_followed (self ): 125real = self .gsq (self .base / "real" ,Q2 ,"Q2_0" ) 126link = self .base / "link" 127try : 128os .symlink (real ,link ,target_is_directory = True ) 129except (OSError ,NotImplementedError ): 130self .skipTest ("this account cannot create symbolic links" ) 131self .assertEqual (models .plan_import (str (link ))["issues" ], []) 132 133def test_a_link_to_a_file_that_is_gone_says_so (self ): 134snap = self .base / "s" 135snap .mkdir () 136try : 137os .symlink (self .base / "gone" ,snap / Q2 [0 ]) 138except (OSError ,NotImplementedError ): 139self .skipTest ("this account cannot create symbolic links" ) 140with self .assertRaises (ProfileError )as ctx : 141gguf_preflight .real_model_file (snap / Q2 [0 ]) 142self .assertIn ("link to a file that is not there" ,str (ctx .exception )) 143 144def test_a_missing_file_is_missing_not_a_link (self ): 145with self .assertRaises (ProfileError )as ctx : 146gguf_preflight .real_model_file (self .base / "nope.gguf" ) 147self .assertIn ("is missing" ,str (ctx .exception )) 148 149def test_run_configs_keep_the_no_link_rule (self ): 150cfg = self .base / "real.json" 151cfg .write_text ("{}" ) 152link = self .base / "strata-x.json" 153try : 154os .symlink (cfg ,link ) 155except (OSError ,NotImplementedError ): 156self .skipTest ("this account cannot create symbolic links" ) 157with self .assertRaises (ProfileError )as ctx : 158models ._read (link )# noqa: SLF001 159self .assertIn ("symlink" ,str (ctx .exception )) 160 161 162class IntegerTensors (Base ): 163"""The ISTA-DAS DeepSeek-V4.1 first shard keeps the Engram's hash parameters as integer tensors instead of metadata 164(tools/dsv41/pack_gguf.py: I32 and I64), so the table in front of setup and the app sizes them instead of refusing the file.""" 165 166def test_a_tensor_directory_with_integer_tensors_is_read (self ): 167p = self .base / "DeepSeek-V4.1-Flash-IST-DAS-2.00bpw-00001-of-00004.gguf" 168p .write_bytes (gguf_bytes (["I32" ,"I64" ])) 169self .assertEqual (gguf_preflight .inspect_metadata (p )["tensor_types" ], {"I32" :1 ,"I64" :1 }) 170 171def test_each_integer_type_is_sized_at_one_value_a_block (self ): 172for kind ,size in INTEGER_BYTES .items (): 173p = self .base / f"integer- { kind } .gguf" 174p .write_bytes (gguf_bytes ([kind ])) 175self .assertEqual (gguf_preflight .inspect_metadata (p )["payload_bytes_required" ],size ,kind ) 176 177 178class Folders (Base ): 179def test_many_other_gguf_files_in_the_folder_do_not_refuse (self ): 180folder = self .gsq (self .base / "m" ,Q2 ,"Q2_0" ) 181for i in range (140 ): 182 (folder / f"other- { i :03d } .gguf" ).write_bytes (b"GGUF" ) 183plan = models .plan_import (str (folder )) 184self .assertEqual (plan ["candidate_selection" ]["model" ],"Q2_0" ) 185self .assertEqual (plan ["file_count" ],2 ) 186 187def test_strata_draft_layer_beside_a_model_is_not_a_second_model (self ): 188# #4693: "the folder holds 2 models" for a model and the mtp-q2_0.gguf beside it 189folder = self .base / "own" 190folder .mkdir (parents = True ) 191 (folder / "My-Finetune-IQ3_S-00001-of-00001.gguf" ).write_bytes (gguf_bytes (["IQ3_S" ])) 192 (folder / "mtp-q2_0.gguf" ).write_bytes (gguf_bytes (["Q2_0" ])) 193plan = models .plan_import (str (folder )) 194self .assertNotIn ("multiple_model_groups" ,plan ["issues" ]) 195self .assertEqual ([f ["name" ]for f in plan ["files" ]], ["My-Finetune-IQ3_S-00001-of-00001.gguf" ]) 196 197def test_several_sizes_in_one_folder_name_them_and_ask_for_one_file (self ): 198folder = self .gsq (self .base / "many" ,Q2 ,"Q2_0" ) 199self .gsq (folder ,IQ3XXS ,"IQ3_XXS" ) 200plan = models .plan_import (str (folder )) 201self .assertEqual (plan ["issues" ], ["multiple_model_groups" ]) 202self .assertIsNone (plan ["candidate_selection" ]) 203self .assertEqual (sorted (h ["model" ]for h in plan ["folder_holds" ]), ["IQ3_XXS" ,"Q2_0" ]) 204self .assertTrue (any ("holds 2 models" in m and "type the path of its .gguf file" in m for m in plan ["messages" ])) 205 206def test_one_known_size_among_other_files_is_used_and_the_rest_is_named (self ): 207folder = self .gsq (self .base / "mix" ,Q2 ,"Q2_0" ) 208 (folder / "some-other-model-Q4_K_M.gguf" ).write_bytes (gguf_bytes (["IQ3_S" ])) 209plan = models .plan_import (str (folder )) 210self .assertEqual (plan ["issues" ], []) 211self .assertEqual (plan ["candidate_selection" ]["model" ],"Q2_0" ) 212self .assertEqual (plan ["file_count" ],2 ) 213self .assertTrue (any ("some-other-model-Q4_K_M.gguf" in m and "leaves the others alone" in m for m in plan ["messages" ])) 214 215def test_a_shard_file_picks_its_set_out_of_a_folder_of_several (self ): 216folder = self .gsq (self .base / "many" ,Q2 ,"Q2_0" ) 217self .gsq (folder ,IQ3XXS ,"IQ3_XXS" ) 218plan = models .plan_import (str (folder / IQ3XXS [1 ])) 219self .assertEqual (plan ["issues" ], []) 220self .assertEqual (plan ["candidate_selection" ]["model" ],"IQ3_XXS" ) 221self .assertEqual (plan ["model_dir" ],str (folder )) 222 223def test_a_folder_above_the_model_folder_is_searched (self ): 224data = self .base / "Strata-data" 225self .gsq (data / "models" / "q2_0" ,Q2 ,"Q2_0" ) 226 (data / "packs" ).mkdir () 227for start in (data ,data / "models" ): 228plan = models .plan_import (str (start )) 229self .assertEqual (plan ["issues" ], [],start ) 230self .assertEqual (plan ["model_dir" ],str (data / "models" / "q2_0" )) 231self .assertTrue (any ("The model files are in" in m for m in plan ["messages" ])) 232 233def test_a_whole_repository_with_one_folder_per_size_asks_which (self ): 234repo = self .base / "repo" 235self .gsq (repo / "Q2_0" ,Q2 ,"Q2_0" ) 236self .gsq (repo / "IQ3_XXS" ,IQ3XXS ,"IQ3_XXS" ) 237plan = models .plan_import (str (repo )) 238self .assertIn ("multiple_model_groups" ,plan ["issues" ]) 239self .assertEqual (sorted ((Path (h ["folder" ]).name ,h ["model" ])for h in plan ["folder_holds" ]), 240 [("IQ3_XXS" ,"IQ3_XXS" ), ("Q2_0" ,"Q2_0" )]) 241self .assertTrue (plan ["file_set_status" ]== "invalid_or_incomplete" ) 242 243def test_one_known_folder_next_to_unrelated_ones_is_used (self ): 244top = self .base / "all" 245self .gsq (top / "models" / "q2_0" ,Q2 ,"Q2_0" ) 246 (top / "stuff" ).mkdir () 247 (top / "stuff" / "notes.txt" ).write_text ("x" ) 248plan = models .plan_import (str (top )) 249self .assertEqual (plan ["candidate_selection" ]["model" ],"Q2_0" ) 250 251def test_a_folder_with_no_gguf_anywhere_says_so (self ): 252 (self .base / "empty" / "a" / "b" ).mkdir (parents = True ) 253with self .assertRaises (ProfileError )as ctx : 254models .plan_import (str (self .base / "empty" )) 255self .assertIn ("no GGUF files found" ,str (ctx .exception )) 256 257def test_a_folder_name_with_spaces_and_a_second_drive_style_path (self ): 258folder = self .gsq (self .base / "my models" / "Qwen 3.8 files" ,Q2 ,"Q2_0" ) 259self .assertEqual (models .plan_import (str (folder ))["issues" ], []) 260 261def test_a_missing_second_shard_is_one_plain_issue (self ): 262folder = self .put (self .base / "half" ,Q2 [:1 ], ["Q2_0" ]) 263plan = models .plan_import (str (folder )) 264self .assertEqual (plan ["issues" ], ["incomplete_or_invalid_shard_set" ]) 265 266 267class Launcher (Base ): 268"""`strata launch --model-path` and the TUI's "use my own files" (strata_cli/launch/runner.py, screens.py).""" 269 270def runner (self ,** kw ): 271from strata_cli .launch import settings as S 272from strata_cli .launch .catalog import Catalog 273from strata_cli .launch .runner import Runner 274from tests .terminal .launch_support import HW_12 275return Runner (S .LaunchSettings (** kw ),catalog = Catalog (hardware = HW_12 )) 276 277def test_a_folder_above_the_model_is_resolved_to_the_model_folder (self ): 278data = self .base / "Strata-data" 279self .gsq (data / "models" / "q2_0" ,Q2 ,"Q2_0" ) 280r = self .runner (model_path = str (data )) 281r .resolve_own_files () 282self .assertEqual ((r .s .family ,r .s .model ), ("qwen" ,"Q2_0" )) 283self .assertEqual (Path (r .s .model_path ),data / "models" / "q2_0" ) 284argv = r .setup_args () 285self .assertEqual (Path (argv [argv .index ("--gguf-dir" )+ 1 ]),data / "models" / "q2_0" ) 286 287def test_a_named_model_in_a_folder_of_several_sizes_is_never_refused_here (self ): 288folder = self .gsq (self .base / "many" ,Q2 ,"Q2_0" ) 289self .gsq (folder ,IQ3XXS ,"IQ3_XXS" ) 290r = self .runner (model_path = str (folder ),family = "qwen" ,model = "IQ3_XXS" ) 291r .resolve_own_files () 292self .assertEqual (Path (r .s .model_path ),folder )# setup picks the IQ3_XXS files by name, as in 0.1.45 293self .assertEqual ((r .s .family ,r .s .model ), ("qwen" ,"IQ3_XXS" )) 294 295def test_a_named_model_is_found_in_the_folder_that_holds_it (self ): 296repo = self .base / "repo" 297self .gsq (repo / "Q2_0" ,Q2 ,"Q2_0" ) 298self .gsq (repo / "IQ3_XXS" ,IQ3XXS ,"IQ3_XXS" ) 299r = self .runner (model_path = str (repo ),family = "qwen" ,model = "IQ3_XXS" ) 300r .resolve_own_files () 301self .assertEqual (Path (r .s .model_path ),repo / "IQ3_XXS" ) 302 303def test_two_models_and_none_named_says_which_in_plain_words (self ): 304from strata_cli .launch .runner import LaunchError 305repo = self .base / "repo" 306self .gsq (repo / "Q2_0" ,Q2 ,"Q2_0" ) 307self .gsq (repo / "IQ3_XXS" ,IQ3XXS ,"IQ3_XXS" ) 308r = self .runner (model_path = str (repo )) 309with self .assertRaises (LaunchError )as ctx : 310r .resolve_own_files () 311text = str (ctx .exception ) 312self .assertIn ("more than one model" ,text ) 313self .assertIn ("holds 2 models" ,text ) 314 315def test_a_linked_model_file_keeps_its_name (self ): 316snap = Links .make_links (self ) 317r = self .runner (model_path = str (snap / Q2 [0 ])) 318r .resolve_own_files () 319self .assertEqual ((r .s .family ,r .s .model ), ("qwen" ,"Q2_0" )) 320self .assertEqual (Path (r .s .model_path ).name ,Q2 [0 ])# not the blob's hash name 321 322 323class Words (unittest .TestCase ): 324def test_a_file_problem_names_the_file_and_says_why (self ): 325self .assertEqual (models .plain_issue ("model-00002-of-00002.gguf: GGUF metadata is truncated" ), 326"model-00002-of-00002.gguf: the file is cut short (a download or copy that did not finish)" ) 327self .assertIn ("shorter than its own header" ,models .plain_issue ("a.gguf: GGUF tensor payload is shorter than its directory requires" )) 328self .assertIn ("old format (version 2)" ,models .plain_issue ("a.gguf: Strata metadata preflight requires GGUF v3" )) 329self .assertEqual (models .plain_issue ("incomplete_or_invalid_shard_set" ),"some parts of the model are missing" ) 330 331def test_the_plan_carries_the_words (self ): 332with tempfile .TemporaryDirectory ()as tmp : 333f = Path (tmp )/ Q2 [0 ] 334f .write_bytes (gguf_bytes (["Q2_0" ])[:60 ]) 335plan = models .plan_import (str (f )) 336self .assertTrue (plan ["plain_issues" ]) 337self .assertTrue (any (x .startswith (Q2 [0 ]+ ":" )or x .startswith (Q2 [1 ]+ ":" )for x in plan ["plain_issues" ]),plan ["plain_issues" ]) 338 339 340class Table (unittest .TestCase ): 341def test_every_size_setup_installs_is_in_the_table (self ): 342"""Setup can install it from --gguf-dir, so the checker in front of it must know it too (0.2.0 knew 7 of 10).""" 343import importlib .util 344spec = importlib .util .spec_from_file_location ("strata_setup_for_test" ,models .ROOT / "setup.py" ) 345setup = importlib .util .module_from_spec (spec ) 346spec .loader .exec_module (setup ) 347table = json .loads ((models .ROOT / "data" / "gguf_fingerprints.json" ).read_text (encoding = "utf-8" )) 348missing = [] 349for family ,fam in setup .FAMILIES .items (): 350if fam .get ("ready" ): 351continue 352for model ,row in setup .MODELS .items (): 353if family not in row .get ("families" , ("qwen" ,"swift" )): 354continue 355names = {setup .model_file (fam ,model ,i )for i in range (1 ,setup .model_shards (fam ,model )+ 1 )} 356key = f" { family } / { model } " 357have = {Path (n ).name for n in (table .get (key )or {}).get ("files" , [])} 358if key not in models .SUPPORTED_SELECTIONS or have != names : 359missing .append (key ) 360self .assertEqual (missing , []) 361 362 363if __name__ == "__main__" : 364unittest .main ()