okami.mom/strata

git clone https://git.nkp.pet/okami.mom/strata

Niko1221Merge f-own-1b (033cfdd8c) into f-own-int: nothing downloads llama.cppb2de2641f

main
6.6 KiB175 linesraw
1#include "models.h"
2
3void llama_model_gemma2::load_arch_hparams(llama_model_loader & ml) {
4    hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
5    hparams.n_swa = 4096; // default value of gemma 2
6    load_swa_pattern(ml, 2);
7    hparams.attn_soft_cap = true;
8    hparams.rope_freq_base_train_swa  = hparams.rope_freq_base_train;
9    hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;
10
11    ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA,          hparams.rope_freq_base_train_swa, false);
12    ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW,    hparams.n_swa, false);
13    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
14    ml.get_key(LLM_KV_ATTN_LOGIT_SOFTCAPPING,      hparams.f_attn_logit_softcapping, false);
15    ml.get_key(LLM_KV_FINAL_LOGIT_SOFTCAPPING,     hparams.f_final_logit_softcapping, false);
16
17    switch (hparams.n_layer()) {
18        case 26: type = LLM_TYPE_2B; break;
19        case 42: type = LLM_TYPE_9B; break;
20        case 46: type = LLM_TYPE_27B; break;
21        default: type = LLM_TYPE_UNKNOWN;
22   }
23
24    // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/config.py#L173
25    hparams.f_attention_scale = type == LLM_TYPE_27B
26        ? 1.0f / std::sqrt(float(hparams.n_embd / hparams.n_head(0)))
27        : 1.0f / std::sqrt(float(hparams.n_embd_head_k()));
28}
29
30void llama_model_gemma2::load_arch_tensors(llama_model_loader &) {
31    LLAMA_LOAD_LOCALS;
32
33    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
34
35    // output
36    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
37    output      = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD,  "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // same as tok_embd, duplicated to allow offloading
38
39    for (int i = 0; i < n_layer; ++i) {
40        auto & layer = layers[i];
41
42        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
43
44        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0);
45        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
46        layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
47
48        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
49        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);
50        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
51        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
52        layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
53    }
54}
55
56std::unique_ptr<llm_graph_context> llama_model_gemma2::build_arch_graph(const llm_graph_params & params) const {
57    return std::make_unique<graph>(*this, params);
58}
59
60llama_model_gemma2::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
61    const int64_t n_embd_head = hparams.n_embd_head_k();
62
63    ggml_tensor * cur;
64    ggml_tensor * inpL;
65
66    inpL = build_inp_embd(model.tok_embd);
67
68    inpL = ggml_scale(ctx0, inpL, sqrtf(n_embd));
69    cb(inpL, "inp_scaled", -1);
70
71    // inp_pos - contains the positions
72    ggml_tensor * inp_pos = build_inp_pos();
73
74    auto * inp_attn = build_attn_inp_kv_iswa();
75
76    ggml_tensor * inp_out_ids = build_inp_out_ids();
77
78    for (int il = 0; il < n_layer; ++il) {
79        const float freq_base_l  = model.get_rope_freq_base (cparams, il);
80        const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
81
82        // norm
83        cur = build_norm(inpL,
84                model.layers[il].attn_norm, NULL,
85                LLM_NORM_RMS, il);
86        cb(cur, "attn_norm", il);
87
88        // self-attention
89        {
90            // compute Q and K and RoPE them
91            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
92                    n_embd_head, n_head, n_head_kv, il);
93
94            Qcur = ggml_rope_ext(
95                    ctx0, Qcur, inp_pos, nullptr,
96                    n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
97                    ext_factor, attn_factor, beta_fast, beta_slow);
98
99            Kcur = ggml_rope_ext(
100                    ctx0, Kcur, inp_pos, nullptr,
101                    n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
102                    ext_factor, attn_factor, beta_fast, beta_slow);
103
104            cb(Qcur, "Qcur", il);
105            cb(Kcur, "Kcur", il);
106            cb(Vcur, "Vcur", il);
107
108            Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);
109
110            cur = build_attn(inp_attn,
111                    model.layers[il].wo, NULL, model.layers[il].wo_s,
112                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
113        }
114        if (il == n_layer - 1 && inp_out_ids) {
115            cur  = ggml_get_rows(ctx0,  cur, inp_out_ids);
116            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
117        }
118        cur = build_norm(cur,
119                model.layers[il].attn_post_norm, NULL,
120                LLM_NORM_RMS, il);
121        cb(cur, "attn_post_norm", il);
122
123        ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
124        cb(sa_out, "sa_out", il);
125
126        cur = build_norm(sa_out,
127                model.layers[il].ffn_norm, NULL,
128                LLM_NORM_RMS, il);
129        cb(cur, "ffn_norm", il);
130
131        // feed-forward network
132        {
133            cur = build_ffn(cur,
134                    model.layers[il].ffn_up,   NULL, NULL,
135                    model.layers[il].ffn_gate, NULL, NULL,
136                    model.layers[il].ffn_down, NULL, NULL,
137                    NULL,
138                    LLM_FFN_GELU, LLM_FFN_PAR, il);
139            cb(cur, "ffn_out", il);
140        }
141        cur = build_norm(cur,
142                model.layers[il].ffn_post_norm, NULL,
143                LLM_NORM_RMS, -1);
144        cb(cur, "ffn_post_norm", -1);
145
146        cur = ggml_add(ctx0, cur, sa_out);
147
148        cur = build_cvec(cur, il);
149        cb(cur, "l_out", il);
150
151        // input for next layer
152        inpL = cur;
153    }
154    cur = inpL;
155
156    cur = build_norm(cur,
157            model.output_norm, NULL,
158            LLM_NORM_RMS, -1);
159
160    cb(cur, "result_norm", -1);
161    res->t_embd = cur;
162
163    // lm_head
164    cur = build_lora_mm(model.output, cur, model.output_s);
165
166    // final logit soft-capping
167    cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
168    cur = ggml_tanh(ctx0, cur);
169    cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
170
171    cb(cur, "result_output", -1);
172    res->t_logits = cur;
173
174    ggml_build_forward_expand(gf, cur);
175}