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
gemma4-assistant.cpp204 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_gemma4_assistant::load_arch_hparams(llama_model_loader & ml) {4    hparams.n_embd_inp_impl = hparams.n_embd_out();5 6    hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;7    ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, hparams.is_swa_impl, hparams.n_layer());8 9    uint32_t n_kv_shared_layers = 0;10    ml.get_key(LLM_KV_ATTENTION_SHARED_KV_LAYERS, n_kv_shared_layers, false);11 12    hparams.f_attention_scale = 1.0f;13 14    ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.n_layer_nextn, false);15    GGML_ASSERT(hparams.n_layer_nextn == hparams.n_layer_all && "n_layer_nextn must be == n_layer_impl");16 17    ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA,           hparams.rope_freq_base_train_swa, false);18    ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW,     hparams.n_swa);19    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS,  hparams.f_norm_rms_eps);20    ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH_SWA,     hparams.n_embd_head_k_swa);21    ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH_SWA,   hparams.n_embd_head_v_swa);22}23 24void llama_model_gemma4_assistant::load_arch_tensors(llama_model_loader &) {25    LLAMA_LOAD_LOCALS;26 27    if (n_embd_head_k != n_embd_head_v) {28        throw std::runtime_error("Gemma 4 assistant requires n_embd_head_k == n_embd_head_v");29    }30    if (hparams.n_embd_head_k_swa != hparams.n_embd_head_v_swa) {31        throw std::runtime_error("Gemma 4 assistant requires n_embd_head_k_swa == n_embd_head_v_swa");32    }33    if (hparams.n_embd_out() == n_embd) {34        throw std::runtime_error("Gemma 4 assistant requires embedding_length_out to carry the target hidden size");35    }36 37    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);38    output   = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);39 40    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);41 42    create_tensor(tn(LLM_TENSOR_MASKED_EMBD_CENTROIDS, "weight"), {}, TENSOR_NOT_REQUIRED);43    create_tensor(tn(LLM_TENSOR_MASKED_EMBD_ORDERING),  {}, TENSOR_NOT_REQUIRED);44 45    const int64_t n_embd_backbone = hparams.n_embd_inp();46    nextn_proj_post = create_tensor(tn(LLM_TENSOR_NEXTN_PROJ_POST, "weight"), { n_embd, n_embd_backbone }, 0);47 48    int rope_freqs_flag = 0;49 50    for (int i = 0; i < n_layer_nextn; ++i) {51        auto & layer = layers[i];52 53        const int64_t n_head      = hparams.n_head(i);54        const int64_t n_embd_head = hparams.n_embd_head_k(i);55        const int64_t n_ff        = hparams.n_ff(i);56 57        if (i == 0) {58            nextn_proj_pre = create_tensor(tn(LLM_TENSOR_NEXTN_PROJ_PRE, "weight", i), { 2*n_embd_backbone, n_embd }, 0);59        }60 61        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);62        layer.wq        = create_tensor(tn(LLM_TENSOR_ATTN_Q,    "weight", i), { n_embd, n_embd_head*n_head }, 0);63        layer.wo        = create_tensor(tn(LLM_TENSOR_ATTN_OUT,  "weight", i), { n_embd_head*n_head, n_embd }, 0);64 65        layer.attn_q_norm    = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM,    "weight", i), { n_embd_head }, 0);66        layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), { n_embd }, 0);67 68        layer.out_scale = create_tensor(tn(LLM_TENSOR_LAYER_OUT_SCALE, "weight", i), { 1u }, 0);69 70        if (!hparams.is_swa(i)) {71            layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), { n_embd_head/2 }, rope_freqs_flag);72            rope_freqs_flag = TENSOR_DUPLICATED;73        }74 75        layer.ffn_norm      = create_tensor(tn(LLM_TENSOR_FFN_NORM,      "weight", i), { n_embd }, 0);76        layer.ffn_gate      = create_tensor(tn(LLM_TENSOR_FFN_GATE,      "weight", i), { n_embd, n_ff }, 0);77        layer.ffn_up        = create_tensor(tn(LLM_TENSOR_FFN_UP,        "weight", i), { n_embd, n_ff }, 0);78        layer.ffn_down      = create_tensor(tn(LLM_TENSOR_FFN_DOWN,      "weight", i), { n_ff, n_embd }, 0);79        layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), { n_embd }, 0);80    }81}82 83std::unique_ptr<llm_graph_context> llama_model_gemma4_assistant::build_arch_graph(const llm_graph_params & params) const {84    return std::make_unique<graph>(*this, params);85}86 87llama_model_gemma4_assistant::graph::graph(const llama_model & model, const llm_graph_params & params) :88        llm_graph_context(params) {89    const int64_t n_embd_backbone = hparams.n_embd_inp();90 91    ggml_tensor * inp_tokens;92    ggml_tensor * inp_h;93    {94        auto inp = std::make_unique<llm_graph_input_embd>(n_embd_backbone);95 96        inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, ubatch.n_tokens);97        cb(inp->tokens, "inp_tokens", -1);98        ggml_set_input(inp->tokens);99        inp_tokens = inp->tokens;100        res->t_inp_tokens = inp->tokens;101 102        inp->embd = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_embd_backbone, ubatch.n_tokens);103        cb(inp->embd, "inp_h", -1);104        ggml_set_input(inp->embd);105        inp_h = inp->embd;106        res->t_inp_embd = inp->embd;107 108        res->add_input(std::move(inp));109    }110 111    GGML_ASSERT(cparams.ctx_other != nullptr);112    const auto * model_other = llama_get_model(cparams.ctx_other);113 114    ggml_tensor * x = ggml_get_rows(ctx0, model_other->tok_embd, inp_tokens);115    x = ggml_scale(ctx0, x, sqrtf((float) n_embd_backbone));116    cb(x, "inp_embd_target", -1);117 118    ggml_tensor * xh = ggml_concat(ctx0, x, inp_h, 0);119    cb(xh, "inp_xh", -1);120 121    ggml_tensor * cur = ggml_mul_mat(ctx0, model.nextn_proj_pre, xh);122    cb(cur, "pre_proj", -1);123 124    auto *        inp_attn    = build_attn_inp_kv_iswa();125    ggml_tensor * inp_pos     = build_inp_pos();126    ggml_tensor * inp_out_ids = build_inp_out_ids();127 128    ggml_tensor * inpL = cur;129 130    for (int il = 0; il < n_layer_nextn; ++il) {131        const bool is_swa = hparams.is_swa(il);132 133        const int64_t n_embd_head = hparams.n_embd_head_k(il);134        const int64_t n_head      = hparams.n_head(il);135 136        const float freq_base_l  = model.get_rope_freq_base(cparams, il);137        const float freq_scale_l = model.get_rope_freq_scale(cparams, il);138        const int   n_rot_l      = hparams.n_rot(il);139 140        ggml_tensor * cur_norm = build_norm(inpL, model.layers[il].attn_norm, nullptr, LLM_NORM_RMS, il);141        cb(cur_norm, "attn_norm", il);142 143        ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur_norm);144        Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);145        Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, nullptr, LLM_NORM_RMS, il);146        cb(Qcur, "Qcur_normed", il);147 148        ggml_tensor * freq_factors = is_swa ? nullptr : model.layers[il].rope_freqs;149        Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, freq_factors, n_rot_l, rope_type, n_ctx_orig,150                             freq_base_l, freq_scale_l, ext_factor, attn_factor, beta_fast, beta_slow);151        cb(Qcur, "Qcur_pos", il);152 153        cur = build_attn(inp_attn, model.layers[il].wo, nullptr, nullptr,154                Qcur, nullptr, nullptr, nullptr, nullptr, nullptr, hparams.f_attention_scale, il);155 156        if (il == n_layer_nextn - 1 && inp_out_ids) {157            cur  = ggml_get_rows(ctx0, cur,  inp_out_ids);158            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);159        }160 161        cur = build_norm(cur, model.layers[il].attn_post_norm, nullptr, LLM_NORM_RMS, il);162        cb(cur, "attn_post_norm", il);163 164        ggml_tensor * attn_out = ggml_add(ctx0, cur, inpL);165        cb(attn_out, "attn_out", il);166 167        cur = build_norm(attn_out, model.layers[il].ffn_norm, nullptr, LLM_NORM_RMS, il);168        cb(cur, "ffn_norm", il);169 170        cur = build_ffn(cur,171                model.layers[il].ffn_up,   nullptr, nullptr,172                model.layers[il].ffn_gate, nullptr, nullptr,173                model.layers[il].ffn_down, nullptr, nullptr,174                nullptr,175                LLM_FFN_GELU, LLM_FFN_PAR, il);176        cb(cur, "ffn_out", il);177 178        cur = build_norm(cur, model.layers[il].ffn_post_norm, nullptr, LLM_NORM_RMS, -1);179        cb(cur, "ffn_post_norm", il);180 181        cur = ggml_add(ctx0, cur, attn_out);182 183        cur = ggml_mul(ctx0, cur, model.layers[il].out_scale);184        cb(cur, "out_scaled", il);185 186        inpL = cur;187    }188    cur = inpL;189 190    cur = build_norm(cur, model.output_norm, nullptr, LLM_NORM_RMS, -1);191    cb(cur, "result_norm", -1);192 193    ggml_tensor * logits = build_lora_mm(model.output, cur);194    cb(logits, "result_output", -1);195    res->t_logits = logits;196 197    ggml_tensor * h_next = ggml_mul_mat(ctx0, model.nextn_proj_post, cur);198    cb(h_next, "h_nextn", -1);199    res->t_h_nextn = h_next;200 201    ggml_build_forward_expand(gf, logits);202    ggml_build_forward_expand(gf, h_next);203}204 
Brunobkr/llama.cpp_AlgMor24_github · Team Ai