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
mellum.cpp226 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_mellum::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_EXPERT_FEED_FORWARD_LENGTH,  hparams.n_ff_exp);6    ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW,    hparams.n_swa, false);7 8    if (hparams.n_swa > 0) {9        hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;10 11        uint32_t swa_period = 4;12        const auto res = ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false);13        if (res) {14            hparams.set_swa_pattern(swa_period);15        } else {16            ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, hparams.is_swa_impl, hparams.n_layer());17        }18 19        hparams.rope_freq_base_train_swa  = hparams.rope_freq_base_train;20        hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;21 22        ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);23    } else {24        hparams.swa_type = LLAMA_SWA_TYPE_NONE;25    }26 27    switch (hparams.n_layer()) {28        case 28: type = LLM_TYPE_12B_A2_5B; break;29        default: type = LLM_TYPE_UNKNOWN;30    }31}32 33void llama_model_mellum::load_arch_tensors(llama_model_loader &) {34    LLAMA_LOAD_LOCALS;35 36    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);37 38    // output39    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);40    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, 0);41 42    for (int i = 0; i < n_layer; ++i) {43        auto & layer = layers[i];44 45        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);46 47        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_gqa, n_embd_gqa, 0);48        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);49 50        layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);51        layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);52 53        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);54 55        layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);56 57        if (n_expert == 0) {58            throw std::runtime_error("n_expert must be > 0 for Mellum");59        }60        if (n_expert_used == 0) {61            throw std::runtime_error("n_expert_used must be > 0 for Mellum");62        }63 64        const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;65 66        layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {  n_embd, n_ff_exp, n_expert}, 0);67        layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp,   n_embd, n_expert}, 0);68        layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {  n_embd, n_ff_exp, n_expert}, 0);69    }70}71 72std::unique_ptr<llm_graph_context> llama_model_mellum::build_arch_graph(const llm_graph_params & params) const {73    if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {74        return std::make_unique<graph<true>>(*this, params);75    }76    return std::make_unique<graph<false>>(*this, params);77}78 79template <bool iswa>80llama_model_mellum::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {81    const int64_t n_embd_head = hparams.n_embd_head_v();82 83    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());84    GGML_ASSERT(n_embd_head == n_rot);85 86    ggml_tensor * cur;87    ggml_tensor * inpL;88 89    inpL = build_inp_embd(model.tok_embd);90 91    // inp_pos - contains the positions92    ggml_tensor * inp_pos = build_inp_pos();93 94    using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;95    inp_attn_type * inp_attn = nullptr;96 97    if constexpr (iswa) {98        inp_attn = build_attn_inp_kv_iswa();99    } else {100        inp_attn = build_attn_inp_kv();101    }102 103    ggml_tensor * inp_out_ids = build_inp_out_ids();104 105    for (int il = 0; il < n_layer; ++il) {106        ggml_tensor * inpSA = inpL;107 108        // norm109        cur = build_norm(inpL,110                model.layers[il].attn_norm, nullptr,111                LLM_NORM_RMS, il);112        cb(cur, "attn_norm", il);113 114        // self_attention115        {116            // compute Q and K and RoPE them117            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, nullptr, LLM_NORM_RMS, il);121            cb(Qcur, "Qcur_normed", il);122 123            Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, nullptr, LLM_NORM_RMS, il);124            cb(Kcur, "Kcur_normed", il);125 126            const bool is_swa = hparams.is_swa(il);127 128            if (is_swa) {129                // For sliding window layers, use regular rope with no yarn rope scaling.130                // This is achieved here by setting freq_scale and attn_factor to 1.131                // We also set ext_factor to 0 to avoid a few unnecessary computations.132                Qcur = ggml_rope_ext(133                    ctx0, Qcur, inp_pos, nullptr,134                    n_rot, rope_type, n_ctx_orig, freq_base, 1.0,135                    0.0, 1.0, beta_fast, beta_slow136                    );137 138                Kcur = ggml_rope_ext(139                    ctx0, Kcur, inp_pos, nullptr,140                    n_rot, rope_type, n_ctx_orig, freq_base, 1.0,141                    0.0, 1.0, beta_fast, beta_slow142                    );143            } else {144                Qcur = ggml_rope_ext(145                    ctx0, Qcur, inp_pos, nullptr,146                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,147                    ext_factor, attn_factor, beta_fast, beta_slow148                    );149 150                Kcur = ggml_rope_ext(151                    ctx0, Kcur, inp_pos, nullptr,152                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,153                    ext_factor, attn_factor, beta_fast, beta_slow154                    );155            }156 157            cb(Qcur, "Qcur", il);158            cb(Kcur, "Kcur", il);159            cb(Vcur, "Vcur", il);160 161            cur = build_attn(inp_attn,162                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,163                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);164        }165        if (il == n_layer - 1 && inp_out_ids) {166            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);167            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);168        }169        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);170        cb(ffn_inp, "ffn_inp", il);171 172        // MoE173        cur = build_norm(ffn_inp,174                model.layers[il].ffn_norm, nullptr,175                LLM_NORM_RMS, il);176        cb(cur, "ffn_norm", il);177 178        ggml_tensor * moe_out =179            build_moe_ffn(cur,180                    model.layers[il].ffn_gate_inp,181                    model.layers[il].ffn_up_exps,182                    model.layers[il].ffn_gate_exps,183                    model.layers[il].ffn_down_exps,184                    nullptr,185                    n_expert, n_expert_used,186                    LLM_FFN_SILU, true,187                    hparams.expert_weights_scale,188                    LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,189                    il,190                    nullptr, nullptr,191                    model.layers[il].ffn_up_exps_s,192                    model.layers[il].ffn_gate_exps_s,193                    model.layers[il].ffn_down_exps_s);194        cb(moe_out, "ffn_moe_out", il);195        cur = moe_out;196 197        cur = ggml_add(ctx0, cur, ffn_inp);198        cb(cur, "ffn_out", il);199 200        cur = build_cvec(cur, il);201        cb(cur, "l_out", il);202 203        // input for next layer204        inpL = cur;205    }206    cur = inpL;207 208    cur = build_norm(cur,209            model.output_norm, nullptr,210            LLM_NORM_RMS, -1);211 212    cb(cur, "result_norm", -1);213    res->t_embd = cur;214 215    // lm_head216    cur = build_lora_mm(model.output, cur, model.output_s);217 218    cb(cur, "result_output", -1);219    res->t_logits = cur;220 221    ggml_build_forward_expand(gf, cur);222}223 224template struct llama_model_mellum::graph<false>;225template struct llama_model_mellum::graph<true>;226 
Brunobkr/llama.cpp_AlgMor24_github · Team Ai