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