Coverage Report

Created: 2026-01-09 06:17

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
template <bool iswa>
4
0
llm_build_gemma3<iswa>::llm_build_gemma3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
5
0
    const int64_t n_embd_head = hparams.n_embd_head_k;
6
7
0
    ggml_tensor * cur;
8
0
    ggml_tensor * inpL;
9
10
0
    inpL = build_inp_embd(model.tok_embd);
11
12
    // important: do not normalize weights for raw embeddings input (i.e. encoded image emdeddings)
13
0
    inpL = ggml_scale(ctx0, inpL, ubatch.token ? sqrtf(n_embd) : 1.0f);
14
0
    cb(inpL, "inp_scaled", -1);
15
16
    // inp_pos - contains the positions
17
0
    ggml_tensor * inp_pos = build_inp_pos();
18
19
    // TODO: is causal == true correct? might need some changes
20
0
    using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
21
0
    inp_attn_type * inp_attn = nullptr;
22
23
0
    if constexpr (iswa) {
24
0
        inp_attn = build_attn_inp_kv_iswa();
25
0
    } else {
26
0
        inp_attn = build_attn_inp_kv();
27
0
    }
28
29
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
30
31
0
    for (int il = 0; il < n_layer; ++il) {
32
0
        float freq_base_l  = 0.0f;
33
0
        float freq_scale_l = 0.0f;
34
35
0
        if constexpr (iswa) {
36
0
            freq_base_l  = model.get_rope_freq_base (cparams, il);
37
0
            freq_scale_l = model.get_rope_freq_scale(cparams, il);
38
0
        } else {
39
0
            freq_base_l  = freq_base;
40
0
            freq_scale_l = freq_scale;
41
0
        }
42
43
        // norm
44
0
        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
45
0
        cb(cur, "attn_norm", il);
46
47
        // self-attention
48
0
        {
49
            // compute Q and K and RoPE them
50
0
            ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
51
0
            cb(Qcur, "Qcur", il);
52
53
0
            ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
54
0
            cb(Kcur, "Kcur", il);
55
56
0
            ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
57
0
            cb(Vcur, "Vcur", il);
58
59
0
            Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head,    n_tokens);
60
0
            Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
61
0
            Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
62
63
0
            Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
64
0
            cb(Qcur, "Qcur_normed", il);
65
66
0
            Qcur = ggml_rope_ext(
67
0
                    ctx0, Qcur, inp_pos, nullptr,
68
0
                    n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
69
0
                    ext_factor, attn_factor, beta_fast, beta_slow);
70
71
0
            Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
72
0
            cb(Kcur, "Kcur_normed", il);
73
74
0
            Kcur = ggml_rope_ext(
75
0
                    ctx0, Kcur, inp_pos, nullptr,
76
0
                    n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
77
0
                    ext_factor, attn_factor, beta_fast, beta_slow);
78
79
0
            cb(Qcur, "Qcur", il);
80
0
            cb(Kcur, "Kcur", il);
81
0
            cb(Vcur, "Vcur", il);
82
83
            // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/model.py#L315
84
0
            Qcur = ggml_scale(ctx0, Qcur, hparams.f_attention_scale);
85
86
0
            cur = build_attn(inp_attn,
87
0
                    model.layers[il].wo, NULL,
88
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
89
0
        }
90
0
        if (il == n_layer - 1 && inp_out_ids) {
91
0
            cur  = ggml_get_rows(ctx0,  cur, inp_out_ids);
92
0
            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
93
0
        }
94
0
        cur = build_norm(cur,
95
0
                model.layers[il].attn_post_norm, NULL,
96
0
                LLM_NORM_RMS, il);
97
0
        cb(cur, "attn_post_norm", il);
98
99
0
        ggml_tensor * sa_out = ggml_add(ctx0, cur, inpL);
100
0
        cb(sa_out, "sa_out", il);
101
102
0
        cur = build_norm(sa_out,
103
0
                model.layers[il].ffn_norm, NULL,
104
0
                LLM_NORM_RMS, il);
105
0
        cb(cur, "ffn_norm", il);
106
107
        // feed-forward network
108
0
        {
109
0
            cur = build_ffn(cur,
110
0
                    model.layers[il].ffn_up,   NULL, NULL,
111
0
                    model.layers[il].ffn_gate, NULL, NULL,
112
0
                    model.layers[il].ffn_down, NULL, NULL,
113
0
                    NULL,
114
0
                    LLM_FFN_GELU, LLM_FFN_PAR, il);
115
0
            cb(cur, "ffn_out", il);
116
0
        }
117
0
        cur = build_norm(cur,
118
0
                model.layers[il].ffn_post_norm, NULL,
119
0
                LLM_NORM_RMS, -1);
120
0
        cb(cur, "ffn_post_norm", il);
121
122
0
        cur = ggml_add(ctx0, cur, sa_out);
123
124
0
        cur = build_cvec(cur, il);
125
0
        cb(cur, "l_out", il);
126
127
        // input for next layer
128
0
        inpL = cur;
129
0
    }
130
0
    cur = inpL;
131
132
0
    cur = build_norm(cur,
133
0
            model.output_norm, NULL,
134
0
            LLM_NORM_RMS, -1);
135
136
0
    cb(cur, "result_norm", -1);
137
0
    res->t_embd = cur;
138
139
    // lm_head
140
0
    cur = build_lora_mm(model.output, cur);
141
142
0
    if (hparams.f_final_logit_softcapping) {
143
0
        cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
144
0
        cur = ggml_tanh(ctx0, cur);
145
0
        cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
146
0
    }
147
148
0
    cb(cur, "result_output", -1);
149
0
    res->t_logits = cur;
150
151
0
    ggml_build_forward_expand(gf, cur);
152
0
}
Unexecuted instantiation: llm_build_gemma3<false>::llm_build_gemma3(llama_model const&, llm_graph_params const&)
Unexecuted instantiation: llm_build_gemma3<true>::llm_build_gemma3(llama_model const&, llm_graph_params const&)
153
154
template struct llm_build_gemma3<false>;
155
template struct llm_build_gemma3<true>;