okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
477fd36bc
main
1"""Tests for setup.py's NVFP4 family (ready-made packs, NVFP4_REPOS): every file fetched from its pinned revision and 2checked against its size and SHA-256, a file both packs share linked instead of downloaded twice, a wrong file 3refused, and the config's name read back as the same size. A local file:// mirror - nothing is downloaded. 4 5python -m unittest tools.test_setup_nvfp4 6""" 7from __future__import annotations 8 9import contextlib 10import hashlib 11import io 12import os 13import sys 14import tempfile 15import unittest 16from pathlib import Path 17from unittest import mock 18 19ROOT = Path (__file__ ).resolve ().parents [1 ] 20sys .path .insert (0 ,str (ROOT )) 21sys .path .insert (0 ,str (ROOT / "tools" )) 22import setup # noqa: E402 23 24A ,B = "owner/pack-a" ,"owner/pack-b" 25REV_A ,REV_B = "a" * 40 ,"b" * 40 26 27 28def quiet (fn ,* args ,** kw ): 29with contextlib .redirect_stdout (io .StringIO ()): 30return fn (* args ,** kw ) 31 32 33class ReadyPacks (unittest .TestCase ): 34def setUp (self ): 35self .tmp = tempfile .TemporaryDirectory () 36self .root = Path (self .tmp .name ) 37self .mirror = self .root / "mirror" 38self .models = self .root / "models" 39bodies = { 40 (A ,REV_A ,"pack/index.txt" ):b"index a" , (A ,REV_A ,"pack/experts.bin" ):b"experts a" * 50 , 41 (A ,REV_A ,"dense-a.gguf" ):b"dense a" , (A ,REV_A ,"ple-fp8.gguf" ):b"the shared table" * 100 , 42 (B ,REV_B ,"pack/index.txt" ):b"index b" , (B ,REV_B ,"pack/experts.bin" ):b"experts b" * 50 , 43 (B ,REV_B ,"dense-b.gguf" ):b"dense b" , (B ,REV_B ,"ple-fp8.gguf" ):b"the shared table" * 100 , 44 } 45roles = {"pack/index.txt" :"pack" ,"pack/experts.bin" :"pack" ,"dense-a.gguf" :"dense" ,"dense-b.gguf" :"dense" , 46"ple-fp8.gguf" :"ple" } 47repos = {A : {"revision" :REV_A ,"files" : {}},B : {"revision" :REV_B ,"files" : {}}} 48for (repo ,rev ,path ),body in bodies .items (): 49f = self .mirror / repo / "resolve" / rev / path 50f .parent .mkdir (parents = True ,exist_ok = True ) 51f .write_bytes (body ) 52repos [repo ]["files" ][path ]= (roles [path ],len (body ),hashlib .sha256 (body ).hexdigest ()) 53repos [setup .NVFP4_FP4LOCAL_REPO ]= setup .NVFP4_REPOS [setup .NVFP4_FP4LOCAL_REPO ]# listed, never fetched here 54models = {"NVFP4" : {** setup .MODELS ["NVFP4" ],"repo" :A },"NVFP4-NVIDIA" : {** setup .MODELS ["NVFP4-NVIDIA" ],"repo" :B }} 55self .patches = [mock .patch .object (setup ,"NVFP4_REPOS" ,repos ), 56mock .patch .dict (setup .MODELS ,models ), 57mock .patch .dict (os .environ , {"HF_ENDPOINT" :"file://" + self .mirror .as_posix ()})] 58for p in self .patches : 59p .start () 60 61def tearDown (self ): 62for p in reversed (self .patches ): 63p .stop () 64self .tmp .cleanup () 65 66def test_fetch_checks_and_links_the_shared_file (self ): 67quiet (setup .ready_fetch ,"NVFP4" ,self .models ) 68for f in setup .ready_files ("NVFP4" ): 69self .assertTrue (setup .ready_verified (setup .ready_where (self .models ,f ),f [5 ]),f [3 ]) 70quiet (setup .ready_fetch ,"NVFP4-NVIDIA" ,self .models ) 71a = self .models / "owner--pack-a" / "ple-fp8.gguf" 72b = self .models / "owner--pack-b" / "ple-fp8.gguf" 73self .assertTrue (setup .ready_verified (b ,setup .NVFP4_REPOS [B ]["files" ]["ple-fp8.gguf" ][2 ])) 74self .assertTrue (os .path .samefile (a ,b ),"the shared table is linked, not a second copy" ) 75p = setup .ready_paths ("NVFP4-NVIDIA" ,self .models ) 76self .assertEqual (p ["pack" ],self .models / "owner--pack-b" / "pack" ) 77self .assertEqual (p ["dense" ].name ,"dense-b.gguf" ) 78self .assertEqual (p ["ple" ],b ) 79 80def test_a_second_run_downloads_nothing (self ): 81quiet (setup .ready_fetch ,"NVFP4" ,self .models ) 82with mock .patch .object (setup ,"download" ,side_effect = AssertionError ("downloaded again" )): 83quiet (setup .ready_fetch ,"NVFP4" ,self .models ) 84 85def test_a_wrong_file_is_refused_and_deleted (self ): 86bad = self .mirror / A / "resolve" / REV_A / "dense-a.gguf" 87bad .write_bytes (b"dense X" )# same size, other bytes 88with self .assertRaises (SystemExit ): 89quiet (setup .ready_fetch ,"NVFP4" ,self .models ) 90self .assertFalse ((self .models / "owner--pack-a" / "dense-a.gguf" ).exists ()) 91 92def test_config_name_reads_back_as_the_size (self ): 93for model in ("NVFP4" ,"NVFP4-NVIDIA" ): 94cfg = self .root / f"strata- { ( setup . FAMILIES [ 'nvfp4' ][ 'tag' ] + model ). lower () } .json" 95cfg .write_text ('{"args": ["--max-context", "32768"]}' ,encoding = "utf-8" ) 96ch = setup .choices_from_config (cfg ) 97self .assertEqual ((ch ["family" ],ch ["model" ]), ("nvfp4" ,model )) 98 99 100class Family (unittest .TestCase ): 101def test_sizes_and_family (self ): 102self .assertTrue (setup .FAMILIES ["nvfp4" ]["ready" ]) 103names = [m for m in setup .MODELS if "nvfp4" in setup .MODELS [m ].get ("families" , ())] 104self .assertEqual (names , ["NVFP4" ,"NVFP4-NVIDIA" ,"NVFP4-QAD" ])# the GPTQ pack first: the default size 105for m in names : 106d = setup .MODELS [m ] 107self .assertTrue (d ["nvidia_only" ]) 108self .assertIs (d ["vision" ],True )# the shared encoder reads pictures for every NVFP4 model (QAD: dense.gguf is the text model) 109self .assertRegex (d ["license" ],"Licen[sc]e|Agreement" ) 110self .assertEqual (d ["engine" ],setup .FP4LOCAL_ENGINE if m == "NVFP4-QAD" else setup .NVFP4_ENGINE ) 111 112def test_gguf_names_skip_the_ready_family (self ): 113self .assertIsNone (setup .gguf_choice ("pack-a.gguf" )) 114self .assertEqual (setup .gguf_choice ("Qwen3.8-Flash-Next-GSQ-RCO-IQ3_XXS-00001-of-00002.gguf" ), ("qwen" ,"IQ3_XXS" )) 115 116 117class PreparedModel (unittest .TestCase ): 118"""NVFP4-QAD (NVIDIA's FP4-Local): the checkpoint is downloaded pinned, the pack is prepared on this PC.""" 119 120def test_the_checkpoint_files_are_pinned (self ): 121files = setup .ready_files ("NVFP4-QAD" ) 122names = {f [3 ]for f in files } 123self .assertEqual (sum (1 for n in names if n .startswith ("tensors/model-" )),21 ) 124self .assertTrue ({"config.json" ,"model.safetensors.index.json" ,"tokenizer.json" }<= names ) 125self .assertEqual (sum (f [4 ]for f in files )/ 1e9 // 1 ,setup .MODELS ["NVFP4-QAD" ]["download_gb" ]// 1 ) 126for f in files : 127self .assertEqual (f [0 ],"src" ) 128self .assertRegex (f [5 ],"^[0-9a-f]{64}$" ) 129self .assertEqual (f [2 ],"b9ce18a6887e18c089fdea8509a33e63429a3300" ) 130 131def test_paths_are_the_prepared_files (self ): 132m = Path ("/models" ) 133p = setup .ready_paths ("NVFP4-QAD" ,m ) 134d = m / "nvidia--Qwen3.8-Flash-Next-FP4-Local" / "prepared" 135self .assertEqual (p , {"pack" :d / "pack" ,"dense" :d / "dense.gguf" ,"ple" :d / "ple-nvfp4.gguf" ,"mtp" :d / "mtp" }) 136self .assertNotIn ("embd" ,p ) 137 138def test_prepared_done_needs_every_mark (self ): 139with tempfile .TemporaryDirectory ()as t : 140self .assertFalse (setup .prepared_done ("NVFP4-QAD" ,Path (t ))) 141d = setup .prepared_dir ("NVFP4-QAD" ,Path (t )) 142d .mkdir (parents = True ) 143marks = ("ple-nvfp4.gguf.done" ,"dense.gguf.done" ,"pack.done" ,"draft.done" ) 144for m in marks [:- 1 ]: 145 (d / m ).write_text ("x" ) 146self .assertFalse (setup .prepared_done ("NVFP4-QAD" ,Path (t ))) 147 (d / marks [- 1 ]).write_text ("x" ) 148self .assertTrue (setup .prepared_done ("NVFP4-QAD" ,Path (t ))) 149 150 151class PreparedWithoutCheckpoint (unittest .TestCase ): 152"""A prepared NVFP4-QAD model needs none of the checkpoint's 95 GB of safetensors: the engine reads the prepared files.""" 153 154def prepare (self ,models :Path )-> None : 155d = setup .prepared_dir ("NVFP4-QAD" ,models ) 156d .mkdir (parents = True ) 157for m in ("ple-nvfp4.gguf.done" ,"dense.gguf.done" ,"pack.done" ,"draft.done" ): 158 (d / m ).write_text ("x" ) 159 160def write_small_files (self ,models :Path )-> None : 161for f in setup .ready_files ("NVFP4-QAD" ): 162if f [3 ].startswith ("tensors/" ): 163continue 164dst = setup .ready_where (models ,f ) 165dst .parent .mkdir (parents = True ,exist_ok = True ) 166dst .write_bytes (b"x" ) 167dst .with_name (dst .name + ".done" ).write_text (f"sha256 { f [ 5 ] } " ) 168 169def test_prepared_model_does_not_need_the_tensors (self ): 170with tempfile .TemporaryDirectory ()as t : 171models = Path (t ) 172self .prepare (models ) 173needed = setup .ready_needed ("NVFP4-QAD" ,models ) 174self .assertTrue (needed ) 175self .assertFalse ([f for f in needed if f [3 ].startswith ("tensors/" )]) 176self .assertIn ("config.json" , {f [3 ]for f in needed }) 177 178def test_unprepared_model_still_needs_the_tensors (self ): 179with tempfile .TemporaryDirectory ()as t : 180needed = setup .ready_needed ("NVFP4-QAD" ,Path (t )) 181self .assertEqual (len (needed ),len (setup .ready_files ("NVFP4-QAD" ))) 182self .assertEqual (sum (1 for f in needed if f [3 ].startswith ("tensors/model-" )),21 ) 183 184def test_a_model_that_is_not_prepared_by_this_pc_keeps_all_its_files (self ): 185with tempfile .TemporaryDirectory ()as t : 186self .assertEqual (len (setup .ready_needed ("NVFP4" ,Path (t ))),len (setup .ready_files ("NVFP4" ))) 187 188def test_ready_fetch_downloads_nothing_for_a_prepared_model_without_tensors (self ): 189with tempfile .TemporaryDirectory ()as t : 190models = Path (t ) 191self .prepare (models ) 192self .write_small_files (models ) 193with mock .patch .object (setup ,"download" ,side_effect = AssertionError ("downloaded" ))as dl : 194setup .ready_fetch ("NVFP4-QAD" ,models ) 195dl .assert_not_called () 196 197def test_ready_fetch_wants_the_tensors_of_an_unprepared_model (self ): 198with tempfile .TemporaryDirectory ()as t : 199models = Path (t ) 200self .write_small_files (models ) 201with mock .patch .object (setup ,"download" ,side_effect = RuntimeError ("would download" )): 202with self .assertRaises (RuntimeError ): 203setup .ready_fetch ("NVFP4-QAD" ,models ) 204 205 206class MockedInstall (unittest .TestCase ): 207"""setup.main() --family nvfp4 --yes on mocked PCs (test_setup_golden's harness): the run config's model flags.""" 208 209def run_install (self ,prof ,model = None ,extra_argv = ()): 210import test_setup_golden as G 211ram ,found = G .PROFILES [prof ] 212return G .install (ram ,found , ["--family" ,"nvfp4" ,"--no-start" ,* (["--model" ,model ]if model else []), 213* extra_argv ]) 214 215def flag (self ,args ,name ): 216return args [args .index (name )+ 1 ]if name in args else None 217 218def test_recommended_size_on_128gb (self ): 219code ,out ,cfg ,_ = self .run_install ("128GB-1x24GB" ) 220self .assertEqual (code ,0 ,out [- 2000 :]) 221a = cfg ["args" ] 222self .assertTrue (self .flag (a ,"--pack" ).endswith ("Maximilian228--Qwen3.8-Flash-Next-NVFP4-GPTQ-Strata/pack" )) 223dense = self .flag (a ,"--native" ) 224self .assertTrue (dense .endswith ("/qwen-nvfp4-gptq-dense.gguf" )) 225self .assertEqual (self .flag (a ,"--native-dense-gguf" ),dense ) 226self .assertTrue (self .flag (a ,"--ple-gguf" ).endswith ("/ple-fp8.gguf" )) 227self .assertTrue (self .flag (a ,"--embd-gguf" ).endswith ("/token-embd-bf16.gguf" )) 228self .assertNotIn ("--mmap-experts" ,a )# 128 GB: every expert in RAM 229self .assertNotIn ("--resident-experts" ,a ) 230self .assertIn ("vision" ,cfg )# --yes: pictures on, as for every model that can read them 231self .assertEqual (cfg ["model_name" ],"qwen3.8-flash-next-nvfp4-nvfp4" ) 232self .assertIn ("Qwen Community License" ,out ) 233 234def test_images_use_the_dense_gguf_and_the_pinned_encoder (self ): 235code ,out ,cfg ,_ = self .run_install ("128GB-1x24GB" ,extra_argv = ["--vision" ,"gpu" ]) 236self .assertEqual (code ,0 ,out [- 2000 :]) 237v = cfg ["vision" ] 238self .assertTrue (v ["model" ].endswith ("/qwen-nvfp4-gptq-dense.gguf" )) 239self .assertTrue (v ["mmproj" ].endswith ("/mmproj-Qwen3.8-Flash-Next-BF16.gguf" )) 240self .assertTrue (v ["gpu" ]) 241self .assertIn ("--vision" ,cfg ["args" ]) 242 243def test_nvidia_size_and_its_license (self ): 244code ,out ,cfg ,_ = self .run_install ("128GB-1x24GB" ,"NVFP4-NVIDIA" ) 245self .assertEqual (code ,0 ,out [- 2000 :]) 246self .assertIn ("NVIDIA-NVFP4-Strata/pack" ,self .flag (cfg ["args" ],"--pack" )) 247self .assertIn ("NVIDIA Open Model License" ,out ) 248 249def test_qad_size_is_prepared_and_uses_its_own_files (self ): 250code ,out ,cfg ,_ = self .run_install ("128GB-1x24GB" ,"NVFP4-QAD" ) 251self .assertEqual (code ,0 ,out [- 2500 :]) 252a = cfg ["args" ] 253base = "nvidia--Qwen3.8-Flash-Next-FP4-Local/prepared" 254self .assertTrue (self .flag (a ,"--pack" ).endswith (base + "/pack" )) 255self .assertTrue (self .flag (a ,"--native" ).endswith (base + "/dense.gguf" )) 256self .assertEqual (self .flag (a ,"--native-dense-gguf" ),self .flag (a ,"--native" )) 257self .assertTrue (self .flag (a ,"--ple-gguf" ).endswith (base + "/ple-nvfp4.gguf" )) 258self .assertNotIn ("--embd-gguf" ,a )# its dense GGUF holds the token embedding 259self .assertTrue (self .flag (a ,"--mtp" ).endswith (base + "/mtp/rt" ))# NVIDIA's own draft layer, not Qwen's 260self .assertIn ("NVIDIA Open Model Agreement" ,out ) 261self .assertIn ("turned into a Strata pack on this PC" ,out ) 262 263def test_qad_reads_pictures_with_its_prepared_dense_gguf (self ): 264code ,out ,cfg ,_ = self .run_install ("128GB-1x24GB" ,"NVFP4-QAD" ,extra_argv = ["--vision" ,"gpu" ]) 265self .assertEqual (code ,0 ,out [- 2500 :]) 266v = cfg ["vision" ] 267self .assertTrue (v ["model" ].endswith ("nvidia--Qwen3.8-Flash-Next-FP4-Local/prepared/dense.gguf" )) 268self .assertTrue (v ["mmproj" ].endswith ("/mmproj-Qwen3.8-Flash-Next-BF16.gguf" )) 269self .assertIn ("--vision" ,cfg ["args" ]) 270self .assertNotIn ("images are not available" ,out ) 271 272def test_96gb_keeps_every_expert_in_ram_64gb_takes_the_ram_budget (self ): 273code ,out ,cfg ,_ = self .run_install ("96GB-1x16GB" )# 68 GB of experts + 10 GB beside them fit 95.8 274self .assertEqual (code ,0 ,out [- 2000 :]) 275self .assertFalse ({"--mmap-experts" ,"--resident-experts" }& set (cfg ["args" ])) 276code ,out ,cfg ,_ = self .run_install ("64GB-1x32GB" )# 64 GB: the RAM-budget mode (RAM less 24 GB) 277self .assertEqual (code ,0 ,out [- 2000 :]) 278a = cfg ["args" ] 279self .assertFalse ({"--mmap-experts" ,"--resident-experts" }& set (a )) 280self .assertEqual (a [a .index ("--resident-budget-gib" )+ 1 ],"40" ) 281 282def test_amd_is_refused_with_a_recommendation (self ): 283import test_setup_golden as G 284ram ,_ = G .PROFILES ["128GB-1x24GB" ] 285amd = [{"index" :0 ,"name" :"AMD Radeon AI PRO R9700" ,"vram_gb" :31.9 ,"arch" :"gfx1201" }] 286code ,out ,cfg ,_ = G .install (ram , [], ["--family" ,"nvfp4" ,"--no-start" ,"--backend" ,"hip" ], 287extra = [mock .patch .object (setup ,"amd_gpus" ,lambda * a ,** k :amd )]) 288self .assertNotEqual (code ,0 ) 289self .assertIn ("UD-IQ4_XS" ,out ) 290 291 292if __name__ == "__main__" : 293unittest .main ()