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
0likes610downloads
gemma-embedding.cpp107 linesDownload Raw Back to models
1#include "models.h"2 3llm_build_gemma_embedding::llm_build_gemma_embedding(const llama_model & model, const llm_graph_params & params) :4    llm_graph_context(params) {5    const int64_t n_embd_head = hparams.n_embd_head_k();6 7    ggml_tensor * cur;8    ggml_tensor * inpL;9 10    inpL = build_inp_embd(model.tok_embd);11 12    // important: do not normalize weights for raw embeddings input (i.e. encoded image embeddings)13    inpL = ggml_scale(ctx0, inpL, ubatch.token ? sqrtf(n_embd) : 1.0f);14    cb(inpL, "inp_scaled", -1);15 16    // inp_pos - contains the positions17    ggml_tensor * inp_pos = build_inp_pos();18 19    auto * inp_attn = build_attn_inp_no_cache();20 21    ggml_tensor * inp_out_ids = build_inp_out_ids();22 23    for (int il = 0; il < n_layer; ++il) {24        const float freq_base_l  = model.get_rope_freq_base(cparams, il);25        const float freq_scale_l = model.get_rope_freq_scale(cparams, il);26 27        // norm28        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, 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 = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);38            cb(Qcur, "Qcur_normed", il);39 40            Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,41                                 ext_factor, attn_factor, beta_fast, beta_slow);42 43            Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);44            cb(Kcur, "Kcur_normed", il);45 46            Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,47                                 ext_factor, attn_factor, beta_fast, beta_slow);48 49            cb(Qcur, "Qcur", il);50            cb(Kcur, "Kcur", il);51            cb(Vcur, "Vcur", il);52 53            // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/model.py#L31554            Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);55 56            cur =57                build_attn(inp_attn,58                    model.layers[il].wo, NULL, model.layers[il].wo_s,59                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);60        }61 62        if (il == n_layer - 1 && inp_out_ids) {63            cur  = ggml_get_rows(ctx0, cur, inp_out_ids);64            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);65        }66 67        cur = build_norm(cur, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il);68        cb(cur, "attn_post_norm", il);69 70        ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);71        cb(sa_out, "sa_out", il);72 73        cur = build_norm(sa_out, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);74        cb(cur, "ffn_norm", il);75 76        // feed-forward network77        {78            cur = build_ffn(cur,79                model.layers[il].ffn_up, NULL, NULL,80                model.layers[il].ffn_gate, NULL, NULL,81                model.layers[il].ffn_down, NULL, NULL,82                NULL, LLM_FFN_GELU, LLM_FFN_PAR, il);83            cb(cur, "ffn_out", il);84        }85 86        cur = build_norm(cur, model.layers[il].ffn_post_norm, NULL, 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 98    cur = inpL;99 100    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);101 102    cb(cur, "result_norm", -1);103    res->t_embd = cur;104 105    ggml_build_forward_expand(gf, cur);106}107