okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
b749503db
main
1"""Tests for image generation's setup side (tools/image/qimage.py, setup.py --images): the pins, the quant and backend 2choices, the CMake definitions, the source unpacking and the run config's "images" section. No GPU, no network. 3 4python -m unittest tools.test_qimage 5""" 6from __future__import annotations 7 8import argparse 9import contextlib 10import io 11import json 12import re 13import sys 14import tempfile 15import unittest 16import zipfile 17from pathlib import Path 18from unittest import mock 19 20ROOT = Path (__file__ ).resolve ().parents [1 ] 21sys .path .insert (0 ,str (ROOT )) 22sys .path .insert (0 ,str (ROOT / "tools" )) 23sys .path .insert (0 ,str (ROOT / "tools" / "image" )) 24import qimage as QI # noqa: E402 25import setup # noqa: E402 26 27 28def quiet (fn ,* args ,** kw ): 29with contextlib .redirect_stdout (io .StringIO ()): 30return fn (* args ,** kw ) 31 32 33class Pins (unittest .TestCase ): 34def test_shapes (self ): 35for c in (QI .SD_CPP_COMMIT ,QI .SD_GGML_COMMIT ,* QI .REVISIONS .values ()): 36self .assertRegex (c ,r"^[0-9a-f]{40}$" ) 37for name , (repo ,path ,size ,sha )in QI .FILES .items (): 38self .assertIn (repo ,QI .REVISIONS ,name ) 39self .assertRegex (sha ,r"^[0-9a-f]{64}$" ) 40self .assertGreater (size ,100 << 20 ) 41self .assertEqual (QI .file_url ("Q8_0" ),"https://huggingface.co/unsloth/Qwen-Image-2.1-Turbo-GGUF/resolve/" 42"5d1f9f759ea877ae556cda88dbec958b62ad3809/qwen-image-2.1-turbo-Q8_0.gguf" ) 43self .assertEqual (QI .local_path (Path ("m" ),"vae" ), 44Path ("m" )/ "qwen-image-2.1-turbo" / "qwen_image_2.1_vae_bf16.safetensors" ) 45 46def test_sigmas_are_the_models (self ): 47"""serve/images.py's schedule is model_index.json's sample_sigmas plus the terminal 0.""" 48from serve import images 49sample = [1.0 ,0.978453 ,0.95418 ,0.926626 ,0.89508 ,0.845148 ,0.704534 ,0.414568 ] 50self .assertEqual ([float (x )for x in images .TURBO_SIGMAS .split ("," )],sample + [0.0 ]) 51 52 53class Choices (unittest .TestCase ): 54def test_quant_by_memory (self ): 55self .assertEqual ([QI .pick_quant (g )for g in (4 ,8 ,8.5 ,12 ,12.1 ,16 ,32 )], 56 ["Q4_K_M" ,"Q4_K_M" ,"Q6_K" ,"Q6_K" ,"Q8_0" ,"Q8_0" ,"Q8_0" ]) 57 58def test_backend (self ): 59self .assertEqual (QI .pick_backend (None ,False ),"cpu" ) 60self .assertEqual (QI .pick_backend ({"vendor" :"nvidia" },True ),"cuda" ) 61self .assertEqual (QI .pick_backend ({"vendor" :"amd" },False ),"hip" ) 62self .assertEqual (QI .pick_backend ({"vendor" :"amd" ,"uma" :True },False ),"vulkan" )# the 780M, Strix Halo 63self .assertEqual (QI .pick_backend ({"vendor" :"amd" },True ),"vulkan" )# AMD on Windows 64self .assertEqual (QI .pick_backend ({"vendor" :"intel" },False ),"vulkan" ) 65self .assertEqual (QI .pick_backend ({"vendor" :"amd" ,"arch" :"gfx1103" },False ),"vulkan" )# no uma mark 66self .assertEqual (QI .pick_backend ({"vendor" :"amd" ,"arch" :"gfx1100" },False ),"hip" ) 67self .assertTrue (QI .is_apu ({"arch" :"gfx1151:xnack-" })) 68 69def test_default_args (self ): 70self .assertEqual (QI .default_args ("cuda" ), ["--diffusion-fa" ]) 71self .assertEqual (QI .default_args ("vulkan" ), ["--diffusion-fa" ,"--vae-tiling" ,"--vae-tile-overlap" ,"0.25" ]) 72 73def test_setup_recommends_never_forces (self ): 74ns = argparse .Namespace (image_backend = None ,image_quant = None ) 75self .assertEqual (quiet (setup .image_backend_choice ,ns , {"vram_gb" :12.0 ,"arch" :86 },False )[:2 ], 76 ("cuda" ,"Q6_K" )) 77self .assertEqual (quiet (setup .image_backend_choice ,ns , {"vram_gb" :32.3 ,"uma" :True ,"arch" :"gfx1103" }, 78True )[:2 ], ("vulkan" ,"Q8_0" )) 79ns .image_quant = "Q8_0" # more than recommended: kept, with a warning 80out = io .StringIO () 81with contextlib .redirect_stdout (out ): 82r = setup .image_backend_choice (ns , {"vram_gb" :8.0 ,"arch" :86 },False ) 83self .assertEqual (r [:2 ], ("cuda" ,"Q8_0" )) 84self .assertIn ("recommends Q4_K_M" ,out .getvalue ()) 85ns .image_quant ,ns .image_backend = None ,"cpu" 86self .assertEqual (quiet (setup .image_backend_choice ,ns , {"vram_gb" :24.0 },False )[:2 ], ("cpu" ,"Q4_K_M" )) 87 88 89class Build (unittest .TestCase ): 90def test_cmake_defs (self ): 91d = QI .cmake_defs ("cuda" ,arch = 86 ,cuda_root = Path ("/usr/local/cuda" )) 92self .assertIn ("-DSD_CUDA=ON" ,d ) 93self .assertIn ("-DCMAKE_CUDA_ARCHITECTURES=86" ,d ) 94self .assertIn ("-DSD_WEBP=OFF" ,d ) 95self .assertIn ("-DSD_VULKAN=ON" ,QI .cmake_defs ("vulkan" )) 96with tempfile .TemporaryDirectory ()as r : 97rocm = Path (r ) 98 (rocm / "lib" / "llvm" / "bin" ).mkdir (parents = True ) 99d = QI .cmake_defs ("hip" ,arch = "gfx1103:sramecc-:xnack-" ,rocm = rocm ) 100self .assertIn ("-DGPU_TARGETS=gfx1103" ,d ) 101self .assertIn (f"-DCMAKE_HIP_COMPILER= { rocm / 'lib' / 'llvm' / 'bin' / 'clang++' } " ,d ) 102with self .assertRaises (ValueError ): 103QI .cmake_defs ("hip" ,arch = "gfx1103" ) 104with self .assertRaises (ValueError ): 105QI .cmake_defs ("metal" ) 106 107def test_unpack_puts_ggml_in_place (self ): 108with tempfile .TemporaryDirectory ()as d : 109d = Path (d ) 110z1 ,z2 = d / "sd.zip" ,d / "ggml.zip" 111with zipfile .ZipFile (z1 ,"w" )as z : 112z .writestr (f"stable-diffusion.cpp- { QI . SD_CPP_COMMIT } /CMakeLists.txt" ,"project(sd)" ) 113z .writestr (f"stable-diffusion.cpp- { QI . SD_CPP_COMMIT } /ggml/" ,"" ) 114with zipfile .ZipFile (z2 ,"w" )as z : 115z .writestr (f"ggml- { QI . SD_GGML_COMMIT } /CMakeLists.txt" ,"project(ggml)" ) 116src = QI .unpack_source (d ,z1 ,z2 ) 117self .assertEqual ((src / "ggml" / "CMakeLists.txt" ).read_text (),"project(ggml)" ) 118self .assertTrue (QI .source_ok (src )) 119 (src / ".strata-pin" ).write_text ("0" * 40 + "\n" + QI .SD_GGML_COMMIT + "\n" ) 120self .assertFalse (QI .source_ok (src ))# another pin: unpacked again 121 122def test_build_is_skipped_when_the_stamp_matches (self ): 123with tempfile .TemporaryDirectory ()as d ,mock .patch .object (setup ,"IMAGE_ENGINE" ,Path (d )): 124 (Path (d )/ QI .SD_EXE ).write_bytes (b"x" ) 125 (Path (d )/ "BUILD.json" ).write_text (json .dumps ({** QI .build_stamp ("cuda" ,86 ),"lib_dirs" : [d ]})) 126with mock .patch .object (setup ,"cmake_build" )as cb : 127exe ,libs = quiet (setup .build_image_engine ,"cuda" , {"arch" :86 }) 128cb .assert_not_called () 129self .assertEqual ((exe ,libs ), (Path (d )/ QI .SD_EXE , [d ])) 130with mock .patch .object (setup ,"cmake_build" )as cb ,mock .patch .object (setup ,"get_sd_cpp" ), \ 131mock .patch .object (setup ,"apply_native_runner" ), \ 132mock .patch .object (setup ,"find_nvcc" ,return_value = (None ,None )): 133with self .assertRaises (SystemExit ):# another arch: compiled again (here: no nvcc, so it stops) 134quiet (setup .build_image_engine ,"cuda" , {"arch" :89 }) 135 136 137class Config (unittest .TestCase ): 138def test_section (self ): 139c = QI .image_config ("vulkan" ,"Q8_0" ,Path ("/e/sd-cli" ),Path ("/m" ),uma = True ,lib_dirs = ["/l" ]) 140self .assertEqual (c ["diffusion_model" ],str (Path ("/m/qwen-image-2.1-turbo/qwen-image-2.1-turbo-Q8_0.gguf" ))) 141self .assertEqual (c ["args" ], ["--diffusion-fa" ,"--vae-tiling" ,"--vae-tile-overlap" ,"0.25" ]) 142self .assertTrue (c ["uma" ]) 143self .assertEqual (c ["gpu_policy" ],"auto" ) 144from serve .images import ImageGen 145with tempfile .TemporaryDirectory ()as d : 146ImageGen ({** c ,"out_dir" :d })# serve accepts what setup writes 147 148def test_images_no_drops_the_section (self ): 149with tempfile .TemporaryDirectory ()as d : 150p = Path (d )/ "strata-q2_0.json" 151p .write_text (json .dumps ({"exe" :"x" ,"images" : {"exe" :"sd-cli" },"mine" :1 })) 152quiet (setup .write_setup_config ,p , {"exe" :"y" ,"images" :None }) 153cfg = json .loads (p .read_text ()) 154self .assertNotIn ("images" ,cfg ) 155self .assertEqual (cfg ["mine" ],1 ) 156p .write_text (json .dumps ({"exe" :"x" ,"images" : {"exe" :"sd-cli" }})) 157quiet (setup .write_setup_config ,p , {"exe" :"y" })# not asked: carried over 158self .assertEqual (json .loads (p .read_text ())["images" ], {"exe" :"sd-cli" }) 159 160def test_flags (self ): 161src = (ROOT / "setup.py" ).read_text (encoding = "utf-8" ) 162for flag in ("--images" ,"--image-quant" ,"--image-backend" ,"--image-gpu-policy" ): 163self .assertRegex (src ,re .escape (f'ap.add_argument(" { flag } "' )) 164 165 166class Native (unittest .TestCase ): 167"""I2: the native DiT runner's patch for the pinned stable-diffusion.cpp, and its config switch.""" 168 169def test_stamp_and_config (self ): 170self .assertIn ("native" ,QI .build_stamp ("cuda" ,86 )) 171self .assertIn ("native" ,QI .build_stamp ("hip" ,"gfx1103" )) 172self .assertNotIn ("native" ,QI .build_stamp ("vulkan" ,None )) 173c = QI .image_config ("hip" ,"Q8_0" ,Path ("/e/sd-cli" ),Path ("/m" ),native = True ) 174self .assertIs (c ["native" ],True ) 175self .assertNotIn ("native" ,QI .image_config ("vulkan" ,"Q8_0" ,Path ("/e/sd-cli" ),Path ("/m" ))) 176 177def test_apply_is_idempotent (self ): 178sys .path .insert (0 ,str (QI .NATIVE_DIR )) 179import apply as A 180with tempfile .TemporaryDirectory ()as d : 181sd = Path (d ) 182 (sd / "ggml" / "src" / "ggml-cuda" ).mkdir (parents = True ) 183 (sd / "src" / "model" / "diffusion" ).mkdir (parents = True ) 184 (sd / "src" / "pipeline" ).mkdir (parents = True ) 185 (sd / "src" / "pipeline" / "model_builders.cpp" ).write_text ( 186"#include <x>\n" + A .INCLUDE_OLD + "int a;\n" + A .BUILDER_OLD + " }\n" ,encoding = "utf-8" ) 187 (sd / "CMakeLists.txt" ).write_text ("x\n" + A .CMAKE_OLD + "y\n" ,encoding = "utf-8" ) 188A .apply (sd ) 189first = [(sd / f ).read_text (encoding = "utf-8" )for f in ("src/pipeline/model_builders.cpp" ,"CMakeLists.txt" )] 190A .apply (sd ) 191again = [(sd / f ).read_text (encoding = "utf-8" )for f in ("src/pipeline/model_builders.cpp" ,"CMakeLists.txt" )] 192self .assertEqual (first ,again ) 193self .assertIn ("QwenImage21NativeRunner" ,first [0 ]) 194self .assertEqual (first [1 ].count ("SD_QI_NATIVE" ),1 ) 195self .assertTrue ((sd / "ggml/src/ggml-cuda/qi21-native.cu" ).is_file ()) 196 (sd / "CMakeLists.txt" ).write_text ("something else\n" ,encoding = "utf-8" ) 197with self .assertRaises (SystemExit ):# another sd.cpp: refused, not half-patched 198A .apply (sd ) 199 200 201class Noise (unittest .TestCase ): 202"""OWN-5 step 1: Strata's noise generator for sd-cli (tools/image/sdcpp_noise): off by default; nobody compiles 203again unasked.""" 204 205def test_stamp_without_the_switch_is_what_it_was (self ): 206self .assertEqual (QI .build_stamp ("cuda" ,86 ), {"sd_cpp" :QI .SD_CPP_COMMIT ,"ggml" :QI .SD_GGML_COMMIT , 207"backend" :"cuda" ,"arch" :"86" ,"native" :QI .native_hash ()}) 208self .assertEqual (QI .build_stamp ("vulkan" ,None ), {"sd_cpp" :QI .SD_CPP_COMMIT ,"ggml" :QI .SD_GGML_COMMIT , 209"backend" :"vulkan" ,"arch" :"" }) 210self .assertEqual (QI .build_stamp ("cuda" ,86 ,False ),QI .build_stamp ("cuda" ,86 )) 211 212def test_stamp_with_the_switch (self ): 213for backend ,arch in (("cuda" ,86 ), ("hip" ,"gfx1103" ), ("vulkan" ,None ), ("cpu" ,None )): 214s = QI .build_stamp (backend ,arch ,True ) 215self .assertEqual (s ["noise" ],QI .noise_hash ()) 216self .assertEqual ({k :v for k ,v in s .items ()if k != "noise" },QI .build_stamp (backend ,arch )) 217 218def test_the_native_hash_does_not_move (self ): 219"""The noise files live apart from sdcpp_native, so no CUDA / HIP user's sd-cli is rebuilt for them.""" 220self .assertNotEqual (QI .NOISE_DIR ,QI .NATIVE_DIR ) 221before = QI .native_hash () 222with tempfile .TemporaryDirectory ()as d : 223for name in QI .NOISE_FILES : 224 (Path (d )/ name ).write_bytes ((QI .NOISE_DIR / name ).read_bytes ()+ b"changed" ) 225with mock .patch .object (QI ,"NOISE_DIR" ,Path (d )): 226self .assertNotEqual (QI .noise_hash (),before ) 227self .assertEqual (QI .native_hash (),before ) 228self .assertFalse ({p .name for p in QI .NATIVE_DIR .iterdir ()}& (set (QI .NOISE_FILES )- {"apply.py" }))# both folders have an apply.py 229 230def test_noise_hash (self ): 231with tempfile .TemporaryDirectory ()as d : 232for name in QI .NOISE_FILES : 233 (Path (d )/ name ).write_bytes ((QI .NOISE_DIR / name ).read_bytes ()) 234with mock .patch .object (QI ,"NOISE_DIR" ,Path (d )): 235h = QI .noise_hash () 236self .assertRegex (h ,r"^[0-9a-f]{16}$" ) 237self .assertEqual (h ,QI .noise_hash ()) 238p = Path (d )/ "strata_noise.hpp" 239p .write_bytes (p .read_bytes ().replace (b"\r\n" ,b"\n" ).replace (b"\n" ,b"\r\n" ))# a Windows checkout: the same hash (the file may be CRLF already) 240self .assertEqual (QI .noise_hash (),h ) 241p .write_bytes (p .read_bytes ()+ b"// edit\n" ) 242self .assertNotEqual (QI .noise_hash (),h ) 243self .assertEqual (QI .noise_hash (),QI .noise_hash ()) 244 245def test_config (self ): 246c = QI .image_config ("cuda" ,"Q8_0" ,Path ("/e/sd-cli" ),Path ("/m" ),noise = True ) 247self .assertEqual (c ["noise" ],"strata" ) 248self .assertNotIn ("noise" ,QI .image_config ("cuda" ,"Q8_0" ,Path ("/e/sd-cli" ),Path ("/m" ))) 249from serve .images import ImageGen 250with tempfile .TemporaryDirectory ()as d : 251ImageGen ({** c ,"out_dir" :d })# serve accepts what setup writes 252 253def test_flags (self ): 254self .assertRegex ((ROOT / "setup.py" ).read_text (encoding = "utf-8" ),re .escape ('ap.add_argument("--image-noise"' )) 255self .assertRegex ((ROOT / "tools" / "image" / "install.py" ).read_text (encoding = "utf-8" ), 256re .escape ('ap.add_argument("--noise"' )) 257 258def test_build_decisions (self ): 259def stamp_file (d ,stamp ): 260 (Path (d )/ QI .SD_EXE ).write_bytes (b"x" ) 261 (Path (d )/ "BUILD.json" ).write_text (json .dumps ({** stamp ,"lib_dirs" : [d ]})) 262with tempfile .TemporaryDirectory ()as d ,mock .patch .object (setup ,"IMAGE_ENGINE" ,Path (d )): 263# a build made before the switch existed: kept when the switch is off, rebuilt with the generator when on 264stamp_file (d ,QI .build_stamp ("cuda" ,86 )) 265with mock .patch .object (setup ,"cmake_build" )as cb : 266quiet (setup .build_image_engine ,"cuda" , {"arch" :86 }) 267quiet (setup .build_image_engine ,"cuda" , {"arch" :86 },False ) 268cb .assert_not_called () 269with mock .patch .object (setup ,"get_sd_cpp" ),mock .patch .object (setup ,"apply_native_runner" ), \ 270mock .patch .object (setup ,"apply_noise_generator" )as an , \ 271mock .patch .object (setup ,"find_nvcc" ,return_value = (None ,None )): 272with self .assertRaises (SystemExit ):# stops at "no nvcc", after the patches 273quiet (setup .build_image_engine ,"cuda" , {"arch" :86 },True ) 274an .assert_called_once () 275# a build that has the generator: kept with the switch on, kept with it off 276stamp_file (d ,QI .build_stamp ("cuda" ,86 ,True )) 277with mock .patch .object (setup ,"cmake_build" )as cb : 278quiet (setup .build_image_engine ,"cuda" , {"arch" :86 },True ) 279quiet (setup .build_image_engine ,"cuda" , {"arch" :86 },False ) 280cb .assert_not_called () 281# another arch with the switch off: compiled again, without the generator 282with mock .patch .object (setup ,"get_sd_cpp" ),mock .patch .object (setup ,"apply_native_runner" ), \ 283mock .patch .object (setup ,"apply_noise_generator" )as an , \ 284mock .patch .object (setup ,"find_nvcc" ,return_value = (None ,None )): 285with self .assertRaises (SystemExit ): 286quiet (setup .build_image_engine ,"cuda" , {"arch" :89 }) 287an .assert_not_called () 288 289 290class VaeDecoder (unittest .TestCase ): 291"""OWN-5 step 2: Strata's VAE decoder for sd-cli (tools/image/sdcpp_vae, CUDA / HIP builds): off by default; nobody 292compiles again unasked; another backend never gets it.""" 293 294def test_stamp_without_the_switch_is_what_it_was (self ): 295for backend ,arch in (("cuda" ,86 ), ("hip" ,"gfx1201" ), ("vulkan" ,None ), ("cpu" ,None )): 296self .assertEqual (QI .build_stamp (backend ,arch ,False ,False ),QI .build_stamp (backend ,arch )) 297self .assertNotIn ("vae_decoder" ,QI .build_stamp (backend ,arch ,True )) 298 299def test_stamp_with_the_switch (self ): 300for backend ,arch in (("cuda" ,86 ), ("hip" ,"gfx1201" )): 301s = QI .build_stamp (backend ,arch ,False ,True ) 302self .assertEqual (s ["vae_decoder" ],QI .vae_hash ()) 303self .assertEqual ({k :v for k ,v in s .items ()if k != "vae_decoder" },QI .build_stamp (backend ,arch )) 304self .assertEqual (QI .build_stamp (backend ,arch ,True ,True )["noise" ],QI .noise_hash ()) 305for backend in ("vulkan" ,"cpu" ): 306self .assertEqual (QI .build_stamp (backend ,None ,False ,True ),QI .build_stamp (backend ,None )) 307 308def test_vae_hash_follows_the_decoder_sources (self ): 309h = QI .vae_hash () 310self .assertRegex (h ,r"^[0-9a-f]{16}$" ) 311self .assertEqual (QI .vae_hash (),h ) 312import importlib .util 313spec = importlib .util .spec_from_file_location ("strata_sdcpp_vae_apply_qtest" ,QI .VAE_DIR / "apply.py" ) 314mod = importlib .util .module_from_spec (spec ) 315spec .loader .exec_module (mod ) 316with tempfile .TemporaryDirectory ()as d : 317for src ,_ in mod .FILES : 318 (Path (d )/ src ).parent .mkdir (parents = True ,exist_ok = True ) 319 (Path (d )/ src ).write_bytes ((ROOT / src ).read_bytes ()) 320 (Path (d )/ mod .HIP_COMPAT ).mkdir (parents = True ,exist_ok = True ) 321for f in (ROOT / mod .HIP_COMPAT ).iterdir (): 322 (Path (d )/ mod .HIP_COMPAT / f .name ).write_bytes (f .read_bytes ().replace (b"\r\n" ,b"\n" ).replace (b"\n" ,b"\r\n" ))# (CRLF already in a Windows checkout) 323self .assertEqual (QI .vae_hash (Path (d )),h )# a CRLF checkout: the same hash 324p = Path (d )/ "src" / "image" / "vae" / "wan_vae.cu" 325p .write_bytes (p .read_bytes ()+ b"// edit\n" ) 326self .assertNotEqual (QI .vae_hash (Path (d )),h ) 327 328def test_config (self ): 329c = QI .image_config ("cuda" ,"Q8_0" ,Path ("/e/sd-cli" ),Path ("/m" ),vae = True ) 330self .assertEqual (c ["vae_decoder" ],"strata" ) 331self .assertTrue (c ["vae" ].endswith ("qwen_image_2.1_vae_bf16.safetensors" ))# the file path is untouched 332self .assertNotIn ("vae_decoder" ,QI .image_config ("cuda" ,"Q8_0" ,Path ("/e/sd-cli" ),Path ("/m" ))) 333self .assertNotIn ("vae_decoder" ,QI .image_config ("vulkan" ,"Q4_K_M" ,Path ("/e/sd-cli" ),Path ("/m" ),vae = True )) 334from serve .images import ImageGen 335with tempfile .TemporaryDirectory ()as d : 336ImageGen ({** c ,"out_dir" :d })# serve accepts what setup writes 337 338def test_flags (self ): 339self .assertRegex ((ROOT / "setup.py" ).read_text (encoding = "utf-8" ),re .escape ('ap.add_argument("--image-vae"' )) 340self .assertIn ('warn(f"Strata\'s own picture decoder runs with CUDA or ROCm only; image generation on {backend} keeps "\n' 341' "stable-diffusion.cpp\'s decoder")' , 342 (ROOT / "setup.py" ).read_text (encoding = "utf-8" ))# the owner's wording of 11-11 343self .assertRegex ((ROOT / "tools" / "image" / "install.py" ).read_text (encoding = "utf-8" ), 344re .escape ('ap.add_argument("--vae"' )) 345 346def test_build_decisions (self ): 347def stamp_file (d ,stamp ): 348 (Path (d )/ QI .SD_EXE ).write_bytes (b"x" ) 349 (Path (d )/ "BUILD.json" ).write_text (json .dumps ({** stamp ,"lib_dirs" : [d ]})) 350 351def build_until_nvcc (* args ): 352with mock .patch .object (setup ,"get_sd_cpp" ),mock .patch .object (setup ,"apply_native_runner" ), \ 353mock .patch .object (setup ,"apply_noise_generator" ), \ 354mock .patch .object (setup ,"apply_vae_decoder" )as av , \ 355mock .patch .object (setup ,"find_nvcc" ,return_value = (None ,None )): 356with self .assertRaises (SystemExit ):# stops at "no nvcc", after the patches 357quiet (setup .build_image_engine ,* args ) 358return av 359with tempfile .TemporaryDirectory ()as d ,mock .patch .object (setup ,"IMAGE_ENGINE" ,Path (d )): 360stamp_file (d ,QI .build_stamp ("cuda" ,86 ))# a build made before the switch existed 361with mock .patch .object (setup ,"cmake_build" )as cb : 362quiet (setup .build_image_engine ,"cuda" , {"arch" :86 },False ,False ) 363cb .assert_not_called () 364build_until_nvcc ("cuda" , {"arch" :86 },False ,True ).assert_called_once ()# switched on: rebuilt 365stamp_file (d ,QI .build_stamp ("cuda" ,86 ,False ,True )) 366with mock .patch .object (setup ,"cmake_build" )as cb : 367quiet (setup .build_image_engine ,"cuda" , {"arch" :86 },False ,True ) 368quiet (setup .build_image_engine ,"cuda" , {"arch" :86 },False ,False )# off again: kept (inert) 369cb .assert_not_called () 370build_until_nvcc ("cuda" , {"arch" :89 },False ,False ).assert_not_called () 371 372 373if __name__ == "__main__" : 374unittest .main ()