char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
a970515bd
char/aliceai-t5-moe
1#include "models.h" 2 3ggml_cgraph * clip_graph_siglip::build () { 4ggml_tensor * inp = build_inp (); 5 6ggml_tensor * learned_pos_embd = model .position_embeddings ; 7if (proj_type == PROJECTOR_TYPE_LFM2 || proj_type == PROJECTOR_TYPE_PHI4 ) { 8learned_pos_embd = resize_position_embeddings (); 9 } 10 11ggml_tensor * cur = build_vit ( 12inp ,n_patches , 13NORM_TYPE_NORMAL , 14hparams .ffn_op , 15learned_pos_embd , 16nullptr ); 17 18if (proj_type == PROJECTOR_TYPE_GEMMA3 ) { 19const int batch_size = 1 ; 20GGML_ASSERT (n_patches_x == n_patches_y ); 21const int patches_per_image = n_patches_x ; 22const int kernel_size = hparams .n_merge ; 23 24cur = ggml_transpose (ctx0 ,cur ); 25cur = ggml_cont_4d (ctx0 ,cur ,patches_per_image ,patches_per_image ,n_embd ,batch_size ); 26 27// doing a pool2d to reduce the number of output tokens 28cur = ggml_pool_2d (ctx0 ,cur ,GGML_OP_POOL_AVG ,kernel_size ,kernel_size ,kernel_size ,kernel_size ,0 ,0 ); 29cur = ggml_reshape_3d (ctx0 ,cur ,cur -> ne [0 ]* cur -> ne [0 ],n_embd ,batch_size ); 30cur = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,cur )); 31 32// apply norm before projection 33cur = ggml_rms_norm (ctx0 ,cur ,eps ); 34cur = ggml_mul (ctx0 ,cur ,model .mm_soft_emb_norm_w ); 35 36// apply projection 37cur = ggml_mul_mat (ctx0 , 38ggml_cont (ctx0 ,ggml_transpose (ctx0 ,model .mm_input_proj_w )), 39cur ); 40 41 }else if (proj_type == PROJECTOR_TYPE_IDEFICS3 ) { 42// pixel_shuffle 43// https://github.com/huggingface/transformers/blob/0a950e0bbe1ed58d5401a6b547af19f15f0c195e/src/transformers/models/idefics3/modeling_idefics3.py#L578 44const int scale_factor = model .hparams .n_merge ; 45cur = build_patch_merge_permute (cur ,scale_factor ); 46cur = build_mm (model .mm_fc_w ,cur ); 47 48 }else if (proj_type == PROJECTOR_TYPE_LFM2 ) { 49// pixel unshuffle block 50const int scale_factor = model .hparams .n_merge ; 51cur = build_patch_merge_permute (cur ,scale_factor ); 52 53// projection, in LFM2-VL input norm is optional 54if (model .mm_input_norm_w ) { 55cur = ggml_norm (ctx0 ,cur ,1e-5 );// default nn.LayerNorm 56cur = ggml_mul (ctx0 ,cur ,model .mm_input_norm_w ); 57 } 58 59if (model .mm_input_norm_b ) { 60cur = ggml_add (ctx0 ,cur ,model .mm_input_norm_b ); 61 } 62 63cur = build_ffn (cur , 64model .mm_1_w ,model .mm_1_b , 65nullptr ,nullptr , 66model .mm_2_w ,model .mm_2_b , 67FFN_GELU , 68-1 ); 69 70 }else if (proj_type == PROJECTOR_TYPE_JANUS_PRO ) { 71cur = build_ffn (cur , 72model .mm_0_w ,model .mm_0_b , 73nullptr ,nullptr , 74model .mm_1_w ,model .mm_1_b , 75hparams .ffn_op , 76-1 ); 77 78 }else if (proj_type == PROJECTOR_TYPE_PHI4 ) { 79cur = build_ffn (cur , 80model .mm_0_w ,model .mm_0_b , 81nullptr ,nullptr , 82model .mm_2_w ,model .mm_2_b , 83FFN_GELU , 84-1 ); 85 86 }else { 87GGML_ABORT ("SigLIP: Unsupported projector type" ); 88 } 89 90// build the graph 91ggml_build_forward_expand (gf ,cur ); 92 93return gf ; 94}