CoolFace
Modelpublic

Felipe97/llama-cpp-compiled

sourceHugging Faceupdated 2d agoView on Hugging Face
0likes1.1kdownloads
gemma3.cpp224 linesDownload Raw Back to models
1#include "models.h"2 3void llama_model_gemma3::load_arch_hparams(llama_model_loader & ml) {4    const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);5    if (found_swa && hparams.n_swa > 0) {6        hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;7        load_swa_pattern(ml, 6);8 9        ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);10    } else {11        hparams.swa_type = LLAMA_SWA_TYPE_NONE;12    }13 14    hparams.f_final_logit_softcapping = 0.0f;15    ml.get_key(LLM_KV_FINAL_LOGIT_SOFTCAPPING, hparams.f_final_logit_softcapping, false);16    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);17 18    switch (hparams.n_layer()) {19        case 18: type = LLM_TYPE_270M; break;20        case 26: type = LLM_TYPE_1B; break;21        case 32: type = LLM_TYPE_8B; break; // Rnj-122        case 34: type = LLM_TYPE_4B; break;23        case 48: type = LLM_TYPE_12B; break;24        case 62: type = LLM_TYPE_27B; break;25        default: type = LLM_TYPE_UNKNOWN;26    }27 28    // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/config.py#L28929    hparams.f_attention_scale = type == LLM_TYPE_27B30        ? 1.0f / std::sqrt(float(hparams.n_embd / hparams.n_head(0)))31        : 1.0f / std::sqrt(float(hparams.n_embd_head_k()));32}33 34void llama_model_gemma3::load_arch_tensors(llama_model_loader &) {35    LLAMA_LOAD_LOCALS;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}, TENSOR_NOT_REQUIRED);42 43    // if output is NULL, init from the input tok embed44    if (output == NULL) {45        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD,   "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);46    }47 48    // Dense linear weights49    dense_2_out_layers = create_tensor(tn(LLM_TENSOR_DENSE_2_OUT, "weight"), {n_embd, hparams.dense_2_feat_out}, TENSOR_NOT_REQUIRED);50    dense_3_out_layers = create_tensor(tn(LLM_TENSOR_DENSE_3_OUT, "weight"), {hparams.dense_3_feat_in, n_embd}, TENSOR_NOT_REQUIRED);51 52 53    for (int i = 0; i < n_layer; ++i) {54        auto & layer = layers[i];55 56        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);57 58        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0);59        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);60 61        layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);62        layer.attn_k_norm    = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM,    "weight", i), {n_embd_head_k}, 0);63        layer.attn_q_norm    = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM,    "weight", i), {n_embd_head_k}, 0);64 65        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);66        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);67        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);68        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);69        layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);70    }71}72 73std::unique_ptr<llm_graph_context> llama_model_gemma3::build_arch_graph(const llm_graph_params & params) const {74    if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {75        return std::make_unique<graph<true>>(*this, params);76    } else {77        return std::make_unique<graph<false>>(*this, params);78    }79}80 81template <bool iswa>82llama_model_gemma3::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {83    const int64_t n_embd_head = hparams.n_embd_head_k();84 85    ggml_tensor * cur;86    ggml_tensor * inpL;87 88    inpL = build_inp_embd(model.tok_embd);89 90    // important: do not normalize weights for raw embeddings input (i.e. encoded image embeddings)91    inpL = ggml_scale(ctx0, inpL, ubatch.token ? sqrtf(n_embd) : 1.0f);92    cb(inpL, "inp_scaled", -1);93 94    // inp_pos - contains the positions95    ggml_tensor * inp_pos = build_inp_pos();96 97    // TODO: is causal == true correct? might need some changes98    using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;99    inp_attn_type * inp_attn = nullptr;100 101    if constexpr (iswa) {102        inp_attn = build_attn_inp_kv_iswa();103    } else {104        inp_attn = build_attn_inp_kv();105    }106 107    ggml_tensor * inp_out_ids = build_inp_out_ids();108 109    for (int il = 0; il < n_layer; ++il) {110        float freq_base_l  = 0.0f;111        float freq_scale_l = 0.0f;112 113        if constexpr (iswa) {114            freq_base_l  = model.get_rope_freq_base (cparams, il);115            freq_scale_l = model.get_rope_freq_scale(cparams, il);116        } else {117            freq_base_l  = freq_base;118            freq_scale_l = freq_scale;119        }120 121        // norm122        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);123        cb(cur, "attn_norm", il);124 125        // self-attention126        {127            // compute Q and K and RoPE them128            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,129                    n_embd_head, n_head, n_head_kv, il);130 131            Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);132            cb(Qcur, "Qcur_normed", il);133 134            Qcur = ggml_rope_ext(135                    ctx0, Qcur, inp_pos, nullptr,136                    n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,137                    ext_factor, attn_factor, beta_fast, beta_slow);138 139            Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);140            cb(Kcur, "Kcur_normed", il);141 142            Kcur = ggml_rope_ext(143                    ctx0, Kcur, inp_pos, nullptr,144                    n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,145                    ext_factor, attn_factor, beta_fast, beta_slow);146 147            cb(Qcur, "Qcur", il);148            cb(Kcur, "Kcur", il);149            cb(Vcur, "Vcur", il);150 151            // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/model.py#L315152            Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);153 154            cur = build_attn(inp_attn,155                    model.layers[il].wo, NULL, model.layers[il].wo_s,156                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);157        }158        if (il == n_layer - 1 && inp_out_ids) {159            cur  = ggml_get_rows(ctx0,  cur, inp_out_ids);160            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);161        }162        cur = build_norm(cur,163                model.layers[il].attn_post_norm, NULL,164                LLM_NORM_RMS, il);165        cb(cur, "attn_post_norm", il);166 167        ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);168        cb(sa_out, "sa_out", il);169 170        cur = build_norm(sa_out,171                model.layers[il].ffn_norm, NULL,172                LLM_NORM_RMS, il);173        cb(cur, "ffn_norm", il);174 175        // feed-forward network176        {177            cur = build_ffn(cur,178                    model.layers[il].ffn_up,   NULL, NULL,179                    model.layers[il].ffn_gate, NULL, NULL,180                    model.layers[il].ffn_down, NULL, NULL,181                    NULL,182                    LLM_FFN_GELU, LLM_FFN_PAR, il);183            cb(cur, "ffn_out", il);184        }185        cur = build_norm(cur,186                model.layers[il].ffn_post_norm, NULL,187                LLM_NORM_RMS, -1);188        cb(cur, "ffn_post_norm", il);189 190        cur = ggml_add(ctx0, cur, sa_out);191 192        cur = build_cvec(cur, il);193        cb(cur, "l_out", il);194 195        // input for next layer196        inpL = cur;197    }198    cur = inpL;199 200    cur = build_norm(cur,201            model.output_norm, NULL,202            LLM_NORM_RMS, -1);203 204    cb(cur, "result_norm", -1);205    res->t_embd = cur;206 207    // lm_head208    cur = build_lora_mm(model.output, cur, model.output_s);209 210    if (hparams.f_final_logit_softcapping) {211        cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);212        cur = ggml_tanh(ctx0, cur);213        cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);214    }215 216    cb(cur, "result_output", -1);217    res->t_logits = cur;218 219    ggml_build_forward_expand(gf, cur);220}221 222template struct llama_model_gemma3::graph<false>;223template struct llama_model_gemma3::graph<true>;224