char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
5fdfa6282
char/aliceai-t5-moe
1#include "models.h" 2#include "llama-memory-recurrent.h" 3 4#include <algorithm> 5 6void llama_model_bailingmoe3::load_arch_hparams (llama_model_loader & ml ) { 7ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 8ml .get_key (LLM_KV_ATTENTION_KEY_LENGTH_MLA ,hparams .n_embd_head_k_mla_impl ); 9ml .get_key (LLM_KV_ATTENTION_VALUE_LENGTH_MLA ,hparams .n_embd_head_v_mla_impl ); 10ml .get_key (LLM_KV_ATTENTION_KV_LORA_RANK ,hparams .n_lora_kv ); 11ml .get_key (LLM_KV_ATTENTION_Q_LORA_RANK ,hparams .n_lora_q , false); 12ml .get_key (LLM_KV_SSM_CONV_KERNEL ,hparams .ssm_d_conv ); 13ml .get_key (LLM_KV_KDA_HEAD_DIM ,hparams .n_embd_head_kda ); 14if (!ml .get_key (LLM_KV_KDA_SAFE_GATE ,hparams .kda_safe_gate , false)) { 15hparams .kda_safe_gate = true; 16 } 17ml .get_key (LLM_KV_KDA_GATE_LOWER_BOUND ,hparams .kda_gate_lower_bound ); 18ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all ); 19ml .get_key (LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH ,hparams .n_ff_shexp , false); 20ml .get_key (LLM_KV_EXPERT_SHARED_COUNT ,hparams .n_expert_shared ); 21ml .get_key (LLM_KV_LEADING_DENSE_BLOCK_COUNT ,hparams .n_layer_dense_lead ); 22ml .get_key (LLM_KV_EXPERT_WEIGHTS_SCALE ,hparams .expert_weights_scale , false); 23ml .get_key (LLM_KV_EXPERT_WEIGHTS_NORM ,hparams .expert_weights_norm , false); 24ml .get_key (LLM_KV_EXPERT_GATING_FUNC ,hparams .expert_gating_func ); 25ml .get_key_or_arr (LLM_KV_SWIGLU_CLAMP_EXP ,hparams .swiglu_clamp_exp ,hparams .n_layer_all , false); 26ml .get_key_or_arr (LLM_KV_SWIGLU_CLAMP_SHEXP ,hparams .swiglu_clamp_shexp ,hparams .n_layer_all , false); 27 28if (hparams .n_ff_shexp == 0 ) { 29hparams .n_ff_shexp = hparams .n_ff_exp ()* std::max (1u ,hparams .n_expert_shared ); 30 } 31 32GGML_ASSERT (hparams .kda_safe_gate ); 33GGML_ASSERT (hparams .kda_gate_lower_bound < 0.0f ); 34 35for (uint32_t il = 0 ;il < hparams .n_layer ();++ il ) { 36hparams .is_recr_impl [il ]= hparams .n_head_kv (il )== 0 ; 37 } 38 39switch (hparams .n_layer ()) { 40case 24 :type = hparams .n_embd == 1536 && hparams .n_expert == 128 ?LLM_TYPE_7_9B_A1_3B :LLM_TYPE_UNKNOWN ;break ; 41case 42 :type = hparams .n_embd == 2560 && hparams .n_expert == 512 ?LLM_TYPE_124B_A5_1B :LLM_TYPE_UNKNOWN ;break ; 42default :type = LLM_TYPE_UNKNOWN ; 43 } 44} 45 46void llama_model_bailingmoe3::load_arch_tensors (llama_model_loader & ml ) { 47LLAMA_LOAD_LOCALS ; 48 49tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 50 51output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 52output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 53if (output == nullptr ) { 54output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 55 } 56 57const int64_t head_dim = hparams .n_embd_head_kda ; 58const int64_t d_inner = head_dim * n_head ; 59const int64_t d_conv = hparams .ssm_d_conv ; 60const int64_t kv_lora_rank = hparams .n_lora_kv ; 61const int64_t q_lora_rank = hparams .n_lora_q ; 62const int64_t qk_rope_head_dim = hparams .n_rot (); 63const int64_t qk_head_dim = hparams .n_embd_head_k_mla (); 64const int64_t v_head_dim = hparams .n_embd_head_v_mla (); 65 66const bool mtp_only = (hparams .n_layer_nextn > 0 )&& (ml .get_weight ("blk.0.attn_norm.weight" )== nullptr ); 67const std::string mtp_probe = "blk." + std::to_string (n_layer )+ ".nextn.eh_proj.weight" ; 68const bool trunk_only = (hparams .n_layer_nextn > 0 )&& (ml .get_weight (mtp_probe .c_str ())== nullptr ); 69const int trunk_flags = mtp_only ?TENSOR_NOT_REQUIRED :0 ; 70int mtp_flags = trunk_only ?TENSOR_NOT_REQUIRED :0 ; 71 72if (!ml .load_mtp ) { 73mtp_flags |=TENSOR_SKIP ; 74 } 75 76for (int il = 0 ;il < n_layer ;++ il ) { 77auto & layer = layers [il ]; 78 79layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,il ), {n_embd },trunk_flags ); 80 81if (hparams .is_recr (il )) { 82layer .ssm_q_conv = create_tensor (tn (LLM_TENSOR_SSM_CONV1D_Q ,"weight" ,il ), {d_conv ,1 ,d_inner ,1 },trunk_flags ); 83layer .ssm_k_conv = create_tensor (tn (LLM_TENSOR_SSM_CONV1D_K ,"weight" ,il ), {d_conv ,1 ,d_inner ,1 },trunk_flags ); 84layer .ssm_v_conv = create_tensor (tn (LLM_TENSOR_SSM_CONV1D_V ,"weight" ,il ), {d_conv ,1 ,d_inner ,1 },trunk_flags ); 85 86create_tensor_qkv (layer ,il ,n_embd ,d_inner ,d_inner ,d_inner ,trunk_flags ); 87layer .ssm_f_a = create_tensor (tn (LLM_TENSOR_SSM_F_A ,"weight" ,il ), {n_embd ,d_inner },trunk_flags ); 88layer .ssm_beta = create_tensor (tn (LLM_TENSOR_SSM_BETA ,"weight" ,il ), {n_embd ,n_head },trunk_flags ); 89layer .ssm_a = create_tensor (tn (LLM_TENSOR_SSM_A_NOSCAN ,il ), {1 ,n_head },trunk_flags ); 90layer .ssm_dt_b = create_tensor (tn (LLM_TENSOR_SSM_DT ,"bias" ,il ), {d_inner },trunk_flags ); 91layer .ssm_g_a = create_tensor (tn (LLM_TENSOR_SSM_G_A ,"weight" ,il ), {n_embd ,d_inner },trunk_flags ); 92layer .ssm_o_norm = create_tensor (tn (LLM_TENSOR_SSM_NORM ,"weight" ,il ), {head_dim },trunk_flags ); 93layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,il ), {d_inner ,n_embd },trunk_flags ); 94 }else { 95if (q_lora_rank > 0 ) { 96layer .wq_a = create_tensor (tn (LLM_TENSOR_ATTN_Q_A ,"weight" ,il ), {n_embd ,q_lora_rank },trunk_flags ); 97layer .attn_q_a_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_A_NORM ,"weight" ,il ), {q_lora_rank },trunk_flags ); 98layer .wq_b = create_tensor (tn (LLM_TENSOR_ATTN_Q_B ,"weight" ,il ), {q_lora_rank ,n_head * qk_head_dim },trunk_flags ); 99 }else { 100layer .wq = create_tensor (tn (LLM_TENSOR_ATTN_Q ,"weight" ,il ), {n_embd ,n_head * qk_head_dim },trunk_flags ); 101 } 102layer .wkv_a_mqa = create_tensor (tn (LLM_TENSOR_ATTN_KV_A_MQA ,"weight" ,il ), {n_embd ,kv_lora_rank + qk_rope_head_dim },trunk_flags ); 103layer .attn_kv_a_norm = create_tensor (tn (LLM_TENSOR_ATTN_KV_A_NORM ,"weight" ,il ), {kv_lora_rank },trunk_flags ); 104layer .wk_b = create_tensor (tn (LLM_TENSOR_ATTN_K_B ,"weight" ,il ), {qk_head_dim - qk_rope_head_dim ,kv_lora_rank ,n_head },trunk_flags ); 105layer .wv_b = create_tensor (tn (LLM_TENSOR_ATTN_V_B ,"weight" ,il ), {kv_lora_rank ,v_head_dim ,n_head },trunk_flags ); 106layer .wqkv_gate = create_tensor (tn (LLM_TENSOR_ATTN_GATE ,"weight" ,il ), {n_embd ,n_head },trunk_flags ); 107layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,il ), {n_head * v_head_dim ,n_embd },trunk_flags ); 108 } 109 110layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,il ), {n_embd },trunk_flags ); 111if ((uint32_t )il < hparams .n_layer_dense_lead ) { 112layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,il ), {n_embd ,n_ff },trunk_flags ); 113layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,il ), {n_embd ,n_ff },trunk_flags ); 114layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,il ), {n_ff ,n_embd },trunk_flags ); 115 }else { 116layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,il ), {n_embd ,n_expert },trunk_flags ); 117layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,il ), {n_expert },trunk_flags ); 118layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,il ), {n_embd ,hparams .n_ff_exp (),n_expert },trunk_flags ); 119layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,il ), {n_embd ,hparams .n_ff_exp (),n_expert },trunk_flags ); 120layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,il ), {hparams .n_ff_exp (),n_embd ,n_expert },trunk_flags ); 121layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,il ), {n_embd ,hparams .n_ff_shexp },trunk_flags ); 122layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,il ), {n_embd ,hparams .n_ff_shexp },trunk_flags ); 123layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,il ), {hparams .n_ff_shexp ,n_embd },trunk_flags ); 124 } 125 } 126 127for (int il = n_layer ;il < n_layer_all ;++ il ) { 128auto & layer = layers [il ]; 129const int flags = mtp_flags ; 130 131layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,il ), {n_embd },flags ); 132if (q_lora_rank > 0 ) { 133layer .wq_a = create_tensor (tn (LLM_TENSOR_ATTN_Q_A ,"weight" ,il ), {n_embd ,q_lora_rank },flags ); 134layer .attn_q_a_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_A_NORM ,"weight" ,il ), {q_lora_rank },flags ); 135layer .wq_b = create_tensor (tn (LLM_TENSOR_ATTN_Q_B ,"weight" ,il ), {q_lora_rank ,n_head * qk_head_dim },flags ); 136 }else { 137layer .wq = create_tensor (tn (LLM_TENSOR_ATTN_Q ,"weight" ,il ), {n_embd ,n_head * qk_head_dim },flags ); 138 } 139layer .wkv_a_mqa = create_tensor (tn (LLM_TENSOR_ATTN_KV_A_MQA ,"weight" ,il ), {n_embd ,kv_lora_rank + qk_rope_head_dim },flags ); 140layer .attn_kv_a_norm = create_tensor (tn (LLM_TENSOR_ATTN_KV_A_NORM ,"weight" ,il ), {kv_lora_rank },flags ); 141layer .wk_b = create_tensor (tn (LLM_TENSOR_ATTN_K_B ,"weight" ,il ), {qk_head_dim - qk_rope_head_dim ,kv_lora_rank ,n_head },flags ); 142layer .wv_b = create_tensor (tn (LLM_TENSOR_ATTN_V_B ,"weight" ,il ), {kv_lora_rank ,v_head_dim ,n_head },flags ); 143layer .wqkv_gate = create_tensor (tn (LLM_TENSOR_ATTN_GATE ,"weight" ,il ), {n_embd ,n_head },flags ); 144layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,il ), {n_head * v_head_dim ,n_embd },flags ); 145layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,il ), {n_embd },flags ); 146layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,il ), {n_embd ,n_expert },flags ); 147layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,il ), {n_expert },flags ); 148layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,il ), {n_embd ,hparams .n_ff_exp (),n_expert },flags ); 149layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,il ), {n_embd ,hparams .n_ff_exp (),n_expert },flags ); 150layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,il ), {hparams .n_ff_exp (),n_embd ,n_expert },flags ); 151layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,il ), {n_embd ,hparams .n_ff_shexp },flags ); 152layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,il ), {n_embd ,hparams .n_ff_shexp },flags ); 153layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,il ), {hparams .n_ff_shexp ,n_embd },flags ); 154layer .nextn .eh_proj = create_tensor (tn (LLM_TENSOR_NEXTN_EH_PROJ ,"weight" ,il ), {2 * n_embd ,n_embd },flags ); 155layer .nextn .enorm = create_tensor (tn (LLM_TENSOR_NEXTN_ENORM ,"weight" ,il ), {n_embd },flags ); 156layer .nextn .hnorm = create_tensor (tn (LLM_TENSOR_NEXTN_HNORM ,"weight" ,il ), {n_embd },flags ); 157layer .nextn .shared_head_norm = create_tensor (tn (LLM_TENSOR_LAYER_OUT_NORM ,"weight" ,il ), {n_embd },flags ); 158 } 159} 160 161std::unique_ptr < llm_graph_context > llama_model_bailingmoe3::build_arch_graph (const llm_graph_params & params )const { 162if (params .gtype == LLM_GRAPH_TYPE_DECODER_MTP ) { 163return std::make_unique < graph_mtp > (* this ,params ); 164 } 165return std::make_unique < graph > (* this ,params ); 166} 167 168static ggml_tensor * bailingmoe3_causal_conv1d ( 169ggml_cgraph * gf , 170ggml_context * ctx0 , 171ggml_tensor * conv_states_all , 172ggml_tensor * conv_state_all , 173int64_t qkv , 174ggml_tensor * x , 175ggml_tensor * proj_w , 176ggml_tensor * conv_w , 177int64_t d_conv , 178int64_t head_dim , 179int64_t n_head , 180int64_t n_seq_tokens , 181int64_t n_seqs , 182int64_t n_tokens , 183int64_t cache_head , 184uint32_t mem_size , 185uint32_t n_rs_seq ) { 186const int64_t d_inner = head_dim * n_head ; 187const int64_t conv_state_size = (d_conv - 1 )* d_inner ; 188const int64_t total_state_size = 3 * conv_state_size ; 189 190ggml_tensor * conv_state = ggml_view_3d (ctx0 ,conv_state_all ,d_conv - 1 ,d_inner ,n_seqs , 191 (d_conv - 1 )* ggml_element_size (conv_state_all ), 192total_state_size * ggml_element_size (conv_state_all ), 193qkv * conv_state_size * ggml_element_size (conv_state_all )); 194 195ggml_tensor * x_proj = ggml_mul_mat (ctx0 ,proj_w ,x ); 196x_proj = ggml_reshape_3d (ctx0 ,x_proj ,d_inner ,n_seq_tokens ,n_seqs ); 197ggml_tensor * conv_x = ggml_concat (ctx0 ,conv_state ,ggml_transpose (ctx0 ,x_proj ),0 ); 198 199const int64_t K = (int64_t )n_rs_seq + 1 ; 200const int64_t n_written = std::min < int64_t > (n_seq_tokens ,K ); 201 202for (int64_t slot = 0 ;slot < n_written ;++ slot ) { 203ggml_tensor * conv_snap = ggml_view_3d (ctx0 ,conv_x ,d_conv - 1 ,d_inner ,n_seqs , 204conv_x -> nb [1 ],conv_x -> nb [2 ], (conv_x -> ne [0 ]- (d_conv - 1 )- slot )* conv_x -> nb [0 ]); 205ggml_build_forward_expand (gf ,ggml_cpy (ctx0 ,conv_snap , 206ggml_view_3d (ctx0 ,conv_states_all ,d_conv - 1 ,d_inner ,n_seqs , 207 (d_conv - 1 )* ggml_element_size (conv_states_all ), 208total_state_size * ggml_element_size (conv_states_all ), 209 ((slot * mem_size + cache_head )* total_state_size + qkv * conv_state_size )* ggml_element_size (conv_states_all )))); 210 } 211 212ggml_tensor * conv_weight = ggml_reshape_2d (ctx0 ,conv_w ,d_conv ,d_inner ); 213ggml_tensor * out = ggml_ssm_conv (ctx0 ,conv_x ,conv_weight ); 214out = ggml_silu (ctx0 ,ggml_reshape_2d (ctx0 ,out ,d_inner ,n_tokens )); 215return ggml_reshape_4d (ctx0 ,out ,head_dim ,n_head ,n_seq_tokens ,n_seqs ); 216} 217 218llama_model_bailingmoe3::graph::graph (const llama_model & model ,const llm_graph_params & params ) : 219llm_build_delta_net_base (params ),model (model ) { 220ggml_tensor * inpL = build_inp_embd (model .tok_embd ); 221cb (inpL ,"model.input_embed" ,-1 ); 222 223auto * inp = build_inp_mem_hybrid_k (); 224auto * inp_rs = inp -> get_recr (); 225auto * inp_attn = inp -> get_attn (); 226 227ggml_tensor * inp_pos = build_inp_pos (); 228ggml_tensor * inp_out_ids = build_inp_out_ids (); 229 230const int64_t n_head = hparams .n_head (); 231const int64_t head_dim = hparams .n_embd_head_kda ; 232const int64_t d_inner = n_head * head_dim ; 233const int64_t d_conv = hparams .ssm_d_conv ; 234const int64_t n_seqs = ubatch .n_seqs ; 235const int64_t n_seq_tokens = ubatch .n_seq_tokens ; 236const int64_t qk_head_dim = hparams .n_embd_head_k_mla (); 237const int64_t v_head_dim = hparams .n_embd_head_v_mla (); 238const int64_t qk_rope_head_dim = hparams .n_rot (); 239const int64_t qk_nope_head_dim = qk_head_dim - qk_rope_head_dim ; 240const int64_t kv_lora_rank = hparams .n_lora_kv ; 241const float kq_scale = 1.0f /sqrtf ((float )qk_head_dim ); 242 243GGML_ASSERT (n_seqs > 0 ); 244GGML_ASSERT (ubatch .equal_seqs ()); 245GGML_ASSERT (ubatch .n_tokens == n_seq_tokens * n_seqs ); 246 247for (int il = 0 ;il < n_layer ;++ il ) { 248res -> t_layer_inp [il ]= inpL ; 249 250const auto & layer = model .layers [il ]; 251ggml_tensor * inpSA = inpL ; 252ggml_tensor * cur = build_norm (inpL ,layer .attn_norm ,nullptr ,LLM_NORM_RMS ,il ); 253cb (cur ,"attn_norm" ,il ); 254 255if (hparams .is_recr (il )) { 256const auto * mctx_cur = inp_rs -> mctx ; 257const auto cache_head = mctx_cur -> get_head (); 258const auto mem_size = mctx_cur -> get_size (); 259ggml_tensor * conv_states_all = mctx_cur -> get_r_l (il ); 260ggml_tensor * conv_state_all = build_rs (inp_rs ,conv_states_all ,hparams .n_embd_r (),n_seqs ); 261 262ggml_tensor * q = bailingmoe3_causal_conv1d ( 263gf ,ctx0 ,conv_states_all ,conv_state_all ,0 ,cur ,layer .wq ,layer .ssm_q_conv , 264d_conv ,head_dim ,n_head ,n_seq_tokens ,n_seqs ,n_tokens ,cache_head ,mem_size ,cparams .n_rs_seq ); 265ggml_tensor * k = bailingmoe3_causal_conv1d ( 266gf ,ctx0 ,conv_states_all ,conv_state_all ,1 ,cur ,layer .wk ,layer .ssm_k_conv , 267d_conv ,head_dim ,n_head ,n_seq_tokens ,n_seqs ,n_tokens ,cache_head ,mem_size ,cparams .n_rs_seq ); 268ggml_tensor * v = bailingmoe3_causal_conv1d ( 269gf ,ctx0 ,conv_states_all ,conv_state_all ,2 ,cur ,layer .wv ,layer .ssm_v_conv , 270d_conv ,head_dim ,n_head ,n_seq_tokens ,n_seqs ,n_tokens ,cache_head ,mem_size ,cparams .n_rs_seq ); 271 272ggml_tensor * gate = ggml_mul_mat (ctx0 ,layer .ssm_f_a ,cur ); 273gate = ggml_add (ctx0 ,gate ,layer .ssm_dt_b ); 274gate = ggml_reshape_3d (ctx0 ,gate ,head_dim ,n_head ,n_tokens ); 275ggml_tensor * a = ggml_reshape_3d (ctx0 ,layer .ssm_a ,1 ,n_head ,1 ); 276gate = ggml_scale (ctx0 ,ggml_sigmoid (ctx0 ,ggml_mul (ctx0 ,gate ,a )),hparams .kda_gate_lower_bound ); 277gate = ggml_reshape_4d (ctx0 ,gate ,head_dim ,n_head ,n_seq_tokens ,n_seqs ); 278cb (gate ,"kda_gate" ,il ); 279 280ggml_tensor * beta = ggml_mul_mat (ctx0 ,layer .ssm_beta ,cur ); 281beta = ggml_sigmoid (ctx0 ,ggml_reshape_4d (ctx0 ,beta ,1 ,n_head ,n_seq_tokens ,n_seqs )); 282 283q = build_gdn_l2_norm (ctx0 ,q ,hparams .f_norm_rms_eps ); 284k = build_gdn_l2_norm (ctx0 ,k ,hparams .f_norm_rms_eps ); 285 286ggml_tensor * states_all = mctx_cur -> get_s_l (il ); 287ggml_tensor * state = build_rs (inp_rs ,states_all ,hparams .n_embd_s (),n_seqs ); 288state = ggml_reshape_4d (ctx0 ,state ,head_dim ,head_dim ,n_head ,n_seqs ); 289 290ggml_tensor * out = ggml_cont (ctx0 ,build_recurrent_attn ( 291inp_rs ,states_all ,q ,k ,v ,gate ,beta ,state ,il )); 292 293ggml_tensor * out_gate = ggml_mul_mat (ctx0 ,layer .ssm_g_a ,cur ); 294out_gate = ggml_reshape_3d (ctx0 ,out_gate ,head_dim ,n_head ,n_tokens ); 295out = ggml_reshape_3d (ctx0 ,out ,head_dim ,n_head ,n_tokens ); 296out = build_norm (out ,layer .ssm_o_norm ,nullptr ,LLM_NORM_RMS ,il ); 297out = ggml_mul (ctx0 ,out ,ggml_sigmoid (ctx0 ,out_gate )); 298cur = ggml_mul_mat (ctx0 ,layer .wo ,ggml_cont_2d (ctx0 ,out ,d_inner ,n_tokens )); 299cb (cur ,"kda_out" ,il ); 300 }else { 301ggml_tensor * attn_input = cur ; 302ggml_tensor * q_all ; 303if (layer .wq_a ) { 304q_all = ggml_mul_mat (ctx0 ,layer .wq_a ,cur ); 305cb (q_all ,"q_a" ,il ); 306q_all = build_norm (q_all ,layer .attn_q_a_norm ,nullptr ,LLM_NORM_RMS ,il ); 307cb (q_all ,"q_a_norm" ,il ); 308q_all = ggml_mul_mat (ctx0 ,layer .wq_b ,q_all ); 309cb (q_all ,"q_b" ,il ); 310 }else { 311q_all = ggml_mul_mat (ctx0 ,layer .wq ,cur ); 312 } 313ggml_tensor * q_nope = ggml_view_3d (ctx0 ,q_all ,qk_nope_head_dim ,n_head ,n_tokens , 314ggml_row_size (q_all -> type ,qk_head_dim ), 315ggml_row_size (q_all -> type ,qk_head_dim )* n_head ,0 ); 316ggml_tensor * q_pe = ggml_view_3d (ctx0 ,q_all ,qk_rope_head_dim ,n_head ,n_tokens , 317ggml_row_size (q_all -> type ,qk_head_dim ), 318ggml_row_size (q_all -> type ,qk_head_dim )* n_head , 319ggml_row_size (q_all -> type ,qk_nope_head_dim )); 320 321ggml_tensor * kv_all = ggml_mul_mat (ctx0 ,layer .wkv_a_mqa ,cur ); 322ggml_tensor * kv = ggml_view_2d (ctx0 ,kv_all ,kv_lora_rank ,n_tokens , 323ggml_row_size (kv_all -> type ,kv_lora_rank + qk_rope_head_dim ),0 ); 324ggml_tensor * k_pe = ggml_view_3d (ctx0 ,kv_all ,qk_rope_head_dim ,1 ,n_tokens , 325ggml_row_size (kv_all -> type ,kv_lora_rank + qk_rope_head_dim ), 326ggml_row_size (kv_all -> type ,kv_lora_rank + qk_rope_head_dim ), 327ggml_row_size (kv_all -> type ,kv_lora_rank )); 328 329q_pe = ggml_rope_ext (ctx0 ,q_pe ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 330ext_factor ,attn_factor ,beta_fast ,beta_slow ); 331k_pe = ggml_rope_ext (ctx0 ,k_pe ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 332ext_factor ,attn_factor ,beta_fast ,beta_slow ); 333kv = build_norm (kv ,layer .attn_kv_a_norm ,nullptr ,LLM_NORM_RMS ,il ); 334 335q_nope = ggml_permute (ctx0 ,q_nope ,0 ,2 ,1 ,3 ); 336q_nope = ggml_mul_mat (ctx0 ,layer .wk_b ,q_nope ); 337q_nope = ggml_permute (ctx0 ,q_nope ,0 ,2 ,1 ,3 ); 338 339ggml_tensor * q = ggml_concat (ctx0 ,q_nope ,q_pe ,0 ); 340kv = ggml_reshape_3d (ctx0 ,kv ,kv_lora_rank ,1 ,n_tokens ); 341ggml_tensor * k = ggml_concat (ctx0 ,kv ,k_pe ,0 ); 342 343cur = build_attn (inp_attn ,nullptr ,nullptr ,nullptr , 344q ,k ,kv ,nullptr ,nullptr ,layer .wv_b ,kq_scale ,il ); 345 346ggml_tensor * attn_gate = ggml_mul_mat (ctx0 ,layer .wqkv_gate ,attn_input ); 347attn_gate = ggml_sigmoid (ctx0 ,ggml_reshape_3d (ctx0 ,attn_gate ,1 ,n_head ,n_tokens )); 348cur = ggml_reshape_3d (ctx0 ,cur ,v_head_dim ,n_head ,n_tokens ); 349cur = ggml_mul (ctx0 ,cur ,attn_gate ); 350cur = ggml_mul_mat (ctx0 ,layer .wo ,ggml_cont_2d (ctx0 ,cur ,v_head_dim * n_head ,n_tokens )); 351cb (cur ,"mla_out" ,il ); 352 } 353 354if (il == n_layer - 1 && inp_out_ids && cparams .embeddings_nextn_masked ) { 355cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 356inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 357 } 358 359ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 360cur = build_norm (ffn_inp ,layer .ffn_norm ,nullptr ,LLM_NORM_RMS ,il ); 361 362if ((uint32_t )il < hparams .n_layer_dense_lead ) { 363cur = build_ffn (cur , 364layer .ffn_up ,nullptr ,nullptr , 365layer .ffn_gate ,nullptr ,nullptr , 366layer .ffn_down ,nullptr ,nullptr , 367nullptr ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 368 }else { 369ggml_tensor * moe = build_moe_ffn (cur , 370layer .ffn_gate_inp , 371layer .ffn_up_exps , 372layer .ffn_gate_exps , 373layer .ffn_down_exps , 374layer .ffn_exp_probs_b , 375n_expert ,n_expert_used , 376LLM_FFN_SILU , 377hparams .expert_weights_norm , 378hparams .expert_weights_scale , 379 (llama_expert_gating_func_type )hparams .expert_gating_func , 380il ); 381ggml_tensor * shared = build_ffn (cur , 382layer .ffn_up_shexp ,nullptr ,nullptr , 383layer .ffn_gate_shexp ,nullptr ,nullptr , 384layer .ffn_down_shexp ,nullptr ,nullptr , 385nullptr ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 386cur = ggml_add (ctx0 ,moe ,shared ); 387 } 388 389cur = ggml_add (ctx0 ,cur ,ffn_inp ); 390cur = build_cvec (cur ,il ); 391cb (cur ,"l_out" ,il ); 392inpL = cur ; 393 } 394 395ggml_tensor * cur = build_norm (inpL ,model .output_norm ,nullptr ,LLM_NORM_RMS ,-1 ); 396cb (cur ,"h_nextn" ,-1 ); 397res -> t_h_nextn = cur ; 398 399if (!cparams .embeddings_nextn_masked && inp_out_ids ) { 400cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 401 } 402 403cb (cur ,"result_norm" ,-1 ); 404res -> t_embd = cur ; 405 406cur = ggml_mul_mat (ctx0 ,model .output ,cur ); 407cb (cur ,"result_output" ,-1 ); 408res -> t_logits = cur ; 409ggml_build_forward_expand (gf ,cur ); 410} 411 412llama_model_bailingmoe3::graph_mtp::graph_mtp (const llama_model & model ,const llm_graph_params & params ) : 413llm_graph_context (params ) { 414GGML_ASSERT (hparams .n_layer_nextn == 1 && "BailingMoE3 MTP requires one NextN layer" ); 415 416const int il = hparams .n_layer ()+ cparams .nextn_layer_offset ; 417GGML_ASSERT (cparams .nextn_layer_offset >=0 && 418cparams .nextn_layer_offset < (int )hparams .n_layer_nextn && 419"nextn_layer_offset out of range" ); 420const auto & layer = model .layers [il ]; 421 422GGML_ASSERT (layer .nextn .eh_proj && "MTP block missing nextn.eh_proj" ); 423GGML_ASSERT (layer .nextn .enorm && "MTP block missing nextn.enorm" ); 424GGML_ASSERT (layer .nextn .hnorm && "MTP block missing nextn.hnorm" ); 425GGML_ASSERT (layer .nextn .shared_head_norm && "MTP block missing final norm" ); 426 427const int64_t n_head = hparams .n_head (); 428const int64_t qk_head_dim = hparams .n_embd_head_k_mla (); 429const int64_t v_head_dim = hparams .n_embd_head_v_mla (); 430const int64_t qk_rope_head_dim = hparams .n_rot (); 431const int64_t qk_nope_head_dim = qk_head_dim - qk_rope_head_dim ; 432const int64_t kv_lora_rank = hparams .n_lora_kv ; 433const float kq_scale = 1.0f /sqrtf ((float )qk_head_dim ); 434 435auto inp = std::make_unique < llm_graph_input_embd > (hparams .n_embd ); 436inp -> tokens = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_tokens ); 437ggml_set_input (inp -> tokens ); 438inp -> embd = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,hparams .n_embd ,n_tokens ); 439ggml_set_input (inp -> embd ); 440ggml_set_name (inp -> embd ,"mtp_h_input" ); 441 442ggml_tensor * tok_embd = ggml_get_rows (ctx0 ,model .tok_embd ,inp -> tokens ); 443ggml_tensor * h_norm = build_norm (inp -> embd ,layer .nextn .hnorm ,nullptr ,LLM_NORM_RMS ,il ); 444ggml_tensor * e_norm = build_norm (tok_embd ,layer .nextn .enorm ,nullptr ,LLM_NORM_RMS ,il ); 445ggml_tensor * cur = ggml_mul_mat (ctx0 ,layer .nextn .eh_proj ,ggml_concat (ctx0 ,e_norm ,h_norm ,0 )); 446cb (cur ,"mtp_eh_proj" ,il ); 447 448res -> add_input (std::move (inp )); 449 450ggml_tensor * inp_pos = build_inp_pos (); 451ggml_tensor * inp_out_ids = build_inp_out_ids (); 452auto * inp_attn = build_attn_inp_k (); 453 454ggml_tensor * inpSA = cur ; 455cur = build_norm (cur ,layer .attn_norm ,nullptr ,LLM_NORM_RMS ,il ); 456ggml_tensor * attn_input = cur ; 457 458ggml_tensor * q_all ; 459if (layer .wq_a ) { 460q_all = ggml_mul_mat (ctx0 ,layer .wq_a ,cur ); 461cb (q_all ,"q_a" ,il ); 462q_all = build_norm (q_all ,layer .attn_q_a_norm ,nullptr ,LLM_NORM_RMS ,il ); 463cb (q_all ,"q_a_norm" ,il ); 464q_all = ggml_mul_mat (ctx0 ,layer .wq_b ,q_all ); 465cb (q_all ,"q_b" ,il ); 466 }else { 467q_all = ggml_mul_mat (ctx0 ,layer .wq ,cur ); 468 } 469ggml_tensor * q_nope = ggml_view_3d (ctx0 ,q_all ,qk_nope_head_dim ,n_head ,n_tokens , 470ggml_row_size (q_all -> type ,qk_head_dim ), 471ggml_row_size (q_all -> type ,qk_head_dim )* n_head ,0 ); 472ggml_tensor * q_pe = ggml_view_3d (ctx0 ,q_all ,qk_rope_head_dim ,n_head ,n_tokens , 473ggml_row_size (q_all -> type ,qk_head_dim ), 474ggml_row_size (q_all -> type ,qk_head_dim )* n_head , 475ggml_row_size (q_all -> type ,qk_nope_head_dim )); 476 477ggml_tensor * kv_all = ggml_mul_mat (ctx0 ,layer .wkv_a_mqa ,cur ); 478ggml_tensor * kv = ggml_view_2d (ctx0 ,kv_all ,kv_lora_rank ,n_tokens , 479ggml_row_size (kv_all -> type ,kv_lora_rank + qk_rope_head_dim ),0 ); 480ggml_tensor * k_pe = ggml_view_3d (ctx0 ,kv_all ,qk_rope_head_dim ,1 ,n_tokens , 481ggml_row_size (kv_all -> type ,kv_lora_rank + qk_rope_head_dim ), 482ggml_row_size (kv_all -> type ,kv_lora_rank + qk_rope_head_dim ), 483ggml_row_size (kv_all -> type ,kv_lora_rank )); 484 485q_pe = ggml_rope_ext (ctx0 ,q_pe ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 486ext_factor ,attn_factor ,beta_fast ,beta_slow ); 487k_pe = ggml_rope_ext (ctx0 ,k_pe ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 488ext_factor ,attn_factor ,beta_fast ,beta_slow ); 489kv = build_norm (kv ,layer .attn_kv_a_norm ,nullptr ,LLM_NORM_RMS ,il ); 490 491q_nope = ggml_permute (ctx0 ,q_nope ,0 ,2 ,1 ,3 ); 492q_nope = ggml_mul_mat (ctx0 ,layer .wk_b ,q_nope ); 493q_nope = ggml_permute (ctx0 ,q_nope ,0 ,2 ,1 ,3 ); 494 495ggml_tensor * q = ggml_concat (ctx0 ,q_nope ,q_pe ,0 ); 496kv = ggml_reshape_3d (ctx0 ,kv ,kv_lora_rank ,1 ,n_tokens ); 497ggml_tensor * k = ggml_concat (ctx0 ,kv ,k_pe ,0 ); 498 499cur = build_attn (inp_attn ,nullptr ,nullptr ,nullptr , 500q ,k ,kv ,nullptr ,nullptr ,layer .wv_b ,kq_scale ,il ); 501 502ggml_tensor * attn_gate = ggml_mul_mat (ctx0 ,layer .wqkv_gate ,attn_input ); 503attn_gate = ggml_sigmoid (ctx0 ,ggml_reshape_3d (ctx0 ,attn_gate ,1 ,n_head ,n_tokens )); 504cur = ggml_reshape_3d (ctx0 ,cur ,v_head_dim ,n_head ,n_tokens ); 505cur = ggml_mul (ctx0 ,cur ,attn_gate ); 506cur = ggml_mul_mat (ctx0 ,layer .wo ,ggml_cont_2d (ctx0 ,cur ,v_head_dim * n_head ,n_tokens )); 507 508ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 509cur = build_norm (ffn_inp ,layer .ffn_norm ,nullptr ,LLM_NORM_RMS ,il ); 510 511ggml_tensor * moe = build_moe_ffn (cur , 512layer .ffn_gate_inp , 513layer .ffn_up_exps , 514layer .ffn_gate_exps , 515layer .ffn_down_exps , 516layer .ffn_exp_probs_b , 517n_expert ,n_expert_used , 518LLM_FFN_SILU , 519hparams .expert_weights_norm , 520hparams .expert_weights_scale , 521 (llama_expert_gating_func_type )hparams .expert_gating_func , 522il ); 523ggml_tensor * shared = build_ffn (cur , 524layer .ffn_up_shexp ,nullptr ,nullptr , 525layer .ffn_gate_shexp ,nullptr ,nullptr , 526layer .ffn_down_shexp ,nullptr ,nullptr , 527nullptr ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 528cur = ggml_add (ctx0 ,moe ,shared ); 529cur = ggml_add (ctx0 ,cur ,ffn_inp ); 530cur = build_norm (cur ,layer .nextn .shared_head_norm ,nullptr ,LLM_NORM_RMS ,-1 ); 531 532cb (cur ,"h_nextn" ,-1 ); 533res -> t_h_nextn = cur ; 534 535cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 536cur = ggml_mul_mat (ctx0 ,model .output ,cur ); 537cb (cur ,"result_output" ,-1 ); 538res -> t_logits = cur ; 539ggml_build_forward_expand (gf ,cur ); 540}