okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e4309aaf3
main
1// src/image/vae/qi21_vae_io_check_main.cpp - test program strata-vae-io-check (not shipped): the two byte-comparable 2// steps around the VAE decoder against stable-diffusion.cpp's own stage dumps (tools/image/gate/stage_hooks.py): 3// latent.bin -> wan_vae_denormalize -> must equal vae_in.bin (what sd.cpp's VAE decoder got); 4// vae_out.bin (sd.cpp's decoded float picture) -> qi21_picture_to_u8 -> written to <out dir>/<tag>.rgb for the 5// comparison with the pixels of sd-cli's own PNG (tools/image/gate/png_pixels.py); a fused-multiply-add variant 6// (v * 255 + 0.5 in one rounding, how GCC may compile sd.cpp on aarch64) is written beside it as <tag>.fma.rgb. 7// 8// strata-vae-io-check <out dir> <dump dir> [<dump dir> ...] 9#include "strata/image/qi21_pixels.hpp" 10#include "strata/image/wan_vae.hpp" 11 12#include <cmath> 13#include <cstdio> 14#include <cstring> 15#include <filesystem> 16#include <fstream> 17#include <string> 18#include <vector> 19 20namespace fs= std::filesystem; 21using namespace strata::image ; 22 23namespace { 24 25bool read_f32 (const fs::path & p , std::vector < float >& v ) { 26 std::ifstream f (p , std::ios::binary ); 27if (!f )return false; 28 std::vector < char > raw ((std::istreambuf_iterator < char > (f )), {}); 29v .resize (raw .size () /4 ); 30 std::memcpy (v .data (),raw .data (),v .size ()* 4 ); 31return true; 32} 33 34std::vector < long long > read_shape (const fs::path & p ) { 35 std::vector < long long > s ; 36 std::ifstream f (p ); 37long long x ; 38while (f >>x )s .push_back (x ); 39return s ; 40} 41 42uint8_t fused_u8 (float v ) {// the aarch64 GCC reading of sd.cpp's expression 43if (v <=0.0f )return 0 ; 44if (v >=1.0f )return 255 ; 45return static_cast < uint8_t > (std::fmaf (v ,255.0f ,0.5f )); 46} 47 48}// namespace 49 50int main (int argc ,char ** argv ) { 51if (argc < 3 ) { 52 std::fprintf (stderr ,"usage: strata-vae-io-check <out dir> <dump dir> [...]\n" ); 53return 2 ; 54 } 55const fs::path out (argv [1 ]); 56 fs::create_directories (out ); 57int bad = 0 ; 58for (int i = 2 ;i < argc ;++ i ) { 59const fs::path d (argv [i ]); 60const std::string tag = d .parent_path ().filename ().string ()+ "__" + d .filename ().string (); 61 std::vector < float > lat ,vin ,vout ; 62const auto ls = read_shape (d /"latent.shape" ),os = read_shape (d /"vae_out.shape" ); 63if (!read_f32 (d /"latent.bin" ,lat )|| !read_f32 (d /"vae_in.bin" ,vin )|| !read_f32 (d /"vae_out.bin" ,vout )|| 64ls .size ()< 3 || os .size ()< 3 ) { 65 std::printf ("%s: dumps missing\n" ,tag .c_str ()); 66++ bad ; 67continue ; 68 } 69const int lw = (int )ls [0 ],lh = (int )ls [1 ]; 70wan_vae_denormalize (lat .data (),lh ,lw ); 71size_t differ = 0 ; 72for (size_t k = 0 ;k < lat .size ()&& k < vin .size ();++ k )differ += std::memcmp (& lat [k ],& vin [k ],4 )!= 0 ; 73const bool in_ok = lat .size ()== vin .size ()&& differ == 0 ; 74 std::printf ("%s: VAE input (%d x %d x %lld) %s" ,tag .c_str (),lw ,lh , (long long )ls [2 ], 75in_ok ?"byte-identical" :"DIFFERS" ); 76if (!in_ok ) std::printf (" (%zu of %zu values; sizes %zu / %zu)" ,differ ,vin .size (),lat .size (),vin .size ()); 77 std::printf ("\n" ); 78bad += !in_ok ; 79const int w = (int )os [0 ],h = (int )os [1 ],c = (int )os [2 ]; 80 std::vector < uint8_t > px ((size_t )w * h * c ),fx ((size_t )w * h * c ); 81qi21_picture_to_u8 (vout .data (),w ,h ,c ,px .data ()); 82const size_t np = (size_t )w * h ; 83for (size_t p = 0 ;p < np ;++ p ) 84for (int k = 0 ;k < c ;++ k )fx [p * c + k ]= fused_u8 (vout [(size_t )k * np + p ]); 85 std::ofstream (out / (tag + ".rgb" ), std::ios::binary ).write ((const char * )px .data (),px .size ()); 86 std::ofstream (out / (tag + ".fma.rgb" ), std::ios::binary ).write ((const char * )fx .data (),fx .size ()); 87size_t fd = 0 ; 88for (size_t k = 0 ;k < px .size ();++ k )fd += px [k ]!= fx [k ]; 89 std::printf ("%s: picture %d x %d x %d -> %s.rgb (separate roundings) and .fma.rgb (fused): %zu bytes differ " 90"between the two\n" ,tag .c_str (),w ,h ,c ,tag .c_str (),fd ); 91 } 92 std::printf ("VAE-IO-CHECK (VAE input) %s\n" ,bad ?"FAIL" :"PASS" ); 93return bad ?1 :0 ; 94}