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/gemma4-assistant.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_gemma4_assistant::load_arch_hparams(llama_model_loader & ml) {
4
0
    hparams.n_embd_inp_impl = hparams.n_embd_out();
5
6
0
    hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
7
0
    ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, hparams.is_swa_impl, hparams.n_layer());
8
9
0
    uint32_t n_kv_shared_layers = 0;
10
0
    ml.get_key(LLM_KV_ATTENTION_SHARED_KV_LAYERS, n_kv_shared_layers, false);
11
12
0
    hparams.f_attention_scale = 1.0f;
13
14
0
    ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.n_layer_nextn, false);
15
0
    GGML_ASSERT(hparams.n_layer_nextn == hparams.n_layer_all && "n_layer_nextn must be == n_layer_impl");
16
17
0
    ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA,           hparams.rope_freq_base_train_swa, false);
18
0
    ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW,     hparams.n_swa);
19
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS,  hparams.f_norm_rms_eps);
20
0
    ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH_SWA,     hparams.n_embd_head_k_swa);
21
0
    ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH_SWA,   hparams.n_embd_head_v_swa);
22
0
}
23
24
0
void llama_model_gemma4_assistant::load_arch_tensors(llama_model_loader &) {
25
0
    LLAMA_LOAD_LOCALS;
26
27
0
    if (n_embd_head_k != n_embd_head_v) {
28
0
        throw std::runtime_error("Gemma 4 assistant requires n_embd_head_k == n_embd_head_v");
29
0
    }
30
0
    if (hparams.n_embd_head_k_swa != hparams.n_embd_head_v_swa) {
31
0
        throw std::runtime_error("Gemma 4 assistant requires n_embd_head_k_swa == n_embd_head_v_swa");
32
0
    }
33
0
    if (hparams.n_embd_out() == n_embd) {
34
0
        throw std::runtime_error("Gemma 4 assistant requires embedding_length_out to carry the target hidden size");
35
0
    }
36
37
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
38
0
    output   = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);
39
40
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
41
42
0
    create_tensor(tn(LLM_TENSOR_MASKED_EMBD_CENTROIDS, "weight"), {}, TENSOR_NOT_REQUIRED);
43
0
    create_tensor(tn(LLM_TENSOR_MASKED_EMBD_ORDERING),  {}, TENSOR_NOT_REQUIRED);
44
45
0
    const int64_t n_embd_backbone = hparams.n_embd_inp();
46
0
    nextn_proj_post = create_tensor(tn(LLM_TENSOR_NEXTN_PROJ_POST, "weight"), { n_embd, n_embd_backbone }, 0);
47
48
0
    int rope_freqs_flag = 0;
49
50
0
    for (int i = 0; i < n_layer_nextn; ++i) {
51
0
        auto & layer = layers[i];
52
53
0
        const int64_t n_head      = hparams.n_head(i);
54
0
        const int64_t n_embd_head = hparams.n_embd_head_k(i);
55
0
        const int64_t n_ff        = hparams.n_ff(i);
56
57
0
        if (i == 0) {
58
0
            nextn_proj_pre = create_tensor(tn(LLM_TENSOR_NEXTN_PROJ_PRE, "weight", i), { 2*n_embd_backbone, n_embd }, 0);
59
0
        }
60
61
0
        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
62
0
        layer.wq        = create_tensor(tn(LLM_TENSOR_ATTN_Q,    "weight", i), { n_embd, n_embd_head*n_head }, 0);
63
0
        layer.wo        = create_tensor(tn(LLM_TENSOR_ATTN_OUT,  "weight", i), { n_embd_head*n_head, n_embd }, 0);
64
65
0
        layer.attn_q_norm    = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM,    "weight", i), { n_embd_head }, 0);
66
0
        layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), { n_embd }, 0);
67
68
0
        layer.out_scale = create_tensor(tn(LLM_TENSOR_LAYER_OUT_SCALE, "weight", i), { 1u }, 0);
69
70
0
        if (!hparams.is_swa(i)) {
71
0
            layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), { n_embd_head/2 }, rope_freqs_flag);
72
0
            rope_freqs_flag = TENSOR_DUPLICATED;
73
0
        }
74
75
0
        layer.ffn_norm      = create_tensor(tn(LLM_TENSOR_FFN_NORM,      "weight", i), { n_embd }, 0);
76
0
        layer.ffn_gate      = create_tensor(tn(LLM_TENSOR_FFN_GATE,      "weight", i), { n_embd, n_ff }, 0);
77
0
        layer.ffn_up        = create_tensor(tn(LLM_TENSOR_FFN_UP,        "weight", i), { n_embd, n_ff }, 0);
78
0
        layer.ffn_down      = create_tensor(tn(LLM_TENSOR_FFN_DOWN,      "weight", i), { n_ff, n_embd }, 0);
79
0
        layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), { n_embd }, 0);
80
0
    }
81
0
}
82
83
0
std::unique_ptr<llm_graph_context> llama_model_gemma4_assistant::build_arch_graph(const llm_graph_params & params) const {
84
0
    return std::make_unique<graph>(*this, params);
85
0
}
86
87
llama_model_gemma4_assistant::graph::graph(const llama_model & model, const llm_graph_params & params) :
88
0
        llm_graph_context(params) {
89
0
    const int64_t n_embd_backbone = hparams.n_embd_inp();
90
91
0
    ggml_tensor * inp_tokens;
92
0
    ggml_tensor * inp_h;
93
0
    {
94
0
        auto inp = std::make_unique<llm_graph_input_embd>(n_embd_backbone);
95
96
0
        inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, ubatch.n_tokens);
97
0
        cb(inp->tokens, "inp_tokens", -1);
98
0
        ggml_set_input(inp->tokens);
99
0
        inp_tokens = inp->tokens;
100
0
        res->t_inp_tokens = inp->tokens;
101
102
0
        inp->embd = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_embd_backbone, ubatch.n_tokens);
103
0
        cb(inp->embd, "inp_h", -1);
104
0
        ggml_set_input(inp->embd);
105
0
        inp_h = inp->embd;
106
0
        res->t_inp_embd = inp->embd;
107
108
0
        res->add_input(std::move(inp));
109
0
    }
110
111
0
    GGML_ASSERT(cparams.ctx_other != nullptr);
112
0
    const auto * model_other = llama_get_model(cparams.ctx_other);
113
114
0
    ggml_tensor * x = ggml_get_rows(ctx0, model_other->tok_embd, inp_tokens);
115
0
    x = ggml_scale(ctx0, x, sqrtf((float) n_embd_backbone));
116
0
    cb(x, "inp_embd_target", -1);
117
118
0
    ggml_tensor * xh = ggml_concat(ctx0, x, inp_h, 0);
119
0
    cb(xh, "inp_xh", -1);
120
121
0
    ggml_tensor * cur = ggml_mul_mat(ctx0, model.nextn_proj_pre, xh);
122
0
    cb(cur, "pre_proj", -1);
123
124
0
    auto *        inp_attn    = build_attn_inp_kv_iswa();
125
0
    ggml_tensor * inp_pos     = build_inp_pos();
126
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
127
128
0
    ggml_tensor * inpL = cur;
129
130
0
    for (int il = 0; il < n_layer_nextn; ++il) {
131
0
        const bool is_swa = hparams.is_swa(il);
132
133
0
        const int64_t n_embd_head = hparams.n_embd_head_k(il);
134
0
        const int64_t n_head      = hparams.n_head(il);
135
136
0
        const float freq_base_l  = model.get_rope_freq_base(cparams, il);
137
0
        const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
138
0
        const int   n_rot_l      = hparams.n_rot(il);
139
140
0
        ggml_tensor * cur_norm = build_norm(inpL, model.layers[il].attn_norm, nullptr, LLM_NORM_RMS, il);
141
0
        cb(cur_norm, "attn_norm", il);
142
143
0
        ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur_norm);
144
0
        Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
145
0
        Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, nullptr, LLM_NORM_RMS, il);
146
0
        cb(Qcur, "Qcur_normed", il);
147
148
0
        ggml_tensor * freq_factors = is_swa ? nullptr : model.layers[il].rope_freqs;
149
0
        Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, freq_factors, n_rot_l, rope_type, n_ctx_orig,
150
0
                             freq_base_l, freq_scale_l, ext_factor, attn_factor, beta_fast, beta_slow);
151
0
        cb(Qcur, "Qcur_pos", il);
152
153
0
        cur = build_attn(inp_attn, model.layers[il].wo, nullptr, nullptr,
154
0
                Qcur, nullptr, nullptr, nullptr, nullptr, nullptr, hparams.f_attention_scale, il);
155
156
0
        if (il == n_layer_nextn - 1 && inp_out_ids) {
157
0
            cur  = ggml_get_rows(ctx0, cur,  inp_out_ids);
158
0
            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
159
0
        }
160
161
0
        cur = build_norm(cur, model.layers[il].attn_post_norm, nullptr, LLM_NORM_RMS, il);
162
0
        cb(cur, "attn_post_norm", il);
163
164
0
        ggml_tensor * attn_out = ggml_add(ctx0, cur, inpL);
165
0
        cb(attn_out, "attn_out", il);
166
167
0
        cur = build_norm(attn_out, model.layers[il].ffn_norm, nullptr, LLM_NORM_RMS, il);
168
0
        cb(cur, "ffn_norm", il);
169
170
0
        cur = build_ffn(cur,
171
0
                model.layers[il].ffn_up,   nullptr, nullptr,
172
0
                model.layers[il].ffn_gate, nullptr, nullptr,
173
0
                model.layers[il].ffn_down, nullptr, nullptr,
174
0
                nullptr,
175
0
                LLM_FFN_GELU, LLM_FFN_PAR, il);
176
0
        cb(cur, "ffn_out", il);
177
178
0
        cur = build_norm(cur, model.layers[il].ffn_post_norm, nullptr, LLM_NORM_RMS, -1);
179
0
        cb(cur, "ffn_post_norm", il);
180
181
0
        cur = ggml_add(ctx0, cur, attn_out);
182
183
0
        cur = ggml_mul(ctx0, cur, model.layers[il].out_scale);
184
0
        cb(cur, "out_scaled", il);
185
186
0
        inpL = cur;
187
0
    }
188
0
    cur = inpL;
189
190
0
    cur = build_norm(cur, model.output_norm, nullptr, LLM_NORM_RMS, -1);
191
0
    cb(cur, "result_norm", -1);
192
193
0
    ggml_tensor * logits = build_lora_mm(model.output, cur);
194
0
    cb(logits, "result_output", -1);
195
0
    res->t_logits = logits;
196
197
0
    ggml_tensor * h_next = ggml_mul_mat(ctx0, model.nextn_proj_post, cur);
198
0
    cb(h_next, "h_nextn", -1);
199
0
    res->t_h_nextn = h_next;
200
201
0
    ggml_build_forward_expand(gf, logits);
202
0
    ggml_build_forward_expand(gf, h_next);
203
0
}