char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
465e49b9c
char/aliceai-t5-moe
1#include "models.h" 2 3void llama_model_deepseek2::load_arch_hparams (llama_model_loader & ml ) { 4uint32_t n_vocab = 0 ; 5ml .get_key (LLM_KV_VOCAB_SIZE ,n_vocab , false)|| ml .get_arr_n (LLM_KV_TOKENIZER_LIST ,n_vocab , false); 6 7// lite variants include DeepSeek-V2-Lite, GigaChat3-10B-A1.8B, Kanana-2-30B-A3B 8const bool is_lite = (hparams .n_layer ()== 27 || hparams .n_layer ()== 26 || (hparams .n_layer ()== 48 && n_vocab == 128256 )); 9 10ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 11ml .get_key (LLM_KV_LEADING_DENSE_BLOCK_COUNT ,hparams .n_layer_dense_lead , false); 12if (!is_lite ) { 13ml .get_key (LLM_KV_ATTENTION_Q_LORA_RANK ,hparams .n_lora_q ); 14 } 15ml .get_key (LLM_KV_ATTENTION_KV_LORA_RANK ,hparams .n_lora_kv ); 16ml .get_key (LLM_KV_ATTENTION_KEY_LENGTH_MLA ,hparams .n_embd_head_k_mla_impl , false); 17ml .get_key (LLM_KV_ATTENTION_VALUE_LENGTH_MLA ,hparams .n_embd_head_v_mla_impl , false); 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_COUNT ,hparams .n_expert_shared ); 20ml .get_key (LLM_KV_EXPERT_WEIGHTS_SCALE ,hparams .expert_weights_scale , false); 21ml .get_key (LLM_KV_EXPERT_WEIGHTS_NORM ,hparams .expert_weights_norm , false); 22ml .get_key (LLM_KV_EXPERT_GATING_FUNC ,hparams .expert_gating_func , false); 23if (hparams .expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE ) { 24// for compatibility with existing DeepSeek V2 and V2.5 GGUFs 25// that have no expert_gating_func model parameter set 26if ((hparams .n_layer ()== 47 || hparams .n_layer ()== 48 )&& n_vocab == 154880 ) { 27// GLM 4.7 Lite 28hparams .expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID ; 29 }else { 30hparams .expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX ; 31 } 32 } 33 34if (ml .get_key (LLM_KV_ROPE_SCALING_YARN_LOG_MUL ,hparams .rope_yarn_log_mul , false)) { 35// [TAG_DEEPSEEK2_YARN_LOG_MUL_FIX] 36// cancel the factor from the convert script 37hparams .rope_yarn_log_mul /=0.1f ; 38 } 39 40// (optional) temperature tuning - used by mistral-large 41ml .get_key (LLM_KV_ATTENTION_TEMPERATURE_SCALE ,hparams .f_attn_temp_scale , false); 42ml .get_key (LLM_KV_ATTENTION_TEMPERATURE_LENGTH ,hparams .n_attn_temp_floor_scale , false);// FIXME why not use temperature_length? 43 44hparams .f_attn_temp_offset = 0.0f ; 45 46switch (hparams .n_layer ()) { 47case 27 :type = LLM_TYPE_16B ;break ; 48case 47 :type = LLM_TYPE_30B_A3B ;break ; 49case 60 :type = LLM_TYPE_236B ;break ; 50case 61 :type = LLM_TYPE_671B ;break ; 51default :type = LLM_TYPE_UNKNOWN ; 52 } 53} 54 55void llama_model_deepseek2::load_arch_tensors (llama_model_loader & ml ) { 56LLAMA_LOAD_LOCALS ; 57const int64_t n_expert_shared = hparams .n_expert_shared ; 58 59const bool mtp_only = (hparams .n_layer_nextn > 0 )&& (ml .get_weight ("blk.0.attn_norm.weight" )== nullptr ); 60const std::string mtp_probe = "blk." + std::to_string (n_layer )+ ".nextn.eh_proj.weight" ; 61const bool trunk_only = (hparams .n_layer_nextn > 0 )&& (ml .get_weight (mtp_probe .c_str ())== nullptr ); 62const int trunk_flags = mtp_only ?TENSOR_NOT_REQUIRED :0 ; 63int mtp_flags = trunk_only ?TENSOR_NOT_REQUIRED :0 ; 64 65if (!ml .load_mtp ) { 66mtp_flags |=TENSOR_SKIP ; 67 } 68 69const bool is_mla = hparams .is_mla (); 70 71// note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA 72const int64_t n_embd_head_k_mla = hparams .n_embd_head_k_mla (); 73const int64_t n_embd_head_v_mla = hparams .n_embd_head_v_mla (); 74 75const int64_t n_embd_head_qk_rope = hparams .n_rot (); 76const int64_t n_embd_head_qk_nope = n_embd_head_k_mla - n_embd_head_qk_rope ; 77GGML_ASSERT (n_embd_head_qk_nope >=1 ); 78 79const int64_t q_lora_rank = hparams .n_lora_q ; 80const int64_t kv_lora_rank = hparams .n_lora_kv ; 81 82const int64_t n_ff_exp = hparams .n_ff_exp (); 83 84tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 85 86// output 87output_norm = create_tensor (tn (LLM_TENSOR_OUTPUT_NORM ,"weight" ), {n_embd },0 ); 88// try to load output.weight, if not found, use token_embd (tied embeddings) 89output = create_tensor (tn (LLM_TENSOR_OUTPUT ,"weight" ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED ); 90if (!output ) { 91output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 92 } 93 94for (int i = 0 ;i < n_layer_all ;++ i ) { 95auto & layer = layers [i ]; 96const int flags = i < n_layer ?trunk_flags :mtp_flags ; 97 98layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },flags ); 99if (q_lora_rank > 0 ) { 100layer .attn_q_a_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_A_NORM ,"weight" ,i ), {q_lora_rank },flags ); 101 } 102 103layer .attn_kv_a_norm = create_tensor (tn (LLM_TENSOR_ATTN_KV_A_NORM ,"weight" ,i ), {kv_lora_rank },flags ); 104 105if (q_lora_rank > 0 ) { 106layer .wq_a = create_tensor (tn (LLM_TENSOR_ATTN_Q_A ,"weight" ,i ), {n_embd ,q_lora_rank },flags ); 107layer .wq_b = create_tensor (tn (LLM_TENSOR_ATTN_Q_B ,"weight" ,i ), {q_lora_rank ,n_head * n_embd_head_k_mla },flags ); 108 }else { 109layer .wq = create_tensor (tn (LLM_TENSOR_ATTN_Q ,"weight" ,i ), {n_embd ,n_head * n_embd_head_k_mla },flags ); 110 } 111 112layer .wkv_a_mqa = create_tensor (tn (LLM_TENSOR_ATTN_KV_A_MQA ,"weight" ,i ), {n_embd ,kv_lora_rank + n_embd_head_qk_rope },flags ); 113 114// note: only old legacy GGUF files will have the unsplit wkv_b tensor in 115if (is_mla ) { 116layer .wk_b = create_tensor (tn (LLM_TENSOR_ATTN_K_B ,"weight" ,i ), {n_embd_head_qk_nope ,kv_lora_rank ,n_head },flags ); 117layer .wv_b = create_tensor (tn (LLM_TENSOR_ATTN_V_B ,"weight" ,i ), {kv_lora_rank ,n_embd_head_v_mla ,n_head },flags ); 118 }else { 119layer .wkv_b = create_tensor (tn (LLM_TENSOR_ATTN_KV_B ,"weight" ,i ), {kv_lora_rank ,n_head * (n_embd_head_qk_nope + n_embd_head_v_mla )},flags ); 120 } 121 122layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_head * n_embd_head_v_mla ,n_embd },flags ); 123 124layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },flags ); 125 126if (i < (int )hparams .n_layer_dense_lead ) { 127layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },flags ); 128layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },flags ); 129layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },flags ); 130 }else { 131layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },flags ); 132layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,i ), {n_expert },TENSOR_NOT_REQUIRED |flags ); 133 134if (n_expert == 0 ) { 135throw std::runtime_error ("n_expert must be > 0" ); 136 } 137if (n_expert_used == 0 ) { 138throw std::runtime_error ("n_expert_used must be > 0" ); 139 } 140 141// MoE branch 142layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },flags ); 143create_tensor_gate_up_exps (layer ,i ,n_embd ,n_ff_exp ,n_expert ,flags ); 144 145// Shared expert branch 146layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,n_ff_exp * n_expert_shared },flags ); 147layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {n_ff_exp * n_expert_shared ,n_embd },flags ); 148layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,n_ff_exp * n_expert_shared },flags ); 149 } 150 151// NextN/MTP tensors 152if (i >=n_layer ) { 153layer .nextn .eh_proj = create_tensor (tn (LLM_TENSOR_NEXTN_EH_PROJ ,"weight" ,i ), {2 * n_embd ,n_embd },mtp_flags ); 154layer .nextn .enorm = create_tensor (tn (LLM_TENSOR_NEXTN_ENORM ,"weight" ,i ), {n_embd },mtp_flags ); 155layer .nextn .hnorm = create_tensor (tn (LLM_TENSOR_NEXTN_HNORM ,"weight" ,i ), {n_embd },mtp_flags ); 156layer .nextn .embed_tokens = create_tensor (tn (LLM_TENSOR_NEXTN_EMBED_TOKENS ,"weight" ,i ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED |flags ); 157layer .nextn .shared_head_head = create_tensor (tn (LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD ,"weight" ,i ), {n_embd ,n_vocab },TENSOR_NOT_REQUIRED |flags ); 158layer .nextn .shared_head_norm = create_tensor (tn (LLM_TENSOR_NEXTN_SHARED_HEAD_NORM ,"weight" ,i ), {n_embd },TENSOR_NOT_REQUIRED |flags ); 159 } 160 } 161} 162 163std::unique_ptr < llm_graph_context > llama_model_deepseek2::build_arch_graph (const llm_graph_params & params )const { 164if (params .gtype == LLM_GRAPH_TYPE_DECODER_MTP ) { 165return std::make_unique < graph_mtp > (* this ,params ); 166 } 167return std::make_unique < graph > (* this ,params ); 168} 169 170llama_model_deepseek2::graph_mtp::graph_mtp (const llama_model & model ,const llm_graph_params & params ) : 171llm_graph_context (params ) { 172GGML_ASSERT (hparams .n_layer_nextn > 0 && "GLM4 MTP requires n_layer_nextn > 0" ); 173GGML_ASSERT (hparams .n_layer_nextn == 1 && "GLM4 MTP currently only supports a single MTP block" ); 174GGML_ASSERT (hparams .is_mla ()&& "GLM4 MTP requires MLA" ); 175GGML_ASSERT (hparams .f_attn_temp_scale == 0.0f && "GLM4 MTP does not support attention temperature scaling" ); 176 177// The appended MTP block is stored immediately after the main decoder layers. 178const int il = hparams .n_layer (); 179const auto & layer = model .layers [il ]; 180 181GGML_ASSERT (layer .nextn .eh_proj && "MTP block missing nextn.eh_proj" ); 182GGML_ASSERT (layer .nextn .enorm && "MTP block missing nextn.enorm" ); 183GGML_ASSERT (layer .nextn .hnorm && "MTP block missing nextn.hnorm" ); 184 185GGML_ASSERT ((uint32_t )il >=hparams .n_layer_dense_lead && "GLM4 MTP block expected to use MoE FFN" ); 186 187const int64_t n_embd_head_k_mla = hparams .n_embd_head_k_mla (); 188const int64_t n_embd_head_qk_rope = hparams .n_rot (); 189const int64_t n_embd_head_qk_nope = n_embd_head_k_mla - n_embd_head_qk_rope ; 190const int64_t kv_lora_rank = hparams .n_lora_kv ; 191 192GGML_ASSERT (n_embd_head_qk_nope >=1 ); 193GGML_ASSERT (hparams .n_lora_q > 0 ); 194GGML_ASSERT (layer .wq_a ); 195GGML_ASSERT (layer .attn_q_a_norm ); 196GGML_ASSERT (layer .wq_b ); 197GGML_ASSERT (layer .wkv_a_mqa ); 198GGML_ASSERT (layer .attn_kv_a_norm ); 199GGML_ASSERT (layer .wk_b ); 200 201const bool has_split_exps = 202layer .ffn_up_exps != nullptr && 203layer .ffn_gate_exps != nullptr ; 204 205const bool has_fused_exps = layer .ffn_gate_up_exps != nullptr ; 206 207GGML_ASSERT (has_split_exps || has_fused_exps ); 208GGML_ASSERT (layer .ffn_norm ); 209GGML_ASSERT (layer .ffn_gate_inp ); 210GGML_ASSERT (layer .ffn_down_exps ); 211GGML_ASSERT (layer .ffn_gate_shexp ); 212GGML_ASSERT (layer .ffn_down_shexp ); 213GGML_ASSERT (layer .ffn_up_shexp ); 214 215auto inp = std::make_unique < llm_graph_input_embd_h > (hparams .n_embd ); 216 217inp -> tokens = ggml_new_tensor_1d (ctx0 ,GGML_TYPE_I32 ,n_tokens ); 218ggml_set_input (inp -> tokens ); 219 220inp -> embd = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,hparams .n_embd_inp (),n_tokens ); 221ggml_set_input (inp -> embd ); 222 223ggml_tensor * tok_embd ; 224if (ubatch .token ) { 225ggml_tensor * tok_embd_w = layer .nextn .embed_tokens 226 ?layer .nextn .embed_tokens 227 :model .tok_embd ; 228 229tok_embd = ggml_get_rows (ctx0 ,tok_embd_w ,inp -> tokens ); 230 }else { 231tok_embd = inp -> embd ; 232 } 233cb (tok_embd ,"mtp_tok_embd" ,il ); 234 235inp -> h = ggml_new_tensor_2d (ctx0 ,GGML_TYPE_F32 ,hparams .n_embd ,n_tokens ); 236ggml_set_input (inp -> h ); 237ggml_set_name (inp -> h ,"mtp_h_input" ); 238 239ggml_tensor * h_embd = inp -> h ; 240 241res -> add_input (std::move (inp )); 242 243ggml_tensor * inp_pos = build_inp_pos (); 244ggml_tensor * inp_out_ids = build_inp_out_ids (); 245 246auto * inp_attn_k = build_attn_inp_k (); 247 248ggml_tensor * h_norm = build_norm (h_embd ,layer .nextn .hnorm ,nullptr ,LLM_NORM_RMS ,il ); 249cb (h_norm ,"mtp_hnorm" ,il ); 250 251ggml_tensor * e_norm = build_norm (tok_embd ,layer .nextn .enorm ,nullptr ,LLM_NORM_RMS ,il ); 252cb (e_norm ,"mtp_enorm" ,il ); 253 254ggml_tensor * concat = ggml_concat (ctx0 ,e_norm ,h_norm ,0 ); 255cb (concat ,"mtp_concat" ,il ); 256 257ggml_tensor * cur = build_lora_mm (layer .nextn .eh_proj ,concat ,layer .nextn .eh_proj_s ); 258cb (cur ,"mtp_eh_proj" ,il ); 259 260ggml_tensor * inpSA = cur ; 261 262cur = build_norm (cur ,layer .attn_norm ,nullptr ,LLM_NORM_RMS ,il ); 263cb (cur ,"mtp_attn_norm" ,il ); 264 265ggml_tensor * q = ggml_mul_mat (ctx0 ,layer .wq_a ,cur ); 266cb (q ,"mtp_q_a" ,il ); 267 268q = build_norm (q ,layer .attn_q_a_norm ,nullptr ,LLM_NORM_RMS ,il ); 269cb (q ,"mtp_q_a_norm" ,il ); 270 271q = ggml_mul_mat (ctx0 ,layer .wq_b ,q ); 272cb (q ,"mtp_q_b" ,il ); 273 274ggml_tensor * q_nope = 275ggml_view_3d (ctx0 ,q ,n_embd_head_qk_nope ,n_head ,n_tokens , 276ggml_row_size (q -> type ,n_embd_head_k_mla ), 277ggml_row_size (q -> type ,n_embd_head_k_mla )* n_head ,0 ); 278cb (q_nope ,"mtp_q_nope" ,il ); 279 280ggml_tensor * q_pe = 281ggml_view_3d (ctx0 ,q ,n_embd_head_qk_rope ,n_head ,n_tokens , 282ggml_row_size (q -> type ,n_embd_head_k_mla ), 283ggml_row_size (q -> type ,n_embd_head_k_mla )* n_head , 284ggml_row_size (q -> type ,n_embd_head_qk_nope )); 285cb (q_pe ,"mtp_q_pe" ,il ); 286 287ggml_tensor * kv_cmpr_pe = ggml_mul_mat (ctx0 ,layer .wkv_a_mqa ,cur ); 288cb (kv_cmpr_pe ,"mtp_kv_cmpr_pe" ,il ); 289 290ggml_tensor * kv_cmpr = 291ggml_view_2d (ctx0 ,kv_cmpr_pe ,kv_lora_rank ,n_tokens , 292ggml_row_size (kv_cmpr_pe -> type ,kv_lora_rank + n_embd_head_qk_rope ),0 ); 293cb (kv_cmpr ,"mtp_kv_cmpr" ,il ); 294 295ggml_tensor * k_pe = 296ggml_view_3d (ctx0 ,kv_cmpr_pe ,n_embd_head_qk_rope ,1 ,n_tokens , 297ggml_row_size (kv_cmpr_pe -> type ,kv_lora_rank + n_embd_head_qk_rope ), 298ggml_row_size (kv_cmpr_pe -> type ,kv_lora_rank + n_embd_head_qk_rope ), 299ggml_row_size (kv_cmpr_pe -> type ,kv_lora_rank )); 300cb (k_pe ,"mtp_k_pe" ,il ); 301 302kv_cmpr = build_norm (kv_cmpr ,layer .attn_kv_a_norm ,nullptr ,LLM_NORM_RMS ,il ); 303cb (kv_cmpr ,"mtp_kv_cmpr_norm" ,il ); 304 305GGML_ASSERT (ext_factor >=0.0f ); 306 307const float attn_factor_org = 308attn_factor * (1.0f + 0.1f * logf (1.0f /freq_scale )); 309 310const float mscale = 311attn_factor_org * (1.0f + 0.1f * hparams .rope_yarn_log_mul * logf (1.0f /freq_scale )); 312 313const float kq_scale = 3141.0f * mscale * mscale /sqrtf (float (n_embd_head_k_mla )); 315 316q_pe = ggml_rope_ext (ctx0 ,q_pe ,inp_pos ,nullptr , 317n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 318ext_factor ,attn_factor ,beta_fast ,beta_slow ); 319cb (q_pe ,"mtp_q_pe_rope" ,il ); 320 321k_pe = ggml_rope_ext (ctx0 ,k_pe ,inp_pos ,nullptr , 322n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 323ext_factor ,attn_factor ,beta_fast ,beta_slow ); 324cb (k_pe ,"mtp_k_pe_rope" ,il ); 325 326q_nope = ggml_permute (ctx0 ,q_nope ,0 ,2 ,1 ,3 ); 327cb (q_nope ,"mtp_q_nope_perm" ,il ); 328 329ggml_tensor * q_nope_absorbed = ggml_mul_mat (ctx0 ,layer .wk_b ,q_nope ); 330cb (q_nope_absorbed ,"mtp_q_nope_absorbed" ,il ); 331 332q_nope_absorbed = ggml_permute (ctx0 ,q_nope_absorbed ,0 ,2 ,1 ,3 ); 333cb (q_nope_absorbed ,"mtp_q_nope_absorbed_perm" ,il ); 334 335ggml_tensor * Qcur = ggml_concat (ctx0 ,q_nope_absorbed ,q_pe ,0 ); 336cb (Qcur ,"mtp_Qcur" ,il ); 337 338kv_cmpr = ggml_reshape_3d (ctx0 ,kv_cmpr ,hparams .n_lora_kv ,1 ,n_tokens ); 339cb (kv_cmpr ,"mtp_kv_cmpr_reshape" ,il ); 340 341ggml_tensor * Kcur = ggml_concat (ctx0 ,kv_cmpr ,k_pe ,0 ); 342cb (Kcur ,"mtp_Kcur" ,il ); 343 344ggml_tensor * Vcur = kv_cmpr ; 345cb (Vcur ,"mtp_Vcur" ,il ); 346 347cur = build_attn (inp_attn_k , 348layer .wo ,nullptr ,layer .wo_s , 349Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,layer .wv_b ,kq_scale ,il ); 350cb (cur ,"mtp_attn_out" ,il ); 351 352ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 353cb (ffn_inp ,"mtp_ffn_inp" ,il ); 354 355cur = build_norm (ffn_inp ,layer .ffn_norm ,nullptr ,LLM_NORM_RMS ,il ); 356cb (cur ,"mtp_ffn_norm" ,il ); 357 358ggml_tensor * moe_out = build_moe_ffn (cur , 359layer .ffn_gate_inp , 360layer .ffn_up_exps , 361layer .ffn_gate_exps , 362layer .ffn_down_exps , 363layer .ffn_exp_probs_b , 364n_expert ,n_expert_used , 365LLM_FFN_SILU ,hparams .expert_weights_norm , 366hparams .expert_weights_scale , 367 (llama_expert_gating_func_type )hparams .expert_gating_func , 368il , 369nullptr , 370layer .ffn_gate_up_exps ); 371cb (moe_out ,"mtp_ffn_moe_out" ,il ); 372 373ggml_tensor * ffn_shexp = build_ffn (cur , 374layer .ffn_up_shexp ,nullptr ,nullptr , 375layer .ffn_gate_shexp ,nullptr ,nullptr , 376layer .ffn_down_shexp ,nullptr ,nullptr , 377nullptr ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 378cb (ffn_shexp ,"mtp_ffn_shexp" ,il ); 379 380cur = ggml_add (ctx0 ,moe_out ,ffn_shexp ); 381cb (cur ,"mtp_ffn_out" ,il ); 382 383cur = ggml_add (ctx0 ,cur ,ffn_inp ); 384cb (cur ,"mtp_post_ffn" ,il ); 385 386ggml_tensor * head_norm_w = layer .nextn .shared_head_norm 387 ?layer .nextn .shared_head_norm 388 :model .output_norm ; 389GGML_ASSERT (head_norm_w && "GLM4 MTP: missing both nextn.shared_head_norm and output_norm" ); 390 391cur = build_norm (cur ,head_norm_w ,nullptr ,LLM_NORM_RMS ,-1 ); 392cb (cur ,"h_nextn" ,-1 ); 393res -> t_h_nextn = cur ; 394 395if (inp_out_ids ) { 396cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 397 } 398cb (cur ,"mtp_shared_head_norm" ,-1 ); 399 400ggml_tensor * head_w = layer .nextn .shared_head_head 401 ?layer .nextn .shared_head_head 402 :model .output ; 403 404ggml_tensor * head_s = layer .nextn .shared_head_head 405 ?layer .nextn .shared_head_head_s 406 :model .output_s ; 407 408GGML_ASSERT (head_w && "GLM4 MTP: missing LM head (nextn.shared_head_head or model.output)" ); 409 410cur = build_lora_mm (head_w ,cur ,head_s ); 411cb (cur ,"result_output" ,-1 ); 412 413res -> t_logits = cur ; 414ggml_build_forward_expand (gf ,cur ); 415} 416 417llama_model_deepseek2::graph::graph (const llama_model & model ,const llm_graph_params & params ) : 418llm_graph_context (params ) { 419// lite variants include DeepSeek-V2-Lite, GigaChat3-10B-A1.8B 420bool is_ocr = model .arch == LLM_ARCH_DEEPSEEK2OCR ; 421 422const bool is_mla = hparams .is_mla (); 423 424// note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA 425const int64_t n_embd_head_k = hparams .n_embd_head_k_mla (); 426const int64_t n_embd_head_v = hparams .n_embd_head_v_mla (); 427 428const int64_t n_embd_head_qk_rope = hparams .n_rot (); 429const int64_t n_embd_head_qk_nope = n_embd_head_k - n_embd_head_qk_rope ; 430 431const uint32_t kv_lora_rank = hparams .n_lora_kv ; 432 433// We have to pre-scale kq_scale and attn_factor to make the YaRN RoPE work correctly. 434// See https://github.com/ggml-org/llama.cpp/discussions/7416 for detailed explanation. 435// And also: https://github.com/ggml-org/llama.cpp/pull/17945 [TAG_DEEPSEEK2_YARN_LOG_MUL_FIX] 436 437// first cancel the adjustment from llama_hparams::yarn_attn_factor_adjust to get the original attn_factor 438GGML_ASSERT (ext_factor >=0.0f ); 439const float attn_factor_org = attn_factor * (1.0f + 0.1f * logf (1.0f /freq_scale )); 440 441// use the original attn_factor to pre-scale the kq_scale 442const float mscale = attn_factor_org * (1.0f + 0.1f * hparams .rope_yarn_log_mul * logf (1.0f /freq_scale )); 443const float kq_scale = 1.0f * mscale * mscale /sqrtf (float (n_embd_head_k )); 444 445ggml_tensor * cur ; 446ggml_tensor * inpL ; 447 448// {n_embd, n_tokens} 449inpL = build_inp_embd (model .tok_embd ); 450 451// (optional) temperature tuning - used by mistral-large 452ggml_tensor * inp_attn_scale = nullptr ; 453if (hparams .f_attn_temp_scale != 0.0f ) { 454inp_attn_scale = build_inp_attn_scale (); 455 } 456 457// inp_pos - contains the positions 458ggml_tensor * inp_pos = build_inp_pos (); 459 460auto * inp_attn_kv = !is_mla ?build_attn_inp_kv () :nullptr ; 461auto * inp_attn_k = is_mla ?build_attn_inp_k () :nullptr ; 462 463ggml_tensor * inp_out_ids = build_inp_out_ids (); 464 465for (int il = 0 ;il < n_layer ;++ il ) { 466ggml_tensor * inpSA = inpL ; 467 468// norm 469cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 470cb (cur ,"attn_norm" ,il ); 471 472// self_attention 473if (is_ocr ) { 474const int n_embed_head = hparams .n_embd /hparams .n_head (); 475const int ocr_rope_type = GGML_ROPE_TYPE_NEOX ; 476GGML_ASSERT (n_embed_head == n_embd_head_k && n_embed_head == n_embd_head_v ); 477 478auto [Qcur ,Kcur ,Vcur ]= build_qkv (model .layers [il ],cur , 479n_embed_head ,n_head ,n_head ,il ); 480cb (Qcur ,"q" ,il ); 481cb (Kcur ,"k" ,il ); 482cb (Vcur ,"v" ,il ); 483 484GGML_ASSERT (fabs (freq_base - 10000.0 )< 1e-4 ); 485Qcur = ggml_rope_ext (ctx0 ,Qcur ,inp_pos ,nullptr ,n_embed_head ,ocr_rope_type ,0 ,freq_base ,1 ,0 ,1 ,0 ,0 ); 486Kcur = ggml_rope_ext (ctx0 ,Kcur ,inp_pos ,nullptr ,n_embed_head ,ocr_rope_type ,0 ,freq_base ,1 ,0 ,1 ,0 ,0 ); 487cb (Qcur ,"q_pe" ,il ); 488cb (Kcur ,"k_pe" ,il ); 489 490cur = build_attn (inp_attn_kv , 491model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 492Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 493cb (cur ,"attn_out" ,il ); 494 } 495else { 496ggml_tensor * q = NULL ; 497 498const bool is_lite = model .layers [il ].wq ; 499 500if (!is_lite ) { 501q = ggml_mul_mat (ctx0 ,model .layers [il ].wq_a ,cur ); 502cb (q ,"q" ,il ); 503 504q = build_norm (q ,model .layers [il ].attn_q_a_norm ,nullptr ,LLM_NORM_RMS ,il ); 505cb (q ,"q" ,il ); 506 507q = ggml_mul_mat (ctx0 ,model .layers [il ].wq_b ,q ); 508cb (q ,"q" ,il ); 509 }else { 510q = ggml_mul_mat (ctx0 ,model .layers [il ].wq ,cur ); 511cb (q ,"q" ,il ); 512 } 513// {n_embd_head_k, n_head, n_tokens} 514q = ggml_reshape_3d (ctx0 ,q ,n_embd_head_k ,n_head ,n_tokens ); 515cb (q ,"q" ,il ); 516 517ggml_tensor * kv_cmpr_pe = ggml_mul_mat (ctx0 ,model .layers [il ].wkv_a_mqa ,cur ); 518cb (kv_cmpr_pe ,"kv_cmpr_pe" ,il ); 519 520// split into {kv_lora_rank, n_tokens} 521ggml_tensor * kv_cmpr = 522ggml_view_2d (ctx0 ,kv_cmpr_pe ,kv_lora_rank ,n_tokens , 523ggml_row_size (kv_cmpr_pe -> type ,kv_lora_rank + n_embd_head_qk_rope ),0 ); 524cb (kv_cmpr ,"kv_cmpr" ,il ); 525 526// and {n_embd_head_qk_rope, 1, n_tokens} 527ggml_tensor * k_pe = ggml_view_3d (ctx0 ,kv_cmpr_pe ,n_embd_head_qk_rope ,1 ,n_tokens , 528ggml_row_size (kv_cmpr_pe -> type ,kv_lora_rank + n_embd_head_qk_rope ), 529ggml_row_size (kv_cmpr_pe -> type ,kv_lora_rank + n_embd_head_qk_rope ), 530ggml_row_size (kv_cmpr_pe -> type ,kv_lora_rank )); 531cb (k_pe ,"k_pe" ,il ); 532 533k_pe = ggml_rope_ext (ctx0 ,k_pe ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 534ext_factor ,attn_factor ,beta_fast ,beta_slow ); 535cb (k_pe ,"k_pe" ,il ); 536 537kv_cmpr = build_norm (kv_cmpr ,model .layers [il ].attn_kv_a_norm ,nullptr ,LLM_NORM_RMS ,il ); 538cb (kv_cmpr ,"kv_cmpr" ,il ); 539 540if (is_mla ) { 541// split into {n_embd_head_qk_nope, n_head, n_tokens} 542ggml_tensor * q_nope = ggml_view_3d (ctx0 ,q ,n_embd_head_qk_nope ,n_head ,n_tokens , 543q -> nb [1 ],q -> nb [2 ],0 ); 544cb (q_nope ,"q_nope" ,il ); 545 546// and {n_embd_head_qk_rope, n_head, n_tokens} 547ggml_tensor * q_pe = ggml_view_3d (ctx0 ,q ,n_embd_head_qk_rope ,n_head ,n_tokens , 548q -> nb [1 ],q -> nb [2 ],ggml_row_size (q -> type ,n_embd_head_qk_nope )); 549cb (q_pe ,"q_pe" ,il ); 550 551q_pe = ggml_rope_ext (ctx0 ,q_pe ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base ,freq_scale , 552ext_factor ,attn_factor ,beta_fast ,beta_slow ); 553cb (q_pe ,"q_pe" ,il ); 554 555// {n_embd_head_qk_nope, n_tokens, n_head} 556q_nope = ggml_permute (ctx0 ,q_nope ,0 ,2 ,1 ,3 ); 557cb (q_nope ,"q_nope_perm" ,il ); 558 559// {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head} 560ggml_tensor * q_nope_absorbed = ggml_mul_mat (ctx0 ,model .layers [il ].wk_b ,q_nope ); 561cb (q_nope_absorbed ,"q_nope_absorbed" ,il ); 562 563// {kv_lora_rank, n_head, n_tokens} 564q_nope_absorbed = ggml_permute (ctx0 ,q_nope_absorbed ,0 ,2 ,1 ,3 ); 565cb (q_nope_absorbed ,"q_nope_absorbed_perm" ,il ); 566 567// {n_embd_head_qk_rope + kv_lora_rank, n_head, n_tokens} 568// note: rope must go first for in-place context shifting in build_rope_shift() 569ggml_tensor * Qcur = ggml_concat (ctx0 ,q_nope_absorbed ,q_pe ,0 ); 570cb (Qcur ,"Qcur" ,il ); 571 572kv_cmpr = ggml_reshape_3d (ctx0 ,kv_cmpr ,kv_lora_rank ,1 ,n_tokens ); 573cb (kv_cmpr ,"kv_cmpr_reshape" ,il ); 574 575// {n_embd_head_qk_rope + kv_lora_rank, 1, n_tokens} 576ggml_tensor * Kcur = ggml_concat (ctx0 ,kv_cmpr ,k_pe ,0 ); 577cb (Kcur ,"Kcur" ,il ); 578 579// {kv_lora_rank, 1, n_tokens} 580ggml_tensor * Vcur = kv_cmpr ; 581cb (Vcur ,"Vcur" ,il ); 582 583if (inp_attn_scale ) { 584// apply llama 4 temperature scaling 585Qcur = ggml_mul (ctx0 ,Qcur ,inp_attn_scale ); 586cb (Qcur ,"Qcur_attn_temp_scaled" ,il ); 587 } 588 589// note: MLA with the absorption optimization converts into MQA (ie: GQA with 1 group) 590cur = build_attn (inp_attn_k , 591model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 592Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,model .layers [il ].wv_b ,kq_scale ,il ); 593 }else { 594ggml_tensor * kv = ggml_mul_mat (ctx0 ,model .layers [il ].wkv_b ,kv_cmpr ); 595cb (kv ,"kv" ,il ); 596 597// split into {n_embd_head_qk_nope, n_head, n_tokens} 598ggml_tensor * k_nope = 599ggml_view_3d (ctx0 ,kv ,n_embd_head_qk_nope ,n_head ,n_tokens , 600ggml_row_size (kv -> type ,n_embd_head_qk_nope + n_embd_head_v ), 601ggml_row_size (kv -> type ,n_embd_head_qk_nope + n_embd_head_v )* n_head ,0 ); 602cb (k_nope ,"k_nope_view" ,il ); 603 604// and {n_embd_head_v, n_head, n_tokens} 605ggml_tensor * Vcur = ggml_view_3d (ctx0 ,kv ,n_embd_head_v ,n_head ,n_tokens , 606ggml_row_size (kv -> type ,n_embd_head_qk_nope + n_embd_head_v ), 607ggml_row_size (kv -> type ,n_embd_head_qk_nope + n_embd_head_v )* n_head , 608ggml_row_size (kv -> type ,n_embd_head_qk_nope )); 609cb (Vcur ,"Vcur_view" ,il ); 610 611Vcur = ggml_cont (ctx0 ,Vcur ); 612cb (Vcur ,"Vcur_cont" ,il ); 613 614// RoPE is applied to the trailing dims only 615ggml_tensor * Qcur = ggml_rope_ext (ctx0 ,q ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig , 616freq_base ,freq_scale ,ext_factor ,attn_factor ,beta_fast ,beta_slow ); 617Qcur = ggml_rope_set_offset (Qcur ,n_embd_head_qk_nope ); 618cb (Qcur ,"Qcur" ,il ); 619 620ggml_tensor * Kcur = ggml_concat (ctx0 ,k_nope , 621ggml_repeat_4d (ctx0 ,k_pe ,n_embd_head_qk_rope ,n_head ,n_tokens ,1 ),0 ); 622cb (Kcur ,"Kcur" ,il ); 623 624if (inp_attn_scale ) { 625// apply llama 4 temperature scaling 626Qcur = ggml_mul (ctx0 ,Qcur ,inp_attn_scale ); 627cb (Qcur ,"Qcur_attn_temp_scaled" ,il ); 628 } 629 630// note: MLA without the absorption optimization converts into MHA (ie: GQA with full n_head groups) 631cur = build_attn (inp_attn_kv , 632model .layers [il ].wo ,NULL ,model .layers [il ].wo_s , 633Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,nullptr ,kq_scale ,il ); 634 } 635 } 636if (il == n_layer - 1 && inp_out_ids && (!cparams .embeddings_nextn || cparams .embeddings_nextn_masked )) { 637cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 638inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 639 } 640ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 641cb (ffn_inp ,"ffn_inp" ,il ); 642 643cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 644cb (cur ,"ffn_norm" ,il ); 645 646if ((uint32_t )il < hparams .n_layer_dense_lead ) { 647cur = build_ffn (cur , 648model .layers [il ].ffn_up ,NULL ,NULL , 649model .layers [il ].ffn_gate ,NULL ,NULL , 650model .layers [il ].ffn_down ,NULL ,NULL , 651NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 652cb (cur ,"ffn_out" ,il ); 653 }else { 654// MoE branch 655ggml_tensor * moe_out = build_moe_ffn (cur , 656model .layers [il ].ffn_gate_inp , 657model .layers [il ].ffn_up_exps , 658model .layers [il ].ffn_gate_exps , 659model .layers [il ].ffn_down_exps , 660model .layers [il ].ffn_exp_probs_b , 661n_expert ,n_expert_used , 662LLM_FFN_SILU ,hparams .expert_weights_norm , 663hparams .expert_weights_scale , 664 (llama_expert_gating_func_type )hparams .expert_gating_func , 665il , 666nullptr , 667model .layers [il ].ffn_gate_up_exps ); 668cb (moe_out ,"ffn_moe_out" ,il ); 669 670// FFN shared expert 671 { 672ggml_tensor * ffn_shexp = 673build_ffn (cur , 674model .layers [il ].ffn_up_shexp ,NULL ,NULL , 675model .layers [il ].ffn_gate_shexp ,NULL ,NULL , 676model .layers [il ].ffn_down_shexp ,NULL ,NULL , 677NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 678cb (ffn_shexp ,"ffn_shexp" ,il ); 679 680cur = ggml_add (ctx0 ,moe_out ,ffn_shexp ); 681cb (cur ,"ffn_out" ,il ); 682 } 683 } 684cur = ggml_add (ctx0 ,cur ,ffn_inp ); 685 686cur = build_cvec (cur ,il ); 687cb (cur ,"l_out" ,il ); 688 689// input for next layer 690inpL = cur ; 691 } 692cur = inpL ; 693 694cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 695 696cb (cur ,"h_nextn" ,-1 ); 697res -> t_h_nextn = cur ; 698 699if (cparams .embeddings_nextn && !cparams .embeddings_nextn_masked && inp_out_ids ) { 700cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 701 } 702 703cb (cur ,"result_norm" ,-1 ); 704res -> t_embd = cur ; 705 706// lm_head 707cur = ggml_mul_mat (ctx0 ,model .output ,cur ); 708 709cb (cur ,"result_output" ,-1 ); 710res -> t_logits = cur ; 711 712ggml_build_forward_expand (gf ,cur ); 713}