okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e4309aaf3
main
1"""tools/test_qi21_noise.py - our initial noise (src/image/noise/qi21_noise.cpp, Strata's Philox generator) against 2stable-diffusion.cpp 2988060's own PhiloxRNG as its x86 build computes it: tools/image/gate/data/qi21_noise_sha.json 3holds the sha256 of sd.cpp's noise for seeds 42, 1234, 7, 2026 at 65,536 and 262,144 values (the 512 and 1024 4latents), made by compiling sd.cpp's core/rng_philox.hpp with g++ -O3 on x86 (p1006). Compiles our generator with 5the system C++ compiler; no GPU, no sd.cpp, no model file. 6 7python -m pytest tools/test_qi21_noise.py""" 8import hashlib 9import json 10import shutil 11import subprocess 12import sys 13import tempfile 14import unittest 15from pathlib import Path 16 17ROOT = Path (__file__ ).resolve ().parents [1 ] 18CXX = shutil .which ("g++" )or shutil .which ("c++" )or shutil .which ("clang++" ) 19 20 21@ unittest . skipUnless ( CXX , "no C++ compiler" ) 22class NoiseMatchesSdcppX86 (unittest .TestCase ): 23def test_seeds (self ): 24cases = json .loads ((ROOT / "tools" / "image" / "gate" / "data" / "qi21_noise_sha.json" ).read_text ()) 25self .assertEqual (len (cases ),8 ) 26with tempfile .TemporaryDirectory ()as td : 27inc = Path (td )/ "inc" 28subprocess .run ([sys .executable ,"-I" ,"-B" ,str (ROOT / "tools" / "image" / "sdcpp_noise" / "apply.py" ), 29"--prepare" ,str (inc )],check = True ,capture_output = True ) 30exe = Path (td )/ "strata-noise-check" 31r = subprocess .run ([CXX ,"-std=c++17" ,"-O2" ,"-I" ,str (ROOT / "include" ), 32"-I" ,str (ROOT / "tools" / "image" / "sdcpp_noise" ),"-I" ,str (inc ), 33str (ROOT / "src" / "image" / "noise" / "qi21_noise.cpp" ), 34str (ROOT / "src" / "image" / "noise" / "qi21_noise_check_main.cpp" ),"-o" ,str (exe )], 35capture_output = True ,text = True ,timeout = 600 ) 36self .assertEqual (r .returncode ,0 ,r .stderr [- 3000 :]) 37bad = [] 38for c in cases : 39out = Path (td )/ "n.bin" 40subprocess .run ([str (exe ),"--write" ,str (c ["seed" ]),str (c ["count" ]),str (out )],check = True ) 41if hashlib .sha256 (out .read_bytes ()).hexdigest ()!= c ["sha256" ]: 42bad .append ((c ["seed" ],c ["count" ])) 43self .assertEqual (bad , []) 44 45 46if __name__ == "__main__" : 47unittest .main ()