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
0likes3kdownloads
cohere2moe.cpp448 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_cohere2moe::load_arch_hparams(llama_model_loader & ml) {4    const bool found_norm     = ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS,     hparams.f_norm_eps,     false);5    const bool found_norm_rms = ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps, false);6    if (!found_norm && !found_norm_rms) {7        throw std::runtime_error("missing Cohere2 MoE norm epsilon");8    }9    if (!found_norm_rms) {10        hparams.f_norm_rms_eps = 0.0f;11    }12 13    ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW,    hparams.n_swa);14    ml.get_key(LLM_KV_LOGIT_SCALE,                 hparams.f_logit_scale);15    ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT,   hparams.n_layer_dense_lead);16    ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH,  hparams.n_ff_exp);17    ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);18    ml.get_key(LLM_KV_EXPERT_SHARED_COUNT,         hparams.n_expert_shared, false);19    ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM,         hparams.expert_weights_norm, false);20    ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE,        hparams.expert_weights_scale, false);21    ml.get_key(LLM_KV_EXPERT_GATING_FUNC,          hparams.expert_gating_func, false);22 23    ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS,        hparams.n_layer_nextn, false);24    GGML_ASSERT(hparams.n_layer_nextn < hparams.n_layer_all && "n_layer_nextn must be < n_layer");25 26    if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) {27        hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID;28    }29 30    hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;31    uint32_t swa_period = 4;32    if (ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false)) {33        hparams.set_swa_pattern(swa_period, true);34    } else {35        ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, hparams.is_swa_impl, hparams.n_layer());36    }37 38    hparams.rope_freq_base_train_swa  = hparams.rope_freq_base_train;39    hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;40    ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);41 42    switch (hparams.n_layer()) {43        case 49: type = LLM_TYPE_30B_A3B; break;44        default: type = LLM_TYPE_UNKNOWN;45    }46}47 48void llama_model_cohere2moe::load_arch_tensors(llama_model_loader & ml) {49    LLAMA_LOAD_LOCALS;50 51    const bool mtp_only = (hparams.n_layer_nextn > 0) && (ml.get_weight("blk.0.attn_norm.weight") == nullptr);52    // Trunk-only: the GGUF declares MTP layers in metadata but the actual MTP53    // tensors live in a separate file. Mark MTP tensors NOT_REQUIRED so the54    // trunk loads cleanly.55    const std::string mtp_probe = "blk." + std::to_string(n_layer) + ".nextn.eh_proj.weight";56    const bool trunk_only = (hparams.n_layer_nextn > 0) && (ml.get_weight(mtp_probe.c_str()) == nullptr);57    const int trunk_flags = mtp_only  ? TENSOR_NOT_REQUIRED : 0;58    int mtp_flags         = trunk_only ? TENSOR_NOT_REQUIRED : 0;59 60    if (!ml.load_mtp) {61        mtp_flags |= TENSOR_SKIP;62    }63 64    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);65 66    // output67    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);68    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED);69 70    // if output is NULL, init from the input tok embed71    if (output == NULL) {72        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);73    }74 75    if (n_expert == 0) {76        throw std::runtime_error("n_expert must be > 0 for Cohere2Moe");77    }78    if (n_expert_used == 0) {79        throw std::runtime_error("n_expert_used must be > 0 for Cohere2Moe");80    }81 82    auto load_block_trunk = [&](int i, int flags) {83        auto & layer = layers[i];84 85        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, flags);86 87        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_gqa, n_embd_gqa, flags);88        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, flags);89 90        if (static_cast<uint32_t>(i) < hparams.n_layer_dense_lead) {91            layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, flags);92            layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, flags);93            layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), { n_embd, n_ff }, flags);94        } else {95            const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff;96 97            layer.ffn_gate_inp  = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP,  "weight", i), { n_embd, n_expert }, flags);98            layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, flags);99            create_tensor_gate_up_exps(layer, i, n_embd, n_ff_exp, n_expert, flags);100 101            if (hparams.n_expert_shared > 0) {102                const int64_t n_ff_shexp = hparams.n_ff_shexp ? hparams.n_ff_shexp : n_ff_exp * hparams.n_expert_shared;103                layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags);104                layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_shexp, n_embd }, flags);105                layer.ffn_up_shexp   = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP,   "weight", i), { n_embd, n_ff_shexp }, flags);106            }107        }108    };109 110    auto load_block_mtp = [&](int i, int flags) {111        auto & layer = layers[i];112 113        // MTP block looks like a full-attention Cohere2 MoE decoder block.114        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, flags);115 116        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_gqa, n_embd_gqa, flags);117        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, flags);118 119        const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff;120 121        // Routed experts122        layer.ffn_gate_inp  = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP,  "weight", i), { n_embd, n_expert }, flags);123        layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, flags);124        create_tensor_gate_up_exps(layer, i, n_embd, n_ff_exp, n_expert, flags);125 126        if (hparams.n_expert_shared > 0) {127            const int64_t n_ff_shexp = hparams.n_ff_shexp ? hparams.n_ff_shexp : n_ff_exp * hparams.n_expert_shared;128 129            // Shared experts130            layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags);131            layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_shexp, n_embd }, flags);132            layer.ffn_up_shexp   = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP,   "weight", i), { n_embd, n_ff_shexp }, flags);133        }134 135        // NextN-specific tensors that define the MTP block.136        layer.nextn.eh_proj          = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ,          "weight", i), { 2 * n_embd, n_embd }, flags);137        layer.nextn.enorm            = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM,            "weight", i), { n_embd },              flags);138        layer.nextn.hnorm            = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM,            "weight", i), { n_embd },              flags);139        layer.nextn.embed_tokens     = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS,     "weight", i), { n_embd, n_vocab },     TENSOR_NOT_REQUIRED);140        layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab },     TENSOR_NOT_REQUIRED);141        layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd },              TENSOR_NOT_REQUIRED);142    };143 144    for (int i = 0; i < n_layer; ++i) {145        load_block_trunk(i, trunk_flags);146    }147    // MTP/NextN layers are loaded as extra decoder blocks.148    for (int i = n_layer; i < n_layer_all; ++i) {149        load_block_mtp(i, mtp_flags);150    }151}152 153std::unique_ptr<llm_graph_context> llama_model_cohere2moe::build_arch_graph(const llm_graph_params & params) const {154    if (params.gtype == LLM_GRAPH_TYPE_DECODER_MTP) {155        return std::make_unique<graph_mtp>(*this, params);156    }157    return std::make_unique<graph>(*this, params);158}159 160llama_model_cohere2moe::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {161    const int64_t n_embd_head = hparams.n_embd_head_v();162 163    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());164    GGML_ASSERT(n_embd_head == n_rot);165 166    const llm_norm_type cohere2moe_norm_type = hparams.f_norm_rms_eps == 0.0f ? LLM_NORM : LLM_NORM_RMS;167    const float f_logit_scale = hparams.f_logit_scale;168    ggml_tensor * cur;169    ggml_tensor * inpL = build_inp_embd(model.tok_embd);170    ggml_tensor * inp_pos = build_inp_pos();171 172    auto * inp_attn = build_attn_inp_kv_iswa();173    ggml_tensor * inp_out_ids = build_inp_out_ids();174 175    // MTP/NextN layers are loaded as extra decoder blocks but not executed in the main pass.176    for (int il = 0; il < n_layer; ++il) {177        const bool is_swa = hparams.is_swa(il);178        // Dense-prefix full-attention layers use RoPE; later layers follow the SWA pattern.179        const bool force_rope = static_cast<uint32_t>(il) < hparams.n_layer_dense_lead;180 181        cur = build_norm(inpL, model.layers[il].attn_norm, nullptr, cohere2moe_norm_type, il);182        cb(cur, "attn_norm", il);183 184        ggml_tensor * ffn_inp = cur;185 186        {187            const auto & layer = model.layers[il];188 189            auto [Qcur, Kcur, Vcur] = build_qkv(layer, cur,190                    n_embd_head, n_head, n_head_kv, il);191 192            if (is_swa || force_rope) {193                ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);194 195                Qcur = ggml_rope_ext(196                        ctx0, Qcur, inp_pos, rope_factors,197                        n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,198                        ext_factor, attn_factor, beta_fast, beta_slow);199 200                Kcur = ggml_rope_ext(201                        ctx0, Kcur, inp_pos, rope_factors,202                        n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,203                        ext_factor, attn_factor, beta_fast, beta_slow);204            }205 206            cb(Qcur, "Qcur", il);207            cb(Kcur, "Kcur", il);208            cb(Vcur, "Vcur", il);209 210            cur = build_attn(inp_attn,211                    layer.wo, layer.wo_b, layer.wo_s,212                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr,213                    1.0f / sqrtf(float(n_embd_head)), il);214        }215 216        if (il == n_layer - 1 && inp_out_ids && cparams.embeddings_nextn_masked) {217            cur     = ggml_get_rows(ctx0, cur, inp_out_ids);218            inpL    = ggml_get_rows(ctx0, inpL, inp_out_ids);219            ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);220        }221 222        ggml_tensor * attn_out = cur;223 224        const auto & layer = model.layers[il];225 226        if (layer.ffn_gate_inp == nullptr) {227            cur = build_ffn(ffn_inp,228                    layer.ffn_up,   nullptr, layer.ffn_up_s,229                    layer.ffn_gate, nullptr, layer.ffn_gate_s,230                    layer.ffn_down, nullptr, layer.ffn_down_s,231                    nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);232            cb(cur, "ffn_out", il);233        } else {234            cur = build_moe_ffn(ffn_inp,235                    layer.ffn_gate_inp,236                    layer.ffn_up_exps,237                    layer.ffn_gate_exps,238                    layer.ffn_down_exps,239                    nullptr,240                    n_expert, n_expert_used,241                    LLM_FFN_SILU, hparams.expert_weights_norm,242                    hparams.expert_weights_scale,243                    (llama_expert_gating_func_type) hparams.expert_gating_func,244                    il,245                    nullptr, layer.ffn_gate_up_exps,246                    layer.ffn_up_exps_s,247                    layer.ffn_gate_exps_s,248                    layer.ffn_down_exps_s);249            cb(cur, "ffn_moe_out", il);250 251            if (layer.ffn_up_shexp) {252                ggml_tensor * ffn_shexp = build_ffn(ffn_inp,253                        layer.ffn_up_shexp,   nullptr, layer.ffn_up_shexp_s,254                        layer.ffn_gate_shexp, nullptr, layer.ffn_gate_shexp_s,255                        layer.ffn_down_shexp, nullptr, layer.ffn_down_shexp_s,256                        nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);257                cb(ffn_shexp, "ffn_shexp", il);258 259                cur = ggml_add(ctx0, cur, ffn_shexp);260                cur = ggml_scale(ctx0, cur, 0.5f);261                cb(cur, "ffn_out", il);262            }263        }264 265        cur = ggml_add(ctx0, cur, inpL);266        cur = ggml_add(ctx0, cur, attn_out);267 268        cur = build_cvec(cur, il);269        cb(cur, "l_out", il);270 271        inpL = cur;272    }273 274    cur = inpL;275    cur = build_norm(cur, model.output_norm, nullptr, cohere2moe_norm_type, -1);276 277    cb(cur, "h_nextn", -1);278    res->t_h_nextn = cur;279 280    if (!cparams.embeddings_nextn_masked && inp_out_ids) {281        cur = ggml_get_rows(ctx0, cur, inp_out_ids);282    }283 284    cb(cur, "result_norm", -1);285    res->t_embd = cur;286 287    cur = build_lora_mm(model.output, cur);288 289    if (f_logit_scale) {290        cur = ggml_scale(ctx0, cur, f_logit_scale);291    }292 293    cb(cur, "result_output", -1);294    res->t_logits = cur;295 296    ggml_build_forward_expand(gf, cur);297}298 299llama_model_cohere2moe::graph_mtp::graph_mtp(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {300    GGML_ASSERT(hparams.n_layer_nextn > 0 && "COHERE2MOE MTP requires n_layer_nextn > 0");301    GGML_ASSERT(hparams.n_layer_nextn == 1 && "COHERE2MOE MTP currently only supports a single MTP block");302 303    const int64_t n_embd_head = hparams.n_embd_head_v();304    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());305    GGML_ASSERT(n_embd_head == n_rot);306 307    const int il = hparams.n_layer();308    const auto & layer = model.layers[il];309    GGML_ASSERT(layer.nextn.eh_proj && "MTP block missing nextn.eh_proj");310    GGML_ASSERT(layer.nextn.enorm   && "MTP block missing nextn.enorm");311    GGML_ASSERT(layer.nextn.hnorm   && "MTP block missing nextn.hnorm");312    GGML_ASSERT(layer.ffn_gate_inp  && "MTP block missing ffn_gate_inp");313 314    const llm_norm_type cohere2moe_norm_type = hparams.f_norm_rms_eps == 0.0f ? LLM_NORM : LLM_NORM_RMS;315 316    // TODO: extract in a common llm_graph_context::build_inp_embd_h()317    auto inp = std::make_unique<llm_graph_input_embd_h>(hparams.n_embd);318 319    inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens);320    ggml_set_input(inp->tokens);321 322    inp->embd = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, hparams.n_embd_inp(), n_tokens);323    ggml_set_input(inp->embd);324 325    // TODO: make static using `ggml_build_forward_select()`326    //       see llm_graph_context::build_inp_embd() for reference327    ggml_tensor * tok_embd;328    if (ubatch.token) {329        ggml_tensor * tok_embd_w = layer.nextn.embed_tokens ? layer.nextn.embed_tokens : model.tok_embd;330        tok_embd = ggml_get_rows(ctx0, tok_embd_w, inp->tokens);331    } else {332        tok_embd = inp->embd;333    }334    cb(tok_embd, "mtp_tok_embd", il);335 336    inp->h = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, hparams.n_embd, n_tokens);337    ggml_set_input(inp->h);338    ggml_set_name(inp->h, "mtp_h_input");339 340    ggml_tensor * h_embd = inp->h;341 342    res->add_input(std::move(inp));343 344    ggml_tensor * inp_pos     = build_inp_pos();345    ggml_tensor * inp_out_ids = build_inp_out_ids();346    auto * inp_attn = build_attn_inp_kv_iswa();347 348    ggml_tensor * h_norm = build_norm(h_embd, layer.nextn.hnorm, nullptr, cohere2moe_norm_type, il);349    cb(h_norm, "mtp_hnorm", il);350 351    ggml_tensor * e_norm = build_norm(tok_embd, layer.nextn.enorm, nullptr, cohere2moe_norm_type, il);352    cb(e_norm, "mtp_enorm", il);353 354    ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, /*dim=*/ 0);355    cb(concat, "mtp_concat", il);356 357    ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s);358    cb(cur, "mtp_eh_proj", il);359 360    ggml_tensor * inpL = cur;361 362    cur = build_norm(cur, layer.attn_norm, nullptr, cohere2moe_norm_type, il);363    cb(cur, "mtp_attn_norm", il);364    ggml_tensor * ffn_inp = cur;365 366    auto [Qcur, Kcur, Vcur] = build_qkv(layer, cur, n_embd_head, n_head, n_head_kv, il);367    ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);368    Qcur = ggml_rope_ext(369            ctx0, Qcur, inp_pos, rope_factors,370            n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,371            ext_factor, attn_factor, beta_fast, beta_slow);372    Kcur = ggml_rope_ext(373            ctx0, Kcur, inp_pos, rope_factors,374            n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,375            ext_factor, attn_factor, beta_fast, beta_slow);376 377    cb(Qcur, "mtp_Qcur", il);378    cb(Kcur, "mtp_Kcur", il);379    cb(Vcur, "mtp_Vcur", il);380 381    cur = build_attn(inp_attn,382            layer.wo, layer.wo_b, layer.wo_s,383            Qcur, Kcur, Vcur, nullptr, nullptr, nullptr,384            1.0f / sqrtf(float(n_embd_head)), il);385    cb(cur, "mtp_attn_out", il);386 387    ggml_tensor * attn_out = cur;388 389    cur = build_moe_ffn(ffn_inp,390            layer.ffn_gate_inp,391            layer.ffn_up_exps,392            layer.ffn_gate_exps,393            layer.ffn_down_exps,394            nullptr,395            n_expert, n_expert_used,396            LLM_FFN_SILU, hparams.expert_weights_norm,397            hparams.expert_weights_scale,398            (llama_expert_gating_func_type) hparams.expert_gating_func,399            il,400            nullptr, layer.ffn_gate_up_exps,401            layer.ffn_up_exps_s,402            layer.ffn_gate_exps_s,403            layer.ffn_down_exps_s);404    cb(cur, "mtp_ffn_moe_out", il);405 406    if (layer.ffn_up_shexp) {407        ggml_tensor * ffn_shexp = build_ffn(ffn_inp,408                layer.ffn_up_shexp,   nullptr, layer.ffn_up_shexp_s,409                layer.ffn_gate_shexp, nullptr, layer.ffn_gate_shexp_s,410                layer.ffn_down_shexp, nullptr, layer.ffn_down_shexp_s,411                nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il);412        cb(ffn_shexp, "mtp_ffn_shexp", il);413 414        cur = ggml_add(ctx0, cur, ffn_shexp);415        cur = ggml_scale(ctx0, cur, 0.5f);416        cb(cur, "mtp_ffn_out", il);417    }418 419    cur = ggml_add(ctx0, cur, inpL);420    cur = ggml_add(ctx0, cur, attn_out);421    cb(cur, "mtp_post_ffn", il);422 423    ggml_tensor * head_norm_w = layer.nextn.shared_head_norm424            ? layer.nextn.shared_head_norm425            : model.output_norm;426    GGML_ASSERT(head_norm_w && "COHERE2MOE MTP: missing both nextn.shared_head_norm and output_norm");427    cur = build_norm(cur, head_norm_w, nullptr, cohere2moe_norm_type, -1);428 429    cb(cur, "h_nextn", -1);430    res->t_h_nextn = cur;431 432    cur = ggml_get_rows(ctx0, cur, inp_out_ids);433    cb(cur, "mtp_shared_head_norm", -1);434 435    ggml_tensor * head_w = layer.nextn.shared_head_head ? layer.nextn.shared_head_head : model.output;436    GGML_ASSERT(head_w && "COHERE2MOE MTP: missing LM head (nextn.shared_head_head or model.output)");437    cur = build_lora_mm(head_w, cur, layer.nextn.shared_head_head ? layer.nextn.shared_head_head_s : nullptr);438 439    if (hparams.f_logit_scale) {440        cur = ggml_scale(ctx0, cur, hparams.f_logit_scale);441    }442 443    cb(cur, "result_output", -1);444    res->t_logits = cur;445 446    ggml_build_forward_expand(gf, cur);447}448 
Brunobkr/llama.cpp_AlgMor24_github · Team Ai