char-fork/llama.cpp
git clone https://git.t4t.associates/char-fork/llama.cpp
c61b98b87
char/aliceai-t5-moe
1#include "models.h" 2 3#include "llama-kv-cache.h" 4#include "llama-kv-cache-dsa.h" 5 6// note: code adapted from deepseek32.cpp (DSA indexer + absorbed MLA) and step35.cpp (head-wise output gate) 7 8void llama_model_dots3note::load_arch_hparams (llama_model_loader & ml ) { 9ml .get_key (LLM_KV_ATTENTION_LAYERNORM_RMS_EPS ,hparams .f_norm_rms_eps ); 10hparams .f_norm_eps = 1e-6 ;// eps for the indexer k_norm layer norm 11 12// MoE parameters 13ml .get_key (LLM_KV_EXPERT_SHARED_COUNT ,hparams .n_expert_shared ); 14ml .get_key_or_arr (LLM_KV_EXPERT_FEED_FORWARD_LENGTH ,hparams .n_ff_exp_arr ,hparams .n_layer_all ); 15ml .get_key (LLM_KV_LEADING_DENSE_BLOCK_COUNT ,hparams .n_layer_dense_lead ); 16ml .get_key (LLM_KV_EXPERT_WEIGHTS_SCALE ,hparams .expert_weights_scale , false); 17ml .get_key (LLM_KV_EXPERT_WEIGHTS_NORM ,hparams .expert_weights_norm , false); 18ml .get_key (LLM_KV_EXPERT_GATING_FUNC ,hparams .expert_gating_func ); 19 20// MLA parameters of the full-attention layers 21ml .get_key (LLM_KV_ATTENTION_Q_LORA_RANK ,hparams .n_lora_q ); 22ml .get_key (LLM_KV_ATTENTION_KV_LORA_RANK ,hparams .n_lora_kv ); 23ml .get_key (LLM_KV_ATTENTION_KEY_LENGTH_MLA ,hparams .n_embd_head_k_mla_impl ); 24ml .get_key (LLM_KV_ATTENTION_VALUE_LENGTH_MLA ,hparams .n_embd_head_v_mla_impl ); 25 26// MLA parameters of the sliding-window layers 27ml .get_key (LLM_KV_ATTENTION_KV_LORA_RANK_SWA ,hparams .n_lora_kv_swa ); 28ml .get_key (LLM_KV_ATTENTION_KEY_LENGTH_MLA_SWA ,hparams .n_embd_head_k_mla_swa ); 29ml .get_key (LLM_KV_ATTENTION_VALUE_LENGTH_MLA_SWA ,hparams .n_embd_head_v_mla_swa ); 30 31hparams .swa_type = LLAMA_SWA_TYPE_STANDARD ; 32ml .get_key (LLM_KV_ATTENTION_SLIDING_WINDOW ,hparams .n_swa ); 33ml .get_key (LLM_KV_ROPE_FREQ_BASE_SWA ,hparams .rope_freq_base_train_swa ); 34ml .get_arr (LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN ,hparams .is_swa_impl ); 35 36// DSA parameters 37ml .get_key (LLM_KV_ATTENTION_INDEXER_HEAD_COUNT ,hparams .indexer_n_head ); 38ml .get_key (LLM_KV_ATTENTION_INDEXER_KEY_LENGTH ,hparams .indexer_head_size ); 39ml .get_key (LLM_KV_ATTENTION_INDEXER_TOP_K ,hparams .indexer_top_k ); 40ml .get_arr (LLM_KV_ATTENTION_INDEXER_TYPES ,hparams .is_indexer_full_impl ); 41 42switch (hparams .n_layer ()) { 43case 46 :type = LLM_TYPE_288B_A19B ;break ; 44default :type = LLM_TYPE_UNKNOWN ; 45 } 46} 47 48void llama_model_dots3note::load_arch_tensors (llama_model_loader & ml ) { 49LLAMA_LOAD_LOCALS ; 50GGML_UNUSED (ml ); 51 52if (!hparams .is_mla ()) { 53throw std::runtime_error ("DOTS3NOTE architecture requires MLA" ); 54 } 55 56const int64_t n_embd_head_qk_rope = hparams .n_rot (); 57 58const int64_t q_lora_rank = hparams .n_lora_q ; 59const int64_t n_ff_exp = hparams .n_ff_exp (); 60const int64_t n_expert_shared = hparams .n_expert_shared ; 61 62tok_embd = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },0 ); 63 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 ); 66if (!output ) { 67output = create_tensor (tn (LLM_TENSOR_TOKEN_EMBD ,"weight" ), {n_embd ,n_vocab },TENSOR_DUPLICATED ); 68 } 69 70for (int i = 0 ;i < n_layer_all ;++ i ) { 71auto & layer = layers [i ]; 72 73const bool is_mtp = i >=n_layer ; 74// the NextN/MTP block uses the sliding-attention geometry 75const bool is_swa = is_mtp || hparams .is_swa (i ); 76 77// MTP tensors are preserved in the GGUF but there is no MTP graph yet 78const int flags = is_mtp ?TENSOR_SKIP |TENSOR_NOT_REQUIRED :0 ; 79 80const int64_t n_head_l = hparams .n_head (i ); 81 82const int64_t kv_lora_rank = is_swa ?hparams .n_lora_kv_swa :hparams .n_lora_kv ; 83const int64_t n_embd_head_k_mla = is_swa ?hparams .n_embd_head_k_mla_swa :hparams .n_embd_head_k_mla (); 84const int64_t n_embd_head_v_mla = is_swa ?hparams .n_embd_head_v_mla_swa :hparams .n_embd_head_v_mla (); 85const int64_t n_embd_head_qk_nope = n_embd_head_k_mla - n_embd_head_qk_rope ; 86 87layer .attn_norm = create_tensor (tn (LLM_TENSOR_ATTN_NORM ,"weight" ,i ), {n_embd },flags ); 88layer .attn_q_a_norm = create_tensor (tn (LLM_TENSOR_ATTN_Q_A_NORM ,"weight" ,i ), {q_lora_rank },flags ); 89layer .attn_kv_a_norm = create_tensor (tn (LLM_TENSOR_ATTN_KV_A_NORM ,"weight" ,i ), {kv_lora_rank },flags ); 90// norm applied on the shared rope key before rope 91layer .attn_k_norm = create_tensor (tn (LLM_TENSOR_ATTN_K_NORM ,"weight" ,i ), {n_embd_head_qk_rope },flags ); 92 93layer .wq_a = create_tensor (tn (LLM_TENSOR_ATTN_Q_A ,"weight" ,i ), {n_embd ,q_lora_rank },flags ); 94layer .wq_b = create_tensor (tn (LLM_TENSOR_ATTN_Q_B ,"weight" ,i ), {q_lora_rank ,n_head_l * n_embd_head_k_mla },flags ); 95 96layer .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 ); 97 98layer .wk_b = create_tensor (tn (LLM_TENSOR_ATTN_K_B ,"weight" ,i ), {n_embd_head_qk_nope ,kv_lora_rank ,n_head_l },flags ); 99layer .wv_b = create_tensor (tn (LLM_TENSOR_ATTN_V_B ,"weight" ,i ), {kv_lora_rank ,n_embd_head_v_mla ,n_head_l },flags ); 100 101layer .wo = create_tensor (tn (LLM_TENSOR_ATTN_OUT ,"weight" ,i ), {n_head_l * n_embd_head_v_mla ,n_embd },flags ); 102 103// head-wise sigmoid output gate 104layer .wqkv_gate = create_tensor (tn (LLM_TENSOR_ATTN_GATE ,"weight" ,i ), {n_embd ,n_head_l },flags ); 105 106layer .ffn_norm = create_tensor (tn (LLM_TENSOR_FFN_NORM ,"weight" ,i ), {n_embd },flags ); 107 108// DSA indexer 109if (!is_mtp && hparams .is_indexer_full (i )) { 110layer .indexer_k_norm = create_tensor (tn (LLM_TENSOR_INDEXER_K_NORM ,"weight" ,i ), {hparams .indexer_head_size },flags ); 111layer .indexer_k_norm_b = create_tensor (tn (LLM_TENSOR_INDEXER_K_NORM ,"bias" ,i ), {hparams .indexer_head_size },flags ); 112layer .indexer_proj = create_tensor (tn (LLM_TENSOR_INDEXER_PROJ ,"weight" ,i ), {n_embd ,hparams .indexer_n_head },flags ); 113layer .indexer_attn_k = create_tensor (tn (LLM_TENSOR_INDEXER_ATTN_K ,"weight" ,i ), {n_embd ,hparams .indexer_head_size },flags ); 114layer .indexer_attn_q_b = create_tensor (tn (LLM_TENSOR_INDEXER_ATTN_Q_B ,"weight" ,i ), {q_lora_rank ,hparams .indexer_n_head * hparams .indexer_head_size },flags ); 115 } 116 117if (is_mtp || i < (int )hparams .n_layer_dense_lead ) { 118layer .ffn_gate = create_tensor (tn (LLM_TENSOR_FFN_GATE ,"weight" ,i ), {n_embd ,n_ff },flags ); 119layer .ffn_down = create_tensor (tn (LLM_TENSOR_FFN_DOWN ,"weight" ,i ), {n_ff ,n_embd },flags ); 120layer .ffn_up = create_tensor (tn (LLM_TENSOR_FFN_UP ,"weight" ,i ), {n_embd ,n_ff },flags ); 121 }else { 122if (n_expert == 0 || n_expert_used == 0 ) { 123throw std::runtime_error ("n_expert and n_expert_used must be > 0" ); 124 } 125 126layer .ffn_gate_inp = create_tensor (tn (LLM_TENSOR_FFN_GATE_INP ,"weight" ,i ), {n_embd ,n_expert },flags ); 127layer .ffn_exp_probs_b = create_tensor (tn (LLM_TENSOR_FFN_EXP_PROBS_B ,"bias" ,i ), {n_expert },flags ); 128 129layer .ffn_gate_exps = create_tensor (tn (LLM_TENSOR_FFN_GATE_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },flags ); 130layer .ffn_down_exps = create_tensor (tn (LLM_TENSOR_FFN_DOWN_EXPS ,"weight" ,i ), {n_ff_exp ,n_embd ,n_expert },flags ); 131layer .ffn_up_exps = create_tensor (tn (LLM_TENSOR_FFN_UP_EXPS ,"weight" ,i ), {n_embd ,n_ff_exp ,n_expert },flags ); 132 133layer .ffn_gate_shexp = create_tensor (tn (LLM_TENSOR_FFN_GATE_SHEXP ,"weight" ,i ), {n_embd ,n_ff_exp * n_expert_shared },flags ); 134layer .ffn_down_shexp = create_tensor (tn (LLM_TENSOR_FFN_DOWN_SHEXP ,"weight" ,i ), {n_ff_exp * n_expert_shared ,n_embd },flags ); 135layer .ffn_up_shexp = create_tensor (tn (LLM_TENSOR_FFN_UP_SHEXP ,"weight" ,i ), {n_embd ,n_ff_exp * n_expert_shared },flags ); 136 } 137 138if (is_mtp ) { 139layer .nextn .eh_proj = create_tensor (tn (LLM_TENSOR_NEXTN_EH_PROJ ,"weight" ,i ), {2 * n_embd ,n_embd },flags ); 140layer .nextn .enorm = create_tensor (tn (LLM_TENSOR_NEXTN_ENORM ,"weight" ,i ), {n_embd },flags ); 141layer .nextn .hnorm = create_tensor (tn (LLM_TENSOR_NEXTN_HNORM ,"weight" ,i ), {n_embd },flags ); 142layer .nextn .embed_tokens = create_tensor (tn (LLM_TENSOR_NEXTN_EMBED_TOKENS ,"weight" ,i ), {n_embd ,n_vocab },flags ); 143layer .nextn .shared_head_norm = create_tensor (tn (LLM_TENSOR_NEXTN_SHARED_HEAD_NORM ,"weight" ,i ), {n_embd },flags ); 144 } 145 } 146} 147 148std::unique_ptr < llm_graph_context > llama_model_dots3note::build_arch_graph (const llm_graph_params & params )const { 149return std::make_unique < graph > (* this ,params ); 150} 151 152llama_model_dots3note::graph::graph (const llama_model & model ,const llm_graph_params & params ) : 153llm_graph_context (params ) { 154GGML_ASSERT (hparams .is_mla ()); 155 156const int64_t n_embd_head_qk_rope = hparams .n_rot (); 157 158const int64_t n_indexer_head = hparams .indexer_n_head ; 159const int64_t n_embd_indexer_head = hparams .indexer_head_size ; 160const uint32_t n_indexer_top_k = hparams .indexer_top_k ; 161 162// the indexer head layout is [rope | nope] 163GGML_ASSERT (hparams .n_rot () <=n_embd_indexer_head ); 164 165ggml_tensor * cur ; 166ggml_tensor * inpL ; 167 168inpL = build_inp_embd (model .tok_embd ); 169 170ggml_tensor * inp_pos = build_inp_pos (); 171 172llm_graph_input_attn_k_dsa_iswa * inp_attn = build_attn_inp_k_dsa_iswa (); 173 174ggml_tensor * inp_out_ids = build_inp_out_ids (); 175 176for (int il = 0 ;il < n_layer ;++ il ) { 177ggml_tensor * inpSA = inpL ; 178 179const bool is_swa = hparams .is_swa (il ); 180 181const int64_t n_head_l = hparams .n_head (il ); 182 183const int64_t kv_lora_rank = is_swa ?hparams .n_lora_kv_swa :hparams .n_lora_kv ; 184const int64_t n_embd_head_k_mla = is_swa ?hparams .n_embd_head_k_mla_swa :hparams .n_embd_head_k_mla (); 185const int64_t n_embd_head_v_mla = is_swa ?hparams .n_embd_head_v_mla_swa :hparams .n_embd_head_v_mla (); 186const int64_t n_embd_head_qk_nope = n_embd_head_k_mla - n_embd_head_qk_rope ; 187 188const float kq_scale = 1.0f /sqrtf (float (n_embd_head_k_mla )); 189const float freq_base_l = model .get_rope_freq_base (cparams ,il ); 190 191// norm 192cur = build_norm (inpL ,model .layers [il ].attn_norm ,NULL ,LLM_NORM_RMS ,il ); 193cb (cur ,"attn_norm" ,il ); 194 195// self_attention 196 { 197ggml_tensor * attn_inp = cur ; 198 199ggml_tensor * qr = ggml_mul_mat (ctx0 ,model .layers [il ].wq_a ,cur ); 200cb (qr ,"qr" ,il ); 201 202qr = build_norm (qr ,model .layers [il ].attn_q_a_norm ,nullptr ,LLM_NORM_RMS ,il ); 203cb (qr ,"qr" ,il ); 204 205ggml_tensor * top_k = nullptr ; 206 207// lightning indexer (full-attention layers only) 208if (!is_swa ) { 209ggml_tensor * indexer_q = ggml_mul_mat (ctx0 ,model .layers [il ].indexer_attn_q_b ,qr ); 210cb (indexer_q ,"indexer_q" ,il ); 211 212// {n_embd_indexer_head, n_indexer_head, n_tokens} 213indexer_q = ggml_reshape_3d (ctx0 ,indexer_q ,n_embd_indexer_head ,n_indexer_head ,n_tokens ); 214indexer_q = ggml_rope_ext (ctx0 ,indexer_q ,inp_pos ,nullptr ,n_rot , 215LLAMA_ROPE_TYPE_NEOX ,n_ctx_orig ,freq_base ,freq_scale , 216ext_factor ,attn_factor ,beta_fast ,beta_slow ); 217cb (indexer_q ,"indexer_q" ,il ); 218 219ggml_tensor * indexer_k = ggml_mul_mat (ctx0 ,model .layers [il ].indexer_attn_k ,cur ); 220cb (indexer_k ,"indexer_k" ,il ); 221 222indexer_k = build_norm (indexer_k ,model .layers [il ].indexer_k_norm ,model .layers [il ].indexer_k_norm_b ,LLM_NORM ,il ); 223cb (indexer_k ,"indexer_k" ,il ); 224 225// {n_embd_indexer_head, 1, n_tokens} 226indexer_k = ggml_reshape_3d (ctx0 ,indexer_k ,n_embd_indexer_head ,1 ,n_tokens ); 227indexer_k = ggml_rope_ext (ctx0 ,indexer_k ,inp_pos ,nullptr ,n_rot , 228LLAMA_ROPE_TYPE_NEOX ,n_ctx_orig ,freq_base ,freq_scale , 229ext_factor ,attn_factor ,beta_fast ,beta_slow ); 230cb (indexer_k ,"indexer_k" ,il ); 231 232// perform Hadamard transform on indexer q and k 233indexer_q = ggml_mul_mat (ctx0 ,inp_attn -> get_dsa ()-> self_k_rot_lid ,indexer_q ); 234cb (indexer_q ,"indexer_q" ,il ); 235indexer_k = ggml_mul_mat (ctx0 ,inp_attn -> get_dsa ()-> self_k_rot_lid ,indexer_k ); 236cb (indexer_k ,"indexer_k" ,il ); 237 238// store indexer keys to KV cache 239const auto * mctx_lid = inp_attn -> get_dsa ()-> mctx -> get_lid (); 240const auto & k_idxs_lid = inp_attn -> get_dsa ()-> get_k_idxs_lid (); 241ggml_build_forward_expand (gf ,mctx_lid -> cpy_k (ctx0 ,indexer_k ,k_idxs_lid ,il )); 242 243ggml_tensor * indexer_weights = ggml_mul_mat (ctx0 ,model .layers [il ].indexer_proj ,cur ); 244cb (indexer_weights ,"indexer_weights" ,il ); 245 246indexer_k = mctx_lid -> get_k (ctx0 ,il ); 247 248// split the batch into streams if needed 249const auto n_stream = indexer_k -> ne [3 ]; 250indexer_q = ggml_view_4d (ctx0 ,indexer_q ,indexer_q -> ne [0 ],indexer_q -> ne [1 ],indexer_q -> ne [2 ]/n_stream ,n_stream ,indexer_q -> nb [1 ],indexer_q -> nb [2 ],indexer_q -> nb [3 ]/n_stream ,0 ); 251indexer_weights = ggml_view_4d (ctx0 ,indexer_weights ,indexer_weights -> ne [0 ],indexer_weights -> ne [1 ]/n_stream ,indexer_weights -> ne [2 ],n_stream ,indexer_weights -> nb [1 ],indexer_weights -> nb [2 ]/n_stream ,indexer_weights -> nb [3 ]/n_stream ,0 ); 252 253// pre-scale weights to avoid scaling operations on huge indexer_score tensor 254indexer_weights = ggml_scale (ctx0 ,indexer_weights ,1.0f /sqrtf (float (n_embd_indexer_head * n_indexer_head ))); 255cb (indexer_weights ,"indexer_weights" ,il ); 256 257ggml_tensor * indexer_score = nullptr ; 258if (cparams .fused_lid ) { 259indexer_score = ggml_lightning_indexer (ctx0 ,indexer_q ,indexer_k ,indexer_weights ,inp_attn -> get_dsa ()-> get_kq_mask_lid ()); 260cb (indexer_score ,"indexer_score" ,il ); 261res -> add_fused_node ({LLM_FUSED_OP_LIGHTNING_INDEXER ,indexer_score ,il }); 262 }else { 263indexer_q = ggml_permute (ctx0 ,indexer_q ,0 ,2 ,1 ,3 ); 264cb (indexer_q ,"indexer_q" ,il ); 265indexer_k = ggml_permute (ctx0 ,indexer_k ,0 ,2 ,1 ,3 ); 266cb (indexer_k ,"indexer_k" ,il ); 267 268ggml_tensor * indexer_kq = ggml_mul_mat (ctx0 ,indexer_k ,indexer_q ); 269cb (indexer_kq ,"indexer_kq" ,il ); 270 271// ReLU requires contiguous tensors 272indexer_kq = ggml_cont (ctx0 ,ggml_permute (ctx0 ,indexer_kq ,2 ,1 ,0 ,3 )); 273cb (indexer_kq ,"indexer_kq" ,il ); 274 275indexer_score = ggml_relu (ctx0 ,indexer_kq ); 276cb (indexer_score ,"indexer_score" ,il ); 277 278indexer_score = ggml_mul (ctx0 ,indexer_score ,indexer_weights ); 279cb (indexer_score ,"indexer_score" ,il ); 280 281// sum by q n_indexer_head dimension 282indexer_score = ggml_sum_rows (ctx0 ,indexer_score ); 283cb (indexer_score ,"indexer_score" ,il ); 284 285// permute result to match KQ mask 286indexer_score = ggml_cont (ctx0 ,ggml_permute (ctx0 ,indexer_score ,2 ,1 ,0 ,3 )); 287cb (indexer_score ,"indexer_score" ,il ); 288 289ggml_tensor * indexer_kq_mask = inp_attn -> get_dsa ()-> get_kq_mask_lid (); 290indexer_score = ggml_add (ctx0 ,indexer_score ,indexer_kq_mask ); 291cb (indexer_score ,"indexer_score" ,il ); 292 } 293 294// get indices of top k indexer scores 295uint32_t n_top_k = indexer_score -> ne [0 ]< n_indexer_top_k ?indexer_score -> ne [0 ] :n_indexer_top_k ; 296top_k = ggml_cont (ctx0 ,ggml_top_k (ctx0 ,indexer_score ,n_top_k )); 297cb (top_k ,"top_k" ,il ); 298 } 299 300ggml_tensor * q = ggml_mul_mat (ctx0 ,model .layers [il ].wq_b ,qr ); 301cb (q ,"q" ,il ); 302 303// split into {n_embd_head_qk_nope, n_head_l, n_tokens} 304ggml_tensor * q_nope = 305ggml_view_3d (ctx0 ,q ,n_embd_head_qk_nope ,n_head_l ,n_tokens ,ggml_row_size (q -> type ,n_embd_head_k_mla ), 306ggml_row_size (q -> type ,n_embd_head_k_mla )* n_head_l ,0 ); 307cb (q_nope ,"q_nope" ,il ); 308 309// and {n_embd_head_qk_rope, n_head_l, n_tokens} 310ggml_tensor * q_pe = ggml_view_3d ( 311ctx0 ,q ,n_embd_head_qk_rope ,n_head_l ,n_tokens ,ggml_row_size (q -> type ,n_embd_head_k_mla ), 312ggml_row_size (q -> type ,n_embd_head_k_mla )* n_head_l ,ggml_row_size (q -> type ,n_embd_head_qk_nope )); 313cb (q_pe ,"q_pe" ,il ); 314 315ggml_tensor * kv_cmpr_pe = ggml_mul_mat (ctx0 ,model .layers [il ].wkv_a_mqa ,cur ); 316cb (kv_cmpr_pe ,"kv_cmpr_pe" ,il ); 317 318// split into {kv_lora_rank, n_tokens} 319ggml_tensor * kv_cmpr = 320ggml_view_2d (ctx0 ,kv_cmpr_pe ,kv_lora_rank ,n_tokens , 321ggml_row_size (kv_cmpr_pe -> type ,kv_lora_rank + n_embd_head_qk_rope ),0 ); 322cb (kv_cmpr ,"kv_cmpr" ,il ); 323 324// and {n_embd_head_qk_rope, 1, n_tokens} 325ggml_tensor * k_pe = ggml_view_3d (ctx0 ,kv_cmpr_pe ,n_embd_head_qk_rope ,1 ,n_tokens , 326ggml_row_size (kv_cmpr_pe -> type ,kv_lora_rank + n_embd_head_qk_rope ), 327ggml_row_size (kv_cmpr_pe -> type ,kv_lora_rank + n_embd_head_qk_rope ), 328ggml_row_size (kv_cmpr_pe -> type ,kv_lora_rank )); 329cb (k_pe ,"k_pe" ,il ); 330 331// norm on the shared rope key, applied before rope 332k_pe = build_norm (k_pe ,model .layers [il ].attn_k_norm ,nullptr ,LLM_NORM_RMS ,il ); 333cb (k_pe ,"k_pe" ,il ); 334 335q_pe = ggml_rope_ext (ctx0 ,q_pe ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale , 336ext_factor ,attn_factor ,beta_fast ,beta_slow ); 337cb (q_pe ,"q_pe" ,il ); 338 339k_pe = ggml_rope_ext (ctx0 ,k_pe ,inp_pos ,nullptr ,n_rot ,rope_type ,n_ctx_orig ,freq_base_l ,freq_scale , 340ext_factor ,attn_factor ,beta_fast ,beta_slow ); 341cb (k_pe ,"k_pe" ,il ); 342 343kv_cmpr = build_norm (kv_cmpr ,model .layers [il ].attn_kv_a_norm ,nullptr ,LLM_NORM_RMS ,il ); 344cb (kv_cmpr ,"kv_cmpr" ,il ); 345 346// MLA attention with the absorption optimization 347 { 348// {n_embd_head_qk_nope, n_tokens, n_head_l} 349q_nope = ggml_permute (ctx0 ,q_nope ,0 ,2 ,1 ,3 ); 350cb (q_nope ,"q_nope_perm" ,il ); 351 352// {n_embd_head_qk_nope, kv_lora_rank, n_head_l} x {n_embd_head_qk_nope, n_tokens, n_head_l} 353ggml_tensor * q_nope_absorbed = ggml_mul_mat (ctx0 ,model .layers [il ].wk_b ,q_nope ); 354cb (q_nope_absorbed ,"q_nope_absorbed" ,il ); 355 356// {kv_lora_rank, n_head_l, n_tokens} 357q_nope_absorbed = ggml_permute (ctx0 ,q_nope_absorbed ,0 ,2 ,1 ,3 ); 358cb (q_nope_absorbed ,"q_nope_absorbed_perm" ,il ); 359 360// {n_embd_head_qk_rope + kv_lora_rank, n_head_l, n_tokens} 361ggml_tensor * Qcur = ggml_concat (ctx0 ,q_nope_absorbed ,q_pe ,0 ); 362cb (Qcur ,"Qcur" ,il ); 363 364kv_cmpr = ggml_reshape_3d (ctx0 ,kv_cmpr ,kv_lora_rank ,1 ,n_tokens ); 365cb (kv_cmpr ,"kv_cmpr_reshape" ,il ); 366 367// {n_embd_head_qk_rope + kv_lora_rank, 1, n_tokens} 368ggml_tensor * Kcur = ggml_concat (ctx0 ,kv_cmpr ,k_pe ,0 ); 369cb (Kcur ,"Kcur" ,il ); 370 371// {kv_lora_rank, 1, n_tokens} 372ggml_tensor * Vcur = kv_cmpr ; 373cb (Vcur ,"Vcur" ,il ); 374 375// apply the head-wise output gate before o_proj, so wo stays out of build_attn 376if (is_swa ) { 377cur = build_attn (inp_attn -> get_swa (), 378nullptr ,nullptr ,nullptr , 379Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,model .layers [il ].wv_b ,kq_scale ,il ); 380 }else { 381cur = build_attn (inp_attn -> get_dsa (), 382nullptr ,nullptr ,nullptr , 383Qcur ,Kcur ,Vcur ,nullptr ,nullptr ,model .layers [il ].wv_b ,top_k ,kq_scale ,il ); 384 } 385cb (cur ,"attn_out" ,il ); 386 387ggml_tensor * gate = build_lora_mm (model .layers [il ].wqkv_gate ,attn_inp ); 388cb (gate ,"attn_gate" ,il ); 389 390gate = ggml_sigmoid (ctx0 ,gate ); 391cb (gate ,"attn_gate_sigmoid" ,il ); 392 393// broadcast the per-head gate over the head dimension 394ggml_tensor * attn_3d = ggml_reshape_3d (ctx0 ,cur ,n_embd_head_v_mla ,n_head_l ,n_tokens ); 395ggml_tensor * gate_3d = ggml_reshape_3d (ctx0 ,gate ,1 ,n_head_l ,n_tokens ); 396attn_3d = ggml_mul (ctx0 ,attn_3d ,gate_3d ); 397cb (attn_3d ,"attn_gated" ,il ); 398 399cur = ggml_reshape_2d (ctx0 ,attn_3d ,n_embd_head_v_mla * n_head_l ,n_tokens ); 400 401cur = build_lora_mm (model .layers [il ].wo ,cur ,model .layers [il ].wo_s ); 402cb (cur ,"attn_output" ,il ); 403 } 404 } 405 406if (il == n_layer - 1 && inp_out_ids ) { 407cur = ggml_get_rows (ctx0 ,cur ,inp_out_ids ); 408inpSA = ggml_get_rows (ctx0 ,inpSA ,inp_out_ids ); 409 } 410 411ggml_tensor * ffn_inp = ggml_add (ctx0 ,cur ,inpSA ); 412cb (ffn_inp ,"ffn_inp" ,il ); 413 414cur = build_norm (ffn_inp ,model .layers [il ].ffn_norm ,NULL ,LLM_NORM_RMS ,il ); 415cb (cur ,"ffn_norm" ,il ); 416 417if ((uint32_t )il < hparams .n_layer_dense_lead ) { 418cur = build_ffn (cur , 419model .layers [il ].ffn_up ,NULL ,model .layers [il ].ffn_up_s , 420model .layers [il ].ffn_gate ,NULL ,model .layers [il ].ffn_gate_s , 421model .layers [il ].ffn_down ,NULL ,model .layers [il ].ffn_down_s , 422NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 423cb (cur ,"ffn_out" ,il ); 424 }else { 425ggml_tensor * moe_out = build_moe_ffn (cur , 426model .layers [il ].ffn_gate_inp , 427model .layers [il ].ffn_up_exps , 428model .layers [il ].ffn_gate_exps , 429model .layers [il ].ffn_down_exps , 430model .layers [il ].ffn_exp_probs_b , 431n_expert ,n_expert_used , 432LLM_FFN_SILU ,hparams .expert_weights_norm , 433hparams .expert_weights_scale , 434 (llama_expert_gating_func_type )hparams .expert_gating_func , 435il , 436nullptr , 437model .layers [il ].ffn_gate_up_exps , 438model .layers [il ].ffn_up_exps_s , 439model .layers [il ].ffn_gate_exps_s , 440model .layers [il ].ffn_down_exps_s ); 441cb (moe_out ,"ffn_moe_out" ,il ); 442 443ggml_tensor * ffn_shexp = 444build_ffn (cur , 445model .layers [il ].ffn_up_shexp ,NULL ,model .layers [il ].ffn_up_shexp_s , 446model .layers [il ].ffn_gate_shexp ,NULL ,model .layers [il ].ffn_gate_shexp_s , 447model .layers [il ].ffn_down_shexp ,NULL ,model .layers [il ].ffn_down_shexp_s , 448NULL ,LLM_FFN_SILU ,LLM_FFN_PAR ,il ); 449cb (ffn_shexp ,"ffn_shexp" ,il ); 450 451cur = ggml_add (ctx0 ,moe_out ,ffn_shexp ); 452cb (cur ,"ffn_out" ,il ); 453 } 454 455cur = ggml_add (ctx0 ,cur ,ffn_inp ); 456 457cur = build_cvec (cur ,il ); 458cb (cur ,"l_out" ,il ); 459 460inpL = cur ; 461 } 462 463cur = inpL ; 464 465cur = build_norm (cur ,model .output_norm ,NULL ,LLM_NORM_RMS ,-1 ); 466 467cb (cur ,"result_norm" ,-1 ); 468res -> t_embd = cur ; 469 470cur = ggml_mul_mat (ctx0 ,model .output ,cur ); 471 472cb (cur ,"result_output" ,-1 ); 473res -> t_logits = cur ; 474 475ggml_build_forward_expand (gf ,cur ); 476}