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/lfm2.cpp
Line
Count
Source
1
#include "models.h"
2
#include "../llama-memory-hybrid-iswa.h"
3
#include "../llama-memory-hybrid.h"
4
5
0
void llama_model_lfm2::load_arch_hparams(llama_model_loader & ml) {
6
0
    ml.get_key(LLM_KV_SHORTCONV_L_CACHE,           hparams.n_shortconv_l_cache);
7
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
8
9
0
    for (uint32_t il = 0; il < hparams.n_layer(); ++il) {
10
0
        hparams.is_recr_impl[il] = hparams.n_head_kv(il) == 0;
11
0
    }
12
13
0
    hparams.n_layer_dense_lead = hparams.n_layer();
14
15
0
    switch (hparams.n_ff()) {
16
0
        case  4608: type = LLM_TYPE_350M; break;
17
0
        case  6912: type = LLM_TYPE_700M; break;
18
0
        case  8192: type = LLM_TYPE_1_2B; break;
19
0
        case 10752: type = LLM_TYPE_2_6B; break;
20
0
        default:    type = LLM_TYPE_UNKNOWN;
21
0
    }
22
23
0
    if (const auto is_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); is_swa && hparams.n_swa > 0) {
24
0
        hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
25
0
        for (uint32_t il = 0; il < hparams.n_layer(); ++il) {
26
0
            hparams.is_swa_impl[il] = !hparams.is_recr_impl[il];
27
0
        }
28
0
    }
29
0
}
30
31
0
void llama_model_lfm2::load_arch_tensors(llama_model_loader &) {
32
0
    LLAMA_LOAD_LOCALS;
33
34
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
35
36
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM_LFM2, "weight"), {n_embd}, 0);
37
0
    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,           "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
38
39
0
    if (output == NULL) {
40
0
        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
41
0
    }
42
43
0
    for (int i = 0; i < n_layer; ++i) {
44
0
        auto & layer = layers[i];
45
46
0
        const bool is_moe_layer = i >= static_cast<int>(hparams.n_layer_dense_lead);
47
48
        // ffn/moe is same for transformer and conv layers
49
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
50
0
        if (is_moe_layer) {
51
0
            GGML_ASSERT(n_expert && n_expert_used);
52
0
            layer.ffn_gate_inp    = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i),  {n_embd, n_expert}, 0);
53
0
            layer.ffn_gate_exps   = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, hparams.n_ff_exp, n_expert}, 0);
54
0
            layer.ffn_down_exps   = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {hparams.n_ff_exp,   n_embd, n_expert}, 0);
55
0
            layer.ffn_up_exps     = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i),   {n_embd, hparams.n_ff_exp, n_expert}, 0);
56
0
            layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, 0);
57
0
        } else {  // dense
58
0
            layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);
59
0
            layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
60
0
            layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
61
0
        }
62
63
        // for operator_norm
64
0
        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
65
66
0
        if (!hparams.is_recr(i)) {
67
0
            layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
68
0
            layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
69
0
            GGML_ASSERT(n_embd_v_gqa == n_embd_k_gqa);
70
71
0
            create_tensor_qkv(layer, i, n_embd, n_embd, hparams.n_embd_k_gqa(i), hparams.n_embd_v_gqa(i), 0);
72
73
0
            layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
74
0
        } else {
75
0
            layer.shortconv.conv     = create_tensor(tn(LLM_TENSOR_SHORTCONV_CONV,    "weight", i), {hparams.n_shortconv_l_cache, n_embd}, 0);
76
0
            layer.shortconv.in_proj  = create_tensor(tn(LLM_TENSOR_SHORTCONV_INPROJ,  "weight", i), {n_embd, 3 * n_embd}, 0);
77
0
            layer.shortconv.out_proj = create_tensor(tn(LLM_TENSOR_SHORTCONV_OUTPROJ, "weight", i), {n_embd, n_embd}, 0);
78
0
        }
79
0
    }
80
81
    // for LFM2-ColBert-350M
82
0
    dense_2_out_layers   = create_tensor(tn(LLM_TENSOR_DENSE_2_OUT, "weight"), {n_embd, hparams.n_embd_out()}, TENSOR_NOT_REQUIRED);
83
0
    dense_2_out_layers_b = create_tensor(tn(LLM_TENSOR_DENSE_2_OUT, "bias"),   {hparams.n_embd_out()        }, TENSOR_NOT_REQUIRED);
84
0
}
85
86
0
std::unique_ptr<llm_graph_context> llama_model_lfm2::build_arch_graph(const llm_graph_params & params) const {
87
0
    if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) {
88
0
        return std::make_unique<graph<true>>(*this, params);
89
0
    } else {
90
0
        return std::make_unique<graph<false>>(*this, params);
91
0
    }
92
0
}
93
94
template <bool iswa>
95
llama_model_lfm2::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) :
96
0
    llm_graph_context(params) {
97
0
    using inp_hybrid_type = std::conditional_t<iswa, llm_graph_input_mem_hybrid_iswa,  llm_graph_input_mem_hybrid>;
98
0
    using inp_attn_type   = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa,     llm_graph_input_attn_kv>;
99
0
    using mem_hybrid_ctx  = std::conditional_t<iswa, llama_memory_hybrid_iswa_context, llama_memory_hybrid_context>;
100
101
    // lambda helpers for readability
102
0
    auto build_dense_feed_forward = [&model, this](ggml_tensor * cur, int il) -> ggml_tensor * {
103
0
        GGML_ASSERT(!model.layers[il].ffn_up_b);
104
0
        GGML_ASSERT(!model.layers[il].ffn_gate_b);
105
0
        GGML_ASSERT(!model.layers[il].ffn_down_b);
106
0
        return build_ffn(cur,
107
0
            model.layers[il].ffn_up, NULL, NULL,
108
0
            model.layers[il].ffn_gate, NULL, NULL,
109
0
            model.layers[il].ffn_down, NULL, NULL,
110
0
            NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
111
0
    };
Unexecuted instantiation: llama_model_lfm2::graph<true>::graph(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, int)#1}::operator()(ggml_tensor*, int) const
Unexecuted instantiation: llama_model_lfm2::graph<false>::graph(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, int)#1}::operator()(ggml_tensor*, int) const
112
0
    auto build_moe_feed_forward = [&model, this](ggml_tensor * cur, int il) -> ggml_tensor * {
113
0
        return build_moe_ffn(cur,
114
0
                model.layers[il].ffn_gate_inp,
115
0
                model.layers[il].ffn_up_exps,
116
0
                model.layers[il].ffn_gate_exps,
117
0
                model.layers[il].ffn_down_exps,
118
0
                model.layers[il].ffn_exp_probs_b,
119
0
                n_expert, n_expert_used,
120
0
                LLM_FFN_SILU, true,
121
0
                hparams.expert_weights_scale,
122
0
                static_cast<llama_expert_gating_func_type>(hparams.expert_gating_func),
123
0
                il);
124
0
    };
Unexecuted instantiation: llama_model_lfm2::graph<true>::graph(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, int)#2}::operator()(ggml_tensor*, int) const
Unexecuted instantiation: llama_model_lfm2::graph<false>::graph(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, int)#2}::operator()(ggml_tensor*, int) const
125
0
    auto build_attn_block = [&model, this](ggml_tensor *   cur,
126
0
                                           ggml_tensor *   inp_pos,
127
0
                                           inp_attn_type * inp_attn,
128
0
                                           int             il) -> ggml_tensor * {
129
0
        GGML_ASSERT(hparams.n_embd_v_gqa(il) == hparams.n_embd_k_gqa(il));
130
0
        const auto n_embd_head = hparams.n_embd_head_v();
131
0
        const auto n_head_kv   = hparams.n_head_kv(il);
132
133
0
        auto [q, k, v] = build_qkv(model.layers[il], cur,
134
0
                n_embd_head, n_head, n_head_kv, il);
135
136
        // qk norm
137
0
        q = build_norm(q, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
138
0
        cb(q, "model.layers.{}.self_attn.q_layernorm", il);
139
0
        k = build_norm(k, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
140
0
        cb(k, "model.layers.{}.self_attn.k_layernorm", il);
141
142
        // RoPE
143
0
        q = ggml_rope_ext(ctx0, q, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, ext_factor,
144
0
                          attn_factor, beta_fast, beta_slow);
145
0
        k = ggml_rope_ext(ctx0, k, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, ext_factor,
146
0
                          attn_factor, beta_fast, beta_slow);
147
148
0
        cur = build_attn(inp_attn,
149
0
                model.layers[il].wo, NULL, model.layers[il].wo_s,
150
0
                q, k, v, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
151
152
0
        cb(cur, "model.layers.{}.self_attn.out_proj", il);
153
154
0
        return cur;
155
0
    };
Unexecuted instantiation: llama_model_lfm2::graph<true>::graph(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, ggml_tensor*, llm_graph_input_attn_kv_iswa*, int)#1}::operator()(ggml_tensor*, ggml_tensor*, llm_graph_input_attn_kv_iswa*, int) const
Unexecuted instantiation: llama_model_lfm2::graph<false>::graph(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, ggml_tensor*, llm_graph_input_attn_kv*, int)#1}::operator()(ggml_tensor*, ggml_tensor*, llm_graph_input_attn_kv*, int) const
156
0
    auto build_shortconv_block = [&model, this](ggml_tensor *        cur,
157
0
                                                llm_graph_input_rs * inp_recr,
158
0
                                                int                  il) -> ggml_tensor * {
159
0
        const auto * mctx_cur = static_cast<const mem_hybrid_ctx *>(mctx)->get_recr();
160
0
        const uint32_t kv_head      = mctx_cur->get_head();
161
0
        const int64_t  n_seq_tokens = ubatch.n_seq_tokens;
162
0
        const int64_t  n_seqs       = ubatch.n_seqs;
163
0
        GGML_ASSERT(n_seqs != 0);
164
0
        GGML_ASSERT(ubatch.equal_seqs());
165
0
        GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
166
167
0
        GGML_ASSERT(hparams.n_shortconv_l_cache > 1);
168
0
        const uint32_t d_conv = hparams.n_shortconv_l_cache - 1;
169
170
        // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
171
0
        cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
172
173
0
        auto * bcx = build_lora_mm(model.layers[il].shortconv.in_proj, cur);
174
0
        cb(bcx, "model.layers.{}.conv.in_proj", il);
175
176
0
        constexpr auto n_chunks = 3;
177
0
        GGML_ASSERT(bcx->ne[0] % n_chunks == 0);
178
0
        const auto chunk_size = bcx->ne[0] / n_chunks;
179
0
        auto *     b          = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2],
180
0
                                             0 * chunk_size * ggml_element_size(bcx));
181
0
        auto *     c          = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2],
182
0
                                             1 * chunk_size * ggml_element_size(bcx));
183
0
        auto *     x          = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2],
184
0
                                             2 * chunk_size * ggml_element_size(bcx));
185
186
0
        auto * bx = ggml_transpose(ctx0, ggml_mul(ctx0, b, x));
187
188
        // read conv state
189
0
        auto * conv_state = mctx_cur->get_r_l(il);
190
0
        auto * conv_rs    = build_rs(inp_recr, conv_state, hparams.n_embd_r(), n_seqs);
191
0
        auto * conv       = ggml_reshape_3d(ctx0, conv_rs, d_conv, hparams.n_embd, n_seqs);
192
193
0
        bx = ggml_concat(ctx0, conv, bx, 0);
194
0
        GGML_ASSERT(bx->ne[0] > conv->ne[0]);
195
196
        // last d_conv columns is a new conv state
197
0
        auto * new_conv = ggml_view_3d(ctx0, bx, conv->ne[0], bx->ne[1], bx->ne[2], bx->nb[1], bx->nb[2],
198
0
                                       (bx->ne[0] - conv->ne[0]) * ggml_element_size(bx));
199
0
        GGML_ASSERT(ggml_are_same_shape(conv, new_conv));
200
201
        // write new conv conv state
202
0
        ggml_build_forward_expand(gf, ggml_cpy(ctx0, new_conv,
203
0
                                               ggml_view_1d(ctx0, conv_state, ggml_nelements(new_conv),
204
0
                                                            kv_head * d_conv * n_embd * ggml_element_size(new_conv))));
205
206
0
        auto * conv_kernel = model.layers[il].shortconv.conv;
207
0
        auto * conv_out    = ggml_ssm_conv(ctx0, bx, conv_kernel);
208
0
        cb(conv_out, "model.layers.{}.conv.conv", il);
209
210
0
        auto * y = ggml_mul(ctx0, c, conv_out);
211
0
        y        = build_lora_mm(model.layers[il].shortconv.out_proj, y);
212
0
        cb(y, "model.layers.{}.conv.out_proj", il);
213
        // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
214
0
        y = ggml_reshape_2d(ctx0, y, y->ne[0], n_seq_tokens * n_seqs);
215
216
0
        return y;
217
0
    };
Unexecuted instantiation: llama_model_lfm2::graph<true>::graph(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, llm_graph_input_rs*, int)#1}::operator()(ggml_tensor*, llm_graph_input_rs*, int) const
Unexecuted instantiation: llama_model_lfm2::graph<false>::graph(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, llm_graph_input_rs*, int)#1}::operator()(ggml_tensor*, llm_graph_input_rs*, int) const
218
219
    // actual graph construction starts here
220
0
    ggml_tensor * cur = build_inp_embd(model.tok_embd);
221
0
    cb(cur, "model.embed_tokens", -1);
222
223
0
    ggml_build_forward_expand(gf, cur);
224
225
0
    inp_hybrid_type * inp_hybrid = nullptr;
226
0
    if constexpr (iswa) {
227
0
        inp_hybrid = build_inp_mem_hybrid_iswa();
228
0
    } else {
229
0
        inp_hybrid = build_inp_mem_hybrid();
230
0
    }
231
232
0
    ggml_tensor * inp_pos     = build_inp_pos();
233
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
234
235
0
    for (int il = 0; il < n_layer; ++il) {
236
0
        const bool is_moe_layer = il >= static_cast<int>(hparams.n_layer_dense_lead);
237
238
0
        auto * prev_cur = cur;
239
0
        cur             = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
240
0
        cb(cur, "model.layers.{}.operator_norm", il);
241
242
0
        cur = hparams.is_recr(il) ? build_shortconv_block(cur, inp_hybrid->get_recr(), il) :
243
0
                                    build_attn_block(cur, inp_pos, inp_hybrid->get_attn(), il);
244
245
0
        if (il == n_layer - 1 && inp_out_ids) {
246
0
            cur      = ggml_get_rows(ctx0, cur, inp_out_ids);
247
0
            prev_cur = ggml_get_rows(ctx0, prev_cur, inp_out_ids);
248
0
        }
249
250
0
        cur = ggml_add(ctx0, prev_cur, cur);
251
252
0
        auto * ffn_norm_out = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
253
0
        cb(ffn_norm_out, "model.layers.{}.ffn_norm", il);
254
255
0
        ggml_tensor * ffn_out =
256
0
            is_moe_layer ? build_moe_feed_forward(ffn_norm_out, il) : build_dense_feed_forward(ffn_norm_out, il);
257
0
        cb(ffn_norm_out, "model.layers.{}.ffn_out", il);
258
259
0
        cur = ggml_add(ctx0, cur, ffn_out);
260
261
0
        cur = build_cvec(cur, il);
262
0
        cb(cur, "l_out", il);
263
0
    }
264
265
0
    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
266
0
    cb(cur, "result_norm", -1);
267
0
    res->t_embd = cur;
268
269
0
    cur = build_lora_mm(model.output, cur, model.output_s);
270
0
    cb(cur, "result_output", -1);
271
272
0
    res->t_logits = cur;
273
274
0
    ggml_build_forward_expand(gf, cur);
275
0
}
Unexecuted instantiation: llama_model_lfm2::graph<true>::graph(llama_model const&, llm_graph_params const&)
Unexecuted instantiation: llama_model_lfm2::graph<false>::graph(llama_model const&, llm_graph_params const&)
276
277
// Explicit template instantiations
278
template struct llama_model_lfm2::graph<true>;
279
template struct llama_model_lfm2::graph<false>;