Coverage Report

Created: 2026-06-13 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/src/models/bailingmoe2.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_bailingmoe2::load_arch_hparams(llama_model_loader & ml) {
4
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS,       hparams.f_norm_rms_eps);
5
0
    ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT,         hparams.n_layer_dense_lead, false);
6
0
    ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH,        hparams.n_ff_exp);
7
0
    ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);
8
0
    ml.get_key(LLM_KV_EXPERT_SHARED_COUNT,               hparams.n_expert_shared);
9
0
    ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE,              hparams.expert_weights_scale, false);
10
0
    ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM,               hparams.expert_weights_norm, false);
11
0
    ml.get_key(LLM_KV_EXPERT_GATING_FUNC,                hparams.expert_gating_func);
12
0
    ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS,              hparams.n_layer_nextn, false);
13
14
0
    GGML_ASSERT(hparams.n_layer_nextn < hparams.n_layer_all && "n_layer_nextn must be < n_layer_impl");
15
16
0
    switch (hparams.n_layer()) {
17
0
        case 20: type = LLM_TYPE_16B_A1B; break;
18
0
        case 32: type = LLM_TYPE_100B_A6B; break;
19
0
        default: type = LLM_TYPE_UNKNOWN;
20
0
    }
21
0
}
22
23
0
void llama_model_bailingmoe2::load_arch_tensors(llama_model_loader &) {
24
0
    LLAMA_LOAD_LOCALS;
25
0
    const int64_t n_expert_shared = hparams.n_expert_shared;
26
27
0
    const int64_t n_ff_exp        = hparams.n_ff_exp;
28
29
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
30
31
    // output
32
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
33
0
    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, 0);
34
35
0
    GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for bailingmoe2");
36
0
    GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for bailingmoe2");
37
38
0
    for (int i = 0; i < n_layer_all; ++i) {
39
0
        int flags = 0;
40
0
        if (i >= n_layer) {
41
            // skip all tensors in the NextN layers
42
0
            flags |= TENSOR_SKIP;
43
0
        }
44
45
0
        auto & layer = layers[i];
46
47
0
        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, flags);
48
49
0
        layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, flags);
50
0
        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, flags);
51
52
0
        layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, flags);
53
0
        layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, flags);
54
55
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, flags);
56
57
0
        if (static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead) { // MoE layers
58
0
            const int64_t n_ff_shexp = (hparams.n_ff_shexp ? hparams.n_ff_shexp : n_ff_exp) * n_expert_shared;
59
60
0
            layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, flags);
61
0
            layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED | flags);
62
63
0
            layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {  n_embd, n_ff_exp, n_expert}, flags);
64
0
            layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp,   n_embd, n_expert}, flags);
65
0
            layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {  n_embd, n_ff_exp, n_expert}, flags);
66
67
0
            layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_shexp}, flags);
68
0
            layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, flags);
69
0
            layer.ffn_up_shexp   = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP,   "weight", i), {n_embd, n_ff_shexp}, flags);
70
0
        } else { // Dense layers
71
0
            layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, flags);
72
0
            layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, flags);
73
0
            layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, flags);
74
0
        }
75
76
        // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers
77
0
        if (i >= n_layer) {
78
0
            layer.nextn.eh_proj          = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags);
79
0
            layer.nextn.embed_tokens     = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED | flags);
80
0
            layer.nextn.enorm            = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags);
81
0
            layer.nextn.hnorm            = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags);
82
0
            layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED | flags);
83
0
            layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, TENSOR_NOT_REQUIRED | flags);
84
0
            layer.layer_out_norm         = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, flags);
85
0
        }
86
0
    }
87
0
}
88
89
0
std::unique_ptr<llm_graph_context> llama_model_bailingmoe2::build_arch_graph(const llm_graph_params & params) const {
90
0
    return std::make_unique<graph>(*this, params);
91
0
}
92
93
llama_model_bailingmoe2::graph::graph(const llama_model & model, const llm_graph_params & params) :
94
0
    llm_graph_context(params) {
95
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
96
97
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
98
99
0
    ggml_tensor * cur;
100
0
    ggml_tensor * inpL;
101
102
0
    inpL = build_inp_embd(model.tok_embd);
103
104
    // inp_pos - contains the positions
105
0
    ggml_tensor * inp_pos = build_inp_pos();
106
107
0
    auto * inp_attn = build_attn_inp_kv();
108
109
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
110
111
0
    for (int il = 0; il < n_layer; ++il) {
112
0
        ggml_tensor * inpSA = inpL;
113
114
        // norm
115
0
        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
116
0
        cb(cur, "attn_norm", il);
117
118
        // self_attention
119
0
        {
120
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
121
0
                    n_embd_head, n_head, n_head_kv, il);
122
123
0
            Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
124
0
            cb(Qcur, "Qcur_normed", il);
125
126
0
            Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
127
0
                                 ext_factor, attn_factor, beta_fast, beta_slow);
128
129
0
            Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
130
0
            cb(Kcur, "Kcur_normed", il);
131
132
0
            Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
133
0
                                 ext_factor, attn_factor, beta_fast, beta_slow);
134
135
0
            cb(Qcur, "Qcur", il);
136
0
            cb(Kcur, "Kcur", il);
137
0
            cb(Vcur, "Vcur", il);
138
139
0
            cur = build_attn(inp_attn,
140
0
                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
141
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
142
0
        }
143
144
0
        if (il == n_layer - 1 && inp_out_ids) {
145
0
            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);
146
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
147
0
        }
148
149
0
        ggml_tensor * sa_out = ggml_add(ctx0, cur, inpSA);
150
0
        cb(sa_out, "sa_out", il);
151
152
        // MoE branch
153
0
        cur = build_norm(sa_out, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
154
0
        cb(cur, "ffn_norm", il);
155
156
0
        if (static_cast<uint32_t>(il) < hparams.n_layer_dense_lead) {
157
0
            cur = build_ffn(cur,
158
0
                    model.layers[il].ffn_up, NULL, NULL,
159
0
                    model.layers[il].ffn_gate, NULL, NULL,
160
0
                    model.layers[il].ffn_down, NULL, NULL,
161
0
                    NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
162
0
            cb(cur, "ffn_out", il);
163
0
        } else {
164
0
            ggml_tensor * moe_out = build_moe_ffn(cur,
165
0
                model.layers[il].ffn_gate_inp,
166
0
                model.layers[il].ffn_up_exps,
167
0
                model.layers[il].ffn_gate_exps,
168
0
                model.layers[il].ffn_down_exps,
169
0
                model.layers[il].ffn_exp_probs_b,
170
0
                n_expert, n_expert_used,
171
0
                LLM_FFN_SILU, hparams.expert_weights_norm,
172
0
                hparams.expert_weights_scale,
173
0
                (llama_expert_gating_func_type) hparams.expert_gating_func,
174
0
                il);
175
0
            cb(moe_out, "ffn_moe_out", il);
176
177
0
            {
178
0
                ggml_tensor * ffn_shexp =
179
0
                    build_ffn(cur,
180
0
                        model.layers[il].ffn_up_shexp, NULL, NULL,
181
0
                        model.layers[il].ffn_gate_shexp, NULL, NULL,
182
0
                        model.layers[il].ffn_down_shexp, NULL, NULL,
183
0
                        NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
184
0
                cb(ffn_shexp, "ffn_shexp", il);
185
186
0
                cur = ggml_add(ctx0, moe_out, ffn_shexp);
187
0
                cb(cur, "ffn_out", il);
188
0
            }
189
0
        }
190
191
0
        cur = ggml_add(ctx0, cur, sa_out);
192
193
0
        cur = build_cvec(cur, il);
194
0
        cb(cur, "l_out", il);
195
196
        // input for next layer
197
0
        inpL = cur;
198
0
    }
199
200
0
    cur = inpL;
201
202
0
    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
203
204
0
    cb(cur, "result_norm", -1);
205
0
    res->t_embd = cur;
206
207
    // lm_head
208
0
    cur = build_lora_mm(model.output, cur, model.output_s);
209
210
0
    cb(cur, "result_output", -1);
211
0
    res->t_logits = cur;
212
213
0
    ggml_build_forward_expand(gf, cur);
214
0
}