char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
c61b98b87
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_cohere2moe::load_arch_hparams (llama_model_loader & ml ) { 4const bool found_norm = ml .get_key (LLM_KV_ATTENTION_LAYERNORM_EPS ,hparams .f_norm_eps , false); 5const bool found_norm_rms = ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps , false); 6if (!found_norm && !found_norm_rms ) { 7throw std::runtime_error ("missing Cohere2 MoE norm epsilon" ); 8 } 9if (!found_norm_rms ) { 10hparams .f_norm_rms_eps = 0.0f ; 11 } 12 13ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa ); 14ml .get_key (LLM_KV_LOGIT_SCALE ,hparams .f_logit_scale ); 15ml .get_key (LLM_KV_LEADING_DENSE_BLOCK_COUNT ,hparams .n_layer_dense_lead ); 16ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all ); 17ml .get_key (LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH ,hparams .n_ff_shexp , false); 18ml .get_key (LLM_KV_EXPERT_SHARED_COUNT ,hparams .n_expert_shared , false); 19ml .get_key (LLM_KV_EXPERT_WEIGHTS_NORM ,hparams .expert_weights_norm , false); 20ml .get_key (LLM_KV_EXPERT_WEIGHTS_SCALE ,hparams .expert_weights_scale , false); 21ml .get_key (LLM_KV_EXPERT_GATING_FUNC ,hparams .expert_gating_func , false); 22 23if (hparams .expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE ) { 24hparams .expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID ; 25 } 26 27hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 28uint32_t swa_period = 4 ; 29if (ml .get_key_or_arr (LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN ,swa_period , false)) { 30hparams .set_swa_pattern (swa_period , true); 31 }else { 32ml .get_key_or_arr (LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN ,hparams .is_swa_impl ,hparams .n_layer ()); 33 } 34 35hparams .rope_freq_base_train_swa = hparams .rope_freq_base_train ; 36hparams .rope_freq_scale_train_swa = hparams .rope_freq_scale_train ; 37ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 38 39switch (hparams .n_layer ()) { 40case 49 :type = LLM_TYPE_30B_A3B ;break ; 41default :type = LLM_TYPE_UNKNOWN ; 42 } 43} 44 45void llama_model_cohere2moe::load_arch_tensors (llama_model_loader & ml ) { 46LLAMA_LOAD_LOCALS ; 47 48const bool mtp_only = (hparams .n_layer_nextn > 0 )&& (ml .get_weight ("blk.0.attn_norm.weight" )== nullptr ); 49// Trunk-only: the GGUF declares MTP layers in metadata but the actual MTP 50// tensors live in a separate file. Mark MTP tensors NOT_REQUIRED so the 51// trunk loads cleanly. 52const std::string mtp_probe = "blk." + std::to_string (n_layer )+ ".nextn.eh_proj.weight" ; 53const bool trunk_only = (hparams .n_layer_nextn > 0 )&& (ml .get_weight (mtp_probe .c_str ())== nullptr ); 54const int trunk_flags = mtp_only ?TENSOR_NOT_REQUIRED :0 ; 55int mtp_flags = trunk_only ?TENSOR_NOT_REQUIRED :0 ; 56 57if (!ml .load_mtp ) { 58mtp_flags |=TENSOR_SKIP ; 59 } 60 61tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 62 63// output 64output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 65output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 66 67// if output is NULL, init from the input tok embed 68if (output == NULL ) { 69output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 70 } 71 72if (n_expert == 0 ) { 73throw std::runtime_error ("n_expert must be > 0 for Cohere2Moe" ); 74 } 75if (n_expert_used == 0 ) { 76throw std::runtime_error ("n_expert_used must be > 0 for Cohere2Moe" ); 77 } 78 79auto load_block_trunk = [& ](int i ,int flags ) { 80auto & layer = layers [i ]; 81 82layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },flags ); 83 84create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_gqa ,n_embd_gqa ,flags ); 85layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },flags ); 86 87if (static_cast < uint32_t > (i )< hparams .n_layer_dense_lead ) { 88layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },flags ); 89layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },flags ); 90layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },flags ); 91 }else { 92const int64_t n_ff_exp = hparams .n_ff_exp () ?hparams .n_ff_exp () :n_ff ; 93 94layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },flags ); 95layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },flags ); 96create_tensor_gate_up_exps (layer ,i ,n_embd ,n_ff_exp ,n_expert ,flags ); 97 98if (hparams .n_expert_shared > 0 ) { 99const int64_t n_ff_shexp = hparams .n_ff_shexp ?hparams .n_ff_shexp :n_ff_exp * hparams .n_expert_shared ; 100layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },flags ); 101layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {n_ff_shexp ,n_embd },flags ); 102layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },flags ); 103 } 104 } 105 }; 106 107auto load_block_mtp = [& ](int i ,int flags ) { 108auto & layer = layers [i ]; 109 110// MTP block looks like a full-attention Cohere2 MoE decoder block. 111layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },flags ); 112 113create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_gqa ,n_embd_gqa ,flags ); 114layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },flags ); 115 116const int64_t n_ff_exp = hparams .n_ff_exp () ?hparams .n_ff_exp () :n_ff ; 117 118// Routed experts 119layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },flags ); 120layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },flags ); 121create_tensor_gate_up_exps (layer ,i ,n_embd ,n_ff_exp ,n_expert ,flags ); 122 123if (hparams .n_expert_shared > 0 ) { 124const int64_t n_ff_shexp = hparams .n_ff_shexp ?hparams .n_ff_shexp :n_ff_exp * hparams .n_expert_shared ; 125 126// Shared experts 127layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },flags ); 128layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {n_ff_shexp ,n_embd },flags ); 129layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },flags ); 130 } 131 132// NextN-specific tensors that define the MTP block. 133layer .nextn .eh_proj = create_tensor (tn (LLM_TENSOR_NEXTN_EH_PROJ ,"weight" ,i ), {2 * n_embd ,n_embd },flags ); 134layer .nextn .enorm = create_tensor (tn (LLM_TENSOR_NEXTN_ENORM ,"weight" ,i ), {n_embd },flags ); 135layer .nextn .hnorm = create_tensor (tn (LLM_TENSOR_NEXTN_HNORM ,"weight" ,i ), {n_embd },flags ); 136layer .nextn .embed_tokens = create_tensor (tn (LLM_TENSOR_NEXTN_EMBED_TOKENS ,"weight" ,i ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 137layer .nextn .shared_head_head = create_tensor (tn (LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD ,"weight" ,i ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 138layer .nextn .shared_head_norm = create_tensor (tn (LLM_TENSOR_NEXTN_SHARED_HEAD_NORM ,"weight" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 139 }; 140 141for (int i = 0 ;i < n_layer ;++ i ) { 142load_block_trunk (i ,trunk_flags ); 143 } 144// MTP/NextN layers are loaded as extra decoder blocks. 145for (int i = n_layer ;i < n_layer_all ;++ i ) { 146load_block_mtp (i ,mtp_flags ); 147 } 148} 149 150std::unique_ptr < llm_graph_context > llama_model_cohere2moe::build_arch_graph (const llm_graph_params & params )const { 151if (params .gtype == LLM_GRAPH_TYPE_DECODER_MTP ) { 152return std::make_unique < graph_mtp > (* this ,params ); 153 } 154return std::make_unique < graph > (* this ,params ); 155} 156 157llama_model_cohere2moe::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 158const int64_t n_embd_head = hparams .n_embd_head_v (); 159 160GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 161GGML_ASSERT (n_embd_head == n_rot ); 162 163const llm_norm_type cohere2moe_norm_type = hparams .f_norm_rms_eps == 0.0f ?LLM_NORM :LLM_NORM_RMS ; 164const float f_logit_scale = hparams .f_logit_scale ; 165ggml_tensor * cur ; 166ggml_tensor * inpL = build_inp_embd (model .tok_embd ); 167ggml_tensor * inp_pos = build_inp_pos (); 168 169auto * inp_attn = build_attn_inp_kv_iswa (); 170ggml_tensor * inp_out_ids = build_inp_out_ids (); 171 172// MTP/NextN layers are loaded as extra decoder blocks but not executed in the main pass. 173for (int il = 0 ;il < n_layer ;++ il ) { 174const bool is_swa = hparams .is_swa (il ); 175// Dense-prefix full-attention layers use RoPE; later layers follow the SWA pattern. 176const bool force_rope = static_cast < uint32_t > (il )< hparams .n_layer_dense_lead ; 177 178cur = build_norm (inpL ,model .layers [il ].attn_norm ,nullptr ,cohere2moe_norm_type ,il ); 179cb (cur ,"attn_norm" ,il ); 180 181ggml_tensor * ffn_inp = cur ; 182 183 { 184const auto & layer = model .layers [il ]; 185 186auto [Qcur ,Kcur ,Vcur ]= build_qkv (layer ,cur , 187n_embd_head ,n_head ,n_head_kv ,il ); 188 189if (is_swa || force_rope ) { 190ggml_tensor * rope_factors = model .get_rope_factors (cparams ,il ); 191 192Qcur = ggml_rope_ext ( 193ctx0 ,Qcur ,inp_pos ,rope_factors , 194n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 195ext_factor ,attn_factor ,beta_fast ,beta_slow ); 196 197Kcur = ggml_rope_ext ( 198ctx0 ,Kcur ,inp_pos ,rope_factors , 199n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 200ext_factor ,attn_factor ,beta_fast ,beta_slow ); 201 } 202 203cb (Qcur ,"Qcur" ,il ); 204cb (Kcur ,"Kcur" ,il ); 205cb (Vcur ,"Vcur" ,il ); 206 207cur = build_attn (inp_attn , 208layer .wo ,layer .wo_b ,layer .wo_s , 209Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr , 2101.0f /sqrtf (float (n_embd_head )),il ); 211 } 212 213if (il == n_layer - 1 && inp_out_ids && cparams .embeddings_nextn_masked ) { 214cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 215inpL = ggml_get_rows (ctx0 ,inpL ,inp_out_ids ); 216ffn_inp = ggml_get_rows (ctx0 ,ffn_inp ,inp_out_ids ); 217 } 218 219ggml_tensor * attn_out = cur ; 220 221const auto & layer = model .layers [il ]; 222 223if (layer .ffn_gate_inp == nullptr ) { 224cur = build_ffn (ffn_inp , 225layer .ffn_up ,nullptr ,layer .ffn_up_s , 226layer .ffn_gate ,nullptr ,layer .ffn_gate_s , 227layer .ffn_down ,nullptr ,layer .ffn_down_s , 228nullptr ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 229cb (cur ,"ffn_out" ,il ); 230 }else { 231cur = build_moe_ffn (ffn_inp , 232layer .ffn_gate_inp , 233layer .ffn_up_exps , 234layer .ffn_gate_exps , 235layer .ffn_down_exps , 236nullptr , 237n_expert ,n_expert_used , 238LLM_FFN_SILU ,hparams .expert_weights_norm , 239hparams .expert_weights_scale , 240 (llama_expert_gating_func_type )hparams .expert_gating_func , 241il , 242nullptr ,layer .ffn_gate_up_exps , 243layer .ffn_up_exps_s , 244layer .ffn_gate_exps_s , 245layer .ffn_down_exps_s ); 246cb (cur ,"ffn_moe_out" ,il ); 247 248if (layer .ffn_up_shexp ) { 249ggml_tensor * ffn_shexp = build_ffn (ffn_inp , 250layer .ffn_up_shexp ,nullptr ,layer .ffn_up_shexp_s , 251layer .ffn_gate_shexp ,nullptr ,layer .ffn_gate_shexp_s , 252layer .ffn_down_shexp ,nullptr ,layer .ffn_down_shexp_s , 253nullptr ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 254cb (ffn_shexp ,"ffn_shexp" ,il ); 255 256cur = ggml_add (ctx0 ,cur ,ffn_shexp ); 257cur = ggml_scale (ctx0 ,cur ,0.5f ); 258cb (cur ,"ffn_out" ,il ); 259 } 260 } 261 262cur = ggml_add (ctx0 ,cur ,inpL ); 263cur = ggml_add (ctx0 ,cur ,attn_out ); 264 265cur = build_cvec (cur ,il ); 266cb (cur ,"l_out" ,il ); 267 268inpL = cur ; 269 } 270 271cur = inpL ; 272cur = build_norm (cur ,model .output_norm ,nullptr ,cohere2moe_norm_type ,-1 ); 273 274cb (cur ,"h_nextn" ,-1 ); 275res -> t_h_nextn = cur ; 276 277if (!cparams .embeddings_nextn_masked && inp_out_ids ) { 278cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 279 } 280 281cb (cur ,"result_norm" ,-1 ); 282res -> t_embd = cur ; 283 284cur = build_lora_mm (model .output ,cur ); 285 286if (f_logit_scale ) { 287cur = ggml_scale (ctx0 ,cur ,f_logit_scale ); 288 } 289 290cb (cur ,"result_output" ,-1 ); 291res -> t_logits = cur ; 292 293ggml_build_forward_expand (gf ,cur ); 294} 295 296llama_model_cohere2moe::graph_mtp::graph_mtp (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 297GGML_ASSERT (hparams .n_layer_nextn > 0 && "COHERE2MOE MTP requires n_layer_nextn > 0" ); 298GGML_ASSERT (hparams .n_layer_nextn == 1 && "COHERE2MOE MTP currently only supports a single MTP block" ); 299 300const int64_t n_embd_head = hparams .n_embd_head_v (); 301GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 302GGML_ASSERT (n_embd_head == n_rot ); 303 304const int il = hparams .n_layer (); 305const auto & layer = model .layers [il ]; 306GGML_ASSERT (layer .nextn .eh_proj && "MTP block missing nextn.eh_proj" ); 307GGML_ASSERT (layer .nextn .enorm && "MTP block missing nextn.enorm" ); 308GGML_ASSERT (layer .nextn .hnorm && "MTP block missing nextn.hnorm" ); 309GGML_ASSERT (layer .ffn_gate_inp && "MTP block missing ffn_gate_inp" ); 310 311const llm_norm_type cohere2moe_norm_type = hparams .f_norm_rms_eps == 0.0f ?LLM_NORM :LLM_NORM_RMS ; 312 313// TODO: extract in a common llm_graph_context::build_inp_embd_h() 314auto inp = std::make_unique < llm_graph_input_embd_h > (hparams .n_embd ); 315 316inp -> tokens = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_tokens ); 317ggml_set_input (inp -> tokens ); 318 319inp -> embd = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,hparams .n_embd_inp (),n_tokens ); 320ggml_set_input (inp -> embd ); 321 322// TODO: make static using `ggml_build_forward_select()` 323// see llm_graph_context::build_inp_embd() for reference 324ggml_tensor * tok_embd ; 325if (ubatch .token ) { 326ggml_tensor * tok_embd_w = layer .nextn .embed_tokens ?layer .nextn .embed_tokens :model .tok_embd ; 327tok_embd = ggml_get_rows (ctx0 ,tok_embd_w ,inp -> tokens ); 328 }else { 329tok_embd = inp -> embd ; 330 } 331cb (tok_embd ,"mtp_tok_embd" ,il ); 332 333inp -> h = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,hparams .n_embd ,n_tokens ); 334ggml_set_input (inp -> h ); 335ggml_set_name (inp -> h ,"mtp_h_input" ); 336 337ggml_tensor * h_embd = inp -> h ; 338 339res -> add_input (std::move (inp )); 340 341ggml_tensor * inp_pos = build_inp_pos (); 342ggml_tensor * inp_out_ids = build_inp_out_ids (); 343auto * inp_attn = build_attn_inp_kv_iswa (); 344 345ggml_tensor * h_norm = build_norm (h_embd ,layer .nextn .hnorm ,nullptr ,cohere2moe_norm_type ,il ); 346cb (h_norm ,"mtp_hnorm" ,il ); 347 348ggml_tensor * e_norm = build_norm (tok_embd ,layer .nextn .enorm ,nullptr ,cohere2moe_norm_type ,il ); 349cb (e_norm ,"mtp_enorm" ,il ); 350 351ggml_tensor * concat = ggml_concat (ctx0 ,e_norm ,h_norm ,/*dim=*/ 0 ); 352cb (concat ,"mtp_concat" ,il ); 353 354ggml_tensor * cur = build_lora_mm (layer .nextn .eh_proj ,concat ,layer .nextn .eh_proj_s ); 355cb (cur ,"mtp_eh_proj" ,il ); 356 357ggml_tensor * inpL = cur ; 358 359cur = build_norm (cur ,layer .attn_norm ,nullptr ,cohere2moe_norm_type ,il ); 360cb (cur ,"mtp_attn_norm" ,il ); 361ggml_tensor * ffn_inp = cur ; 362 363auto [Qcur ,Kcur ,Vcur ]= build_qkv (layer ,cur ,n_embd_head ,n_head ,n_head_kv ,il ); 364ggml_tensor * rope_factors = model .get_rope_factors (cparams ,il ); 365Qcur = ggml_rope_ext ( 366ctx0 ,Qcur ,inp_pos ,rope_factors , 367n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 368ext_factor ,attn_factor ,beta_fast ,beta_slow ); 369Kcur = ggml_rope_ext ( 370ctx0 ,Kcur ,inp_pos ,rope_factors , 371n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 372ext_factor ,attn_factor ,beta_fast ,beta_slow ); 373 374cb (Qcur ,"mtp_Qcur" ,il ); 375cb (Kcur ,"mtp_Kcur" ,il ); 376cb (Vcur ,"mtp_Vcur" ,il ); 377 378cur = build_attn (inp_attn , 379layer .wo ,layer .wo_b ,layer .wo_s , 380Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr , 3811.0f /sqrtf (float (n_embd_head )),il ); 382cb (cur ,"mtp_attn_out" ,il ); 383 384ggml_tensor * attn_out = cur ; 385 386cur = build_moe_ffn (ffn_inp , 387layer .ffn_gate_inp , 388layer .ffn_up_exps , 389layer .ffn_gate_exps , 390layer .ffn_down_exps , 391nullptr , 392n_expert ,n_expert_used , 393LLM_FFN_SILU ,hparams .expert_weights_norm , 394hparams .expert_weights_scale , 395 (llama_expert_gating_func_type )hparams .expert_gating_func , 396il , 397nullptr ,layer .ffn_gate_up_exps , 398layer .ffn_up_exps_s , 399layer .ffn_gate_exps_s , 400layer .ffn_down_exps_s ); 401cb (cur ,"mtp_ffn_moe_out" ,il ); 402 403if (layer .ffn_up_shexp ) { 404ggml_tensor * ffn_shexp = build_ffn (ffn_inp , 405layer .ffn_up_shexp ,nullptr ,layer .ffn_up_shexp_s , 406layer .ffn_gate_shexp ,nullptr ,layer .ffn_gate_shexp_s , 407layer .ffn_down_shexp ,nullptr ,layer .ffn_down_shexp_s , 408nullptr ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 409cb (ffn_shexp ,"mtp_ffn_shexp" ,il ); 410 411cur = ggml_add (ctx0 ,cur ,ffn_shexp ); 412cur = ggml_scale (ctx0 ,cur ,0.5f ); 413cb (cur ,"mtp_ffn_out" ,il ); 414 } 415 416cur = ggml_add (ctx0 ,cur ,inpL ); 417cur = ggml_add (ctx0 ,cur ,attn_out ); 418cb (cur ,"mtp_post_ffn" ,il ); 419 420ggml_tensor * head_norm_w = layer .nextn .shared_head_norm 421 ?layer .nextn .shared_head_norm 422 :model .output_norm ; 423GGML_ASSERT (head_norm_w && "COHERE2MOE MTP: missing both nextn.shared_head_norm and output_norm" ); 424cur = build_norm (cur ,head_norm_w ,nullptr ,cohere2moe_norm_type ,-1 ); 425 426cb (cur ,"h_nextn" ,-1 ); 427res -> t_h_nextn = cur ; 428 429cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 430cb (cur ,"mtp_shared_head_norm" ,-1 ); 431 432ggml_tensor * head_w = layer .nextn .shared_head_head ?layer .nextn .shared_head_head :model .output ; 433GGML_ASSERT (head_w && "COHERE2MOE MTP: missing LM head (nextn.shared_head_head or model.output)" ); 434cur = build_lora_mm (head_w ,cur ,layer .nextn .shared_head_head ?layer .nextn .shared_head_head_s :nullptr ); 435 436if (hparams .f_logit_scale ) { 437cur = ggml_scale (ctx0 ,cur ,hparams .f_logit_scale ); 438 } 439 440cb (cur ,"result_output" ,-1 ); 441res -> t_logits = cur ; 442 443ggml_build_forward_expand (gf ,cur ); 444}