char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
a2878d30d
char/aliceai-t5-moe
1#include "models.h" 2#include "llama-memory-recurrent.h" 3 4void llama_model_plamo2::load_arch_hparams (llama_model_loader & ml ) { 5ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 6 7// Load Mamba SSM parameters 8ml .get_key (LLM_KV_SSM_CONV_KERNEL ,hparams .ssm_d_conv ); 9ml .get_key (LLM_KV_SSM_INNER_SIZE ,hparams .ssm_d_inner ); 10ml .get_key (LLM_KV_SSM_STATE_SIZE ,hparams .ssm_d_state ); 11ml .get_key (LLM_KV_SSM_TIME_STEP_RANK ,hparams .ssm_dt_rank ); 12ml .get_key (LLM_KV_SSM_GROUP_COUNT ,hparams .ssm_n_group ); 13 14// Load attention parameters 15ml .get_key (LLM_KV_ATTENTION_KEY_LENGTH ,hparams .n_embd_head_k_full , false); 16ml .get_key (LLM_KV_ATTENTION_VALUE_LENGTH ,hparams .n_embd_head_v_full , false); 17 18for (uint32_t i = 0 ;i < hparams .n_layer ();++ i ) { 19hparams .is_recr_impl [i ]= hparams .n_head_kv (i )== 0 ; 20 } 21 22switch (hparams .n_layer ()) { 23case 16 :type = LLM_TYPE_1B ;break ; 24case 32 : 25if (hparams .n_embd == 2048 ) { 26type = LLM_TYPE_2B ; 27 }else if (hparams .n_embd == 4096 ) { 28type = LLM_TYPE_8B ; 29 } 30break ; 31default :type = LLM_TYPE_UNKNOWN ; 32 } 33} 34 35void llama_model_plamo2::load_arch_tensors (llama_model_loader & ) { 36LLAMA_LOAD_LOCALS ; 37 38// mamba parameters 39const uint32_t d_conv = hparams .ssm_d_conv ; 40const uint32_t d_state = hparams .ssm_d_state ; 41const uint32_t num_heads = hparams .ssm_dt_rank ; 42const uint32_t intermediate_size = hparams .ssm_d_inner ; 43const int64_t dt_dim = std::max (64 ,int (hparams .n_embd /16 )); 44 45// attention parameters 46const uint32_t qk_dim = hparams .n_embd_head_k (); 47const uint32_t v_dim = hparams .n_embd_head_v (); 48 49tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 50 51// output 52output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 53output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 54// if output is NULL, init from the input tok embed 55if (output == NULL ) { 56output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 57 } 58 59for (int i = 0 ;i < n_layer ;++ i ) { 60auto & layer = layers [i ]; 61bool is_mamba_layer = hparams .is_recr (i ); 62 63layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },0 ); 64 65if (is_mamba_layer ) { 66layer .ssm_in = create_tensor (tn (LLM_TENSOR_SSM_IN ,"weight" ,i ), {n_embd ,2 * intermediate_size },0 ); 67layer .ssm_conv1d = create_tensor (tn (LLM_TENSOR_SSM_CONV1D ,"weight" ,i ), {d_conv ,intermediate_size },0 ); 68 69layer .ssm_x = create_tensor (tn (LLM_TENSOR_SSM_X ,"weight" ,i ), {intermediate_size ,dt_dim + 2 * d_state },0 ); 70layer .ssm_dt = create_tensor (tn (LLM_TENSOR_SSM_DT ,"weight" ,i ), {dt_dim ,num_heads },0 ); 71layer .ssm_dt_b = create_tensor (tn (LLM_TENSOR_SSM_DT ,"bias" ,i ), {num_heads },0 ); 72 73layer .ssm_a = create_tensor (tn (LLM_TENSOR_SSM_A ,i ), {num_heads },0 ); 74layer .ssm_d = create_tensor (tn (LLM_TENSOR_SSM_D ,i ), {num_heads },0 ); 75 76layer .ssm_out = create_tensor (tn (LLM_TENSOR_SSM_OUT ,"weight" ,i ), {intermediate_size ,n_embd },0 ); 77 78layer .ssm_dt_norm = create_tensor (tn (LLM_TENSOR_SSM_DT_NORM ,i ), {dt_dim },0 ); 79layer .ssm_b_norm = create_tensor (tn (LLM_TENSOR_SSM_B_NORM ,i ), {d_state },0 ); 80layer .ssm_c_norm = create_tensor (tn (LLM_TENSOR_SSM_C_NORM ,i ), {d_state },0 ); 81 }else { 82const int64_t num_attention_heads = hparams .n_head (i ); 83const int64_t q_num_heads = num_attention_heads ; 84const int64_t num_key_value_heads = hparams .n_head_kv (i ); 85const int64_t k_num_heads = num_key_value_heads ; 86const int64_t v_num_heads = num_key_value_heads ; 87const int64_t q_proj_dim = q_num_heads * qk_dim ; 88const int64_t k_proj_dim = k_num_heads * qk_dim ; 89const int64_t v_proj_dim = v_num_heads * v_dim ; 90 91layer .wqkv = create_tensor (tn (LLM_TENSOR_ATTN_QKV ,"weight" ,i ), {n_embd ,q_proj_dim + k_proj_dim + v_proj_dim },0 ); 92layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {qk_dim ,num_attention_heads },0 ); 93layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {qk_dim ,k_num_heads },0 ); 94layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {q_num_heads * v_dim ,n_embd },0 ); 95 } 96 97// All layers have post-attention norm, FFN norm, and FFN tensors 98layer .attn_post_norm = create_tensor (tn (LLM_TENSOR_ATTN_POST_NORM ,i ), {n_embd },0 ); 99layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },0 ); 100layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },0 ); 101layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff * 2 },0 ); 102layer .ffn_post_norm = create_tensor (tn (LLM_TENSOR_FFN_POST_NORM ,i ), {n_embd },0 ); 103 } 104} 105 106std::unique_ptr < llm_graph_context > llama_model_plamo2::build_arch_graph (const llm_graph_params & params )const { 107return std::make_unique < graph > (* this ,params ); 108} 109 110llama_model_plamo2::graph::graph (const llama_model & model ,const llm_graph_params & params ) : 111llm_build_mamba_base (params ) { 112ggml_tensor * cur ; 113ggml_tensor * inpL ; 114 115// {n_embd, n_tokens} 116inpL = build_inp_embd (model .tok_embd ); 117cb (inpL ,"embedding_output" ,-1 ); 118 119ggml_tensor * inp_pos = build_inp_pos (); 120 121auto * inp_hybrid = build_inp_mem_hybrid (); 122 123ggml_tensor * inp_out_ids = build_inp_out_ids (); 124 125for (int il = 0 ;il < n_layer ;++ il ) { 126ggml_tensor * residual = inpL ; 127 128// ggml_graph_add_node(gf, model.layers[il].attn_norm); 129// cb(model.layers[il].attn_norm, "attn_norm", il); 130 131// pre_mixer_norm 132cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 133 134// check if this layer is Mamba or Attention 135const bool is_mamba_layer = hparams .is_recr (il ); 136 137if (is_mamba_layer ) { 138// PLaMo-2 Mamba layer 139cur = build_plamo2_mamba_layer (inp_hybrid -> get_recr (),cur ,model ,ubatch ,il ); 140 }else { 141// PLaMo-2 Attention layer 142cur = build_plamo2_attn_layer (inp_hybrid -> get_attn (),inp_pos ,cur ,model ,il ); 143 } 144 145if (il == n_layer - 1 && inp_out_ids ) { 146cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 147residual = ggml_get_rows (ctx0 ,residual ,inp_out_ids ); 148 } 149 150// post_mixer_norm 151cur = build_norm (cur ,model .layers [il ].attn_post_norm ,NULL ,LLM_NORM_RMS ,il ); 152cb (cur ,"attn_post_norm" ,il ); 153 154// residual connection 155cur = ggml_add (ctx0 ,cur ,residual ); 156cb (cur ,"attn_residual" ,il ); 157residual = cur ; 158 159// pre-ffn norm 160cur = build_norm (cur ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 161cb (cur ,"ffn_pre_norm" ,il ); 162 163// feed-forward network 164cur = build_ffn (cur , 165model .layers [il ].ffn_up ,NULL ,NULL , 166NULL ,NULL ,NULL , 167model .layers [il ].ffn_down ,NULL ,NULL , 168NULL ,LLM_FFN_SWIGLU ,LLM_FFN_SEQ ,il ); 169cb (cur ,"ffn_out" ,il ); 170 171// post ffn norm 172cur = build_norm (cur ,model .layers [il ].ffn_post_norm ,NULL ,LLM_NORM_RMS ,il ); 173cb (cur ,"ffn_post_norm" ,il ); 174 175// residual connection 176cur = ggml_add (ctx0 ,cur ,residual ); 177cb (cur ,"ffn_residual" ,il ); 178 179// input for next layer 180inpL = cur ; 181 } 182 183cur = inpL ; 184 185// final norm 186cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 187cb (cur ,"result_norm" ,-1 ); 188 189res -> t_embd = cur ; 190 191// lm_head 192cur = build_lora_mm (model .output ,cur ,model .output_s ); 193cb (cur ,"result_output" ,-1 ); 194 195// Explicitly mark as output tensor to ensure proper backend assignment 196ggml_set_output (cur ); 197 198res -> t_logits = cur ; 199 200ggml_build_forward_expand (gf ,cur ); 201} 202 203ggml_tensor * llama_model_plamo2::graph::build_plamo2_attn_layer (llm_graph_input_attn_kv * inp , 204ggml_tensor * inp_pos , 205ggml_tensor * cur , 206const llama_model & model , 207int il ) { 208// self-attention 209 { 210// PLaMo-2 uses combined QKV tensor 211ggml_tensor * qkv = build_lora_mm (model .layers [il ].wqkv ,cur ); 212cb (qkv ,"wqkv" ,il ); 213 214// split QKV tensor into Q, K, V 215const int64_t n_embd_head_q = hparams .n_embd_head_k (); 216const int64_t n_embd_head_k = hparams .n_embd_head_k (); 217const int64_t n_embd_head_v = hparams .n_embd_head_v (); 218int32_t n_head = hparams .n_head (il ); 219int32_t n_head_kv = hparams .n_head_kv (il ); 220 221const int64_t q_offset = 0 ; 222const int64_t k_offset = n_embd_head_q * n_head ; 223const int64_t v_offset = k_offset + n_embd_head_k * n_head_kv ; 224 225ggml_tensor * Qcur = ggml_view_3d (ctx0 ,qkv ,n_embd_head_q ,n_head ,n_tokens ,n_embd_head_q * sizeof (float ), 226qkv -> nb [1 ],q_offset * ggml_element_size (qkv )); 227ggml_tensor * Kcur = ggml_view_3d (ctx0 ,qkv ,n_embd_head_k ,n_head_kv ,n_tokens ,n_embd_head_k * sizeof (float ), 228qkv -> nb [1 ],k_offset * ggml_element_size (qkv )); 229ggml_tensor * Vcur = ggml_view_3d (ctx0 ,qkv ,n_embd_head_v ,n_head_kv ,n_tokens ,n_embd_head_v * sizeof (float ), 230qkv -> nb [1 ],v_offset * ggml_element_size (qkv )); 231 232cb (Qcur ,"Qcur" ,il ); 233cb (Kcur ,"Kcur" ,il ); 234cb (Vcur ,"Vcur" ,il ); 235 236Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,NULL ,LLM_NORM_RMS ,il ); 237cb (Qcur ,"Qcur_normed" ,il ); 238 239Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 240ext_factor ,attn_factor ,beta_fast ,beta_slow ); 241 242Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,NULL ,LLM_NORM_RMS ,il ); 243cb (Kcur ,"Kcur_normed" ,il ); 244 245Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 246ext_factor ,attn_factor ,beta_fast ,beta_slow ); 247 248cur = build_attn (inp , 249model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 250Qcur ,Kcur ,Vcur ,NULL ,NULL ,NULL ,1.0f /sqrtf (float (n_embd_head_v )),il ); 251 } 252 253cb (cur ,"attn_out" ,il ); 254 255return cur ; 256} 257 258ggml_tensor * llama_model_plamo2::graph::build_plamo2_mamba_layer (llm_graph_input_rs * inp , 259ggml_tensor * cur , 260const llama_model & model , 261const llama_ubatch & ubatch , 262int il ) { 263const auto * mctx_cur = inp -> mctx ; 264 265const auto kv_head = mctx_cur -> get_head (); 266 267const int64_t d_conv = hparams .ssm_d_conv ; 268const int64_t d_inner = hparams .ssm_d_inner ; 269const int64_t d_state = hparams .ssm_d_state ; 270const int64_t n_heads = hparams .ssm_dt_rank ; 271const int64_t head_dim = d_inner /n_heads ; 272const int64_t n_group = hparams .ssm_n_group ; 273const int64_t n_seqs = ubatch .n_seqs ; 274 275const int64_t n_seq_tokens = ubatch .n_seq_tokens ; 276 277GGML_ASSERT (n_seqs != 0 ); 278GGML_ASSERT (ubatch .equal_seqs ()); 279GGML_ASSERT (ubatch .n_tokens == n_seq_tokens * n_seqs ); 280GGML_ASSERT (d_inner %n_heads == 0 ); 281GGML_ASSERT (n_group == 0 ); 282 283ggml_tensor * conv_states_all = mctx_cur -> get_r_l (il ); 284ggml_tensor * ssm_states_all = mctx_cur -> get_s_l (il ); 285 286ggml_tensor * conv = build_rs (inp ,conv_states_all ,hparams .n_embd_r (),n_seqs ); 287conv = ggml_reshape_3d (ctx0 ,conv ,d_conv - 1 ,d_inner + 2 * n_group * d_state ,n_seqs ); 288 289// {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs} 290cur = ggml_reshape_3d (ctx0 ,cur ,cur -> ne [0 ],n_seq_tokens ,n_seqs ); 291 292// in_proj: {n_embd, 2*d_inner} @ {n_embd, n_seq_tokens, n_seqs} => {2*d_inner, n_seq_tokens, n_seqs} 293ggml_tensor * zx = build_lora_mm (model .layers [il ].ssm_in ,cur ); 294cb (zx ,"mamba_in_proj" ,il ); 295// {8192, 5, 1, 1} -> {8192, 1, 5, 1} 296zx = ggml_permute (ctx0 ,zx ,0 ,2 ,1 ,3 ); 297zx = ggml_cont_4d (ctx0 ,zx ,head_dim * 2 ,n_heads ,n_seq_tokens ,n_seqs ); 298cb (zx ,"mamba_in_proj_out" ,il ); 299 300// split into z and x 301// => {head_dim * n_heads, n_seq_tokens, n_seqs} 302ggml_tensor * x = ggml_view_4d (ctx0 ,zx ,head_dim ,n_heads ,n_seq_tokens ,n_seqs ,zx -> nb [1 ],zx -> nb [2 ],zx -> nb [3 ], 303head_dim * ggml_element_size (zx )); 304x = ggml_cont_3d (ctx0 ,x ,head_dim * n_heads ,n_seq_tokens ,n_seqs ); 305// x = ggml_permute(ctx0, x, 0, 2, 1, 3); 306cb (x ,"mamba_x_split" ,il ); 307 308ggml_tensor * z = 309ggml_view_4d (ctx0 ,zx ,head_dim ,n_heads ,n_seq_tokens ,n_seqs ,zx -> nb [1 ],zx -> nb [2 ],zx -> nb [3 ],0 ); 310cb (z ,"mamba_z_split" ,il ); 311 312// conv1d 313 { 314// => {d_conv - 1 + n_seq_tokens, d_inner, n_seqs} 315ggml_tensor * conv_x = ggml_concat (ctx0 ,conv ,ggml_transpose (ctx0 ,x ),0 ); 316cb (conv_x ,"mamba_conv1d_input" ,il ); 317 318// copy last (d_conv - 1) columns back into the state cache 319ggml_tensor * last_conv = ggml_view_3d (ctx0 ,conv_x ,d_conv - 1 ,d_inner ,n_seqs ,conv_x -> nb [1 ],conv_x -> nb [2 ], 320n_seq_tokens * (conv_x -> nb [0 ])); 321 322ggml_build_forward_expand (gf ,ggml_cpy (ctx0 ,last_conv , 323ggml_view_1d (ctx0 ,conv_states_all , 324 (d_conv - 1 )* (d_inner + 2 * n_group * d_state )* (n_seqs ), 325kv_head * (d_conv - 1 )* (d_inner + 2 * n_group * d_state )* 326ggml_element_size (conv_states_all )))); 327cb (conv_states_all ,"mamba_conv1d_state" ,il ); 328 329// 1D convolution 330x = ggml_ssm_conv (ctx0 ,conv_x ,model .layers [il ].ssm_conv1d ); 331cb (x ,"mamba_conv1d" ,il ); 332 333x = ggml_silu (ctx0 ,x ); 334cb (x ,"mamba_conv1d_silu" ,il ); 335 } 336 337// SSM 338 { 339// bcdt_proj: {d_inner, dt_rank + 2*d_state} @ {d_inner, n_seq_tokens, n_seqs} => {dt_rank + 2*d_state, n_seq_tokens, n_seqs} 340ggml_tensor * x_bcdt = build_lora_mm (model .layers [il ].ssm_x ,x ); 341cb (x_bcdt ,"mamba_bcdt_proj" ,il ); 342 343// split into dt, B, C 344const int64_t dt_dim = std::max (64 ,int (hparams .n_embd /16 )); 345ggml_tensor * B = ggml_view_3d (ctx0 ,x_bcdt ,d_state ,n_seq_tokens ,n_seqs ,x_bcdt -> nb [1 ],x_bcdt -> nb [2 ],0 ); 346ggml_tensor * C = ggml_view_3d (ctx0 ,x_bcdt ,d_state ,n_seq_tokens ,n_seqs ,x_bcdt -> nb [1 ],x_bcdt -> nb [2 ], 347ggml_element_size (x_bcdt )* d_state ); 348ggml_tensor * dt = ggml_view_3d (ctx0 ,x_bcdt ,dt_dim ,n_seq_tokens ,n_seqs ,x_bcdt -> nb [1 ],x_bcdt -> nb [2 ], 349ggml_element_size (x_bcdt )* (2 * d_state )); 350cb (B ,"mamba_B_raw" ,il ); 351cb (C ,"mamba_C_raw" ,il ); 352cb (dt ,"mamba_dt_raw" ,il ); 353 354// Apply RMS norm to dt, B, C (PLaMo-2 specific) 355B = build_norm (B ,model .layers [il ].ssm_b_norm ,NULL ,LLM_NORM_RMS ,il ); 356C = build_norm (C ,model .layers [il ].ssm_c_norm ,NULL ,LLM_NORM_RMS ,il ); 357dt = build_norm (dt ,model .layers [il ].ssm_dt_norm ,NULL ,LLM_NORM_RMS ,il ); 358cb (B ,"mamba_B_normed" ,il ); 359cb (C ,"mamba_C_normed" ,il ); 360cb (dt ,"mamba_dt_normed" ,il ); 361 362// dt_proj: {dt_rank, d_inner} @ {dt_rank, n_seq_tokens, n_seqs} => {d_inner, n_seq_tokens, n_seqs} 363dt = build_lora_mm (model .layers [il ].ssm_dt ,dt ); 364dt = ggml_add (ctx0 ,dt ,model .layers [il ].ssm_dt_b ); 365cb (dt ,"mamba_dt_proj" ,il ); 366 367ggml_tensor * A = ggml_reshape_2d (ctx0 ,model .layers [il ].ssm_a ,1 ,n_heads ); 368cb (A ,"mamba_A" ,il ); 369 370x = ggml_view_4d (ctx0 ,x ,head_dim ,n_heads ,n_seq_tokens ,n_seqs ,head_dim * ggml_element_size (x ), 371head_dim * n_heads * ggml_element_size (x ), 372head_dim * n_heads * n_seq_tokens * ggml_element_size (x ),0 ); 373B = ggml_view_4d (ctx0 ,B ,d_state ,1 ,n_seq_tokens ,n_seqs ,d_state * B -> nb [0 ],B -> nb [1 ],B -> nb [2 ],0 ); 374C = ggml_view_4d (ctx0 ,C ,d_state ,1 ,n_seq_tokens ,n_seqs ,d_state * C -> nb [0 ],C -> nb [1 ],C -> nb [2 ],0 ); 375 376// use the states and the indices provided by build_recurrent_state 377// (this is necessary in order to properly use the states before they are overwritten, 378// while avoiding to make unnecessary copies of the states) 379auto get_ssm_rows = [& ](ggml_context * ctx ,ggml_tensor * states ,ggml_tensor * ids ) { 380ggml_tensor * ssm = ggml_reshape_4d (ctx ,states ,d_state ,head_dim ,n_heads ,mctx_cur -> get_size ()); 381 382// Custom operator to optimize the parallel associative scan 383// as described in the Annex D of the Mamba paper. 384// => {d_inner, n_seq_tokens, n_seqs} and {d_state, d_inner, n_seqs} 385return ggml_ssm_scan (ctx ,ssm ,x ,dt ,A ,B ,C ,ids ,/*K=*/ 1 ); 386 }; 387 388ggml_tensor * y_ssm = build_rs (inp ,ssm_states_all ,hparams .n_embd_s (),ubatch .n_seqs ,get_ssm_rows ); 389cb (y_ssm ,"mamba_ssm_scan" ,il ); 390 391// store last states 392ggml_build_forward_expand ( 393gf ,ggml_cpy ( 394ctx0 , 395ggml_view_1d (ctx0 ,y_ssm ,n_heads * head_dim * d_state * n_seqs , 396n_heads * head_dim * n_seq_tokens * n_seqs * ggml_element_size (y_ssm )), 397ggml_view_1d (ctx0 ,ssm_states_all ,n_heads * head_dim * d_state * n_seqs , 398kv_head * n_seqs * n_heads * head_dim * d_state * ggml_element_size (ssm_states_all )))); 399cb (ssm_states_all ,"mamba_ssm_states" ,il ); 400 401ggml_tensor * y = ggml_view_4d (ctx0 ,y_ssm ,head_dim ,n_heads ,n_seq_tokens ,n_seqs , 402head_dim * ggml_element_size (x ),head_dim * n_heads * ggml_element_size (x ), 403head_dim * n_heads * n_seq_tokens * ggml_element_size (x ),0 ); 404cb (y ,"mamba_y_view" ,il ); 405 406// Add D parameter and apply gating with z 407// {d_inner, n_seq_tokens, n_seqs} * {d_inner} => {d_inner, n_seq_tokens, n_seqs} 408ggml_tensor * D = ggml_reshape_2d (ctx0 ,model .layers [il ].ssm_d ,1 ,n_heads ); 409y = ggml_add (ctx0 ,y ,ggml_mul (ctx0 ,x ,D )); 410cb (y ,"mamba_y_add_d" ,il ); 411 412y = ggml_swiglu_split (ctx0 ,ggml_cont (ctx0 ,z ),y ); 413cb (y ,"mamba_y_swiglu_z" ,il ); 414 415// out_proj: {d_inner, n_embd} @ {d_inner, n_seq_tokens, n_seqs} => {n_embd, n_seq_tokens, n_seqs} 416y = ggml_view_3d (ctx0 ,y ,head_dim * n_heads ,n_seq_tokens ,n_seqs ,y -> nb [2 ],y -> nb [3 ],0 ); 417cur = build_lora_mm (model .layers [il ].ssm_out ,y ); 418cb (cur ,"mamba_out_proj" ,il ); 419 } 420 421// {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens} 422cur = ggml_reshape_2d (ctx0 ,cur ,cur -> ne [0 ],n_seq_tokens * n_seqs ); 423cb (cur ,"mamba_out" ,il ); 424 425return cur ; 426}