CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 2d agoView on Hugging Face
0likes1.1kdownloads
exaone-moe.cpp240 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_exaone_moe::load_arch_hparams(llama_model_loader & ml) {4    hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;5    hparams.n_swa = 128;6    load_swa_pattern(ml, 4);7    hparams.rope_freq_base_train_swa  = hparams.rope_freq_base_train;8    hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;9 10    ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA,                hparams.rope_freq_base_train_swa, false);11    ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW,          hparams.n_swa);12    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS,       hparams.f_norm_rms_eps);13    ml.get_key(LLM_KV_EXPERT_SHARED_COUNT,               hparams.n_expert_shared, false);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_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);16    ml.get_key(LLM_KV_EXPERT_GATING_FUNC,                hparams.expert_gating_func);17    ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE,              hparams.expert_weights_scale, false);18    ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM,               hparams.expert_weights_norm, false);19    ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT,         hparams.n_layer_dense_lead, false);20 21    switch (hparams.n_layer()) {22        case 32: type = LLM_TYPE_30B_A3B; break;23        case 48: type = LLM_TYPE_235B_A22B; break;24        default: type = LLM_TYPE_UNKNOWN;25    }26}27 28void llama_model_exaone_moe::load_arch_tensors(llama_model_loader &) {29    LLAMA_LOAD_LOCALS;30 31    const int64_t n_ff_exp       = hparams.n_ff_exp();32    const int64_t n_ff_shexp     = hparams.n_ff_shexp > 0 ? hparams.n_ff_shexp : n_ff_exp;33    const int64_t head_dim       = hparams.n_embd_head_k();34    const int64_t n_qo_dim       = n_head * head_dim;35    const int64_t n_kv_dim       = n_head_kv * head_dim;36 37    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);38 39    // output40    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);41    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, 0);42 43    if (output == NULL) {44        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);45    }46 47    for (int i = 0; i < n_layer_all; ++i) {48        int flags = 0;49        if (i >= n_layer) {50            // skip all tensors in the NextN layers51            flags |= TENSOR_SKIP;52        }53 54        auto & layer = layers[i];55        create_tensor_qkv(layer, i, n_embd, n_qo_dim, n_kv_dim, n_kv_dim, flags);56        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_qo_dim, n_embd}, flags);57 58        layer.rope_freqs   = create_tensor(tn(LLM_TENSOR_ROPE_FREQS,  "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0) | flags);59 60        layer.attn_norm    = create_tensor(tn(LLM_TENSOR_ATTN_NORM,   "weight", i), {n_embd}, flags);61        layer.attn_q_norm  = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, flags);62        layer.attn_k_norm  = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, flags);63 64        layer.ffn_norm     = create_tensor(tn(LLM_TENSOR_FFN_NORM,    "weight", i), {n_embd}, flags);65 66        // dense layers for first n_layer_dense_lead layers or nextn_predict_layers layers at the end67        if (i < (int) hparams.n_layer_dense_lead || (i >= n_layer)) {68            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        } else {72            layer.ffn_gate_inp    = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP,  "weight", i), {n_embd, n_expert}, flags);73            layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED | flags);74 75            if (n_expert == 0) {76                throw std::runtime_error("n_expert must be > 0");77            }78            if (n_expert_used == 0) {79                throw std::runtime_error("n_expert_used must be > 0");80            }81 82            layer.ffn_gate_exps  = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS,  "weight", i), {n_embd, n_ff_exp, n_expert}, flags);83            layer.ffn_down_exps  = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS,  "weight", i), {n_ff_exp, n_embd, n_expert}, flags);84            layer.ffn_up_exps    = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,    "weight", i), {n_embd, n_ff_exp, n_expert}, flags);85 86            layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_shexp}, flags);87            layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, flags);88            layer.ffn_up_shexp   = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP,   "weight", i), {n_embd, n_ff_shexp}, flags);89        }90 91        // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers92        if (i >= n_layer) {93            layer.nextn.eh_proj          = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), {2 * n_embd, n_embd}, flags);94            layer.nextn.enorm            = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM,   "weight", i), {n_embd}, flags);95            layer.nextn.hnorm            = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM,   "weight", i), {n_embd}, flags);96 97            layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), {n_embd}, flags | TENSOR_NOT_REQUIRED);98            layer.nextn.embed_tokens     = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS,     "weight", i), {n_embd, n_vocab}, flags | TENSOR_NOT_REQUIRED);99            layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), {n_embd, n_vocab}, flags | TENSOR_NOT_REQUIRED);100        }101    }102}103 104std::unique_ptr<llm_graph_context> llama_model_exaone_moe::build_arch_graph(const llm_graph_params & params) const {105    return std::make_unique<graph>(*this, params);106}107 108llama_model_exaone_moe::graph::graph(const llama_model & model, const llm_graph_params & params) :109    llm_graph_context(params) {110    const int64_t n_embd_head = hparams.n_embd_head_k();111 112    GGML_ASSERT(n_embd_head == hparams.n_embd_head_v());113    GGML_ASSERT(n_embd_head == n_rot);114 115    ggml_tensor * cur;116    ggml_tensor * inpL;117 118    inpL = build_inp_embd(model.tok_embd);119 120    // inp_pos - contains the positions121    ggml_tensor * inp_pos = build_inp_pos();122 123    auto * inp_attn_iswa = build_attn_inp_kv_iswa();124 125    ggml_tensor * inp_out_ids = build_inp_out_ids();126 127    for (int il = 0; il < n_layer; ++il) {128        ggml_tensor * inpSA = inpL;129 130        // use RoPE for SWA layers131        const bool is_local_layer = hparams.is_swa(il);132 133        // norm134        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);135        cb(cur, "attn_norm", il);136 137        // self-attention138        {139            ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);140 141            // compute Q and K and RoPE them142            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,143                    n_embd_head, n_head, n_head_kv, il);144 145            Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);146            Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);147            cb(Qcur, "Qcur_normed", il);148            cb(Kcur, "Kcur_normed", il);149 150            if (is_local_layer) {151                Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base,152                                     freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);153 154                Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base,155                                     freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);156            }157            cb(Qcur, "Qcur", il);158            cb(Kcur, "Kcur", il);159            cb(Vcur, "Vcur", il);160 161            cur = build_attn(inp_attn_iswa,162                model.layers[il].wo, NULL, model.layers[il].wo_s,163                Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);164            cb(cur, "attn_out", il);165        }166        if (il == n_layer - 1 && inp_out_ids) {167            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);168            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);169        }170        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);171        cb(ffn_inp, "ffn_inp", il);172 173        // norm174        cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);175        cb(cur, "ffn_norm", il);176 177        // feed-forward network178        if (model.layers[il].ffn_gate_inp == nullptr) {179            // dense branch180            cur = build_ffn(cur,181                    model.layers[il].ffn_up, NULL, NULL,182                    model.layers[il].ffn_gate, NULL, NULL,183                    model.layers[il].ffn_down, NULL, NULL, NULL,184                    LLM_FFN_SILU, LLM_FFN_PAR, il);185            cb(cur, "ffn_out", il);186        } else {187            // MoE branch188            ggml_tensor * moe_out = build_moe_ffn(cur,189                model.layers[il].ffn_gate_inp,190                model.layers[il].ffn_up_exps,191                model.layers[il].ffn_gate_exps,192                model.layers[il].ffn_down_exps,193                model.layers[il].ffn_exp_probs_b,194                n_expert, n_expert_used,195                LLM_FFN_SILU, hparams.expert_weights_norm,196                hparams.expert_weights_scale,197                (llama_expert_gating_func_type) hparams.expert_gating_func,198                il);199            cb(moe_out, "ffn_moe_out", il);200 201            // FFN shared expert202            {203                ggml_tensor * ffn_shexp =204                    build_ffn(cur,205                        model.layers[il].ffn_up_shexp, NULL, NULL,206                        model.layers[il].ffn_gate_shexp, NULL, NULL,207                        model.layers[il].ffn_down_shexp, NULL, NULL,208                        NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);209                cb(ffn_shexp, "ffn_shexp", il);210 211                cur = ggml_add(ctx0, moe_out, ffn_shexp);212                cb(cur, "ffn_out", il);213            }214        }215 216        cur = ggml_add(ctx0, cur, ffn_inp);217 218        cur = build_cvec(cur, il);219        cb(cur, "l_out", il);220 221        // input for next layer222        inpL = cur;223    }224    cur = inpL;225 226    // final norm227    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);228 229    cb(cur, "result_norm", -1);230    res->t_embd = cur;231 232    // lm_head233    cur = build_lora_mm(model.output, cur, model.output_s);234 235    cb(cur, "result_output", -1);236    res->t_logits = cur;237 238    ggml_build_forward_expand(gf, cur);239}240