okami.mom/strata
git clone https://git.nkp.pet/okami.mom/strata
0479b72e9
main
1// tools/vision/own/vit_geometry.cpp - see vit_geometry.h 2#include "vit_geometry.h" 3 4#include <algorithm> 5#include <cmath> 6 7namespace strata_vit { 8 9namespace { 10 11std::string blk (int il ,const char * what ,const char * kind ) { 12return "v.blk." + std::to_string (il )+ "." + what + "." + kind ; 13} 14 15bool need_u32 (const Gguf & gg ,const char * key ,int & out , std::string & err ) { 16uint32_t v = 0 ; 17if (!gg .get_u32 (key ,v )|| v == 0 || v > (1u <<24 )) {err = std::string ("the vision projector has no usable " )+ key ;return false; } 18out = (int )v ; 19return true; 20} 21 22// shape check: the tensor exists and has exactly these dimensions (as stored, fastest first) 23bool shape_is (const Gguf & gg ,const std::string & name , std::initializer_list < int64_t > ne , std::string & err ) { 24const GgufTensorInfo * t = gg .tensor (name ); 25if (!t ) {err = "the vision projector lacks the tensor " + name ;return false; } 26if (t -> ne .size ()!= ne .size ()|| !std::equal (ne .begin (),ne .end (),t -> ne .begin ())) { 27 std::string have ; 28for (int64_t d :t -> ne )have += (have .empty () ?"" :" x " )+ std::to_string (d ); 29err = "tensor " + name + " has shape [" + have + "], not what this tower's geometry needs" ; 30return false; 31 } 32return true; 33} 34 35}// namespace 36 37bool vit_geometry_from_gguf (const Gguf & gg ,VitGeometry & g , std::string & err ) { 38g = VitGeometry {}; 39 std::string arch ; 40if (!gg .get_str ("general.architecture" ,arch )|| arch != "clip" ) {err = "not an mmproj file (general.architecture is not clip)" ;return false; } 41bool has_v = false; 42if (!gg .get_bool ("clip.has_vision_encoder" ,has_v )|| !has_v ) {err = "the mmproj file has no vision encoder" ;return false; } 43if (!gg .get_str ("clip.projector_type" ,g .projector )|| g .projector .empty ()) {err = "the mmproj file names no projector type" ;return false; } 44if (!need_u32 (gg ,"clip.vision.block_count" ,g .layers ,err )|| !need_u32 (gg ,"clip.vision.embedding_length" ,g .width ,err )|| 45 !need_u32 (gg ,"clip.vision.attention.head_count" ,g .heads ,err )|| !need_u32 (gg ,"clip.vision.feed_forward_length" ,g .ffn ,err )|| 46 !need_u32 (gg ,"clip.vision.patch_size" ,g .patch ,err )|| !need_u32 (gg ,"clip.vision.projection_dim" ,g .out_dim ,err )) 47return false; 48if (g .width %g .heads != 0 ) {err = "the vision projector's width is not a multiple of its head count" ;return false; } 49g .head_dim = g .width /g .heads ; 50uint32_t u = 0 ; 51if (gg .get_u32 ("clip.vision.image_size" ,u ))g .image_size = (int )u ; 52if (gg .get_u32 ("clip.vision.spatial_merge_size" ,u )&& u > 0 && u <=16 )g .merge = (int )u ; 53gg .get_f32 ("clip.vision.attention.layer_norm_epsilon" ,g .eps ); 54 std::vector < float > v3 ; 55if (gg .get_f32_array ("clip.vision.image_mean" ,v3 )&& v3 .size ()== 3 )for (int i = 0 ;i < 3 ;++ i )g .mean [i ]= v3 [i ]; 56if (gg .get_f32_array ("clip.vision.image_std" ,v3 )&& v3 .size ()== 3 )for (int i = 0 ;i < 3 ;++ i )g .stdv [i ]= v3 [i ]; 57if (gg .get_u32 ("clip.vision.n_wa_pattern" ,u ))g .window_pattern = (int )u ; 58 59bool use_gelu = false,use_silu = false; 60gg .get_bool ("clip.use_gelu" ,use_gelu ); 61gg .get_bool ("clip.use_silu" ,use_silu ); 62g .act = use_gelu ?ActKind ::GeluTanh :use_silu ?ActKind ::Silu :ActKind ::GeluQuick ;// llama.cpp's own default when neither is set 63g .norm = g .projector == "qwen2.5vl_merger" ?NormKind ::RMSNorm :NormKind ::LayerNorm ; 64 65// the layer layout, from which tensors exist 66g .fused_qkv = gg .tensor (blk (0 ,"attn_qkv" ,"weight" ))!= nullptr ; 67g .gated_ffn = gg .tensor (blk (0 ,"ffn_gate" ,"weight" ))!= nullptr ; 68g .patch_taps = gg .tensor ("v.patch_embd.weight.1" )!= nullptr ?2 :1 ; 69g .pre_ln = gg .tensor ("v.pre_ln.weight" )!= nullptr ; 70 std::vector < int64_t > ds ; 71if (gg .get_int_array ("clip.vision.is_deepstack_layers" ,ds )) 72for (size_t i = 0 ;i < ds .size ();++ i )if (ds [i ])g .deepstack .push_back ((int )i ); 73for (int il = 0 ;il < g .layers ;++ il ) { 74if (gg .tensor ("v.deepstack." + std::to_string (il )+ ".fc1.weight" )&& std::find (g .deepstack .begin (),g .deepstack .end (),il )== g .deepstack .end ()) 75g .deepstack .push_back (il ); 76 } 77 78const int64_t W = g .width ,F = g .ffn ; 79if (!shape_is (gg ,"v.patch_embd.weight" , {g .patch ,g .patch ,3 ,W },err )|| !shape_is (gg ,"v.patch_embd.bias" , {W },err ))return false; 80if (g .patch_taps == 2 && !shape_is (gg ,"v.patch_embd.weight.1" , {g .patch ,g .patch ,3 ,W },err ))return false; 81const GgufTensorInfo * pos = gg .tensor ("v.position_embd.weight" ); 82if (!pos || pos -> ne .size ()!= 2 || pos -> ne [0 ]!= W ) {err = "the vision projector's position table is missing or not [width, rows]" ;return false; } 83g .pos_grid = (int ) std::lround (std::sqrt ((double )pos -> ne [1 ])); 84if ((int64_t )g .pos_grid * g .pos_grid != pos -> ne [1 ]) {err = "the position table's rows are not a square grid" ;return false; } 85for (int il = 0 ;il < g .layers ;++ il ) { 86for (const char * n : {"ln1" ,"ln2" }) 87if (!shape_is (gg ,blk (il ,n ,"weight" ), {W },err )|| !shape_is (gg ,blk (il ,n ,"bias" ), {W },err ))return false; 88if (g .fused_qkv ) { 89if (!shape_is (gg ,blk (il ,"attn_qkv" ,"weight" ), {W ,3 * W },err )|| !shape_is (gg ,blk (il ,"attn_qkv" ,"bias" ), {3 * W },err ))return false; 90 }else { 91for (const char * n : {"attn_q" ,"attn_k" ,"attn_v" }) 92if (!shape_is (gg ,blk (il ,n ,"weight" ), {W ,W },err ))return false; 93 } 94if (!shape_is (gg ,blk (il ,"attn_out" ,"weight" ), {W ,W },err )|| !shape_is (gg ,blk (il ,"attn_out" ,"bias" ), {W },err ))return false; 95if (!shape_is (gg ,blk (il ,"ffn_up" ,"weight" ), {W ,F },err )|| !shape_is (gg ,blk (il ,"ffn_up" ,"bias" ), {F },err ))return false; 96if (!shape_is (gg ,blk (il ,"ffn_down" ,"weight" ), {F ,W },err )|| !shape_is (gg ,blk (il ,"ffn_down" ,"bias" ), {W },err ))return false; 97if (g .gated_ffn && !shape_is (gg ,blk (il ,"ffn_gate" ,"weight" ), {W ,F },err ))return false; 98 } 99if (!shape_is (gg ,"v.post_ln.weight" , {W },err )|| !shape_is (gg ,"v.post_ln.bias" , {W },err ))return false; 100g .merger_in = (int ) (W * g .merge * g .merge ); 101const GgufTensorInfo * m0 = gg .tensor ("mm.0.weight" ); 102if (!m0 || m0 -> ne .size ()!= 2 || m0 -> ne [0 ]!= g .merger_in ) {err = "the vision projector's mm.0.weight is missing or not [width * merge^2, hidden]" ;return false; } 103g .merger_hidden = (int )m0 -> ne [1 ]; 104if (!shape_is (gg ,"mm.0.bias" , {g .merger_hidden },err )|| !shape_is (gg ,"mm.2.weight" , {g .merger_hidden ,g .out_dim },err )|| 105 !shape_is (gg ,"mm.2.bias" , {g .out_dim },err )) 106return false; 107return true; 108} 109 110bool vit_geometry_supported (const VitGeometry & g , std::string & why ) { 111if (g .projector != "qwen3vl_merger" ) {why = "projector type " + g .projector + " is not one of this tower's families" ;return false; } 112if (g .norm != NormKind ::LayerNorm ) {why = "RMS norm" ;return false; } 113if (g .act != ActKind ::GeluTanh ) {why = "an activation other than the tanh GELU" ;return false; } 114if (g .gated_ffn ) {why = "a gated FFN" ;return false; } 115if (!g .fused_qkv ) {why = "separate q/k/v projections" ;return false; } 116if (g .window_pattern != 0 ) {why = "window attention" ;return false; } 117if (g .pre_ln ) {why = "a norm before the first layer" ;return false; } 118if (!g .deepstack .empty ()) {why = "deepstack outputs" ;return false; } 119if (g .head_dim %4 != 0 ) {why = "a head size that is not a multiple of 4 (the 2D rotary splits it in 4)" ;return false; } 120if (g .merge != 2 ) {why = "a merge size other than 2" ;return false; } 121return true; 122} 123 124}// namespace strata_vit