okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
cd9745050
main
1"""Tests for setup.py's Unsloth choices (docs/UNSLOTH_Q4.md): UD-IQ4_XS (regular from 0.1.39) and the experimental 2UD-Q4_K_XL: the four pinned shards with 3their sizes and SHA-256, the RAM budget from the PC's RAM, the pack with --compat-bf16 (never experts.bin), the 4engine version it needs, one GPU, no images. Mocked - no GPU, no downloads, nothing written outside a temp folder. 5 6python -m unittest tools.test_setup_unsloth 7""" 8from __future__import annotations 9 10import contextlib 11import hashlib 12import io 13import json 14import os 15import shutil 16import sys 17import tempfile 18import types 19import unittest 20from pathlib import Path 21from unittest import mock 22 23ROOT = Path (__file__ ).resolve ().parents [1 ] 24sys .path .insert (0 ,str (ROOT )) 25import setup # noqa: E402 26 27M = "UD-Q4_K_XL" 28REV = "38bb39ee97821de2c9009abb7e93950eec396e66" 29 30 31def budget_mode (): 32"""The RAM-budget mode whatever the RAM (MODEL-UPDATE Phase 2 writes it only where the experts do not fit): for the 33tests of the budget's own paths on PCs that would now keep every expert in RAM (as in sergqwer's fork).""" 34return mock .patch .object (setup ,"unsloth_budget" ,lambda * a ,** k :True ) 35 36 37class Pins (unittest .TestCase ): 38def test_the_four_shards (self ): 39fam = setup .FAMILIES ["unsloth" ] 40names = [fam ["file" ].format (q = M ,i = i )for i in range (1 ,fam ["shards" ]+ 1 )] 41self .assertEqual (names ,list (setup .UNSLOTH_SHARDS )) 42self .assertEqual (sum (b for b ,_ in setup .UNSLOTH_SHARDS .values ()),111334654784 ) 43for _ ,sha in setup .UNSLOTH_SHARDS .values (): 44self .assertRegex (sha ,r"^[0-9a-f]{64}$" ) 45self .assertEqual (fam ["hf" ].format (q = M ), 46f"https://huggingface.co/unsloth/Qwen3.8-Flash-Next-GGUF/resolve/ { REV } /UD-Q4_K_XL/" ) 47self .assertEqual (setup .MODELS [M ]["families" ], ("unsloth" ,))# no other family lists it 48self .assertFalse (setup .MODELS [M ].get ("experimental" ))# MODEL-UPDATE Phase 2: official 49self .assertFalse (fam .get ("experimental" )) 50self .assertEqual (list (setup .FAMILIES )[0 ],"qwen" )# not the default choice 51 52def test_not_in_the_other_families (self ): 53for f in ("qwen" ,"swift" ,"coder" ): 54self .assertNotIn (M , [m for m in setup .MODELS if f in setup .MODELS [m ].get ("families" , ("qwen" ,"swift" ))]) 55 56 57class Budget (unittest .TestCase ): 58def test_ram_less_24 (self ): 59self .assertEqual (setup .resident_budget_gib (M ,63.7 ),40 )# this PC (64 GB): the measured setting 60self .assertEqual (setup .resident_budget_gib (M ,47.8 ),24 ) 61self .assertEqual (setup .resident_budget_gib (M ,96 ),71 )# at most all of the experts (77 GB = 71.7 GiB) 62self .assertEqual (setup .resident_budget_gib (M ,256 ),71 ) 63self .assertEqual (setup .resident_budget_gib (M ,20 ),8 )# the floor 64self .assertEqual (setup .resident_budget_gib (M ,63.7 ,1.7 ),38 )# a KV cache streamed to RAM comes out of it 65 66 67def quiet (fn ,* args ): 68"""-> (exit code or None, printed text).""" 69out = io .StringIO () 70code = None 71with contextlib .redirect_stdout (out ): 72try : 73code = fn (* args ) 74except SystemExit as e : 75code = e .code 76return code ,out .getvalue () 77 78 79class Sha256 (unittest .TestCase ): 80def setUp (self ): 81self .tmp = tempfile .TemporaryDirectory () 82self .f = Path (self .tmp .name )/ "x-00002-of-00004.gguf" 83self .f .write_bytes (b"strata" * 1000 ) 84self .sha = hashlib .sha256 (self .f .read_bytes ()).hexdigest () 85 86def tearDown (self ): 87self .tmp .cleanup () 88 89def test_good_file_marked_once (self ): 90code ,out = quiet (setup .verify_sha256 ,self .f ,6000 ,self .sha ) 91self .assertIsNone (code ,out ) 92self .assertIn (f"sha256 { self . sha } " ,self .f .with_name (self .f .name + ".done" ).read_text ()) 93with mock .patch ("hashlib.sha256" ,side_effect = AssertionError ("hashed again" )): 94code ,out = quiet (setup .verify_sha256 ,self .f ,6000 ,self .sha )# the mark says it was checked 95self .assertIsNone (code ,out ) 96 97def test_a_download_mark_alone_is_not_enough (self ): 98setup .mark (self .f )# the download's finish mark (a date) 99with mock .patch ("hashlib.sha256" ,wraps = hashlib .sha256 )as h : 100code ,out = quiet (setup .verify_sha256 ,self .f ,6000 ,self .sha ) 101self .assertIsNone (code ,out ) 102self .assertTrue (h .called ) 103 104def test_wrong_hash_keeps_it_as_bad (self ): 105setup .mark (self .f ) 106code ,out = quiet (setup .verify_sha256 ,self .f ,6000 ,"0" * 64 ) 107self .assertEqual (code ,1 ) 108self .assertIn ("does not match the pinned SHA-256" ,out ) 109self .assertFalse (self .f .exists ()) 110self .assertEqual (self .f .with_name (self .f .name + ".bad" ).read_bytes (),b"strata" * 1000 )# the bytes are kept 111self .assertFalse (self .f .with_name (self .f .name + ".done" ).exists ())# downloaded again next time 112 113def test_wrong_size (self ): 114code ,out = quiet (setup .verify_sha256 ,self .f ,6001 ,self .sha ) 115self .assertEqual (code ,1 ) 116self .assertIn ("6,000 bytes, not 6,001" ,out ) 117self .assertTrue (self .f .exists ()) 118 119 120class Config (unittest .TestCase ): 121def test_choices_from_config (self ): 122with tempfile .TemporaryDirectory ()as t : 123for name ,family ,model in (("strata-unsloth-ud-q4_k_xl.json" ,"unsloth" ,M ), 124 ("strata-swift-iq3_xxs.json" ,"swift" ,"IQ3_XXS" ), 125 ("strata-iq3_s.json" ,"qwen" ,"IQ3_S" ), 126 ("strata-coder-iq1_m.json" ,"coder" ,"IQ1_M" )): 127p = Path (t )/ name 128p .write_text (json .dumps ({"args" : ["--max-context" ,"8192" ]})) 129ch = setup .choices_from_config (p ) 130self .assertEqual ((ch ["family" ],ch ["model" ]), (family ,model ),name ) 131 132 133class FakeGGUF : 134"""gguf_reader.GGUFFile: shard 2 holds the PLE table.""" 135def __init__ (self ,path ): 136names = ["per_layer_token_embd.weight" ]if "00002-of" in str (path )else ["blk.0.ffn_up_exps.weight" ] 137self .tensors = [types .SimpleNamespace (name = n )for n in names ] 138 139 140class Base (unittest .TestCase ): 141"""setup.main() for the Unsloth choice, every outside effect mocked.""" 142 143def setUp (self ): 144self .tmp = tempfile .TemporaryDirectory () 145self .t = Path (self .tmp .name ) 146 (self .t / "data" / "mtp" / "rt" ).mkdir (parents = True ) 147 (self .t / "data" / "mtp" / "rt" / "experts.bin" ).write_bytes (b"" ) 148self .downloads ,self .runs ,self .verified = [], [], [] 149 150def tearDown (self ): 151self .tmp .cleanup () 152 153def main (self ,argv ,ram = 63.7 ,version = "0.1.32" ,n_gpus = 1 ,model = True ,amd = (),free = 500.0 ,m = M ,answers = None , 154family = True ,vram = 11.9 ): 155"""answers: None = --yes; else {words of a question: its answer} (Enter for the others), and every question 156asked is kept in self.asked. family=False: no --family (the first menu is shown).""" 157self .asked = [] 158 159def fake_input (prompt = "" ): 160if answers is None : 161raise AssertionError (f"asked { prompt !r } " ) 162self .asked .append (prompt ) 163return next ((v for k ,v in answers .items ()if k in prompt ),"" ) 164 165eng = self .t / "engine" 166eng .mkdir (exist_ok = True ) 167 (eng / "BUILD.json" ).write_text (json .dumps ({"version" :version ,"source" :"local" })) 168found = [{"index" :i ,"name" :"NVIDIA GeForce RTX 5070" ,"vram_gb" :vram ,"arch" :"120" ,"driver" :"580.97" } 169for i in range (n_gpus )] 170 171def fake_download (url ,dst ,what = None ): 172self .downloads .append (url ) 173dst .parent .mkdir (parents = True ,exist_ok = True ) 174dst .write_bytes (b"" ) 175setup .mark (dst ) 176 177def fake_run (cmd ,* a ,** k ): 178self .runs .append ([str (x )for x in cmd ]) 179 180patches = [ 181mock .patch .dict (os .environ , {"STRATA_FEED_MODELS" :"0" }),# the feed's model list is not read from the real feed (the test guard in conftest.py) 182mock .patch .object (setup ,"ROOT" ,self .t ), 183mock .patch .object (setup ,"GPU_PICK" ,None ), 184mock .patch .object (setup ,"data_folder" ,lambda d ,* a ,** k : (self .t / "data" , [])), 185mock .patch .object (setup ,"installed_configs" ,lambda : []), 186mock .patch .object (setup ,"gpus" ,lambda :found ), 187# the cards' speeds come from the CUDA driver of the machine that runs the test (setup.card_speeds), so the Main card was 188# whichever the real driver (or none) made it; the tests are written for card 0 as the faster one 189mock .patch .object (setup ,"card_speeds" ,lambda : {g ["index" ]:2.0 if g ["index" ]== 0 else 1.0 for g in found }), 190mock .patch .object (setup ,"amd_gpus" ,lambda * a :list (amd )), 191mock .patch .object (setup ,"ram_gb" ,lambda :ram ), 192mock .patch .object (setup ,"cpu_info" ,lambda : ("Test CPU" ,True ,True )), 193mock .patch .object (setup ,"cpu_cores" ,lambda :None ),# #642: not a hybrid CPU 194mock .patch .object (setup ,"page_file_gb" ,lambda :16.0 ), 195mock .patch .object (setup ,"free_gb" ,lambda p :free ), 196# whatever else asks the disk (shutil.disk_usage) gets the same answer: the tests never depend on this PC's free space 197mock .patch .object (shutil ,"disk_usage" ,lambda p :shutil ._ntuple_diskusage (int (max (free ,1000 )* 1e9 ),int (max (free ,1000 )* 1e9 - free * 1e9 ),int (free * 1e9 ))), 198mock .patch .object (setup ,"rotational_disk" ,lambda p :None ),# #605: not the test PC's disk 199mock .patch .object (setup ,"is_wsl" ,lambda :False ),# #974: the tests are not run inside WSL 200mock .patch .object (setup ,"pip_install" ,lambda * a ,** k :None ), 201mock .patch .object (setup ,"get_llama_cpp" ,lambda :self .t / "llama.cpp" ), 202mock .patch .object (setup ,"get_prebuilt" ,lambda * a ,** k :eng ), 203mock .patch .object (setup ,"download" ,fake_download ), 204mock .patch .object (setup ,"check_shards" ,lambda shards :None ), 205mock .patch .object (setup ,"verify_sha256" ,lambda s ,size ,sha ,own = False :self .verified .append ((s .name ,size ,sha ))), 206mock .patch .object (setup ,"run" ,fake_run ), 207mock .patch .object (setup ,"refresh_draft_vocab" ,lambda * a ,** k :None ), 208mock .patch .object (setup ,"write_run_script" ,lambda tag ,cfg ,port ,* _ :self .t / f"start- { tag } .bat" ), 209mock .patch .object (setup ,"saved_calibration" ,lambda cfg :None ), 210mock .patch .object (setup ,"start" ,mock .Mock (side_effect = AssertionError ("started" ))), 211mock .patch .dict (sys .modules , {"gguf_reader" :types .SimpleNamespace (GGUFFile = FakeGGUF )}), 212mock .patch .object (sys ,"argv" , ["setup.py" ,* (["--family" ,"unsloth" ]if family else []), 213* (["--model" ,m ]if model else []), 214* (["--yes" ]if answers is None else []),"--no-start" , 215"--models-dir" ,str (self .t / "models" ),* argv ]), 216mock .patch ("builtins.input" ,fake_input ), 217 ] 218with contextlib .ExitStack ()as st : 219for p in patches : 220st .enter_context (p ) 221code ,out = quiet (setup .main ) 222cfg = self .t / f"strata-unsloth- { m . lower () } .json" 223return code ,out , (json .loads (cfg .read_text ())if cfg .exists ()else None ) 224 225class Main (Base ): 226"""UD-Q4_K_XL.""" 227 228def test_install (self ): 229code ,out ,cfg = self .main (["--context" ,"8192" ]) 230self .assertEqual (code ,0 ,out ) 231self .assertNotIn ("EXPERIMENTAL" ,out ) 232base = f"https://huggingface.co/unsloth/Qwen3.8-Flash-Next-GGUF/resolve/ { REV } /UD-Q4_K_XL/" 233self .assertEqual (self .downloads [:- 1 ], [base + n for n in setup .UNSLOTH_SHARDS ])# then the image encoder: pictures are on by default 234self .assertIn ("mmproj" ,self .downloads [- 1 ]) 235self .assertEqual ([v [0 ]for v in self .verified ],list (setup .UNSLOTH_SHARDS )) 236self .assertEqual ([v [1 :]for v in self .verified ],list (setup .UNSLOTH_SHARDS .values ())) 237packs = [r for r in self .runs if r [1 ].endswith ("iq_pack.py" )] 238self .assertEqual (len (packs ),1 ,self .runs ) 239self .assertIn ("--compat-bf16" ,packs [0 ]) 240self .assertNotIn ("--experts-bin" ,packs [0 ]) 241self .assertTrue (packs [0 ][packs [0 ].index ("--gguf" )+ 1 ].endswith ("UD-Q4_K_XL-00001-of-00004.gguf" )) 242args = cfg ["args" ] 243self .assertEqual (args [args .index ("--resident-budget-gib" )+ 1 ],"40" ) 244self .assertTrue (args [args .index ("--native" )+ 1 ].endswith ("-00001-of-00004.gguf" )) 245for flag in ("--ple-gguf" ,"--mmap-experts" ,"--resident-experts" ,"--control-vector-scaled" , 246"--kv-resident" ): 247self .assertNotIn (flag ,args ) 248self .assertEqual (args [args .index ("--max-context" )+ 1 ],"8192" ) 249self .assertEqual (cfg ["model_name" ],"qwen3.8-flash-next-unsloth-ud-q4_k_xl" ) 250self .assertFalse (cfg ["vision" ]["gpu" ])# 11.9 GB: the CPU reader 251 252def test_recommended_context_on_12_gb (self ): 253code ,out ,cfg = self .main ([]) 254self .assertEqual (code ,0 ,out ) 255self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--max-context" )+ 1 ],"131072" )# 128K on every card (owner, 2026-11-01) 256 257@budget_mode() 258def test_more_ram_bigger_budget (self ): 259code ,out ,cfg = self .main (["--context" ,"8192" ],ram = 95.8 ) 260self .assertEqual (code ,0 ,out ) 261self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--resident-budget-gib" )+ 1 ],"71" ) 262 263def test_kv_streaming_comes_out_of_the_budget (self ): 264code ,out ,cfg = self .main (["--context" ,"131072" ]) 265self .assertEqual (code ,0 ,out ) 266args = cfg ["args" ] 267self .assertIn ("--kv-resident" ,args ) 268kv_gb = 131072 * 13 * 1056 / 1e9 269self .assertEqual (args [args .index ("--resident-budget-gib" )+ 1 ],str (40 - int (- (- kv_gb // 1 )))) 270 271def test_old_engine_stops_before_the_download (self ): 272code ,out ,cfg = self .main (["--context" ,"8192" ],version = "0.1.31" ) 273self .assertEqual (code ,1 ) 274self .assertIn ("needs engine 0.1.32 or newer; this one is 0.1.31" ,out ) 275self .assertEqual (self .downloads , []) 276self .assertIsNone (cfg ) 277 278def test_too_little_ram (self ): 279code ,out ,cfg = self .main (["--context" ,"8192" ],ram = 31.9 ,model = False )# --yes alone: still a stop 280self .assertEqual (code ,1 ) 281self .assertIn ("needs 48 GB of RAM or more" ,out ) 282self .assertIn ("--model UD-IQ4_XS --yes" ,out )# the way to insist (0.1.39: the family's default size) 283self .assertEqual (self .downloads , []) 284 285def test_amd_is_not_asked (self ): 286"""#429 is closed by MODEL-UPDATE Phase 2: UD-Q4_K_XL ran on an R9700 (quality yardstick, decode and prompt 287numbers in docs/QUANTS.md), so AMD is no longer asked before the download.""" 288r9700 = [{"index" :0 ,"name" :"AMD Radeon AI PRO R9700" ,"vram_gb" :31.9 ,"arch" :"gfx1201" , 289"driver" :"amdgpu" }] 290with mock .patch .object (setup ,"get_prebuilt_hip" ,lambda * a ,** k :self .t / "engine" ),mock .patch .object (setup ,"build_engine_hip" ,lambda * a ,** k :self .t / "engine" ): 291code ,out ,cfg = self .main (["--context" ,"8192" ,"--backend" ,"hip" ,"--vision" ,"no" ],amd = r9700 ) 292self .assertNotIn ("has not been run on AMD cards yet" ,out ) 293self .assertIn ("RAM budget: 40 GiB of UD-Q4_K_XL's experts" ,out ) 294 295def test_resumed_download_needs_room_for_the_rest_only (self ): 296"""#425: the disk check counts the finished shards and .part files already there (sizes scaled down: a 2972 MB "model" with 1.5 MB on the disk, 8 GB of other room needed, 8.001 GB free).""" 298d = self .t / "models" / "unsloth-UD-Q4_K_XL" 299d .mkdir (parents = True ) 300 (d / (list (setup .UNSLOTH_SHARDS )[0 ]+ ".part" )).write_bytes (b"x" * 1_500_000 ) 301with mock .patch .dict (setup .MODELS [M ], {"download_gb" :0.002 }): 302code ,out ,cfg = self .main (["--context" ,"8192" ],free = 8.001 ) 303self .assertNotIn ("not enough free disk space" ,out ) 304self .assertEqual (code ,0 ,out ) 305for f in d .iterdir ():# the first run "downloaded" them 306f .unlink () 307import shutil 308shutil .rmtree (self .t / "data" / "mtp" ,ignore_errors = True )# #897: a draft layer already there needs less room 309 (d / (list (setup .UNSLOTH_SHARDS )[0 ]+ ".part" )).write_bytes (b"x" * 1_500_000 ) 310with mock .patch .dict (setup .MODELS [M ], {"download_gb" :0.003 }):# 1.5 MB still missing, but the 8 GB working room is only a guess (0.2.2): setup warns and goes on 311code ,out ,cfg = self .main (["--context" ,"8192" ],free = 8.001 ) 312self .assertEqual (code ,0 ,out ) 313self .assertNotIn ("not enough free disk space" ,out ) 314self .assertIn ("little free disk space" ,out ) 315for f in d .iterdir (): 316f .unlink () 317shutil .rmtree (self .t / "data" / "mtp" ,ignore_errors = True ) 318 (d / (list (setup .UNSLOTH_SHARDS )[0 ]+ ".part" )).write_bytes (b"x" * 1_500_000 ) 319with mock .patch .dict (setup .MODELS [M ], {"download_gb" :3.0 }):# 1.5 GB of real files still to write, 1 GB free: refuse 320code ,out ,cfg = self .main (["--context" ,"8192" ],free = 1.0 ) 321self .assertEqual (code ,1 ) 322self .assertIn ("not enough free disk space" ,out ) 323 324def test_too_little_ram_with_an_explicit_model_installs (self ): 325"""S6 (the owner's rule): --model with --yes is the consent; the risk is said.""" 326code ,out ,cfg = self .main (["--context" ,"8192" ],ram = 31.9 ) 327self .assertEqual (code ,0 ,out ) 328self .assertIn ("needs 48 GB of RAM or more; this PC has 32 GB" ,out ) 329self .assertIn ("installing UD-Q4_K_XL with 32 GB of RAM, as you chose" ,out ) 330self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--resident-budget-gib" )+ 1 ],"8" ) 331 332def test_budget_flag (self ): 333"""S4: --resident-budget-gib N is kept; more than the recommendation says what it risks.""" 334code ,out ,cfg = self .main (["--context" ,"8192" ,"--resident-budget-gib" ,"48" ]) 335self .assertEqual (code ,0 ,out ) 336self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--resident-budget-gib" )+ 1 ],"48" ) 337self .assertIn ("more than setup recommends for this PC (40 GiB" ,out ) 338self .assertIn ("Kept as you chose" ,out ) 339code ,out ,cfg = self .main (["--context" ,"8192" ,"--resident-budget-gib" ,"32.5" ]) 340self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--resident-budget-gib" )+ 1 ],"32.5" ) 341self .assertNotIn ("more than setup recommends" ,out ) 342 343def test_budget_flag_with_kv_streaming_is_kept (self ): 344code ,out ,cfg = self .main (["--context" ,"131072" ,"--resident-budget-gib" ,"40" ]) 345self .assertEqual (code ,0 ,out ) 346args = cfg ["args" ] 347self .assertIn ("--kv-resident" ,args ) 348self .assertEqual (args [args .index ("--resident-budget-gib" )+ 1 ],"40" )# not 38: the user's number 349self .assertIn ("come on top of your 40 GiB RAM budget" ,out ) 350 351def test_budget_flag_needs_a_positive_number (self ): 352with contextlib .redirect_stderr (io .StringIO ()): 353code ,out ,cfg = self .main (["--context" ,"8192" ,"--resident-budget-gib" ,"0" ]) 354self .assertEqual (code ,2 )# argparse: N > 0 355self .assertIsNone (cfg ) 356 357def test_one_gpu_and_images_with_a_warning (self ): 358"""#967: images are allowed with UD-Q4_K_XL (tested end to end since MODEL-UPDATE).""" 359code ,out ,cfg = self .main (["--context" ,"8192" ,"--vision" ,"yes" ,"--low-ram" ,"on" ],n_gpus = 2 ) 360self .assertEqual (code ,0 ,out ) 361self .assertIn ("(NVIDIA GeForce RTX 5070, 12 GB) only (--gpus 1,0 shares it across" ,out ) 362self .assertNotIn ("images are not available" ,out ) 363self .assertIn ("images: on" ,out ) 364self .assertNotIn ("are untested" ,out )# MODEL-UPDATE: run end to end (CUDA, HIP) 365self .assertIn ("--low-ram on does not apply" ,out ) 366self .assertNotIn ("layer_split" ,cfg ) 367self .assertIn ("--vision" ,cfg ["args" ]) 368self .assertTrue (cfg ["vision" ]["model" ].endswith ("UD-Q4_K_XL-00001-of-00004.gguf" )) 369self .assertNotIn ("--mmap-experts" ,cfg ["args" ]) 370self .assertFalse (any ("--experts-bin" in r for r in self .runs )) 371 372@budget_mode() 373@ mock . patch . object ( setup , "MIN_ENGINE" , ( 0 , 1 , 39 )) # before RESIDENT_SPLIT_ENGINE: no budget on a split 374def test_split_when_the_ram_holds_it (self ): 375# #498: 165 GiB and --gpus 0,1: the split without the RAM budget (the engine refuses the pair) 376code ,out ,cfg = self .main (["--context" ,"8192" ,"--gpus" ,"0,1" ],n_gpus = 2 ,ram = 165.0 ) 377self .assertEqual (code ,0 ,out ) 378self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 379self .assertEqual (cfg ["layer_split" ],"auto" ) 380for flag in ("--resident-budget-gib" ,"--mmap-experts" ,"--resident-experts" ): 381self .assertNotIn (flag ,cfg ["args" ]) 382self .assertIn ("as you chose (--gpus): no RAM budget" ,out ) 383self .assertIn ("OS file cache" ,out ) 384self .assertNotIn ("RAM budget: " ,out ) 385 386@budget_mode() 387@ mock . patch . object ( setup , "MIN_ENGINE" , ( 0 , 1 , 39 )) # before RESIDENT_SPLIT_ENGINE: no budget on a split 388def test_split_without_the_ram_is_a_warning_not_a_wall (self ): 389# #737: below the worst-case estimate (a 128 GB PC ran it) the split is still the user's call 390need = setup .unsloth_split_need_gb () 391self .assertAlmostEqual (need ,setup .MODELS [M ]["download_gb" ]+ setup .UNSLOTH_RAM_LEFT_GB ) 392code ,out ,cfg = self .main (["--context" ,"8192" ],n_gpus = 2 ,ram = 127.8 )# no --gpus: stays on one GPU 393self .assertEqual (code ,0 ,out ) 394self .assertNotIn ("layer_split" ,cfg )# the model cannot share its RAM budget: one card (the Main one) 395self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--resident-budget-gib" )+ 1 ],"71" )# all of them 396code ,out ,cfg = self .main (["--context" ,"8192" ,"--gpus" ,"0,1" ],n_gpus = 2 ,ram = 127.8 )# asked for by name 397self .assertEqual (code ,0 ,out ) 398self .assertIn ("worst case" ,out ) 399self .assertIn ("as you chose (--gpus), with 128 GB of RAM" ,out ) 400self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 401self .assertNotIn ("--resident-budget-gib" ,cfg ["args" ]) 402 403@budget_mode() 404@ mock . patch . object ( setup , "MIN_ENGINE" , ( 0 , 1 , 39 )) # before RESIDENT_SPLIT_ENGINE: no budget on a split 405def test_an_explicit_budget_keeps_one_gpu (self ): 406code ,out ,cfg = self .main (["--context" ,"8192" ,"--gpus" ,"0,1" ,"--resident-budget-gib" ,"60" ],n_gpus = 2 , 407ram = 165.0 ) 408self .assertEqual (code ,0 ,out ) 409self .assertIn ("--resident-budget-gib has no layer split" ,out ) 410self .assertNotIn ("layer_split" ,cfg ) 411self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--resident-budget-gib" )+ 1 ],"60" ) 412 413def test_split_keeps_the_budget (self ): 414"""#642: with a resident-split engine the cards asked for keep the RAM budget, at any RAM.""" 415with mock .patch .object (setup ,"MIN_ENGINE" ,setup .RESIDENT_SPLIT_ENGINE ): 416code ,out ,cfg = self .main (["--context" ,"8192" ,"--gpus" ,"0,1" ],n_gpus = 2 )# 64 GB: no 135 GB asked 417self .assertEqual (code ,0 ,out ) 418self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 419self .assertEqual (cfg ["layer_split" ],"auto" ) 420self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--resident-budget-gib" )+ 1 ],"40" ) 421for flag in ("--mmap-experts" ,"--resident-experts" ): 422self .assertNotIn (flag ,cfg ["args" ]) 423self .assertIn ("as you chose (--gpus), with its RAM budget" ,out ) 424self .assertIn ("RAM budget: 40 GiB" ,out ) 425self .assertNotIn ("worst case" ,out ) 426code ,out ,cfg = self .main (["--context" ,"8192" ,"--gpus" ,"0,1" ,"--resident-budget-gib" ,"30" ],n_gpus = 2 ) 427self .assertEqual (code ,0 ,out )# an explicit budget too 428self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 429self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--resident-budget-gib" )+ 1 ],"30" ) 430self .assertNotIn ("has no layer split" ,out ) 431 432def test_asked_with_the_budget_one_gpu_by_default (self ): 433"""#642: asked as before (one GPU recommended); "2" takes both cards with the budget.""" 434with mock .patch .object (setup ,"MIN_ENGINE" ,setup .RESIDENT_SPLIT_ENGINE ): 435code ,out ,cfg = self .main (["--context" ,"8192" ],n_gpus = 2 ,answers = {})# Enter: one GPU 436self .assertEqual (code ,0 ,out ) 437self .assertTrue (any (f" { M } : which GPUs?" in q and "[1]" in q for q in self .asked ),self .asked ) 438self .assertNotIn ("layer_split" ,cfg ) 439self .assertIn ("--resident-budget-gib" ,cfg ["args" ]) 440code ,out ,cfg = self .main (["--context" ,"8192" ],n_gpus = 2 ,answers = {"which GPUs?" :"2" }) 441self .assertEqual (code ,0 ,out ) 442self .assertEqual (cfg ["gpu" ], [1 ,0 ]) 443self .assertIn ("--resident-budget-gib" ,cfg ["args" ]) 444 445@budget_mode() 446def test_yes_alone_keeps_one_gpu (self ): 447code ,out ,cfg = self .main (["--context" ,"8192" ],n_gpus = 2 ,ram = 165.0 ) 448self .assertEqual (code ,0 ,out ) 449self .assertIn ("Main: GPU 1" ,out )# the roles are shown; the model keeps to one card 450self .assertNotIn ("layer_split" ,cfg ) 451self .assertIn ("--resident-budget-gib" ,cfg ["args" ]) 452 453 454X = "UD-IQ4_XS" 455 456 457class IQ4XS (Base ): 458"""#621: Unsloth's UD-IQ4_XS, a regular (not experimental) choice from 0.1.39 - three pinned shards, the same 459RAM-budget setup as UD-Q4_K_XL (the pack with --compat-bf16, never experts.bin, one GPU), engine 0.1.38 or newer, 460no AMD question; images are asked like the 2-3-bit models' (off by default).""" 461 462def test_pins (self ): 463fam = setup .FAMILIES ["unsloth" ] 464names = [setup .model_file (fam ,X ,i )for i in range (1 ,setup .model_shards (fam ,X )+ 1 )] 465self .assertEqual (names ,list (setup .UNSLOTH_IQ4_XS_SHARDS )) 466self .assertEqual (sum (b for b ,_ in setup .UNSLOTH_IQ4_XS_SHARDS .values ()),93682584224 ) 467for n , (size ,sha )in setup .UNSLOTH_IQ4_XS_SHARDS .items (): 468self .assertRegex (sha ,r"^[0-9a-f]{64}$" ) 469self .assertEqual (fam ["sha256" ][n ], (size ,sha )) 470for n in setup .UNSLOTH_SHARDS :# UD-Q4_K_XL's are still there 471self .assertEqual (fam ["sha256" ][n ],setup .UNSLOTH_SHARDS [n ]) 472self .assertAlmostEqual (setup .MODELS [X ]["download_gb" ],93682584224 / 1e9 ,places = 1 ) 473self .assertEqual (setup .MODELS [X ]["families" ], ("unsloth" ,)) 474self .assertEqual ([m for m in setup .MODELS if "unsloth" in setup .MODELS [m ].get ("families" , ())], 475 [X ,M ,"UD-Q5_K_XL" ,"UD-Q6_K_XL" ,"Q8_0" ,"UD-IQ1_M" ,"UD-Q2_0" ,"UD-IQ2_XS" ,"UD-Q2_K_XL" , 476"UD-IQ3_XXS" ,"UD-Q3_K_XL" ,"UD-IQ4_XS-v31" ]) 477self .assertIsNone (setup .gguf_unsupported (names [0 ]))# #444: a file setup runs now 478self .assertEqual (setup .gguf_choice (names [0 ]), ("unsloth" ,X )) 479self .assertEqual (setup .budget_model ({"args" : ["--native" ,"/m/" + names [0 ]]}),X ) 480self .assertEqual (setup .budget_model ({"args" : ["--native" ,"/m/" + list (setup .UNSLOTH_SHARDS )[0 ]]}),M ) 481self .assertEqual (setup .budget_model ({"args" : []}),M ) 482 483def test_budget (self ): 484self .assertEqual (setup .resident_budget_gib (X ,63.7 ),40 ) 485self .assertEqual (setup .resident_budget_gib (X ,95.8 ),55 )# at most all of them (59.5 GB) 486self .assertAlmostEqual (setup .unsloth_split_need_gb (X ),93.7 + setup .UNSLOTH_RAM_LEFT_GB ) 487 488def test_install (self ): 489code ,out ,cfg = self .main (["--context" ,"8192" ],version = "0.1.38" ,m = X ) 490self .assertEqual (code ,0 ,out ) 491self .assertNotIn ("is EXPERIMENTAL" ,out )# (the menu still lists UD-Q4_K_XL) 492base = f"https://huggingface.co/unsloth/Qwen3.8-Flash-Next-GGUF/resolve/ { REV } /UD-IQ4_XS/" 493self .assertEqual (self .downloads [:- 1 ], [base + n for n in setup .UNSLOTH_IQ4_XS_SHARDS ])# then the image encoder (pictures on by default) 494self .assertIn ("mmproj" ,self .downloads [- 1 ]) 495self .assertEqual (self .verified , [(n ,* v )for n ,v in setup .UNSLOTH_IQ4_XS_SHARDS .items ()]) 496packs = [r for r in self .runs if r [1 ].endswith ("iq_pack.py" )] 497self .assertEqual (len (packs ),1 ,self .runs ) 498self .assertIn ("--compat-bf16" ,packs [0 ]) 499self .assertNotIn ("--experts-bin" ,packs [0 ]) 500self .assertTrue (packs [0 ][packs [0 ].index ("--gguf" )+ 1 ].endswith ("UD-IQ4_XS-00001-of-00003.gguf" )) 501args = cfg ["args" ] 502self .assertEqual (args [args .index ("--resident-budget-gib" )+ 1 ],"40" ) 503self .assertTrue (args [args .index ("--native" )+ 1 ].endswith ("UD-IQ4_XS-00001-of-00003.gguf" )) 504for flag in ("--ple-gguf" ,"--mmap-experts" ,"--resident-experts" ): 505self .assertNotIn (flag ,args ) 506self .assertIn ("--vision" ,args )# pictures are on by default 507self .assertEqual (cfg ["model_name" ],"qwen3.8-flash-next-unsloth-ud-iq4_xs" ) 508pack = self .t / "data" / "packs" / "unsloth-ud-iq4_xs" # its own pack and tokenizer 509self .assertEqual (packs [0 ][packs [0 ].index ("--out" )+ 1 ],str (pack )) 510self .assertEqual (args [args .index ("--pack" )+ 1 ],str (pack )) 511self .assertEqual (cfg ["tokenizer" ],str (pack / "tokenizer" )) 512self .assertFalse (cfg ["vision" ]["gpu" ])# pictures on by default, the CPU reader under 24 GB 513with tempfile .TemporaryDirectory ()as t :# a start reads the choice back 514p = Path (t )/ "strata-unsloth-ud-iq4_xs.json" 515p .write_text (json .dumps (cfg )) 516ch = setup .choices_from_config (p ) 517self .assertEqual ((ch ["family" ],ch ["model" ]), ("unsloth" ,X )) 518 519def test_family_default_and_menus (self ): 520"""The first menu lists the Unsloth family without [experimental]; its size menu lists the five files by size, 521UD-IQ4_XS first (the default on 64 GB, Enter), none marked EXPERIMENTAL; --model names are unchanged.""" 522code ,out ,cfg = self .main (["--context" ,"8192" ,"--vision" ,"no" ],version = "0.1.38" ,model = False , 523family = False ,answers = {"Which model" :"4" },m = M ) 524self .assertEqual (code ,0 ,out ) 525fam_line = next (ln for ln in out .splitlines ()if ln .startswith (" 4) " )) 526self .assertIn ("Qwen3.8-Flash-Next (Unsloth)" ,fam_line ) 527self .assertIn ("UD-IQ1_M, UD-Q2_0, UD-IQ2_XS, UD-Q2_K_XL, UD-IQ3_XXS, UD-Q3_K_XL, UD-IQ4_XS, UD-IQ4_XS-v31, UD-Q4_K_XL, UD-Q5_K_XL, UD-Q6_K_XL or Q8_0 (68 to 188 GB downloads)" ,fam_line ) 528self .assertNotIn ("[experimental]" ,out ) 529size_lines = [ln for ln in out .splitlines () 530if any (ln .startswith (f" { i } ) { m } " )for i in range (1 ,6 )for m in setup .MODELS )] 531self .assertEqual (len (size_lines ),5 ,out ) 532for i , (line ,m )in enumerate (zip (size_lines , (X ,M ,"UD-Q5_K_XL" ,"UD-Q6_K_XL" ,"Q8_0" )),1 ): 533self .assertTrue (line .startswith (f" { i } ) { m } " ),line ) 534self .assertNotIn ("EXPERIMENTAL" ,line ) 535self .assertTrue (any ("Which size" in q and "[2]" in q for q in self .asked ),self .asked ) 536self .assertEqual (cfg ["model_name" ],"qwen3.8-flash-next-unsloth-ud-q4_k_xl" ) 537code ,out ,cfg = self .main (["--context" ,"8192" ],version = "0.1.38" ,model = False ,m = M )# --yes: same default 538self .assertEqual (code ,0 ,out ) 539self .assertEqual (cfg ["model_name" ],"qwen3.8-flash-next-unsloth-ud-q4_k_xl" ) 540for m ,name in ((M ,"qwen3.8-flash-next-unsloth-ud-q4_k_xl" ), (X ,"qwen3.8-flash-next-unsloth-ud-iq4_xs" )): 541code ,out ,cfg = self .main (["--context" ,"8192" ],version = "0.1.38" ,m = m )# --model by name 542self .assertEqual ((code ,cfg ["model_name" ]), (0 ,name ),out ) 543 544def test_images (self ): 545"""Images on: the original model's image encoder (the same base model), the vision flags in the config.""" 546code ,out ,cfg = self .main (["--context" ,"8192" ,"--vision" ,"yes" ],version = "0.1.38" ,m = X ) 547self .assertEqual (code ,0 ,out ) 548self .assertNotIn ("images are not available" ,out ) 549self .assertIn ("images: on" ,out ) 550self .assertEqual (self .downloads [- 1 ],setup .hf ("ISTA-DASLab/Qwen3.8-Flash-Next-GSQ-RCO-GGUF" ) 551+ "mmproj-Qwen3.8-Flash-Next-BF16.gguf" ) 552self .assertIn ("--vision" ,cfg ["args" ]) 553self .assertTrue (cfg ["vision" ]["model" ].endswith ("UD-IQ4_XS-00001-of-00003.gguf" )) 554self .assertIn ("--resident-budget-gib" ,cfg ["args" ]) 555 556def test_check_verdict (self ): 557code ,out ,_ = self .main (["--check" ],m = X ) 558self .assertEqual (code ,0 ,out ) 559line = next (ln for ln in out .splitlines ()if ln .strip ().startswith (X )) 560self .assertRegex (line ,r"needs ~65 GB of RAM \+ VRAM: fits$" )# 93.7 GB download minus the 28.8 GB table = 64.9 GB, against 63.7 GB of RAM + 11.9 GB of VRAM 561self .assertNotIn ("EXPERIMENTAL" ,line ) 562self .assertNotIn ("EXPERIMENTAL" ,next (ln for ln in out .splitlines ()if ln .strip ().startswith (M ))) 563 564def test_check_below_every_floor (self ): 565# #977: 8 GB of RAM and a 12 GB card fit no size (the Coder's 24 GB of weights is the smallest); the RAM line is not [ok] and the verdict says so 566code ,out ,_ = self .main (["--check" ],ram = 8.0 ,m = X ) 567self .assertEqual (code ,1 ,out ) 568self .assertNotIn ("[ok] RAM" ,out ) 569self .assertIn ("[!] RAM: 8 GB (less than" ,out ) 570self .assertIn ("This PC cannot run Strata yet" ,out ) 571self .assertNotIn ("This PC can run Strata" ,out ) 572 573def test_engine_0138 (self ): 574code ,out ,cfg = self .main (["--context" ,"8192" ],version = "0.1.37" ,m = X ) 575self .assertEqual (code ,1 ) 576self .assertIn ("needs engine 0.1.38 or newer; this one is 0.1.37" ,out ) 577self .assertEqual (self .downloads , []) 578 579def test_amd_is_not_asked (self ): 580r9700 = [{"index" :0 ,"name" :"AMD Radeon AI PRO R9700" ,"vram_gb" :31.9 ,"arch" :"gfx1201" , 581"driver" :"amdgpu" }] 582with mock .patch .object (setup ,"get_prebuilt_hip" ,lambda * a ,** k :self .t / "engine" ), \ 583mock .patch .object (setup ,"build_engine_hip" ,lambda * a ,** k :self .t / "engine" ):# Linux compiles it 584code ,out ,cfg = self .main (["--context" ,"8192" ,"--backend" ,"hip" ],version = "0.1.38" ,amd = r9700 ,m = X ) 585self .assertNotIn ("has not been run on AMD cards yet" ,out ) 586self .assertIn ("RAM budget: 40 GiB of UD-IQ4_XS's experts" ,out ) 587 588 589class StrixHaloCache (Base ): 590"""#1715: on a unified-memory APU the expert cache and the RAM budget are the same RAM: setup writes a cache size 591that leaves the OS room, not `--expert-cache auto` (it OOM-killed a 128 GB Strix Halo with the desktop open).""" 592 593HALO = [{"index" :0 ,"name" :"AMD Radeon 8060S" ,"vram_gb" :114.0 ,"arch" :"gfx1151" ,"driver" :"amdgpu" , 594"uma" :True ,"dedicated_gb" :2.0 ,"shared_gb" :112.0 }] 595 596def run_halo (self ,ram ,argv = ("--context" ,"8192" ),amd = None ,m = "UD-IQ4_XS" ,version = "0.1.41" ): 597with mock .patch .object (setup ,"get_prebuilt_hip" ,lambda * a ,** k :self .t / "engine" ),mock .patch .object (setup ,"WIN" ,False ),mock .patch .object (setup ,"build_engine_hip" ,lambda * a ,** k :self .t / "engine" ): 598return self .main ([* argv ,"--backend" ,"hip" ],version = version ,amd = self .HALO if amd is None else amd , 599ram = ram ,m = m ) 600 601def test_cache_is_a_number_that_leaves_room (self ): 602code ,out ,cfg = self .run_halo (124.0 ) 603self .assertEqual (code ,0 ,out ) 604args = cfg ["args" ] 605self .assertEqual (args [args .index ("--resident-budget-gib" )+ 1 ],"55" ) 606slots = int (args [args .index ("--expert-cache" )+ 1 ]) 607# 124 - 24 (left) - 55 (budget) = 45 GiB of 1.1 x the average expert (59.5 GB / 24576) 608self .assertEqual (slots ,int (45 / (59.5e9 / setup .UMA_EXPERTS * 1.1 / 2 ** 30 ))) 609self .assertTrue (0 < slots < setup .UMA_EXPERTS ) 610self .assertIn ("expert cache: %d slots (not auto)" % slots ,out ) 611 612def test_never_more_than_all_the_experts (self ): 613self .assertEqual (setup .uma_expert_cache ("UD-IQ4_XS" ,512.0 ,55 ),setup .UMA_EXPERTS ) 614 615def test_no_room_keeps_auto_and_says_so (self ): 616self .assertIsNone (setup .uma_expert_cache ("UD-IQ4_XS" ,64.0 ,40 )) 617code ,out ,cfg = self .run_halo (64.0 , ("--context" ,"8192" ,"--resident-budget-gib" ,"40" )) 618self .assertEqual (code ,0 ,out ) 619args = cfg ["args" ] 620self .assertEqual (args [args .index ("--expert-cache" )+ 1 ],"auto" ) 621self .assertIn ("The expert cache is sized from the graphics memory (2 GB carve-out + shared), not from the RAM." ,out ) 622self .assertNotIn ("take the room the OS needs" ,out ) 623# the carve-out is the one setup detected, never a fixed 64 624halo48 = [dict (self .HALO [0 ],dedicated_gb = 48.0 ,shared_gb = 32.0 ,vram_gb = 80.0 )] 625code ,out ,cfg = self .run_halo (64.0 , ("--context" ,"8192" ,"--resident-budget-gib" ,"40" ),amd = halo48 ) 626self .assertEqual (code ,0 ,out ) 627self .assertIn ("(48 GB carve-out + shared)" ,out ) 628 629# Round 0214 (the release blocker): a 64 GB Strix Halo gave UD-IQ2_XS a fixed 367-slot cache and a 35 GiB pinned part, and its first launch failed 3 of 3; 630# `--expert-cache auto` with nothing pinned passed. A v3.1 set whose experts fit the shared pool whole gets that plan; every other set keeps its plan. 631HALO64 = [{"index" :0 ,"name" :"AMD Radeon 8060S" ,"vram_gb" :64.0 ,"arch" :"gfx1151" ,"driver" :"amdgpu" , 632"uma" :True ,"dedicated_gb" :32.0 ,"shared_gb" :32.0 }] 633 634def plan (self ,model ,ram ,amd = None ): 635code ,out ,cfg = self .run_halo (ram ,amd = amd or self .HALO64 ,m = model ,version = "0.1.57" ) 636self .assertEqual (code ,0 ,out ) 637a = cfg ["args" ] 638return a [a .index ("--expert-cache" )+ 1 ], (a [a .index ("--resident-budget-gib" )+ 1 ]if "--resident-budget-gib" in a else None ) 639 640def test_the_v31_sets_that_fit_the_pool_get_cache_auto_and_nothing_pinned (self ): 641for model in ("UD-IQ1_M" ,"UD-Q2_0" ,"UD-IQ2_XS" ): 642with self .subTest (model = model ): 643self .assertTrue (setup .uma_holds_experts (model ,63.7 ,self .HALO64 [0 ])) 644self .assertFalse (setup .unsloth_budget (model ,63.7 ,self .HALO64 [0 ])) 645self .assertEqual (self .plan (model ,63.7 ), ("auto" ,None )) 646 647def test_the_sets_too_big_for_the_pool_keep_the_budget_plan (self ): 648for model in ("UD-Q2_K_XL" ,"UD-IQ3_XXS" ,"UD-Q3_K_XL" ,"UD-IQ4_XS-v31" ): 649with self .subTest (model = model ): 650self .assertFalse (setup .uma_holds_experts (model ,63.7 ,self .HALO64 [0 ])) 651self .assertTrue (setup .unsloth_budget (model ,63.7 ,self .HALO64 [0 ])) 652cache ,budget = self .plan (model ,63.7 ) 653self .assertEqual (budget ,"40" )# as UD-IQ4_XS's: the RAM less 24 654 655def test_a_bigger_pool_holds_more_of_the_v31_sets (self ): 656big = [{** self .HALO [0 ],"vram_gb" :114.0 }] 657for model in ("UD-IQ3_XXS" ,"UD-Q3_K_XL" ,"UD-IQ4_XS-v31" ): 658with self .subTest (model = model ): 659self .assertEqual (self .plan (model ,124.0 ,amd = big ), ("auto" ,None )) 660 661def test_the_pinned_sets_keep_exactly_their_plans (self ): 662"""UD-IQ4_XS / UD-Q4_K_XL / UD-Q5_K_XL / UD-Q6_K_XL / Q8_0: not marked, so the rule never touches them, on Strix at 64 and 124 GB.""" 663for model in ("UD-IQ4_XS" ,"UD-Q4_K_XL" ,"UD-Q5_K_XL" ,"UD-Q6_K_XL" ,"Q8_0" ): 664self .assertNotIn ("uma_pool_auto" ,setup .MODELS [model ]) 665for ram ,halo in ((63.7 ,self .HALO64 [0 ]), (124.0 ,self .HALO [0 ])): 666self .assertFalse (setup .uma_holds_experts (model ,ram ,halo )) 667self .assertTrue (setup .unsloth_budget (model ,ram ,halo ), (model ,ram )) 668self .assertEqual (self .plan ("UD-IQ4_XS" ,63.7 ), ("auto" ,"40" ))# no room for a number: auto, with the budget (it passes on the box) 669code ,out ,cfg = self .run_halo (124.0 ) 670a = cfg ["args" ] 671self .assertEqual ((a [a .index ("--resident-budget-gib" )+ 1 ],a [a .index ("--expert-cache" )+ 1 ]), ("55" ,str (int (45 / (59.5e9 / setup .UMA_EXPERTS * 1.1 / 2 ** 30 ))))) 672 673def test_other_cards_and_a_person_s_budget_are_unchanged (self ): 674r9700 = {"vram_gb" :31.9 ,"uma" :False } 675nvidia = {"vram_gb" :12.0 } 676for gpu in (r9700 ,nvidia ,None ): 677self .assertFalse (setup .uma_holds_experts ("UD-IQ2_XS" ,63.7 ,gpu )) 678self .assertFalse (setup .unsloth_budget ("UD-IQ2_XS" ,63.7 ,gpu ),gpu )# (63.7 GB holds its 39 GiB of experts and 16 more: all in RAM, as before) 679self .assertTrue (setup .unsloth_budget ("UD-IQ2_XS" ,40.0 ,gpu ))# too little RAM: the budget, as before 680self .assertTrue (setup .unsloth_budget ("UD-IQ2_XS" ,63.7 ,self .HALO64 [0 ],asked = 24.0 ))# --resident-budget-gib N is still honoured 681asked = self .run_halo (63.7 , ("--context" ,"8192" ,"--resident-budget-gib" ,"24" ),amd = self .HALO64 ,m = "UD-IQ2_XS" ,version = "0.1.57" )[2 ]["args" ] 682self .assertIn ("--resident-budget-gib" ,asked )# the budget the person asked for is written (setup's own pool check may lower it, as before) 683 684def test_a_mac_is_never_touched (self ): 685with mock .patch .object (setup .sys ,"platform" ,"darwin" ): 686self .assertFalse (setup .uma_holds_experts ("UD-IQ2_XS" ,63.7 ,self .HALO64 [0 ])) 687self .assertTrue (setup .unsloth_budget ("UD-IQ2_XS" ,63.7 ,self .HALO64 [0 ]))# the answer it gave before round 0214 688 689def test_the_spark_profile (self ): 690"""A DGX Spark (GB10, unified memory, 128 GB): the old sets keep the budget plan, a v3.1 set that fits the pool has all of its experts in it.""" 691spark = {"vram_gb" :119.0 ,"uma" :True ,"arch" :"121" } 692for model in ("UD-IQ4_XS" ,"UD-Q4_K_XL" ,"UD-Q5_K_XL" ,"UD-Q6_K_XL" ,"Q8_0" ): 693self .assertTrue (setup .unsloth_budget (model ,121.0 ,spark ),model ) 694for model in ("UD-IQ1_M" ,"UD-IQ2_XS" ,"UD-IQ3_XXS" ,"UD-Q3_K_XL" ,"UD-IQ4_XS-v31" ): 695self .assertFalse (setup .unsloth_budget (model ,121.0 ,spark ),model ) 696 697def test_nvidia_plans_are_the_same_for_every_set (self ): 698for model in ("UD-IQ4_XS" ,"UD-Q4_K_XL" ,"UD-IQ1_M" ,"UD-IQ4_XS-v31" ): 699for ram in (47.0 ,63.7 ,124.0 ): 700want = ram < setup .MODELS [model ]["arena_gb" ]/ 1.073741824 + setup .UNSLOTH_RESIDENT_BESIDE_GB 701self .assertEqual (setup .unsloth_budget (model ,ram , {"vram_gb" :12.0 }),want , (model ,ram )) 702 703def test_a_discrete_card_keeps_auto (self ): 704r9700 = [{"index" :0 ,"name" :"AMD Radeon AI PRO R9700" ,"vram_gb" :31.9 ,"arch" :"gfx1201" , 705"driver" :"amdgpu" }] 706code ,out ,cfg = self .run_halo (124.0 ,amd = r9700 ) 707self .assertEqual (code ,0 ,out ) 708self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--expert-cache" )+ 1 ],"auto" ) 709 710 711class LayerSplit (unittest .TestCase ): 712"""#498: UD-Q4_K_XL across GPUs - asked at setup (one GPU by default), and a start with --gpus no longer keeps 713the RAM budget the engine refused with a split (it exited with code 2). #642: from RESIDENT_SPLIT_ENGINE the 714budget is kept on the split.""" 715 716def setUp (self ): 717# the behaviour before RESIDENT_SPLIT_ENGINE (an engine from before 0.1.40); the tests of the newer engine 718# patch MIN_ENGINE themselves 719p = mock .patch .object (setup ,"MIN_ENGINE" , (0 ,1 ,39 )) 720p .start () 721self .addCleanup (p .stop ) 722sys .path .insert (0 ,str (ROOT / "tools" )) 723from test_setup_golden import card 724self .found = [card (i ,"NVIDIA GeForce RTX 3090" ,24.0 ,"86" )for i in range (2 )] 725 726@budget_mode() 727def test_asked_one_gpu_by_default (self ): 728from test_setup_golden import install 729argv = ["--family" ,"unsloth" ,"--model" ,M ,"--context" ,"32768" ,"--no-start" ] 730code ,out ,cfg ,asked = install (165.0 ,self .found ,argv ,answers = {"which GPUs?" :"1" ,"Which GPUs?" :"1" }) 731self .assertEqual (code ,0 ,out ) 732self .assertTrue (any ("Use these roles? [y]" in q for q in asked ),asked )# the roles question; the model's own question follows 733self .assertEqual (cfg ["gpu" ],0 ) 734self .assertIn ("--resident-budget-gib" ,cfg ["args" ]) 735code ,out ,cfg ,asked = install (165.0 ,self .found ,argv ,answers = {f" { M } : which GPUs?" :"2" }) 736self .assertEqual (code ,0 ,out ) 737self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 738self .assertEqual (cfg ["layer_split" ],"auto" ) 739self .assertNotIn ("--resident-budget-gib" ,cfg ["args" ]) 740 741def start (self ,ram ,gpu = (0 ,1 ),offered = None ): 742from test_setup_risk import run 743with tempfile .TemporaryDirectory ()as d : 744exe = Path (d )/ "strata.exe" 745exe .write_bytes (b"" ) 746p = Path (d )/ "strata-unsloth-ud-q4_k_xl.json" 747before = {"exe" :str (exe ),"args" : ["--pack" ,"p" ,"--resident-budget-gib" ,"40" ,"--kv" ,"int8" ], 748"gpu" :0 ,"gpus_asked" :offered is None } 749p .write_text (json .dumps (before )) 750call = mock .Mock (return_value = 0 ) 751with mock .patch .object (setup ,"gpus" ,lambda :self .found ), \ 752mock .patch .object (setup ,"ram_gb" ,lambda :ram ), \ 753mock .patch .object (setup ,"engine_runs_on" ,lambda g :True ), \ 754mock .patch .object (setup ,"ensure_engine_for" ,lambda cards ,path ,cfg ,yes :cfg ), \ 755mock .patch .object (setup ,"is_wsl" ,lambda :False ), \ 756mock .patch .object (setup .subprocess ,"call" ,call ): 757code ,out ,asked = run (setup .start ,p ,None ,list (gpu )if gpu else None ,False ,offered is None , 758stdin = offered ) 759return code ,out ,json .loads (p .read_text ()),call .called 760 761def test_start_with_gpus_drops_the_budget (self ): 762code ,out ,cfg ,started = self .start (165.0 ) 763self .assertIsNone (code ,out ) 764self .assertTrue (started ) 765self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 766self .assertEqual (cfg ["args" ], ["--pack" ,"p" ,"--kv" ,"int8" ])# #1447: no helper cache 767self .assertIn ("no RAM budget" ,out ) 768 769def test_start_with_gpus_without_the_ram_is_asked (self ): 770# #737: a warning and a question (default no), not a wall; --gpus with --yes is the consent 771code ,out ,cfg ,started = self .start (63.7 ,offered = "n" )# asked: "n" stops, nothing saved 772self .assertEqual (code ,1 ) 773self .assertFalse (started ) 774self .assertIn ("cannot share its RAM budget across GPUs" ,out ) 775self .assertIn ("--gpu N" ,out ) 776self .assertEqual (cfg ["gpu" ],0 ) 777self .assertIn ("--resident-budget-gib" ,cfg ["args" ]) 778code ,out ,cfg ,started = self .start (63.7 )# --gpus + --yes: goes on, no budget 779self .assertIsNone (code ,out ) 780self .assertTrue (started ) 781self .assertIn ("cannot share its RAM budget across GPUs" ,out ) 782self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 783self .assertNotIn ("--resident-budget-gib" ,cfg ["args" ]) 784 785def test_offered_at_a_start (self ): 786code ,out ,cfg ,_ = self .start (165.0 ,gpu = None ,offered = "" )# Enter: one GPU (the default here) 787self .assertIsNone (code ,out ) 788self .assertEqual (cfg ["gpu" ],0 ) 789self .assertIn ("--resident-budget-gib" ,cfg ["args" ]) 790self .assertIn ("about twice as fast in #498" ,out ) 791code ,out ,cfg ,_ = self .start (165.0 ,gpu = None ,offered = "y" ) 792self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 793self .assertNotIn ("--resident-budget-gib" ,cfg ["args" ]) 794code ,out ,cfg ,_ = self .start (63.7 ,gpu = None ,offered = "y" )# not offered: the RAM does not hold it 795self .assertEqual (cfg ["gpu" ],0 ) 796self .assertNotIn ("Use both" ,out ) 797 798def test_resident_split_engine_keeps_the_budget (self ): 799"""#642: a start with --gpus keeps the budget at any RAM; offered both cards (Enter: one, as before).""" 800with mock .patch .object (setup ,"MIN_ENGINE" ,setup .RESIDENT_SPLIT_ENGINE ): 801for ram in (165.0 ,63.7 ): 802code ,out ,cfg ,started = self .start (ram ) 803self .assertIsNone (code ,out ) 804self .assertTrue (started ) 805self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 806self .assertEqual (cfg ["args" ], ["--pack" ,"p" ,"--resident-budget-gib" ,"40" ,"--kv" ,"int8" ]) 807self .assertNotIn ("no RAM budget" ,out ) 808self .assertNotIn ("cannot share its RAM budget" ,out ) 809code ,out ,cfg ,_ = self .start (63.7 ,gpu = None ,offered = "" )# offered at 64 GB too; Enter: one GPU 810self .assertIsNone (code ,out ) 811self .assertIn ("On both it keeps the budget" ,out ) 812self .assertIn ("staying on one GPU" ,out ) 813self .assertEqual (cfg ["gpu" ],0 ) 814code ,out ,cfg ,_ = self .start (63.7 ,gpu = None ,offered = "y" ) 815self .assertEqual (cfg ["gpu" ], [0 ,1 ]) 816self .assertIn ("--resident-budget-gib" ,cfg ["args" ]) 817 818 819Q5 ,Q6 ,Q8 = "UD-Q5_K_XL" ,"UD-Q6_K_XL" ,"Q8_0" 820 821 822class Resident (Base ): 823"""MODEL-UPDATE Phase 2 (sergqwer's fork, 0.1.42-nvfp4.2): every expert of an Unsloth file in RAM (a pinned arena, no 824--resident-budget-gib) where they fit with UNSLOTH_RESIDENT_BESIDE_GB more; the budget below that, when asked for, 825and on a unified-memory APU.""" 826 827def test_the_rule (self ): 828self .assertEqual (setup .UNSLOTH_RESIDENT_BESIDE_GB ,16 ) 829need = {m :setup .MODELS [m ]["arena_gb" ]/ 1.073741824 + 16 for m in (X ,M ,Q5 ,Q6 ,Q8 )} 830self .assertEqual ({m :round (v ,1 )for m ,v in need .items ()}, 831 {X :71.4 ,M :87.7 ,Q5 :107.6 ,Q6 :117.7 ,Q8 :135.5 }) 832for m in (X ,M ,Q5 ,Q6 ,Q8 ):# "ram_gb" of the bigger files is that need, so below it setup asks 833if m in (Q5 ,Q6 ,Q8 ): 834self .assertEqual (setup .MODELS [m ]["ram_gb" ],int (- (- need [m ]// 1 ))) 835for ram ,budget in ((63.7 , {X ,M ,Q5 ,Q6 ,Q8 }), (80.0 , {M ,Q5 ,Q6 ,Q8 }), (95.8 , {Q5 ,Q6 ,Q8 }), 836 (127.8 , {Q8 }), (191.0 ,set ())): 837for m in (X ,M ,Q5 ,Q6 ,Q8 ): 838self .assertEqual (setup .unsloth_budget (m ,ram ),m in budget , (m ,ram )) 839self .assertTrue (setup .unsloth_budget (X ,124.0 , {"uma" :True }))# #1715: measured on discrete cards only 840self .assertTrue (setup .unsloth_budget (M ,127.2 ,None ,40 ))# --resident-budget-gib: asked for 841self .assertFalse (setup .unsloth_budget (M ,127.2 , {"uma" :False })) 842 843def test_install_at_128_gb (self ): 844for m ,ver in ((M ,"0.1.32" ), (X ,"0.1.38" ), (Q5 ,"0.1.44" )): 845with self .subTest (m ): 846code ,out ,cfg = self .main (["--context" ,"131072" ],ram = 127.8 ,version = ver ,m = m ) 847self .assertEqual (code ,0 ,out ) 848args = cfg ["args" ] 849for flag in ("--resident-budget-gib" ,"--mmap-experts" ,"--resident-experts" ,"--low-ram" ): 850self .assertNotIn (flag ,args ) 851self .assertIn (f"of { m } 's experts in RAM (they and 16 GiB more fit" ,out ) 852self .assertNotIn ("RAM budget:" ,out ) 853self .assertEqual (args [args .index ("--max-context" )+ 1 ],"131072" ) 854 855def test_64_gb_keeps_the_budget (self ): 856for m ,ver in ((M ,"0.1.32" ), (X ,"0.1.38" )): 857with self .subTest (m ): 858code ,out ,cfg = self .main (["--context" ,"8192" ],ram = 63.7 ,version = ver ,m = m ) 859self .assertEqual (code ,0 ,out ) 860self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--resident-budget-gib" )+ 1 ],"40" ) 861self .assertIn (f"RAM budget: 40 GiB of { m } 's experts in RAM" ,out ) 862 863def test_an_asked_budget_is_kept (self ): 864code ,out ,cfg = self .main (["--context" ,"8192" ,"--resident-budget-gib" ,"40" ],ram = 127.8 ) 865self .assertEqual (code ,0 ,out ) 866self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--resident-budget-gib" )+ 1 ],"40" ) 867self .assertNotIn (f"of { M } 's experts in RAM (they and" ,out ) 868 869def test_the_menu_and_check_say_so (self ): 870code ,out ,_ = self .main (["--check" ],ram = 127.8 ) 871for m in (X ,M ,Q5 ,Q8 ):# 127.8 GB of RAM and a 12 GB card: the weights (60 to 129 GB, no lookup table) fit together 872line = next (ln for ln in out .splitlines ()if ln .strip ().startswith (m + " " )) 873self .assertTrue (line .endswith (": fits" ),line ) 874code ,out ,_ = self .main (["--check" ],ram = 100.0 ) 875line = next (ln for ln in out .splitlines ()if ln .strip ().startswith (Q8 + " " )) 876self .assertTrue (line .endswith (": may run slower" ),line )# 129 GB of weights against 100 GB + 12 GB 877 878def test_default_is_ud_q4_k_xl_where_it_fits (self ): 879"""0.1.44 (owner): UD-Q4_K_XL and UD-IQ4_XS are the recommended sizes; the default is UD-Q4_K_XL when the RAM fits 880it (any PC from 48 GB), else UD-IQ4_XS. A larger file stays a choice, never the default.""" 881for ram in (63.7 ,95.8 ,127.8 ,191.0 ): 882with self .subTest (ram ): 883code ,out ,cfg = self .main (["--context" ,"8192" ],ram = ram ,version = "0.1.44" ,model = False ,m = M ) 884self .assertEqual (code ,0 ,out ) 885self .assertEqual (cfg ["model_name" ],"qwen3.8-flash-next-unsloth-ud-q4_k_xl" ) 886 887def test_the_menu_marks_both_recommended_sizes (self ): 888names = [m for m in setup .MODELS if "unsloth" in setup .MODELS [m ].get ("families" , ())] 889self .assertEqual ([m for m in names if setup .size_tag ("unsloth" ,m )], ["UD-IQ4_XS" ,"UD-Q4_K_XL" ]) 890self .assertEqual (setup .recommended_size ("unsloth" ,names ,63.7 ),"UD-Q4_K_XL" ) 891self .assertEqual (setup .recommended_size ("unsloth" ,names ,191.0 ),"UD-Q4_K_XL" ) 892self .assertEqual (setup .recommended_size ("unsloth" ,names ,40.0 ),"UD-IQ4_XS" )# nothing fits: the 4-bit i-quant 893 894def test_two_gpus_split_with_every_expert_in_ram (self ): 895"""Where the experts fit, two cards are the GGUF models' layer split (no budget question, no #498 note).""" 896code ,out ,cfg = self .main (["--context" ,"8192" ],n_gpus = 2 ,ram = 127.8 ,answers = {"Use these roles?" :"y" })# the recommended roles 897self .assertEqual (code ,0 ,out ) 898self .assertEqual (cfg ["gpu" ], [1 ,0 ]) 899self .assertEqual (cfg ["layer_split" ],"auto" ) 900self .assertNotIn ("--resident-budget-gib" ,cfg ["args" ]) 901self .assertNotIn ("which GPUs?" ,out ) 902 903 904class BigFiles (Base ): 905"""MODEL-UPDATE: UD-Q5_K_XL, UD-Q6_K_XL and Q8_0 - six pinned shards each (sizes and SHA-256 from the Hub), the same 906GGUF-in-place setup as UD-Q4_K_XL (--compat-bf16 pack, every expert in RAM where it fits); below the all-in-RAM need 907setup asks first (default no) and says how much is read from the SSD.""" 908 909def test_pins (self ): 910fam = setup .FAMILIES ["unsloth" ] 911for m ,total in ((Q5 ,158286406650 ), (Q6 ,169165382688 ), (Q8 ,188225033248 )): 912names = [setup .model_file (fam ,m ,i )for i in range (1 ,setup .model_shards (fam ,m )+ 1 )] 913self .assertEqual (len (names ),6 ) 914self .assertEqual (sum (setup .UNSLOTH_BIG_SHARDS [n ][0 ]for n in names ),total ) 915for n in names : 916self .assertRegex (fam ["sha256" ][n ][1 ],r"^[0-9a-f]{64}$" ) 917self .assertIsNone (setup .gguf_unsupported (names [0 ])) 918self .assertEqual (setup .gguf_choice (names [0 ]), ("unsloth" ,m )) 919self .assertEqual (setup .budget_model ({"args" : ["--native" ,"/m/" + names [0 ]]}),m ) 920self .assertAlmostEqual (setup .MODELS [m ]["download_gb" ],total / 1e9 ,places = 1 ) 921for i in (2 ,3 ):# the head and the PLE table: the same bytes in all three 922self .assertEqual (len ({setup .UNSLOTH_BIG_SHARDS [setup .model_file (fam ,m ,i )]for m in (Q5 ,Q6 ,Q8 )}),1 ) 923 924def test_install_q8_at_192_gb (self ): 925code ,out ,cfg = self .main (["--context" ,"32768" ],ram = 191.0 ,version = "0.1.44" ,m = Q8 ) 926self .assertEqual (code ,0 ,out ) 927base = f"https://huggingface.co/unsloth/Qwen3.8-Flash-Next-GGUF/resolve/ { REV } /Q8_0/" 928names = [setup .model_file (setup .FAMILIES ["unsloth" ],Q8 ,i )for i in range (1 ,7 )] 929self .assertEqual (self .downloads [:- 1 ], [base + n for n in names ])# then the image encoder (pictures on by default) 930self .assertIn ("mmproj" ,self .downloads [- 1 ]) 931self .assertEqual ([v [0 ]for v in self .verified ][:len (names )],names ) 932packs = [r for r in self .runs if r [1 ].endswith ("iq_pack.py" )] 933self .assertIn ("--compat-bf16" ,packs [0 ]) 934args = cfg ["args" ] 935self .assertNotIn ("--resident-budget-gib" ,args ) 936self .assertTrue (args [args .index ("--native" )+ 1 ].endswith ("Q8_0-00001-of-00006.gguf" )) 937self .assertEqual (cfg ["model_name" ],"qwen3.8-flash-next-unsloth-q8_0" ) 938 939def test_q8_below_its_ram_is_asked (self ): 940# (127.8 GB of RAM and a 12 GB card hold its 129 GB of weights together: "fits", nothing is asked; 100 GB and the card do not) 941code ,out ,cfg = self .main (["--context" ,"8192" ],ram = 100.0 ,version = "0.1.44" ,m = Q8 , 942answers = {"Install it anyway" :"n" }) 943self .assertEqual (code ,1 ,out ) 944self .assertIn ("Q8_0 needs 136 GB of RAM or more; this PC has 100 GB" ,out ) 945self .assertEqual (self .downloads , []) 946code ,out ,cfg = self .main (["--context" ,"8192" ],ram = 100.0 ,version = "0.1.44" ,m = Q8 )# --model + --yes 947self .assertEqual (code ,0 ,out ) 948self .assertEqual (cfg ["args" ][cfg ["args" ].index ("--resident-budget-gib" )+ 1 ],"76" ) 949self .tmp .cleanup () 950self .setUp () 951code ,out ,cfg = self .main (["--context" ,"8192" ],ram = 127.8 ,version = "0.1.44" ,m = Q8 ) 952self .assertEqual (code ,0 ,out ) 953self .assertNotIn ("Q8_0 needs 136 GB of RAM or more" ,out ) 954 955def test_q5_q6_need_the_q6k_engine (self ): 956for m in (Q5 ,Q6 ): 957code ,out ,cfg = self .main (["--context" ,"8192" ],ram = 191.0 ,version = "0.1.43" ,m = m ) 958self .assertEqual (code ,1 ) 959self .assertIn (f" { m } needs engine 0.1.44 or newer; this one is 0.1.43" ,out ) 960self .assertEqual (self .downloads , []) 961 962 963if __name__ == "__main__" : 964unittest .main ()