char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
f5c6ae182
char/aliceai-t5-moe
1// similar to qwen2vl, except for GQA attention 2#include "models.h" 3 4ggml_cgraph * clip_graph_exaone4_5::build () { 5GGML_ASSERT (model .patch_bias == nullptr ); 6GGML_ASSERT (model .class_embedding == nullptr ); 7 8const int batch_size = 1 ; 9const bool use_window_attn = hparams .n_wa_pattern > 0 ; 10const int n_wa_pattern = hparams .n_wa_pattern ; 11const int n_pos = n_patches ; 12const int num_position_ids = n_pos * 4 ; 13 14const norm_type norm_t = NORM_TYPE_RMS ; 15 16const int64_t n_kv_head = hparams .n_head_kv > 0 ?hparams .n_head_kv :n_head ; 17GGML_ASSERT (n_head %n_kv_head == 0 ); 18 19int rope_sections [4 ]= {d_head /4 ,d_head /4 ,d_head /4 ,d_head /4 }; 20const float rope_freq_base = hparams .rope_theta > 0.0f ?hparams .rope_theta :10000.0f ; 21 22ggml_tensor * inp_raw = build_inp_raw (); 23ggml_tensor * inp = ggml_conv_2d (ctx0 ,model .patch_embeddings_0 ,inp_raw ,patch_size ,patch_size ,0 ,0 ,1 ,1 ); 24 25GGML_ASSERT (img .nx () % (patch_size * 2 )== 0 ); 26GGML_ASSERT (img .ny () % (patch_size * 2 )== 0 ); 27 28 { 29ggml_tensor * inp_1 = ggml_conv_2d (ctx0 ,model .patch_embeddings_1 ,inp_raw ,patch_size ,patch_size ,0 ,0 ,1 ,1 ); 30inp = ggml_add (ctx0 ,inp ,inp_1 ); 31inp = ggml_permute (ctx0 ,inp ,1 ,2 ,0 ,3 ); 32inp = ggml_cont_4d ( 33ctx0 ,inp , 34n_embd * 2 ,n_patches_x /2 ,n_patches_y ,batch_size ); 35inp = ggml_reshape_4d ( 36ctx0 ,inp , 37n_embd * 2 ,n_patches_x /2 ,2 ,batch_size * (n_patches_y /2 )); 38inp = ggml_permute (ctx0 ,inp ,0 ,2 ,1 ,3 ); 39inp = ggml_cont_3d ( 40ctx0 ,inp , 41n_embd ,n_patches_x * n_patches_y ,batch_size ); 42 } 43 44ggml_tensor * positions = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,num_position_ids ); 45ggml_set_name (positions ,"positions" ); 46ggml_set_input (positions ); 47 48ggml_tensor * window_mask = nullptr ; 49ggml_tensor * window_idx = nullptr ; 50ggml_tensor * inv_window_idx = nullptr ; 51 52if (use_window_attn ) { 53window_idx = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_pos /4 ); 54ggml_set_name (window_idx ,"window_idx" ); 55ggml_set_input (window_idx ); 56 57inv_window_idx = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_pos /4 ); 58ggml_set_name (inv_window_idx ,"inv_window_idx" ); 59ggml_set_input (inv_window_idx ); 60 61window_mask = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,n_pos ,n_pos ); 62ggml_set_name (window_mask ,"window_mask" ); 63ggml_set_input (window_mask ); 64 65if (flash_attn_type == CLIP_FLASH_ATTN_TYPE_ENABLED ) { 66window_mask = ggml_cast (ctx0 ,window_mask ,GGML_TYPE_F16 ); 67 } 68 } 69 70ggml_tensor * inpL = inp ; 71 72if (use_window_attn ) { 73GGML_ASSERT (batch_size == 1 ); 74inpL = ggml_reshape_2d (ctx0 ,inpL ,n_embd * 4 ,n_patches_x * n_patches_y * batch_size /4 ); 75inpL = ggml_get_rows (ctx0 ,inpL ,inv_window_idx ); 76inpL = ggml_reshape_3d (ctx0 ,inpL ,n_embd ,n_patches_x * n_patches_y ,batch_size ); 77 } 78 79for (int il = 0 ;il < n_layer ;il ++ ) { 80const auto & layer = model .layers [il ]; 81const bool full_attn = use_window_attn ? (il + 1 ) %n_wa_pattern == 0 : true; 82ggml_tensor * cur = inpL ; 83 84cur = build_norm (cur ,layer .ln_1_w ,layer .ln_1_b ,norm_t ,eps ,il ); 85cb (cur ,"ln1" ,il ); 86 87 { 88GGML_ASSERT (layer .qkv_w != nullptr ); 89cur = build_mm (layer .qkv_w ,cur ); 90if (layer .qkv_b ) { 91cur = ggml_add (ctx0 ,cur ,layer .qkv_b ); 92 } 93 94const int64_t n_embd_kv = d_head * n_kv_head ; 95ggml_tensor * Qcur = ggml_view_3d (ctx0 ,cur ,d_head ,n_head ,n_patches , 96ggml_row_size (cur -> type ,d_head ), 97cur -> nb [1 ], 980 ); 99ggml_tensor * Kcur = ggml_view_3d (ctx0 ,cur ,d_head ,n_kv_head ,n_patches , 100ggml_row_size (cur -> type ,d_head ), 101cur -> nb [1 ], 102ggml_row_size (cur -> type ,n_embd )); 103ggml_tensor * Vcur = ggml_view_3d (ctx0 ,cur ,d_head ,n_kv_head ,n_patches , 104ggml_row_size (cur -> type ,d_head ), 105cur -> nb [1 ], 106ggml_row_size (cur -> type ,n_embd + n_embd_kv )); 107 108cb (Qcur ,"Qcur" ,il ); 109cb (Kcur ,"Kcur" ,il ); 110cb (Vcur ,"Vcur" ,il ); 111 112Qcur = ggml_rope_multi ( 113ctx0 ,Qcur ,positions ,nullptr , 114d_head /2 ,rope_sections ,GGML_ROPE_TYPE_VISION ,32768 ,rope_freq_base ,1 ,0 ,1 ,32 ,1 ); 115Kcur = ggml_rope_multi ( 116ctx0 ,Kcur ,positions ,nullptr , 117d_head /2 ,rope_sections ,GGML_ROPE_TYPE_VISION ,32768 ,rope_freq_base ,1 ,0 ,1 ,32 ,1 ); 118 119cb (Qcur ,"Qcur_rope" ,il ); 120cb (Kcur ,"Kcur_rope" ,il ); 121cb (Vcur ,"Vcur" ,il ); 122 123ggml_tensor * attn_mask = full_attn ?nullptr :window_mask ; 124cur = build_attn (layer .o_w ,layer .o_b ,Qcur ,Kcur ,Vcur ,attn_mask ,kq_scale ,il ); 125cb (cur ,"attn_out" ,il ); 126 } 127 128cur = ggml_add (ctx0 ,cur ,inpL ); 129inpL = cur ; 130 131cb (cur ,"ffn_inp" ,il ); 132 133cur = build_norm (cur ,layer .ln_2_w ,layer .ln_2_b ,norm_t ,eps ,il ); 134cb (cur ,"ffn_inp_normed" ,il ); 135 136cur = build_ffn (cur , 137layer .ff_up_w ,layer .ff_up_b , 138layer .ff_gate_w ,layer .ff_gate_b , 139layer .ff_down_w ,layer .ff_down_b , 140hparams .ffn_op ,il ); 141 142cb (cur ,"ffn_out" ,il ); 143 144cur = ggml_add (ctx0 ,inpL ,cur ); 145cb (cur ,"layer_out" ,il ); 146 147inpL = cur ; 148 } 149 150ggml_tensor * embeddings = inpL ; 151embeddings = build_norm (embeddings ,model .post_ln_w ,model .post_ln_b ,norm_t ,eps ,n_layer ); 152embeddings = ggml_reshape_3d (ctx0 ,embeddings ,n_embd * 4 ,n_pos /4 ,batch_size ); 153embeddings = build_ffn (embeddings , 154model .mm_0_w ,model .mm_0_b , 155nullptr ,nullptr , 156model .mm_1_w ,model .mm_1_b , 157FFN_GELU , 158-1 ); 159 160if (use_window_attn ) { 161GGML_ASSERT (batch_size == 1 ); 162embeddings = ggml_reshape_2d (ctx0 ,embeddings ,hparams .projection_dim ,n_patches_x * n_patches_y /4 ); 163embeddings = ggml_get_rows (ctx0 ,embeddings ,window_idx ); 164embeddings = ggml_reshape_3d (ctx0 ,embeddings ,hparams .projection_dim ,n_patches_x * n_patches_y /4 ,batch_size ); 165 } 166 167ggml_build_forward_expand (gf ,embeddings ); 168 169return gf ; 170}