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/grovemoe.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_grovemoe::load_arch_hparams(llama_model_loader & ml) {
4
0
    ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH,        hparams.n_ff_exp);
5
0
    ml.get_key(LLM_KV_EXPERT_CHUNK_FEED_FORWARD_LENGTH,  hparams.n_ff_chexp, false);
6
0
    ml.get_key(LLM_KV_EXPERT_GROUP_SCALE,                hparams.expert_group_scale);
7
0
    ml.get_key(LLM_KV_EXPERTS_PER_GROUP,                 hparams.n_group_experts);
8
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS,       hparams.f_norm_rms_eps);
9
10
0
    switch (hparams.n_layer()) {
11
0
        case 48: type = LLM_TYPE_30B_A3B; break;
12
0
        default: type = LLM_TYPE_UNKNOWN;
13
0
    }
14
0
}
15
16
0
void llama_model_grovemoe::load_arch_tensors(llama_model_loader &) {
17
0
    LLAMA_LOAD_LOCALS;
18
19
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
20
21
    // output
22
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
23
0
    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
24
    // if output is NULL, init from the input tok embed
25
0
    if (output == NULL) {
26
0
        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
27
0
    }
28
29
0
    GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for GROVEMOE");
30
0
    GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for GROVEMOE");
31
0
    GGML_ASSERT(hparams.n_group_experts > 0 && "n_group_experts must be > 0 for GROVEMOE");
32
33
0
    for (int i = 0; i < n_layer; ++i) {
34
0
        auto & layer = layers[i];
35
36
0
        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
37
38
0
        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_gqa, n_embd_gqa, 0);
39
0
        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
40
41
0
        layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
42
0
        layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
43
44
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
45
46
0
        layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
47
48
        // MoE branch
49
0
        const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
50
0
        const int64_t n_ff_chexp = hparams.n_ff_chexp ? hparams.n_ff_chexp : n_embd_head_k;
51
0
        const int64_t n_chunk_expert = n_expert / hparams.n_group_experts;
52
53
0
        layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {  n_embd, n_ff_exp, n_expert}, 0);
54
0
        layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp,   n_embd, n_expert}, 0);
55
0
        layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {  n_embd, n_ff_exp, n_expert}, 0);
56
57
0
        layer.ffn_gate_chexps = create_tensor(tn(LLM_TENSOR_FFN_GATE_CHEXPS, "weight", i), {  n_embd, n_ff_chexp, n_chunk_expert}, 0);
58
0
        layer.ffn_down_chexps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_CHEXPS, "weight", i), {n_ff_chexp,   n_embd, n_chunk_expert}, 0);
59
0
        layer.ffn_up_chexps   = create_tensor(tn(LLM_TENSOR_FFN_UP_CHEXPS,   "weight", i), {  n_embd, n_ff_chexp, n_chunk_expert}, 0);
60
0
    }
61
0
}
62
63
0
std::unique_ptr<llm_graph_context> llama_model_grovemoe::build_arch_graph(const llm_graph_params & params) const {
64
0
    return std::make_unique<graph>(*this, params);
65
0
}
66
67
llama_model_grovemoe::graph::graph(const llama_model & model, const llm_graph_params & params) :
68
0
    llm_graph_context(params) {
69
0
    const int64_t n_embd_head    = hparams.n_embd_head_v();
70
0
    const int64_t n_chunk_expert = n_expert / hparams.n_group_experts;
71
72
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
73
0
    GGML_ASSERT(n_embd_head == n_rot);
74
75
0
    ggml_tensor * cur;
76
0
    ggml_tensor * inpL;
77
78
0
    inpL = build_inp_embd(model.tok_embd);
79
80
    // inp_pos - contains the positions
81
0
    ggml_tensor * inp_pos = build_inp_pos();
82
83
0
    auto * inp_attn = build_attn_inp_kv();
84
85
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
86
87
0
    for (int il = 0; il < n_layer; ++il) {
88
0
        ggml_tensor * inpSA = inpL;
89
90
        // norm
91
0
        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
92
0
        cb(cur, "attn_norm", il);
93
94
        // self_attention
95
0
        {
96
            // compute Q and K and RoPE them
97
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
98
0
                    n_embd_head, n_head, n_head_kv, il);
99
100
0
            Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
101
0
            cb(Qcur, "Qcur_normed", il);
102
103
0
            Qcur = ggml_rope_ext(ctx0, Qcur, 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
106
0
            Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
107
0
            cb(Kcur, "Kcur_normed", il);
108
109
0
            Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
110
0
                                 ext_factor, attn_factor, beta_fast, beta_slow);
111
112
0
            cb(Qcur, "Qcur", il);
113
0
            cb(Kcur, "Kcur", il);
114
0
            cb(Vcur, "Vcur", il);
115
116
0
            cur = build_attn(inp_attn,
117
0
                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
118
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
119
0
        }
120
121
0
        if (il == n_layer - 1 && inp_out_ids) {
122
0
            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);
123
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
124
0
        }
125
126
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
127
0
        cb(ffn_inp, "ffn_inp", il);
128
129
        // MoE branch
130
0
        cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
131
0
        cb(cur, "ffn_norm", il);
132
133
0
        ggml_tensor * probs = build_lora_mm(model.layers[il].ffn_gate_inp, cur);  // [n_expert, n_tokens]
134
0
        cb(probs, "ffn_moe_logits", il);
135
136
0
        ggml_tensor * moe_out =
137
0
            build_moe_ffn(cur,
138
0
                nullptr,
139
0
                model.layers[il].ffn_up_exps,
140
0
                model.layers[il].ffn_gate_exps,
141
0
                model.layers[il].ffn_down_exps,
142
0
                nullptr,
143
0
                n_expert, n_expert_used,
144
0
                LLM_FFN_SILU, true,
145
0
                hparams.expert_weights_scale,
146
0
                LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
147
0
                il,
148
0
                probs);
149
0
        cb(moe_out, "ffn_moe_out", il);
150
0
        cur = moe_out;
151
152
        // TODO: Only do the expert selection and weights once
153
0
        moe_out = build_moe_ffn(cur,
154
0
                    nullptr,
155
0
                    model.layers[il].ffn_up_chexps,
156
0
                    model.layers[il].ffn_gate_chexps,
157
0
                    model.layers[il].ffn_down_chexps,
158
0
                    nullptr,
159
0
                    n_chunk_expert, n_expert_used > n_chunk_expert ? n_chunk_expert : n_expert_used,
160
0
                    LLM_FFN_SILU, true,
161
0
                    hparams.expert_weights_scale,
162
0
                    LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
163
0
                    il,
164
0
                    probs);
165
0
        cb(moe_out, "ffn_adj_moe_out", il);
166
167
0
        cur = ggml_add(ctx0, cur, ggml_scale(ctx0, moe_out, hparams.expert_group_scale));
168
0
        cb(cur, "ffn_final_moe_out", il);
169
170
0
        cur = ggml_add(ctx0, cur, ffn_inp);
171
172
0
        cur = build_cvec(cur, il);
173
0
        cb(cur, "l_out", il);
174
175
        // input for next layer
176
0
        inpL = cur;
177
0
    }
178
179
0
    cur = inpL;
180
181
0
    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
182
183
0
    cb(cur, "result_norm", -1);
184
0
    res->t_embd = cur;
185
186
    // lm_head
187
0
    cur = build_lora_mm(model.output, cur, model.output_s);
188
189
0
    cb(cur, "result_output", -1);
190
0
    res->t_logits = cur;
191
192
0
    ggml_build_forward_expand(gf, cur);
193
0
}