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/deepseek2.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_deepseek2::load_arch_hparams(llama_model_loader & ml) {
4
0
    uint32_t n_vocab = 0;
5
0
    ml.get_key(LLM_KV_VOCAB_SIZE, n_vocab, false) || ml.get_arr_n(LLM_KV_TOKENIZER_LIST, n_vocab, false);
6
7
    // lite variants include DeepSeek-V2-Lite, GigaChat3-10B-A1.8B, Kanana-2-30B-A3B
8
0
    const bool is_lite = (hparams.n_layer() == 27 || hparams.n_layer() == 26 || (hparams.n_layer() == 48 && n_vocab == 128256));
9
10
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
11
0
    ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT,   hparams.n_layer_dense_lead, false);
12
0
    if (!is_lite) {
13
0
        ml.get_key(LLM_KV_ATTENTION_Q_LORA_RANK, hparams.n_lora_q);
14
0
    }
15
0
    ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK,     hparams.n_lora_kv);
16
0
    ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH_MLA,   hparams.n_embd_head_k_mla_impl, false);
17
0
    ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH_MLA, hparams.n_embd_head_v_mla_impl, false);
18
0
    ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
19
0
    ml.get_key(LLM_KV_EXPERT_SHARED_COUNT,        hparams.n_expert_shared);
20
0
    ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE,       hparams.expert_weights_scale, false);
21
0
    ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM,        hparams.expert_weights_norm, false);
22
0
    ml.get_key(LLM_KV_EXPERT_GATING_FUNC,         hparams.expert_gating_func, false);
23
0
    if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) {
24
        // for compatibility with existing DeepSeek V2 and V2.5 GGUFs
25
        // that have no expert_gating_func model parameter set
26
0
        if ((hparams.n_layer() == 47 || hparams.n_layer() == 48) && n_vocab == 154880) {
27
            // GLM 4.7 Lite
28
0
            hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID;
29
0
        } else {
30
0
            hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX;
31
0
        }
32
0
    }
33
34
0
    if (ml.get_key(LLM_KV_ROPE_SCALING_YARN_LOG_MUL, hparams.rope_yarn_log_mul, false)) {
35
        // [TAG_DEEPSEEK2_YARN_LOG_MUL_FIX]
36
        // cancel the factor from the convert script
37
0
        hparams.rope_yarn_log_mul /= 0.1f;
38
0
    }
39
40
    // (optional) temperature tuning - used by mistral-large
41
0
    ml.get_key(LLM_KV_ATTENTION_TEMPERATURE_SCALE,  hparams.f_attn_temp_scale,       false);
42
0
    ml.get_key(LLM_KV_ATTENTION_TEMPERATURE_LENGTH, hparams.n_attn_temp_floor_scale, false); // FIXME why not use temperature_length?
43
44
0
    hparams.f_attn_temp_offset = 0.0f;
45
46
0
    switch (hparams.n_layer()) {
47
0
        case 27: type = LLM_TYPE_16B; break;
48
0
        case 47: type = LLM_TYPE_30B_A3B; break;
49
0
        case 60: type = LLM_TYPE_236B; break;
50
0
        case 61: type = LLM_TYPE_671B; break;
51
0
        default: type = LLM_TYPE_UNKNOWN;
52
0
    }
53
0
}
54
55
0
void llama_model_deepseek2::load_arch_tensors(llama_model_loader &) {
56
0
    LLAMA_LOAD_LOCALS;
57
0
    const int64_t n_expert_shared = hparams.n_expert_shared;
58
59
0
    const bool is_mla = hparams.is_mla();
60
61
    // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA
62
0
    const int64_t n_embd_head_k_mla = hparams.n_embd_head_k_mla();
63
0
    const int64_t n_embd_head_v_mla = hparams.n_embd_head_v_mla();
64
65
0
    const int64_t n_embd_head_qk_rope = hparams.n_rot();
66
0
    const int64_t n_embd_head_qk_nope = n_embd_head_k_mla - n_embd_head_qk_rope;
67
0
    GGML_ASSERT(n_embd_head_qk_nope >= 1);
68
69
0
    const int64_t q_lora_rank  = hparams.n_lora_q;
70
0
    const int64_t kv_lora_rank = hparams.n_lora_kv;
71
72
0
    const int64_t n_ff_exp        = hparams.n_ff_exp;
73
74
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
75
76
    // output
77
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
78
    // try to load output.weight, if not found, use token_embd (tied embeddings)
79
0
    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
80
0
    if (!output) {
81
0
        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
82
0
    }
83
84
0
    for (int i = 0; i < n_layer; ++i) {
85
0
        auto & layer = layers[i];
86
87
0
        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
88
0
        if (q_lora_rank > 0) {
89
0
            layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, 0);
90
0
        }
91
92
0
        layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);
93
94
0
        if (q_lora_rank > 0) {
95
0
            layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, 0);
96
0
            layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k_mla}, 0);
97
0
        } else {
98
0
            layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_embd_head_k_mla}, 0);
99
0
        }
100
101
0
        layer.wkv_a_mqa = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_MQA, "weight", i), {n_embd, kv_lora_rank + n_embd_head_qk_rope}, 0);
102
103
        // note: only old legacy GGUF files will have the unsplit wkv_b tensor in
104
0
        if (is_mla) {
105
0
            layer.wk_b = create_tensor(tn(LLM_TENSOR_ATTN_K_B, "weight", i), {n_embd_head_qk_nope, kv_lora_rank, n_head}, 0);
106
0
            layer.wv_b = create_tensor(tn(LLM_TENSOR_ATTN_V_B, "weight", i), {kv_lora_rank, n_embd_head_v_mla, n_head}, 0);
107
0
        } else {
108
0
            layer.wkv_b = create_tensor(tn(LLM_TENSOR_ATTN_KV_B, "weight", i), {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v_mla)}, 0);
109
0
        }
110
111
0
        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_embd_head_v_mla, n_embd}, 0);
112
113
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
114
115
0
        if (i < (int) hparams.n_layer_dense_lead) {
116
0
            layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);
117
0
            layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
118
0
            layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
119
0
        } else {
120
0
            layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
121
0
            layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);
122
123
0
            if (n_expert == 0) {
124
0
                throw std::runtime_error("n_expert must be > 0");
125
0
            }
126
0
            if (n_expert_used == 0) {
127
0
                throw std::runtime_error("n_expert_used must be > 0");
128
0
            }
129
130
            // MoE branch
131
0
            layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp,   n_embd, n_expert}, 0);
132
0
            create_tensor_gate_up_exps(layer, i, n_embd, n_ff_exp, n_expert, 0);
133
134
            // Shared expert branch
135
0
            layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
136
0
            layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {        n_ff_exp * n_expert_shared, n_embd}, 0);
137
0
            layer.ffn_up_shexp   = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP,   "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0);
138
0
        }
139
0
    }
140
0
}
141
142
0
std::unique_ptr<llm_graph_context> llama_model_deepseek2::build_arch_graph(const llm_graph_params & params) const {
143
0
    return std::make_unique<graph>(*this, params);
144
0
}
145
146
llama_model_deepseek2::graph::graph(const llama_model & model, const llm_graph_params & params) :
147
0
    llm_graph_context(params) {
148
    // lite variants include DeepSeek-V2-Lite, GigaChat3-10B-A1.8B
149
0
    bool is_ocr = model.arch == LLM_ARCH_DEEPSEEK2OCR;
150
151
0
    const bool is_mla = hparams.is_mla();
152
153
    // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA
154
0
    const int64_t n_embd_head_k = hparams.n_embd_head_k_mla();
155
0
    const int64_t n_embd_head_v = hparams.n_embd_head_v_mla();
156
157
0
    const int64_t n_embd_head_qk_rope = hparams.n_rot();
158
0
    const int64_t n_embd_head_qk_nope = n_embd_head_k - n_embd_head_qk_rope;
159
160
0
    const uint32_t kv_lora_rank = hparams.n_lora_kv;
161
162
    // We have to pre-scale kq_scale and attn_factor to make the YaRN RoPE work correctly.
163
    // See https://github.com/ggml-org/llama.cpp/discussions/7416 for detailed explanation.
164
    // And also: https://github.com/ggml-org/llama.cpp/pull/17945 [TAG_DEEPSEEK2_YARN_LOG_MUL_FIX]
165
166
    // first cancel the adjustment from llama_hparams::yarn_attn_factor_adjust to get the original attn_factor
167
0
    GGML_ASSERT(ext_factor >= 0.0f);
168
0
    const float attn_factor_org = attn_factor * (1.0f + 0.1f * logf(1.0f / freq_scale));
169
170
    // use the original attn_factor to pre-scale the kq_scale
171
0
    const float mscale   = attn_factor_org * (1.0f + 0.1f * hparams.rope_yarn_log_mul * logf(1.0f / freq_scale));
172
0
    const float kq_scale = 1.0f * mscale * mscale / sqrtf(float(n_embd_head_k));
173
174
0
    ggml_tensor * cur;
175
0
    ggml_tensor * inpL;
176
177
    // {n_embd, n_tokens}
178
0
    inpL = build_inp_embd(model.tok_embd);
179
180
    // (optional) temperature tuning - used by mistral-large
181
0
    ggml_tensor * inp_attn_scale = nullptr;
182
0
    if (hparams.f_attn_temp_scale != 0.0f) {
183
0
        inp_attn_scale = build_inp_attn_scale();
184
0
    }
185
186
    // inp_pos - contains the positions
187
0
    ggml_tensor * inp_pos = build_inp_pos();
188
189
0
    auto * inp_attn_kv = !is_mla ? build_attn_inp_kv() : nullptr;
190
0
    auto * inp_attn_k  =  is_mla ? build_attn_inp_k()  : nullptr;
191
192
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
193
194
0
    for (int il = 0; il < n_layer; ++il) {
195
0
        ggml_tensor * inpSA = inpL;
196
197
        // norm
198
0
        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
199
0
        cb(cur, "attn_norm", il);
200
201
        // self_attention
202
0
        if (is_ocr) {
203
0
            const int n_embed_head = hparams.n_embd / hparams.n_head();
204
0
            const int ocr_rope_type = GGML_ROPE_TYPE_NEOX;
205
0
            GGML_ASSERT(n_embed_head == n_embd_head_k && n_embed_head == n_embd_head_v);
206
207
0
            ggml_tensor * Qcur = NULL;
208
0
            ggml_tensor * Kcur = NULL;
209
0
            ggml_tensor * Vcur = NULL;
210
211
0
            Qcur = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
212
0
            Kcur = ggml_mul_mat(ctx0, model.layers[il].wk, cur);
213
0
            Vcur = ggml_mul_mat(ctx0, model.layers[il].wv, cur);
214
0
            cb(Qcur, "q", il);
215
0
            cb(Kcur, "k", il);
216
0
            cb(Vcur, "v", il);
217
218
0
            Qcur = ggml_reshape_3d(ctx0, Qcur, n_embed_head, n_head, n_tokens);
219
0
            Kcur = ggml_reshape_3d(ctx0, Kcur, n_embed_head, n_head, n_tokens);
220
0
            Vcur = ggml_reshape_3d(ctx0, Vcur, n_embed_head, n_head, n_tokens);
221
222
0
            GGML_ASSERT(fabs(freq_base - 10000.0) < 1e-4);
223
0
            Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_embed_head, ocr_rope_type, 0, freq_base, 1, 0, 1, 0, 0);
224
0
            Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_embed_head, ocr_rope_type, 0, freq_base, 1, 0, 1, 0, 0);
225
0
            cb(Qcur, "q_pe", il);
226
0
            cb(Kcur, "k_pe", il);
227
228
0
            cur = build_attn(inp_attn_kv,
229
0
                        model.layers[il].wo, NULL, model.layers[il].wo_s,
230
0
                        Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
231
0
            cb(cur, "attn_out", il);
232
0
        }
233
0
        else {
234
0
            ggml_tensor * q = NULL;
235
236
0
            const bool is_lite = model.layers[il].wq;
237
238
0
            if (!is_lite) {
239
0
                q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
240
0
                cb(q, "q", il);
241
242
0
                q = build_norm(q, model.layers[il].attn_q_a_norm, nullptr, LLM_NORM_RMS, il);
243
0
                cb(q, "q", il);
244
245
0
                q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
246
0
                cb(q, "q", il);
247
0
            } else {
248
0
                q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
249
0
                cb(q, "q", il);
250
0
            }
251
            // split into {n_embd_head_qk_nope, n_head, n_tokens}
252
0
            ggml_tensor * q_nope =
253
0
                ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens, ggml_row_size(q->type, n_embd_head_k),
254
0
                             ggml_row_size(q->type, n_embd_head_k) * n_head, 0);
255
0
            cb(q_nope, "q_nope", il);
256
257
            // and {n_embd_head_qk_rope, n_head, n_tokens}
258
0
            ggml_tensor * q_pe = ggml_view_3d(
259
0
                ctx0, q, n_embd_head_qk_rope, n_head, n_tokens, ggml_row_size(q->type, n_embd_head_k),
260
0
                ggml_row_size(q->type, n_embd_head_k) * n_head, ggml_row_size(q->type, n_embd_head_qk_nope));
261
0
            cb(q_pe, "q_pe", il);
262
263
0
            ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
264
0
            cb(kv_cmpr_pe, "kv_cmpr_pe", il);
265
266
            // split into {kv_lora_rank, n_tokens}
267
0
            ggml_tensor * kv_cmpr =
268
0
                ggml_view_2d(ctx0, kv_cmpr_pe, kv_lora_rank, n_tokens,
269
0
                             ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope), 0);
270
0
            cb(kv_cmpr, "kv_cmpr", il);
271
272
            // and {n_embd_head_qk_rope, 1, n_tokens}
273
0
            ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_cmpr_pe, n_embd_head_qk_rope, 1, n_tokens,
274
0
                                              ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
275
0
                                              ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope),
276
0
                                              ggml_row_size(kv_cmpr_pe->type, kv_lora_rank));
277
0
            cb(k_pe, "k_pe", il);
278
279
0
            q_pe = ggml_rope_ext(ctx0, q_pe, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
280
0
                                 ext_factor, attn_factor, beta_fast, beta_slow);
281
0
            cb(q_pe, "q_pe", il);
282
283
0
            k_pe = ggml_rope_ext(ctx0, k_pe, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
284
0
                                 ext_factor, attn_factor, beta_fast, beta_slow);
285
0
            cb(k_pe, "k_pe", il);
286
287
0
            kv_cmpr = build_norm(kv_cmpr, model.layers[il].attn_kv_a_norm, nullptr, LLM_NORM_RMS, il);
288
0
            cb(kv_cmpr, "kv_cmpr", il);
289
290
0
            if (is_mla) {
291
                // {n_embd_head_qk_nope, n_tokens, n_head}
292
0
                q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3);
293
0
                cb(q_nope, "q_nope_perm", il);
294
295
                // {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head}
296
0
                ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, model.layers[il].wk_b, q_nope);
297
0
                cb(q_nope_absorbed, "q_nope_absorbed", il);
298
299
                // {kv_lora_rank, n_head, n_tokens}
300
0
                q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3);
301
0
                cb(q_nope_absorbed, "q_nope_absorbed_perm", il);
302
303
                // {n_embd_head_qk_rope + kv_lora_rank, n_head, n_tokens}
304
                // note: rope must go first for in-place context shifting in build_rope_shift()
305
0
                ggml_tensor * Qcur = ggml_concat(ctx0, q_nope_absorbed, q_pe, 0);
306
0
                cb(Qcur, "Qcur", il);
307
308
0
                kv_cmpr = ggml_reshape_3d(ctx0, kv_cmpr, kv_lora_rank, 1, n_tokens);
309
0
                cb(kv_cmpr, "kv_cmpr_reshape", il);
310
311
                // {n_embd_head_qk_rope + kv_lora_rank, 1, n_tokens}
312
0
                ggml_tensor * Kcur = ggml_concat(ctx0, kv_cmpr, k_pe, 0);
313
0
                cb(Kcur, "Kcur", il);
314
315
                // {kv_lora_rank, 1, n_tokens}
316
0
                ggml_tensor * Vcur = kv_cmpr;
317
0
                cb(Vcur, "Vcur", il);
318
319
0
                if (inp_attn_scale) {
320
                    // apply llama 4 temperature scaling
321
0
                    Qcur = ggml_mul(ctx0, Qcur, inp_attn_scale);
322
0
                    cb(Qcur, "Qcur_attn_temp_scaled", il);
323
0
                }
324
325
                // note: MLA with the absorption optimization converts into MQA (ie: GQA with 1 group)
326
0
                cur = build_attn(inp_attn_k,
327
0
                        model.layers[il].wo, NULL, model.layers[il].wo_s,
328
0
                        Qcur, Kcur, Vcur, nullptr, nullptr, model.layers[il].wv_b, kq_scale, il);
329
0
            } else {
330
0
                ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_cmpr);
331
0
                cb(kv, "kv", il);
332
333
                // split into {n_embd_head_qk_nope, n_head, n_tokens}
334
0
                ggml_tensor * k_nope =
335
0
                    ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
336
0
                                 ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v),
337
0
                                 ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v) * n_head, 0);
338
0
                cb(k_nope, "k_nope_view", il);
339
340
                // and {n_embd_head_v, n_head, n_tokens}
341
0
                ggml_tensor * Vcur = ggml_view_3d(ctx0, kv, n_embd_head_v, n_head, n_tokens,
342
0
                                                  ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v),
343
0
                                                  ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v) * n_head,
344
0
                                                  ggml_row_size(kv->type, n_embd_head_qk_nope));
345
0
                cb(Vcur, "Vcur_view", il);
346
347
0
                Vcur = ggml_cont(ctx0, Vcur);
348
0
                cb(Vcur, "Vcur_cont", il);
349
350
0
                ggml_tensor * Qcur = ggml_concat(ctx0, q_nope, q_pe, 0);
351
0
                cb(Qcur, "Qcur", il);
352
353
0
                ggml_tensor * Kcur = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);
354
0
                cb(Kcur, "Kcur", il);
355
356
0
                if (inp_attn_scale) {
357
                    // apply llama 4 temperature scaling
358
0
                    Qcur = ggml_mul(ctx0, Qcur, inp_attn_scale);
359
0
                    cb(Qcur, "Qcur_attn_temp_scaled", il);
360
0
                }
361
362
                // note: MLA without the absorption optimization converts into MHA (ie: GQA with full n_head groups)
363
0
                cur = build_attn(inp_attn_kv,
364
0
                            model.layers[il].wo, NULL, model.layers[il].wo_s,
365
0
                            Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
366
0
            }
367
0
        }
368
0
        if (il == n_layer - 1 && inp_out_ids) {
369
0
            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);
370
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
371
0
        }
372
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
373
0
        cb(ffn_inp, "ffn_inp", il);
374
375
0
        cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
376
0
        cb(cur, "ffn_norm", il);
377
378
0
        if ((uint32_t) il < hparams.n_layer_dense_lead) {
379
0
            cur = build_ffn(cur,
380
0
                model.layers[il].ffn_up, NULL, NULL,
381
0
                model.layers[il].ffn_gate, NULL, NULL,
382
0
                model.layers[il].ffn_down, NULL, NULL,
383
0
                NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
384
0
            cb(cur, "ffn_out", il);
385
0
        } else {
386
            // MoE branch
387
0
            ggml_tensor * moe_out = build_moe_ffn(cur,
388
0
                model.layers[il].ffn_gate_inp,
389
0
                model.layers[il].ffn_up_exps,
390
0
                model.layers[il].ffn_gate_exps,
391
0
                model.layers[il].ffn_down_exps,
392
0
                model.layers[il].ffn_exp_probs_b,
393
0
                n_expert, n_expert_used,
394
0
                LLM_FFN_SILU, hparams.expert_weights_norm,
395
0
                hparams.expert_weights_scale,
396
0
                (llama_expert_gating_func_type) hparams.expert_gating_func,
397
0
                il,
398
0
                nullptr,
399
0
                model.layers[il].ffn_gate_up_exps);
400
0
            cb(moe_out, "ffn_moe_out", il);
401
402
            // FFN shared expert
403
0
            {
404
0
                ggml_tensor * ffn_shexp =
405
0
                    build_ffn(cur,
406
0
                        model.layers[il].ffn_up_shexp, NULL, NULL,
407
0
                        model.layers[il].ffn_gate_shexp, NULL, NULL,
408
0
                        model.layers[il].ffn_down_shexp, NULL, NULL,
409
0
                        NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
410
0
                cb(ffn_shexp, "ffn_shexp", il);
411
412
0
                cur = ggml_add(ctx0, moe_out, ffn_shexp);
413
0
                cb(cur, "ffn_out", il);
414
0
            }
415
0
        }
416
0
        cur = ggml_add(ctx0, cur, ffn_inp);
417
418
0
        cur = build_cvec(cur, il);
419
0
        cb(cur, "l_out", il);
420
421
        // input for next layer
422
0
        inpL = cur;
423
0
    }
424
0
    cur = inpL;
425
426
0
    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
427
428
0
    cb(cur, "result_norm", -1);
429
0
    res->t_embd = cur;
430
431
    // lm_head
432
0
    cur = ggml_mul_mat(ctx0, model.output, cur);
433
434
0
    cb(cur, "result_output", -1);
435
0
    res->t_logits = cur;
436
437
0
    ggml_build_forward_expand(gf, cur);
438
0
}