char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
b21e4de74
char/aliceai-t5-moe
1#include "models.h" 2 3ggml_tensor * clip_graph_minimax_m3::apply_rope ( 4ggml_tensor * x ,ggml_tensor * pos_h ,ggml_tensor * pos_w ) { 5const int dh = (int )x -> ne [0 ]; 6const int axd = 2 * ((2 * (dh /2 ) /3 ) /2 ); 7 8GGML_ASSERT (3 * axd <=dh ); 9 10const float th = hparams .rope_theta ; 11 12// layout of x is [t, h, w, pad] 13// t is unrotated, h and w are rotated, pad is unrotated 14x = ggml_rope_ext (ctx0 ,x ,pos_h ,nullptr ,axd ,GGML_ROPE_TYPE_NEOX ,0 ,th ,1.0f ,0.0f ,1.0f ,0.0f ,0.0f ); 15x = ggml_rope_set_offset (x ,axd ); 16 17x = ggml_rope_ext (ctx0 ,x ,pos_w ,nullptr ,axd ,GGML_ROPE_TYPE_NEOX ,0 ,th ,1.0f ,0.0f ,1.0f ,0.0f ,0.0f ); 18x = ggml_rope_set_offset (x ,2 * axd ); 19 20return x ; 21} 22 23ggml_cgraph * clip_graph_minimax_m3::build () { 24GGML_ASSERT (model .patch_bias == nullptr ); 25GGML_ASSERT (model .class_embedding == nullptr ); 26GGML_ASSERT (model .patch_embeddings_0 && model .patch_embeddings_1 ); 27GGML_ASSERT (model .mm_1_w && model .mm_2_w ); 28GGML_ASSERT (model .mm_merger_fc1_w && model .mm_merger_fc2_w ); 29 30const int batch_size = 1 ; 31const int n_pos = n_patches ; 32const int merge = hparams .n_merge ; 33 34// patch embedding 35ggml_tensor * inp_raw = build_inp_raw (); 36ggml_tensor * inp = ggml_add (ctx0 , 37ggml_conv_2d (ctx0 ,model .patch_embeddings_0 ,inp_raw ,patch_size ,patch_size ,0 ,0 ,1 ,1 ), 38ggml_conv_2d (ctx0 ,model .patch_embeddings_1 ,inp_raw ,patch_size ,patch_size ,0 ,0 ,1 ,1 )); 39 40// spatial merge 41 { 42inp = ggml_permute (ctx0 ,inp ,1 ,2 ,0 ,3 ); 43inp = ggml_cont_4d (ctx0 ,inp ,n_embd * merge ,n_patches_x /merge ,n_patches_y ,batch_size ); 44inp = ggml_reshape_4d (ctx0 ,inp ,n_embd * merge ,n_patches_x /merge ,merge ,batch_size * (n_patches_y /merge )); 45inp = ggml_permute (ctx0 ,inp ,0 ,2 ,1 ,3 ); 46inp = ggml_cont_3d (ctx0 ,inp ,n_embd ,n_patches_x * n_patches_y ,batch_size ); 47 } 48 49// t (time axis) is always 0 for now, so we leave it unrotated 50ggml_tensor * pos_h = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_pos ); 51ggml_set_name (pos_h ,"minimax_pos_h" );ggml_set_input (pos_h ); 52ggml_tensor * pos_w = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_pos ); 53ggml_set_name (pos_w ,"minimax_pos_w" );ggml_set_input (pos_w ); 54 55ggml_tensor * inpL = build_vit ( 56inp ,n_pos ,NORM_TYPE_NORMAL ,FFN_GELU_ERF ,nullptr , 57 [& ](ggml_tensor * c ,const clip_layer & ) { 58return apply_rope (c ,pos_h ,pos_w ); 59 }); 60 61// projector 62ggml_tensor * emb = inpL ; 63emb = build_ffn (emb ,model .mm_1_w ,model .mm_1_b , 64nullptr ,nullptr , 65model .mm_2_w ,model .mm_2_b ,FFN_GELU_ERF ,-1 ); 66 67const int64_t proj = emb -> ne [0 ]; 68emb = ggml_reshape_2d (ctx0 ,emb ,proj * merge * merge ,n_pos / (merge * merge )); 69 70emb = build_ffn (emb ,model .mm_merger_fc1_w ,model .mm_merger_fc1_b , 71nullptr ,nullptr , 72model .mm_merger_fc2_w ,model .mm_merger_fc2_b ,FFN_GELU_ERF ,-1 ); 73 74ggml_build_forward_expand (gf ,emb ); 75return gf ; 76}