char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
31e82494c
char/aliceai-t5-moe
1#include "models.h" 2 3ggml_tensor * clip_graph_qwen2vl::build_inp_with_temporal_merge () { 4ggml_tensor * inp_raw = build_inp_raw (); 5 6GGML_ASSERT (img .nx () % (patch_size * 2 )== 0 ); 7GGML_ASSERT (img .ny () % (patch_size * 2 )== 0 ); 8 9const size_t nb1 = ggml_row_size (inp_raw -> type ,img .nx ()); 10const size_t nb2 = ggml_row_size (inp_raw -> type ,img .nx ()* img .ny ()); 11 12if (n_batch == 1 ) { 13// still image input 14return ggml_add (ctx0 , 15ggml_conv_2d (ctx0 ,model .patch_embeddings_0 ,inp_raw ,patch_size ,patch_size ,0 ,0 ,1 ,1 ), 16ggml_conv_2d (ctx0 ,model .patch_embeddings_1 ,inp_raw ,patch_size ,patch_size ,0 ,0 ,1 ,1 )); 17 }else if (n_batch == 2 ) { 18// 2 frames input (video input) 19ggml_tensor * inp_0 = ggml_view_3d (ctx0 ,inp_raw , 20img .nx (),img .ny (),3 ,nb1 ,nb2 ,0 ); 21ggml_tensor * inp_1 = ggml_view_3d (ctx0 ,inp_raw , 22img .nx (),img .ny (),3 ,nb1 ,nb2 , 23nb2 * 3 );// move to the second frame 24return ggml_add (ctx0 , 25ggml_conv_2d (ctx0 ,model .patch_embeddings_0 ,inp_0 ,patch_size ,patch_size ,0 ,0 ,1 ,1 ), 26ggml_conv_2d (ctx0 ,model .patch_embeddings_1 ,inp_1 ,patch_size ,patch_size ,0 ,0 ,1 ,1 )); 27 }else { 28GGML_ASSERT (false&& "n_batch > 2 is not supported" ); 29 } 30} 31 32ggml_cgraph * clip_graph_qwen2vl::build () { 33GGML_ASSERT (model .patch_bias == nullptr ); 34GGML_ASSERT (model .class_embedding == nullptr ); 35 36const int batch_size = 1 ; 37const bool use_window_attn = hparams .n_wa_pattern > 0 ; 38const int n_wa_pattern = hparams .n_wa_pattern ; 39const int n_pos = n_patches ; 40const int num_position_ids = n_pos * 4 ;// m-rope requires 4 dim per position 41 42norm_type norm_t = proj_type == PROJECTOR_TYPE_QWEN25VL 43 ?NORM_TYPE_RMS // qwen 2.5 vl 44 :NORM_TYPE_NORMAL ;// qwen 2 vl 45 46int mrope_sections [4 ]= {d_head /4 ,d_head /4 ,d_head /4 ,d_head /4 }; 47 48ggml_tensor * inp = build_inp_with_temporal_merge (); 49 50// second conv dimension 51 { 52inp = ggml_permute (ctx0 ,inp ,1 ,2 ,0 ,3 );// [w, h, c, b] -> [c, w, h, b] 53inp = ggml_cont_4d ( 54ctx0 ,inp , 55n_embd * 2 ,n_patches_x /2 ,n_patches_y ,batch_size ); 56inp = ggml_reshape_4d ( 57ctx0 ,inp , 58n_embd * 2 ,n_patches_x /2 ,2 ,batch_size * (n_patches_y /2 )); 59inp = ggml_permute (ctx0 ,inp ,0 ,2 ,1 ,3 ); 60inp = ggml_cont_3d ( 61ctx0 ,inp , 62n_embd ,n_patches_x * n_patches_y ,batch_size ); 63 } 64 65ggml_tensor * inpL = inp ; 66ggml_tensor * window_mask = nullptr ; 67ggml_tensor * window_idx = nullptr ; 68ggml_tensor * inv_window_idx = nullptr ; 69 70ggml_tensor * positions = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,num_position_ids ); 71ggml_set_name (positions ,"positions" ); 72ggml_set_input (positions ); 73 74// pre-layernorm 75if (model .pre_ln_w ) { 76inpL = build_norm (inpL ,model .pre_ln_w ,model .pre_ln_b ,norm_t ,eps ,-1 ); 77 } 78 79if (use_window_attn ) { 80// handle window attention inputs 81inv_window_idx = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_pos /4 ); 82ggml_set_name (inv_window_idx ,"inv_window_idx" ); 83ggml_set_input (inv_window_idx ); 84// mask for window attention 85window_mask = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,n_pos ,n_pos ); 86ggml_set_name (window_mask ,"window_mask" ); 87ggml_set_input (window_mask ); 88 89// if flash attn is used, we need to pad the mask and cast to f16 90if (flash_attn_type == CLIP_FLASH_ATTN_TYPE_ENABLED ) { 91window_mask = ggml_cast (ctx0 ,window_mask ,GGML_TYPE_F16 ); 92 } 93 94// inpL shape: [n_embd, n_patches_x * n_patches_y, batch_size] 95GGML_ASSERT (batch_size == 1 ); 96inpL = ggml_reshape_2d (ctx0 ,inpL ,n_embd * 4 ,n_patches_x * n_patches_y * batch_size /4 ); 97inpL = ggml_get_rows (ctx0 ,inpL ,inv_window_idx ); 98inpL = ggml_reshape_3d (ctx0 ,inpL ,n_embd ,n_patches_x * n_patches_y ,batch_size ); 99 } 100 101// loop over layers 102for (int il = 0 ;il < n_layer ;il ++ ) { 103const auto & layer = model .layers [il ]; 104const bool full_attn = use_window_attn ? (il + 1 ) %n_wa_pattern == 0 : true; 105 106ggml_tensor * cur = inpL ;// inpL = residual, cur = hidden_states 107 108// layernorm1 109cur = build_norm (cur ,layer .ln_1_w ,layer .ln_1_b ,norm_t ,eps ,il ); 110cb (cur ,"ln1" ,il ); 111 112// self-attention 113 { 114ggml_tensor * Qcur = ggml_add (ctx0 , 115build_mm (layer .q_w ,cur ),layer .q_b ); 116ggml_tensor * Kcur = ggml_add (ctx0 , 117build_mm (layer .k_w ,cur ),layer .k_b ); 118ggml_tensor * Vcur = ggml_add (ctx0 , 119build_mm (layer .v_w ,cur ),layer .v_b ); 120 121Qcur = ggml_reshape_3d (ctx0 ,Qcur ,d_head ,n_head ,n_patches ); 122Kcur = ggml_reshape_3d (ctx0 ,Kcur ,d_head ,n_head ,n_patches ); 123Vcur = ggml_reshape_3d (ctx0 ,Vcur ,d_head ,n_head ,n_patches ); 124 125cb (Qcur ,"Qcur" ,il ); 126cb (Kcur ,"Kcur" ,il ); 127cb (Vcur ,"Vcur" ,il ); 128 129// apply M-RoPE 130Qcur = ggml_rope_multi ( 131ctx0 ,Qcur ,positions ,nullptr , 132d_head /2 ,mrope_sections ,GGML_ROPE_TYPE_VISION ,32768 ,10000 ,1 ,0 ,1 ,32 ,1 ); 133Kcur = ggml_rope_multi ( 134ctx0 ,Kcur ,positions ,nullptr , 135d_head /2 ,mrope_sections ,GGML_ROPE_TYPE_VISION ,32768 ,10000 ,1 ,0 ,1 ,32 ,1 ); 136 137cb (Qcur ,"Qcur_rope" ,il ); 138cb (Kcur ,"Kcur_rope" ,il ); 139 140ggml_tensor * attn_mask = full_attn ?nullptr :window_mask ; 141 142cur = build_attn (layer .o_w ,layer .o_b , 143Qcur ,Kcur ,Vcur ,attn_mask ,kq_scale ,il ); 144cb (cur ,"attn_out" ,il ); 145 } 146 147// re-add the layer input, e.g., residual 148cur = ggml_add (ctx0 ,cur ,inpL ); 149 150inpL = cur ;// inpL = residual, cur = hidden_states 151 152cb (cur ,"ffn_inp" ,il ); 153 154// layernorm2 155cur = build_norm (cur ,layer .ln_2_w ,layer .ln_2_b ,norm_t ,eps ,il ); 156cb (cur ,"ffn_inp_normed" ,il ); 157 158// ffn 159cur = build_ffn (cur , 160layer .ff_up_w ,layer .ff_up_b , 161layer .ff_gate_w ,layer .ff_gate_b , 162layer .ff_down_w ,layer .ff_down_b , 163hparams .ffn_op ,il ); 164 165cb (cur ,"ffn_out" ,il ); 166 167// residual 2 168cur = ggml_add (ctx0 ,inpL ,cur ); 169cb (cur ,"layer_out" ,il ); 170 171inpL = cur ; 172 } 173 174// post-layernorm 175if (model .post_ln_w ) { 176inpL = build_norm (inpL ,model .post_ln_w ,model .post_ln_b ,norm_t ,eps ,n_layer ); 177 } 178 179// multimodal projection 180ggml_tensor * embeddings = inpL ; 181embeddings = ggml_reshape_3d (ctx0 ,embeddings ,n_embd * 4 ,n_pos /4 ,batch_size ); 182embeddings = build_ffn (embeddings , 183model .mm_0_w ,model .mm_0_b , 184nullptr ,nullptr , 185model .mm_1_w ,model .mm_1_b , 186FFN_GELU , 187-1 ); 188 189if (use_window_attn ) { 190window_idx = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_pos /4 ); 191ggml_set_name (window_idx ,"window_idx" ); 192ggml_set_input (window_idx ); 193 194// embeddings shape: [n_embd, n_patches_x * n_patches_y, batch_size] 195GGML_ASSERT (batch_size == 1 ); 196embeddings = ggml_reshape_2d (ctx0 ,embeddings ,hparams .projection_dim ,n_patches_x * n_patches_y /4 ); 197embeddings = ggml_get_rows (ctx0 ,embeddings ,window_idx ); 198embeddings = ggml_reshape_3d (ctx0 ,embeddings ,hparams .projection_dim ,n_patches_x * n_patches_y /4 ,batch_size ); 199 } 200 201// build the graph 202ggml_build_forward_expand (gf ,embeddings ); 203 204return gf ; 205}