okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0479b72e9
main
1// tools/vision/own/oracle_prep.cpp - mtmd's side of the picture-preparation check (built in tools/vision's mtmd build only, EXCLUDE_FROM_ALL). 2// 3// strata-vision-prep-oracle <mmproj.gguf> <out dir> <min_tokens> <max_tokens> <image> [<image> ...] 4// For each image: decodes it with mtmd's own helper, runs mtmd's Qwen preprocessor (the same class mtmd_tokenize uses) with the given token 5// limits (0 = the tower's default), and writes <out dir>/<image file name>.<min>.<max>.prep: int32 {w, h}, then float32 [h][w][3] (what the 6// tower is given: resized, padded, normalised). `own_prep_check` (test_image_prep.cpp) compares its own result with these files, bit for bit. 7#include "clip.h" 8#include "clip-impl.h" 9#include "mtmd.h" 10#include "mtmd-helper.h" 11#include "mtmd-image.h" 12 13#include <cstdio> 14#include <cstdlib> 15#include <cstring> 16#include <string> 17#include <vector> 18 19static std::string base_name (const std::string & p ) { 20const size_t s = p .find_last_of ("/\\" ); 21return s == std::string::npos ?p :p .substr (s + 1 ); 22} 23 24int main (int argc ,char ** argv ) { 25if (argc < 6 ) { std::fprintf (stderr ,"usage: strata-vision-prep-oracle <mmproj> <out dir> <min_tokens> <max_tokens> <image>...\n" );return 2 ; } 26const std::string mmproj = argv [1 ],out_dir = argv [2 ]; 27const int min_t = std::atoi (argv [3 ]),max_t = std::atoi (argv [4 ]); 28 29clip_context_params cp {}; 30cp .use_gpu = false; 31cp .flash_attn_type = CLIP_FLASH_ATTN_TYPE_DISABLED ; 32cp .image_min_tokens = min_t > 0 ?min_t :-1 ; 33cp .image_max_tokens = max_t > 0 ?max_t :-1 ; 34cp .warmup = false; 35clip_init_result r = clip_init (mmproj .c_str (),cp ); 36if (!r .ctx_v ) { std::fprintf (stderr ,"cannot load the vision encoder %s\n" ,mmproj .c_str ());return 1 ; } 37mtmd_image_preprocessor_dyn_size pre (r .ctx_v ); 38 39// a bitmap decoder context is not needed for stb images: the helper takes a null mtmd_context only for audio/video, which these are not 40int rc = 0 ; 41for (int a = 5 ;a < argc ;++ a ) { 42const std::string img = argv [a ]; 43// mtmd's own decode (stb_image, 3 channels) 44FILE * f = std::fopen (img .c_str (),"rb" ); 45if (!f ) { std::fprintf (stderr ,"cannot open %s\n" ,img .c_str ());rc = 1 ;continue ; } 46 std::vector < unsigned char > buf ; 47unsigned char tmp [1 <<16 ]; 48size_t k ; 49while ((k = std::fread (tmp ,1 ,sizeof tmp ,f ))> 0 )buf .insert (buf .end (),tmp ,tmp + k ); 50 std::fclose (f ); 51mtmd_helper_bitmap_wrapper bw = mtmd_helper_bitmap_init_from_buf (nullptr ,buf .data (),buf .size (), false,mtmd_helper_init_opt_default ()); 52if (!bw .bitmap ) { std::fprintf (stderr ,"cannot decode %s\n" ,img .c_str ());rc = 1 ;continue ; } 53clip_image_u8 u8 ; 54u8 .set_size ({(int )mtmd_bitmap_get_nx (bw .bitmap ), (int )mtmd_bitmap_get_ny (bw .bitmap )}, false); 55u8 .cpy_buf (std::vector < uint8_t > (mtmd_bitmap_get_data (bw .bitmap ),mtmd_bitmap_get_data (bw .bitmap )+ mtmd_bitmap_get_n_bytes (bw .bitmap ))); 56mtmd_image_preproc_out out = pre .preprocess (u8 ); 57if (out .entries .size ()!= 1 ) { std::fprintf (stderr ,"%s: %zu entries\n" ,img .c_str (),out .entries .size ());rc = 1 ;continue ; } 58const clip_image_f32 & e = out .entries [0 ]; 59const std::string path = out_dir + "/" + base_name (img )+ "." + std::to_string (min_t )+ "." + std::to_string (max_t )+ ".prep" ; 60FILE * o = std::fopen (path .c_str (),"wb" ); 61if (!o ) { std::fprintf (stderr ,"cannot write %s\n" ,path .c_str ());rc = 1 ;continue ; } 62const int32_t hdr [2 ]= {e .nx (),e .ny ()}; 63 std::fwrite (hdr ,sizeof hdr ,1 ,o ); 64const auto & b = e .get_ro_buf (); 65 std::fwrite (b .data (),sizeof (float ),b .size (),o ); 66 std::fclose (o ); 67 std::printf ("%s: %dx%d -> %dx%d\n" ,img .c_str (),u8 .get_size ().width ,u8 .get_size ().height ,e .nx (),e .ny ()); 68mtmd_bitmap_free (bw .bitmap ); 69 } 70clip_free (r .ctx_v ); 71return rc ; 72}