okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
aff3a132f
main
1"""Tests for setup.py's choices that depend on the PC (no GPU, no network, nothing installed): the image encoder of a 2ready-made engine that has no code for the card (#331), --gguf-dir's shard count (#305), the experimental 3Pascal/Volta build (#295), the CPU image encoder with the AMD backend (#304). 4 5python -m unittest tools.test_setup_choices 6""" 7from __future__import annotations 8 9import contextlib 10import io 11import json 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 24 25def quiet (fn ,* args ,** kw ): 26out = io .StringIO () 27with contextlib .redirect_stdout (out ): 28return fn (* args ,** kw ),out .getvalue () 29 30 31class PrebuiltVision (unittest .TestCase ): 32"""#331: the 0.1.30/0.1.31 zip's encoder has no sm_75 code: an RTX 20 card gets the CPU encoder, not a compile.""" 33META = {"version" :"0.1.31" ,"archs" : [75 ,86 ,89 ,120 ],"ptx" :True ,"vision_archs" : [86 ,89 ,120 ]} 34 35def test_a_card_the_encoder_has_no_code_for_gets_the_cpu_encoder (self ): 36got ,out = quiet (setup .prebuilt_vision ,self .META , {"arch" :75 },"gpu" ) 37self .assertEqual (got ,"cpu" ) 38self .assertIn ("runs on the CPU instead" ,out ) 39 40def test_covered_cards_keep_the_gpu_encoder (self ): 41for arch in (86 ,89 ,120 ): 42self .assertEqual (quiet (setup .prebuilt_vision ,self .META , {"arch" :arch },"gpu" )[0 ],"gpu" ) 43# a newer card than the newest encoder code: only with PTX in the zip 44self .assertEqual (quiet (setup .prebuilt_vision , {** self .META ,"vision_archs" : [86 ,89 ]}, {"arch" :120 },"gpu" )[0 ], 45"gpu" ) 46self .assertEqual (quiet (setup .prebuilt_vision , {** self .META ,"vision_archs" : [86 ,89 ],"ptx" :False }, 47 {"arch" :120 },"gpu" )[0 ],"cpu" ) 48# 0.1.32's zip: the encoder built with 75-real too 49self .assertEqual (quiet (setup .prebuilt_vision , {** self .META ,"vision_archs" : [75 ,86 ,89 ,120 ]}, 50 {"arch" :75 },"gpu" )[0 ],"gpu" ) 51 52def test_other_choices_are_left_alone (self ): 53for v in ("cpu" ,"none" ): 54self .assertEqual (quiet (setup .prebuilt_vision ,self .META , {"arch" :75 },v ), (v ,"" )) 55# an older BUILD.json without vision_archs: the engine's archs 56self .assertEqual (quiet (setup .prebuilt_vision , {"archs" : [75 ,86 ]}, {"arch" :75 },"gpu" )[0 ],"gpu" ) 57 58 59class VisionArchsAsAString (unittest .TestCase ): 60"""#3810: the packaging recipes write BUILD.json `vision_archs` as ONE string ("75;86;89;120"); 0.2.3 iterated it character by 61character and setup stopped with int(';') on the released Windows CUDA package. A string and a list mean the same.""" 62RELEASED = {"version" :"0.2.3" ,"archs" : [75 ,86 ,89 ,120 ],"ptx" :True ,"cuda" :"13.0" ,"vision" :"gpu-cuda13" , 63"vision_archs" :"75;86;89;120" ,"portable" :True } 64 65def test_the_released_package_manifest_is_read (self ): 66for arch in (75 ,86 ,89 ,120 ): 67self .assertEqual (quiet (setup .prebuilt_vision ,self .RELEASED , {"arch" :arch },"gpu" )[0 ],"gpu" ) 68 69def test_a_card_the_string_does_not_list_gets_the_cpu_reader (self ): 70meta = {** self .RELEASED ,"vision_archs" :"86;89;120" ,"ptx" :False } 71got ,out = quiet (setup .prebuilt_vision ,meta , {"arch" :75 },"gpu" ) 72self .assertEqual (got ,"cpu" ) 73self .assertIn ("runs on the CPU instead" ,out ) 74self .assertEqual (quiet (setup .prebuilt_vision , {** meta ,"ptx" :True }, {"arch" :120 },"gpu" )[0 ],"gpu" ) 75 76def test_a_string_and_a_list_agree (self ): 77for value in ("75;86;89;120" , [75 ,86 ,89 ,120 ], ["sm_75" ,"86-real" ,"89" ,"compute_120" ],"75,86 89;120" ): 78self .assertEqual (setup .arch_numbers (value ), [75 ,86 ,89 ,120 ],value ) 79self .assertEqual (setup .arch_numbers (None ), []) 80self .assertEqual (setup .arch_numbers ("" ), []) 81self .assertEqual (setup .arch_names ("gfx1100;gfx1201" ), ["gfx1100" ,"gfx1201" ]) 82self .assertEqual (setup .arch_names (["gfx1100" ]), ["gfx1100" ]) 83self .assertEqual (setup .arch_names (None ), []) 84 85def test_the_other_readers_of_the_manifest_take_a_string_too (self ): 86import tempfile 87from pathlib import Path 88from unittest import mock 89with tempfile .TemporaryDirectory ()as d : 90 (Path (d )/ "BUILD.json" ).write_text ('{"archs": "60;61;70", "ptx": true}' ,encoding = "utf-8" ) 91with mock .patch .object (setup ,"engine_dir" ,return_value = Path (d )): 92self .assertEqual (setup .engine_archs (), ([60 ,61 ,70 ],True )) 93 (Path (d )/ "BUILD.json" ).write_text ('{"backend": "hip", "archs": ["gfx1100"], "vision_archs": "gfx1100;gfx1201"}' ,encoding = "utf-8" ) 94self .assertEqual (setup .reader_archs (Path (d )), ["gfx1100" ,"gfx1201" ]) 95 (Path (d )/ "BUILD.json" ).write_text ('{"backend": "hip", "archs": ["gfx1100", "gfx1201"]}' ,encoding = "utf-8" ) 96self .assertEqual (setup .reader_archs (Path (d )), ["gfx1100" ,"gfx1201" ]) 97self .assertEqual (setup .reader_archs (Path (__file__ ).parent / "no-such-folder" ), []) 98 99 100class GgufDirShards (unittest .TestCase ): 101"""#305: --gguf-dir reads the shard count from the -of-N part of the name instead of assuming two.""" 102 103def shards (self ,names ,family = "qwen" ,model = "IQ3_XXS" ): 104with tempfile .TemporaryDirectory ()as d : 105for n in names : 106 (Path (d )/ n ).write_bytes (b"" ) 107return [p .name for p in setup .gguf_dir_shards (Path (d ),setup .FAMILIES [family ],model )] 108 109def test_the_published_two_shards (self ): 110names = ["Qwen3.8-Flash-Next-GSQ-RCO-IQ3_XXS-00001-of-00002.gguf" , 111"Qwen3.8-Flash-Next-GSQ-RCO-IQ3_XXS-00002-of-00002.gguf" ] 112self .assertEqual (self .shards (names ),names ) 113self .assertEqual (self .shards (names [:1 ]),names )# shard 2 missing: check_shards says so later 114swift = ["Swift-Qwen3.8-Flash-Next-GSQ-RCO-Q2_0-00001-of-00002.gguf" , 115"Swift-Qwen3.8-Flash-Next-GSQ-RCO-Q2_0-00002-of-00002.gguf" ] 116self .assertEqual (self .shards (swift + ["mmproj-Swift-Qwen3.8-Flash-Next-BF16.gguf" ],"swift" ,"Q2_0" ),swift ) 117 118def test_four_unsloth_shards (self ): 119names = ["Qwen3.8-Flash-Next-UD-Q4_K_XL-%05d-of-00004.gguf" % i for i in range (1 ,5 )] 120self .assertEqual (self .shards (names + ["mmproj-F16.gguf" ],model = "Q4_K_XL" ),names ) 121self .assertEqual (self .shards (names [:1 ],model = "Q4_K_XL" ),names )# the others named from the first 122 123def test_another_split_of_a_setup_size (self ): 124names = ["my-IQ3_XXS-%05d-of-00003.gguf" % i for i in range (1 ,4 )] 125other = ["my-Q2_0-%05d-of-00002.gguf" % i for i in range (1 ,3 )] 126self .assertEqual (self .shards (names + other ),names )# the one with the size in its name 127self .assertEqual (self .shards (other ,model = "Q2_0" ),other ) 128 129def test_nothing_recognisable_keeps_the_published_names (self ): 130want = ["Qwen3.8-Flash-Next-GSQ-RCO-IQ3_XXS-00001-of-00002.gguf" , 131"Qwen3.8-Flash-Next-GSQ-RCO-IQ3_XXS-00002-of-00002.gguf" ] 132self .assertEqual (self .shards ([]),want ) 133self .assertEqual (self .shards (["a-00001-of-00002.gguf" ,"b-00001-of-00002.gguf" ]),want )# ambiguous 134 135 136class GgufDirUnsupported (unittest .TestCase ): 137"""#444: --gguf-dir with GGUFs Strata cannot run (Unsloth's UD-IQ3_XXS, UD-Q2_K_XL) says so, names the files it 138runs (ISTA-DASLab's GSQ-RCO, the Coder's, Unsloth's UD-Q4_K_XL) and the --family/--model of the usable ones.""" 139UD = ["Qwen3.8-Flash-Next-UD-IQ1_S-%05d-of-00003.gguf" % i for i in range (1 ,4 )]+ \ 140 ["Qwen3.8-Flash-Next-BF16-%05d-of-00008.gguf" % i for i in range (1 ,4 )]# round 0214: UD-IQ3_XXS and UD-Q2_K_XL run now; UD-IQ1_S and BF16 do not 141 142def problem (self ,names ,family = "qwen" ,model = "IQ3_XXS" ): 143with tempfile .TemporaryDirectory ()as d : 144for n in names : 145 (Path (d )/ n ).write_bytes (b"" ) 146fam = setup .FAMILIES [family ] 147first = setup .gguf_dir_shards (Path (d ),fam ,model )[0 ] 148p = setup .gguf_dir_problem (Path (d ),first ,fam ,model ) 149return p and (p [0 ].replace (d ,"<D>" ),p [1 ]) 150 151def test_quant_names (self ): 152for name ,want in (("Qwen3.8-Flash-Next-UD-IQ1_S-00001-of-00003.gguf" ,"UD-IQ1_S" ), 153 ("Qwen3.8-Flash-Next-UD-Q2_K_XL-00001-of-00003.gguf" ,None ), 154 ("Qwen3.8-Flash-Next-UD-IQ3_XXS-00001-of-00004.gguf" ,None ), 155 ("model-Q4_K_M.gguf" ,"Q4_K_M" ), 156 ("Qwen3.8-Flash-Next-GSQ-RCO-IQ3_XXS-00001-of-00002.gguf" ,None ), 157 ("Qwen3.8-Flash-Next-UD-Q4_K_XL-00001-of-00004.gguf" ,None ), 158 ("my-IQ3_XXS-00001-of-00003.gguf" ,None ), ("mmproj-F16.gguf" ,None ), 159 ("a-00001-of-00002.gguf" ,None )): 160self .assertEqual (setup .gguf_unsupported (name ),want ,name ) 161 162def test_an_unsloth_ud_file_is_not_taken_for_a_gsq_rco_size (self ): 163msg ,hint = self .problem (self .UD [:3 ],"unsloth" ,"UD-IQ1_M" )# a UD-IQ1_S set asked for as UD-IQ1_M 164self .assertIn ("UD-IQ1_S" ,msg + hint ) 165self .assertIn ("ISTA-DASLab's GSQ-RCO files" ,hint ) 166 167def test_a_folder_without_the_choice_names_what_is_there (self ): 168gsq = ["Qwen3.8-Flash-Next-GSQ-RCO-IQ1_M-%05d-of-00002.gguf" % i for i in (1 ,2 )] 169msg ,hint = self .problem (self .UD + gsq ,"unsloth" ,"UD-Q4_K_XL" ) 170self .assertEqual (msg ,"<D> has no Qwen3.8-Flash-Next (Unsloth) UD-Q4_K_XL file" ) 171self .assertIn ("Not usable here: Qwen3.8-Flash-Next-BF16-00001-of-00008.gguf, " 172"Qwen3.8-Flash-Next-UD-IQ1_S-00001-of-00003.gguf" ,hint ) 173self .assertIn ("Usable here: --family coder --model IQ1_M" ,hint ) 174 175def test_usable_folders_are_left_alone (self ): 176good = ["Qwen3.8-Flash-Next-GSQ-RCO-IQ3_XXS-%05d-of-00002.gguf" % i for i in (1 ,2 )] 177self .assertIsNone (self .problem (good + self .UD )) 178self .assertIsNone (self .problem (["my-IQ3_XXS-%05d-of-00003.gguf" % i for i in (1 ,2 ,3 )])) 179self .assertIsNone (self .problem ([]))# nothing there: check_shards' "missing", as before 180 181def test_the_size_of_another_family (self ): 182from test_setup_golden import PROFILES ,install 183ram ,found = PROFILES ["64GB-1x32GB" ] 184with tempfile .TemporaryDirectory ()as d : 185code ,out ,_ ,_ = install (ram ,found , ["--gguf-dir" ,d ,"--family" ,"unsloth" ,"--model" ,"IQ3_XXS" ]) 186self .assertEqual (code ,1 ) 187self .assertIn ("has no IQ3_XXS model file" ,out ) 188self .assertIn ("choose one of: UD-IQ4_XS, UD-Q4_K_XL, UD-Q5_K_XL, UD-Q6_K_XL, Q8_0, UD-IQ1_M, UD-Q2_0, UD-IQ2_XS, UD-Q2_K_XL, UD-IQ3_XXS, UD-Q3_K_XL, " 189"UD-IQ4_XS-v31 (or IQ3_XXS: --family qwen --model IQ3_XXS, --family swift --model IQ3_XXS)" ,out ) 190self .assertIn ("Strata runs ISTA-DASLab's GSQ-RCO files" ,out ) 191 192 193class ReadOnlyGgufDir (unittest .TestCase ): 194"""--gguf-dir on a read-only folder (a share): whole shards without a finish mark still install, with a warning.""" 195 196def test_a_finish_mark_that_cannot_be_written_is_a_warning (self ): 197from test_setup_golden import PROFILES ,install 198ram ,found = PROFILES ["64GB-1x32GB" ] 199real_mark = setup .mark 200 201def read_only_mark (path ,text = "" ): 202if "gguf_share" in str (path ): 203raise PermissionError (13 ,"Read-only file system" ) 204real_mark (path ,text ) 205 206with tempfile .TemporaryDirectory ()as tmp : 207d = Path (tmp )/ "gguf_share" 208d .mkdir () 209for i in (1 ,2 ): 210 (d / f"Qwen3.8-Flash-Next-GSQ-RCO-Q2_0- { i :05d } -of-00002.gguf" ).write_bytes (b"" ) 211code ,out ,cfg ,_ = install (ram ,found , ["--gguf-dir" ,str (d ),"--family" ,"qwen" ,"--model" ,"Q2_0" , 212"--no-start" ], 213extra = [mock .patch .object (setup ,"whole_shard" ,lambda s :True ), 214mock .patch .object (setup ,"mark" ,read_only_mark )]) 215self .assertEqual (code ,0 ,out ) 216self .assertIn ("is whole but its finish mark cannot be written" ,out ) 217 218 219def test_mark_itself_never_stops_on_a_read_only_folder (self ): 220"""#570: every finish mark (the SHA-256 result of a verified shard too), not only the one for a whole shard.""" 221with tempfile .TemporaryDirectory ()as tmp ,mock .patch .object (Path ,"write_text" , 222side_effect = PermissionError (13 ,"read-only" )): 223setup .mark (Path (tmp )/ "x.gguf" ,"sha256 abc" )# no exception 224self .assertFalse (setup .done (Path (tmp )/ "x.gguf" )) 225 226 227class ReadOnlyModelFolderMark (unittest .TestCase ): 228"""0.2.10: a model folder Strata cannot write to (a read-only disk) keeps its finish marks under Strata's own folder, so the 229SHA-256 of the 110 GB Engram is not computed again on every launch.""" 230 231def test_the_mark_is_kept_elsewhere_and_the_hash_is_not_repeated (self ): 232real_write = Path .write_text 233 234def write (self ,* a ,** k ): 235if self .name .endswith (".done" )and "strata_marks" not in str (self ): 236raise PermissionError (13 ,"read-only" ) 237return real_write (self ,* a ,** k ) 238 239with tempfile .TemporaryDirectory ()as tmp : 240root ,share = Path (tmp )/ "strata_marks" ,Path (tmp )/ "share" 241share .mkdir () 242f = share / "engram.gguf" 243f .write_bytes (b"abc" * 10 ) 244sha = "f" * 64 245hashed = [] 246 247def fake_hash (path ,size ): 248hashed .append (path ) 249return sha 250 251with mock .patch .object (setup ,"ROOT" ,root ),mock .patch .object (Path ,"write_text" ,write ),mock .patch .object (setup ,"_hash_file" ,fake_hash ): 252setup .verify_sha256 (f ,30 ,sha ) 253self .assertEqual (len (hashed ),1 ) 254self .assertFalse ((share / "engram.gguf.done" ).exists ()) 255self .assertTrue (setup .done (f )) 256setup .verify_sha256 (f ,30 ,sha )# the second launch 257self .assertEqual (len (hashed ),1 ,"the kept mark is found; nothing is hashed again" ) 258f .write_bytes (b"abcd" * 10 )# a changed file has no mark 259self .assertFalse (setup .done (f )) 260 261 262class OfflineStart (unittest .TestCase ): 263"""0.2.10: a PC with every file and no internet starts a model that is already prepared.""" 264 265def test_preparing_a_model_never_fetches_llama_cpp (self ): 266"""OWN-1B/2B: the preparation tools have their own GGUF code (tools/strata_gguf.py), so neither starting a prepared model nor running a 267preparation tool reaches github.com, and the tools are not told to use gguf-py unless the user set STRATA_GGUF_PY.""" 268fetched = [] 269env = {k :v for k ,v in os .environ .items ()if k not in ("STRATA_GGUF_PY" ,"STRATA_GGML_DIR" )} 270with mock .patch .dict (os .environ ,env ,clear = True ),mock .patch .object (setup ,"get_llama_cpp" ,lambda :fetched .append (1 )): 271self .assertIsNone (setup .llama_cpp_if_here (),"no override tree by default" ) 272self .assertEqual (setup .gguf_py_dir (),setup .ROOT / "third_party" / "strata-gguf-py" ) 273self .assertTrue ((setup .gguf_py_dir ()/ "gguf" ).is_dir ()) 274self .assertEqual (setup .gguf_py_env (), {},"the tools use their own GGUF code by default" ) 275with mock .patch .object (setup .subprocess ,"run" ,lambda * a ,** k :mock .Mock (returncode = 0 )), \ 276mock .patch .object (setup .acquisition ,"require" ,lambda * a ,** k :None ),mock .patch .object (setup ,"CONTROL" ,False ): 277setup .run ([sys .executable ,"tools/x.py" ],env = dict (os .environ ,** setup .gguf_py_env ())) 278self .assertEqual (fetched , [],"nothing is downloaded" ) 279 280def test_the_overrides_are_kept (self ): 281"""STRATA_GGUF_PY and STRATA_GGML_DIR still name another copy (kept for two releases).""" 282with tempfile .TemporaryDirectory ()as tmp : 283 (Path (tmp )/ "gguf" ).mkdir () 284 (Path (tmp )/ "ggml" ).mkdir () 285 (Path (tmp )/ "ggml" / "CMakeLists.txt" ).write_text ("" ,encoding = "utf-8" ) 286with mock .patch .dict (os .environ , {"STRATA_GGUF_PY" :tmp ,"STRATA_GGML_DIR" :tmp }): 287self .assertEqual (setup .gguf_py_dir (),Path (tmp )) 288self .assertEqual (setup .gguf_py_env (), {"STRATA_GGUF_PY" :tmp },"the override is passed on to the tools" ) 289self .assertEqual (setup .llama_override (),Path (tmp )) 290self .assertEqual (setup .ggml_defs (setup .llama_override ()), [f"-DSTRATA_GGML_DIR= { tmp } " ]) 291self .assertEqual (setup .ggml_defs (None ), [],"nothing is passed by default" ) 292 293def test_a_pinned_file_copied_in_by_hand_is_checked_here_without_the_network (self ): 294with tempfile .TemporaryDirectory ()as tmp : 295f = Path (tmp )/ "drafter.gguf" 296f .write_bytes (b"abc" * 10 ) 297url = "https://huggingface.co/x/y/resolve/main/drafter.gguf" 298with mock .patch .object (setup ,"file_pin" ,lambda u : (30 ,"a" * 64 )),mock .patch .object (setup ,"_hash_file" ,lambda p ,n :"a" * 64 ),mock .patch .object (setup ,"_urlopen" ,side_effect = OSError ("no network" )): 299quiet (setup .download ,url ,f ,"drafter" ) 300self .assertTrue (setup .done (f )) 301self .assertIn ("sha256 " + "a" * 64 ,setup .mark_text (f )) 302 303 304 305class VsRange (unittest .TestCase ): 306"""#985: Visual Studio 2026 (version 18) is looked for only with CUDA 13.3 or newer.""" 307 308def asked (self ,cuda_v ): 309seen = [] 310with mock .patch .object (setup .Path ,"exists" ,lambda self :True ),mock .patch .object (setup ,"out" ,lambda cmd ,* a ,** k :seen .append (cmd )or "C:/VS" ): 311setup .find_vcvars (cuda_v ) 312return seen [0 ][seen [0 ].index ("-version" )+ 1 ]if "-version" in seen [0 ]else None 313 314def test_the_range_follows_the_toolkit (self ): 315self .assertIsNone (self .asked (None ))# #881: without CUDA vswhere gets no version range 316self .assertEqual (self .asked ((13 ,0 )),"[17.0,18.0)" ) 317self .assertEqual (self .asked ((13 ,2 )),"[17.0,18.0)" ) 318self .assertEqual (self .asked ((13 ,3 )),"[17.0,19.0)" ) 319self .assertEqual (self .asked ((14 ,0 )),"[17.0,19.0)" ) 320 321 322def test_vcvars_by_hand (self ): 323"""#881: STRATA_VCVARS names a vcvars64.bat directly, for an install vswhere cannot use.""" 324import tempfile 325with tempfile .TemporaryDirectory ()as d : 326bat = Path (d )/ "vcvars64.bat" 327bat .write_text ("rem" ) 328with mock .patch .dict (os .environ , {"STRATA_VCVARS" :str (bat )}): 329self .assertEqual (setup .find_vcvars (),bat ) 330with mock .patch .dict (os .environ , {"STRATA_VCVARS" :str (Path (d )/ "missing.bat" )}): 331self .assertIsNone (setup .find_vcvars ()) 332 333class ExperimentalSm60 (unittest .TestCase ): 334"""#295: Pascal (6.x) and Volta (7.0) only with STRATA_EXPERIMENTAL_SM60=1, built with -DSTRATA_EXPERIMENTAL_SM60=ON 335and a CUDA 12.x toolkit; nothing changes without the variable.""" 336 337def card (self ,arch ): 338return {"arch" :arch ,"vram_gb" :11.0 ,"index" :0 ,"name" :"card" } 339 340def test_the_gate (self ): 341with mock .patch .dict (os .environ , {"STRATA_EXPERIMENTAL_SM60" :"" }): 342for arch in ("60" ,"61" ,"70" ): 343p = setup .gpu_problem (self .card (arch )) 344self .assertIn ("not supported" ,p ) 345self .assertIn ("choose it with --gpu 0" ,p )# the CUDA 12 engine (docs/OLDER_GPUS.md) 346self .assertNotIn ("--gpu" ,setup .gpu_problem (self .card ("52" ))) 347self .assertIsNone (setup .gpu_problem (self .card ("75" ))) 348with mock .patch .dict (os .environ , {"STRATA_EXPERIMENTAL_SM60" :"1" }): 349for arch in ("60" ,"61" ,"70" ,"75" ,"120" ): 350self .assertIsNone (setup .gpu_problem (self .card (arch )),arch ) 351for arch in ("52" ,"72" ): 352self .assertIsNotNone (setup .gpu_problem (self .card (arch )),arch ) 353 354def test_the_build_flag (self ): 355self .assertEqual (setup .engine_defs ([61 ]), ["-DSTRATA_EXPERIMENTAL_SM60=ON" ]) 356self .assertEqual (setup .engine_defs ([70 ,86 ]), ["-DSTRATA_EXPERIMENTAL_SM60=ON" ]) 357self .assertEqual (setup .engine_defs ([75 ,86 ,120 ]), []) 358 359def test_find_nvcc_below (self ): 360with tempfile .TemporaryDirectory ()as d : 361new ,old = Path (d )/ "13" / "nvcc" ,Path (d )/ "12" / "bin" / "nvcc" 362for p in (new ,old ): 363p .parent .mkdir (parents = True ) 364p .write_bytes (b"" ) 365versions = {str (new ):"Cuda compilation tools, release 13.0, V13.0.88" , 366str (old ):"Cuda compilation tools, release 12.9, V12.9.86" } 367with mock .patch .object (setup ,"WIN" ,False ),mock .patch .object (setup .shutil ,"which" ,lambda n :str (new )), \ 368mock .patch .dict (os .environ , {"CUDA_PATH" :str (Path (d )/ "12" )}), \ 369mock .patch .object (setup ,"out" ,lambda cmd :versions .get (cmd [0 ],"" )):# #414 370self .assertEqual (setup .find_nvcc (), (str (new ), (13 ,0 ))) 371self .assertEqual (setup .find_nvcc (below = (13 ,0 )), (str (old ), (12 ,9 ))) 372# #601: STRATA_NVCC is the only one considered; CUDA_HOME is a candidate like CUDA_PATH 373with mock .patch .dict (os .environ , {"STRATA_NVCC" :str (old )}): 374self .assertEqual (setup .find_nvcc (), (str (old ), (12 ,9 ))) 375with mock .patch .dict (os .environ , {"STRATA_NVCC" :str (new )}): 376got ,text = quiet (setup .find_nvcc ,below = (13 ,0 )) 377self .assertEqual (got , (None ,None )) 378self .assertIn ("needs one older than 13.0" ,text ) 379with mock .patch .dict (os .environ , {"STRATA_NVCC" :str (Path (d )/ "missing" )}): 380got ,text = quiet (setup .find_nvcc ) 381self .assertEqual (got , (str (new ), (13 ,0 ))) 382self .assertIn ("no such file" ,text ) 383with mock .patch .object (setup ,"WIN" ,False ),mock .patch .object (setup .shutil ,"which" ,lambda n :None ), \ 384mock .patch .dict (os .environ , {"CUDA_HOME" :str (Path (d )/ "12" )}), \ 385mock .patch .object (setup ,"out" ,lambda cmd :versions .get (cmd [0 ],"" )): 386os .environ .pop ("CUDA_PATH" ,None ) 387got = setup .find_nvcc (below = (13 ,0 )) 388self .assertEqual (got , (str (old ), (12 ,9 ))) 389 390def tools (self ,archs ,nvcc ): 391seen = [] 392 393def find (below = None ): 394seen .append (below ) 395return nvcc (below ) 396 397with mock .patch .object (setup ,"find_nvcc" ,find ),mock .patch .object (setup ,"find_vcvars" ,lambda cuda_v = None :"vcvars" ), \ 398mock .patch .object (setup .shutil ,"which" ,lambda n :"/usr/bin/" + n ): 399got ,_ = quiet (setup .install_build_tools , {"arch" :str (archs [0 ]),"archs" :archs },True ) 400return got ,seen 401 402def test_pascal_takes_cuda_12 (self ): 403got ,seen = self .tools ([61 ],lambda below : ("nvcc12" , (12 ,9 ))if below == (13 ,0 )else ("nvcc13" , (13 ,0 ))) 404self .assertEqual (got [0 ],"nvcc12" ) 405self .assertEqual (seen , [(13 ,0 )]) 406got ,seen = self .tools ([86 ,120 ],lambda below : ("nvcc13" , (13 ,0 ))) 407self .assertEqual ((got [0 ],seen ), ("nvcc13" , [None ]))# the default: unchanged 408 409def test_cuda_132_on_sm120 (self ): 410"""#892 / #968: nvcc 13.2 for an RTX 50 card: an older 13.x is taken when there is one, else a warning; never an 411error, and no other card or toolkit is touched.""" 412def nvcc (below ): 413return ("nvcc130" , (13 ,0 ))if below == (13 ,2 )else ("nvcc132" , (13 ,2 )) 414 415got ,seen = self .tools ([120 ],nvcc ) 416self .assertEqual (got [0 ],"nvcc130" ) 417self .assertEqual (seen , [None , (13 ,2 )]) 418with mock .patch .dict (os .environ , {"STRATA_NVCC" :"x" }):# the user's own pick is kept 419got ,seen = self .tools ([120 ],nvcc ) 420self .assertEqual ((got [0 ],seen ), ("nvcc132" , [None ])) 421only132 = lambda below : (None ,None )if below == (13 ,2 )else ("nvcc132" , (13 ,2 )) 422with mock .patch .object (setup ,"warn" )as warn :# nothing older: the one found, with a warning 423got ,_ = self .tools ([120 ],only132 ) 424self .assertEqual (got [0 ],"nvcc132" ) 425self .assertIn ("#892" ,warn .call_args [0 ][0 ]) 426for archs in ([86 ], [89 ,86 ]):# not an sm_120 card: as before 427got ,seen = self .tools (archs ,nvcc ) 428self .assertEqual ((got [0 ],seen ), ("nvcc132" , [None ])) 429 430def test_pascal_without_cuda_12_stops (self ): 431for archs in ([61 ], [70 ,120 ]): 432with self .subTest (archs = archs ),self .assertRaises (SystemExit ): 433self .tools (archs ,lambda below : (None ,None )if below else ("nvcc13" , (13 ,0 ))) 434 435 436class HipVision (unittest .TestCase ): 437"""#304: --vision cpu with --backend hip builds the CPU image encoder beside the HIP engine.""" 438 439@ mock . patch . object ( setup , "WIN" , False ) 440def test_the_choice (self ): 441self .assertEqual (quiet (setup .hip_vision ,"cpu" ), ("cpu" ,"" )) 442for asked in ("no" ,"none" ): 443self .assertEqual (quiet (setup .hip_vision ,asked ), ("none" ,"" )) 444for asked in (None ,"yes" ,"gpu" ):# pictures are on by default; the engine folder's readers settle gpu vs cpu 445self .assertEqual (quiet (setup .hip_vision ,asked ), ("gpu" ,"" )) 446 447def build (self ,meta ,vision ,vexe = False ): 448"""build_engine_hip with an engine that is already built: only the encoder can be missing.""" 449with tempfile .TemporaryDirectory ()as d : 450root = Path (d ) 451eng = root / "engine" 452eng .mkdir () 453 (eng / setup .EXE ).write_bytes (b"engine" ) 454if vexe : 455 (eng / setup .VEXE ).write_bytes (b"vision" ) 456src ,vsrc = "S" ,"V" 457 (eng / "BUILD.json" ).write_text (json .dumps ({"backend" :"hip" ,"archs" : ["gfx1201" ],"src" :src ,** meta })) 458built = [] 459 460def cmake_build (src_dir ,bdir ,target ,defs ,vcvars ,bat ): 461built .append ((target ,defs )) 462 (bdir / "bin" ).mkdir (parents = True ) 463 (bdir / "bin" / setup .VEXE ).write_bytes (b"vision" ) 464 465with mock .patch .object (setup ,"ROOT" ,root ),mock .patch .object (setup ,"cmake_build" ,cmake_build ), \ 466mock .patch .object (setup ,"cpu_info" ,lambda : ("Test CPU" ,True ,False )), \ 467mock .patch .object (setup ,"source_hash" ,lambda paths :vsrc if paths == setup .VISION_SOURCES else src ): 468# an AVX2 CPU whatever the box has: on one without AVX2 (or one whose CPU flags cannot be read) the engine itself 469# would be rebuilt with an ISA floor 470quiet (setup .build_engine_hip , {"arch" :"gfx1201" },"llama" ,vision ) 471return built ,json .loads ((eng / "BUILD.json" ).read_text ()), (eng / setup .VEXE ).exists () 472 473def test_the_cpu_encoder_is_built_once (self ): 474built ,meta ,have = self .build ({},"cpu" ) 475self .assertEqual ([t for t ,_ in built ], ["strata-vision" ]) 476self .assertIn ("-DSTRATA_VISION_CUDA=OFF" ,built [0 ][1 ]) 477self .assertIn ("-DSTRATA_PORTABLE=OFF" ,built [0 ][1 ])# built for this PC (#411: portable is the default) 478self .assertEqual ((meta ["vision" ],meta ["vision_src" ],have ), ("cpu" ,"V" ,True )) 479built ,meta ,_ = self .build ({"vision" :"cpu" ,"vision_src" :"V" },"cpu" ,vexe = True ) 480self .assertEqual (built , [])# built and unchanged: nothing to do 481built ,meta ,_ = self .build ({"vision" :"cpu" ,"vision_src" :"old" },"cpu" ,vexe = True ) 482self .assertEqual ([t for t ,_ in built ], ["strata-vision" ])# its source changed: again 483 484def test_without_images_nothing_changes (self ): 485built ,meta ,have = self .build ({},"none" ) 486self .assertEqual ((built ,have ), ([],False )) 487self .assertNotIn ("vision_src" ,meta ) 488 489 490class RotationalDisk (unittest .TestCase ): 491"""#605: a model on a rotational disk gets --ple-io ram when the n-gram table fits the RAM, else a warning.""" 492 493def install (self ,ram ,disk ,platform_ok = False ,model = "Q2_0" ): 494from test_setup_golden import PROFILES ,install 495_ ,found = PROFILES ["64GB-1x32GB" ] 496return install (ram ,found , ["--family" ,"qwen" ,"--model" ,model ,"--no-start" ], 497extra = [mock .patch .object (setup ,"rotational_disk" ,lambda p :disk ), 498mock .patch .object (setup ,"ple_ram_platform_ok" ,lambda :platform_ok )]) 499 500def test_fits (self ): 501code ,out ,cfg ,_ = self .install (127.8 ,"sdb" ) 502self .assertEqual (code ,0 ,out ) 503a = cfg ["args" ] 504self .assertEqual (a [a .index ("--ple-io" )+ 1 ],"ram" ) 505self .assertIn ("rotational disk (sdb)" ,out ) 506 507def test_does_not_fit (self ): 508code ,out ,cfg ,_ = self .install (63.7 ,"sdb" ) 509self .assertEqual (code ,0 ,out ) 510self .assertNotIn ("--ple-io" ,cfg ["args" ]) 511self .assertIn ("An SSD is recommended" ,out ) 512 513def test_ssd (self ): 514code ,out ,cfg ,_ = self .install (127.8 ,None ) 515self .assertNotIn ("--ple-io" ,cfg ["args" ]) 516self .assertNotIn ("rotational" ,out ) 517 518def test_ssd_ram_to_spare (self ): 519# 147-verify (Linux): an SSD and RAM beyond the model's need + the table + 16 GB: the table in RAM too 520code ,out ,cfg ,_ = self .install (127.8 ,None ,platform_ok = True ) 521self .assertEqual (code ,0 ,out ) 522a = cfg ["args" ] 523self .assertEqual (a [a .index ("--ple-io" )+ 1 ],"ram" ) 524self .assertIn ("kept in RAM (--ple-io ram)" ,out ) 525self .assertNotIn ("rotational" ,out ) 526 527def test_ssd_little_ram_keeps_the_ssd (self ): 528# Q2_0 needs 48 GB + the 28.8 GB table + 16: a 64 GB / 92 GB PC keeps the SSD reads 529for ram in (63.7 ,92.0 ): 530code ,out ,cfg ,_ = self .install (ram ,None ,platform_ok = True ) 531self .assertEqual (code ,0 ,out ) 532self .assertNotIn ("--ple-io" ,cfg ["args" ]) 533 534def test_ssd_ram_rule_follows_the_model (self ): 535# IQ3_S needs 62 GB: 96 GB is not enough to spare the table, 128 GB is 536for ram ,want in ((95.8 ,False ), (127.8 ,True )): 537code ,out ,cfg ,_ = self .install (ram ,None ,platform_ok = True ,model = "IQ3_S" ) 538self .assertEqual (code ,0 ,out ) 539self .assertEqual ("--ple-io" in cfg ["args" ],want , (ram ,cfg ["args" ])) 540 541def test_sysfs (self ): 542if not sys .platform .startswith ("linux" ): 543self .assertIsNone (setup .rotational_disk (__file__ ))# Windows: never 544return 545self .assertIn (setup .rotational_disk (__file__ ), (None ,)+ tuple (os .listdir ("/sys/block" ))) 546 547 548class VramReserve (unittest .TestCase ): 549"""#493: --vram-reserve-mib N - VRAM the engine leaves free for other programs - goes into the config only when 550given (the default config is tools/test_setup_golden.py's, unchanged), at setup and on a start.""" 551 552def setUp (self ): 553from test_setup_golden import PROFILES 554self .ram ,self .found = PROFILES ["64GB-1x32GB" ] 555 556def install (self ,* extra ): 557from test_setup_golden import install 558return install (self .ram ,self .found , ["--family" ,"qwen" ,"--model" ,"Q2_0" ,"--no-start" ,* extra ]) 559 560def test_given_at_setup (self ): 561code ,out ,cfg ,_ = self .install ("--vram-reserve-mib" ,"2048" ) 562self .assertEqual (code ,0 ,out ) 563a = cfg ["args" ] 564self .assertEqual (a .count ("--vram-reserve-mib" ),1 ) 565self .assertEqual (a [a .index ("--vram-reserve-mib" )+ 1 ],"2048" ) 566self .assertIn ("VRAM kept free for other programs: 2048 MiB" ,out ) 567code ,out ,cfg ,_ = self .install ("--vision" ,"no" ) 568self .assertNotIn ("--vram-reserve-mib" ,cfg ["args" ])# not given: the engine's default, as before 569code ,out ,cfg ,_ = self .install ("--vision" ,"cpu" ) 570self .assertNotIn ("--vram-reserve-mib" ,cfg ["args" ])# the CPU reader needs no reserve 571code ,out ,cfg ,_ = self .install () 572self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--vram-reserve-mib" )+ 1 ],"700" )# pictures on by default, GPU reader on a 32 GB card: its 700 MiB 573 574def test_with_images_one_value (self ): 575code ,out ,cfg ,_ = self .install ("--vision" ,"cpu" ,"--vram-reserve-mib" ,"1500" ) 576self .assertEqual (code ,0 ,out ) 577a = cfg ["args" ] 578self .assertEqual (a .count ("--vram-reserve-mib" ),1 ) 579self .assertEqual (a [a .index ("--vram-reserve-mib" )+ 1 ],"1500" ) 580 581def test_a_negative_value_is_refused (self ): 582with contextlib .redirect_stderr (io .StringIO ())as err : 583code ,out ,cfg ,_ = self .install ("--vram-reserve-mib" ,"-1" ) 584self .assertEqual (code ,2 ) 585self .assertIn ("--vram-reserve-mib takes a number of MiB" ,err .getvalue ()) 586 587def test_kept_from_an_earlier_install (self ): 588with tempfile .TemporaryDirectory ()as d : 589p = Path (d )/ "strata-q2_0.json" 590p .write_text (json .dumps ({"args" : ["--max-context" ,"8192" ,"--vram-reserve-mib" ,"2048" ]})) 591self .assertEqual (setup .choices_from_config (p )["vram_reserve_mib" ],2048 ) 592p .write_text (json .dumps ({"args" : ["--vision" ,"--vram-reserve-mib" ,"700" ],"vision" : {"gpu" :True }})) 593self .assertIsNone (setup .choices_from_config (p )["vram_reserve_mib" ])# the images' own reserve 594p .write_text (json .dumps ({"args" : ["--max-context" ,"8192" ]})) 595self .assertIsNone (setup .choices_from_config (p )["vram_reserve_mib" ]) 596 597def test_given_on_a_start (self ): 598with tempfile .TemporaryDirectory ()as d : 599exe = Path (d )/ "strata.exe" 600exe .write_bytes (b"" ) 601p = Path (d )/ "strata-q2_0.json" 602p .write_text (json .dumps ({"exe" :str (exe ),"args" : ["--kv" ,"int8" ],"gpu" :0 ,"gpus_asked" :True })) 603call = mock .Mock (return_value = 0 ) 604with mock .patch .object (setup ,"gpus" ,lambda :self .found ), \ 605mock .patch .object (setup ,"is_wsl" ,lambda :False ), \ 606mock .patch .object (setup ,"ensure_engine_for" ,lambda cards ,path ,cfg ,yes :cfg ), \ 607mock .patch .object (setup .subprocess ,"call" ,call ): 608for n in ("2048" ,"1024" ): 609_ ,out = quiet (setup .start ,p ,None ,None ,False ,True ,None , {"vram_reserve_mib" :int (n )}) 610self .assertEqual (json .loads (p .read_text ())["args" ], ["--kv" ,"int8" ,"--vram-reserve-mib" ,n ]) 611self .assertIn ("1024 MiB of VRAM kept free" ,out ) 612self .assertTrue (call .called ) 613 614 615class SmallCardTip (unittest .TestCase ): 616"""#496: a card under 8 GB (a 6 GB laptop RTX 3060) gets a tip for when the start has no room for the expert cache; 617the engine chooses its own reserve there, so the config is the one any card gets. An 8 GB card: no tip.""" 618 619def install (self ,vram ,* extra ): 620from test_setup_golden import card ,install 621return install (63.7 , [card (0 ,"NVIDIA GeForce RTX 3060 Laptop GPU" ,vram ,"86" )], 622 ["--family" ,"qwen" ,"--model" ,"Q2_0" ,"--no-start" ,* extra ]) 623 624def test_a_6gb_card (self ): 625code ,out ,cfg ,_ = self .install (6.0 ) 626self .assertEqual (code ,0 ,out ) 627self .assertNotIn ("--vram-reserve-mib" ,cfg ["args" ])# the engine decides (no fixed reserve) 628self .assertIn ("no VRAM is left for the expert cache" ,out ) 629self .assertIn ("--draft-vocab en" ,out ) 630self .assertIn ("--mtp" ,cfg ["args" ])# the draft layer stays: the server needs it 631 632def test_an_8gb_card_has_no_tip (self ): 633for vram in (8188 / 1024 ,8.0 ):# nvidia-smi lists an 8 GB card as 8188 MiB 634code ,out ,cfg ,_ = self .install (vram ) 635self .assertEqual (code ,0 ,out ) 636self .assertNotIn ("no VRAM is left for the expert cache" ,out ) 637self .assertNotIn ("--vram-reserve-mib" ,cfg ["args" ]) 638 639def test_a_given_reserve_is_kept (self ): 640code ,out ,cfg ,_ = self .install (6.0 ,"--vram-reserve-mib" ,"500" ) 641self .assertEqual (code ,0 ,out ) 642a = cfg ["args" ] 643self .assertEqual (a [a .index ("--vram-reserve-mib" )+ 1 ],"500" ) 644 645def test_the_tip (self ): 646self .assertIn ("an 8K context" ," " .join (setup .small_card_note (32768 ,None ))) 647self .assertIn ("--draft-vocab en" ," " .join (setup .small_card_note (32768 ,"cjk" ))) 648tip = " " .join (setup .small_card_note (8192 ,"en" )) 649self .assertNotIn ("--draft-vocab" ,tip ) 650self .assertIn ("close other programs" ,tip ) 651 652 653class DesktopReserveTip (unittest .TestCase ): 654"""#560 #516: an AMD card on a Linux desktop gets a recommended reserve (3072 MiB) - a tip, the config is not 655changed.""" 656 657def test_desktop_detection (self ): 658with mock .patch .object (setup .sys ,"platform" ,"linux" ): 659self .assertTrue (setup .linux_desktop ({"WAYLAND_DISPLAY" :"wayland-0" })) 660self .assertTrue (setup .linux_desktop ({"DISPLAY" :":0" })) 661self .assertFalse (setup .linux_desktop ({}))# a headless box / ssh 662with mock .patch .object (setup .sys ,"platform" ,"win32" ): 663self .assertFalse (setup .linux_desktop ({"DISPLAY" :":0" }))# Windows counts the desktop itself (#497) 664 665def test_the_tip (self ): 666tip = " " .join (setup .desktop_reserve_note ()) 667self .assertIn ("--vram-reserve-mib 3072" ,tip ) 668self .assertIn ("desktop" ,tip ) 669 670 671class CudaVision (unittest .TestCase ): 672"""#411: the image encoder setup compiles beside the CUDA engine is built for this PC, not portable.""" 673 674def test_the_encoder_is_built_native (self ): 675for vision in ("gpu" ,"cpu" ): 676with self .subTest (vision ),tempfile .TemporaryDirectory ()as d : 677root = Path (d ) 678eng = root / "engine" 679eng .mkdir () 680 (eng / setup .EXE ).write_bytes (b"engine" )# the engine is built: only the encoder 681 (eng / "BUILD.json" ).write_text (json .dumps ({"source" :"local" ,"archs" : [86 ],"src" :"S" })) 682built = [] 683 684def cmake_build (src_dir ,bdir ,target ,defs ,vcvars ,bat ): 685built .append ((target ,defs )) 686 (bdir / "bin" ).mkdir (parents = True ) 687 (bdir / "bin" / setup .VEXE ).write_bytes (b"vision" ) 688 689with mock .patch .object (setup ,"ROOT" ,root ),mock .patch .object (setup ,"cmake_build" ,cmake_build ), \ 690mock .patch .object (setup ,"source_hash" ,lambda p :"V" if p == setup .VISION_SOURCES else "S" ), \ 691mock .patch .object (setup ,"install_build_tools" ,lambda gpu ,yes : (str (root / "nvcc" ),None )), \ 692mock .patch .object (setup ,"gpus" ,lambda : []), \ 693mock .patch .object (setup ,"cpu_info" ,lambda : ("Test CPU" ,True ,False )), \ 694mock .patch .object (setup ,"source_version" ,lambda :"test" ):# no real card of the box (#4662), an AVX2 CPU 695quiet (setup .build_engine , {"arch" :"86" },vision ,True ,"llama" ) 696self .assertEqual ([t for t ,_ in built ], ["strata-vision" ]) 697self .assertIn ("-DSTRATA_PORTABLE=OFF" ,built [0 ][1 ]) 698self .assertIn (f"-DSTRATA_VISION_CUDA= { 'ON' if vision == 'gpu' else 'OFF' } " ,built [0 ][1 ]) 699 700 701class ProxyHint (unittest .TestCase ): 702"""0.2.10: a download that cannot reach its server says what to do behind a proxy.""" 703 704def test_the_hint_names_the_variable_and_notices_one_already_set (self ): 705names = ("HTTPS_PROXY" ,"https_proxy" ,"ALL_PROXY" ,"all_proxy" ) 706with mock .patch .dict (os .environ , {},clear = False ): 707for n in names : 708os .environ .pop (n ,None ) 709self .assertIn ("set HTTPS_PROXY" ,setup .proxy_hint ()) 710os .environ ["HTTPS_PROXY" ]= "http://user:secret@proxy:3128" 711hint = setup .proxy_hint () 712self .assertIn ("a proxy is set" ,hint ) 713self .assertNotIn ("secret" ,hint ) 714 715 716class SwiftIq3s (unittest .TestCase ): 717"""#1651: Swift 1.5 has its own IQ3_S tier since 2026-10-08 (ukisai's Swift-1.5 GGUF repo) - the model choice 718must not hide it behind the qwen family. Names and sizes checked against the Hub listing when written.""" 719 720def test_iq3_s_is_a_swift_choice (self ): 721self .assertIn ("swift" ,setup .MODELS ["IQ3_S" ].get ("families" , ("qwen" ,"swift" ))) 722 723def test_the_published_file_names (self ): 724fam = setup .FAMILIES ["swift" ] 725self .assertEqual (setup .model_shards (fam ,"IQ3_S" ),2 ) 726self .assertEqual ([setup .model_file (fam ,"IQ3_S" ,i )for i in (1 ,2 )], 727 ["Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00001-of-00002.gguf" , 728"Swift-Qwen3.8-Flash-Next-GSQ-RCO-IQ3_S-00002-of-00002.gguf" ]) 729 730 731class HostCompilers (unittest .TestCase ): 732"""#1645: CC / CXX / CUDAHOSTCXX name the build's compilers - handed to CMake as -D, so a build folder that is 733already configured picks them up too (CXX alone reaches CMake only on the first configure: the reporter's 734`CXX=g++-14` run died at the last step). CUDAHOSTCXX is nvcc's host compiler and defaults to CXX.""" 735 736def test_env_names_the_compilers (self ): 737with mock .patch .dict (os .environ , {"CC" :"gcc-14" ,"CXX" :"g++-14" ,"CUDAHOSTCXX" :"g++-14-alt" }): 738self .assertEqual (setup .host_compiler_defs (True ), ["-DCMAKE_C_COMPILER=gcc-14" , 739"-DCMAKE_CXX_COMPILER=g++-14" , 740"-DCMAKE_CUDA_HOST_COMPILER=g++-14-alt" ]) 741 742def test_cudahostcxx_defaults_to_cxx_and_needs_cuda (self ): 743with mock .patch .dict (os .environ , {"CC" :"" ,"CXX" :"g++-14" ,"CUDAHOSTCXX" :"" }): 744self .assertEqual (setup .host_compiler_defs (True ), ["-DCMAKE_CXX_COMPILER=g++-14" , 745"-DCMAKE_CUDA_HOST_COMPILER=g++-14" ]) 746self .assertEqual (setup .host_compiler_defs (False ), ["-DCMAKE_CXX_COMPILER=g++-14" ])# a C++-only project 747 748def test_unset_adds_nothing (self ): 749with mock .patch .dict (os .environ , {"CC" :"" ,"CXX" :"" ,"CUDAHOSTCXX" :"" }): 750self .assertEqual (setup .host_compiler_defs (True ), []) 751 752def test_the_defs_reach_cmake (self ): 753"""The engine's and the CUDA encoder's configure both take them.""" 754with tempfile .TemporaryDirectory ()as d : 755root = Path (d ) 756eng = root / "engine" 757eng .mkdir () 758 (eng / "BUILD.json" ).write_text (json .dumps ({"source" :"local" ,"archs" : [86 ],"src" :"old" })) 759built = [] 760 761def cmake_build (src_dir ,bdir ,target ,defs ,vcvars ,bat ): 762built .append ((target ,defs )) 763 (bdir / "bin" ).mkdir (parents = True ,exist_ok = True ) 764 (bdir / "bin" / setup .VEXE ).write_bytes (b"vision" ) 765 (bdir / setup .EXE ).write_bytes (b"engine" ) 766 767env = {"CC" :"gcc-14" ,"CXX" :"g++-14" ,"CUDAHOSTCXX" :"" } 768with mock .patch .dict (os .environ ,env ),mock .patch .object (setup ,"ROOT" ,root ), \ 769mock .patch .object (setup ,"cmake_build" ,cmake_build ), \ 770mock .patch .object (setup ,"source_hash" ,lambda p :"V" if p == setup .VISION_SOURCES else "new" ), \ 771mock .patch .object (setup ,"install_build_tools" ,lambda gpu ,yes : (str (root / "nvcc" ),None )), \ 772mock .patch .object (setup ,"source_version" ,lambda :"test" ): 773quiet (setup .build_engine , {"arch" :"86" },"gpu" ,True ,"llama" ) 774for target ,defs in built : 775self .assertIn ("-DCMAKE_CXX_COMPILER=g++-14" ,defs ,target ) 776self .assertIn ("-DCMAKE_CUDA_HOST_COMPILER=g++-14" ,defs ,target ) 777 778 779if __name__ == "__main__" : 780unittest .main () 781 782 783class CalibrateWithModel (unittest .TestCase ): 784"""#2036: --calibrate --model X tunes model X (it used to fall through to a plain re-setup); a model that is not 785installed is an error that says so; --calibrate with --setup or --check says it does nothing.""" 786 787def run_main (self ,* argv ,installed = ("iq3_xxs" ,"q2_0" )): 788tmp = tempfile .TemporaryDirectory () 789self .addCleanup (tmp .cleanup ) 790cfgs = [] 791for tag in installed : 792p = Path (tmp .name )/ f"strata- { tag } .json" 793p .write_text (json .dumps ({"exe" :"x" ,"args" : ["--max-context" ,"8192" ],"model_name" :tag })) 794cfgs .append (p ) 795tuned = [] 796with mock .patch .object (sys ,"argv" , ["setup.py" ,* argv ]), \ 797mock .patch .object (setup ,"installed_configs" ,lambda :cfgs ), \ 798mock .patch .object (setup ,"update_installed_engine" ,lambda * a ,** k :None ), \ 799mock .patch .object (setup ,"calibrate_config" ,lambda c :tuned .append (c .name )or True ), \ 800mock .patch .object (setup ,"start" ,lambda * a ,** k :0 ), \ 801mock .patch .object (setup ,"data_folder" ,lambda d ,* a ,** k : (Path (tmp .name ), [])), \ 802mock .patch .object (setup ,"step" ,side_effect = SystemExit ("reached the plain setup" )): 803try : 804code ,out = quiet (setup .main ) 805except SystemExit as e : 806code ,out = e .code ,"" 807return code ,tuned 808 809def test_model_is_the_target (self ): 810code ,tuned = self .run_main ("--calibrate" ,"--model" ,"IQ3_XXS" ,"--yes" ,"--no-start" ) 811self .assertEqual ((code ,tuned ), (0 , ["strata-iq3_xxs.json" ])) 812 813def test_a_model_not_installed_is_an_error (self ): 814code ,tuned = self .run_main ("--calibrate" ,"--model" ,"IQ3_S" ,"--yes" ,"--no-start" ) 815self .assertEqual ((code ,tuned ), (1 , [])) 816 817def test_targets (self ): 818with tempfile .TemporaryDirectory ()as d : 819ps = [] 820for tag in ("iq3_xxs" ,"q2_0" ): 821p = Path (d )/ f"strata- { tag } .json" 822p .write_text (json .dumps ({"args" : []})) 823ps .append (p ) 824self .assertEqual (setup .calibration_targets (ps ,None ,None ),ps ) 825self .assertEqual (setup .calibration_targets (ps ,"Q2_0" ,None ), [ps [1 ]]) 826self .assertEqual (setup .calibration_targets (ps ,"Q2_0" ,"qwen" ), [ps [1 ]]) 827self .assertEqual (setup .calibration_targets (ps ,"Q4_K_M" ,None ), [])