okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
736266e87
main
1"""Tests for the patch that puts Strata's VAE decoder into sd-cli (tools/image/sdcpp_vae/apply.py), on stand-in files 2with the code it edits at the pinned stable-diffusion.cpp commit: the sources it copies (byte for byte), the CMake 3target, the decode hook with its switch and its fallback (one warning that names the reason, then sd.cpp's own 4decoder), that a second run changes nothing, that it works before or after the native runner's and the noise 5generator's patches, and that a checkout at another commit is refused. 6 7python -m unittest tools.test_sdcpp_vae 8""" 9from __future__import annotations 10 11import importlib .util 12import tempfile 13import unittest 14from pathlib import Path 15 16ROOT = Path (__file__ ).resolve ().parents [1 ] 17 18 19def load (name ,path ): 20spec = importlib .util .spec_from_file_location (name ,path ) 21m = importlib .util .module_from_spec (spec ) 22spec .loader .exec_module (m ) 23return m 24 25 26V = load ("strata_sdcpp_vae_apply_test" ,ROOT / "tools" / "image" / "sdcpp_vae" / "apply.py" ) 27N = load ("strata_sdcpp_native_apply_for_vae_test" ,ROOT / "tools" / "image" / "sdcpp_native" / "apply.py" ) 28Z = load ("strata_sdcpp_noise_apply_for_vae_test" ,ROOT / "tools" / "image" / "sdcpp_noise" / "apply.py" ) 29 30ENGINE = """#include "core/rng.hpp" 31#include "core/rng_mt19937.hpp" 32#include "core/rng_philox.hpp" 33#include "core/util.h" 34 35bool StableDiffusionGGML::init(const sd_ctx_params_t* sd_ctx_params) { 36rng = get_rng(sd_ctx_params->rng_type); 37if (sd_ctx_params->sampler_rng_type != RNG_TYPE_COUNT && sd_ctx_params->sampler_rng_type != sd_ctx_params->rng_type) { 38sampler_rng = get_rng(sd_ctx_params->sampler_rng_type); 39} else { 40sampler_rng = rng; 41} 42} 43 44sd::Tensor<float> StableDiffusionGGML::decode_first_stage(const sd::Tensor<float>& x, bool decode_video) { 45auto latents = first_stage_model->diffusion_to_vae_latents(x); 46auto tiling_params = first_stage_model->resolve_tiling_params(vae_tiling_params); 47return {}; 48} 49""" 50CMAKE = "add_library(${SD_LIB} STATIC ${SD_LIB_SOURCES})\ntarget_include_directories(${SD_LIB} PUBLIC . src include)\n" 51BUILDERS = '#include "model/diffusion/minit2i.hpp"\n' + N .BUILDER_OLD 52 53 54def fake_tree (d :Path )-> Path : 55sd = d / "stable-diffusion.cpp" 56 (sd / "src" / "pipeline" ).mkdir (parents = True ) 57 (sd / "src" / "core" ).mkdir (parents = True ) 58 (sd / "src" / "model" / "diffusion" ).mkdir (parents = True ) 59 (sd / "ggml" / "src" / "ggml-cuda" ).mkdir (parents = True ) 60 (sd / "src" / "pipeline" / "diffusion_engine.cpp" ).write_text (ENGINE ) 61 (sd / "src" / "pipeline" / "model_builders.cpp" ).write_text (BUILDERS ) 62 (sd / "CMakeLists.txt" ).write_text (CMAKE ) 63return sd 64 65 66class Apply (unittest .TestCase ): 67def test_copies_edits_and_is_idempotent (self ): 68with tempfile .TemporaryDirectory ()as t : 69sd = fake_tree (Path (t )) 70V .apply (sd ) 71for src ,rel in V .FILES : 72self .assertEqual ((sd / "src" / "strata_vae" / rel ).read_bytes (), (ROOT / src ).read_bytes (),rel ) 73self .assertTrue ((sd / "src" / "strata_vae" / "include" / "strata" / "hip_compat" / "cublas_v2.h" ).is_file ()) 74eng = (sd / "src" / "pipeline" / "diffusion_engine.cpp" ).read_text () 75cm = (sd / "CMakeLists.txt" ).read_text () 76self .assertIn ('std::strstr(config_->params.model_args, "strata_vae=1")' ,eng ) 77self .assertIn ("version == VERSION_QWEN_IMAGE_2_1" ,eng ) 78self .assertIn ("#ifdef SD_STRATA_VAE" ,eng ) 79# the fallback: the reason goes into the warning, then stable-diffusion.cpp's decoder runs (no return) 80self .assertEqual (eng .count ('LOG_WARN("strata VAE decoder: %s; using stable-diffusion.cpp\'s decoder", ' 81'strata_err.c_str());' ),2 ) 82# a non-finite picture: the warning says that sd.cpp's decoder (the same f16 operands) may fail the same way 83self .assertIn ('if (strata_err.find("not finite") != std::string::npos)' ,eng ) 84self .assertIn ("using stable-diffusion.cpp's decoder, which may fail the same way" ,eng ) 85cu = (ROOT / "src" / "image" / "vae" / "wan_vae.cu" ).read_text (encoding = "utf-8" ) 86self .assertIn ('"wan_vae: the picture has values that are not finite' ,cu )# the reason the hook looks for 87self .assertLess (eng .index ("return decoded;" ),eng .rindex ("LOG_WARN" )) 88self .assertLess (eng .rindex ("LOG_WARN" ),eng .index ("auto tiling_params" )) 89self .assertIn ("add_library(strata_vae STATIC" ,cm ) 90self .assertIn ("target_compile_definitions(${SD_LIB} PRIVATE SD_STRATA_VAE)" ,cm ) 91self .assertNotIn ("use_fast_math" ,cm ) 92# HIP: the library defined before enable_language(HIP) needs the architectures too (p620, 11-10) 93self .assertIn ('set_target_properties(strata_vae ${SD_LIB} PROPERTIES HIP_ARCHITECTURES ' 94'"${CMAKE_HIP_ARCHITECTURES}")' ,cm ) 95self .assertLess (cm .index ("if (SD_CUDA OR SD_HIPBLAS)" ),cm .index (V .CMAKE_OLD )) 96before = (eng ,cm ) 97V .apply (sd ) 98self .assertEqual (((sd / "src" / "pipeline" / "diffusion_engine.cpp" ).read_text (), 99 (sd / "CMakeLists.txt" ).read_text ()),before ) 100 101def test_with_native_and_noise_in_either_order (self ): 102results = [] 103for order in ((V ,N ,Z ), (N ,Z ,V )): 104with tempfile .TemporaryDirectory ()as t : 105sd = fake_tree (Path (t )) 106for m in order : 107if m is N : 108N .edit (sd / "src" / "pipeline" / "model_builders.cpp" ,N .INCLUDE_OLD ,N .INCLUDE_NEW ) 109N .edit (sd / "src" / "pipeline" / "model_builders.cpp" ,N .BUILDER_OLD ,N .BUILDER_NEW ) 110N .edit (sd / "CMakeLists.txt" ,N .CMAKE_OLD ,N .CMAKE_NEW ) 111elif m is Z : 112Z .apply (sd ) 113else : 114V .apply (sd ) 115eng = (sd / "src" / "pipeline" / "diffusion_engine.cpp" ).read_text () 116cm = (sd / "CMakeLists.txt" ).read_text () 117self .assertIn (V .MARK ,eng ) 118self .assertIn (Z .MARK ,eng ) 119self .assertIn ("SD_QI_NATIVE" ,cm ) 120self .assertIn ("SD_STRATA_VAE" ,cm ) 121results .append (eng .count (V .MARK )) 122self .assertEqual (results [0 ],results [1 ]) 123 124def test_other_commit_is_refused (self ): 125with tempfile .TemporaryDirectory ()as t : 126sd = fake_tree (Path (t )) 127 (sd / "src" / "pipeline" / "diffusion_engine.cpp" ).write_text (ENGINE .replace ("diffusion_to_vae_latents(x)" , 128"to_vae(x)" )) 129with self .assertRaises (SystemExit ): 130V .apply (sd ) 131 132 133if __name__ == "__main__" : 134unittest .main ()