okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e4309aaf3
main
1// src/image/pipeline/qi21_glue_check_main.cpp - test program strata-glue-check (not shipped): the steps of 2// include/strata/image/qi21_image.hpp on given inputs, for tools/test_qi21_glue.py (numpy float32 expectations) and 3// for the check against stable-diffusion.cpp's own stage dumps. 4// 5// strata-glue-check latent <noise.bin> <sigma0> <out.bin> 6// strata-glue-check cond <hidden.bin> <n> <hidden_dim> <weights.bin> <n_system> <out.bin> 7// strata-glue-check unit <in.bin> <out.bin> 8// strata-glue-check dumps <dump dir> [...] noise.bin -> qi21_initial_latent(noise, 1.0) == dit_x_0.bin 9// byte for byte (prints GLUE-DUMPS PASS / FAIL) 10#include "strata/image/qi21_image.hpp" 11 12#include <cstdio> 13#include <cstdlib> 14#include <cstring> 15#include <fstream> 16#include <string> 17#include <vector> 18 19using namespace strata::image ; 20 21namespace { 22 23bool read_f32 (const std::string & p , std::vector < float >& v ) { 24 std::ifstream f (p , std::ios::binary ); 25if (!f )return false; 26 std::vector < char > raw ((std::istreambuf_iterator < char > (f )), {}); 27v .resize (raw .size () /4 ); 28 std::memcpy (v .data (),raw .data (),v .size ()* 4 ); 29return true; 30} 31 32bool write_f32 (const std::string & p ,const std::vector < float >& v ) { 33 std::ofstream f (p , std::ios::binary ); 34f .write (reinterpret_cast < const char *> (v .data ()), (std::streamsize ) (v .size ()* 4 )); 35return (bool )f ; 36} 37 38}// namespace 39 40int main (int argc ,char ** argv ) { 41const std::string mode = argc > 1 ?argv [1 ] :"" ; 42 std::vector < float > a ,w ; 43if (mode == "latent" && argc == 5 ) { 44if (!read_f32 (argv [2 ],a ))return 2 ; 45return write_f32 (argv [4 ],qi21_initial_latent (a , std::strtof (argv [3 ],nullptr ))) ?0 :2 ; 46 } 47if (mode == "cond" && argc == 8 ) { 48if (!read_f32 (argv [2 ],a )|| !read_f32 (argv [5 ],w ))return 2 ; 49const int n = std::atoi (argv [3 ]),hd = std::atoi (argv [4 ]),ns = std::atoi (argv [6 ]); 50if ((size_t )n * hd != a .size ()|| (size_t )n != w .size ())return 2 ; 51return write_f32 (argv [7 ],qi21_condition (std::move (a ),n ,hd ,w ,ns )) ?0 :2 ; 52 } 53if (mode == "unit" && argc == 4 ) { 54if (!read_f32 (argv [2 ],a ))return 2 ; 55qi21_picture_to_unit (a .data (),a .size ()); 56return write_f32 (argv [3 ],a ) ?0 :2 ; 57 } 58if (mode == "dumps" && argc >=3 ) { 59int bad = 0 ; 60for (int i = 2 ;i < argc ;++ i ) { 61const std::string d = argv [i ]; 62 std::vector < float > noise ,x0 ; 63if (!read_f32 (d + "/noise.bin" ,noise )|| !read_f32 (d + "/dit_x_0.bin" ,x0 )) { 64 std::printf ("%s: dumps missing\n" ,d .c_str ()); 65++ bad ; 66continue ; 67 } 68const std::vector < float > got = qi21_initial_latent (noise ,1.0f ); 69size_t differ = got .size ()== x0 .size () ?0 :got .size (); 70for (size_t k = 0 ;k < got .size ()&& k < x0 .size ();++ k )differ += std::memcmp (& got [k ],& x0 [k ],4 )!= 0 ; 71 std::printf ("%s: starting latent %s (%zu of %zu values differ)\n" ,d .c_str (), 72differ ?"DIFFERS" :"byte-identical to dit_x_0" ,differ ,x0 .size ()); 73bad += differ != 0 ; 74 } 75 std::printf ("GLUE-DUMPS %s\n" ,bad ?"FAIL" :"PASS" ); 76return bad ?1 :0 ; 77 } 78 std::fprintf (stderr ,"usage: see the head of src/image/pipeline/qi21_glue_check_main.cpp\n" ); 79return 2 ; 80}