okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
ddbfa1d5c
main
1"""Tests for setup.py's model menu (0.1.44): the family order, NVFP4 hidden on AMD and Intel, the NVFP4 line's card note 2(RTX 50 native FP4, older RTX slower than UD-IQ4_XS), the Unsloth sizes in order with images on, and the prebuilt 3install removing a stale strata_kq library. 4 5python -m unittest tools.test_setup_menu 6""" 7from __future__import annotations 8 9import re 10import sys 11import tempfile 12import unittest 13from unittest import mock 14from pathlib import Path 15 16ROOT = Path (__file__ ).resolve ().parents [1 ] 17sys .path .insert (0 ,str (ROOT )) 18sys .path .insert (0 ,str (ROOT / "tools" )) 19import setup # noqa: E402 20 21 22class FamilyOrder (unittest .TestCase ): 23def test_the_menu_order_on_nvidia (self ): 24fams = setup .menu_families (False )+ list (setup .OWN_PATH_FAMILIES ) 25self .assertEqual (fams , ["qwen" ,"swift" ,"coder" ,"unsloth" ,"gyro" ,"nvfp4" ,"qwen36" ]) 26 27def test_the_default_stays_the_first_family (self ): 28self .assertEqual (setup .menu_families (False )[0 ],"qwen" ) 29self .assertIn ("GSQ-RCO" ,setup .FAMILIES ["qwen" ]["by" ]) 30 31def test_nvfp4_is_not_listed_on_amd_and_intel (self ): 32fams = setup .menu_families (True ) 33self .assertNotIn ("nvfp4" ,fams ) 34self .assertEqual (fams , ["qwen" ,"swift" ,"coder" ,"unsloth" ,"gyro" ]) 35self .assertIn ("nvfp4" ,setup .FAMILIES )# --family nvfp4 still exists, and says why not 36 37def test_the_unsloth_line_names_the_alternative_on_amd_and_intel (self ): 38self .assertIn ("UD-IQ4_XS" ,setup .family_menu_note ("unsloth" ,True ,None )) 39self .assertEqual (setup .family_menu_note ("unsloth" ,False , {"arch" :86 }),"" ) 40self .assertEqual (setup .family_menu_note ("coder" ,True ,None ),"" ) 41 42 43class Nvfp4Note (unittest .TestCase ): 44def test_rtx_50_has_native_fp4 (self ): 45line = setup .family_menu_note ("nvfp4" ,False , {"arch" :120 ,"name" :"RTX 5070" }) 46self .assertIn ("native FP4 on this card" ,line ) 47 48def test_older_rtx_runs_but_is_slower_than_ud_iq4_xs (self ): 49for arch in (75 ,86 ,89 ): 50line = setup .family_menu_note ("nvfp4" ,False , {"arch" :arch }) 51self .assertIn ("runs, but UD-IQ4_XS is faster on this card" ,line ,arch ) 52self .assertNotIn ("native FP4" ,line ) 53 54def test_an_avx2_cpu_is_told_its_rows_but_not_refused (self ):# #2347 55line = setup .family_menu_note ("nvfp4" ,False , {"arch" :120 },avx2 = True ,avx512 = False ) 56self .assertIn ("no AVX-512" ,line ) 57self .assertIn ("AVX2" ,line ) 58self .assertIn ("native FP4 on this card" ,line )# the card part is unchanged 59self .assertTrue (setup .nvfp4_cpu_note (True ,False )) 60 61def test_an_avx512_cpu_and_a_cpu_without_avx2_get_no_cpu_note (self ): 62self .assertEqual (setup .nvfp4_cpu_note (True ,True ),"" ) 63self .assertEqual (setup .nvfp4_cpu_note (False ,False ),"" )# the older-CPU build says its own piece 64line512 = setup .family_menu_note ("nvfp4" ,False , {"arch" :120 }) 65self .assertNotIn ("AVX" ,line512 ) 66self .assertEqual (line512 ,setup .family_menu_note ("nvfp4" ,False , {"arch" :120 },avx2 = True ,avx512 = True )) 67 68def test_the_line_gives_the_download_and_the_ram_need (self ): 69line = setup .family_menu_note ("nvfp4" ,False , {"arch" :120 }) 70d = setup .MODELS ["NVFP4" ] 71self .assertIn (f" { d [ 'download_gb' ]:.0f } GB download" ,line ) 72self .assertIn (f"~ { d [ 'ram_gb' ] } GB of RAM" ,line ) 73 74 75class UnslothSizes (unittest .TestCase ): 76def test_the_sizes_in_menu_order (self ): 77names = [m for m in setup .MODELS if "unsloth" in setup .MODELS [m ].get ("families" , ())] 78self .assertEqual (names , ["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" , 79"UD-IQ3_XXS" ,"UD-Q3_K_XL" ,"UD-IQ4_XS-v31" ])# (round 0214 adds the v3.1 sets after the pinned five) 80 81def test_nothing_is_experimental_and_images_are_on (self ): 82for m in ("UD-IQ4_XS" ,"UD-Q4_K_XL" ,"UD-Q5_K_XL" ,"UD-Q6_K_XL" ,"Q8_0" ): 83d = setup .MODELS [m ] 84self .assertFalse (d .get ("experimental" ),m ) 85self .assertFalse (d .get ("nvidia_only" ),m ) 86self .assertIs (d .get ("vision" ),True ,m ) 87self .assertIn ("download_gb" ,d ) 88self .assertIn ("ram_gb" ,d ) 89self .assertNotIn ("experimental" ,setup .MODELS ["UD-Q4_K_XL" ]["about" ].lower ()) 90self .assertTrue (setup .FAMILIES ["unsloth" ]["vision" ]) 91 92def test_the_family_line_lists_every_size (self ): 93about = setup .FAMILIES ["unsloth" ]["about" ] 94for m in ("UD-IQ4_XS" ,"UD-Q4_K_XL" ,"UD-Q5_K_XL" ,"UD-Q6_K_XL" ,"Q8_0" ): 95self .assertIn (m ,about ) 96 97 98class RecommendedSizes (unittest .TestCase ): 99"""0.1.44 (owner): IQ3_S, UD-Q4_K_XL and UD-IQ4_XS are the recommended sizes; recommend, never force.""" 100 101def names (self ,family ): 102return [m for m in setup .MODELS if family in setup .MODELS [m ].get ("families" , ("qwen" ,"swift" ))] 103 104def test_qwen_and_swift_default_to_iq3_s_when_it_fits (self ): 105for fam in ("qwen" ,"swift" ): 106n = self .names (fam ) 107self .assertIn ("IQ3_S" ,n ) 108self .assertEqual (setup .recommended_size (fam ,n ,67.7 ),"IQ3_S" ) 109self .assertEqual (setup .recommended_size (fam ,n ,191.0 ),"IQ3_S" )# not the largest: IQ3_S stays the pick 110self .assertEqual ([m for m in n if setup .size_tag (fam ,m )], ["IQ3_S" ])# the others are choices, unmarked 111 112def test_without_the_ram_the_largest_that_fits_and_iq3_s_stays_marked (self ): 113n = self .names ("qwen" ) 114self .assertEqual (setup .recommended_size ("qwen" ,n ,61.0 ),"IQ3_XXS" )# IQ3_S needs 62 GB 115self .assertEqual (setup .recommended_size ("qwen" ,n ,52.0 ),"IQ3_XXS" )# 48 GB tier (RAM + VRAM - 5 GB) 116self .assertEqual (setup .recommended_size ("qwen" ,n ,40.0 ,11.0 ),"IQ3_XXS" )# 40 + 11 - 5 = 46 GB together 117self .assertEqual (setup .recommended_size ("qwen" ,n ,32.0 ,12.0 ),"IQ2_XS" )# 44 - 5 = 39 GB: the 40 GB tier 118self .assertEqual (setup .recommended_size ("swift" ,self .names ("swift" ),32.0 ,12.0 ),"IQ2_XS" ) 119self .assertEqual (setup .recommended_size ("qwen" ,n ,32.0 ,35.0 ),"IQ3_S" )# 67 - 5 = 62 GB together 120self .assertEqual (setup .size_tag ("qwen" ,"IQ3_S" )," (recommended)" ) 121 122def test_3425_the_tiers_leave_5_gb_for_windows (self ): 123# the reporter: RTX 5080 (16 GB) + 32 GB of RAM summed to 48 GB and got IQ3_XXS, which read ~11 GB from the SSD 124n = self .names ("qwen" ) 125self .assertEqual (setup .TIER_SYSTEM_RESERVE_GB ,5.0 ) 126self .assertEqual (setup .recommended_size ("qwen" ,n ,31.9 ,15.9 ),"IQ2_XS" ) 127self .assertEqual (setup .tier_pick (31.9 ,15.9 ), ("swift" ,"IQ2_XS" )) 128# boundaries: floors 38 / 46 / 62 plus the 5 GB 129self .assertEqual (setup .tier_pick (43.0 ), ("swift" ,"IQ2_XS" )) 130self .assertIsNone (setup .tier_pick (42.9 )) 131self .assertEqual (setup .tier_pick (51.0 ), ("qwen" ,"IQ3_XXS" )) 132self .assertEqual (setup .tier_pick (50.9 ), ("swift" ,"IQ2_XS" )) 133self .assertEqual (setup .tier_pick (67.0 ), ("qwen" ,"IQ3_S" )) 134self .assertEqual (setup .tier_pick (66.9 ), ("qwen" ,"IQ3_XXS" )) 135self .assertEqual (setup .tier_pick (40.0 ,3.0 ), ("swift" ,"IQ2_XS" ))# VRAM counts the same as RAM 136 137def test_coder_has_nothing_marked (self ): 138n = self .names ("coder" ) 139self .assertEqual ([setup .size_tag ("coder" ,m )for m in n ], ["" ]* len (n )) 140self .assertEqual (setup .recommended_size ("coder" ,n ,64.0 ),n [0 ]) 141 142 143class UnslothDefaultEnv (unittest .TestCase ): 144"""0.1.44 (owner): the env setup writes for Unsloth's files (review-quants.md section 5).""" 145 146def test_q5_and_q6_turn_the_k_quant_prompt_kernels_on_q4_does_not (self ): 147f = setup .unsloth_default_env 148self .assertEqual (f ("unsloth" ,"UD-Q5_K_XL" ,False ,False )["STRATA_MMQ_KQ" ],"1" ) 149self .assertEqual (f ("unsloth" ,"UD-Q6_K_XL" ,True ,True )["STRATA_MMQ_KQ" ],"1" ) 150for m in ("UD-Q4_K_XL" ,"UD-IQ4_XS" ,"Q8_0" ): 151self .assertNotIn ("STRATA_MMQ_KQ" ,f ("unsloth" ,m ,False ,False ),m ) 152 153def test_hc_q8_is_the_engines_own_default_not_setups (self ): 154f = setup .unsloth_default_env 155for m in ("UD-IQ4_XS" ,"UD-Q4_K_XL" ,"UD-Q5_K_XL" ,"UD-Q6_K_XL" ,"Q8_0" ): 156for hip in (False ,True ): 157self .assertNotIn ("STRATA_HC_Q8" ,f ("unsloth" ,m ,hip ,True ), (m ,hip )) 158 159def test_tail_skip_off_only_when_every_expert_is_in_ram (self ): 160f = setup .unsloth_default_env 161self .assertEqual (f ("unsloth" ,"UD-Q4_K_XL" ,False ,False )["STRATA_ROUTE_TAIL_SKIP" ],"0" ) 162self .assertNotIn ("STRATA_ROUTE_TAIL_SKIP" ,f ("unsloth" ,"UD-Q4_K_XL" ,False ,True ))# budget mode keeps the skip 163 164@ mock . patch . object ( setup , "WIN" , False ) # "Linux" gfx12: setup asks the host OS (setup.WIN), so the test names Linux itself 165def test_tail_skip_stays_on_for_linux_gfx12 (self ): 166# owner 11-03 ("3 Yes"): on Linux gfx12 (R9700, RX 9000) setup no longer turns the skip off for the UD files; the engine's own default (rank 7) runs 167f = setup .unsloth_default_env 168for arch in ("gfx1201" ,"gfx1200" ,"gfx1201:sramecc-:xnack-" ): 169for m in ("UD-IQ4_XS" ,"UD-Q4_K_XL" ,"UD-Q5_K_XL" ,"UD-Q6_K_XL" ): 170self .assertNotIn ("STRATA_ROUTE_TAIL_SKIP" ,f ("unsloth" ,m ,True ,False ,arch ), (arch ,m )) 171# every other card keeps the 0: other AMD (gfx1100, gfx1151), NVIDIA, no arch given (the old signature) 172for arch in ("gfx1100" ,"gfx1151" ,"gfx1030" ,"" ,"sm_120" ): 173self .assertEqual (f ("unsloth" ,"UD-Q4_K_XL" ,True ,False ,arch )["STRATA_ROUTE_TAIL_SKIP" ],"0" ,arch ) 174self .assertEqual (f ("unsloth" ,"UD-Q4_K_XL" ,False ,False ,"gfx1201" )["STRATA_ROUTE_TAIL_SKIP" ],"0" )# not hip 175self .assertEqual (f ("unsloth" ,"UD-Q4_K_XL" ,True ,False )["STRATA_ROUTE_TAIL_SKIP" ],"0" )# the old call 176# Windows gfx12 was not measured: it keeps the 0 177old = setup .WIN 178try : 179setup .WIN = True 180self .assertEqual (f ("unsloth" ,"UD-Q4_K_XL" ,True ,False ,"gfx1201" )["STRATA_ROUTE_TAIL_SKIP" ],"0" ) 181finally : 182setup .WIN = old 183 184def test_other_families_are_untouched (self ): 185for fam ,m in (("qwen" ,"IQ3_S" ), ("swift" ,"Q2_0" ), ("coder" ,"IQ1_M" ), ("nvfp4" ,"NVFP4" )): 186self .assertEqual (setup .unsloth_default_env (fam ,m ,False ,False ), {},fam ) 187 188 189class EnginePlaceholders (unittest .TestCase ): 190def test_every_engine_minimum_is_this_release (self ): 191self .assertEqual (setup .NVFP4_ENGINE , (0 ,1 ,44 )) 192self .assertEqual (setup .MIN_ENGINE , (0 ,1 ,46 )) 193for m in ("UD-Q5_K_XL" ,"UD-Q6_K_XL" ,"NVFP4" ,"NVFP4-NVIDIA" ): 194self .assertEqual (setup .MODELS [m ]["engine" ], (0 ,1 ,44 ),m ) 195# No minimum may name an engine newer than this tree (a placeholder for a release that does not exist yet would make setup 196# ask for an engine nobody can download). The tree's version is read from CMakeLists.txt, so this does not go stale each release. 197text = (ROOT / "CMakeLists.txt" ).read_text (encoding = "utf-8" ) 198tree = tuple (int (n )for n in re .search (r"project\(strata VERSION ([0-9.]+)" ,text ).group (1 ).split ("." )) 199for what ,minimum in [("MIN_ENGINE" ,setup .MIN_ENGINE ), ("NVFP4_ENGINE" ,setup .NVFP4_ENGINE )]+ [(m ,setup .MODELS [m ]["engine" ])for m in setup .MODELS if "engine" in setup .MODELS [m ]]: 200self .assertLessEqual (tuple (minimum ),tree [:len (minimum )],f" { what } { minimum } is ahead of the tree { tree } " ) 201 202 203class KqLibrary (unittest .TestCase ): 204def test_a_zip_without_strata_kq_does_not_leave_an_old_one (self ): 205with tempfile .TemporaryDirectory ()as d : 206eng ,tmp = Path (d )/ "engine" ,Path (d )/ "new" 207eng .mkdir () 208tmp .mkdir () 209 (eng / setup .EXE ).write_bytes (b"old" ) 210 (eng / setup .KQ_LIB ).write_bytes (b"old kq" ) 211 (tmp / setup .EXE ).write_bytes (b"new" ) 212setup .install_unpacked (tmp ,eng ) 213self .assertEqual ((eng / setup .EXE ).read_bytes (),b"new" ) 214self .assertFalse ((eng / setup .KQ_LIB ).exists ()) 215self .assertEqual ((eng / setup .PREVIOUS_ENGINE / setup .KQ_LIB ).read_bytes (),b"old kq" )# a rollback gets it back 216 217def test_a_zip_with_strata_kq_replaces_it (self ): 218with tempfile .TemporaryDirectory ()as d : 219eng ,tmp = Path (d )/ "engine" ,Path (d )/ "new" 220eng .mkdir () 221tmp .mkdir () 222 (eng / setup .KQ_LIB ).write_bytes (b"old kq" ) 223 (tmp / setup .EXE ).write_bytes (b"new" ) 224 (tmp / setup .KQ_LIB ).write_bytes (b"new kq" ) 225setup .install_unpacked (tmp ,eng ) 226self .assertEqual ((eng / setup .KQ_LIB ).read_bytes (),b"new kq" ) 227 228 229class ImageEncoderDefault (unittest .TestCase ): 230"""#2406: "images: yes" gets the CPU encoder on an NVIDIA card under 24 GB (the GPU encoder cost 3-5% of text speed on 23116 GB cards, 35-60% on an RTX 4080 SUPER under Windows); --vision gpu is kept with a note.""" 232 233def test_yes_follows_the_card (self ): 234f = setup .flash_vision_encoder 235self .assertEqual (f ("yes" , {"vram_gb" :12.0 }),"cpu" ) 236self .assertEqual (f ("yes" , {"vram_gb" :15.9 }),"cpu" ) 237self .assertEqual (f ("yes" , {"vram_gb" :24.0 }),"gpu" ) 238self .assertEqual (f ("yes" , {"vram_gb" :32.0 }),"gpu" ) 239self .assertEqual (f ("yes" , {}),"gpu" )# unknown VRAM: the old default 240self .assertEqual (f ("cpu" , {"vram_gb" :32.0 }),"cpu" ) 241self .assertEqual (f ("none" , {"vram_gb" :16.0 }),"none" ) 242 243def test_gpu_asked_is_kept_with_a_note (self ): 244import contextlib ,io 245out = io .StringIO () 246with contextlib .redirect_stdout (out ): 247self .assertEqual (setup .flash_vision_encoder ("gpu" , {"vram_gb" :16.0 }),"gpu" ) 248self .assertEqual (setup .flash_vision_encoder ("gpu" , {"vram_gb" :32.0 }),"gpu" ) 249self .assertEqual (out .getvalue ().count ("kept as you chose" ),1 ) 250self .assertIn ("#2406" ,out .getvalue ()) 251 252 253if __name__ == "__main__" : 254unittest .main ()