okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
e4309aaf3
main
1// src/image/dit/qi21_dit_step_main.cpp - test program strata-dit-step (not shipped): one Qwen-Image-2.1 DiT step in 2// Strata's own code (include/strata/image/qi21_dit.hpp) on the inputs stable-diffusion.cpp gave its DiT. 3// 4// strata-dit-step <dit.gguf> <out dir> <dump dir> [<dump dir> ...] [--repeat N] [--call K] [--f16-resident] 5// 6// Each dump dir holds the stage-hook dumps of one picture (tools/image/gate/stage_hooks.py): dit_x_<K> (the latent, 7// shape "W H C 1"), dit_t_<K> (the timestep) and cond (shape "context L"). Writes <out dir>/<dump parent name>/<dump name>/vel.bin, the 8// velocity as sd.cpp's vel_<K>.bin ([C][H][W] float32), and vel.shape. With --repeat N the step runs N times on the 9// same inputs (the first runs the text prefix, the rest reuse it, like sampling steps 2..8) and every run must give 10// the same bytes. Prints the open report, per run the step and prefix time, the memory peak of the DiT's own buffers 11// against the GGUF size, and DIT-STEP PASS / FAIL (FAIL = an error or runs that differ). 12#include "strata/image/qi21_dit.hpp" 13 14#include <algorithm> 15#include <cstdio> 16#include <cstdlib> 17#include <cstring> 18#include <filesystem> 19#include <fstream> 20#include <string> 21#include <vector> 22 23namespace fs= std::filesystem; 24using namespace strata::image ; 25 26namespace { 27 28bool read_f32 (const fs::path & p , std::vector < float >& v ) { 29 std::ifstream f (p , std::ios::binary ); 30if (!f )return false; 31 std::vector < char > raw ((std::istreambuf_iterator < char > (f )), {}); 32v .resize (raw .size () /4 ); 33 std::memcpy (v .data (),raw .data (),v .size ()* 4 ); 34return true; 35} 36 37std::vector < long long > read_shape (const fs::path & p ) { 38 std::vector < long long > s ; 39 std::ifstream f (p ); 40long long x ; 41while (f >>x )s .push_back (x ); 42return s ; 43} 44 45}// namespace 46 47int main (int argc ,char ** argv ) { 48if (argc < 4 ) { 49 std::fprintf (stderr ,"usage: strata-dit-step <dit.gguf> <out dir> <dump dir> [...] [--repeat N] [--call K]\n" ); 50return 2 ; 51 } 52int repeat = 1 ,call = 0 ,flags = 0 ; 53 std::vector < fs::path > dumps ; 54for (int i = 3 ;i < argc ;++ i ) { 55if (std::strcmp (argv [i ],"--repeat" )== 0 && i + 1 < argc )repeat = std::max (1 , std::atoi (argv [++ i ])); 56else if (std::strcmp (argv [i ],"--call" )== 0 && i + 1 < argc )call = std::atoi (argv [++ i ]); 57else if (std::strcmp (argv [i ],"--f16-resident" )== 0 )flags |=QI21_DIT_F16_RESIDENT ; 58else dumps .emplace_back (argv [i ]); 59 } 60const unsigned long long gguf_bytes = fs::file_size (argv [1 ]); 61 std::string err ; 62Qi21Dit * dit = qi21_dit_open (argv [1 ],-1 ,& err ,flags ); 63if (!dit ) { 64 std::fprintf (stderr ,"%s\n" ,err .c_str ()); 65 std::printf ("DIT-STEP FAIL\n" ); 66return 2 ; 67 } 68 std::printf ("open: %s\n" ,qi21_dit_open_report (dit ).c_str ()); 69int bad = 0 ; 70size_t peak = 0 ; 71for (const auto & d :dumps ) { 72const std::string tag = d .parent_path ().filename ().string ()+ "/" + d .filename ().string (); 73const std::string xs = "dit_x_" + std::to_string (call ),ts = "dit_t_" + std::to_string (call ); 74 std::vector < float > x ,t ,cond ; 75const auto sx = read_shape (d / (xs + ".shape" )),sc = read_shape (d /"cond.shape" ); 76if (!read_f32 (d / (xs + ".bin" ),x )|| !read_f32 (d / (ts + ".bin" ),t )|| !read_f32 (d /"cond.bin" ,cond )|| 77sx .size ()< 3 || sc .size ()< 2 || t .empty ()) { 78 std::printf ("%s: inputs missing\n" ,tag .c_str ()); 79++ bad ; 80continue ; 81 } 82const int W = (int )sx [0 ],H = (int )sx [1 ],C = (int )sx [2 ],ctx = (int )sc [0 ],L = (int )sc [1 ]; 83if (C != qi21_dit_in_channels (dit )|| ctx != qi21_dit_context_dim (dit )|| x .size ()!= (size_t )W * H * C || 84cond .size ()!= (size_t )ctx * L ) { 85 std::printf ("%s: shapes do not match the model\n" ,tag .c_str ()); 86++ bad ; 87continue ; 88 } 89const int oc = qi21_dit_out_channels (dit ); 90 std::vector < float > vel ((size_t )oc * W * H ),first ; 91 std::vector < double > later ;// step times of runs 2.. (the text prefix reused, as in sampling steps 2..8) 92for (int r = 0 ;r < repeat ;++ r ) { 93Qi21DitStats st ; 94err = qi21_dit_step (dit ,x .data (),W ,H ,cond .data (),L ,t [0 ],vel .data (),& st ); 95if (!err .empty ()) { 96 std::printf ("%s: %s\n" ,tag .c_str (),err .c_str ()); 97++ bad ; 98break ; 99 } 100peak = std::max (peak ,st .peak_bytes ); 101if (r > 0 )later .push_back (st .step_ms ); 102 std::printf ("%s run %d: %dx%d latent, %d text tokens, t %.4f: step %.1f ms%s; weights %.3f GB, work %.3f GB, " 103"peak %.3f GB (GGUF %.3f GB)\n" , 104tag .c_str (),r + 1 ,W ,H ,L ,t [0 ],st .step_ms , 105st .prefix_ran ? (" (text prefix " + std::to_string ((int )st .prefix_ms )+ " ms of it)" ).c_str () :"" , 106st .weight_bytes /1e9 ,st .work_bytes /1e9 ,st .peak_bytes /1e9 ,gguf_bytes /1e9 ); 107if (r == 0 ) { 108first = vel ; 109 }else if (std::memcmp (first .data (),vel .data (),vel .size ()* 4 )!= 0 ) { 110 std::printf ("%s run %d: DIFFERS from run 1\n" ,tag .c_str (),r + 1 ); 111++ bad ; 112 } 113 } 114if (!later .empty ()) { 115 std::sort (later .begin (),later .end ()); 116 std::printf ("%s: median step of runs 2..%d: %.1f ms (min %.1f, max %.1f)\n" ,tag .c_str (),repeat , 117later [later .size () /2 ],later .front (),later .back ()); 118 } 119const fs::path od = fs::path (argv [2 ]) /d .parent_path ().filename () /d .filename (); 120 fs::create_directories (od ); 121 std::ofstream (od /"vel.bin" , std::ios::binary ).write (reinterpret_cast < const char *> (first .data ()),first .size ()* 4 ); 122 std::ofstream (od /"vel.shape" ) <<W <<" " <<H <<" " <<oc <<" 1\n" ; 123 } 124 std::printf ("peak of the DiT's own device memory: %.3f GB (GGUF %.3f GB)\n" ,peak /1e9 ,gguf_bytes /1e9 ); 125qi21_dit_close (dit ); 126 std::printf ("DIT-STEP %s\n" ,bad == 0 ?"PASS" :"FAIL" ); 127return bad == 0 ?0 :1 ; 128}