okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e4309aaf3
main
1"""tools/test_qi21_glue.py - the steps between Strata's image parts (src/image/pipeline/qi21_glue.cpp) give the bytes 2stable-diffusion.cpp 2988060's code gives: the starting latent (DiscreteFlowDenoiser::noise_scaling on a zero latent), 3the condition (apply_token_weights + the system rows dropped) and the decoder output to 0..1 (scale_tensor_to_0_1). 4The expectations are those operations written out with numpy float32 (and a sequential float64 sum for sd.cpp's 5Tensor<float>::mean). Compiled twice (-O2; -O3 -ffp-contract=fast: the file must not fuse). No GPU, no model file. 6 7python -m pytest tools/test_qi21_glue.py""" 8import shutil 9import subprocess 10import tempfile 11import unittest 12from pathlib import Path 13 14import numpy as np 15 16ROOT = Path (__file__ ).resolve ().parents [1 ] 17CXX = shutil .which ("g++" )or shutil .which ("c++" )or shutil .which ("clang++" ) 18F = np .float32 19 20 21def sd_mean (v :np .ndarray )-> np .float32 : 22"""sd::Tensor<float>::mean: a double sum in element order, divided in double, then float.""" 23s = np .cumsum (v .reshape (- 1 ).astype (np .float64 ))[- 1 ] 24return F (s / np .float64 (v .size )) 25 26 27def want_condition (h :np .ndarray ,w :np .ndarray ,n_system :int )-> np .ndarray : 28h = h .astype (np .float32 ).copy () 29if not np .all (w == F (1 )): 30m0 = sd_mean (h ) 31h = (h * w [:,None ]).astype (np .float32 ) 32m1 = sd_mean (h ) 33if np .isfinite (m0 )and np .isfinite (m1 )and m1 != 0 : 34h = (h * F (m0 / m1 )).astype (np .float32 ) 35return h [n_system :] 36 37 38def want_unit (x :np .ndarray )-> np .ndarray : 39v = ((x + F (1 )).astype (np .float32 )* F (0.5 )).astype (np .float32 ) 40return np .where (v < 0 ,F (0 ),np .where (v > 1 ,F (1 ),v )).astype (np .float32 ) 41 42 43@ unittest . skipUnless ( CXX , "no C++ compiler" ) 44class GlueMatchesSdcpp (unittest .TestCase ): 45def run_all (self ,flags ): 46rng = np .random .default_rng (7 ) 47with tempfile .TemporaryDirectory ()as td : 48t = Path (td ) 49exe = t / "strata-glue-check" 50r = subprocess .run ([CXX ,"-std=c++17" ,* flags ,"-I" ,str (ROOT / "include" ), 51str (ROOT / "src" / "image" / "pipeline" / "qi21_glue.cpp" ), 52str (ROOT / "src" / "image" / "pipeline" / "qi21_glue_check_main.cpp" ),"-o" ,str (exe )], 53capture_output = True ,text = True ,timeout = 300 ) 54self .assertEqual (r .returncode ,0 ,r .stderr [- 3000 :]) 55 56def run (* args ): 57p = subprocess .run ([str (exe ),* map (str ,args )],capture_output = True ,text = True ,timeout = 60 ) 58self .assertEqual (p .returncode ,0 ,p .stdout + p .stderr ) 59 60# starting latent: sigma0 1.0 (Turbo) and another sigma; -0.0 noise becomes +0.0 as in sd.cpp 61noise = np .concatenate ([rng .standard_normal (4096 ), [- 0.0 ,0.0 ,1e-45 ,- 1e-45 ]]).astype (np .float32 ) 62noise .tofile (t / "noise.bin" ) 63for sigma in ("1.0" ,"0.978453" ): 64run ("latent" ,t / "noise.bin" ,sigma ,t / "x0.bin" ) 65s = F (float (sigma )) 66want = ((np .zeros_like (noise )* (F (1 )- s )).astype (np .float32 )+ (noise * s ).astype (np .float32 )) 67got = np .fromfile (t / "x0.bin" ,dtype = np .float32 ) 68self .assertEqual (got .tobytes (),want .astype (np .float32 ).tobytes (),sigma ) 69 70# condition: plain weights (rows dropped only), weighted rows, a BREAK-like -1, a zero new mean 71n ,hd = 23 ,96 72h = (rng .standard_normal ((n ,hd ))* 40 ).astype (np .float32 ) 73h .tofile (t / "h.bin" ) 74cases = [np .ones (n ,np .float32 ), 75np .array ([1.0 ]* 10 + [1.21 ]* 5 + [0.9 ]* 8 ,np .float32 ), 76np .array ([1.0 ]* 20 + [- 1.0 ,1.1 ,1.0 ],np .float32 )] 77for i ,w in enumerate (cases ): 78w .tofile (t / "w.bin" ) 79run ("cond" ,t / "h.bin" ,n ,hd ,t / "w.bin" ,14 ,t / "c.bin" ) 80got = np .fromfile (t / "c.bin" ,dtype = np .float32 ) 81self .assertEqual (got .tobytes (),want_condition (h ,w ,14 ).tobytes (),f"weights case { i } " ) 82z = np .zeros ((n ,hd ),np .float32 ) 83z .tofile (t / "z.bin" ) 84cases [1 ].tofile (t / "w.bin" ) 85run ("cond" ,t / "z.bin" ,n ,hd ,t / "w.bin" ,14 ,t / "c.bin" ) 86self .assertEqual (np .fromfile (t / "c.bin" ,dtype = np .float32 ).tobytes (),z [14 :].tobytes ()) 87 88# decoder output to 0..1, including the ends and values next to them 89x = np .concatenate ([rng .uniform (- 1.3 ,1.3 ,20000 ), 90 [- 1.0 ,1.0 ,- 3.0 ,3.0 ,0.0 ,- 0.0 ]]).astype (np .float32 ) 91x = np .concatenate ([x ,np .nextafter (np .float32 ([- 1 ,1 ]),np .float32 (0 )), 92np .nextafter (np .float32 ([- 1 ,1 ]),np .float32 ([- 2 ,2 ]))]).astype (np .float32 ) 93x .tofile (t / "u.bin" ) 94run ("unit" ,t / "u.bin" ,t / "u_out.bin" ) 95self .assertEqual (np .fromfile (t / "u_out.bin" ,dtype = np .float32 ).tobytes (),want_unit (x ).tobytes ()) 96 97def test_o2 (self ): 98self .run_all (["-O2" ]) 99 100def test_o3_contract_fast (self ): 101self .run_all (["-O3" ,"-ffp-contract=fast" ]) 102 103 104if __name__ == "__main__" : 105unittest .main ()