char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
db52540f7
char/aliceai-t5-moe
1#include "models.h" 2 3ggml_cgraph * clip_graph_internvl::build () { 4GGML_ASSERT (model .class_embedding != nullptr ); 5GGML_ASSERT (model .position_embeddings != nullptr ); 6 7const int n_pos = n_patches + 1 ; 8ggml_tensor * inp = build_inp (); 9 10// add CLS token 11ggml_tensor * cls_repeated = ggml_repeat_4d (ctx0 ,model .class_embedding , 12model .class_embedding -> ne [0 ],1 ,n_batch ,1 ); 13inp = ggml_concat (ctx0 ,inp ,cls_repeated ,1 ); 14 15// The larger models use a different ViT, which uses RMS norm instead of layer norm 16// ref: https://github.com/ggml-org/llama.cpp/pull/13443#issuecomment-2869786188 17norm_type norm_t = (hparams .n_embd == 3200 && hparams .n_layer == 45 ) 18 ?NORM_TYPE_RMS // 6B ViT (Used by InternVL 2.5/3 - 26B, 38B, 78B) 19 :NORM_TYPE_NORMAL ;// 300M ViT (Used by all smaller InternVL models) 20 21ggml_tensor * cur = build_vit ( 22inp ,n_pos , 23norm_t , 24hparams .ffn_op , 25model .position_embeddings , 26nullptr ); 27 28// remove CLS token 29cur = ggml_view_3d (ctx0 ,cur , 30n_embd ,n_patches ,n_batch , 31cur -> nb [1 ],cur -> nb [2 ],0 ); 32cur = ggml_cont (ctx0 ,cur ); 33 34// pixel shuffle 35 { 36const int scale_factor = model .hparams .n_merge ; 37const int bsz = n_batch ; 38const int height = n_patches_y ; 39const int width = n_patches_x ; 40GGML_ASSERT (scale_factor > 0 ); 41cur = ggml_reshape_4d (ctx0 ,cur ,n_embd * scale_factor ,height /scale_factor ,width ,bsz ); 42cur = ggml_permute (ctx0 ,cur ,0 ,2 ,1 ,3 ); 43cur = ggml_cont_4d (ctx0 ,cur , 44n_embd * scale_factor * scale_factor , 45height /scale_factor , 46width /scale_factor , 47bsz ); 48cur = ggml_permute (ctx0 ,cur ,0 ,2 ,1 ,3 ); 49// flatten to 2D 50cur = ggml_cont_3d (ctx0 ,cur , 51n_embd * scale_factor * scale_factor , 52cur -> ne [1 ]* cur -> ne [2 ], 53cur -> ne [3 ]); 54 } 55 56// projector (always using GELU activation) 57 { 58// projector LayerNorm uses pytorch's default eps = 1e-5 59// ref: https://huggingface.co/OpenGVLab/InternVL3-8B-Instruct/blob/a34d3e4e129a5856abfd6aa6de79776484caa14e/modeling_internvl_chat.py#L79 60cur = build_norm (cur ,model .mm_0_w ,model .mm_0_b ,NORM_TYPE_NORMAL ,1e-5 ,-1 ); 61cur = build_ffn (cur , 62model .mm_1_w ,model .mm_1_b , 63nullptr ,nullptr , 64model .mm_3_w ,model .mm_3_b , 65FFN_GELU , 66-1 ); 67 } 68 69// build the graph 70ggml_build_forward_expand (gf ,cur ); 71 72return gf ; 73}