Coverage Report

Created: 2026-06-22 06:47

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