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/ernie4-5-moe.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
std::unique_ptr<llm_graph_context> llama_model_ernie4_5_moe::build_arch_graph(const llm_graph_params & params) const {
4
0
    return std::make_unique<graph>(*this, params);
5
0
}
6
7
llama_model_ernie4_5_moe::graph::graph(const llama_model & model, const llm_graph_params & params) :
8
0
    llm_graph_context(params) {
9
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
10
11
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
12
0
    GGML_ASSERT(n_embd_head == n_rot);
13
14
0
    ggml_tensor * cur;
15
0
    ggml_tensor * inpL;
16
17
0
    inpL = build_inp_embd(model.tok_embd);
18
19
    // inp_pos - contains the positions
20
0
    ggml_tensor * inp_pos = build_inp_pos();
21
22
0
    auto * inp_attn = build_attn_inp_kv();
23
24
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
25
26
0
    GGML_ASSERT(hparams.n_moe_layer_step > 0 && "Ernie 4.5 MoE requires n_moe_layer_step > 0");
27
0
    for (int il = 0; il < n_layer; ++il) {
28
0
        ggml_tensor * inpSA = inpL;
29
        // norm
30
0
        {
31
0
            cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
32
0
            cb(cur, "attn_norm", il);
33
0
        }
34
        // self-attention
35
0
        {
36
            // compute Q and K and RoPE them
37
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
38
0
                    n_embd_head, n_head, n_head_kv, il);
39
40
0
            Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
41
0
                                 ext_factor, attn_factor, beta_fast, beta_slow);
42
43
0
            Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
44
0
                                 ext_factor, attn_factor, beta_fast, beta_slow);
45
46
0
            cb(Qcur, "Qcur", il);
47
0
            cb(Kcur, "Kcur", il);
48
0
            cb(Vcur, "Vcur", il);
49
50
0
            cur = build_attn(inp_attn,
51
0
                    model.layers[il].wo, NULL, model.layers[il].wo_s,
52
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
53
0
            cb(cur, "attn_out", il);
54
0
        }
55
0
        if (il == n_layer - 1 && inp_out_ids) {
56
0
            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);
57
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
58
0
        }
59
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
60
0
        cb(ffn_inp, "ffn_inp", il);
61
62
        // feed-forward network
63
0
        bool is_moe_layer =
64
0
            static_cast<uint32_t>(il) >= hparams.n_layer_dense_lead && (il + 1) % hparams.n_moe_layer_step == 0;
65
66
0
        if (!is_moe_layer) {
67
0
            cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
68
0
            cb(cur, "ffn_norm", il);
69
70
0
            cur = build_ffn(cur,
71
0
                    model.layers[il].ffn_up, NULL, NULL,
72
0
                    model.layers[il].ffn_gate, NULL, NULL,
73
0
                    model.layers[il].ffn_down, NULL, NULL,
74
0
                    NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
75
0
            cb(cur, "ffn_out", il);
76
0
        } else {
77
            // MoE branch
78
0
            cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
79
0
            cb(cur, "ffn_norm", il);
80
81
0
            ggml_tensor * moe_out = build_moe_ffn(cur,
82
0
                                        model.layers[il].ffn_gate_inp,
83
0
                                        model.layers[il].ffn_up_exps,
84
0
                                        model.layers[il].ffn_gate_exps,
85
0
                                        model.layers[il].ffn_down_exps,
86
0
                                        model.layers[il].ffn_exp_probs_b,
87
0
                                        n_expert, n_expert_used,
88
0
                                        LLM_FFN_SILU, true,
89
0
                                        hparams.expert_weights_scale,
90
0
                                        LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
91
0
                                        il);
92
0
            cb(moe_out, "ffn_moe_out", il);
93
94
            // Shared expert (if present)
95
0
            if (hparams.n_ff_shexp > 0) {
96
0
                ggml_tensor * ffn_shexp =
97
0
                    build_ffn(cur,
98
0
                        model.layers[il].ffn_up_shexp, NULL, NULL,
99
0
                        model.layers[il].ffn_gate_shexp, NULL, NULL,
100
0
                        model.layers[il].ffn_down_shexp, NULL, NULL,
101
0
                        NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
102
0
                cb(ffn_shexp, "ffn_shexp", il);
103
104
0
                cur = ggml_add(ctx0, moe_out, ffn_shexp);
105
0
            } else {
106
0
                cur = moe_out;
107
0
            }
108
0
            cb(cur, "ffn_out", il);
109
0
        }
110
0
        cur = ggml_add(ctx0, cur, ffn_inp);
111
0
        cb(cur, "ffn_out", il);
112
113
0
        cur = build_cvec(cur, il);
114
0
        cb(cur, "l_out", il);
115
116
        // input for next layer
117
0
        inpL = cur;
118
0
    }
119
0
    cur = inpL;
120
121
0
    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
122
123
0
    cb(cur, "result_norm", -1);
124
0
    res->t_embd = cur;
125
126
    // lm_head
127
0
    cur = build_lora_mm(model.output, cur, model.output_s);
128
129
0
    cb(cur, "result_output", -1);
130
0
    res->t_logits = cur;
131
132
0
    ggml_build_forward_expand(gf, cur);
133
0
}