char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
717dad5c8
char/aliceai-t5-moe
1#include "models.h" 2 3ggml_cgraph * clip_graph_deepseekocr2::build () { 4GGML_ASSERT (hparams .n_head_kv > 0 ); 5GGML_ASSERT (n_head %hparams .n_head_kv == 0 ); 6 7// patch embedding 8ggml_tensor * inp_raw = build_inp_raw (); 9 10ggml_tensor * sam_out = build_sam (inp_raw ); 11 12ggml_tensor * qwen2_out ; 13// Building Qwen2 encoder 14 { 15ggml_tensor * inp ; 16 17// H*W, C, B 18inp = ggml_reshape_3d (ctx0 ,sam_out ,sam_out -> ne [0 ]* sam_out -> ne [1 ],sam_out -> ne [2 ],sam_out -> ne [3 ]); 19inp = ggml_cont (ctx0 ,ggml_permute (ctx0 ,inp ,1 ,0 ,2 ,3 ));// C, H*W, B 20 21auto num_image_tokens = inp -> ne [1 ];// H*W 22GGML_ASSERT (num_image_tokens == 144 || num_image_tokens == 256 ); 23 24// query based on numbers of image tokens (in SAM output) 25// 16x16 -> query_1024 (1024x1024 images) 26// 12x12 -> query_768 (768x768 images) 27 28ggml_tensor * query_embed = model .resample_query_1024 ; 29int num_queries = 256 ; 30 31if (num_image_tokens == 144 ) { 32query_embed = model .resample_query_768 ; 33num_queries = 144 ; 34 } 35 36// repeat the query embedding per batch item, then append: (C, num_image_tokens + num_queries, B) 37query_embed = ggml_cast (ctx0 ,query_embed ,inp -> type ); 38query_embed = ggml_repeat_4d (ctx0 ,query_embed ,query_embed -> ne [0 ],num_queries ,inp -> ne [2 ],1 ); 39inp = ggml_concat (ctx0 ,inp ,query_embed ,1 ); 40 41auto seq_len = inp -> ne [1 ]; 42 43// qwen2 encoder attention mask 44ggml_tensor * attn_mask = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,seq_len ,seq_len ); 45ggml_set_name (attn_mask ,"qwen2_attn_mask" ); 46ggml_set_input (attn_mask ); 47 48ggml_tensor * inp_pos = ggml_cast (ctx0 ,ggml_arange (ctx0 ,0 ,seq_len ,1 ),GGML_TYPE_I32 ); 49 50auto add_rope = [& ](ggml_tensor * x ,const clip_layer & ) { 51return ggml_rope_ext (ctx0 ,x ,inp_pos ,nullptr ,d_head , 52GGML_ROPE_TYPE_NEOX ,131072 ,1000000 ,1 ,0 ,1 ,0 ,0 ); 53 }; 54 55build_vit_opts vit_opts ; 56vit_opts .attn_mask = attn_mask ; 57 58// build_vit applies model.post_ln_w internally; do not re-apply 59ggml_tensor * cur = build_vit (inp ,seq_len ,NORM_TYPE_RMS ,FFN_SILU , 60/* learned_pos_embd */ nullptr ,add_rope ,vit_opts ); 61 62cur = ggml_cont (ctx0 , 63ggml_view_3d (ctx0 ,cur ,cur -> ne [0 ],num_queries ,cur -> ne [2 ],cur -> nb [1 ],cur -> nb [2 ], 64cur -> nb [1 ]* (cur -> ne [1 ]- num_queries )));// only take query tokens for output 65 66ggml_build_forward_expand (gf ,cur ); 67qwen2_out = cur ; 68 } 69 70ggml_tensor * cur ; 71 72cur = ggml_mul_mat (ctx0 ,model .mm_fc_w ,qwen2_out ); 73cur = ggml_add (ctx0 ,cur ,model .mm_fc_b ); 74 75// view_seperator only after the global view 76if (img .add_viewsep ) { 77ggml_tensor * vs = ggml_repeat_4d (ctx0 ,model .view_seperator ,model .view_seperator -> ne [0 ],1 ,cur -> ne [2 ],1 ); 78cur = ggml_concat (ctx0 ,cur ,vs ,1 );// (n_dim, 257, n_batch) 79 } 80 81cb (cur ,"dsocr2_output" ,-1 ); 82 83ggml_build_forward_expand (gf ,cur ); 84return gf ; 85}