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/llama4.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_llama4::load_arch_hparams(llama_model_loader & ml) {
4
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
5
0
    ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH,  hparams.n_ff_exp);
6
0
    ml.get_key(LLM_KV_INTERLEAVE_MOE_LAYER_STEP,   hparams.n_moe_layer_step);
7
8
0
    const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
9
0
    if (found_swa && hparams.n_swa == 0) {
10
0
        hparams.swa_type             = LLAMA_SWA_TYPE_NONE;
11
0
        hparams.n_no_rope_layer_step = hparams.n_layer(); // always use rope
12
0
    } else {
13
0
        hparams.swa_type                = LLAMA_SWA_TYPE_CHUNKED;
14
0
        hparams.n_swa                   = 8192;
15
0
        hparams.n_attn_temp_floor_scale = 8192;
16
0
        hparams.f_attn_temp_scale       = 0.1f;
17
0
        hparams.f_attn_temp_offset      = 1.0f;
18
19
0
        uint32_t swa_period = 4; // pattern: 3 chunked - 1 full
20
0
        ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false);
21
0
        hparams.set_swa_pattern(swa_period);
22
23
0
        hparams.rope_freq_base_train_swa  = hparams.rope_freq_base_train;
24
0
        hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;
25
0
        ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);
26
0
    }
27
28
0
    switch (hparams.n_expert) {
29
0
        case 0: {
30
            // MobileLLM (no MoE)
31
0
            switch (hparams.n_embd) {
32
0
                case 2048: type = LLM_TYPE_140M; break;
33
0
                case 4096: type = LLM_TYPE_360M; break;
34
0
                case 6144: type = LLM_TYPE_950M; break;
35
0
                default:   type = LLM_TYPE_UNKNOWN;
36
0
            }
37
0
        } break;
38
0
        case 16:  type = LLM_TYPE_17B_16E; break;
39
0
        case 128: type = LLM_TYPE_17B_128E; break;
40
0
        default:  type = LLM_TYPE_UNKNOWN;
41
0
    }
42
43
0
    hparams.use_kq_norm = type != LLM_TYPE_17B_128E;
44
0
}
45
46
0
void llama_model_llama4::load_arch_tensors(llama_model_loader &) {
47
0
    LLAMA_LOAD_LOCALS;
48
49
0
    if (n_expert == 0) {
50
0
        throw std::runtime_error(arch_name() + " model cannot have zero experts");
51
0
    }
52
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
53
54
    // output
55
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
56
0
    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
57
58
    // if output is NULL, init from the input tok embed
59
0
    if (output == NULL) {
60
0
        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
61
0
    }
62
63
0
    for (int i = 0; i < n_layer; ++i) {
64
0
        const bool is_moe_layer = hparams.n_moe_layer_step > 0 && (i + 1) % hparams.n_moe_layer_step == 0;
65
66
0
        auto & layer = layers[i];
67
68
0
        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
69
70
0
        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0);
71
0
        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
72
73
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
74
75
0
        layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
76
77
0
        if (is_moe_layer) {
78
0
            const int64_t n_ff_exp = hparams.n_ff_exp;
79
80
0
            layer.ffn_gate_inp  = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP,  "weight", i), {n_embd, n_expert}, 0);
81
0
            layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd,   n_ff_exp, n_expert}, 0);
82
0
            layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {  n_ff_exp, n_embd, n_expert}, 0);
83
0
            layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {n_embd,   n_ff_exp, n_expert}, 0);
84
85
            // Shared expert
86
0
            const int64_t n_ff_shexp = n_ff_exp;
87
0
            layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {    n_embd, n_ff_shexp}, 0);
88
0
            layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd    }, 0);
89
0
            layer.ffn_up_shexp   = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP,   "weight", i), {    n_embd, n_ff_shexp}, 0);
90
0
        } else {
91
0
            layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);
92
0
            layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
93
0
            layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
94
0
        }
95
0
    }
96
0
}
97
98
0
std::unique_ptr<llm_graph_context> llama_model_llama4::build_arch_graph(const llm_graph_params & params) const {
99
0
    if (hparams.swa_type == LLAMA_SWA_TYPE_NONE) {
100
0
        return std::make_unique<graph<false>>(*this, params);
101
0
    } else {
102
0
        return std::make_unique<graph<true>>(*this, params);
103
0
    }
104
0
}
105
106
template <bool iswa>
107
0
llama_model_llama4::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
108
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
109
110
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
111
0
    GGML_ASSERT(n_embd_head == n_rot);
112
113
0
    ggml_tensor * cur;
114
0
    ggml_tensor * inpL;
115
116
0
    inpL = build_inp_embd(model.tok_embd);
117
118
    // inp_pos - contains the positions
119
0
    ggml_tensor * inp_pos = build_inp_pos();
120
121
    // temperature tuning
122
0
    ggml_tensor * inp_attn_scale = nullptr;
123
0
    inp_attn_scale = build_inp_attn_scale();
124
125
0
    using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
126
0
    inp_attn_type * inp_attn = nullptr;
127
128
0
    if constexpr (iswa) {
129
0
        inp_attn = build_attn_inp_kv_iswa();
130
0
    } else {
131
0
        inp_attn = build_attn_inp_kv();
132
0
    }
133
134
0
    const float kq_scale = hparams.f_attention_scale == 0.0f ? 1.0f/sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
135
136
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
137
138
0
    for (int il = 0; il < n_layer; ++il) {
139
0
        const float freq_base_l  = model.get_rope_freq_base (cparams, il);
140
0
        const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
141
142
0
        ggml_tensor * inpSA = inpL;
143
144
        // This overlaps with SWA layers in current models, so get_rope_freq_base/scale may be superfluous
145
0
        const bool use_rope = hparams.n_no_rope_layer_step > 0 &&
146
0
                              (il + 1) % hparams.n_no_rope_layer_step != 0;
147
148
        // norm
149
0
        cur = build_norm(inpL,
150
0
                model.layers[il].attn_norm, NULL,
151
0
                LLM_NORM_RMS, il);
152
0
        cb(cur, "attn_norm", il);
153
154
        // self-attention
155
0
        {
156
            // rope freq factors for llama3; may return nullptr for llama2 and other models
157
0
            ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
158
159
            // compute Q and K and RoPE them
160
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
161
0
                    n_embd_head, n_head, n_head_kv, il);
162
163
0
            if (use_rope) {
164
0
                Qcur = ggml_rope_ext(
165
0
                        ctx0, Qcur, inp_pos, rope_factors,
166
0
                        n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
167
0
                        ext_factor, attn_factor, beta_fast, beta_slow
168
0
                        );
169
170
0
                Kcur = ggml_rope_ext(
171
0
                        ctx0, Kcur, inp_pos, rope_factors,
172
0
                        n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
173
0
                        ext_factor, attn_factor, beta_fast, beta_slow
174
0
                        );
175
0
            } else if (inp_attn_scale) {
176
0
                Qcur = ggml_mul(ctx0, Qcur, inp_attn_scale);
177
0
            }
178
0
            cb(Qcur, "Qcur", il);
179
0
            cb(Kcur, "Kcur", il);
180
0
            cb(Vcur, "Vcur", il);
181
182
0
            if (use_rope && hparams.use_kq_norm) {
183
                // Llama4TextL2Norm
184
0
                Qcur = ggml_rms_norm(ctx0, Qcur, hparams.f_norm_rms_eps);
185
0
                Kcur = ggml_rms_norm(ctx0, Kcur, hparams.f_norm_rms_eps);
186
0
                cb(Qcur, "Qcur_normed", il);
187
0
                cb(Kcur, "Kcur_normed", il);
188
0
            }
189
0
            cur = build_attn(inp_attn,
190
0
                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
191
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
192
0
            cb(cur, "attn_out", il);
193
0
        }
194
0
        if (il == n_layer - 1 && inp_out_ids) {
195
0
            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
196
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
197
0
        }
198
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
199
0
        cb(ffn_inp, "ffn_inp", il);
200
201
        // feed-forward network (non-MoE)
202
0
        if (model.layers[il].ffn_gate_inp == nullptr) {
203
0
            cur = build_norm(ffn_inp,
204
0
                    model.layers[il].ffn_norm, NULL,
205
0
                    LLM_NORM_RMS, il);
206
0
            cb(cur, "ffn_norm", il);
207
208
0
            cur = build_ffn(cur,
209
0
                    model.layers[il].ffn_up,   model.layers[il].ffn_up_b,   NULL,
210
0
                    model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
211
0
                    model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
212
0
                    NULL,
213
0
                    LLM_FFN_SILU, LLM_FFN_PAR, il);
214
0
            cb(cur, "ffn_out", il);
215
0
        } else {
216
0
            ggml_tensor * ffn_inp_normed = build_norm(ffn_inp,
217
0
                    model.layers[il].ffn_norm, NULL,
218
0
                    LLM_NORM_RMS, il);
219
0
            cb(cur, "ffn_norm", il);
220
221
0
            ggml_tensor * moe_out = build_moe_ffn(ffn_inp_normed,
222
0
                    model.layers[il].ffn_gate_inp,
223
0
                    model.layers[il].ffn_up_exps,
224
0
                    model.layers[il].ffn_gate_exps,
225
0
                    model.layers[il].ffn_down_exps,
226
0
                    nullptr,
227
0
                    n_expert, n_expert_used,
228
0
                    LLM_FFN_SILU, false,
229
0
                    hparams.expert_weights_scale,
230
0
                    LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID,
231
0
                    il);
232
233
            // Shared experts
234
0
            ggml_tensor * shexp_out = build_ffn(ffn_inp_normed,
235
0
                model.layers[il].ffn_up_shexp,   NULL, NULL,
236
0
                model.layers[il].ffn_gate_shexp, NULL, NULL,
237
0
                model.layers[il].ffn_down_shexp, NULL, NULL,
238
0
                NULL,
239
0
                LLM_FFN_SILU, LLM_FFN_PAR, il);
240
0
            cb(shexp_out, "ffn_moe_shexp", il);
241
242
0
            cur = ggml_add(ctx0, moe_out, shexp_out);
243
0
            cb(cur, "ffn_moe_out_merged", il);
244
0
        }
245
0
        cur = ggml_add(ctx0, cur, ffn_inp);
246
0
        cb(cur, "ffn_out", il);
247
248
0
        cur = build_cvec(cur, il);
249
0
        cb(cur, "l_out", il);
250
251
        // input for next layer
252
0
        inpL = cur;
253
0
    }
254
0
    cur = inpL;
255
256
0
    cur = build_norm(cur,
257
0
            model.output_norm, NULL,
258
0
            LLM_NORM_RMS, -1);
259
260
0
    cb(cur, "result_norm", -1);
261
0
    res->t_embd = cur;
262
263
    // lm_head
264
0
    cur = build_lora_mm(model.output, cur, model.output_s);
265
266
0
    cb(cur, "result_output", -1);
267
0
    res->t_logits = cur;
268
269
0
    ggml_build_forward_expand(gf, cur);
270
0
}
Unexecuted instantiation: llama_model_llama4::graph<false>::graph(llama_model const&, llm_graph_params const&)
Unexecuted instantiation: llama_model_llama4::graph<true>::graph(llama_model const&, llm_graph_params const&)
271
272
// Explicit template instantiations
273
template struct llama_model_llama4::graph<false>;
274
template struct llama_model_llama4::graph<true>;