okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
11c8c701a
main
1// src/image/te/qwen3vl_te_encode_main.cpp - test program strata-te-encode (not shipped): Strata's text encoder on token 2// ids that stable-diffusion.cpp built (tools/image/gate/stage_hooks.py dumps tokens.bin + tokens.start). 3// 4// strata-te-encode <te.gguf> <src dir> <dst dir> [--repeat N] [--mode auto|resident|streamed] 5// 6// For every <src dir>/dump_*/ with tokens.bin: the condition sd.cpp's LLMEmbedder would return for those ids (our 7// encoder's rows from tokens.start on; the gate prompts carry no token weights) -> <dst dir>/dump_*/cond.bin (float32, 8// [n][hidden]) and cond.shape ("hidden n", as the stage hooks write it). With --repeat N each prompt is encoded N times 9// and every repeat must give the same bytes. Prints the open time, per prompt the encode time and the peak device 10// memory, and TE-ENCODE PASS / FAIL. 11#include "strata/image/qwen3vl_te.hpp" 12 13#include <algorithm> 14#include <cstdio> 15#include <cstring> 16#include <filesystem> 17#include <fstream> 18#include <string> 19#include <vector> 20 21namespace fs= std::filesystem; 22using namespace strata::image ; 23 24int main (int argc ,char ** argv ) { 25if (argc < 4 ) { 26 std::fprintf (stderr ,"usage: strata-te-encode <te.gguf> <src dir> <dst dir> [--repeat N]\n" ); 27return 2 ; 28 } 29int repeat = 1 ,mode = 0 ; 30for (int i = 4 ;i + 1 < argc ;++ i ) { 31if (std::strcmp (argv [i ],"--repeat" )== 0 )repeat = std::max (1 , std::atoi (argv [i + 1 ])); 32if (std::strcmp (argv [i ],"--mode" )== 0 ) 33mode = std::strcmp (argv [i + 1 ],"resident" )== 0 ?1 : std::strcmp (argv [i + 1 ],"streamed" )== 0 ?2 :0 ; 34 } 35 std::string err ; 36Qwen3VlTe * te = qwen3vl_te_open (argv [1 ],-1 ,& err ,mode ); 37if (!te ) { 38 std::fprintf (stderr ,"%s\n" ,err .c_str ()); 39return 2 ; 40 } 41 std::printf ("open: %s\n" ,qwen3vl_te_open_report (te ).c_str ()); 42const int C = qwen3vl_te_hidden (te ); 43 std::vector < fs::path > dirs ; 44for (const auto & e : fs::directory_iterator (argv [2 ])) 45if (e .is_directory ()&& fs::exists (e .path () /"tokens.bin" ))dirs .push_back (e .path ()); 46 std::sort (dirs .begin (),dirs .end ()); 47int bad = 0 ; 48for (const auto & d :dirs ) { 49 std::vector < int32_t > ids ; 50 std::vector < char > raw ; 51 { 52 std::ifstream g (d /"tokens.bin" , std::ios::binary ); 53raw .assign (std::istreambuf_iterator < char > (g ), {}); 54 } 55ids .resize (raw .size () /4 ); 56 std::memcpy (ids .data (),raw .data (),ids .size ()* 4 ); 57int start = 0 ; 58 std::ifstream (d /"tokens.start" ) >>start ; 59const int n = (int )ids .size (); 60 std::vector < float > out ((size_t )n * C ),first ; 61bool same = true; 62double ms0 = 0 ; 63size_t peak = 0 ; 64for (int r = 0 ;r < repeat ;++ r ) { 65Qwen3VlTeStats st ; 66err = qwen3vl_te_encode (te ,ids .data (),n ,out .data (),& st ); 67if (!err .empty ()) { 68 std::fprintf (stderr ,"%s: %s\n" ,d .filename ().string ().c_str (),err .c_str ()); 69qwen3vl_te_close (te ); 70return 2 ; 71 } 72if (r == 0 ) { 73first = out ; 74ms0 = st .encode_ms ; 75peak = st .peak_bytes ; 76 }else { 77same = same && std::memcmp (first .data (),out .data (),out .size ()* sizeof (float ))== 0 ; 78 std::printf ("%s repeat %d: %.1f ms\n" ,d .filename ().string ().c_str (),r ,st .encode_ms ); 79 } 80 } 81if (!same )++ bad ; 82const fs::path od = fs::path (argv [3 ]) /d .filename (); 83 fs::create_directories (od ); 84 std::ofstream (od /"cond.bin" , std::ios::binary ) 85 .write (reinterpret_cast < const char *> (first .data ()+ (size_t )start * C ), (std::streamsize ) ((size_t ) (n - start )* C * 4 )); 86 std::ofstream (od /"cond.shape" ) <<C <<" " << (n - start ) <<" \n" ; 87 std::printf ("%s: %d tokens (start %d), encode %.1f ms, peak %.0f MiB, repeats identical %s\n" , 88d .filename ().string ().c_str (),n ,start ,ms0 ,peak /1048576.0 ,same ?"yes" :"NO" ); 89 } 90qwen3vl_te_close (te ); 91 std::printf ("TE-ENCODE %s (%zu prompts)\n" ,bad || dirs .empty () ?"FAIL" :"PASS" ,dirs .size ()); 92return bad || dirs .empty () ?1 :0 ; 93}