okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
736266e87
main
1// src/image/vae/wan_vae_decode_main.cpp - test program for Strata's VAE decoder (OWN-5 gate harness). 2// 3// strata-vae-decode <vae.safetensors> <latent.bin> <out.f32> [--dump DIR] [--repeat N] [--device D] [--stream on|off] [--band-rows N] [--threads N] [--stats FILE] 4// 5// latent.bin: the diffusion-space latent as run_set.py --dump writes it (float32 planar [64][h][w], latent.shape beside 6// it says "w h 64 1"); it is denormalized like sd.cpp's decode_first_stage does, then decoded. out.f32: float32 7// planar [4][16h][16w] (before sd.cpp's 0-1 scaling; tools/image/gate/vae_ref.py --png makes the picture). 8// --dump DIR writes every stage as DIR/<name>.f32 (pixel-major [H][W][C]) with DIR/<name>.shape "H W C". 9// --band-rows N: the row bands N picture rows high instead of the default (the identity test only). 10// --threads N: threads for reading and converting the weights at open (default 1). 11// --stats FILE: the operand range report of the decode (wan_vae_range_report) into FILE. 12// --stream off: the one-pass reference decode (the product always uses the row bands; same bytes, more memory). 13// --repeat N decodes N times and checks that every run gives the same bytes (run to run), printing each time. 14// Prints one line per run: "decode <ms> ms, peak <MiB> MiB (weights+work <MiB>)". 15#include "strata/image/wan_vae.hpp" 16 17#include <chrono> 18#include <cstdio> 19#include <cstdlib> 20#include <cstring> 21#include <fstream> 22#include <string> 23#include <vector> 24 25using namespace strata::image ; 26 27static bool read_file (const std::string & p , std::vector < float >& v ,size_t n ) { 28FILE * f = std::fopen (p .c_str (),"rb" ); 29if (!f )return false; 30v .resize (n ); 31size_t got = std::fread (v .data (),4 ,n ,f ); 32 std::fclose (f ); 33return got == n ; 34} 35 36int main (int argc ,char ** argv ) { 37if (argc < 4 ) { 38 std::fprintf (stderr ,"usage: strata-vae-decode <vae.safetensors> <latent.bin> <out.f32> [--dump DIR] [--repeat N] [--device D]\n" ); 39return 2 ; 40 } 41 std::string vae = argv [1 ],lat = argv [2 ],outp = argv [3 ],dump_dir ,stats_path ; 42int repeat = 1 ,device = -1 ,stream = 1 ,band_rows = 0 ,threads = 1 ; 43for (int i = 4 ;i + 1 < argc ;i += 2 ) { 44if (!std::strcmp (argv [i ],"--dump" ))dump_dir = argv [i + 1 ]; 45else if (!std::strcmp (argv [i ],"--repeat" ))repeat = std::atoi (argv [i + 1 ]); 46else if (!std::strcmp (argv [i ],"--device" ))device = std::atoi (argv [i + 1 ]); 47else if (!std::strcmp (argv [i ],"--stream" ))stream = !std::strcmp (argv [i + 1 ],"on" ); 48else if (!std::strcmp (argv [i ],"--stats" ))stats_path = argv [i + 1 ]; 49else if (!std::strcmp (argv [i ],"--band-rows" ))band_rows = std::atoi (argv [i + 1 ]); 50else if (!std::strcmp (argv [i ],"--threads" ))threads = std::atoi (argv [i + 1 ]); 51 } 52 std::string shp = lat .substr (0 ,lat .size ()- 4 )+ ".shape" ; 53 std::ifstream sf (shp ); 54int w = 0 ,h = 0 ,c = 0 ; 55if (!(sf >>w >>h >>c )|| c != 64 ) { 56 std::fprintf (stderr ,"cannot read %s (expected \"w h 64 1\")\n" ,shp .c_str ()); 57return 2 ; 58 } 59 std::vector < float > latent ; 60if (!read_file (lat ,latent , (size_t )64 * h * w )) { 61 std::fprintf (stderr ,"cannot read %s\n" ,lat .c_str ()); 62return 2 ; 63 } 64wan_vae_denormalize (latent .data (),h ,w ); 65 std::string err ; 66const auto t_open = std::chrono::steady_clock::now (); 67WanVae * v = wan_vae_open (vae ,device ,& err ,threads ); 68if (!v ) { 69 std::fprintf (stderr ,"%s\n" ,err .c_str ()); 70return 3 ; 71 } 72 std::printf ("open %.1f ms (%s)\n" , std::chrono::duration < double , std::milli > (std::chrono::steady_clock::now ()- t_open ).count (), 73wan_vae_open_report (v ).c_str ()); 74wan_vae_set_streaming (v ,stream != 0 ); 75if (band_rows > 0 )wan_vae_set_band_rows (v ,band_rows ); 76if (!stats_path .empty ())wan_vae_set_range_stats (v , true); 77if (!dump_dir .empty ()) { 78wan_vae_set_dump (v , [& ](const char * name ,const float * data ,int H ,int W ,int C ) { 79 std::string b = dump_dir + "/" + name ; 80if (FILE * f = std::fopen ((b + ".f32" ).c_str (),"wb" )) { std::fwrite (data ,4 , (size_t )H * W * C ,f ); std::fclose (f ); } 81if (FILE * f = std::fopen ((b + ".shape" ).c_str (),"w" )) { std::fprintf (f ,"%d %d %d\n" ,H ,W ,C ); std::fclose (f ); } 82 }); 83 } 84const size_t n_out = (size_t )4 * 16 * h * 16 * w ; 85 std::vector < float > out (n_out ),first ; 86int rc = 0 ; 87for (int r = 0 ;r < repeat ;++ r ) { 88WanVaeStats st ; 89err = wan_vae_decode (v ,latent .data (),h ,w ,out .data (),& st ); 90if (!err .empty ()) { 91 std::fprintf (stderr ,"%s\n" ,err .c_str ()); 92rc = 4 ; 93break ; 94 } 95bool same = true; 96if (r == 0 )first = out ; 97else same = std::memcmp (first .data (),out .data (),n_out * 4 )== 0 ; 98 std::printf ("decode %dx%d run %d: %.1f ms, peak %.0f MiB (weights+work %.0f MiB)%s\n" ,16 * w ,16 * h ,r , 99st .decode_ms ,st .peak_bytes /1048576.0 ,st .weight_bytes /1048576.0 , 100r == 0 ?"" : (same ?", identical to run 0" :", DIFFERS from run 0" )); 101if (!same )rc = 5 ; 102if (r == 0 )wan_vae_set_dump (v ,nullptr ); 103 } 104if (rc == 0 || rc == 5 ) { 105FILE * f = std::fopen (outp .c_str (),"wb" ); 106if (!f || std::fwrite (first .data (),4 ,n_out ,f )!= n_out )rc = 6 ; 107if (f ) std::fclose (f ); 108 } 109if (!stats_path .empty ()) {// the range report of all runs, one line per convolution 110if (FILE * f = std::fopen (stats_path .c_str (),"w" )) { std::fputs (wan_vae_range_report (v ).c_str (),f ); std::fclose (f ); } 111 } 112wan_vae_close (v ); 113return rc ; 114}