Coverage Report

Created: 2026-02-26 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/src/models/deepseek2.cpp
Line
Count
Source
1
#include "models.h"
2
3
llm_build_deepseek2::llm_build_deepseek2(const llama_model & model, const llm_graph_params & params) :
4
0
    llm_graph_context(params) {
5
0
    const bool is_mla = hparams.is_mla();
6
7
    // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA
8
0
    const int64_t n_embd_head_k = hparams.n_embd_head_k_mla();
9
0
    const int64_t n_embd_head_v = hparams.n_embd_head_v_mla();
10
11
0
    const int64_t n_embd_head_qk_rope = hparams.n_rot;
12
0
    const int64_t n_embd_head_qk_nope = n_embd_head_k - n_embd_head_qk_rope;
13
14
0
    const uint32_t kv_lora_rank = hparams.n_lora_kv;
15
16
    // We have to pre-scale kq_scale and attn_factor to make the YaRN RoPE work correctly.
17
    // See https://github.com/ggml-org/llama.cpp/discussions/7416 for detailed explanation.
18
    // And also: https://github.com/ggml-org/llama.cpp/pull/17945 [TAG_DEEPSEEK2_YARN_LOG_MUL_FIX]
19
20
    // first cancel the adjustment from llama_hparams::yarn_attn_factor_adjust to get the original attn_factor
21
0
    GGML_ASSERT(ext_factor >= 0.0f);
22
0
    const float attn_factor_org = attn_factor * (1.0f + 0.1f * logf(1.0f / freq_scale));
23
24
    // use the original attn_factor to pre-scale the kq_scale
25
0
    const float mscale   = attn_factor_org * (1.0f + 0.1f * hparams.rope_yarn_log_mul * logf(1.0f / freq_scale));
26
0
    const float kq_scale = 1.0f * mscale * mscale / sqrtf(float(n_embd_head_k));
27
28
0
    ggml_tensor * cur;
29
0
    ggml_tensor * inpL;
30
31
    // {n_embd, n_tokens}
32
0
    inpL = build_inp_embd(model.tok_embd);
33
34
    // (optional) temperature tuning - used by mistral-large
35
0
    ggml_tensor * inp_attn_scale = nullptr;
36
0
    if (hparams.f_attn_temp_scale != 0.0f) {
37
0
        inp_attn_scale = build_inp_attn_scale();
38
0
    }
39
40
    // inp_pos - contains the positions
41
0
    ggml_tensor * inp_pos = build_inp_pos();
42
43
0
    auto * inp_attn_kv = !is_mla ? build_attn_inp_kv() : nullptr;
44
0
    auto * inp_attn_k  =  is_mla ? build_attn_inp_k()  : nullptr;
45
46
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
47
48
0
    int effective_n_layers = hparams.n_layer - hparams.nextn_predict_layers;
49
0
    for (int il = 0; il < effective_n_layers; ++il) {
50
0
        ggml_tensor * inpSA = inpL;
51
52
        // norm
53
0
        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
54
0
        cb(cur, "attn_norm", il);
55
56
        // self_attention
57
0
        {
58
0
            ggml_tensor * q = NULL;
59
60
0
            const bool is_lite = model.layers[il].wq;
61
62
0
            if (!is_lite) {
63
0
                q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
64
0
                cb(q, "q", il);
65
66
0
                q = build_norm(q, model.layers[il].attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
67
0
                cb(q, "q", il);
68
69
0
                q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
70
0
                cb(q, "q", il);
71
0
            } else {
72
0
                q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
73
0
                cb(q, "q", il);
74
0
            }
75
            // split into {n_embd_head_qk_nope, n_head, n_tokens}
76
0
            ggml_tensor * q_nope =
77
0
                ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens, ggml_row_size(q->type, n_embd_head_k),
78
0
                             ggml_row_size(q->type, n_embd_head_k) * n_head, 0);
79
0
            cb(q_nope, "q_nope", il);
80
81
            // and {n_embd_head_qk_rope, n_head, n_tokens}
82
0
            ggml_tensor * q_pe = ggml_view_3d(
83
0
                ctx0, q, n_embd_head_qk_rope, n_head, n_tokens, ggml_row_size(q->type, n_embd_head_k),
84
0
                ggml_row_size(q->type, n_embd_head_k) * n_head, ggml_row_size(q->type, n_embd_head_qk_nope));
85
0
            cb(q_pe, "q_pe", il);
86
87
0
            ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
88
0
            cb(kv_cmpr_pe, "kv_cmpr_pe", il);
89
90
            // split into {kv_lora_rank, n_tokens}
91
0
            ggml_tensor * kv_cmpr =
92
0
                ggml_view_2d(ctx0, kv_cmpr_pe, kv_lora_rank, n_tokens,
93
0
                             ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope), 0);
94
0
            cb(kv_cmpr, "kv_cmpr", il);
95
96
            // and {n_embd_head_qk_rope, 1, n_tokens}
97
0
            ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_cmpr_pe, n_embd_head_qk_rope, 1, n_tokens,
98
0
                                              ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
99
0
                                              ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
100
0
                                              ggml_row_size(kv_cmpr_pe->type, kv_lora_rank));
101
0
            cb(k_pe, "k_pe", il);
102
103
0
            q_pe = ggml_rope_ext(ctx0, q_pe, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
104
0
                                 ext_factor, attn_factor, beta_fast, beta_slow);
105
0
            cb(q_pe, "q_pe", il);
106
107
0
            k_pe = ggml_rope_ext(ctx0, k_pe, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
108
0
                                 ext_factor, attn_factor, beta_fast, beta_slow);
109
0
            cb(k_pe, "k_pe", il);
110
111
0
            kv_cmpr = build_norm(kv_cmpr, model.layers[il].attn_kv_a_norm, nullptr, LLM_NORM_RMS, il);
112
0
            cb(kv_cmpr, "kv_cmpr", il);
113
114
0
            if (is_mla) {
115
                // {n_embd_head_qk_nope, n_tokens, n_head}
116
0
                q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
117
0
                cb(q_nope, "q_nope_perm", il);
118
119
                // {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head}
120
0
                ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, model.layers[il].wk_b, q_nope);
121
0
                cb(q_nope_absorbed, "q_nope_absorbed", il);
122
123
                // {kv_lora_rank, n_head, n_tokens}
124
0
                q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3);
125
0
                cb(q_nope_absorbed, "q_nope_absorbed_perm", il);
126
127
                // {n_embd_head_qk_rope + kv_lora_rank, n_head, n_tokens}
128
                // note: rope must go first for in-place context shifting in build_rope_shift()
129
0
                ggml_tensor * Qcur = ggml_concat(ctx0, q_nope_absorbed, q_pe, 0);
130
0
                cb(Qcur, "Qcur", il);
131
132
0
                kv_cmpr = ggml_reshape_3d(ctx0, kv_cmpr, kv_lora_rank, 1, n_tokens);
133
0
                cb(kv_cmpr, "kv_cmpr_reshape", il);
134
135
                // {n_embd_head_qk_rope + kv_lora_rank, 1, n_tokens}
136
0
                ggml_tensor * Kcur = ggml_concat(ctx0, kv_cmpr, k_pe, 0);
137
0
                cb(Kcur, "Kcur", il);
138
139
                // {kv_lora_rank, 1, n_tokens}
140
0
                ggml_tensor * Vcur = kv_cmpr;
141
0
                cb(Vcur, "Vcur", il);
142
143
0
                if (inp_attn_scale) {
144
                    // apply llama 4 temperature scaling
145
0
                    Qcur = ggml_mul(ctx0, Qcur, inp_attn_scale);
146
0
                    cb(Qcur, "Qcur_attn_temp_scaled", il);
147
0
                }
148
149
                // note: MLA with the absorption optimzation converts into MQA (ie: GQA with 1 group)
150
0
                cur = build_attn(inp_attn_k,
151
0
                        model.layers[il].wo, NULL,
152
0
                        Qcur, Kcur, Vcur, nullptr, nullptr, model.layers[il].wv_b, kq_scale, il);
153
0
            } else {
154
0
                ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_cmpr);
155
0
                cb(kv, "kv", il);
156
157
                // split into {n_embd_head_qk_nope, n_head, n_tokens}
158
0
                ggml_tensor * k_nope =
159
0
                    ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
160
0
                                 ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v),
161
0
                                 ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v) * n_head, 0);
162
0
                cb(k_nope, "k_nope_view", il);
163
164
                // and {n_embd_head_v, n_head, n_tokens}
165
0
                ggml_tensor * Vcur = ggml_view_3d(ctx0, kv, n_embd_head_v, n_head, n_tokens,
166
0
                                                  ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v),
167
0
                                                  ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v) * n_head,
168
0
                                                  ggml_row_size(kv->type, n_embd_head_qk_nope));
169
0
                cb(Vcur, "Vcur_view", il);
170
171
0
                Vcur = ggml_cont(ctx0, Vcur);
172
0
                cb(Vcur, "Vcur_cont", il);
173
174
0
                ggml_tensor * Qcur = ggml_concat(ctx0, q_nope, q_pe, 0);
175
0
                cb(Qcur, "Qcur", il);
176
177
0
                ggml_tensor * Kcur = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);
178
0
                cb(Kcur, "Kcur", il);
179
180
0
                if (inp_attn_scale) {
181
                    // apply llama 4 temperature scaling
182
0
                    Qcur = ggml_mul(ctx0, Qcur, inp_attn_scale);
183
0
                    cb(Qcur, "Qcur_attn_temp_scaled", il);
184
0
                }
185
186
                // note: MLA without the absorption optimization converts into MHA (ie: GQA with full n_head groups)
187
0
                cur = build_attn(inp_attn_kv,
188
0
                            model.layers[il].wo, NULL,
189
0
                            Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
190
0
            }
191
0
        }
192
0
        if (il == effective_n_layers - 1 && inp_out_ids) {
193
0
            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);
194
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
195
0
        }
196
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
197
0
        cb(ffn_inp, "ffn_inp", il);
198
199
0
        cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
200
0
        cb(cur, "ffn_norm", il);
201
202
0
        if ((uint32_t) il < hparams.n_layer_dense_lead) {
203
0
            cur = build_ffn(cur,
204
0
                model.layers[il].ffn_up, NULL, NULL,
205
0
                model.layers[il].ffn_gate, NULL, NULL,
206
0
                model.layers[il].ffn_down, NULL, NULL,
207
0
                NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
208
0
            cb(cur, "ffn_out", il);
209
0
        } else {
210
            // MoE branch
211
0
            ggml_tensor * moe_out = build_moe_ffn(cur,
212
0
                model.layers[il].ffn_gate_inp,
213
0
                model.layers[il].ffn_up_exps,
214
0
                model.layers[il].ffn_gate_exps,
215
0
                model.layers[il].ffn_down_exps,
216
0
                model.layers[il].ffn_exp_probs_b,
217
0
                n_expert, n_expert_used,
218
0
                LLM_FFN_SILU, hparams.expert_weights_norm,
219
0
                hparams.expert_weights_scale, hparams.expert_weights_scale,
220
0
                (llama_expert_gating_func_type) hparams.expert_gating_func,
221
0
                il);
222
0
            cb(moe_out, "ffn_moe_out", il);
223
224
            // FFN shared expert
225
0
            {
226
0
                ggml_tensor * ffn_shexp =
227
0
                    build_ffn(cur,
228
0
                        model.layers[il].ffn_up_shexp, NULL, NULL,
229
0
                        model.layers[il].ffn_gate_shexp, NULL, NULL,
230
0
                        model.layers[il].ffn_down_shexp, NULL, NULL,
231
0
                        NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
232
0
                cb(ffn_shexp, "ffn_shexp", il);
233
234
0
                cur = ggml_add(ctx0, moe_out, ffn_shexp);
235
0
                cb(cur, "ffn_out", il);
236
0
            }
237
0
        }
238
0
        cur = ggml_add(ctx0, cur, ffn_inp);
239
240
0
        cur = build_cvec(cur, il);
241
0
        cb(cur, "l_out", il);
242
243
        // input for next layer
244
0
        inpL = cur;
245
0
    }
246
0
    cur = inpL;
247
248
0
    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
249
250
0
    cb(cur, "result_norm", -1);
251
0
    res->t_embd = cur;
252
253
    // lm_head
254
0
    cur = ggml_mul_mat(ctx0, model.output, cur);
255
256
0
    cb(cur, "result_output", -1);
257
0
    res->t_logits = cur;
258
259
0
    ggml_build_forward_expand(gf, cur);
260
0
}