char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
a3900a669
char/aliceai-t5-moe
1#include "models.h" 2 3#include <algorithm> 4 5ggml_cgraph * clip_graph_granite_speech::build () { 6const int n_frames = img .nx (); 7const int context_size = hparams .audio_chunk_size ; 8const int ctc_layer = n_layer /2 ; 9const int conv_kernel = hparams .audio_conv_kernel_size ; 10const int conv_pad = conv_kernel /2 ; 11 12const int num_blocks = (n_frames + context_size - 1 ) /context_size ; 13const int padded_len = num_blocks * context_size ; 14const int remainder = n_frames %context_size ; 15 16// Calculate projector input dimension based on feature layers 17const int proj_input_dim = n_embd * (hparams .feature_layers .size ()+ 1 ); 18const bool use_feature_concat = !hparams .feature_layers .empty (); 19 20ggml_tensor * attn_dists = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,context_size * context_size ); 21ggml_set_name (attn_dists ,"attn_dists" ); 22ggml_set_input (attn_dists ); 23 24ggml_tensor * attn_mask = nullptr ; 25if (remainder > 0 ) { 26attn_mask = ggml_new_tensor_4d (ctx0 ,GGML_TYPE_F32 , 27context_size ,context_size ,1 ,num_blocks ); 28ggml_set_name (attn_mask ,"attn_mask" ); 29ggml_set_input (attn_mask ); 30 } 31 32ggml_tensor * inp = build_inp_raw (1 ); 33auto * cur = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,inp )); 34cb (cur ,"inp_transposed" ,-1 ); 35 36cur = build_mm (model .inp_proj_w ,cur ); 37cur = ggml_add (ctx0 ,cur ,model .inp_proj_b ); 38cb (cur ,"inp_linear" ,-1 ); 39 40// Capture layer 0 if requested (after input_linear) 41ggml_tensor * concat_result = nullptr ; 42if (use_feature_concat ) { 43if (std::find (hparams .feature_layers .begin (),hparams .feature_layers .end (),0 )!= hparams .feature_layers .end ()) { 44concat_result = cur ; 45cb (concat_result ,"feature_layer_0" ,-1 ); 46 } 47 } 48 49for (int il = 0 ;il < n_layer ;il ++ ) { 50const auto & layer = model .layers [il ]; 51auto * residual = cur ; 52 53// ffn1 (half-step) 54 { 55auto * ffn1 = build_norm (cur ,layer .ff_norm_w ,layer .ff_norm_b , 56NORM_TYPE_NORMAL ,eps ,il ); 57cb (ffn1 ,"ffn1_norm" ,il ); 58 59ffn1 = build_ffn (ffn1 , 60layer .ff_up_w ,layer .ff_up_b , 61nullptr ,nullptr , 62layer .ff_down_w ,layer .ff_down_b , 63FFN_SILU ,il ); 64cb (ffn1 ,"ffn1_out" ,il ); 65 66residual = ggml_add (ctx0 ,residual ,ggml_scale (ctx0 ,ffn1 ,0.5f )); 67cb (residual ,"ffn1_residual" ,il ); 68 } 69 70// build_attn not used here: Shaw RPE needs pos_attn = mul_mat(pos_emb, Q) 71// injected between KQ product and softmax, which build_attn doesn't support 72 { 73auto * normed = build_norm (residual ,layer .ln_1_w ,layer .ln_1_b , 74NORM_TYPE_NORMAL ,eps ,il ); 75cb (normed ,"attn_norm" ,il ); 76 77if (n_frames < padded_len ) { 78normed = ggml_pad (ctx0 ,normed ,0 ,padded_len - n_frames ,0 ,0 ); 79 } 80 81ggml_tensor * Q = build_mm (layer .q_w ,normed ); 82ggml_tensor * K = build_mm (layer .k_w ,normed ); 83ggml_tensor * V = build_mm (layer .v_w ,normed ); 84 85Q = ggml_reshape_4d (ctx0 ,Q ,d_head ,n_head ,context_size ,num_blocks ); 86K = ggml_reshape_4d (ctx0 ,K ,d_head ,n_head ,context_size ,num_blocks ); 87V = ggml_reshape_4d (ctx0 ,V ,d_head ,n_head ,context_size ,num_blocks ); 88 89ggml_tensor * Q_perm = ggml_permute (ctx0 ,Q ,0 ,2 ,1 ,3 ); 90ggml_tensor * K_perm = ggml_cont (ctx0 ,ggml_permute (ctx0 ,K ,0 ,2 ,1 ,3 )); 91 92ggml_tensor * kq = ggml_mul_mat (ctx0 ,K_perm ,Q_perm ); 93 94// Shaw RPE: pos_emb ne[2]=1 broadcasts against Q ne[2]=num_blocks in mul_mat 95ggml_tensor * pos_emb = ggml_get_rows (ctx0 ,layer .attn_rel_pos_emb ,attn_dists ); 96pos_emb = ggml_reshape_3d (ctx0 ,pos_emb ,d_head ,context_size ,context_size ); 97pos_emb = ggml_reshape_4d (ctx0 ,pos_emb ,d_head ,context_size ,1 ,context_size ); 98 99ggml_tensor * Q_shaw = ggml_permute (ctx0 ,Q ,0 ,1 ,3 ,2 ); 100ggml_tensor * pos_attn = ggml_mul_mat (ctx0 ,pos_emb ,Q_shaw ); 101pos_attn = ggml_cont (ctx0 ,ggml_permute (ctx0 ,pos_attn ,0 ,2 ,3 ,1 )); 102 103ggml_tensor * scores = ggml_add (ctx0 ,kq ,pos_attn ); 104ggml_tensor * attn_weights = ggml_soft_max_ext (ctx0 ,scores ,attn_mask , 105kq_scale ,0.0f ); 106 107ggml_tensor * V_perm = ggml_cont (ctx0 ,ggml_permute (ctx0 ,V ,1 ,2 ,0 ,3 )); 108ggml_tensor * attn_out = ggml_mul_mat (ctx0 ,V_perm ,attn_weights ); 109 110attn_out = ggml_permute (ctx0 ,attn_out ,0 ,2 ,1 ,3 ); 111attn_out = ggml_cont_2d (ctx0 ,attn_out ,n_embd ,padded_len ); 112 113if (n_frames < padded_len ) { 114attn_out = ggml_view_2d (ctx0 ,attn_out , 115n_embd ,n_frames ,attn_out -> nb [1 ],0 ); 116 } 117 118cur = build_mm (layer .o_w ,attn_out ); 119cur = ggml_add (ctx0 ,cur ,layer .o_b ); 120cb (cur ,"attn_out" ,il ); 121 } 122 123residual = ggml_add (ctx0 ,residual ,cur ); 124 125// conv module 126 { 127cur = build_norm (residual ,layer .norm_conv_w ,layer .norm_conv_b , 128NORM_TYPE_NORMAL ,eps ,il ); 129cb (cur ,"conv_norm" ,il ); 130 131auto * x = build_mm (layer .conv_pw1_w ,cur ); 132x = ggml_add (ctx0 ,x ,layer .conv_pw1_b ); 133cb (x ,"conv_pw1" ,il ); 134 135// GLU: ggml has no fused op, manual split + sigmoid gate 136 { 137int64_t d = x -> ne [0 ] /2 ; 138ggml_tensor * gate = ggml_sigmoid (ctx0 , 139ggml_view_2d (ctx0 ,x ,d ,x -> ne [1 ],x -> nb [1 ],d * x -> nb [0 ])); 140x = ggml_mul (ctx0 , 141ggml_view_2d (ctx0 ,x ,d ,x -> ne [1 ],x -> nb [1 ],0 ),gate ); 142x = ggml_cont (ctx0 ,ggml_transpose (ctx0 ,x )); 143 } 144cb (x ,"conv_glu" ,il ); 145 146x = ggml_pad (ctx0 ,x ,conv_pad ,0 ,0 ,0 ); 147x = ggml_roll (ctx0 ,x ,conv_pad ,0 ,0 ,0 ); 148x = ggml_pad (ctx0 ,x ,conv_pad ,0 ,0 ,0 ); 149x = ggml_ssm_conv (ctx0 ,x ,layer .conv_dw_w ); 150cb (x ,"conv_dw" ,il ); 151 152// folded batch norm 153x = ggml_add (ctx0 ,ggml_mul (ctx0 ,x ,layer .conv_norm_w ),layer .conv_norm_b ); 154x = ggml_silu (ctx0 ,x ); 155cb (x ,"conv_bn_silu" ,il ); 156 157x = build_mm (layer .conv_pw2_w ,x ); 158x = ggml_add (ctx0 ,x ,layer .conv_pw2_b ); 159cb (x ,"conv_pw2" ,il ); 160 161cur = x ; 162 } 163 164residual = ggml_add (ctx0 ,residual ,cur ); 165 166// ffn2 (half-step) 167 { 168auto * ffn2 = build_norm (residual ,layer .ff_norm_1_w ,layer .ff_norm_1_b , 169NORM_TYPE_NORMAL ,eps ,il ); 170cb (ffn2 ,"ffn2_norm" ,il ); 171 172ffn2 = build_ffn (ffn2 , 173layer .ff_up_1_w ,layer .ff_up_1_b , 174nullptr ,nullptr , 175layer .ff_down_1_w ,layer .ff_down_1_b , 176FFN_SILU ,il ); 177cb (ffn2 ,"ffn2_out" ,il ); 178 179residual = ggml_add (ctx0 ,residual ,ggml_scale (ctx0 ,ffn2 ,0.5f )); 180 } 181 182cur = build_norm (residual ,layer .ln_2_w ,layer .ln_2_b , 183NORM_TYPE_NORMAL ,eps ,il ); 184cb (cur ,"layer_out" ,il ); 185 186// Capture intermediate layer (il + 1) if requested 187if (use_feature_concat ) { 188if (hparams .is_feature_layer (il + 1 )) { 189if (concat_result == nullptr ) { 190concat_result = cur ; 191 }else { 192concat_result = ggml_concat (ctx0 ,concat_result ,cur ,0 ); 193 } 194cb (concat_result ,string_format ("feature_layer_%d" ,il + 1 ).c_str (),il ); 195 } 196 } 197 198// CTC branch 199if (il + 1 == ctc_layer ) { 200auto * mid = build_mm (model .ctc_out_w ,cur ); 201mid = ggml_add (ctx0 ,mid ,model .ctc_out_b ); 202mid = ggml_soft_max (ctx0 ,mid ); 203mid = build_mm (model .ctc_out_mid_w ,mid ); 204mid = ggml_add (ctx0 ,mid ,model .ctc_out_mid_b ); 205cur = ggml_add (ctx0 ,cur ,mid ); 206cb (cur ,"ctc_branch" ,il ); 207 } 208 } 209 210// Append final output to concatenated features if using feature concatenation 211if (use_feature_concat && concat_result != nullptr ) { 212concat_result = ggml_concat (ctx0 ,concat_result ,cur ,0 ); 213cb (concat_result ,"concat_final" ,-1 ); 214cur = concat_result ; 215 } 216 217cb (cur ,"encoder_out" ,-1 ); 218 219// QFormer projector 220 { 221const int window_size = hparams .audio_proj_window_size ; 222const int num_queries = window_size /hparams .audio_proj_downsample_rate ; 223const int proj_n_head = hparams .audio_proj_head_count ; 224const int proj_d_head = n_embd /proj_n_head ; 225const float proj_kq_scale = 1.0f /sqrtf ((float )proj_d_head ); 226const float proj_eps = 1e-12f ; 227const int nblocks_proj = (n_frames + window_size - 1 ) /window_size ; 228const int padded_proj = nblocks_proj * window_size ; 229 230if (n_frames < padded_proj ) { 231cur = ggml_pad (ctx0 ,cur ,0 ,padded_proj - n_frames ,0 ,0 ); 232 } 233 234ggml_tensor * enc_windows = ggml_reshape_3d (ctx0 ,cur ,proj_input_dim ,window_size ,nblocks_proj ); 235 236ggml_tensor * queries = build_norm (model .qf_proj_blocks [0 ].qf_proj_query , 237model .qf_proj_blocks [0 ].qf_proj_norm_w ,model .qf_proj_blocks [0 ].qf_proj_norm_b , 238NORM_TYPE_NORMAL ,proj_eps ,-1 ); 239 { 240ggml_tensor * q_3d = ggml_reshape_3d (ctx0 ,queries ,n_embd ,num_queries ,1 ); 241ggml_tensor * q_shape = ggml_new_tensor_3d (ctx0 ,GGML_TYPE_F32 , 242n_embd ,num_queries ,nblocks_proj ); 243queries = ggml_repeat (ctx0 ,q_3d ,q_shape ); 244 } 245 246for (int il = 0 ;il < (int )model .qf_proj_blocks [0 ].qf_proj_layers .size ();il ++ ) { 247const auto & pl = model .qf_proj_blocks [0 ].qf_proj_layers [il ]; 248 249// self-attention 250 { 251ggml_tensor * Q = ggml_add (ctx0 ,build_mm (pl .q_w ,queries ),pl .q_b ); 252ggml_tensor * K = ggml_add (ctx0 ,build_mm (pl .k_w ,queries ),pl .k_b ); 253ggml_tensor * V = ggml_add (ctx0 ,build_mm (pl .v_w ,queries ),pl .v_b ); 254 255Q = ggml_reshape_4d (ctx0 ,Q ,proj_d_head ,proj_n_head ,num_queries ,nblocks_proj ); 256K = ggml_reshape_4d (ctx0 ,K ,proj_d_head ,proj_n_head ,num_queries ,nblocks_proj ); 257V = ggml_reshape_4d (ctx0 ,V ,proj_d_head ,proj_n_head ,num_queries ,nblocks_proj ); 258 259ggml_tensor * sa_out = build_attn (pl .o_w ,pl .o_b , 260Q ,K ,V ,nullptr ,proj_kq_scale ,il ); 261sa_out = ggml_reshape_3d (ctx0 ,sa_out ,n_embd ,num_queries ,nblocks_proj ); 262 263queries = build_norm (ggml_add (ctx0 ,sa_out ,queries ), 264pl .ln_1_w ,pl .ln_1_b , 265NORM_TYPE_NORMAL ,proj_eps ,il ); 266 } 267 268// cross-attention 269 { 270ggml_tensor * Q = ggml_add (ctx0 ,build_mm (pl .cross_attn_q_w ,queries ),pl .cross_attn_q_b ); 271ggml_tensor * K = ggml_add (ctx0 ,build_mm (pl .cross_attn_k_w ,enc_windows ),pl .cross_attn_k_b ); 272ggml_tensor * V = ggml_add (ctx0 ,build_mm (pl .cross_attn_v_w ,enc_windows ),pl .cross_attn_v_b ); 273 274Q = ggml_reshape_4d (ctx0 ,Q ,proj_d_head ,proj_n_head ,num_queries ,nblocks_proj ); 275K = ggml_reshape_4d (ctx0 ,K ,proj_d_head ,proj_n_head ,window_size ,nblocks_proj ); 276V = ggml_reshape_4d (ctx0 ,V ,proj_d_head ,proj_n_head ,window_size ,nblocks_proj ); 277 278ggml_tensor * ca_out = build_attn (pl .cross_attn_o_w ,pl .cross_attn_o_b , 279Q ,K ,V ,nullptr ,proj_kq_scale ,il ); 280ca_out = ggml_reshape_3d (ctx0 ,ca_out ,n_embd ,num_queries ,nblocks_proj ); 281 282queries = build_norm (ggml_add (ctx0 ,ca_out ,queries ), 283pl .cross_attn_norm_w ,pl .cross_attn_norm_b , 284NORM_TYPE_NORMAL ,proj_eps ,il ); 285 } 286 287// ffn 288 { 289ggml_tensor * ffn_out = build_ffn (queries , 290pl .ff_up_w ,pl .ff_up_b , 291nullptr ,nullptr , 292pl .ff_down_w ,pl .ff_down_b , 293FFN_GELU ,il ); 294 295queries = build_norm (ggml_add (ctx0 ,ffn_out ,queries ), 296pl .ln_2_w ,pl .ln_2_b , 297NORM_TYPE_NORMAL ,proj_eps ,il ); 298 } 299 } 300 301cur = ggml_reshape_2d (ctx0 ,queries ,n_embd ,num_queries * nblocks_proj ); 302cur = ggml_add (ctx0 ,build_mm (model .qf_proj_blocks [0 ].qf_proj_linear_w ,cur ),model .qf_proj_blocks [0 ].qf_proj_linear_b ); 303cb (cur ,"projector_out" ,-1 ); 304 } 305 306ggml_build_forward_expand (gf ,cur ); 307 308return gf ; 309}