Coverage Report

Created: 2026-03-07 06:35

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
3
#include "../llama-memory-hybrid-iswa.h"
4
#include "../llama-memory-hybrid.h"
5
6
template <bool iswa>
7
llm_build_lfm2<iswa>::llm_build_lfm2(const llama_model & model, const llm_graph_params & params) :
8
0
    llm_graph_context(params) {
9
0
    using inp_hybrid_type = std::conditional_t<iswa, llm_graph_input_mem_hybrid_iswa,  llm_graph_input_mem_hybrid>;
10
0
    using inp_attn_type   = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa,     llm_graph_input_attn_kv>;
11
0
    using mem_hybrid_ctx  = std::conditional_t<iswa, llama_memory_hybrid_iswa_context, llama_memory_hybrid_context>;
12
13
    // lambda helpers for readability
14
0
    auto build_dense_feed_forward = [&model, this](ggml_tensor * cur, int il) -> ggml_tensor * {
15
0
        GGML_ASSERT(!model.layers[il].ffn_up_b);
16
0
        GGML_ASSERT(!model.layers[il].ffn_gate_b);
17
0
        GGML_ASSERT(!model.layers[il].ffn_down_b);
18
0
        return build_ffn(cur,
19
0
            model.layers[il].ffn_up, NULL, NULL,
20
0
            model.layers[il].ffn_gate, NULL, NULL,
21
0
            model.layers[il].ffn_down, NULL, NULL,
22
0
            NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
23
0
    };
Unexecuted instantiation: llm_build_lfm2<true>::llm_build_lfm2(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, int)#1}::operator()(ggml_tensor*, int) const
Unexecuted instantiation: llm_build_lfm2<false>::llm_build_lfm2(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, int)#1}::operator()(ggml_tensor*, int) const
24
0
    auto build_moe_feed_forward = [&model, this](ggml_tensor * cur, int il) -> ggml_tensor * {
25
0
        return build_moe_ffn(cur,
26
0
                            model.layers[il].ffn_gate_inp, model.layers[il].ffn_up_exps,
27
0
                            model.layers[il].ffn_gate_exps, model.layers[il].ffn_down_exps,
28
0
                            model.layers[il].ffn_exp_probs_b, n_expert, n_expert_used, LLM_FFN_SILU, true, false, 0.0,
29
0
                            static_cast<llama_expert_gating_func_type>(hparams.expert_gating_func), il);
30
0
    };
Unexecuted instantiation: llm_build_lfm2<true>::llm_build_lfm2(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, int)#2}::operator()(ggml_tensor*, int) const
Unexecuted instantiation: llm_build_lfm2<false>::llm_build_lfm2(llama_model const&, llm_graph_params const&)::{lambda(ggml_tensor*, int)#2}::operator()(ggml_tensor*, int) const
31
0
    auto build_attn_block = [&model, this](ggml_tensor *   cur,
32
0
                                           ggml_tensor *   inp_pos,
33
0
                                           inp_attn_type * inp_attn,
34
0
                                           int             il) -> ggml_tensor * {
35
0
        GGML_ASSERT(hparams.n_embd_v_gqa(il) == hparams.n_embd_k_gqa(il));
36
0
        const auto n_embd_head = hparams.n_embd_head_v;
37
0
        const auto n_head_kv   = hparams.n_head_kv(il);
38
39
0
        auto * q = build_lora_mm(model.layers[il].wq, cur);
40
0
        cb(q, "model.layers.{}.self_attn.q_proj", il);
41
0
        auto * k = build_lora_mm(model.layers[il].wk, cur);
42
0
        cb(k, "model.layers.{}.self_attn.k_proj", il);
43
0
        auto * v = build_lora_mm(model.layers[il].wv, cur);
44
0
        cb(v, "model.layers.{}.self_attn.v_proj", il);
45
46
0
        q = ggml_reshape_3d(ctx0, q, n_embd_head, n_head, n_tokens);
47
0
        k = ggml_reshape_3d(ctx0, k, n_embd_head, n_head_kv, n_tokens);
48
0
        v = ggml_reshape_3d(ctx0, v, n_embd_head, n_head_kv, n_tokens);
49
50
        // qk norm
51
0
        q = build_norm(q, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
52
0
        cb(q, "model.layers.{}.self_attn.q_layernorm", il);
53
0
        k = build_norm(k, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
54
0
        cb(k, "model.layers.{}.self_attn.k_layernorm", il);
55
56
        // RoPE
57
0
        q = ggml_rope_ext(ctx0, q, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, ext_factor,
58
0
                          attn_factor, beta_fast, beta_slow);
59
0
        k = ggml_rope_ext(ctx0, k, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, ext_factor,
60
0
                          attn_factor, beta_fast, beta_slow);
61
62
0
        cur = build_attn(inp_attn,
63
0
                model.layers[il].wo, NULL,
64
0
                q, k, v, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
65
66
0
        cb(cur, "model.layers.{}.self_attn.out_proj", il);
67
68
0
        return cur;
69
0
    };
Unexecuted instantiation: llm_build_lfm2<true>::llm_build_lfm2(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: llm_build_lfm2<false>::llm_build_lfm2(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
70
0
    auto build_shortconv_block = [&model, this](ggml_tensor *        cur,
71
0
                                                llm_graph_input_rs * inp_recr,
72
0
                                                int                  il) -> ggml_tensor * {
73
0
        const auto * mctx_cur = static_cast<const mem_hybrid_ctx *>(mctx)->get_recr();
74
0
        const uint32_t kv_head      = mctx_cur->get_head();
75
0
        const int64_t  n_seq_tokens = ubatch.n_seq_tokens;
76
0
        const int64_t  n_seqs       = ubatch.n_seqs;
77
0
        GGML_ASSERT(n_seqs != 0);
78
0
        GGML_ASSERT(ubatch.equal_seqs());
79
0
        GGML_ASSERT(ubatch.n_tokens == n_seq_tokens * n_seqs);
80
81
0
        GGML_ASSERT(hparams.n_shortconv_l_cache > 1);
82
0
        const uint32_t d_conv = hparams.n_shortconv_l_cache - 1;
83
84
        // {n_embd, n_tokens} => {n_embd, n_seq_tokens, n_seqs}
85
0
        cur = ggml_reshape_3d(ctx0, cur, cur->ne[0], n_seq_tokens, n_seqs);
86
87
0
        auto * bcx = build_lora_mm(model.layers[il].shortconv.in_proj, cur);
88
0
        cb(bcx, "model.layers.{}.conv.in_proj", il);
89
90
0
        constexpr auto n_chunks = 3;
91
0
        GGML_ASSERT(bcx->ne[0] % n_chunks == 0);
92
0
        const auto chunk_size = bcx->ne[0] / n_chunks;
93
0
        auto *     b          = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2],
94
0
                                             0 * chunk_size * ggml_element_size(bcx));
95
0
        auto *     c          = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2],
96
0
                                             1 * chunk_size * ggml_element_size(bcx));
97
0
        auto *     x          = ggml_view_3d(ctx0, bcx, chunk_size, bcx->ne[1], bcx->ne[2], bcx->nb[1], bcx->nb[2],
98
0
                                             2 * chunk_size * ggml_element_size(bcx));
99
100
0
        auto * bx = ggml_transpose(ctx0, ggml_mul(ctx0, b, x));
101
102
        // read conv state
103
0
        auto * conv_state = mctx_cur->get_r_l(il);
104
0
        auto * conv_rs    = build_rs(inp_recr, conv_state, hparams.n_embd_r(), n_seqs);
105
0
        auto * conv       = ggml_reshape_3d(ctx0, conv_rs, d_conv, hparams.n_embd, n_seqs);
106
107
0
        bx = ggml_concat(ctx0, conv, bx, 0);
108
0
        GGML_ASSERT(bx->ne[0] > conv->ne[0]);
109
110
        // last d_conv columns is a new conv state
111
0
        auto * new_conv = ggml_view_3d(ctx0, bx, conv->ne[0], bx->ne[1], bx->ne[2], bx->nb[1], bx->nb[2],
112
0
                                       (bx->ne[0] - conv->ne[0]) * ggml_element_size(bx));
113
0
        GGML_ASSERT(ggml_are_same_shape(conv, new_conv));
114
115
        // write new conv conv state
116
0
        ggml_build_forward_expand(gf, ggml_cpy(ctx0, new_conv,
117
0
                                               ggml_view_1d(ctx0, conv_state, ggml_nelements(new_conv),
118
0
                                                            kv_head * d_conv * n_embd * ggml_element_size(new_conv))));
119
120
0
        auto * conv_kernel = model.layers[il].shortconv.conv;
121
0
        auto * conv_out    = ggml_ssm_conv(ctx0, bx, conv_kernel);
122
0
        cb(conv_out, "model.layers.{}.conv.conv", il);
123
124
0
        auto * y = ggml_mul(ctx0, c, conv_out);
125
0
        y        = build_lora_mm(model.layers[il].shortconv.out_proj, y);
126
0
        cb(y, "model.layers.{}.conv.out_proj", il);
127
        // {n_embd, n_seq_tokens, n_seqs} => {n_embd, n_tokens}
128
0
        y = ggml_reshape_2d(ctx0, y, y->ne[0], n_seq_tokens * n_seqs);
129
130
0
        return y;
131
0
    };
Unexecuted instantiation: llm_build_lfm2<true>::llm_build_lfm2(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: llm_build_lfm2<false>::llm_build_lfm2(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
132
133
    // actual graph construction starts here
134
0
    ggml_tensor * cur = build_inp_embd(model.tok_embd);
135
0
    cb(cur, "model.embed_tokens", -1);
136
137
0
    ggml_build_forward_expand(gf, cur);
138
139
0
    inp_hybrid_type * inp_hybrid = nullptr;
140
0
    if constexpr (iswa) {
141
0
        inp_hybrid = build_inp_mem_hybrid_iswa();
142
0
    } else {
143
0
        inp_hybrid = build_inp_mem_hybrid();
144
0
    }
145
146
0
    ggml_tensor * inp_pos     = build_inp_pos();
147
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
148
149
0
    for (int il = 0; il < n_layer; ++il) {
150
0
        const bool is_moe_layer = il >= static_cast<int>(hparams.n_layer_dense_lead);
151
152
0
        auto * prev_cur = cur;
153
0
        cur             = build_norm(cur, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
154
0
        cb(cur, "model.layers.{}.operator_norm", il);
155
156
0
        cur = hparams.is_recurrent(il) ? build_shortconv_block(cur, inp_hybrid->get_recr(), il) :
157
0
                                         build_attn_block(cur, inp_pos, inp_hybrid->get_attn(), il);
158
159
0
        if (il == n_layer - 1 && inp_out_ids) {
160
0
            cur      = ggml_get_rows(ctx0, cur, inp_out_ids);
161
0
            prev_cur = ggml_get_rows(ctx0, prev_cur, inp_out_ids);
162
0
        }
163
164
0
        cur = ggml_add(ctx0, prev_cur, cur);
165
166
0
        auto * ffn_norm_out = build_norm(cur, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
167
0
        cb(ffn_norm_out, "model.layers.{}.ffn_norm", il);
168
169
0
        ggml_tensor * ffn_out =
170
0
            is_moe_layer ? build_moe_feed_forward(ffn_norm_out, il) : build_dense_feed_forward(ffn_norm_out, il);
171
0
        cb(ffn_norm_out, "model.layers.{}.ffn_out", il);
172
173
0
        cur = ggml_add(ctx0, cur, ffn_out);
174
0
    }
175
176
0
    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
177
0
    cb(cur, "result_norm", -1);
178
0
    res->t_embd = cur;
179
180
0
    cur = build_lora_mm(model.output, cur);
181
0
    cb(cur, "result_output", -1);
182
183
0
    res->t_logits = cur;
184
185
0
    ggml_build_forward_expand(gf, cur);
186
0
}
Unexecuted instantiation: llm_build_lfm2<true>::llm_build_lfm2(llama_model const&, llm_graph_params const&)
Unexecuted instantiation: llm_build_lfm2<false>::llm_build_lfm2(llama_model const&, llm_graph_params const&)
187
188
// Explicit template instantiations
189
template struct llm_build_lfm2<true>;
190
template struct llm_build_lfm2<false>;