char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
09343c019
char/aliceai-t5-moe
1#include "models.h" 2 3ggml_cgraph * clip_graph_step3vl::build () { 4GGML_ASSERT (model .class_embedding == nullptr ); 5GGML_ASSERT (model .patch_embeddings_0 != nullptr ); 6GGML_ASSERT (model .position_embeddings != nullptr ); 7 8norm_type norm_t = NORM_TYPE_NORMAL ; 9 10ggml_tensor * pos_h = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_patches ); 11ggml_set_name (pos_h ,"pos_h" ); 12ggml_set_input (pos_h ); 13 14ggml_tensor * pos_w = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_patches ); 15ggml_set_name (pos_w ,"pos_w" ); 16ggml_set_input (pos_w ); 17 18ggml_tensor * inp = build_inp (); 19ggml_tensor * learned_pos_embd = resize_position_embeddings (); 20 21auto add_pos = [& ](ggml_tensor * cur ,const clip_layer & ) { 22return build_rope_2d (ctx0 ,cur ,pos_w ,pos_h ,hparams .rope_theta , false); 23 }; 24 25auto add_spatial_bias = [& ](ggml_tensor * cur ,ggml_tensor * bias ) { 26if (bias == nullptr ) { 27return cur ; 28 } 29 30const int64_t width = cur -> ne [0 ]; 31const int64_t height = cur -> ne [1 ]; 32const int64_t channels = cur -> ne [2 ]; 33 34cur = ggml_reshape_2d (ctx0 ,cur ,width * height ,channels ); 35cur = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,cur )); 36cur = ggml_add (ctx0 ,cur ,bias ); 37cur = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,cur )); 38cur = ggml_reshape_3d (ctx0 ,cur ,width ,height ,channels ); 39 40return cur ; 41 }; 42 43ggml_tensor * cur = build_vit ( 44inp , 45n_patches , 46norm_t , 47hparams .ffn_op , 48learned_pos_embd , 49add_pos ); 50cb (cur ,"vit_out" ,-1 ); 51 52// [n_embd, n_patches] -> [w, h, n_embd] for spatial downsampling convolutions. 53cur = ggml_permute (ctx0 ,cur ,1 ,0 ,2 ,3 ); 54cur = ggml_cont_3d (ctx0 ,cur ,n_patches_x ,n_patches_y ,n_embd ); 55 56// First downsampler: Conv2d(1536 -> 3072, k=3, s=2, p=1) 57cur = ggml_conv_2d (ctx0 ,model .mm_0_w ,cur ,2 ,2 ,1 ,1 ,1 ,1 ); 58cur = add_spatial_bias (cur ,model .mm_0_b ); 59cb (cur ,"downsample_0" ,-1 ); 60 61// Second downsampler: Conv2d(3072 -> 6144, k=3, s=2, p=1) 62cur = ggml_conv_2d (ctx0 ,model .mm_1_w ,cur ,2 ,2 ,1 ,1 ,1 ,1 ); 63cur = add_spatial_bias (cur ,model .mm_1_b ); 64cb (cur ,"downsample_1" ,-1 ); 65 66// [w, h, c] -> [c, w*h] 67 { 68const int64_t w = cur -> ne [0 ]; 69const int64_t h = cur -> ne [1 ]; 70cur = ggml_reshape_3d (ctx0 ,cur ,w * h ,cur -> ne [2 ],cur -> ne [3 ]); 71cur = ggml_cont (ctx0 ,ggml_permute (ctx0 ,cur ,1 ,0 ,2 ,3 )); 72 } 73cb (cur ,"downsample_flatten" ,-1 ); 74 75// Final projector: Linear(6144 -> projection_dim) 76cur = ggml_mul_mat (ctx0 ,model .mm_model_proj ,cur ); 77cb (cur ,"projector_out" ,-1 ); 78 79ggml_build_forward_expand (gf ,cur ); 80return gf ; 81}