okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
870ea9763
main
1// tools/vision/own/test_gguf_geometry.cpp - the GGUF reader and the geometry, on files made here and (given paths) on real mmproj files. 2// 3// strata-vision-own-test [mmproj.gguf ...] 4// Exit 0 when every check passes. A real mmproj given on the command line is parsed and its geometry printed; the two known Qwen 5// towers (27 layers, width 1152, out 2560 or 2048) are also checked against the numbers read from their headers. 6#include "own_reader.h" 7#include "vit_geometry.h" 8#include "vit_gpu.h" 9 10#include <cmath> 11#include <cstdio> 12#include <cstdlib> 13#include <cstring> 14#include <string> 15#include <vector> 16 17using namespace strata_vit ; 18 19static int g_fail = 0 ; 20#define CHECK (c ) do { if (!(c)) { std::printf("FAIL %s:%d: %s\n", __FILE__, __LINE__, #c); ++g_fail; } } while (0) 21 22// ---- a tiny GGUF writer ---- 23struct W { 24 std::vector < unsigned char > b ; 25template < class T > void put (T v ) {const unsigned char * p = (const unsigned char * )& v ;b .insert (b .end (),p ,p + sizeof v ); } 26void str (const std::string & s ) {put < uint64_t > (s .size ());b .insert (b .end (),s .begin (),s .end ()); } 27void raw (const void * p ,size_t n ) {b .insert (b .end (), (const unsigned char * )p , (const unsigned char * )p + n ); } 28}; 29 30static uint16_t f32_to_f16 (float f ) {// good enough for the values used here (normal range, round toward zero) 31uint32_t u ; std::memcpy (& u ,& f ,4 ); 32const uint32_t sign = (u >>16 )& 0x8000u ; 33int exp = (int ) ((u >>23 )& 0xFF )- 127 + 15 ; 34const uint32_t man = (u >>13 )& 0x3FFu ; 35if (f == 0.0f )return (uint16_t )sign ; 36return (uint16_t ) (sign | ((uint32_t )exp <<10 ) |man ); 37} 38 39int main (int argc ,char ** argv ) { 40// --- 0. the GPU classes where `--tower auto` keeps mtmd (vit_gpu.h) --- 41for (int mt : {0 ,300 ,1024 }) {// at or below the default limit: Pascal 6.1 / 6.2 keep mtmd (not measured); cc 6.0 takes ours with F16 and BF16 files 42CHECK (vit_gpu_slower_nvidia (6 ,1 , false,mt )&& vit_gpu_slower_nvidia (6 ,1 , true,mt )&& vit_gpu_slower_nvidia (6 ,2 , false,mt )&& vit_gpu_slower_nvidia (6 ,2 , true,mt )); 43CHECK (!vit_gpu_slower_nvidia (6 ,0 , true,mt )&& !vit_gpu_slower_nvidia (6 ,0 , false,mt )); 44CHECK (!vit_gpu_slower_nvidia (5 ,2 , false,mt )&& !vit_gpu_slower_nvidia (7 ,0 , false,mt )&& !vit_gpu_slower_nvidia (8 ,6 , true,mt )&& !vit_gpu_slower_nvidia (12 ,0 , false,mt )); 45 } 46for (int mt : {1025 ,4096 ,16384 }) {// above it: ours on every CUDA class 47CHECK (!vit_gpu_slower_nvidia (6 ,0 , false,mt )&& !vit_gpu_slower_nvidia (6 ,1 , false,mt )&& !vit_gpu_slower_nvidia (6 ,1 , true,mt )&& !vit_gpu_slower_nvidia (6 ,2 , true,mt )); 48CHECK (!vit_gpu_slower_nvidia (6 ,0 , true,mt )&& !vit_gpu_slower_nvidia (8 ,6 , true,mt )&& !vit_gpu_slower_nvidia (12 ,0 , false,mt )); 49 } 50CHECK (vit_gpu_is_gfx1151 ("gfx1151" )&& vit_gpu_is_gfx1151 ("gfx1151:xnack-" )); 51CHECK (!vit_gpu_is_gfx1151 ("gfx1150" )&& !vit_gpu_is_gfx1151 ("gfx1201" )&& !vit_gpu_is_gfx1151 ("gfx11510" )&& !vit_gpu_is_gfx1151 ("gfx115" )&& !vit_gpu_is_gfx1151 ("" )); 52// Windows HIP keeps mtmd under auto except on gfx1151 (Strix, measured with that package; owner 11-11); Linux HIP never 53CHECK (!vit_gpu_hip_windows_not_measured (true,"gfx1151" )&& !vit_gpu_hip_windows_not_measured (true,"gfx1151:xnack-" )); 54CHECK (vit_gpu_hip_windows_not_measured (true,"gfx1201" )&& vit_gpu_hip_windows_not_measured (true,"gfx1100" )&& vit_gpu_hip_windows_not_measured (true,"gfx1150" )); 55CHECK (!vit_gpu_hip_windows_not_measured (false,"gfx1151" )&& !vit_gpu_hip_windows_not_measured (false,"gfx1201" )); 56 {// the context retry (vit_gpu.h): only "out of memory", only an integrated GPU, only a Linux CUDA build, at most kVitCtxRetries times 57CHECK (kVitCtxRetries == 12 && kVitCtxWaitMs == 1000 ); 58for (int done = 0 ;done < kVitCtxRetries ;++ done )CHECK (vit_gpu_retry_context (true, true, true,done )); 59CHECK (!vit_gpu_retry_context (true, true, true,kVitCtxRetries )&& !vit_gpu_retry_context (true, true, true,kVitCtxRetries + 5 )&& !vit_gpu_retry_context (true, true, true,-1 )); 60CHECK (!vit_gpu_retry_context (false, true, true,0 ));// HIP, Windows, Metal: no retry 61CHECK (!vit_gpu_retry_context (true, false, true,0 ));// a discrete card: the failure is real 62CHECK (!vit_gpu_retry_context (true, true, false,0 ));// any other error (no device, driver too old): no retry 63int tries = 0 ;// a start that fails 3 times and then works waits 3 times; one that never works gives up after 8 64for (int fails = 3 ;vit_gpu_retry_context (true, true, true,tries )&& tries < fails ; )++ tries ; 65CHECK (tries == 3 ); 66tries = 0 ; 67while (vit_gpu_retry_context (true, true, true,tries ))++ tries ; 68CHECK (tries == kVitCtxRetries ); 69 } 70 {// the default tower (no --tower): auto, or STRATA_VISION_TOWER's value when it is a known one 71 std::string note ; 72CHECK (vision_tower_default (nullptr ,note )== "auto" && note .empty ()); 73CHECK (vision_tower_default ("" ,note )== "auto" && note .empty ()); 74CHECK (vision_tower_default ("mtmd" ,note )== "mtmd" && vision_tower_default ("own" ,note )== "own" && vision_tower_default ("auto" ,note )== "auto" && note .empty ()); 75CHECK (vision_tower_default ("llama" ,note )== "auto" && note .find ("STRATA_VISION_TOWER=llama" )!= std::string::npos ); 76 } 77 {// `--tower auto` on the CPU path by CPU class (owner 11-11: ours below AVX2 too), for both reader files' geometry; the explicit settings are never overruled 78VitGeometry g38 ;// the Qwen3.8 tower (projection 2560) and the Qwen3.6 tower (2048): the two public mmproj files' numbers 79g38 .projector = "qwen3vl_merger" ;g38 .layers = 27 ;g38 .width = 1152 ;g38 .heads = 16 ;g38 .head_dim = 72 ;g38 .ffn = 4304 ;g38 .patch = 16 ;g38 .merge = 2 ; 80g38 .image_size = 768 ;g38 .pos_grid = 48 ;g38 .out_dim = 2560 ;g38 .patch_taps = 2 ;g38 .merger_in = 4608 ;g38 .merger_hidden = 4608 ; 81VitGeometry g36 = g38 ; 82g36 .out_dim = 2048 ; 83 quant::Caps none_ ;// an x86-64 CPU older than SSE4.1: the scalar tier 84 quant::Caps nehalem = none_ ;nehalem .sse41 = true;// no AVX: Nehalem, Westmere 85 quant::Caps sandy = nehalem ;sandy .avx = true;// AVX, no F16C: Sandy Bridge 86 quant::Caps ivy = sandy ;ivy .f16c = true;// AVX with F16C: Ivy Bridge 87 quant::Caps haswell = ivy ;haswell .avx2 = true;haswell .fma = true; 88 quant::Caps skylake_x = haswell ;skylake_x .avx512 = true; 89struct Cls {const char * name ;const quant::Caps * c ; quant::Isa tier ; }; 90const Cls classes []= {{"no SSE4.1" ,& none_ , quant::Isa ::Scalar }, {"no AVX" ,& nehalem , quant::Isa ::Sse41 }, {"AVX without F16C" ,& sandy , quant::Isa ::Avx }, 91 {"AVX with F16C" ,& ivy , quant::Isa ::Avx }, {"AVX2" ,& haswell , quant::Isa ::Avx2 }, {"AVX-512" ,& skylake_x , quant::Isa ::Avx512 }}; 92VitGeometry g_rms = g38 ; 93g_rms .norm = NormKind ::RMSNorm ;// a tower this code cannot run: `auto` keeps mtmd on every tier 94for (const Cls & k :classes ) { 95 std::string why ; 96#if defined(__x86_64__ )|| defined(_M_X64 ) 97const quant::Isa tier = quant::best_isa_for (* k .c ,nullptr ); 98if (tier != k .tier ) { std::printf ("FAIL tier of the %s class: %s\n" ,k .name , quant::isa_name (tier ));++ g_fail ; } 99#else 100const quant::Isa tier = k .tier ; 101#endif 102for (const VitGeometry * g : {& g38 ,& g36 }) { 103CHECK (own_cpu_reader_supported (* g ,tier ,why ));// ours on every tier, both reader files 104CHECK (vision_tower_resolve ("auto" ,own_cpu_reader_supported (* g ,tier ,why ))== "own" ); 105 } 106CHECK (!own_cpu_reader_supported (g_rms ,tier ,why )&& !why .empty ()); 107CHECK (vision_tower_resolve ("auto" ,own_cpu_reader_supported (g_rms ,tier ,why ))== "mtmd" ); 108 } 109#if defined(__x86_64__ )|| defined(_M_X64 ) 110// the tier mask (STRATA_FORCE_ISA) caps the tier a CPU runs, never raises it 111CHECK (quant::best_isa_for (skylake_x ,"sse41" )== quant::Isa ::Sse41 && quant::best_isa_for (skylake_x ,"avx" )== quant::Isa ::Avx && quant::best_isa_for (skylake_x ,"avx2" )== quant::Isa ::Avx2 ); 112CHECK (quant::best_isa_for (skylake_x ,"scalar" )== quant::Isa ::Scalar && quant::best_isa_for (sandy ,"avx2" )== quant::Isa ::Avx && quant::best_isa_for (nehalem ,"avx512" )== quant::Isa ::Sse41 ); 113CHECK (quant::isa_ok_for (ivy , quant::Isa ::Avx )&& !quant::isa_ok_for (ivy , quant::Isa ::Avx2 )&& !quant::isa_ok_for (nehalem , quant::Isa ::Avx )&& !quant::isa_ok_for (none_ , quant::Isa ::Sse41 )); 114#endif 115// the explicit settings, both ways, whatever `own_ok` says 116CHECK (vision_tower_resolve ("mtmd" , true)== "mtmd" && vision_tower_resolve ("mtmd" , false)== "mtmd" ); 117CHECK (vision_tower_resolve ("own" , true)== "own" && vision_tower_resolve ("own" , false)== "own" ); 118CHECK (vision_tower_resolve ("auto" , true)== "own" && vision_tower_resolve ("auto" , false)== "mtmd" ); 119 std::string note ; 120CHECK (vision_tower_resolve (vision_tower_default ("mtmd" ,note ), true)== "mtmd" && vision_tower_resolve (vision_tower_default ("own" ,note ), false)== "own" ); 121CHECK (vision_tower_resolve (vision_tower_default (nullptr ,note ), true)== "own" );// no setting at all: auto 122 } 123 {// a start failure under auto comes back as kOwnStartFailed with its reason (nothing on stdout), so strata-vision can start mtmd's reader 124OwnReaderOptions o ; 125o .mmproj = "/nonexistent/strata-own-test-mmproj.gguf" ; 126 std::string why ; 127o .start_error = & why ; 128CHECK (own_reader_run (o )== kOwnStartFailed && why .find ("cannot load the vision encoder" )!= std::string::npos ); 129 } 130 {// the test hook names one point at a time 131#ifdef _WIN32 132_putenv_s ("STRATA_VIT_TEST_FAIL" ,"load" ); 133#else 134setenv ("STRATA_VIT_TEST_FAIL" ,"load" ,1 ); 135#endif 136CHECK (own_reader_inject_failure ("load" )&& !own_reader_inject_failure ("gpu" )&& !own_reader_inject_failure ("warmup" )); 137#ifdef _WIN32 138_putenv_s ("STRATA_VIT_TEST_FAIL" ,"" ); 139#else 140unsetenv ("STRATA_VIT_TEST_FAIL" ); 141#endif 142CHECK (!own_reader_inject_failure ("load" )); 143 } 144const std::string tmp = std::string (std::getenv ("TMPDIR" ) ? std::getenv ("TMPDIR" ) :"." )+ "/strata_own_test_" + std::to_string ((long ) std::rand ())+ ".gguf" ; 145 146// --- 1. a file with every value kind and the four tensor types --- 147 { 148W w ; 149w .raw ("GGUF" ,4 );w .put < uint32_t > (3 );w .put < uint64_t > (4 );// tensors 150w .put < uint64_t > (9 );// keys 151auto kv_u32 = [& ](const char * k ,uint32_t v ) {w .str (k );w .put < uint32_t > (4 );w .put < uint32_t > (v ); }; 152kv_u32 ("a.u32" ,7 ); 153w .str ("a.neg" );w .put < uint32_t > (5 );w .put < int32_t > (-3 ); 154w .str ("a.f32" );w .put < uint32_t > (6 );w .put < float > (2.5f ); 155w .str ("a.bool" );w .put < uint32_t > (7 );w .put < uint8_t > (1 ); 156w .str ("a.str" );w .put < uint32_t > (8 );w .str ("hello" ); 157w .str ("a.u64" );w .put < uint32_t > (10 );w .put < uint64_t > (5000000000ull ); 158w .str ("a.arr_f" );w .put < uint32_t > (9 );w .put < uint32_t > (6 );w .put < uint64_t > (3 );w .put < float > (0.5f );w .put < float > (1.5f );w .put < float > (-2.0f ); 159w .str ("a.arr_b" );w .put < uint32_t > (9 );w .put < uint32_t > (7 );w .put < uint64_t > (4 );w .put < uint8_t > (0 );w .put < uint8_t > (1 );w .put < uint8_t > (1 );w .put < uint8_t > (0 ); 160w .str ("a.arr_s" );w .put < uint32_t > (9 );w .put < uint32_t > (8 );w .put < uint64_t > (2 );w .str ("x" );w .str ("yz" ); 161// tensor directory: f32 [4], f16 [4], bf16 [4], q8_0 [32, 2] 162uint64_t off = 0 ; 163auto dir = [& ](const char * n , std::vector < int64_t > ne ,uint32_t type ,uint64_t nbytes ) { 164w .str (n );w .put < uint32_t > ((uint32_t )ne .size ());for (int64_t d :ne )w .put < uint64_t > ((uint64_t )d );w .put < uint32_t > (type );w .put < uint64_t > (off ); 165off += (nbytes + 31 ) /32 * 32 ; 166 }; 167dir ("t.f32" , {4 },GT_F32 ,16 ); 168dir ("t.f16" , {4 },GT_F16 ,8 ); 169dir ("t.bf16" , {4 },GT_BF16 ,8 ); 170dir ("t.q8" , {32 ,2 },GT_Q8_0 ,68 ); 171while (w .b .size () %32 )w .b .push_back (0 ); 172auto pad = [& ]() {while (w .b .size () %32 )w .b .push_back (0 ); }; 173const float f [4 ]= {1.0f ,-2.0f ,0.25f ,3.5f }; 174w .raw (f ,16 );pad (); 175for (float x :f )w .put < uint16_t > (f32_to_f16 (x )); 176pad (); 177for (float x :f ) {uint32_t u ; std::memcpy (& u ,& x ,4 );w .put < uint16_t > ((uint16_t ) (u >>16 )); } 178pad (); 179for (int blk = 0 ;blk < 2 ;++ blk ) { 180w .put < uint16_t > (f32_to_f16 (0.5f * (blk + 1 ))); 181for (int j = 0 ;j < 32 ;++ j )w .put < int8_t > ((int8_t ) (j - 16 )); 182 } 183pad (); 184FILE * fp = std::fopen (tmp .c_str (),"wb" ); 185CHECK (fp != nullptr ); 186if (fp ) { std::fwrite (w .b .data (),1 ,w .b .size (),fp ); std::fclose (fp ); } 187 188Gguf g ; 189 std::string err ; 190CHECK (g .open (tmp ,err )); 191uint32_t u = 0 ;float fl = 0 ;bool bo = false; std::string s ; 192CHECK (g .get_u32 ("a.u32" ,u )&& u == 7 ); 193CHECK (!g .get_u32 ("a.neg" ,u )); 194CHECK (!g .get_u32 ("a.u64" ,u )); 195CHECK (!g .get_u32 ("a.f32" ,u )); 196CHECK (g .get_f32 ("a.f32" ,fl )&& fl == 2.5f ); 197CHECK (g .get_f32 ("a.neg" ,fl )&& fl == -3.0f ); 198CHECK (g .get_bool ("a.bool" ,bo )&& bo ); 199CHECK (g .get_str ("a.str" ,s )&& s == "hello" ); 200CHECK (!g .get_str ("a.u32" ,s )); 201 std::vector < float > af ; std::vector < int64_t > ai ; 202CHECK (g .get_f32_array ("a.arr_f" ,af )&& af .size ()== 3 && af [1 ]== 1.5f && af [2 ]== -2.0f ); 203CHECK (g .get_int_array ("a.arr_b" ,ai )&& ai .size ()== 4 && ai [1 ]== 1 && ai [3 ]== 0 ); 204CHECK (!g .get_int_array ("a.arr_f" ,ai )); 205CHECK (!g .get_int_array ("a.arr_s" ,ai )); 206CHECK (g .tensors ().size ()== 4 ); 207const GgufTensorInfo * t = g .tensor ("t.f16" ); 208CHECK (t && t -> nbytes == 8 && t -> ne .size ()== 1 && t -> ne [0 ]== 4 ); 209float v [64 ]; 210CHECK (t && g .read_f32 (* t ,v ,err )&& v [0 ]== 1.0f && v [1 ]== -2.0f && v [2 ]== 0.25f && v [3 ]== 3.5f ); 211t = g .tensor ("t.f32" ); 212CHECK (t && g .read_f32 (* t ,v ,err )&& v [3 ]== 3.5f ); 213t = g .tensor ("t.bf16" ); 214CHECK (t && g .read_f32 (* t ,v ,err )&& v [1 ]== -2.0f && v [2 ]== 0.25f ); 215t = g .tensor ("t.q8" ); 216CHECK (t && t -> nbytes == 68 && g .read_f32 (* t ,v ,err )); 217CHECK (std::fabs (v [0 ]- 0.5f * -16 )< 1e-6 && std::fabs (v [31 ]- 0.5f * 15 )< 1e-6 && std::fabs (v [32 ]- 1.0f * -16 )< 1e-6 ); 218CHECK (g .tensor ("nope" )== nullptr ); 219CHECK (std::fabs (gguf_f16_to_f32 (0x0001 )- 5.9604645e-08f )< 1e-12f );// the smallest subnormal half 220CHECK (gguf_f16_to_f32 (0x7C00 )> 1e30f );// inf 221// not a GGUF / cut short 222FILE * bad = std::fopen (tmp .c_str (),"wb" ); 223if (bad ) { std::fwrite ("GGUF" ,1 ,4 ,bad ); std::fclose (bad ); } 224Gguf g2 ; 225CHECK (!g2 .open (tmp ,err )); 226CHECK (!g2 .open (tmp + ".missing" ,err )); 227 std::remove (tmp .c_str ()); 228 } 229 230// --- 2. real mmproj files given on the command line --- 231for (int a = 1 ;a < argc ;++ a ) { 232Gguf g ; 233 std::string err ,why ; 234VitGeometry geo ; 235if (!g .open (argv [a ],err )) { std::printf ("FAIL %s: %s\n" ,argv [a ],err .c_str ());++ g_fail ;continue ; } 236if (!vit_geometry_from_gguf (g ,geo ,err )) { std::printf ("FAIL %s: geometry: %s\n" ,argv [a ],err .c_str ());++ g_fail ;continue ; } 237const bool sup = vit_geometry_supported (geo ,why ); 238 std::printf ("%s: %s layers %d width %d heads %d (head %d) ffn %d patch %d merge %d pos grid %d (image %d) out %d eps %g taps %d " 239"deepstack %zu supported %s%s%s\n" ,argv [a ],geo .projector .c_str (),geo .layers ,geo .width ,geo .heads ,geo .head_dim ,geo .ffn , 240geo .patch ,geo .merge ,geo .pos_grid ,geo .image_size ,geo .out_dim , (double )geo .eps ,geo .patch_taps ,geo .deepstack .size (), 241sup ?"yes" :"no" ,sup ?"" :": " ,sup ?"" :why .c_str ()); 242CHECK (sup ); 243CHECK (geo .layers == 27 && geo .width == 1152 && geo .heads == 16 && geo .head_dim == 72 && geo .ffn == 4304 ); 244CHECK (geo .patch == 16 && geo .merge == 2 && geo .pos_grid == 48 && geo .patch_taps == 2 ); 245CHECK (geo .out_dim == 2560 || geo .out_dim == 2048 ); 246CHECK (geo .merger_in == 4608 && geo .merger_hidden == 4608 ); 247 } 248 249 std::printf (g_fail ?"strata-vision-own-test: %d FAILED\n" :"strata-vision-own-test: ok\n" ,g_fail ); 250return g_fail ?1 :0 ; 251}