Team Ai
Datasetpublic

echodict/llama.cpp

version https://git-lfs.github.com/spec/v1 oid sha256:cfc44b7ba25614df70e6b65e3341cae0310163bd32fd31a6b928a542df433faf size 30786

sourceHugging Faceupdated 6mo agoView on Hugging Face
0likes479downloads
gemma2-iswa.cpp119 linesDownload Raw Back to models
1#include "models.h"2 3llm_build_gemma2_iswa::llm_build_gemma2_iswa(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {4    const int64_t n_embd_head = hparams.n_embd_head_k();5 6    ggml_tensor * cur;7    ggml_tensor * inpL;8 9    inpL = build_inp_embd(model.tok_embd);10 11    inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));12    cb(inpL, "inp_scaled", -1);13 14    // inp_pos - contains the positions15    ggml_tensor * inp_pos = build_inp_pos();16 17    auto * inp_attn = build_attn_inp_kv_iswa();18 19    ggml_tensor * inp_out_ids = build_inp_out_ids();20 21    for (int il = 0; il < n_layer; ++il) {22        const float freq_base_l  = model.get_rope_freq_base (cparams, il);23        const float freq_scale_l = model.get_rope_freq_scale(cparams, il);24 25        // norm26        cur = build_norm(inpL,27                model.layers[il].attn_norm, NULL,28                LLM_NORM_RMS, il);29        cb(cur, "attn_norm", il);30 31        // self-attention32        {33            // compute Q and K and RoPE them34            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,35                    n_embd_head, n_head, n_head_kv, il);36 37            Qcur = ggml_rope_ext(38                    ctx0, Qcur, inp_pos, nullptr,39                    n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,40                    ext_factor, attn_factor, beta_fast, beta_slow);41 42            Kcur = ggml_rope_ext(43                    ctx0, Kcur, inp_pos, nullptr,44                    n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,45                    ext_factor, attn_factor, beta_fast, beta_slow);46 47            cb(Qcur, "Qcur", il);48            cb(Kcur, "Kcur", il);49            cb(Vcur, "Vcur", il);50 51            Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);52 53            cur = build_attn(inp_attn,54                    model.layers[il].wo, NULL, model.layers[il].wo_s,55                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);56        }57        if (il == n_layer - 1 && inp_out_ids) {58            cur  = ggml_get_rows(ctx0,  cur, inp_out_ids);59            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);60        }61        cur = build_norm(cur,62                model.layers[il].attn_post_norm, NULL,63                LLM_NORM_RMS, il);64        cb(cur, "attn_post_norm", il);65 66        ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);67        cb(sa_out, "sa_out", il);68 69        cur = build_norm(sa_out,70                model.layers[il].ffn_norm, NULL,71                LLM_NORM_RMS, il);72        cb(cur, "ffn_norm", il);73 74        // feed-forward network75        {76            cur = build_ffn(cur,77                    model.layers[il].ffn_up,   NULL, NULL,78                    model.layers[il].ffn_gate, NULL, NULL,79                    model.layers[il].ffn_down, NULL, NULL,80                    NULL,81                    LLM_FFN_GELU, LLM_FFN_PAR, il);82            cb(cur, "ffn_out", il);83        }84        cur = build_norm(cur,85                model.layers[il].ffn_post_norm, NULL,86                LLM_NORM_RMS, -1);87        cb(cur, "ffn_post_norm", -1);88 89        cur = ggml_add(ctx0, cur, sa_out);90 91        cur = build_cvec(cur, il);92        cb(cur, "l_out", il);93 94        // input for next layer95        inpL = cur;96    }97    cur = inpL;98 99    cur = build_norm(cur,100            model.output_norm, NULL,101            LLM_NORM_RMS, -1);102 103    cb(cur, "result_norm", -1);104    res->t_embd = cur;105 106    // lm_head107    cur = build_lora_mm(model.output, cur);108 109    // final logit soft-capping110    cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);111    cur = ggml_tanh(ctx0, cur);112    cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);113 114    cb(cur, "result_output", -1);115    res->t_logits = cur;116 117    ggml_build_forward_expand(gf, cur);118}119