char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
1e6453457
char/aliceai-t5-moe
1#include "models.h" 2 3ggml_cgraph * clip_graph_youtuvl::build () { 4GGML_ASSERT (model .class_embedding == nullptr ); 5const int batch_size = 1 ; 6const bool use_window_attn = !hparams .wa_layer_indexes .empty (); 7const int n_pos = n_patches ; 8const int num_position_ids = n_pos * 4 ; 9const int m = 2 ; 10const int Wp = n_patches_x ; 11const int Hp = n_patches_y ; 12const int Hm = Hp /m ; 13const int Wm = Wp /m ; 14norm_type norm_t = NORM_TYPE_NORMAL ; 15 16int mrope_sections [4 ]= {d_head /4 ,d_head /4 ,d_head /4 ,d_head /4 }; 17 18ggml_tensor * inp = build_inp_raw (); 19 20// change conv3d to linear 21// reshape and permute to get patches, permute from (patch_size, m, Wm, patch_size, m, Hm, C) to (C, patch_size, patch_size, m, m, Wm, Hm) 22 { 23inp = ggml_reshape_4d ( 24ctx0 ,inp , 25Wm * m * patch_size ,m * patch_size ,Hm ,3 ); 26inp = ggml_permute (ctx0 ,inp ,1 ,2 ,3 ,0 ); 27inp = ggml_cont_4d ( 28ctx0 ,inp , 29m * patch_size * 3 ,Wm ,m * patch_size ,Hm ); 30 31inp = ggml_permute (ctx0 ,inp ,0 ,2 ,1 ,3 ); 32inp = ggml_cont_4d ( 33ctx0 ,inp , 34m * patch_size * 3 ,patch_size ,m ,Hm * Wm ); 35 36inp = ggml_permute (ctx0 ,inp ,1 ,0 ,2 ,3 ); 37inp = ggml_cont_4d ( 38ctx0 ,inp , 39patch_size ,3 ,patch_size ,Hm * Wm * m * m ); 40 41inp = ggml_permute (ctx0 ,inp ,2 ,0 ,1 ,3 ); 42inp = ggml_cont_3d ( 43ctx0 ,inp , 443 * patch_size * patch_size ,Hm * Wm * m * m ,1 ); 45 } 46inp = build_mm (model .patch_embeddings_0 ,inp ); 47 48if (model .patch_bias ) { 49inp = ggml_add (ctx0 ,inp ,model .patch_bias ); 50 } 51 52inp = ggml_reshape_2d (ctx0 ,inp ,n_embd ,n_patches ); 53 54ggml_tensor * inpL = inp ; 55ggml_tensor * window_mask = nullptr ; 56ggml_tensor * window_idx = nullptr ; 57ggml_tensor * inv_window_idx = nullptr ; 58 59ggml_tensor * positions = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,num_position_ids ); 60ggml_set_name (positions ,"positions" ); 61ggml_set_input (positions ); 62 63// pre-layernorm 64if (model .pre_ln_w ) { 65inpL = build_norm (inpL ,model .pre_ln_w ,model .pre_ln_b ,norm_t ,eps ,-1 ); 66 } 67if (use_window_attn ) { 68inv_window_idx = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_pos /4 ); 69ggml_set_name (inv_window_idx ,"inv_window_idx" ); 70ggml_set_input (inv_window_idx ); 71// mask for window attention 72window_mask = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,n_pos ,n_pos ); 73ggml_set_name (window_mask ,"window_mask" ); 74ggml_set_input (window_mask ); 75 76// if flash attn is used, we need to pad the mask and cast to f16 77if (flash_attn_type == CLIP_FLASH_ATTN_TYPE_ENABLED ) { 78window_mask = ggml_cast (ctx0 ,window_mask ,GGML_TYPE_F16 ); 79 } 80 81// inpL shape: [n_embd, n_patches_x * n_patches_y, batch_size] 82GGML_ASSERT (batch_size == 1 ); 83inpL = ggml_reshape_2d (ctx0 ,inpL ,n_embd * 4 ,n_patches_x * n_patches_y * batch_size /4 ); 84inpL = ggml_get_rows (ctx0 ,inpL ,inv_window_idx ); 85inpL = ggml_reshape_3d (ctx0 ,inpL ,n_embd ,n_patches_x * n_patches_y ,batch_size ); 86 } 87 88// loop over layers 89for (int il = 0 ;il < n_layer ;il ++ ) { 90const auto & layer = model .layers [il ]; 91const bool full_attn = use_window_attn ?hparams .wa_layer_indexes .count (il )> 0 : true; 92 93ggml_tensor * cur = inpL ;// inpL = residual, cur = hidden_states 94 95// layernorm1 96cur = build_norm (cur ,layer .ln_1_w ,layer .ln_1_b ,norm_t ,eps ,il ); 97// self-attention 98 { 99ggml_tensor * Qcur = ggml_add (ctx0 , 100build_mm (layer .q_w ,cur ),layer .q_b ); 101ggml_tensor * Kcur = ggml_add (ctx0 , 102build_mm (layer .k_w ,cur ),layer .k_b ); 103ggml_tensor * Vcur = ggml_add (ctx0 , 104build_mm (layer .v_w ,cur ),layer .v_b ); 105 106Qcur = ggml_reshape_3d (ctx0 ,Qcur ,d_head ,n_head ,n_patches ); 107Kcur = ggml_reshape_3d (ctx0 ,Kcur ,d_head ,n_head ,n_patches ); 108Vcur = ggml_reshape_3d (ctx0 ,Vcur ,d_head ,n_head ,n_patches ); 109 110Qcur = ggml_rope_multi ( 111ctx0 ,Qcur ,positions ,nullptr , 112d_head /2 ,mrope_sections ,GGML_ROPE_TYPE_VISION ,32768 ,10000 ,1 ,0 ,1 ,32 ,1 ); 113Kcur = ggml_rope_multi ( 114ctx0 ,Kcur ,positions ,nullptr , 115d_head /2 ,mrope_sections ,GGML_ROPE_TYPE_VISION ,32768 ,10000 ,1 ,0 ,1 ,32 ,1 ); 116 117ggml_tensor * attn_mask = full_attn ?nullptr :window_mask ; 118 119cur = build_attn (layer .o_w ,layer .o_b , 120Qcur ,Kcur ,Vcur ,attn_mask ,kq_scale ,il ); 121 } 122// re-add the layer input, e.g., residual 123cur = ggml_add (ctx0 ,cur ,inpL ); 124 125inpL = cur ;// inpL = residual, cur = hidden_states 126 127// layernorm2 128cur = build_norm (cur ,layer .ln_2_w ,layer .ln_2_b ,norm_t ,eps ,il ); 129 130// ffn 131cur = build_ffn (cur , 132layer .ff_up_w ,layer .ff_up_b , 133nullptr ,nullptr , 134layer .ff_down_w ,layer .ff_down_b , 135hparams .ffn_op ,il ); 136 137// residual 2 138cur = ggml_add (ctx0 ,inpL ,cur ); 139 140inpL = cur ; 141 } 142 143ggml_tensor * embeddings = inpL ; 144if (use_window_attn ) { 145const int spatial_merge_unit = 4 ; 146window_idx = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_pos /spatial_merge_unit ); 147ggml_set_name (window_idx ,"window_idx" ); 148ggml_set_input (window_idx ); 149GGML_ASSERT (batch_size == 1 ); 150embeddings = ggml_reshape_2d (ctx0 ,embeddings ,n_embd * spatial_merge_unit ,n_patches /spatial_merge_unit ); 151embeddings = ggml_get_rows (ctx0 ,embeddings ,window_idx ); 152embeddings = ggml_reshape_3d (ctx0 ,embeddings ,n_embd ,n_patches ,batch_size ); 153cb (embeddings ,"window_order_restored" ,-1 ); 154 } 155 156// post-layernorm (part of Siglip2VisionTransformer, applied after encoder) 157if (model .post_ln_w ) { 158embeddings = build_norm (embeddings ,model .post_ln_w ,model .post_ln_b ,norm_t ,eps ,n_layer ); 159 } 160 161// Now apply merger (VLPatchMerger): 162// 1. Apply RMS norm (ln_q in VLPatchMerger) 163embeddings = build_norm (embeddings ,model .mm_input_norm_w ,nullptr ,NORM_TYPE_RMS ,1e-6 ,-1 ); 164cb (embeddings ,"merger_normed" ,-1 ); 165 166// 2. First reshape for spatial merge (merge 2x2 patches) 167embeddings = ggml_reshape_3d (ctx0 ,embeddings ,n_embd * 4 ,n_pos /4 ,batch_size ); 168cb (embeddings ,"merger_reshaped" ,-1 ); 169 170embeddings = build_ffn (embeddings , 171model .mm_0_w ,model .mm_0_b , 172nullptr ,nullptr , 173model .mm_1_w ,model .mm_1_b , 174FFN_GELU , 175-1 ); 176ggml_build_forward_expand (gf ,embeddings ); 177 178return gf ; 179}