CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 2d agoView on Hugging Face
0likes1.1kdownloads
bailingmoe2.cpp212 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_bailingmoe2::load_arch_hparams(llama_model_loader & ml) {4    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS,       hparams.f_norm_rms_eps);5    ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT,         hparams.n_layer_dense_lead, false);6    ml.get_key_or_arr(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp_arr, hparams.n_layer_all);7    ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);8    ml.get_key(LLM_KV_EXPERT_SHARED_COUNT,               hparams.n_expert_shared);9    ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE,              hparams.expert_weights_scale, false);10    ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM,               hparams.expert_weights_norm, false);11    ml.get_key(LLM_KV_EXPERT_GATING_FUNC,                hparams.expert_gating_func);12 13    switch (hparams.n_layer()) {14        case 20: type = LLM_TYPE_16B_A1B; break;15        case 32: type = LLM_TYPE_100B_A6B; break;16        default: type = LLM_TYPE_UNKNOWN;17    }18}19 20void llama_model_bailingmoe2::load_arch_tensors(llama_model_loader &) {21    LLAMA_LOAD_LOCALS;22    const int64_t n_expert_shared = hparams.n_expert_shared;23 24    const int64_t n_ff_exp        = hparams.n_ff_exp();25 26    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);27 28    // output29    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);30    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, 0);31 32    GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for bailingmoe2");33    GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for bailingmoe2");34 35    for (int i = 0; i < n_layer_all; ++i) {36        int flags = 0;37        if (i >= n_layer) {38            // skip all tensors in the NextN layers39            flags |= TENSOR_SKIP;40        }41 42        auto & layer = layers[i];43 44        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, flags);45 46        layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, flags);47        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, flags);48 49        layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, flags);50        layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, flags);51 52        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, flags);53 54        if (static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead) { // MoE layers55            const int64_t n_ff_shexp = (hparams.n_ff_shexp ? hparams.n_ff_shexp : n_ff_exp) * n_expert_shared;56 57            layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, flags);58            layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED | flags);59 60            layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {  n_embd, n_ff_exp, n_expert}, flags);61            layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp,   n_embd, n_expert}, flags);62            layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {  n_embd, n_ff_exp, n_expert}, flags);63 64            layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_shexp}, flags);65            layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, flags);66            layer.ffn_up_shexp   = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP,   "weight", i), {n_embd, n_ff_shexp}, flags);67        } else { // Dense layers68            layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, flags);69            layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, flags);70            layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, flags);71        }72 73        // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers74        if (i >= n_layer) {75            layer.nextn.eh_proj          = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags);76            layer.nextn.embed_tokens     = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED | flags);77            layer.nextn.enorm            = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags);78            layer.nextn.hnorm            = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags);79            layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED | flags);80            layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, TENSOR_NOT_REQUIRED | flags);81            layer.layer_out_norm         = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, flags);82        }83    }84}85 86std::unique_ptr<llm_graph_context> llama_model_bailingmoe2::build_arch_graph(const llm_graph_params & params) const {87    return std::make_unique<graph>(*this, params);88}89 90llama_model_bailingmoe2::graph::graph(const llama_model & model, const llm_graph_params & params) :91    llm_graph_context(params) {92    const int64_t n_embd_head = hparams.n_embd_head_v();93 94    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());95 96    ggml_tensor * cur;97    ggml_tensor * inpL;98 99    inpL = build_inp_embd(model.tok_embd);100 101    // inp_pos - contains the positions102    ggml_tensor * inp_pos = build_inp_pos();103 104    auto * inp_attn = build_attn_inp_kv();105 106    ggml_tensor * inp_out_ids = build_inp_out_ids();107 108    for (int il = 0; il < n_layer; ++il) {109        ggml_tensor * inpSA = inpL;110 111        // norm112        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);113        cb(cur, "attn_norm", il);114 115        // self_attention116        {117            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,118                    n_embd_head, n_head, n_head_kv, il);119 120            Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);121            cb(Qcur, "Qcur_normed", il);122 123            Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,124                                 ext_factor, attn_factor, beta_fast, beta_slow);125 126            Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);127            cb(Kcur, "Kcur_normed", il);128 129            Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,130                                 ext_factor, attn_factor, beta_fast, beta_slow);131 132            cb(Qcur, "Qcur", il);133            cb(Kcur, "Kcur", il);134            cb(Vcur, "Vcur", il);135 136            cur = build_attn(inp_attn,137                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,138                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);139        }140 141        if (il == n_layer - 1 && inp_out_ids) {142            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);143            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);144        }145 146        ggml_tensor * sa_out = ggml_add(ctx0, cur, inpSA);147        cb(sa_out, "sa_out", il);148 149        // MoE branch150        cur = build_norm(sa_out, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);151        cb(cur, "ffn_norm", il);152 153        if (static_cast<uint32_t>(il) < hparams.n_layer_dense_lead) {154            cur = build_ffn(cur,155                    model.layers[il].ffn_up, NULL, NULL,156                    model.layers[il].ffn_gate, NULL, NULL,157                    model.layers[il].ffn_down, NULL, NULL,158                    NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);159            cb(cur, "ffn_out", il);160        } else {161            ggml_tensor * moe_out = build_moe_ffn(cur,162                model.layers[il].ffn_gate_inp,163                model.layers[il].ffn_up_exps,164                model.layers[il].ffn_gate_exps,165                model.layers[il].ffn_down_exps,166                model.layers[il].ffn_exp_probs_b,167                n_expert, n_expert_used,168                LLM_FFN_SILU, hparams.expert_weights_norm,169                hparams.expert_weights_scale,170                (llama_expert_gating_func_type) hparams.expert_gating_func,171                il);172            cb(moe_out, "ffn_moe_out", il);173 174            {175                ggml_tensor * ffn_shexp =176                    build_ffn(cur,177                        model.layers[il].ffn_up_shexp, NULL, NULL,178                        model.layers[il].ffn_gate_shexp, NULL, NULL,179                        model.layers[il].ffn_down_shexp, NULL, NULL,180                        NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);181                cb(ffn_shexp, "ffn_shexp", il);182 183                cur = ggml_add(ctx0, moe_out, ffn_shexp);184                cb(cur, "ffn_out", il);185            }186        }187 188        cur = ggml_add(ctx0, cur, sa_out);189 190        cur = build_cvec(cur, il);191        cb(cur, "l_out", il);192 193        // input for next layer194        inpL = cur;195    }196 197    cur = inpL;198 199    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);200 201    cb(cur, "result_norm", -1);202    res->t_embd = cur;203 204    // lm_head205    cur = build_lora_mm(model.output, cur, model.output_s);206 207    cb(cur, "result_output", -1);208    res->t_logits = cur;209 210    ggml_build_forward_expand(gf, cur);211}212