char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
97e4ca735
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_step35::load_arch_hparams (llama_model_loader & ml ) { 4ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 5 6hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 7 8// full_attention layer only use half of the RoPE dimensions 9hparams .n_rot_full = hparams .n_rot_full /2 ; 10 11// MoE + SWA parameters 12ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all ); 13ml .get_key (LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH ,hparams .n_ff_shexp , false); 14ml .get_key (LLM_KV_EXPERT_GATING_FUNC ,hparams .expert_gating_func , false); 15ml .get_key (LLM_KV_EXPERT_WEIGHTS_SCALE ,hparams .expert_weights_scale , false); 16ml .get_key (LLM_KV_EXPERT_WEIGHTS_NORM ,hparams .expert_weights_norm , false); 17 18// Step35 uses sigmoid gating by default (if not set in GGUF) 19if (hparams .expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE ) { 20hparams .expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID ; 21 } 22 23ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa ); 24ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa , false); 25 26ml .get_arr (LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN ,hparams .is_swa_impl ); 27 28ml .get_key_or_arr (LLM_KV_SWIGLU_CLAMP_EXP ,hparams .swiglu_clamp_exp ,hparams .n_layer_all , false); 29ml .get_key_or_arr (LLM_KV_SWIGLU_CLAMP_SHEXP ,hparams .swiglu_clamp_shexp ,hparams .n_layer_all , false); 30 31switch (hparams .n_layer ()) { 32case 45 :type = LLM_TYPE_196B_A11B ;break ; 33default :type = LLM_TYPE_UNKNOWN ; 34 } 35} 36 37void llama_model_step35::load_arch_tensors (llama_model_loader & ml ) { 38LLAMA_LOAD_LOCALS ; 39 40const bool mtp_only = (hparams .n_layer_nextn > 0 )&& (ml .get_weight ("blk.0.attn_norm.weight" )== nullptr ); 41// Trunk-only: the GGUF declares MTP layers in metadata but the actual MTP 42// tensors live in a separate file (e.g. user split target/draft). Mark 43// MTP tensors NOT_REQUIRED so the trunk loads cleanly. 44const std::string mtp_probe = "blk." + std::to_string (n_layer )+ ".nextn.eh_proj.weight" ; 45const bool trunk_only = (hparams .n_layer_nextn > 0 )&& (ml .get_weight (mtp_probe .c_str ())== nullptr ); 46const int trunk_flags = mtp_only ?TENSOR_NOT_REQUIRED :0 ; 47int mtp_flags = trunk_only ?TENSOR_NOT_REQUIRED :0 ; 48 49if (!ml .load_mtp ) { 50mtp_flags |=TENSOR_SKIP ; 51 } 52 53tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 54 55// output 56output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 57output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },trunk_flags ); 58 59// STEP35 supports per-layer partial RoPE dims; rope factors are stored as a single shared tensor 60// ("rope_freqs.weight") and ggml uses only the first (n_rot_l/2) entries per layer. 61uint32_t n_rot_max = 0 ; 62for (int i = 0 ;i < n_layer ;++ i ) { 63n_rot_max = std::max (n_rot_max ,hparams .n_rot (i )); 64 } 65if (n_rot_max == 0 ) { 66n_rot_max = n_rot ; 67 } 68 69auto load_block_trunk = [& ](int i ,int flags ) { 70auto & layer = layers [i ]; 71 72const uint32_t n_head_l = hparams .n_head (i ); 73const uint32_t n_embd_k_gqa = hparams .n_embd_k_gqa (i ); 74const uint32_t n_embd_v_gqa = hparams .n_embd_v_gqa (i ); 75 76layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },flags ); 77layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },TENSOR_NOT_REQUIRED ); 78layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },TENSOR_NOT_REQUIRED ); 79 80// optional rope factors (llama3) / longrope tensors 81if (hparams .rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE ) { 82layer .rope_long = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_LONG ,"weight" ,i ), {n_rot_max /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 83layer .rope_short = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_SHORT ,"weight" ,i ), {n_rot_max /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 84 }else { 85layer .rope_freqs = create_tensor (tn (LLM_TENSOR_ROPE_FREQS ,"weight" ,i ), {n_rot_max /2 },TENSOR_NOT_REQUIRED | (i != 0 ?TENSOR_DUPLICATED :0 )); 86 } 87 88create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head_l ,n_embd_k_gqa ,n_embd_v_gqa ,flags ); 89layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_v * n_head_l ,n_embd },flags ); 90 91// head-wise attention gate (Step35 self_attn.g_proj) 92layer .wqkv_gate = create_tensor (tn (LLM_TENSOR_ATTN_GATE ,"weight" ,i ), {n_embd ,n_head_l },TENSOR_NOT_REQUIRED ); 93 94layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },flags ); 95 96// dense MLP (leading dense blocks) 97layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },TENSOR_NOT_REQUIRED ); 98layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },TENSOR_NOT_REQUIRED ); 99layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },TENSOR_NOT_REQUIRED ); 100 101// MoE routed experts + selection bias (router_bias) 102const int64_t n_ff_exp = hparams .n_ff_exp (); 103layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },TENSOR_NOT_REQUIRED ); 104layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },TENSOR_NOT_REQUIRED ); 105layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },TENSOR_NOT_REQUIRED ); 106layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },TENSOR_NOT_REQUIRED ); 107layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,i ), {n_expert },TENSOR_NOT_REQUIRED ); 108 109// shared expert MLP 110layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },TENSOR_NOT_REQUIRED ); 111layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },TENSOR_NOT_REQUIRED ); 112layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {hparams .n_ff_shexp ,n_embd },TENSOR_NOT_REQUIRED ); 113 }; 114 115auto load_block_mtp = [& ](int i ) { 116auto & layer = layers [i ]; 117 118const uint32_t n_head_l = hparams .n_head (i ); 119const uint32_t n_embd_k_gqa = hparams .n_embd_k_gqa (i ); 120const uint32_t n_embd_v_gqa = hparams .n_embd_v_gqa (i ); 121 122// The MTP block is a full Step3p5 decoder layer (mtp_block) plus the 123// NextN-specific wiring (enorm/hnorm/eh_proj + optional shared head). 124// Multi-block MTP: every declared MTP block is required (the draft chain 125// runs all n_layer_nextn heads), so each block uses the captured 126// `mtp_flags` directly — already NOT_REQUIRED for a trunk-only GGUF, 127// which keeps that path correct. 128 129layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },mtp_flags ); 130layer .attn_q_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_NORM ,"weight" ,i ), {n_embd_head_k },TENSOR_NOT_REQUIRED ); 131layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_k },TENSOR_NOT_REQUIRED ); 132 133if (hparams .rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE ) { 134layer .rope_long = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_LONG ,"weight" ,i ), {n_rot_max /2 },TENSOR_NOT_REQUIRED |TENSOR_DUPLICATED ); 135layer .rope_short = create_tensor (tn (LLM_TENSOR_ROPE_FACTORS_SHORT ,"weight" ,i ), {n_rot_max /2 },TENSOR_NOT_REQUIRED |TENSOR_DUPLICATED ); 136 }else { 137layer .rope_freqs = create_tensor (tn (LLM_TENSOR_ROPE_FREQS ,"weight" ,i ), {n_rot_max /2 },TENSOR_NOT_REQUIRED |TENSOR_DUPLICATED ); 138 } 139 140create_tensor_qkv (layer ,i ,n_embd ,n_embd_head_k * n_head_l ,n_embd_k_gqa ,n_embd_v_gqa ,mtp_flags ); 141layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_embd_head_v * n_head_l ,n_embd },mtp_flags ); 142 143layer .wqkv_gate = create_tensor (tn (LLM_TENSOR_ATTN_GATE ,"weight" ,i ), {n_embd ,n_head_l },TENSOR_NOT_REQUIRED ); 144 145layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },mtp_flags ); 146 147// dense MLP (leading dense blocks) — present if the MTP block isn't MoE 148layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },TENSOR_NOT_REQUIRED ); 149layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },TENSOR_NOT_REQUIRED ); 150layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },TENSOR_NOT_REQUIRED ); 151 152// MoE routed experts + selection bias (router_bias) 153const int64_t n_ff_exp = hparams .n_ff_exp (); 154layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },TENSOR_NOT_REQUIRED ); 155layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },TENSOR_NOT_REQUIRED ); 156layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },TENSOR_NOT_REQUIRED ); 157layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },TENSOR_NOT_REQUIRED ); 158layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,i ), {n_expert },TENSOR_NOT_REQUIRED ); 159 160layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },TENSOR_NOT_REQUIRED ); 161layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,hparams .n_ff_shexp },TENSOR_NOT_REQUIRED ); 162layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {hparams .n_ff_shexp ,n_embd },TENSOR_NOT_REQUIRED ); 163 164// NextN-specific tensors that define the MTP block. 165layer .nextn .eh_proj = create_tensor (tn (LLM_TENSOR_NEXTN_EH_PROJ ,"weight" ,i ), {2 * n_embd ,n_embd },mtp_flags ); 166layer .nextn .enorm = create_tensor (tn (LLM_TENSOR_NEXTN_ENORM ,"weight" ,i ), {n_embd },mtp_flags ); 167layer .nextn .hnorm = create_tensor (tn (LLM_TENSOR_NEXTN_HNORM ,"weight" ,i ), {n_embd },mtp_flags ); 168layer .nextn .embed_tokens = create_tensor (tn (LLM_TENSOR_NEXTN_EMBED_TOKENS ,"weight" ,i ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 169layer .nextn .shared_head_head = create_tensor (tn (LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD ,"weight" ,i ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 170layer .nextn .shared_head_norm = create_tensor (tn (LLM_TENSOR_NEXTN_SHARED_HEAD_NORM ,"weight" ,i ), {n_embd },TENSOR_NOT_REQUIRED ); 171 }; 172 173for (int i = 0 ;i < n_layer ;++ i ) { 174load_block_trunk (i ,trunk_flags ); 175 } 176// All n_layer_nextn MTP blocks are required — the multi-block draft chain 177// runs every head (head k at offset k). The GGUF declares the count via 178// step35.nextn_predict_layers. 179for (int i = n_layer ;i < n_layer_all ;++ i ) { 180load_block_mtp (i ); 181 } 182} 183 184std::unique_ptr < llm_graph_context > llama_model_step35::build_arch_graph (const llm_graph_params & params )const { 185if (params .gtype == LLM_GRAPH_TYPE_DECODER_MTP ) { 186return std::make_unique < graph_mtp > (* this ,params ); 187 } 188return std::make_unique < graph > (* this ,params ); 189} 190 191llama_model_step35::graph::graph (const llama_model & model ,const llm_graph_params & params ) :llm_graph_context (params ) { 192ggml_tensor * cur ; 193ggml_tensor * inpL ; 194 195inpL = build_inp_embd (model .tok_embd ); 196ggml_tensor * inp_pos = build_inp_pos (); 197auto * inp_attn = build_attn_inp_kv_iswa (); 198ggml_tensor * inp_out_ids = build_inp_out_ids (); 199 200// MTP/NextN layers are loaded as extra decoder blocks but not executed in the main pass. 201for (int il = 0 ;il < n_layer ;++ il ) { 202ggml_tensor * inpSA = inpL ; 203 204const uint32_t n_head_l = hparams .n_head (il ); 205const uint32_t n_head_kv_l = hparams .n_head_kv (il ); 206 207const float freq_base_l = model .get_rope_freq_base (cparams ,il ); 208const float freq_scale_l = model .get_rope_freq_scale (cparams ,il ); 209 210cur = inpL ; 211 212// dump pre-attn RMSNorm input to pinpoint layer boundary issues 213cb (cur ,"attn_norm_in" ,il ); 214 215// self-attention 216 { 217cur = build_norm (cur ,model .layers [il ].attn_norm ,nullptr ,LLM_NORM_RMS ,il ); 218cb (cur ,"attn_norm" ,il ); 219auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 220n_embd_head_k ,n_head_l , 221n_embd_head_k ,n_head_kv_l , 222n_embd_head_v ,n_head_kv_l , 223il , false); 224 225cb (Qcur ,"Qcur" ,il ); 226cb (Kcur ,"Kcur" ,il ); 227cb (Vcur ,"Vcur" ,il ); 228 229Qcur = ggml_reshape_3d (ctx0 ,Qcur ,n_embd_head_k ,n_head_l ,n_tokens ); 230Kcur = ggml_reshape_3d (ctx0 ,Kcur ,n_embd_head_k ,n_head_kv_l ,n_tokens ); 231Vcur = ggml_reshape_3d (ctx0 ,Vcur ,n_embd_head_v ,n_head_kv_l ,n_tokens ); 232 233// Q/K per-head RMSNorm (Step35 q_norm / k_norm) 234if (model .layers [il ].attn_q_norm ) { 235Qcur = build_norm (Qcur ,model .layers [il ].attn_q_norm ,nullptr ,LLM_NORM_RMS ,il ); 236cb (Qcur ,"Qcur_normed" ,il ); 237 } 238if (model .layers [il ].attn_k_norm ) { 239Kcur = build_norm (Kcur ,model .layers [il ].attn_k_norm ,nullptr ,LLM_NORM_RMS ,il ); 240cb (Kcur ,"Kcur_normed" ,il ); 241 } 242 243// RoPE (partial rotary factors per layer) 244const bool is_swa = hparams .is_swa (il ); 245ggml_tensor * rope_factors = is_swa ?nullptr :model .get_rope_factors (cparams ,il ); 246const int64_t n_rot_l = hparams .n_rot (il ); 247Qcur = ggml_rope_ext ( 248ctx0 ,Qcur ,inp_pos ,rope_factors , 249n_rot_l ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 250ext_factor ,attn_factor ,beta_fast ,beta_slow 251 ); 252Kcur = ggml_rope_ext ( 253ctx0 ,Kcur ,inp_pos ,rope_factors , 254n_rot_l ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 255ext_factor ,attn_factor ,beta_fast ,beta_slow 256 ); 257cb (Qcur ,"Qcur_pos" ,il ); 258cb (Kcur ,"Kcur_pos" ,il ); 259 260const float kq_scale = 1.0f /sqrtf (float (n_embd_head_k )); 261ggml_tensor * attn_out = build_attn (inp_attn , 262nullptr ,nullptr ,nullptr , 263Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 264cb (attn_out ,"attn_out" ,il ); 265// head-wise attention gate: sigmoid(g_proj(x)) in torch 266if (model .layers [il ].wqkv_gate ) { 267ggml_tensor * gate = build_lora_mm (model .layers [il ].wqkv_gate ,cur );// [n_head_l, n_tokens] 268cb (gate ,"attn_gate" ,il ); 269 270gate = ggml_sigmoid (ctx0 ,gate ); 271cb (gate ,"attn_gate_sigmoid" ,il ); 272 273// reshape + broadcast to [n_embd_head_v, n_head_l, n_tokens] 274ggml_tensor * attn_3d = ggml_reshape_3d (ctx0 ,attn_out ,n_embd_head_v ,n_head_l ,n_tokens ); 275ggml_tensor * gate_3d = ggml_reshape_3d (ctx0 ,gate ,1 ,n_head_l ,n_tokens ); 276cb (gate_3d ,"attn_gate_3d" ,il ); 277 278attn_3d = ggml_mul (ctx0 ,attn_3d ,gate_3d ); 279cb (attn_3d ,"attn_gated_3d" ,il ); 280 281attn_out = ggml_reshape_2d (ctx0 ,attn_3d ,n_embd_head_v * n_head_l ,n_tokens ); 282cb (attn_out ,"attn_gated" ,il ); 283 } 284 285// output projection 286cur = build_lora_mm (model .layers [il ].wo ,attn_out ,model .layers [il ].wo_s ); 287cb (cur ,"attn_proj" ,il ); 288 } 289 290if (il == n_layer - 1 && inp_out_ids && cparams .embeddings_nextn_masked ) { 291cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 292inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 293 } 294 295ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 296cb (ffn_inp ,"ffn_inp" ,il ); 297 298cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,nullptr ,LLM_NORM_RMS ,il ); 299cb (cur ,"ffn_norm" ,il ); 300 301// feed-forward 302if (model .layers [il ].ffn_gate_inp == nullptr ) { 303// dense MLP 304cur = build_ffn (cur , 305model .layers [il ].ffn_up ,model .layers [il ].ffn_up_b ,nullptr , 306model .layers [il ].ffn_gate ,model .layers [il ].ffn_gate_b ,nullptr , 307model .layers [il ].ffn_down ,model .layers [il ].ffn_down_b ,nullptr , 308nullptr , 309LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 310cb (cur ,"ffn_out" ,il ); 311 }else { 312// MoE routed experts 313ggml_tensor * moe_out = build_moe_ffn (cur , 314model .layers [il ].ffn_gate_inp , 315model .layers [il ].ffn_up_exps , 316model .layers [il ].ffn_gate_exps , 317model .layers [il ].ffn_down_exps , 318model .layers [il ].ffn_exp_probs_b , 319n_expert ,n_expert_used , 320LLM_FFN_SILU ,hparams .expert_weights_norm , 321hparams .expert_weights_scale , 322 (llama_expert_gating_func_type )hparams .expert_gating_func , 323il ); 324cb (moe_out ,"ffn_moe_out" ,il ); 325 326// shared expert MLP (always added on MoE layers in Step35) 327ggml_tensor * sh_out = build_ffn (cur , 328model .layers [il ].ffn_up_shexp ,nullptr ,nullptr , 329model .layers [il ].ffn_gate_shexp ,nullptr ,nullptr , 330model .layers [il ].ffn_down_shexp ,nullptr ,nullptr , 331nullptr , 332LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 333cb (sh_out ,"ffn_shared_out" ,il ); 334 335cur = ggml_add (ctx0 ,moe_out ,sh_out ); 336cb (cur ,"ffn_out" ,il ); 337 } 338cur = ggml_add (ctx0 ,cur ,ffn_inp ); 339 340cur = build_cvec (cur ,il ); 341cb (cur ,"l_out" ,il ); 342 343// input for next layer 344inpL = cur ; 345 } 346 347cur = inpL ; 348 349cb (cur ,"h_nextn" ,-1 ); 350res -> t_h_nextn = cur ; 351 352if (!cparams .embeddings_nextn_masked && inp_out_ids ) { 353cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 354 } 355 356cur = build_norm (cur ,model .output_norm ,nullptr ,LLM_NORM_RMS ,-1 ); 357cb (cur ,"result_norm" ,-1 ); 358res -> t_embd = cur ; 359 360cur = build_lora_mm (model .output ,cur ,model .output_s ); 361cb (cur ,"result_output" ,-1 ); 362res -> t_logits = cur ; 363 364ggml_build_forward_expand (gf ,cur ); 365} 366 367// LLM_GRAPH_TYPE_DECODER_MTP draft head for Step3p5 (MoE) 368llama_model_step35::graph_mtp::graph_mtp (const llama_model & model ,const llm_graph_params & params ) 369 :llm_graph_context (params ) { 370GGML_ASSERT (hparams .n_layer_nextn > 0 && "STEP35 MTP requires n_layer_nextn > 0" ); 371 372// Multi-block MTP: the DECODER_MTP graph runs the MTP head selected by 373// cparams.nextn_layer_offset (0 = first trained head). The speculative driver 374// bumps the offset per draft step to chain heads 45->46->47. offset 0 keeps 375// single-block behavior identical to before. 376const int il = hparams .n_layer ()+ cparams .nextn_layer_offset ; 377GGML_ASSERT (cparams .nextn_layer_offset >=0 && 378cparams .nextn_layer_offset < (int )hparams .n_layer_nextn && 379"nextn_layer_offset out of range [0, n_layer_nextn)" ); 380const auto & layer = model .layers [il ]; 381 382GGML_ASSERT (layer .nextn .eh_proj && "MTP block missing nextn.eh_proj" ); 383GGML_ASSERT (layer .nextn .enorm && "MTP block missing nextn.enorm" ); 384GGML_ASSERT (layer .nextn .hnorm && "MTP block missing nextn.hnorm" ); 385 386const uint32_t n_head_l = hparams .n_head (il ); 387const uint32_t n_head_kv_l = hparams .n_head_kv (il ); 388 389const float freq_base_l = model .get_rope_freq_base (cparams ,il ); 390const float freq_scale_l = model .get_rope_freq_scale (cparams ,il ); 391 392auto inp = std::make_unique < llm_graph_input_embd > (hparams .n_embd ); 393 394inp -> tokens = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_tokens ); 395ggml_set_input (inp -> tokens ); 396 397inp -> embd = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,hparams .n_embd ,n_tokens ); 398ggml_set_input (inp -> embd ); 399ggml_set_name (inp -> embd ,"mtp_h_input" ); 400 401ggml_tensor * tok_embd_w = layer .nextn .embed_tokens ?layer .nextn .embed_tokens :model .tok_embd ; 402 403ggml_tensor * h_input = inp -> embd ; 404ggml_tensor * tok_embd = ggml_get_rows (ctx0 ,tok_embd_w ,inp -> tokens ); 405cb (tok_embd ,"mtp_tok_embd" ,il ); 406 407res -> add_input (std::move (inp )); 408 409ggml_tensor * inp_pos = build_inp_pos (); 410auto * inp_attn = build_attn_inp_kv_iswa (); 411 412ggml_tensor * h_norm = build_norm (h_input ,layer .nextn .hnorm ,nullptr ,LLM_NORM_RMS ,il ); 413cb (h_norm ,"mtp_hnorm" ,il ); 414 415ggml_tensor * e_norm = build_norm (tok_embd ,layer .nextn .enorm ,nullptr ,LLM_NORM_RMS ,il ); 416cb (e_norm ,"mtp_enorm" ,il ); 417 418ggml_tensor * concat = ggml_concat (ctx0 ,e_norm ,h_norm ,/*dim=*/ 0 ); 419cb (concat ,"mtp_concat" ,il ); 420 421ggml_tensor * cur = build_lora_mm (layer .nextn .eh_proj ,concat ); 422cb (cur ,"mtp_eh_proj" ,il ); 423 424ggml_tensor * inpSA = cur ; 425 426// mtp_block: full Step3p5 decoder layer (attention with optional head-wise gate, then MoE/dense FFN) 427cur = build_norm (cur ,layer .attn_norm ,nullptr ,LLM_NORM_RMS ,il ); 428cb (cur ,"mtp_attn_norm" ,il ); 429 430auto [Qcur ,Kcur ,Vcur ]= build_qkv (layer ,cur , 431n_embd_head_k ,n_head_l , 432n_embd_head_k ,n_head_kv_l , 433n_embd_head_v ,n_head_kv_l , 434il , false); 435cb (Qcur ,"mtp_Qcur" ,il ); 436cb (Kcur ,"mtp_Kcur" ,il ); 437cb (Vcur ,"mtp_Vcur" ,il ); 438 439Qcur = ggml_reshape_3d (ctx0 ,Qcur ,n_embd_head_k ,n_head_l ,n_tokens ); 440Kcur = ggml_reshape_3d (ctx0 ,Kcur ,n_embd_head_k ,n_head_kv_l ,n_tokens ); 441Vcur = ggml_reshape_3d (ctx0 ,Vcur ,n_embd_head_v ,n_head_kv_l ,n_tokens ); 442 443if (layer .attn_q_norm ) { 444Qcur = build_norm (Qcur ,layer .attn_q_norm ,nullptr ,LLM_NORM_RMS ,il ); 445cb (Qcur ,"mtp_Qcur_normed" ,il ); 446 } 447if (layer .attn_k_norm ) { 448Kcur = build_norm (Kcur ,layer .attn_k_norm ,nullptr ,LLM_NORM_RMS ,il ); 449cb (Kcur ,"mtp_Kcur_normed" ,il ); 450 } 451 452const bool is_swa = hparams .is_swa (il ); 453ggml_tensor * rope_factors = is_swa ?nullptr :model .get_rope_factors (cparams ,il ); 454const int64_t n_rot_l = hparams .n_rot (il ); 455 456Qcur = ggml_rope_ext ( 457ctx0 ,Qcur ,inp_pos ,rope_factors , 458n_rot_l ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 459ext_factor ,attn_factor ,beta_fast ,beta_slow ); 460Kcur = ggml_rope_ext ( 461ctx0 ,Kcur ,inp_pos ,rope_factors , 462n_rot_l ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale_l , 463ext_factor ,attn_factor ,beta_fast ,beta_slow ); 464cb (Qcur ,"mtp_Qcur_pos" ,il ); 465cb (Kcur ,"mtp_Kcur_pos" ,il ); 466 467const float kq_scale = 1.0f /sqrtf (float (n_embd_head_k )); 468ggml_tensor * attn_out = build_attn (inp_attn , 469nullptr ,nullptr ,nullptr , 470Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 471cb (attn_out ,"mtp_attn_out" ,il ); 472 473// head-wise attention gate: sigmoid(g_proj(x)) 474if (layer .wqkv_gate ) { 475ggml_tensor * gate = build_lora_mm (layer .wqkv_gate ,cur );// [n_head_l, n_tokens] 476cb (gate ,"mtp_attn_gate" ,il ); 477 478gate = ggml_sigmoid (ctx0 ,gate ); 479cb (gate ,"mtp_attn_gate_sigmoid" ,il ); 480 481ggml_tensor * attn_3d = ggml_reshape_3d (ctx0 ,attn_out ,n_embd_head_v ,n_head_l ,n_tokens ); 482ggml_tensor * gate_3d = ggml_reshape_3d (ctx0 ,gate ,1 ,n_head_l ,n_tokens ); 483cb (gate_3d ,"mtp_attn_gate_3d" ,il ); 484 485attn_3d = ggml_mul (ctx0 ,attn_3d ,gate_3d ); 486cb (attn_3d ,"mtp_attn_gated_3d" ,il ); 487 488attn_out = ggml_reshape_2d (ctx0 ,attn_3d ,n_embd_head_v * n_head_l ,n_tokens ); 489cb (attn_out ,"mtp_attn_gated" ,il ); 490 } 491 492cur = build_lora_mm (layer .wo ,attn_out ,layer .wo_s ); 493cb (cur ,"mtp_attn_proj" ,il ); 494 495cur = ggml_add (ctx0 ,cur ,inpSA ); 496cb (cur ,"mtp_attn_residual" ,il ); 497 498ggml_tensor * ffn_inp = cur ; 499cur = build_norm (cur ,layer .ffn_norm ,nullptr ,LLM_NORM_RMS ,il ); 500cb (cur ,"mtp_ffn_norm" ,il ); 501 502// FFN: dense MLP or MoE (mirrors trunk path) 503if (layer .ffn_gate_inp == nullptr ) { 504cur = build_ffn (cur , 505layer .ffn_up ,layer .ffn_up_b ,nullptr , 506layer .ffn_gate ,layer .ffn_gate_b ,nullptr , 507layer .ffn_down ,layer .ffn_down_b ,nullptr , 508nullptr , 509LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 510cb (cur ,"mtp_ffn_out" ,il ); 511 }else { 512ggml_tensor * moe_out = build_moe_ffn (cur , 513layer .ffn_gate_inp , 514layer .ffn_up_exps , 515layer .ffn_gate_exps , 516layer .ffn_down_exps , 517layer .ffn_exp_probs_b , 518n_expert ,n_expert_used , 519LLM_FFN_SILU ,hparams .expert_weights_norm , 520hparams .expert_weights_scale , 521 (llama_expert_gating_func_type )hparams .expert_gating_func , 522il ); 523cb (moe_out ,"mtp_ffn_moe_out" ,il ); 524 525ggml_tensor * sh_out = build_ffn (cur , 526layer .ffn_up_shexp ,nullptr ,nullptr , 527layer .ffn_gate_shexp ,nullptr ,nullptr , 528layer .ffn_down_shexp ,nullptr ,nullptr , 529nullptr , 530LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 531cb (sh_out ,"mtp_ffn_shared_out" ,il ); 532 533cur = ggml_add (ctx0 ,moe_out ,sh_out ); 534cb (cur ,"mtp_ffn_out" ,il ); 535 } 536cur = ggml_add (ctx0 ,cur ,ffn_inp ); 537cb (cur ,"mtp_post_ffn" ,il ); 538 539ggml_tensor * inp_out_ids = build_inp_out_ids (); 540cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 541 542// Pre-norm hidden state: used by the AR draft loop to seed the next MTP step. 543cb (cur ,"h_nextn" ,-1 ); 544res -> t_h_nextn = cur ; 545 546ggml_tensor * head_norm_w = layer .nextn .shared_head_norm 547 ?layer .nextn .shared_head_norm 548 :model .output_norm ; 549GGML_ASSERT (head_norm_w && "STEP35 MTP: missing both nextn.shared_head_norm and output_norm" ); 550cur = build_norm (cur ,head_norm_w ,nullptr ,LLM_NORM_RMS ,-1 ); 551cb (cur ,"mtp_shared_head_norm" ,-1 ); 552 553ggml_tensor * head_w = layer .nextn .shared_head_head ?layer .nextn .shared_head_head :model .output ; 554GGML_ASSERT (head_w && "STEP35 MTP: missing LM head (nextn.shared_head_head or model.output)" ); 555cur = build_lora_mm (head_w ,cur ); 556cb (cur ,"result_output" ,-1 ); 557 558res -> t_logits = cur ; 559ggml_build_forward_expand (gf ,cur ); 560}