Team Ai
Datasetpublic

Brunobkr/llama.cpp_AlgMor24_github

ΩFFFΣLLIa • llama.cpp • AlgMor24 ██████╗ ███████╗███████╗███████╗██╗ ██╗ ██╗ █████╗ ██╔═══██╗██╔════╝██╔════╝██╔════╝██║ ██║ ██║██╔══██╗ ██║ ██║█████╗ █████╗ █████╗ ██║ ██║ ██║███████║ ██║ ██║██╔══╝ ██╔══╝ ██╔══╝ ██║ ██║ ██║██╔══██║ ╚██████╔╝██║ ██║ ███████╗███████╗███████╗██║██║ ██║ ╚═════╝ ╚═╝ ╚═╝ ╚══════╝╚══════╝╚══════╝╚═╝╚═╝ ╚═╝ High-Performance LLM / VLM Inference & Autonomous Agentic Ecosystem… See the full description on the dataset page: https://huggingface.co/datasets/Brunobkr/llama.cpp_AlgMor24_github.

sourceHugging Faceupdated 2mo agoView on Hugging Face
0likes3.1kdownloads
glm-dsa.cpp811 linesDownload Raw Back to models
1#include "models.h"2 3#include "llama-kv-cache-dsa.h"4 5// https://huggingface.co/zai-org/GLM-5.2/blob/main/config.json#L266const std::array<uint32_t, LLAMA_MAX_LAYERS> GLM_5_2_DEFAULT_INDEXER_TYPES = {7    1, 1,8    1, 0, 0, 0,9    1, 0, 0, 0,10    1, 0, 0, 0,11    1, 0, 0, 0,12    1, 0, 0, 0,13    1, 0, 0, 0,14    1, 0, 0, 0,15    1, 0, 0, 0,16    1, 0, 0, 0,17    1, 0, 0, 0,18    1, 0, 0, 0,19    1, 0, 0, 0,20    1, 0, 0, 0,21    1, 0, 0, 0,22    1, 0, 0, 0,23    1, 0, 0, 0,24    1, 0, 0, 0,25    1, 0, 0, 0,26    1, 0, 0, 0,27};28 29void llama_model_glm_dsa::load_arch_hparams(llama_model_loader & ml) {30    ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH,     hparams.n_ff_exp);31    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS,    hparams.f_norm_rms_eps);32    ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, false);33 34    // MoE parameters35    ml.get_key(LLM_KV_EXPERT_COUNT,                hparams.n_expert);36    ml.get_key(LLM_KV_EXPERT_USED_COUNT,           hparams.n_expert_used);37    ml.get_key(LLM_KV_EXPERT_SHARED_COUNT,         hparams.n_expert_shared);38    ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT,   hparams.n_layer_dense_lead, false);39    ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE,        hparams.expert_weights_scale, false);40    ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM,         hparams.expert_weights_norm, false);41 42    // deepseek MLA parameters43    ml.get_key(LLM_KV_ATTENTION_Q_LORA_RANK,      hparams.n_lora_q);44    ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK,     hparams.n_lora_kv);45    ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH_MLA,   hparams.n_embd_head_k_mla_impl, false);46    ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH_MLA, hparams.n_embd_head_v_mla_impl, false);47    ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);48    ml.get_key(LLM_KV_EXPERT_SHARED_COUNT,        hparams.n_expert_shared);49 50    // DSA parameters51    ml.get_key(LLM_KV_ATTENTION_INDEXER_HEAD_COUNT, hparams.indexer_n_head);52    ml.get_key(LLM_KV_ATTENTION_INDEXER_KEY_LENGTH, hparams.indexer_head_size);53    ml.get_key(LLM_KV_ATTENTION_INDEXER_TOP_K,      hparams.indexer_top_k);54 55    // Expert gating function (GLM-4.5 uses sigmoid)56    ml.get_key(LLM_KV_EXPERT_GATING_FUNC,          hparams.expert_gating_func, false);57    if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) {58        hparams.expert_gating_func =  LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID;59    }60 61    // NextN/MTP parameters62    ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.n_layer_nextn, false);63    GGML_ASSERT(hparams.n_layer_nextn < hparams.n_layer_all && "n_layer_nextn must be < n_layer_all");64 65    // BC for GLM 5, 5.1 (full indexers) without indexer_types metadata66    const bool is_pre_5_2 = hparams.n_ctx_train < 1048576;67    if (is_pre_5_2) {68        std::fill(hparams.is_indexer_full_impl.begin(), hparams.is_indexer_full_impl.end(), 1);69    } else {70        hparams.is_indexer_full_impl = GLM_5_2_DEFAULT_INDEXER_TYPES;71    }72    ml.get_key_or_arr(LLM_KV_ATTENTION_INDEXER_TYPES, hparams.is_indexer_full_impl, hparams.n_layer(), false);73 74    switch (hparams.n_layer()) {75        case 78: // GGUF with NextN/MTP metadata: n_layer() excludes the nextn layer76        case 79:77            type = LLM_TYPE_744B_A40B; break;78        default: type = LLM_TYPE_UNKNOWN;79    }80}81 82void llama_model_glm_dsa::load_arch_tensors(llama_model_loader & ml) {83    LLAMA_LOAD_LOCALS;84    const int64_t n_expert_shared = hparams.n_expert_shared;85 86    // MTP-only: the GGUF carries only the NextN/MTP block(s) (user split target/draft).87    const bool mtp_only = (hparams.n_layer_nextn > 0) && (ml.get_weight("blk.0.attn_norm.weight") == nullptr);88    // Trunk-only: the GGUF declares MTP layers in metadata but the actual MTP89    // tensors live in a separate file (or were stripped at conversion). Mark90    // MTP tensors NOT_REQUIRED so the trunk loads cleanly.91    const std::string mtp_probe = "blk." + std::to_string(n_layer) + ".nextn.eh_proj.weight";92    const bool trunk_only = (hparams.n_layer_nextn > 0) && (ml.get_weight(mtp_probe.c_str()) == nullptr);93    const int trunk_flags = mtp_only   ? TENSOR_NOT_REQUIRED : 0;94    int mtp_flags         = trunk_only ? TENSOR_NOT_REQUIRED : 0;95 96    if (!ml.load_mtp) {97        mtp_flags |= TENSOR_SKIP;98    }99 100    const bool is_mla = hparams.is_mla();101    if (!is_mla) {102        throw std::runtime_error("GLM_DSA architecture requires MLA");103    }104 105    // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA106    const int64_t n_embd_head_k_mla = hparams.n_embd_head_k_mla();107    const int64_t n_embd_head_v_mla = hparams.n_embd_head_v_mla();108 109    const int64_t n_embd_head_qk_rope = hparams.n_rot();110    const int64_t n_embd_head_qk_nope = n_embd_head_k_mla - n_embd_head_qk_rope;111 112    const int64_t q_lora_rank  = hparams.n_lora_q;113    const int64_t kv_lora_rank = hparams.n_lora_kv;114 115    const int64_t n_ff_exp        = hparams.n_ff_exp;116 117    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);118 119    // output120    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);121    // try to load output.weight, if not found, use token_embd (tied embeddings)122    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);123    if (!output) {124        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);125    }126 127    for (int i = 0; i < n_layer_all; ++i) {128        // NextN/MTP layers (i >= n_layer) are full decoder blocks used by the129        // LLM_GRAPH_TYPE_DECODER_MTP draft head; load them like qwen35moe/step35/hy_v3.130        const int flags = (i >= n_layer) ? mtp_flags : trunk_flags;131 132        auto & layer = layers[i];133 134        layer.attn_norm      = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, flags);135        layer.attn_q_a_norm  = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, flags);136        layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, flags);137 138        layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, flags);139        layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k_mla}, flags);140 141        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);142 143        // note: only old legacy GGUF files will have the unsplit wkv_b tensor in144        layer.wk_b = create_tensor(tn(LLM_TENSOR_ATTN_K_B, "weight", i), {n_embd_head_qk_nope, kv_lora_rank, n_head}, flags);145        layer.wv_b = create_tensor(tn(LLM_TENSOR_ATTN_V_B, "weight", i), {kv_lora_rank, n_embd_head_v_mla, n_head}, flags);146 147        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_embd_head_v_mla, n_embd}, flags);148 149        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, flags);150 151        // DSA indexer152        layer.indexer_k_norm   = create_tensor(tn(LLM_TENSOR_INDEXER_K_NORM,   "weight", i), {hparams.indexer_head_size}, flags | TENSOR_NOT_REQUIRED);153        layer.indexer_k_norm_b = create_tensor(tn(LLM_TENSOR_INDEXER_K_NORM,   "bias",   i), {hparams.indexer_head_size}, flags | TENSOR_NOT_REQUIRED);154        layer.indexer_proj     = create_tensor(tn(LLM_TENSOR_INDEXER_PROJ,     "weight", i), {n_embd, hparams.indexer_n_head}, flags | TENSOR_NOT_REQUIRED);155        layer.indexer_attn_k   = create_tensor(tn(LLM_TENSOR_INDEXER_ATTN_K,   "weight", i), {n_embd, hparams.indexer_head_size}, flags | TENSOR_NOT_REQUIRED);156        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 | TENSOR_NOT_REQUIRED);157        if (i < (int) hparams.n_layer_dense_lead) {158            layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, flags);159            layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, flags);160            layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, flags);161        } else {162            layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, flags);163            layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);164 165            if (n_expert == 0) {166                throw std::runtime_error("n_expert must be > 0");167            }168            if (n_expert_used == 0) {169                throw std::runtime_error("n_expert_used must be > 0");170            }171 172            // MoE branch173            layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {  n_embd, n_ff_exp, n_expert}, flags);174            layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp,   n_embd, n_expert}, flags);175            layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {  n_embd, n_ff_exp, n_expert}, flags);176 177            // Shared expert branch178            layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, flags);179            layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {        n_ff_exp * n_expert_shared, n_embd}, flags);180            layer.ffn_up_shexp   = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP,   "weight", i), {n_embd, n_ff_exp * n_expert_shared}, flags);181        }182 183        // NextN/MTP tensors - the NextN-specific wiring around the extra decoder block184        if (i >= n_layer) {185            layer.nextn.eh_proj          = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags);186            layer.nextn.enorm            = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags);187            layer.nextn.hnorm            = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags);188 189            // Optional tensors190            layer.nextn.embed_tokens     = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED);191            layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED);192            layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, flags | TENSOR_NOT_REQUIRED);193        }194    }195}196 197std::unique_ptr<llm_graph_context> llama_model_glm_dsa::build_arch_graph(const llm_graph_params & params) const {198    if (params.gtype == LLM_GRAPH_TYPE_DECODER_MTP) {199        return std::make_unique<graph_mtp>(*this, params);200    }201    return std::make_unique<graph>(*this, params);202}203 204llama_model_glm_dsa::graph::graph(const llama_model & model, const llm_graph_params & params) :205    llm_graph_context(params) {206    const bool is_mla = hparams.is_mla();207    GGML_ASSERT(is_mla);208 209    // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA210    const int64_t n_embd_head_k = hparams.n_embd_head_k_mla();211    const int64_t n_embd_head_v = hparams.n_embd_head_v_mla();212    GGML_UNUSED(n_embd_head_v);213 214    const int64_t n_embd_head_qk_rope = hparams.n_rot();215    const int64_t n_embd_head_qk_nope = n_embd_head_k - n_embd_head_qk_rope;216 217    const int64_t n_indexer_head = hparams.indexer_n_head;218    const int64_t n_embd_indexer_head = hparams.indexer_head_size;219    const int64_t n_embd_indexer_head_rope = hparams.n_rot();220    const int64_t n_embd_indexer_head_nope = n_embd_indexer_head - n_embd_indexer_head_rope;221    const uint32_t n_indexer_top_k = hparams.indexer_top_k;222 223    const uint32_t kv_lora_rank = hparams.n_lora_kv;224 225    // We have to pre-scale kq_scale and attn_factor to make the YaRN RoPE work correctly.226    // See https://github.com/ggml-org/llama.cpp/discussions/7416 for detailed explanation.227    // And also: https://github.com/ggml-org/llama.cpp/pull/17945 [TAG_DEEPSEEK2_YARN_LOG_MUL_FIX]228 229    // first cancel the adjustment from llama_hparams::yarn_attn_factor_adjust to get the original attn_factor230    GGML_ASSERT(ext_factor >= 0.0f);231    const float attn_factor_org = attn_factor * (1.0f + 0.1f * logf(1.0f / freq_scale));232 233    // use the original attn_factor to pre-scale the kq_scale234    const float mscale   = attn_factor_org * (1.0f + 0.1f * hparams.rope_yarn_log_mul * logf(1.0f / freq_scale));235    const float kq_scale = 1.0f * mscale * mscale / sqrtf(float(n_embd_head_k));236 237    ggml_tensor * cur;238    ggml_tensor * inpL;239 240    // {n_embd, n_tokens}241    inpL = build_inp_embd(model.tok_embd);242 243    // inp_pos - contains the positions244    ggml_tensor * inp_pos = build_inp_pos();245 246    llm_graph_input_attn_k_dsa * inp_attn_dsa = build_attn_inp_k_dsa();247 248    ggml_tensor * inp_out_ids = build_inp_out_ids();249 250    // Difference vs Deepseek 3.2: shared indexer layers reuse the top_k from the previous full indexer layers251    // See https://huggingface.co/zai-org/GLM-5.2/blob/main/config.json#L30252    ggml_tensor * prev_top_k = nullptr;253    for (int il = 0; il < n_layer; ++il) {254        ggml_tensor * inpSA = inpL;255 256        // norm257        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);258        cb(cur, "attn_norm", il);259 260        // self_attention261        {262            ggml_tensor * qr = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);263            cb(qr, "qr", il);264 265            qr = build_norm(qr, model.layers[il].attn_q_a_norm, nullptr, LLM_NORM_RMS, il);266            cb(qr, "qr", il);267 268            ggml_tensor * top_k = nullptr;269 270            // lightning indexer271            if (hparams.is_indexer_full(il)) {272                // "full" layer273                ggml_tensor * indexer_q = ggml_mul_mat(ctx0, model.layers[il].indexer_attn_q_b, qr);274                cb(indexer_q, "indexer_q", il);275 276                // split into {n_embd_indexer_head_rope, n_indexer_head, n_tokens}277                ggml_tensor * indexer_q_pe =278                    ggml_view_3d(ctx0, indexer_q, n_embd_indexer_head_rope, n_indexer_head, n_tokens,279                                 ggml_row_size(indexer_q->type, n_embd_indexer_head),280                                 ggml_row_size(indexer_q->type, n_embd_indexer_head) * n_indexer_head, 0);281                cb(indexer_q_pe, "indexer_q_pe", il);282 283                // and {n_embd_indexer_head_nope, n_indexer_head, n_tokens}284                ggml_tensor * indexer_q_nope =285                    ggml_view_3d(ctx0, indexer_q, n_embd_indexer_head_nope, n_indexer_head, n_tokens,286                                 ggml_row_size(indexer_q->type, n_embd_indexer_head),287                                 ggml_row_size(indexer_q->type, n_embd_indexer_head) * n_indexer_head,288                                 ggml_row_size(indexer_q->type, n_embd_indexer_head_nope));289                cb(indexer_q_nope, "indexer_q_nope", il);290 291                indexer_q_pe = ggml_rope_ext(ctx0, indexer_q_pe, inp_pos, nullptr, n_rot,292                                     LLAMA_ROPE_TYPE_NORM, n_ctx_orig, freq_base, freq_scale,293                                     ext_factor, attn_factor, beta_fast, beta_slow);294                cb(indexer_q_pe, "indexer_q_pe", il);295 296                // {n_embd_indexer_head_rope + n_embd_indexer_head_nope, n_head, n_tokens}297                indexer_q = ggml_concat(ctx0, indexer_q_pe, indexer_q_nope, 0);298                cb(indexer_q, "indexer_q", il);299 300                ggml_tensor * indexer_k = ggml_mul_mat(ctx0, model.layers[il].indexer_attn_k, cur);301                cb(indexer_k, "indexer_k", il);302 303                indexer_k = build_norm(indexer_k, model.layers[il].indexer_k_norm, model.layers[il].indexer_k_norm_b, LLM_NORM, il);304                cb(indexer_k, "indexer_k", il);305 306                // split into {n_embd_indexer_head_rope, 1, n_tokens}307                ggml_tensor * indexer_k_pe =308                    ggml_view_3d(ctx0, indexer_k, n_embd_indexer_head_rope, 1, n_tokens,309                                 ggml_row_size(indexer_k->type, n_embd_indexer_head),310                                 ggml_row_size(indexer_k->type, n_embd_indexer_head) * 1, 0);311                cb(indexer_k_pe, "indexer_k_pe", il);312 313                // and {n_embd_indexer_head_nope, 1, n_tokens}314                ggml_tensor * indexer_k_nope =315                    ggml_view_3d(ctx0, indexer_k, n_embd_indexer_head_nope, 1, n_tokens,316                                 ggml_row_size(indexer_k->type, n_embd_indexer_head),317                                 ggml_row_size(indexer_k->type, n_embd_indexer_head) * 1,318                                 ggml_row_size(indexer_k->type, n_embd_indexer_head_nope));319                cb(indexer_k_nope, "indexer_k_nope", il);320 321                indexer_k_pe = ggml_rope_ext(ctx0, indexer_k_pe, inp_pos, nullptr, n_rot,322                                     LLAMA_ROPE_TYPE_NORM, n_ctx_orig, freq_base, freq_scale,323                                     ext_factor, attn_factor, beta_fast, beta_slow);324                cb(indexer_k_pe, "indexer_k_pe", il);325 326                // {n_embd_indexer_head_rope + n_embd_indexer_head_nope, 1, n_tokens}327                indexer_k = ggml_concat(ctx0, indexer_k_pe, indexer_k_nope, 0);328                cb(indexer_k, "indexer_k", il);329 330                // perform Hadamard transform on indexer q and k331                indexer_q = ggml_mul_mat(ctx0, inp_attn_dsa->self_k_rot_lid, indexer_q);332                cb(indexer_q, "indexer_q", il);333                indexer_k = ggml_mul_mat(ctx0, inp_attn_dsa->self_k_rot_lid, indexer_k);334                cb(indexer_k, "indexer_k", il);335 336                // store indexer keys to KV cache337                const auto * mctx_lid = inp_attn_dsa->mctx->get_lid();338                const auto & k_idxs_lid = inp_attn_dsa->get_k_idxs_lid();339                ggml_build_forward_expand(gf, mctx_lid->cpy_k(ctx0, indexer_k, k_idxs_lid, il));340 341                // prepare indexer weights342                ggml_tensor * indexer_weights = ggml_mul_mat(ctx0, model.layers[il].indexer_proj, cur);343                cb(indexer_weights, "indexer_weights", il);344 345                // get cached indexer keys346                indexer_k = mctx_lid->get_k(ctx0, il);347 348                // split the batch into streams if needed349                const auto n_stream = indexer_k->ne[3];350                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);351                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);352 353                // pre-scale weights to avoid scaling operations on huge indexer_score tensor354                indexer_weights = ggml_scale(ctx0, indexer_weights, 1.0f / sqrtf(float(n_embd_indexer_head * n_indexer_head)));355                cb(indexer_weights, "indexer_weights", il);356 357                ggml_tensor * indexer_score = nullptr;358                if (cparams.fused_lid) {359                    indexer_score = ggml_lightning_indexer(ctx0, indexer_q, indexer_k, indexer_weights, inp_attn_dsa->get_kq_mask_lid());360                    cb(indexer_score, "indexer_score", il);361                    res->add_fused_node({LLM_FUSED_OP_LIGHTNING_INDEXER, indexer_score, il});362                } else {363                    // calculate indexer kq364                    indexer_q = ggml_permute(ctx0, indexer_q, 0, 2, 1, 3);365                    cb(indexer_q, "indexer_q", il);366                    indexer_k = ggml_permute(ctx0, indexer_k, 0, 2, 1, 3);367                    cb(indexer_k, "indexer_k", il);368 369                    ggml_tensor * indexer_kq = ggml_mul_mat(ctx0, indexer_k, indexer_q);370                    cb(indexer_kq, "indexer_kq", il);371 372                    // ReLU requires contiguous tensors373                    indexer_kq = ggml_cont(ctx0, ggml_permute(ctx0, indexer_kq, 2, 1, 0, 3));374                    cb(indexer_kq, "indexer_kq", il);375 376                    // apply ReLU377                    indexer_score = ggml_relu(ctx0, indexer_kq);378                    cb(indexer_score, "indexer_score", il);379 380                    // multiply scores by indexer weights381                    indexer_score = ggml_mul(ctx0, indexer_score, indexer_weights);382                    cb(indexer_score, "indexer_score", il);383 384                    // sum by q n_indexer_head dimension385                    indexer_score = ggml_sum_rows(ctx0, indexer_score);386                    cb(indexer_score, "indexer_score", il);387 388                    // permute result to match KQ mask389                    indexer_score = ggml_cont(ctx0, ggml_permute(ctx0, indexer_score, 2, 1, 0, 3));390                    cb(indexer_score, "indexer_score", il);391 392                    // mask indexer scores393                    ggml_tensor * indexer_kq_mask = inp_attn_dsa->get_kq_mask_lid();394                    indexer_score = ggml_add(ctx0, indexer_score, indexer_kq_mask);395                    cb(indexer_score, "indexer_score", il);396                }397 398                // get indices of top k indexer scores399                uint32_t n_top_k = indexer_score->ne[0] < n_indexer_top_k ? indexer_score->ne[0] : n_indexer_top_k;400                top_k = ggml_cont(ctx0, ggml_top_k(ctx0, indexer_score, n_top_k));401                prev_top_k = top_k;402                cb(top_k, "top_k", il);403            } else {404                // "shared" indexer layer - reuse top-k from a previous full layer405                GGML_ASSERT(prev_top_k != nullptr && "shared indexer layer must follow a previous full indexer layer");406                top_k = prev_top_k;407                cb(top_k, "top_k", il);408            }409 410            ggml_tensor * q = ggml_mul_mat(ctx0, model.layers[il].wq_b, qr);411            cb(q, "q", il);412 413            // split into {n_embd_head_qk_nope, n_head, n_tokens}414            ggml_tensor * q_nope =415                ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens, ggml_row_size(q->type, n_embd_head_k),416                             ggml_row_size(q->type, n_embd_head_k) * n_head, 0);417            cb(q_nope, "q_nope", il);418 419            // and {n_embd_head_qk_rope, n_head, n_tokens}420            ggml_tensor * q_pe = ggml_view_3d(421                ctx0, q, n_embd_head_qk_rope, n_head, n_tokens, ggml_row_size(q->type, n_embd_head_k),422                ggml_row_size(q->type, n_embd_head_k) * n_head, ggml_row_size(q->type, n_embd_head_qk_nope));423            cb(q_pe, "q_pe", il);424 425            ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);426            cb(kv_cmpr_pe, "kv_cmpr_pe", il);427 428            // split into {kv_lora_rank, n_tokens}429            ggml_tensor * kv_cmpr =430                ggml_view_2d(ctx0, kv_cmpr_pe, kv_lora_rank, n_tokens,431                             ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope), 0);432            cb(kv_cmpr, "kv_cmpr", il);433 434            // and {n_embd_head_qk_rope, 1, n_tokens}435            ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_cmpr_pe, n_embd_head_qk_rope, 1, n_tokens,436                                              ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),437                                              ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),438                                              ggml_row_size(kv_cmpr_pe->type, kv_lora_rank));439            cb(k_pe, "k_pe", il);440 441            q_pe = ggml_rope_ext(ctx0, q_pe, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,442                                 ext_factor, attn_factor, beta_fast, beta_slow);443            cb(q_pe, "q_pe", il);444 445            k_pe = ggml_rope_ext(ctx0, k_pe, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,446                                 ext_factor, attn_factor, beta_fast, beta_slow);447            cb(k_pe, "k_pe", il);448 449            kv_cmpr = build_norm(kv_cmpr, model.layers[il].attn_kv_a_norm, nullptr, LLM_NORM_RMS, il);450            cb(kv_cmpr, "kv_cmpr", il);451 452            // MLA attention453            {454                // {n_embd_head_qk_nope, n_tokens, n_head}455                q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);456                cb(q_nope, "q_nope_perm", il);457 458                // {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head}459                ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, model.layers[il].wk_b, q_nope);460                cb(q_nope_absorbed, "q_nope_absorbed", il);461 462                // {kv_lora_rank, n_head, n_tokens}463                q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3);464                cb(q_nope_absorbed, "q_nope_absorbed_perm", il);465 466                // {n_embd_head_qk_rope + kv_lora_rank, n_head, n_tokens}467                // note: rope must go first for in-place context shifting in build_rope_shift()468                ggml_tensor * Qcur = ggml_concat(ctx0, q_nope_absorbed, q_pe, 0);469                cb(Qcur, "Qcur", il);470 471                kv_cmpr = ggml_reshape_3d(ctx0, kv_cmpr, kv_lora_rank, 1, n_tokens);472                cb(kv_cmpr, "kv_cmpr_reshape", il);473 474                // {n_embd_head_qk_rope + kv_lora_rank, 1, n_tokens}475                ggml_tensor * Kcur = ggml_concat(ctx0, kv_cmpr, k_pe, 0);476                cb(Kcur, "Kcur", il);477 478                // {kv_lora_rank, 1, n_tokens}479                ggml_tensor * Vcur = kv_cmpr;480                cb(Vcur, "Vcur", il);481 482                // note: MLA with the absorption optimization converts into MQA (ie: GQA with 1 group)483                cur = build_attn(inp_attn_dsa,484                        model.layers[il].wo, NULL, model.layers[il].wo_s,485                        Qcur, Kcur, Vcur, nullptr, nullptr, model.layers[il].wv_b, top_k, kq_scale, il);486            }487        }488        // when unmasked nextn embeddings are requested, t_h_nextn must keep all rows,489        // so the early output masking has to be skipped (it is applied after the final norm instead)490        if (il == n_layer - 1 && inp_out_ids && (!cparams.embeddings_nextn || cparams.embeddings_nextn_masked)) {491            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);492            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);493        }494        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);495        cb(ffn_inp, "ffn_inp", il);496 497        cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);498        cb(cur, "ffn_norm", il);499 500        if ((uint32_t) il < hparams.n_layer_dense_lead) {501            cur = build_ffn(cur,502                model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s,503                model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s,504                model.layers[il].ffn_down, NULL, model.layers[il].ffn_down_s,505                NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);506            cb(cur, "ffn_out", il);507        } else {508            // MoE branch509            ggml_tensor * moe_out = build_moe_ffn(cur,510                model.layers[il].ffn_gate_inp,511                model.layers[il].ffn_up_exps,512                model.layers[il].ffn_gate_exps,513                model.layers[il].ffn_down_exps,514                model.layers[il].ffn_exp_probs_b,515                n_expert, n_expert_used,516                LLM_FFN_SILU, hparams.expert_weights_norm,517                hparams.expert_weights_scale,518                (llama_expert_gating_func_type) hparams.expert_gating_func,519                il,520                nullptr,521                model.layers[il].ffn_gate_up_exps,522                model.layers[il].ffn_up_exps_s,523                model.layers[il].ffn_gate_exps_s,524                model.layers[il].ffn_down_exps_s);525            cb(moe_out, "ffn_moe_out", il);526 527            // FFN shared expert528            {529                ggml_tensor * ffn_shexp =530                    build_ffn(cur,531                        model.layers[il].ffn_up_shexp, NULL, model.layers[il].ffn_up_shexp_s,532                        model.layers[il].ffn_gate_shexp, NULL, model.layers[il].ffn_gate_shexp_s,533                        model.layers[il].ffn_down_shexp, NULL, model.layers[il].ffn_down_shexp_s,534                        NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);535                cb(ffn_shexp, "ffn_shexp", il);536 537                cur = ggml_add(ctx0, moe_out, ffn_shexp);538                cb(cur, "ffn_out", il);539            }540        }541        cur = ggml_add(ctx0, cur, ffn_inp);542 543        cur = build_cvec(cur, il);544        cb(cur, "l_out", il);545 546        // input for next layer547        inpL = cur;548    }549    cur = inpL;550 551    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);552 553    // post-norm hidden state feeds the NextN/MTP draft head554    cb(cur, "h_nextn", -1);555    res->t_h_nextn = cur;556 557    if (cparams.embeddings_nextn && !cparams.embeddings_nextn_masked && inp_out_ids) {558        cur = ggml_get_rows(ctx0, cur, inp_out_ids);559    }560 561    cb(cur, "result_norm", -1);562    res->t_embd = cur;563 564    // lm_head565    cur = ggml_mul_mat(ctx0, model.output, cur);566 567    cb(cur, "result_output", -1);568    res->t_logits = cur;569 570    ggml_build_forward_expand(gf, cur);571}572 573// LLM_GRAPH_TYPE_DECODER_MTP draft head for GLM-5.2 (GLM_DSA).574// Semantics mirror the deepseek-family NextN/MTP layer:575//   enorm(embed) + hnorm(prev_hidden) -> concat(e, h) -> eh_proj ->576//   full glm_dsa decoder block (dense MLA attention + sigmoid-gated MoE FFN577//   with shared expert, exactly as the trunk deepseek2 graph builds it) ->578//   shared_head_norm (fallback output_norm) -> shared LM head.579// The DSA indexer is not used at runtime (same as the trunk graph).580llama_model_glm_dsa::graph_mtp::graph_mtp(const llama_model & model, const llm_graph_params & params)581    : llm_graph_context(params) {582    GGML_ASSERT(hparams.n_layer_nextn > 0 && "GLM_DSA MTP requires n_layer_nextn > 0");583    GGML_ASSERT(hparams.n_layer_nextn == 1 && "GLM_DSA MTP currently only supports a single MTP block");584    GGML_ASSERT(hparams.is_mla() && "GLM_DSA MTP requires MLA");585 586    const int il = hparams.n_layer() + cparams.nextn_layer_offset;587    GGML_ASSERT(cparams.nextn_layer_offset >= 0 &&588                cparams.nextn_layer_offset < (int) hparams.n_layer_nextn &&589                "nextn_layer_offset out of range [0, n_layer_nextn)");590    const auto & layer = model.layers[il];591 592    GGML_ASSERT(layer.nextn.eh_proj && "MTP block missing nextn.eh_proj");593    GGML_ASSERT(layer.nextn.enorm   && "MTP block missing nextn.enorm");594    GGML_ASSERT(layer.nextn.hnorm   && "MTP block missing nextn.hnorm");595    GGML_ASSERT(layer.ffn_gate_inp  && "MTP block missing ffn_gate_inp");596 597    // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA598    const int64_t n_embd_head_k = hparams.n_embd_head_k_mla();599 600    const int64_t n_embd_head_qk_rope = hparams.n_rot();601    const int64_t n_embd_head_qk_nope = n_embd_head_k - n_embd_head_qk_rope;602 603    const uint32_t kv_lora_rank = hparams.n_lora_kv;604 605    // We have to pre-scale kq_scale and attn_factor to make the YaRN RoPE work correctly.606    // See the deepseek2 trunk graph for the detailed explanation - this must match it EXACTLY.607    GGML_ASSERT(ext_factor >= 0.0f);608    const float attn_factor_org = attn_factor * (1.0f + 0.1f * logf(1.0f / freq_scale));609 610    const float mscale   = attn_factor_org * (1.0f + 0.1f * hparams.rope_yarn_log_mul * logf(1.0f / freq_scale));611    const float kq_scale = 1.0f * mscale * mscale / sqrtf(float(n_embd_head_k));612 613    // TODO: extract in a common llm_graph_context::build_inp_embd_h()614    auto inp = std::make_unique<llm_graph_input_embd_h>(hparams.n_embd);615 616    inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens);617    ggml_set_input(inp->tokens);618 619    inp->embd = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, hparams.n_embd_inp(), n_tokens);620    ggml_set_input(inp->embd);621 622    ggml_tensor * tok_embd;623    if (ubatch.token) {624        ggml_tensor * tok_embd_w = layer.nextn.embed_tokens ? layer.nextn.embed_tokens : model.tok_embd;625 626        tok_embd = ggml_get_rows(ctx0, tok_embd_w, inp->tokens);627    } else {628        tok_embd = inp->embd;629    }630    cb(tok_embd, "mtp_tok_embd", il);631 632    inp->h = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, hparams.n_embd, n_tokens);633    ggml_set_input(inp->h);634    ggml_set_name(inp->h, "mtp_h_input");635 636    ggml_tensor * h_embd = inp->h;637 638    res->add_input(std::move(inp));639 640    ggml_tensor * inp_pos     = build_inp_pos();641    ggml_tensor * inp_out_ids = build_inp_out_ids();642 643    // MLA with the absorption optimization uses a K-only cache (V is a view of K)644    auto * inp_attn = build_attn_inp_k();645 646    ggml_tensor * h_norm = build_norm(h_embd, layer.nextn.hnorm, nullptr, LLM_NORM_RMS, il);647    cb(h_norm, "mtp_hnorm", il);648 649    ggml_tensor * e_norm = build_norm(tok_embd, layer.nextn.enorm, nullptr, LLM_NORM_RMS, il);650    cb(e_norm, "mtp_enorm", il);651 652    ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, /*dim=*/ 0);653    cb(concat, "mtp_concat", il);654 655    ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s);656    cb(cur, "mtp_eh_proj", il);657 658    ggml_tensor * inpSA = cur;659 660    cur = build_norm(cur, layer.attn_norm, nullptr, LLM_NORM_RMS, il);661    cb(cur, "mtp_attn_norm", il);662 663    // self-attention: dense MLA, same construction as the deepseek2 trunk graph664    {665        ggml_tensor * q = ggml_mul_mat(ctx0, layer.wq_a, cur);666        cb(q, "mtp_q", il);667 668        q = build_norm(q, layer.attn_q_a_norm, nullptr, LLM_NORM_RMS, il);669        cb(q, "mtp_q", il);670 671        q = ggml_mul_mat(ctx0, layer.wq_b, q);672        cb(q, "mtp_q", il);673 674        // split into {n_embd_head_qk_nope, n_head, n_tokens}675        ggml_tensor * q_nope =676            ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens, ggml_row_size(q->type, n_embd_head_k),677                         ggml_row_size(q->type, n_embd_head_k) * n_head, 0);678        cb(q_nope, "mtp_q_nope", il);679 680        // and {n_embd_head_qk_rope, n_head, n_tokens}681        ggml_tensor * q_pe = ggml_view_3d(682            ctx0, q, n_embd_head_qk_rope, n_head, n_tokens, ggml_row_size(q->type, n_embd_head_k),683            ggml_row_size(q->type, n_embd_head_k) * n_head, ggml_row_size(q->type, n_embd_head_qk_nope));684        cb(q_pe, "mtp_q_pe", il);685 686        ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, layer.wkv_a_mqa, cur);687        cb(kv_cmpr_pe, "mtp_kv_cmpr_pe", il);688 689        // split into {kv_lora_rank, n_tokens}690        ggml_tensor * kv_cmpr =691            ggml_view_2d(ctx0, kv_cmpr_pe, kv_lora_rank, n_tokens,692                         ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope), 0);693        cb(kv_cmpr, "mtp_kv_cmpr", il);694 695        // and {n_embd_head_qk_rope, 1, n_tokens}696        ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_cmpr_pe, n_embd_head_qk_rope, 1, n_tokens,697                                          ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),698                                          ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),699                                          ggml_row_size(kv_cmpr_pe->type, kv_lora_rank));700        cb(k_pe, "mtp_k_pe", il);701 702        q_pe = ggml_rope_ext(ctx0, q_pe, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,703                             ext_factor, attn_factor, beta_fast, beta_slow);704        cb(q_pe, "mtp_q_pe", il);705 706        k_pe = ggml_rope_ext(ctx0, k_pe, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,707                             ext_factor, attn_factor, beta_fast, beta_slow);708        cb(k_pe, "mtp_k_pe", il);709 710        kv_cmpr = build_norm(kv_cmpr, layer.attn_kv_a_norm, nullptr, LLM_NORM_RMS, il);711        cb(kv_cmpr, "mtp_kv_cmpr", il);712 713        // {n_embd_head_qk_nope, n_tokens, n_head}714        q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);715        cb(q_nope, "mtp_q_nope_perm", il);716 717        // {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head}718        ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, layer.wk_b, q_nope);719        cb(q_nope_absorbed, "mtp_q_nope_absorbed", il);720 721        // {kv_lora_rank, n_head, n_tokens}722        q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3);723        cb(q_nope_absorbed, "mtp_q_nope_absorbed_perm", il);724 725        // {n_embd_head_qk_rope + kv_lora_rank, n_head, n_tokens}726        // note: rope must go first for in-place context shifting in build_rope_shift()727        ggml_tensor * Qcur = ggml_concat(ctx0, q_nope_absorbed, q_pe, 0);728        cb(Qcur, "mtp_Qcur", il);729 730        kv_cmpr = ggml_reshape_3d(ctx0, kv_cmpr, kv_lora_rank, 1, n_tokens);731        cb(kv_cmpr, "mtp_kv_cmpr_reshape", il);732 733        // {n_embd_head_qk_rope + kv_lora_rank, 1, n_tokens}734        ggml_tensor * Kcur = ggml_concat(ctx0, kv_cmpr, k_pe, 0);735        cb(Kcur, "mtp_Kcur", il);736 737        // {kv_lora_rank, 1, n_tokens}738        ggml_tensor * Vcur = kv_cmpr;739        cb(Vcur, "mtp_Vcur", il);740 741        // note: MLA with the absorption optimization converts into MQA (ie: GQA with 1 group)742        cur = build_attn(inp_attn,743                layer.wo, NULL, layer.wo_s,744                Qcur, Kcur, Vcur, nullptr, nullptr, layer.wv_b, kq_scale, il);745        cb(cur, "mtp_attn_out", il);746    }747 748    ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);749    cb(ffn_inp, "mtp_ffn_inp", il);750 751    cur = build_norm(ffn_inp, layer.ffn_norm, NULL, LLM_NORM_RMS, il);752    cb(cur, "mtp_ffn_norm", il);753 754    // MoE FFN with shared expert - same construction as the deepseek2 trunk graph755    ggml_tensor * moe_out = build_moe_ffn(cur,756        layer.ffn_gate_inp,757        layer.ffn_up_exps,758        layer.ffn_gate_exps,759        layer.ffn_down_exps,760        layer.ffn_exp_probs_b,761        n_expert, n_expert_used,762        LLM_FFN_SILU, hparams.expert_weights_norm,763        hparams.expert_weights_scale,764        (llama_expert_gating_func_type) hparams.expert_gating_func,765        il,766        nullptr,767        layer.ffn_gate_up_exps,768        layer.ffn_up_exps_s,769        layer.ffn_gate_exps_s,770        layer.ffn_down_exps_s);771    cb(moe_out, "mtp_ffn_moe_out", il);772 773    // FFN shared expert774    ggml_tensor * ffn_shexp =775        build_ffn(cur,776            layer.ffn_up_shexp, NULL, layer.ffn_up_shexp_s,777            layer.ffn_gate_shexp, NULL, layer.ffn_gate_shexp_s,778            layer.ffn_down_shexp, NULL, layer.ffn_down_shexp_s,779            NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);780    cb(ffn_shexp, "mtp_ffn_shexp", il);781 782    cur = ggml_add(ctx0, moe_out, ffn_shexp);783    cb(cur, "mtp_ffn_out", il);784 785    cur = ggml_add(ctx0, cur, ffn_inp);786    cb(cur, "mtp_post_ffn", il);787 788    // shared_head_norm applied after the decoder block, before the shared LM head.789    // The post-norm hidden state seeds the next MTP step.790    ggml_tensor * head_norm_w = layer.nextn.shared_head_norm791            ? layer.nextn.shared_head_norm792            : model.output_norm;793    GGML_ASSERT(head_norm_w && "GLM_DSA MTP: missing both nextn.shared_head_norm and output_norm");794    cur = build_norm(cur, head_norm_w, nullptr, LLM_NORM_RMS, -1);795 796    cb(cur, "h_nextn", -1);797    res->t_h_nextn = cur;798 799    cur = ggml_get_rows(ctx0, cur, inp_out_ids);800    cb(cur, "mtp_shared_head_norm", -1);801 802    ggml_tensor * head_w = layer.nextn.shared_head_head ? layer.nextn.shared_head_head : model.output;803    ggml_tensor * head_s = layer.nextn.shared_head_head ? layer.nextn.shared_head_head_s : model.output_s;804    GGML_ASSERT(head_w && "GLM_DSA MTP: missing LM head (nextn.shared_head_head or model.output)");805    cur = build_lora_mm(head_w, cur, head_s);806    cb(cur, "result_output", -1);807 808    res->t_logits = cur;809    ggml_build_forward_expand(gf, cur);810}811 
Brunobkr/llama.cpp_AlgMor24_github · Team Ai