okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0479b72e9
main
1// tools/vision/own/test_image_prep.cpp - own_prep_check: our picture preparation against mtmd's, bit for bit. 2// 3// strata-vision-own-prep-check <mmproj.gguf> <oracle dir> <min_tokens> <max_tokens> <image> [<image> ...] 4// The oracle files come from strata-vision-prep-oracle (oracle_prep.cpp) run with the same limits. For every image: our decode, smart_resize, 5// bicubic resize with padding and normalisation must reproduce mtmd's float buffer exactly (same size, every value equal). 6// Without images it only runs the self-checks of smart_resize. 7#include "image_prep.h" 8 9#include <cmath> 10#include <cstdio> 11#include <cstdlib> 12#include <cstring> 13#include <string> 14#include <vector> 15 16using namespace strata_vit ; 17 18static int g_fail = 0 ; 19#define CHECK (c ) do { if (!(c)) { std::printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); ++g_fail; } } while (0) 20 21static std::string base_name (const std::string & p ) { 22const size_t s = p .find_last_of ("/\\" ); 23return s == std::string::npos ?p :p .substr (s + 1 ); 24} 25 26int main (int argc ,char ** argv ) { 27// --- smart_resize self-checks (the 32-aligned sides and the pixel bounds) --- 28 { 29const int align = 32 ,mn = 8 * 1024 ,mx = 4096 * 1024 ; 30for (int w : {1 ,2 ,31 ,33 ,64 ,100 ,640 ,800 ,1000 ,1024 ,3000 ,4000 ,8000 ,20000 }) 31for (int h : {1 ,3 ,32 ,45 ,90 ,400 ,450 ,517 ,1024 ,3000 ,8000 }) { 32const Size2 s = smart_resize (w ,h ,align ,mn ,mx ); 33CHECK (s .w >=align && s .h >=align && s .w %align == 0 && s .h %align == 0 ); 34CHECK ((long long )s .w * s .h <= (long long )mx + 2LL * align * (s .w + s .h ));// the floor of each side can only shrink it 35if ((long long )w * h * 4 >=mn && (long long )w * h <=mx /2 )CHECK ((long long )s .w * s .h >=mn /2 ); 36 } 37CHECK (smart_resize (640 ,400 ,32 ,8192 ,4194304 )== (Size2 {640 ,416 })); 38CHECK (smart_resize (1024 ,1024 ,32 ,8192 ,4194304 )== (Size2 {1024 ,1024 })); 39CHECK (smart_resize (64 ,48 ,32 ,8192 ,4194304 ).w * smart_resize (64 ,48 ,32 ,8192 ,4194304 ).h >=8192 ); 40CHECK (smart_resize (0 ,5 ,32 ,8192 ,4194304 )== (Size2 {0 ,0 })); 41 } 42if (argc < 6 ) { 43 std::printf (g_fail ?"own_prep_check: %d FAILED\n" :"own_prep_check: self-checks ok\n" ,g_fail ); 44return g_fail ?1 :0 ; 45 } 46 47const std::string mmproj = argv [1 ],dir = argv [2 ]; 48const int min_t = std::atoi (argv [3 ]),max_t = std::atoi (argv [4 ]); 49Gguf gg ; 50 std::string err ; 51VitGeometry geo ; 52if (!gg .open (mmproj ,err )|| !vit_geometry_from_gguf (gg ,geo ,err )) { std::printf ("FAIL %s: %s\n" ,mmproj .c_str (),err .c_str ());return 1 ; } 53 54int same = 0 ,total = 0 ; 55for (int a = 5 ;a < argc ;++ a ) { 56const std::string img = argv [a ]; 57++ total ; 58ImageU8 src ,resized ; 59PreparedImage prep ; 60if (!image_load_file (img ,src ,err )|| !prepare_image (src ,geo ,min_t ,max_t ,prep ,err ,& resized )) { 61 std::printf ("FAIL %s: %s\n" ,img .c_str (),err .c_str ()); 62++ g_fail ; 63continue ; 64 } 65const std::string path = dir + "/" + base_name (img )+ "." + std::to_string (min_t )+ "." + std::to_string (max_t )+ ".prep" ; 66FILE * f = std::fopen (path .c_str (),"rb" ); 67if (!f ) { std::printf ("FAIL %s: no oracle file %s\n" ,img .c_str (),path .c_str ());++ g_fail ;continue ; } 68int32_t hdr [2 ]= {0 ,0 }; 69if (std::fread (hdr ,sizeof hdr ,1 ,f )!= 1 ) { std::printf ("FAIL %s: oracle header\n" ,img .c_str ());++ g_fail ; std::fclose (f );continue ; } 70 std::vector < float > ref ((size_t )hdr [0 ]* hdr [1 ]* 3 ); 71const bool got = std::fread (ref .data (),sizeof (float ),ref .size (),f )== ref .size (); 72 std::fclose (f ); 73if (!got ) { std::printf ("FAIL %s: oracle data short\n" ,img .c_str ());++ g_fail ;continue ; } 74if (hdr [0 ]!= prep .w || hdr [1 ]!= prep .h ) { 75 std::printf ("FAIL %s: %dx%d -> ours %dx%d, mtmd %dx%d\n" ,img .c_str (),src .w ,src .h ,prep .w ,prep .h ,hdr [0 ],hdr [1 ]); 76++ g_fail ; 77continue ; 78 } 79const size_t n = (size_t )prep .w * prep .h ; 80size_t differ = 0 ; 81double maxd = 0 ; 82for (size_t i = 0 ;i < n ;++ i ) 83for (int c = 0 ;c < 3 ;++ c ) { 84const float x = prep .chw [c * n + i ],y = ref [i * 3 + c ]; 85if (std::memcmp (& x ,& y ,4 )!= 0 ) {++ differ ;maxd = std::fmax (maxd , std::fabs ((double )x - y )); } 86 } 87 std::printf ("%-28s %5dx%-5d -> %4dx%-4d (%d x %d tokens) %s" ,base_name (img ).c_str (),src .w ,src .h ,prep .w ,prep .h ,prep .w /geo .align (), 88prep .h /geo .align (),differ ?"DIFFERS" :"identical" ); 89if (differ ) std::printf (": %zu of %zu values, max |diff| %g" ,differ ,n * 3 ,maxd ); 90 std::printf ("\n" ); 91if (differ )++ g_fail ;else ++ same ; 92 } 93 std::printf ("own_prep_check: %d of %d pictures identical to mtmd (min_tokens %d, max_tokens %d)%s\n" ,same ,total ,min_t ,max_t ,g_fail ?", FAILED" :"" ); 94return g_fail ?1 :0 ; 95}