okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
3b8dc8fa3
main
1"""Older NVIDIA GPUs (experimental): Pascal / Volta run a second engine built with CUDA 12 (engine-cuda12/), chosen per 2model config by its oldest card; --cuda 12|13 overrides it. A PC whose cards the ready-made CUDA 13 engine runs on 3gets exactly what it got before. No GPU, no downloads. 4 5python -m unittest tools.test_setup_older_gpus 6""" 7from __future__import annotations 8 9import json 10import os 11import sys 12import tempfile 13import unittest 14from pathlib import Path 15from unittest import mock 16 17ROOT = Path (__file__ ).resolve ().parents [1 ] 18sys .path .insert (0 ,str (ROOT )) 19sys .path .insert (0 ,str (Path (__file__ ).resolve ().parent )) 20import setup # noqa: E402 21from test_setup_choices import quiet # noqa: E402 22from test_setup_golden import PROFILES ,install ,argv_for # noqa: E402 23 24 25def card (i ,name ,vram ,arch ,driver = "580.97" ): 26return {"index" :i ,"name" :name ,"vram_gb" :vram ,"arch" :arch ,"driver" :driver } 27 28 29V100 = card (0 ,"Tesla V100-PCIE-32GB" ,32.0 ,"70" ,"575.57" ) 30P40 = card (0 ,"Tesla P40" ,24.0 ,"61" ,"535.104" ) 31 32 33class Choice (unittest .TestCase ): 34def test_by_the_oldest_card (self ): 35self .assertEqual (setup .cuda_choice (["120" ]), (13 ,None )) 36self .assertEqual (setup .cuda_choice ([75 ,86 ,89 ]), (13 ,None )) 37for archs in ([70 ], [61 ], [60 ], [70 ,86 ]): 38tk ,why = setup .cuda_choice (archs ) 39self .assertEqual (tk ,12 ,archs ) 40self .assertIn (f"sm_ { min ( archs ) } is older than CUDA 13 supports" ,why ) 41 42def test_a_driver_too_old_for_cuda13_gets_the_cuda12_engine (self ):# #3843 43self .assertEqual (setup .cuda_choice ([86 ],None ,576 )[0 ],12 ) 44self .assertIn ("older than CUDA 13 needs" ,setup .cuda_choice ([86 ],None ,576 )[1 ]) 45self .assertEqual (setup .cuda_choice ([86 ],None ,580 ), (13 ,None )) 46self .assertEqual (setup .cuda_choice ([86 ],None ,0 ), (13 ,None ))# driver unknown: as before 47self .assertEqual (setup .cuda_choice ([86 ],None ,500 ), (13 ,None ))# too old for both: setup stops with the driver message 48self .assertEqual (setup .cuda_choice ([86 ],"13" ,576 )[0 ],13 )# chosen by hand: kept 49 50def test_the_override_is_honored_with_a_warning (self ): 51tk ,why = setup .cuda_choice ([70 ],"13" ) 52self .assertEqual (tk ,13 )# recommend, never force: kept, and said 53self .assertIn ("will not run on that card" ,why ) 54self .assertEqual (setup .cuda_choice ([86 ],"12" )[0 ],12 ) 55self .assertIn ("#220" ,setup .cuda_choice ([86 ,120 ],"12" )[1 ]) 56self .assertEqual (setup .cuda_choice ([70 ],"auto" )[0 ],12 ) 57self .assertEqual (setup .cuda_choice ([86 ],"auto" ), (13 ,None )) 58 59def test_engine_folders_and_configs (self ): 60with mock .patch .object (setup ,"ROOT" ,Path ("/s" )): 61self .assertEqual (setup .engine_dir (),Path ("/s/engine" )) 62self .assertEqual (setup .engine_dir (12 ),Path ("/s/engine-cuda12" )) 63self .assertEqual (setup .config_toolkit ({"exe" :"/s/engine/strata.exe" }),13 ) 64self .assertEqual (setup .config_toolkit ({"exe" :"/s/engine-cuda12/strata.exe" }),12 ) 65self .assertEqual (setup .config_toolkit ({"exe" :"x" ,"cuda" :12 }),12 ) 66self .assertEqual (setup .engine_defs ([86 ],12 ), ["-DSTRATA_EXPERIMENTAL_SM60=ON" ]) 67self .assertEqual (setup .engine_defs ([86 ]), []) 68 69 70class OptIn (unittest .TestCase ): 71"""Pascal / Volta cards are used only when chosen; a PC with a newer card keeps recommending the newer one.""" 72 73def setUp (self ): 74self .env = mock .patch .dict (os .environ , {"STRATA_EXPERIMENTAL_SM60" :"" }) 75self .env .start () 76 77def tearDown (self ): 78self .env .stop () 79 80def test_reasons (self ): 81rtx = card (0 ,"RTX 3090" ,24.0 ,"86" ) 82v100 = {** V100 ,"index" :1 } 83self .assertIsNone (setup .old_gpus_opt_in ([rtx ])) 84self .assertIsNone (setup .old_gpus_opt_in ([rtx ,v100 ]))# the default: unchanged 85self .assertIn ("you chose GPU 1" ,setup .old_gpus_opt_in ([rtx ,v100 ], [1 ])) 86self .assertIsNone (setup .old_gpus_opt_in ([rtx ,v100 ], [0 ])) 87self .assertEqual (setup .old_gpus_opt_in ([rtx ,v100 ],cuda = "12" ),"--cuda 12" ) 88self .assertIn ("only kind" ,setup .old_gpus_opt_in ([V100 ])) 89self .assertIsNone (setup .old_gpus_opt_in ([V100 ],other = True ))# an AMD card it can use instead 90self .assertIsNone (setup .old_gpus_opt_in ([card (0 ,"GTX 980" ,4.0 ,"52" )])) 91# #3218: a Maxwell card is never routed to the CUDA 12 engine (it holds no sm_52 code), with or without a switch 92m6000 = card (0 ,"Quadro M6000" ,24.0 ,"52" ) 93self .assertTrue (setup .sm60_card ("60" )and setup .sm60_card ("70" )and not setup .sm60_card ("75" )) 94self .assertFalse (setup .sm60_card ("52" )) 95self .assertIn ("not supported" ,setup .gpu_problem (m6000 )) 96self .assertIn ("Maxwell only runs from a source build" ,setup .gpu_problem (m6000 )) 97self .assertNotIn ("experimental: choose it" ,setup .gpu_problem (m6000 )) 98self .assertIsNone (setup .old_gpus_opt_in ([m6000 ])) 99for env in ({"STRATA_EXPERIMENTAL_SM60" :"1" }, {"STRATA_EXPERIMENTAL_SM52" :"1" }): 100with mock .patch .dict (os .environ ,env ): 101self .assertFalse (setup .sm60_card ("52" )) 102self .assertIn ("not supported" ,setup .gpu_problem (m6000 )) 103self .assertIsNone (setup .old_gpus_opt_in ([m6000 ])) 104self .assertIsNone (setup .old_gpus_opt_in ([m6000 ],named = [0 ],cuda = "12" ))# naming it does not admit it either 105with mock .patch .dict (os .environ , {"STRATA_EXPERIMENTAL_SM60" :"1" }): 106self .assertEqual (setup .old_gpus_opt_in ([rtx ,v100 ]),"STRATA_EXPERIMENTAL_SM60=1" ) 107 108def test_maxwell_note_is_a_warning_with_the_recipe (self ): 109m6000 = card (0 ,"Quadro M6000" ,24.0 ,"52" ) 110note = setup .maxwell_note ([m6000 , {** m6000 ,"index" :1 }]) 111self .assertIn ("GPU 0 (Quadro M6000), GPU 1 (Quadro M6000)" ,note ) 112self .assertIn ("-DSTRATA_EXPERIMENTAL_SM60=ON -DCMAKE_CUDA_ARCHITECTURES=52" ,note ) 113self .assertIn ("docs/OLDER_GPUS.md" ,note ) 114self .assertNotIn ("STRATA_EXPERIMENTAL_SM52" ,note )# no switch the user has to set 115self .assertEqual (setup .maxwell_note ([V100 ,P40 ]),"" ) 116self .assertEqual (setup .maxwell_note ([]),"" ) 117 118def test_named (self ): 119self .assertEqual (setup .named_gpus (None ,"0,2" ), [0 ,2 ]) 120self .assertEqual (setup .named_gpus (1 ,None ), [1 ]) 121self .assertEqual (setup .named_gpus (None ,"all" ), []) 122self .assertEqual (setup .named_gpus (None ,"x" ), []) 123 124def test_the_gate_follows_the_opt_in (self ): 125with mock .patch .object (setup ,"OLD_GPUS" ,None ): 126self .assertIsNotNone (setup .gpu_problem (V100 )) 127with mock .patch .object (setup ,"OLD_GPUS" ,"you chose GPU 0" ): 128self .assertIsNone (setup .gpu_problem (V100 )) 129self .assertIsNotNone (setup .gpu_problem (card (0 ,"GTX 980" ,4.0 ,"52" ))) 130 131 132class Libraries (unittest .TestCase ): 133def test_cuda12_wheels_two_folders (self ): 134with tempfile .TemporaryDirectory ()as d : 135sp = Path (d )/ "site-packages" 136names = {"cublas/bin/cublas64_12.dll" ,"cuda_runtime/bin/cudart64_12.dll" ,"cu13/bin/x86_64/cublas64_13.dll" , 137"cublas/lib/libcublas.so.12" ,"cuda_runtime/lib/libcudart.so.12" ,"cu13/lib/libcublas.so.13" } 138for n in names : 139f = sp / "nvidia" / n 140f .parent .mkdir (parents = True ,exist_ok = True ) 141f .write_bytes (b"" ) 142with mock .patch .object (sys ,"path" , [str (sp )]): 143for win in (True ,False ): 144with mock .patch .object (setup ,"WIN" ,win ): 145got12 = sorted (Path (x ).relative_to (sp / "nvidia" ).as_posix ()for x in setup .cuda_lib_dirs (12 )) 146got13 = [Path (x ).relative_to (sp / "nvidia" ).as_posix ()for x in setup .cuda_lib_dirs ()] 147if win : 148self .assertEqual (got12 , ["cublas/bin" ,"cuda_runtime/bin" ]) 149self .assertEqual (got13 , ["cu13/bin/x86_64" ]) 150else : 151self .assertEqual (got12 , ["cublas/lib" ,"cuda_runtime/lib" ]) 152self .assertEqual (got13 , ["cu13/lib" ]) 153 154def test_wheels_pinned_to_12 (self ): 155self .assertTrue (all ("-cu12==12." in w for w in setup .CUDA12_WHEELS )) 156self .assertEqual (setup .CUDA12_ASSET ,"strata-windows-x64-cuda12.zip" if setup .WIN else 157"strata-linux-x64-cuda12.zip" ) 158 159 160class BuildTools (unittest .TestCase ): 161"""The compile path: a CUDA 12 engine looks for a 12.x toolkit (STRATA_NVCC picks one), sm_120 needs 12.8+.""" 162 163def tools (self ,gpu ,nvcc ): 164seen = [] 165 166def find (below = None ): 167seen .append (below ) 168return nvcc (below ) 169 170with mock .patch .object (setup ,"find_nvcc" ,find ),mock .patch .object (setup ,"find_vcvars" ,lambda cuda_v = None :"vcvars" ), \ 171mock .patch .object (setup .shutil ,"which" ,lambda n :"/usr/bin/" + n ): 172got ,text = quiet (setup .install_build_tools ,gpu ,True ) 173return got ,seen ,text 174 175def test_cuda12_on_a_newer_card (self ): 176got ,seen ,_ = self .tools ({"arch" :"86" ,"archs" : [86 ],"toolkit" :12 }, 177lambda below : ("nvcc12" , (12 ,9 ))if below else ("nvcc13" , (13 ,0 ))) 178self .assertEqual ((got [0 ],seen ), ("nvcc12" , [(13 ,0 )])) 179 180def test_sm120_in_a_cuda12_engine_warns_and_needs_12_8 (self ): 181got ,_ ,text = self .tools ({"arch" :"70" ,"archs" : [70 ,120 ]}, 182lambda below : ("nvcc12" , (12 ,9 ))if below else ("nvcc13" , (13 ,0 ))) 183self .assertEqual (got [0 ],"nvcc12" ) 184self .assertIn ("#220" ,text ) 185with self .assertRaises (SystemExit ): 186self .tools ({"arch" :"70" ,"archs" : [70 ,120 ]},lambda below : ("nvcc12" , (12 ,6 ))if below else (None ,None )) 187 188 189def fake_prebuilt (calls ): 190"""get_prebuilt that installs an engine in the folder of the toolkit it is asked for (and says which).""" 191def get (url_base ,gpu ,vision ,updating = False ,toolkit = 13 ): 192calls .append (toolkit ) 193eng = setup .engine_dir (toolkit ) 194eng .mkdir (exist_ok = True ) 195 (eng / "BUILD.json" ).write_text (json .dumps ({"version" :"0.1.39" ,"source" :"release" ,"archs" : [86 ],"ptx" :True , 196"cuda" :"12.9" if toolkit == 12 else "13.0" ,"vision" :"gpu" })) 197 (eng / setup .EXE ).write_bytes (b"" ) 198 (eng / setup .VEXE ).write_bytes (b"" )# pictures are on by default: the engine carries the reader 199return eng 200return get 201 202 203class Install (unittest .TestCase ): 204"""setup.main() on mocked PCs (tools/test_setup_golden.py's harness).""" 205 206def run_setup (self ,ram ,found ,argv ,env = None ): 207calls ,libs = [], [] 208extra = [mock .patch .object (setup ,"get_prebuilt" ,fake_prebuilt (calls )), 209mock .patch .object (setup ,"pip_cuda_libs" ,lambda tk = 13 :libs .append (tk )), 210mock .patch .object (setup ,"OLD_GPUS" ,None ), 211mock .patch .object (setup ,"cuda12_min_driver" ,lambda feed = False : (575 ,"575.51" ,"the published figure" )),# never the real feed 212mock .patch .dict (os .environ , {"STRATA_EXPERIMENTAL_SM60" :"" ,"STRATA_CUDA" :"" ,** (env or {})})] 213code ,out ,cfg ,asked = install (ram ,found ,argv ,extra = extra ) 214return code ,out ,cfg ,calls ,libs 215 216def test_a_v100_alone_gets_the_cuda12_engine (self ): 217code ,out ,cfg ,calls ,libs = self .run_setup (95.0 , [V100 ],argv_for ("qwen" ,"IQ3_XXS" )) 218self .assertEqual (code ,0 ,out [- 3000 :]) 219self .assertEqual ((calls ,libs ), ([12 ], [12 ])) 220self .assertEqual (cfg ["cuda" ],12 ) 221self .assertEqual (cfg ["exe" ],"<T>/engine-cuda12/<EXE>" ) 222self .assertIn ("sm_70 is older than CUDA 13 supports" ,out ) 223self .assertIn ("experimental" ,out ) 224 225def test_a_maxwell_pc_is_warned_with_the_recipe_and_gets_no_engine (self ): 226"""#3218: no ready-made engine holds sm_52 code, so none is installed; the way forward is printed, with or 227without any switch (a user never has to set one).""" 228m6000 = card (0 ,"Quadro M6000" ,24.0 ,"52" ) 229for env in (None , {"STRATA_EXPERIMENTAL_SM60" :"1" }, {"STRATA_EXPERIMENTAL_SM52" :"1" }): 230with self .subTest (env = env ): 231code ,out ,cfg ,calls ,libs = self .run_setup (95.0 , [m6000 ],argv_for ("qwen" ,"IQ3_XXS" ),env ) 232self .assertNotEqual (code ,0 ,out [- 3000 :]) 233self .assertEqual ((calls ,libs ), ([], [])) 234self .assertIsNone (cfg ) 235self .assertIn ("Maxwell (compute capability 5.2)" ,out ) 236self .assertIn ("-DCMAKE_CUDA_ARCHITECTURES=52" ,out ) 237self .assertIn ("docs/OLDER_GPUS.md" ,out ) 238 239def test_a_maxwell_card_beside_a_newer_one_is_not_a_warning (self ): 240rtx = card (0 ,"NVIDIA GeForce RTX 3090" ,24.0 ,"86" ) 241code ,out ,cfg ,calls ,_ = self .run_setup (95.0 , [rtx ,card (1 ,"GTX 980" ,4.0 ,"52" )],argv_for ("qwen" ,"IQ3_XXS" )) 242self .assertEqual ((code ,calls ), (0 , [13 ]),out [- 3000 :]) 243self .assertNotIn ("Maxwell (compute capability" ,out ) 244 245def test_a_p40_with_an_older_driver (self ): 246code ,out ,cfg ,calls ,_ = self .run_setup (95.0 , [P40 ],argv_for ("qwen" ,"IQ3_XXS" )) 247self .assertEqual (code ,0 ,out [- 3000 :])# driver 535: enough for CUDA 12 248self .assertIn ("575.51" ,out )# #4247: said with the package's own figure, and as a warning, not a stop 249self .assertNotIn ("works with driver 528" ,out ) 250self .assertEqual (calls , [12 ]) 251 252def test_a_newer_card_beside_a_v100_is_unchanged (self ): 253"""The RTX 3090 is recommended as before; the V100 is listed with how to choose it.""" 254rtx = card (0 ,"NVIDIA GeForce RTX 3090" ,24.0 ,"86" ) 255found = [rtx , {** V100 ,"index" :1 }] 256code ,out ,cfg ,calls ,libs = self .run_setup (95.0 ,found ,argv_for ("qwen" ,"IQ3_XXS" )) 257code0 ,_ ,cfg0 ,_ = install (95.0 , [rtx ],argv_for ("qwen" ,"IQ3_XXS" )) 258self .assertEqual (code ,0 ,out [- 3000 :]) 259self .assertEqual ((calls ,libs ), ([13 ], [13 ])) 260self .assertNotIn ("cuda" ,cfg ) 261self .assertEqual (cfg ["exe" ],"<T>/engine/<EXE>" ) 262self .assertIn ("choose it with --gpu 1" ,out ) 263skip = ("gpu" ,"gpus_asked" ,"lib_dirs" )# one card vs two; the fake engine's library folders 264self .assertEqual ({k :v for k ,v in cfg .items ()if k not in skip }, {k :v for k ,v in cfg0 .items ()if k not in skip }) 265 266def test_naming_the_v100_beside_a_newer_card (self ): 267found = [card (0 ,"NVIDIA GeForce RTX 3090" ,24.0 ,"86" ), {** V100 ,"index" :1 }] 268code ,out ,cfg ,calls ,_ = self .run_setup (95.0 ,found ,argv_for ("qwen" ,"IQ3_XXS" )+ ["--gpu" ,"1" ]) 269self .assertEqual (code ,0 ,out [- 3000 :]) 270self .assertEqual (calls , [12 ]) 271self .assertEqual ((cfg ["cuda" ],cfg ["gpu" ]), (12 ,1 )) 272self .assertIn ("you chose GPU 1" ,out ) 273 274def test_cuda12_asked_for_on_a_newer_card (self ): 275ram ,found = PROFILES ["128GB-1x24GB" ] 276for argv ,env in ((["--cuda" ,"12" ],None ), ([], {"STRATA_CUDA" :"12" })): 277with self .subTest (argv = argv ,env = env ): 278code ,out ,cfg ,calls ,_ = self .run_setup (ram ,found ,argv_for ("qwen" ,"IQ3_XXS" )+ argv ,env ) 279self .assertEqual (code ,0 ,out [- 3000 :]) 280self .assertEqual ((calls ,cfg ["cuda" ]), ([12 ],12 )) 281self .assertIn ("--cuda 12 (as you chose" ,out ) 282 283def test_an_old_driver_gets_cuda12_by_itself (self ):# #3843: said in one line, not a stop 284code ,out ,cfg ,calls ,_ = self .run_setup (127.8 , [card (0 ,"NVIDIA GeForce RTX 4090" ,24.0 ,"89" ,"550.54" )], 285argv_for ("qwen" ,"IQ3_XXS" )) 286self .assertEqual (code ,0 ,out [- 3000 :]) 287self .assertEqual ((calls ,cfg ["cuda" ]), ([12 ],12 )) 288self .assertIn ("older than CUDA 13 needs (580): this model runs the CUDA 12 engine" ,out ) 289code ,out ,_ ,_ ,_ = self .run_setup (127.8 , [card (0 ,"NVIDIA GeForce RTX 4090" ,24.0 ,"89" ,"470.10" )], 290argv_for ("qwen" ,"IQ3_XXS" )) 291self .assertEqual (code ,1 )# too old for both: the stop stays 292self .assertIn ("is too old" ,out ) 293self .assertNotIn ("--cuda 12" ,out ) 294code ,out ,cfg ,calls ,_ = self .run_setup (127.8 , [card (0 ,"NVIDIA GeForce RTX 4090" ,24.0 ,"89" ,"550.54" )], 295argv_for ("qwen" ,"IQ3_XXS" )+ ["--cuda" ,"12" ]) 296self .assertEqual (code ,0 ,out [- 3000 :]) 297self .assertEqual (calls , [12 ]) 298 299def test_cuda13_forced_on_a_v100_is_said (self ): 300code ,out ,cfg ,calls ,_ = self .run_setup (95.0 , [V100 ],argv_for ("qwen" ,"IQ3_XXS" )+ ["--cuda" ,"13" ]) 301self .assertIn ("will not run on that card" ,out ) 302 303 304class Start (unittest .TestCase ): 305"""At a start: a Pascal / Volta card added to a CUDA 13 model moves that model to the CUDA 12 engine; updates 306look at both engine folders.""" 307 308def test_an_old_card_added_moves_the_model (self ): 309with tempfile .TemporaryDirectory ()as d : 310root = Path (d ) 311cfg_path = root / "strata-iq3_xxs.json" 312cfg = {"exe" :str (root / "engine" / setup .EXE ),"args" : [],"lib_dirs" : ["<cu13>" ]} 313calls = [] 314with mock .patch .object (setup ,"ROOT" ,root ), \ 315mock .patch .object (setup ,"get_prebuilt" ,fake_prebuilt (calls )), \ 316mock .patch .object (setup ,"pip_cuda_libs" ,lambda tk = 13 :None ), \ 317mock .patch .object (setup ,"cuda_lib_dirs" ,lambda tk = 13 : [f"<cu { tk } >" ]), \ 318mock .patch .object (setup ,"gpu_info" ,lambda i = None :None ): 319got ,text = quiet (setup .ensure_engine_for , [card (0 ,"RTX 3090" ,24.0 ,"86" ), {** V100 ,"index" :1 }], 320cfg_path ,dict (cfg ),True ) 321self .assertEqual (calls , [12 ]) 322self .assertEqual (Path (got ["exe" ]).parent .name ,"engine-cuda12" ) 323self .assertEqual ((got ["cuda" ],got ["lib_dirs" ]), (12 , ["<cu12>" ])) 324self .assertIn ("moves to the experimental CUDA 12 engine" ,text ) 325self .assertEqual (json .loads (cfg_path .read_text ())["cuda" ],12 ) 326 327def test_update_looks_at_both_engines (self ): 328with tempfile .TemporaryDirectory ()as d : 329root = Path (d ) 330seen = [] 331real = setup .update_installed_engine 332 333def spy (url_base ,toolkit = None ): 334if toolkit is None : 335return real (url_base ,toolkit ) 336seen .append (toolkit ) 337with mock .patch .object (setup ,"ROOT" ,root ),mock .patch .object (setup ,"update_installed_engine" ,spy ): 338spy ("u" ) 339self .assertEqual (seen , [13 ]) 340 (root / "engine-cuda12" ).mkdir () 341 (root / "engine-cuda12" / "BUILD.json" ).write_text ("{}" ) 342seen .clear () 343spy ("u" ) 344self .assertEqual (seen , [13 ,12 ]) 345 346 347if __name__ == "__main__" : 348unittest .main ()