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