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_glm4_moe::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all ); 5ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 6ml .get_key_or_arr (LLM_KV_ROPE_DIMENSION_SECTIONS ,hparams .rope_sections ,4 , false); 7 8// MoE parameters 9ml .get_key (LLM_KV_EXPERT_SHARED_COUNT ,hparams .n_expert_shared ); 10ml .get_key (LLM_KV_LEADING_DENSE_BLOCK_COUNT ,hparams .n_layer_dense_lead , false); 11ml .get_key (LLM_KV_EXPERT_WEIGHTS_SCALE ,hparams .expert_weights_scale , false); 12ml .get_key (LLM_KV_EXPERT_WEIGHTS_NORM ,hparams .expert_weights_norm , false); 13 14// Expert gating function (GLM-4.5 uses sigmoid) 15ml .get_key (LLM_KV_EXPERT_GATING_FUNC ,hparams .expert_gating_func , false); 16if (hparams .expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE ) { 17hparams .expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID ; 18 } 19 20switch (hparams .n_layer ()) { 21case 46 :type = LLM_TYPE_106B_A12B ;break ;// GLM-4.5-Air 22case 48 :type = LLM_TYPE_102B_A12B ;break ;// Solar Open 23case 92 :type = LLM_TYPE_355B_A32B ;break ;// GLM-4.5 24default :type = LLM_TYPE_UNKNOWN ; 25 } 26} 27 28void llama_model_glm4_moe::load_arch_tensors (llama_model_loader & ml ) { 29LLAMA_LOAD_LOCALS ; 30const int64_t n_expert_shared = hparams .n_expert_shared ; 31 32const bool mtp_only = (hparams .n_layer_nextn > 0 )&& (ml .get_weight ("blk.0.attn_norm.weight" )== nullptr ); 33const std::string mtp_probe = "blk." + std::to_string (n_layer )+ ".nextn.eh_proj.weight" ; 34const bool trunk_only = (hparams .n_layer_nextn > 0 )&& (ml .get_weight (mtp_probe .c_str ())== nullptr ); 35const int trunk_flags = mtp_only ?TENSOR_NOT_REQUIRED :0 ; 36int mtp_flags = trunk_only ?TENSOR_NOT_REQUIRED :0 ; 37 38if (!ml .load_mtp ) { 39mtp_flags |=TENSOR_SKIP ; 40 } 41 42GGML_ASSERT (hparams .n_expert > 0 && "n_expert must be > 0 for GLM4_MOE MoE layers" ); 43GGML_ASSERT (hparams .n_expert_used ()> 0 && "n_expert_used must be > 0 for GLM4_MOE MoE layers" ); 44 45tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 46 47// output 48output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 49output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 50// if output is NULL, init from the input tok embed 51if (output == NULL ) { 52output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 53 } 54 55for (int i = 0 ;i < n_layer_all ;++ i ) { 56auto & layer = layers [i ]; 57const int flags = i < n_layer ?trunk_flags :mtp_flags ; 58 59layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },flags ); 60 61// GLM-style attention with bias terms 62create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head ,n_embd_k_gqa ,n_embd_v_gqa ,flags ); 63 64layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_k * n_head ,n_embd },flags ); 65 66// K/Q norm tensors (optional for GLM-4.5 355B variant) 67layer .attn_q_norm = create_tensor ( 68tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },TENSOR_NOT_REQUIRED |flags ); 69layer .attn_k_norm = create_tensor ( 70tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },TENSOR_NOT_REQUIRED |flags ); 71 72layer .attn_post_norm = create_tensor (tn (LLM_TENSOR_ATTN_POST_NORM ,"weight" ,i ), {n_embd },flags ); 73 74// Check if this layer uses MoE or dense FFN based on n_layer_dense_lead 75// GLM 4.5 uses hybrid architecture: layer 0 is dense, layers 1+ are MoE 76const bool use_moe = (static_cast < uint32_t > (i ) >=hparams .n_layer_dense_lead ); 77 78if (use_moe ) { 79// MoE layers 80layer .ffn_gate_inp = 81create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },flags ); 82layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,i ), {n_expert },flags ); 83 84// MoE branch 85const int64_t n_ff_exp = hparams .n_ff_exp () ?hparams .n_ff_exp () :n_ff /n_expert_used ; 86 87layer .ffn_gate_exps = create_tensor ( 88tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },flags ); 89layer .ffn_down_exps = create_tensor ( 90tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },flags ); 91layer .ffn_up_exps = create_tensor ( 92tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },flags ); 93 94// Shared expert 95if (n_expert_shared > 0 ) { 96const int64_t n_ff_shexp = n_ff_exp * n_expert_shared ; 97layer .ffn_gate_shexp = create_tensor ( 98tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },flags ); 99layer .ffn_down_shexp = create_tensor ( 100tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {n_ff_shexp ,n_embd },flags ); 101layer .ffn_up_shexp = create_tensor ( 102tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,n_ff_shexp },flags ); 103 } 104 }else { 105// Dense layers (first k layers) - GLM uses separate gate/up projections 106layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },flags ); 107layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },flags ); 108layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },flags ); 109 } 110 111// NextN/MTP tensors 112if (i >=n_layer ) { 113layer .nextn .eh_proj = create_tensor (tn (LLM_TENSOR_NEXTN_EH_PROJ ,"weight" ,i ), {2 * n_embd ,n_embd },flags ); 114layer .nextn .enorm = create_tensor (tn (LLM_TENSOR_NEXTN_ENORM ,"weight" ,i ), {n_embd },flags ); 115layer .nextn .hnorm = create_tensor (tn (LLM_TENSOR_NEXTN_HNORM ,"weight" ,i ), {n_embd },flags ); 116 117// Optional tensors 118layer .nextn .embed_tokens = create_tensor (tn (LLM_TENSOR_NEXTN_EMBED_TOKENS ,"weight" ,i ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED |flags ); 119layer .nextn .shared_head_head = create_tensor (tn (LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD ,"weight" ,i ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED |flags ); 120layer .nextn .shared_head_norm = create_tensor (tn (LLM_TENSOR_NEXTN_SHARED_HEAD_NORM ,"weight" ,i ), {n_embd },TENSOR_NOT_REQUIRED |flags ); 121 } 122 } 123} 124 125std::unique_ptr < llm_graph_context > llama_model_glm4_moe::build_arch_graph (const llm_graph_params & params )const { 126if (params .gtype == LLM_GRAPH_TYPE_DECODER_MTP ) { 127return std::make_unique < graph_mtp > (* this ,params ); 128 } 129return std::make_unique < graph > (* this ,params ); 130} 131 132llama_model_glm4_moe::graph_mtp::graph_mtp (const llama_model & model ,const llm_graph_params & params ) 133 :llm_graph_context (params ) { 134GGML_ASSERT (hparams .n_layer_nextn > 0 && "GLM4_MOE MTP requires n_layer_nextn > 0" ); 135GGML_ASSERT (hparams .n_layer_nextn == 1 && "GLM4_MOE MTP currently only supports a single MTP block" ); 136 137const int64_t n_embd_head = hparams .n_embd_head_v (); 138GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 139 140const int il = hparams .n_layer ()+ cparams .nextn_layer_offset ; 141GGML_ASSERT (cparams .nextn_layer_offset >=0 && 142cparams .nextn_layer_offset < (int )hparams .n_layer_nextn && 143"nextn_layer_offset out of range [0, n_layer_nextn)" ); 144 145const auto & layer = model .layers [il ]; 146 147GGML_ASSERT (layer .nextn .eh_proj && "MTP block missing nextn.eh_proj" ); 148GGML_ASSERT (layer .nextn .enorm && "MTP block missing nextn.enorm" ); 149GGML_ASSERT (layer .nextn .hnorm && "MTP block missing nextn.hnorm" ); 150GGML_ASSERT (layer .ffn_gate_inp && "MTP block missing ffn_gate_inp" ); 151 152auto inp = std::make_unique < llm_graph_input_embd_h > (hparams .n_embd ); 153 154inp -> tokens = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_tokens ); 155ggml_set_input (inp -> tokens ); 156 157inp -> embd = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,hparams .n_embd_inp (),n_tokens ); 158ggml_set_input (inp -> embd ); 159 160ggml_tensor * tok_embd ; 161if (ubatch .token ) { 162ggml_tensor * tok_embd_w = layer .nextn .embed_tokens ?layer .nextn .embed_tokens :model .tok_embd ; 163tok_embd = ggml_get_rows (ctx0 ,tok_embd_w ,inp -> tokens ); 164 }else { 165tok_embd = inp -> embd ; 166 } 167cb (tok_embd ,"mtp_tok_embd" ,il ); 168 169inp -> h = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,hparams .n_embd ,n_tokens ); 170ggml_set_input (inp -> h ); 171ggml_set_name (inp -> h ,"mtp_h_input" ); 172 173ggml_tensor * h_embd = inp -> h ; 174 175res -> add_input (std::move (inp )); 176 177ggml_tensor * inp_pos = build_inp_pos (); 178ggml_tensor * inp_out_ids = build_inp_out_ids (); 179 180auto * inp_attn = build_attn_inp_kv (); 181 182ggml_tensor * h_norm = build_norm (h_embd ,layer .nextn .hnorm ,nullptr ,LLM_NORM_RMS ,il ); 183cb (h_norm ,"mtp_hnorm" ,il ); 184 185ggml_tensor * e_norm = build_norm (tok_embd ,layer .nextn .enorm ,nullptr ,LLM_NORM_RMS ,il ); 186cb (e_norm ,"mtp_enorm" ,il ); 187 188ggml_tensor * concat = ggml_concat (ctx0 ,e_norm ,h_norm ,0 ); 189cb (concat ,"mtp_concat" ,il ); 190 191ggml_tensor * cur = build_lora_mm (layer .nextn .eh_proj ,concat ,layer .nextn .eh_proj_s ); 192cb (cur ,"mtp_eh_proj" ,il ); 193 194ggml_tensor * inpSA = cur ; 195 196cur = build_norm (cur ,layer .attn_norm ,nullptr ,LLM_NORM_RMS ,il ); 197cb (cur ,"mtp_attn_norm" ,il ); 198 199auto [Qcur ,Kcur ,Vcur ]= build_qkv (layer ,cur , 200n_embd_head ,n_head ,n_head_kv ,il ); 201 202if (layer .attn_q_norm ) { 203Qcur = build_norm (Qcur ,layer .attn_q_norm ,nullptr ,LLM_NORM_RMS ,il ); 204cb (Qcur ,"mtp_Qcur_normed" ,il ); 205 } 206if (layer .attn_k_norm ) { 207Kcur = build_norm (Kcur ,layer .attn_k_norm ,nullptr ,LLM_NORM_RMS ,il ); 208cb (Kcur ,"mtp_Kcur_normed" ,il ); 209 } 210 211Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,nullptr ,n_rot , 212rope_type ,n_ctx_orig ,freq_base ,freq_scale , 213ext_factor ,attn_factor ,beta_fast ,beta_slow ); 214 215Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,nullptr ,n_rot , 216rope_type ,n_ctx_orig ,freq_base ,freq_scale , 217ext_factor ,attn_factor ,beta_fast ,beta_slow ); 218 219cb (Qcur ,"mtp_Qcur" ,il ); 220cb (Kcur ,"mtp_Kcur" ,il ); 221cb (Vcur ,"mtp_Vcur" ,il ); 222 223cur = build_attn (inp_attn , 224layer .wo ,nullptr ,layer .wo_s , 225Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr , 2261.0f /sqrtf (float (n_embd_head )),il ); 227cb (cur ,"mtp_attn_out" ,il ); 228 229ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 230cb (ffn_inp ,"mtp_ffn_inp" ,il ); 231 232cur = build_norm (ffn_inp ,layer .attn_post_norm ,nullptr ,LLM_NORM_RMS ,il ); 233cb (cur ,"mtp_post_attn_norm" ,il ); 234 235ggml_tensor * routed_out = build_moe_ffn (cur , 236layer .ffn_gate_inp , 237layer .ffn_up_exps , 238layer .ffn_gate_exps , 239layer .ffn_down_exps , 240layer .ffn_exp_probs_b , 241n_expert ,n_expert_used , 242LLM_FFN_SILU ,hparams .expert_weights_norm , 243hparams .expert_weights_scale , 244 (llama_expert_gating_func_type )hparams .expert_gating_func , 245il ); 246cb (routed_out ,"mtp_ffn_moe_out" ,il ); 247 248ggml_tensor * shared_out = build_ffn (cur , 249layer .ffn_up_shexp ,nullptr ,nullptr , 250layer .ffn_gate_shexp ,nullptr ,nullptr , 251layer .ffn_down_shexp ,nullptr ,nullptr , 252nullptr , 253LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 254cb (shared_out ,"mtp_ffn_shexp_out" ,il ); 255 256cur = ggml_add (ctx0 ,routed_out ,shared_out ); 257cb (cur ,"mtp_ffn_out" ,il ); 258 259cur = ggml_add (ctx0 ,cur ,ffn_inp ); 260cb (cur ,"mtp_post_ffn" ,il ); 261 262ggml_tensor * head_norm_w = layer .nextn .shared_head_norm 263 ?layer .nextn .shared_head_norm 264 :model .output_norm ; 265GGML_ASSERT (head_norm_w && "GLM4_MOE MTP: missing both nextn.shared_head_norm and output_norm" ); 266 267cur = build_norm (cur ,head_norm_w ,nullptr ,LLM_NORM_RMS ,-1 ); 268cb (cur ,"h_nextn" ,-1 ); 269res -> t_h_nextn = cur ; 270 271if (inp_out_ids ) { 272cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 273 } 274cb (cur ,"mtp_shared_head_norm" ,-1 ); 275 276ggml_tensor * head_w = layer .nextn .shared_head_head 277 ?layer .nextn .shared_head_head 278 :model .output ; 279ggml_tensor * head_s = layer .nextn .shared_head_head 280 ?layer .nextn .shared_head_head_s 281 :model .output_s ; 282GGML_ASSERT (head_w && "GLM4_MOE MTP: missing LM head (nextn.shared_head_head or model.output)" ); 283 284cur = build_lora_mm (head_w ,cur ,head_s ); 285cb (cur ,"result_output" ,-1 ); 286 287res -> t_logits = cur ; 288ggml_build_forward_expand (gf ,cur ); 289} 290 291llama_model_glm4_moe::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 292const int64_t n_embd_head = hparams .n_embd_head_v (); 293 294GGML_ASSERT (n_embd_head == hparams .n_embd_head_k ()); 295 296int sections [4 ]; 297 std::copy (std::begin (hparams .rope_sections ), std::begin (hparams .rope_sections )+ 4 ,sections ); 298 299ggml_tensor * cur ; 300ggml_tensor * inpL ; 301 302inpL = build_inp_embd (model .tok_embd ); 303 304bool use_mrope = hparams .use_mrope (); 305if (ubatch .embd && !use_mrope ) { 306// unfortunately, we need to forcefully stop here, to avoid users complaining about wrong results 307GGML_ABORT ("This GGUF does not support multimodal. Please reconvert it." ); 308 } 309 310// inp_pos - contains the positions 311ggml_tensor * inp_pos = build_inp_pos (); 312 313auto * inp_attn = build_attn_inp_kv (); 314 315ggml_tensor * inp_out_ids = build_inp_out_ids (); 316 317// NextN layers are processed by graph_mtp. 318for (int il = 0 ;il < n_layer ;++ il ) { 319ggml_tensor * inpSA = inpL ; 320 321// Pre-attention norm 322cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 323cb (cur ,"attn_norm" ,il ); 324 325// self-attention 326 { 327auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 328n_embd_head ,n_head ,n_head_kv ,il ); 329 330// Apply Q/K norm if available (GLM-4.5 355B variant) 331if (model .layers [il ].attn_q_norm ) { 332Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,NULL ,LLM_NORM_RMS ,il ); 333cb (Qcur ,"Qcur_normed" ,il ); 334 } 335if (model .layers [il ].attn_k_norm ) { 336Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,NULL ,LLM_NORM_RMS ,il ); 337cb (Kcur ,"Kcur_normed" ,il ); 338 } 339 340if (use_mrope ) { 341Qcur = ggml_rope_multi (ctx0 ,Qcur ,inp_pos ,nullptr , 342n_rot ,sections ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 343ext_factor ,attn_factor ,beta_fast ,beta_slow ); 344 345Kcur = ggml_rope_multi (ctx0 ,Kcur ,inp_pos ,nullptr , 346n_rot ,sections ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 347ext_factor ,attn_factor ,beta_fast ,beta_slow ); 348 }else { 349// Normal RoPE 350Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,nullptr ,n_rot , 351rope_type ,n_ctx_orig ,freq_base ,freq_scale , 352ext_factor ,attn_factor ,beta_fast ,beta_slow ); 353 354Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,nullptr ,n_rot , 355rope_type ,n_ctx_orig ,freq_base ,freq_scale , 356ext_factor ,attn_factor ,beta_fast ,beta_slow ); 357 } 358 359cb (Qcur ,"Qcur" ,il ); 360cb (Kcur ,"Kcur" ,il ); 361cb (Vcur ,"Vcur" ,il ); 362 363cur = build_attn (inp_attn , 364model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 365Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,1.0f /sqrtf (float (n_embd_head )),il ); 366 } 367if (il == n_layer - 1 && inp_out_ids && (!cparams .embeddings_nextn || cparams .embeddings_nextn_masked )) { 368cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 369inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 370 } 371ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 372cb (ffn_inp ,"ffn_inp" ,il ); 373 374// Post-attention norm 375cur = build_norm (ffn_inp ,model .layers [il ].attn_post_norm ,NULL ,LLM_NORM_RMS ,il ); 376cb (cur ,"post_attn_norm" ,il ); 377 378// Check if this is a dense layer (n_layer_dense_lead=1, so layer 0 is dense) 379if (static_cast < uint32_t > (il )< hparams .n_layer_dense_lead ) { 380// Dense FFN layer 381cur = build_ffn (cur , 382model .layers [il ].ffn_up ,NULL ,NULL , 383model .layers [il ].ffn_gate ,NULL ,NULL , 384model .layers [il ].ffn_down ,NULL ,NULL , 385NULL , 386LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 387cb (cur ,"ffn_out" ,il ); 388 }else { 389// Process routed experts using existing MoE infrastructure 390ggml_tensor * routed_out = build_moe_ffn (cur , 391model .layers [il ].ffn_gate_inp , 392model .layers [il ].ffn_up_exps , 393model .layers [il ].ffn_gate_exps , 394model .layers [il ].ffn_down_exps , 395model .layers [il ].ffn_exp_probs_b , 396n_expert ,n_expert_used , 397LLM_FFN_SILU ,hparams .expert_weights_norm , 398hparams .expert_weights_scale , 399 (llama_expert_gating_func_type )hparams .expert_gating_func , 400il ); 401cb (routed_out ,"ffn_moe_out" ,il ); 402 403// Process shared expert on original input 404ggml_tensor * shared_out = build_ffn (cur , 405model .layers [il ].ffn_up_shexp ,NULL ,NULL , 406model .layers [il ].ffn_gate_shexp ,NULL ,NULL , 407model .layers [il ].ffn_down_shexp ,NULL ,NULL , 408NULL , 409LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 410cb (shared_out ,"ffn_shexp_out" ,il ); 411 412// Final output: routed_output + shared_output 413cur = ggml_add (ctx0 ,routed_out ,shared_out ); 414cb (cur ,"ffn_out" ,il ); 415 } 416cur = ggml_add (ctx0 ,cur ,ffn_inp ); 417 418cur = build_cvec (cur ,il ); 419cb (cur ,"l_out" ,il ); 420 421// input for next layer 422inpL = cur ; 423 } 424cur = inpL ; 425cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 426 427cb (cur ,"h_nextn" ,-1 ); 428res -> t_h_nextn = cur ; 429 430if (cparams .embeddings_nextn && !cparams .embeddings_nextn_masked && inp_out_ids ) { 431cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 432 } 433 434cb (cur ,"result_norm" ,-1 ); 435res -> t_embd = cur ; 436 437// lm_head 438cur = build_lora_mm (model .output ,cur ,model .output_s ); 439 440cb (cur ,"result_output" ,-1 ); 441res -> t_logits = cur ; 442 443ggml_build_forward_expand (gf ,cur ); 444}