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.cpp102 linesDownload Raw Back to models
1#include "models.h"2 3llm_build_gemma::llm_build_gemma(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {4    const int64_t n_embd_head = hparams.n_embd_head_v();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();18 19    ggml_tensor * inp_out_ids = build_inp_out_ids();20 21    for (int il = 0; il < n_layer; ++il) {22        // norm23        cur = build_norm(inpL,24                model.layers[il].attn_norm, NULL,25                LLM_NORM_RMS, il);26        cb(cur, "attn_norm", il);27 28        // self-attention29        {30            // compute Q and K and RoPE them31            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,32                    n_embd_head, n_head, n_head_kv, il);33 34            Qcur = ggml_rope_ext(35                    ctx0, Qcur, inp_pos, nullptr,36                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,37                    ext_factor, attn_factor, beta_fast, beta_slow);38 39            Kcur = ggml_rope_ext(40                    ctx0, Kcur, inp_pos, nullptr,41                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,42                    ext_factor, attn_factor, beta_fast, beta_slow);43 44            cb(Qcur, "Qcur", il);45            cb(Kcur, "Kcur", il);46            cb(Vcur, "Vcur", il);47 48            Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head)));49            cb(Qcur, "Qcur_scaled", il);50 51            cur = build_attn(inp_attn,52                    model.layers[il].wo, NULL, model.layers[il].wo_s,53                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);54        }55        if (il == n_layer - 1 && inp_out_ids) {56            cur  = ggml_get_rows(ctx0,  cur, inp_out_ids);57            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);58        }59        ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);60        cb(sa_out, "sa_out", il);61 62        cur = build_norm(sa_out,63                model.layers[il].ffn_norm, NULL,64                LLM_NORM_RMS, il);65        cb(cur, "ffn_norm", il);66 67        // feed-forward network68        {69            cur = build_ffn(cur,70                    model.layers[il].ffn_up,   NULL, NULL,71                    model.layers[il].ffn_gate, NULL, NULL,72                    model.layers[il].ffn_down, NULL, NULL,73                    NULL,74                    LLM_FFN_GELU, LLM_FFN_PAR, il);75            cb(cur, "ffn_out", il);76        }77        cur = ggml_add(ctx0, cur, sa_out);78 79        cur = build_cvec(cur, il);80        cb(cur, "l_out", il);81 82        // input for next layer83        inpL = cur;84    }85    cur = inpL;86 87    cur = build_norm(cur,88            model.output_norm, NULL,89            LLM_NORM_RMS, -1);90 91    cb(cur, "result_norm", -1);92    res->t_embd = cur;93 94    // lm_head95    cur = build_lora_mm(model.output, cur);96 97    cb(cur, "result_output", -1);98    res->t_logits = cur;99 100    ggml_build_forward_expand(gf, cur);101}102