okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e4309aaf3
main
1"""tools/test_qi21_pixels.py - the decoded picture to 8-bit pixels (src/image/vae/qi21_pixels.cpp) as 2stable-diffusion.cpp 2988060 makes them: tools/image/gate/data/qi21_pixels_fixture.bin holds 8,184 values of sd.cpp's 3decoded float picture (512x512, prompt 0, seed 42; 729 of them within 1e-3 of a rounding boundary) with the bytes of 4sd-cli's own PNG at the same places; plus edge values whose expected bytes are computed here with numpy's float32 5(v x 255 rounded, + 0.5 rounded, truncated; 0 / 255 at or outside the ends). Compiled twice (-O2; -O3 6-ffp-contract=fast: the file must not fuse). No GPU, no sd.cpp, no model file. 7 8python -m pytest tools/test_qi21_pixels.py""" 9import shutil 10import struct 11import subprocess 12import tempfile 13import unittest 14from pathlib import Path 15 16import numpy as np 17 18ROOT = Path (__file__ ).resolve ().parents [1 ] 19CXX = shutil .which ("g++" )or shutil .which ("c++" )or shutil .which ("clang++" ) 20FIXTURE = ROOT / "tools" / "image" / "gate" / "data" / "qi21_pixels_fixture.bin" 21 22 23def edge_fixture (path :Path )-> None : 24k = np .arange (256 ,dtype = np .float32 ) 25v = np .concatenate ([ 26np .array ([- 1.0 ,- 0.0 ,0.0 ,1e-45 ,1.0 ,1.5 ,0.99999994 ],dtype = np .float32 ), 27k / np .float32 (255 ), (k + np .float32 (0.5 ))/ np .float32 (255 ), 28np .nextafter ((k + np .float32 (0.5 ))/ np .float32 (255 ),np .float32 (0 )), 29np .nextafter ((k + np .float32 (0.5 ))/ np .float32 (255 ),np .float32 (1 ))]).astype (np .float32 ) 30m = (v * np .float32 (255 )).astype (np .float32 ) 31s = (m + np .float32 (0.5 )).astype (np .float32 ) 32want = np .where (v <= 0 ,0 ,np .where (v >= 1 ,255 ,np .clip (s ,0 ,255 ).astype (np .int64 ))).astype (np .uint8 ) 33path .write_bytes (struct .pack ("<i" ,len (v ))+ v .astype ("<f4" ).tobytes ()+ want .tobytes ()) 34 35 36@ unittest . skipUnless ( CXX , "no C++ compiler" ) 37class PixelsMatchSdcpp (unittest .TestCase ): 38def build_and_check (self ,flags ): 39with tempfile .TemporaryDirectory ()as td : 40exe = Path (td )/ "strata-pixels-check" 41r = subprocess .run ([CXX ,"-std=c++17" ,* flags ,"-I" ,str (ROOT / "include" ), 42str (ROOT / "src" / "image" / "vae" / "qi21_pixels.cpp" ), 43str (ROOT / "src" / "image" / "vae" / "qi21_pixels_check_main.cpp" ),"-o" ,str (exe )], 44capture_output = True ,text = True ,timeout = 300 ) 45self .assertEqual (r .returncode ,0 ,r .stderr [- 3000 :]) 46edges = Path (td )/ "edges.bin" 47edge_fixture (edges ) 48r = subprocess .run ([str (exe ),str (FIXTURE ),str (edges )],capture_output = True ,text = True ,timeout = 60 ) 49self .assertIn ("PIXELS-CHECK PASS" ,r .stdout ,r .stdout ) 50 51def test_o2 (self ): 52self .build_and_check (["-O2" ]) 53 54def test_o3_contract_fast (self ): 55self .build_and_check (["-O3" ,"-ffp-contract=fast" ]) 56 57 58if __name__ == "__main__" : 59unittest .main ()