okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
1f0ace2d1
main
1"""Setup's MTP download, the Gyro low-RAM pack and the FP4-Local conversion packages (#3193 #3214 #3257 #3227): the draft layer's download is run again when it stops, every tool 2setup runs ends with setup, and Gyro's low-RAM pack gets --compat-bf16 like its first pack. Everything mocked; nothing downloaded. 3 4python -m unittest tools.test_setup_dl_fix 5""" 6import os 7import pathlib 8import sys 9import unittest 10from unittest import mock 11 12ROOT = pathlib .Path (__file__ ).resolve ().parent .parent 13sys .path .insert (0 ,str (ROOT )) 14sys .path .insert (0 ,str (ROOT / "tools" )) 15import setup # noqa: E402 16import test_setup_unsloth as U # noqa: E402 17 18 19class FetchMtp (unittest .TestCase ): 20def run_with (self ,codes ,grow = True ): 21"""grow: each run writes a few bytes into the folder (a run that moved data); the network rule runs it again only then (lane NET).""" 22import tempfile 23calls = [] 24tmp = tempfile .TemporaryDirectory () 25self .addCleanup (tmp .cleanup ) 26mtp = pathlib .Path (tmp .name )/ "mtp" 27 28def fake_run (cmd ,cwd = None ,env = None ,check = True ,quiet = False ): 29calls .append ((cmd ,env ,check )) 30if grow : 31mtp .mkdir (exist_ok = True ) 32 (mtp / f"part { len ( calls ) } " ).write_bytes (b"x" ) 33return mock .Mock (returncode = codes [min (len (calls )- 1 ,len (codes )- 1 )]) 34with mock .patch .object (setup ,"run" ,fake_run ),mock .patch .object (setup .time ,"sleep" ,lambda s :None ), \ 35mock .patch .object (setup ,"say" ,lambda * a ,** k :None ),mock .patch .object (setup ,"warn" ,lambda * a ,** k :None ): 36try : 37setup .fetch_mtp (mtp , {"A" :"1" }) 38result = "ok" 39except SystemExit as e : 40result = e .code 41return result ,calls 42 43def test_a_run_that_moved_no_bytes_is_not_run_again (self ): 44out = [] 45with mock .patch .object (setup ,"fail" ,lambda msg ,hint = None :out .append ((msg ,hint ))or (_ for _ in ()).throw (SystemExit (1 ))): 46result ,calls = self .run_with ([1 ],grow = False ) 47self .assertEqual ((result ,len (calls )), (1 ,1 )) 48 49def test_a_server_that_does_not_answer_stops_at_once_with_the_fetchs_own_reason (self ): 50result ,calls = self .run_with ([4 ]) 51self .assertEqual ((result ,len (calls )), (1 ,1 )) 52 53def test_offline_setup_goes_on_without_the_draft_layer (self ): 54# 0.2.14 real-use run, R9700 offline: a first Auto launch stopped with exit 6 at the draft layer; offline is never a stop (the model starts without it) 55result ,calls = self .run_with ([5 ]) 56self .assertEqual ((result ,len (calls )), ("ok" ,1 )) 57 58def test_the_fetch_says_offline_with_its_own_exit_code (self ): 59import mtp_fetch 60from strata_control import netpolicy 61for kind ,code in (("offline" ,mtp_fetch .OFFLINE ), ("no_answer" ,mtp_fetch .NO_ANSWER )): 62with self .subTest (kind = kind ),mock .patch .object (sys ,"argv" , ["mtp_fetch.py" ,"inventory" ,"--out" ,"x" ]), \ 63mock .patch .object (mtp_fetch ,"exit_with_parent" ,lambda :None ), \ 64mock .patch .object (mtp_fetch ,"resolve_repo" ,mock .Mock (side_effect = netpolicy .NetFailure ("huggingface.co" ,kind ))), \ 65mock .patch ("sys.stderr" ,new = __import__ ("io" ).StringIO ()): 66with self .assertRaises (SystemExit )as got : 67mtp_fetch .main () 68self .assertEqual (got .exception .code ,code ) 69 70def test_a_stopped_download_is_run_again_and_continues (self ): 71result ,calls = self .run_with ([1 ,1 ,0 ]) 72self .assertEqual ((result ,len (calls )), ("ok" ,3 )) 73self .assertTrue (all (c [0 ][2 ]== "fetch" and c [1 ]["A" ]== "1" and c [2 ]is False for c in calls )) 74 75def test_a_download_that_never_works_ends_setup_with_a_hint (self ): 76out = [] 77with mock .patch .object (setup ,"fail" ,lambda msg ,hint = None :out .append ((msg ,hint ))or (_ for _ in ()).throw (SystemExit (1 ))): 78result ,calls = self .run_with ([1 ]) 79self .assertEqual ((result ,len (calls )), (1 ,3 )) 80self .assertIn ("modelscope" ,out [0 ][1 ]) 81 82def test_the_source_reaches_the_fetch (self ): 83with mock .patch .object (setup ,"model_source" ,lambda :"modelscope" ): 84_ ,calls = self .run_with ([0 ]) 85self .assertEqual (calls [0 ][1 ]["STRATA_SOURCE" ],"modelscope" ) 86 87 88class RunEndsWithSetup (unittest .TestCase ): 89def test_tools_are_told_to_end_when_setup_ends (self ): 90seen = {} 91 92def fake_subprocess_run (cmd ,** kw ): 93seen .update (kw ) 94return mock .Mock (returncode = 0 ) 95with mock .patch .object (setup .subprocess ,"run" ,fake_subprocess_run ),mock .patch .object (setup ,"say" ,lambda * a ,** k :None ), \ 96mock .patch .object (setup .acquisition ,"require" ,lambda * a ,** k :None ): 97setup .run ([sys .executable ,"x.py" ]) 98self .assertEqual (seen ["env" ]["STRATA_EXIT_WITH_PARENT" ],"1" ) 99self .assertIn ("PATH" ,seen ["env" ]if os .name != "nt" else {k .upper ():v for k ,v in seen ["env" ].items ()}) 100setup .run ([sys .executable ,"x.py" ],env = {"B" :"2" ,"STRATA_EXIT_WITH_PARENT" :"0" }) 101self .assertEqual (seen ["env" ]["STRATA_EXIT_WITH_PARENT" ],"0" ,"a value the caller set is kept" ) 102self .assertEqual (seen ["env" ]["B" ],"2" ) 103 104 105class GyroLowRam (U .Base ): 106def test_the_low_ram_pack_gets_compat_bf16_like_the_first_pack (self ): 107code ,out ,_ = self .main (["--family" ,"gyro" ,"--low-ram" ,"on" ,"--context" ,"8192" ],m = "Gyro-S" ,family = False ,ram = 31.9 ,version = "0.1.45.1" ) 108packs = [r for r in self .runs if any (x .endswith ("iq_pack.py" )for x in r )] 109self .assertTrue (packs , (code ,out [- 1500 :])) 110self .assertTrue (all ("--compat-bf16" in r for r in packs ),packs ) 111self .assertTrue (any ("--experts-bin" in r for r in packs ),"the low-RAM pack ran" ) 112 113 114class Fp4LocalPackages (unittest .TestCase ): 115"""#3227: torch, transformers and safetensors come from a pinned requirements file, right before a prepare that runs.""" 116 117def test_the_requirements_file_pins_what_the_converter_imports (self ): 118pkgs = setup .nvfp4_local_packages () 119self .assertEqual (sorted ({p .split ("==" )[0 ]for p in pkgs }),sorted (setup .NVFP4_LOCAL_MODULES )) 120self .assertTrue (all ("==" in p for p in pkgs ),pkgs ) 121 122def test_missing_packages_are_installed_once_with_the_pins (self ): 123calls = [] 124with mock .patch ("importlib.util.find_spec" ,lambda name :None ),mock .patch .object (setup ,"CONTROL" ,False ), \ 125mock .patch .object (setup ,"say" ,lambda * a ,** k :None ), \ 126mock .patch .object (setup ,"pip_install" ,lambda pkgs ,what ,extra = ():calls .append ((list (pkgs ),list (extra )))): 127setup .ensure_nvfp4_local_packages () 128self .assertEqual (len (calls ),1 ) 129self .assertEqual (calls [0 ][0 ],setup .nvfp4_local_packages ()) 130self .assertEqual (bool (calls [0 ][1 ]),not setup .WIN )# the CPU wheel index only on Linux 131 132def test_nothing_is_installed_when_they_are_there (self ): 133with mock .patch ("importlib.util.find_spec" ,lambda name :object ()), \ 134mock .patch .object (setup ,"pip_install" ,side_effect = AssertionError ("pip ran" )): 135setup .ensure_nvfp4_local_packages () 136 137def test_the_app_says_so_instead_of_installing (self ): 138with mock .patch ("importlib.util.find_spec" ,lambda name :None ),mock .patch .object (setup ,"CONTROL" ,True ), \ 139mock .patch .object (setup ,"say" ,lambda * a ,** k :None ), \ 140mock .patch .object (setup ,"pip_install" ,side_effect = AssertionError ("pip ran" )), \ 141self .assertRaises (SystemExit ): 142setup .ensure_nvfp4_local_packages () 143 144def test_the_tool_names_the_missing_package_when_run_by_hand (self ): 145import nvfp4_local_pack as P 146with mock .patch ("importlib.util.find_spec" ,lambda name :None if name == "transformers" else object ()): 147with self .assertRaises (SystemExit )as cm : 148P .require_packages () 149self .assertIn ("transformers" ,str (cm .exception )) 150self .assertIn ("requirements-nvfp4-local.txt" ,str (cm .exception )) 151self .assertNotIn ("torch," ,str (cm .exception )) 152 153 154class Fp4LocalOrder (unittest .TestCase ): 155def test_the_packages_come_before_the_prepare_run (self ): 156import test_setup_golden as G 157import test_setup_nvfp4 as N 158events = [] 159with mock .patch .object (setup ,"ensure_nvfp4_local_packages" ,lambda :events .append ("packages" )): 160ram ,found = G .PROFILES ["128GB-1x24GB" ] 161_ = G .install (ram ,found , ["--family" ,"nvfp4" ,"--no-start" ,"--model" ,"NVFP4-QAD" ]) 162self .assertEqual (events , ["packages" ]) 163 164 165if __name__ == "__main__" : 166unittest .main ()