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/mimo2.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_mimo2::load_arch_hparams(llama_model_loader & ml) {
4
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
5
6
0
    hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
7
8
0
    ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp);
9
0
    ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW,   hparams.n_swa);
10
0
    ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA,         hparams.rope_freq_base_train_swa, false);
11
12
0
    ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, hparams.is_swa_impl, hparams.n_layer());
13
14
0
    float value_scale = 0.0f;
15
0
    if (ml.get_key(LLM_KV_ATTENTION_VALUE_SCALE, value_scale, false) && value_scale != 1.0f) {
16
0
        hparams.f_attn_value_scale = value_scale;
17
0
    }
18
19
0
    ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.n_layer_nextn, false);
20
0
    GGML_ASSERT(hparams.n_layer_nextn < hparams.n_layer_all && "n_layer_nextn must be < n_layer_impl");
21
22
0
    switch (hparams.n_layer()) {
23
0
        case 48: type = LLM_TYPE_310B_A15B; break;
24
0
        default: type = LLM_TYPE_UNKNOWN;
25
0
    }
26
0
}
27
28
0
void llama_model_mimo2::load_arch_tensors(llama_model_loader &) {
29
0
    LLAMA_LOAD_LOCALS;
30
31
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
32
33
    // output
34
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
35
0
    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, 0);
36
37
0
    for (int i = 0; i < n_layer_all; ++i) {
38
0
        auto & layer = layers[i];
39
0
        uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(i);
40
0
        uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(i);
41
0
        uint32_t n_head = hparams.n_head(i);
42
43
        // NextN/MTP layers (the last n_nextn blocks) are preserved but disabled pending support
44
0
        const bool is_nextn = i >= n_layer;
45
0
        const int  skip     = is_nextn ? TENSOR_SKIP : 0;
46
47
0
        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, skip);
48
0
        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_v * n_head, n_embd }, skip);
49
50
0
        layer.attn_norm  = create_tensor(tn(LLM_TENSOR_ATTN_NORM,  "weight", i), {n_embd}, skip);
51
0
        layer.attn_sinks = create_tensor(tn(LLM_TENSOR_ATTN_SINKS, "weight", i), {n_head}, TENSOR_NOT_REQUIRED | skip);
52
53
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, skip);
54
55
        // non-MoE branch
56
0
        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, TENSOR_NOT_REQUIRED | skip);
57
0
        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, TENSOR_NOT_REQUIRED | skip);
58
0
        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, TENSOR_NOT_REQUIRED | skip);
59
60
        // MoE branch
61
0
        int64_t n_ff_exp = hparams.n_ff_exp;
62
0
        layer.ffn_gate_inp  = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP,  "weight", i), {n_embd, n_expert}, TENSOR_NOT_REQUIRED | skip);
63
0
        layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp,   n_expert}, TENSOR_NOT_REQUIRED | skip);
64
0
        layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp,   n_embd, n_expert}, TENSOR_NOT_REQUIRED | skip);
65
0
        layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {n_embd, n_ff_exp,   n_expert}, TENSOR_NOT_REQUIRED | skip);
66
0
        layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED | skip);
67
68
0
        if (is_nextn) {
69
0
            layer.nextn.eh_proj  = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), {2 * n_embd, n_embd}, skip);
70
0
            layer.nextn.enorm    = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM,   "weight", i), {n_embd}, skip);
71
0
            layer.nextn.hnorm    = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM,   "weight", i), {n_embd}, skip);
72
0
            layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, skip);
73
0
        }
74
0
    }
75
0
}
76
77
0
std::unique_ptr<llm_graph_context> llama_model_mimo2::build_arch_graph(const llm_graph_params & params) const {
78
0
    return std::make_unique<graph>(*this, params);
79
0
}
80
81
0
llama_model_mimo2::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
82
0
    ggml_tensor * cur;
83
0
    ggml_tensor * inpL;
84
85
0
    inpL = build_inp_embd(model.tok_embd);
86
87
0
    ggml_tensor * inp_pos = build_inp_pos();
88
0
    auto * inp_attn = build_attn_inp_kv_iswa();
89
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
90
91
0
    const float v_scale = hparams.f_attn_value_scale;
92
93
0
    for (int il = 0; il < n_layer; ++il) {
94
0
        ggml_tensor * inpSA = inpL;
95
96
0
        uint32_t n_head_l    = hparams.n_head(il);
97
0
        uint32_t n_head_kv_l = hparams.n_head_kv(il);
98
0
        const float freq_base_l  = model.get_rope_freq_base(cparams, il);
99
0
        const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
100
101
0
        cur = inpL;
102
103
        // self_attention
104
0
        {
105
0
            cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
106
0
            cb(cur, "attn_norm", il);
107
108
0
            ggml_tensor * Qcur;
109
0
            ggml_tensor * Kcur;
110
0
            ggml_tensor * Vcur;
111
112
0
            if (model.layers[il].wqkv) {
113
                // Fused qkv_proj - Q/K share head_dim_k, V uses head_dim_v
114
0
                ggml_tensor * qkv = build_lora_mm(model.layers[il].wqkv, cur);
115
0
                cb(qkv, "wqkv", il);
116
117
0
                const size_t row_k    = ggml_row_size(qkv->type, n_embd_head_k);
118
0
                const size_t row_v    = ggml_row_size(qkv->type, n_embd_head_v);
119
0
                const size_t row_full = qkv->nb[1];
120
0
                const size_t k_off    = row_k * n_head_l;
121
0
                const size_t v_off    = k_off + row_k * n_head_kv_l;
122
123
0
                Qcur = ggml_view_3d(ctx0, qkv, n_embd_head_k, n_head_l,    n_tokens, row_k, row_full, 0);
124
0
                Kcur = ggml_view_3d(ctx0, qkv, n_embd_head_k, n_head_kv_l, n_tokens, row_k, row_full, k_off);
125
0
                Vcur = ggml_view_3d(ctx0, qkv, n_embd_head_v, n_head_kv_l, n_tokens, row_v, row_full, v_off);
126
0
            } else {
127
                // Split path
128
0
                Qcur = build_lora_mm(model.layers[il].wq, cur);
129
0
                cb(Qcur, "Qcur", il);
130
131
0
                Kcur = build_lora_mm(model.layers[il].wk, cur);
132
0
                cb(Kcur, "Kcur", il);
133
134
0
                Vcur = build_lora_mm(model.layers[il].wv, cur);
135
0
                cb(Vcur, "Vcur", il);
136
137
0
                Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head_k, n_head_l,    n_tokens);
138
0
                Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head_k, n_head_kv_l, n_tokens);
139
0
                Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head_v, n_head_kv_l, n_tokens);
140
0
            }
141
142
0
            Qcur = ggml_rope_ext(
143
0
                ctx0, Qcur, inp_pos, nullptr,
144
0
                n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
145
0
                ext_factor, attn_factor, beta_fast, beta_slow
146
0
                );
147
148
0
            Kcur = ggml_rope_ext(
149
0
                ctx0, Kcur, inp_pos, nullptr,
150
0
                n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
151
0
                ext_factor, attn_factor, beta_fast, beta_slow
152
0
                );
153
154
0
            cb(Qcur, "Qcur", il);
155
0
            cb(Kcur, "Kcur", il);
156
0
            cb(Vcur, "Vcur", il);
157
158
0
            ggml_tensor * sinks = model.layers[il].attn_sinks;
159
160
0
            cur = build_attn(inp_attn,
161
0
                    model.layers[il].wo, NULL, model.layers[il].wo_s,
162
0
                    Qcur, Kcur, Vcur, nullptr, sinks, nullptr, 1.0f/sqrtf(float(n_embd_head_k)), il);
163
0
            cb(cur, "attn_out", il);
164
165
0
            if (v_scale) {
166
0
                cur = ggml_scale(ctx0, cur, v_scale);
167
0
                cb(cur, "attn_out_scaled", il);
168
0
            }
169
0
        }
170
171
0
        if (il == n_layer - 1 && inp_out_ids) {
172
0
            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
173
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
174
0
        }
175
176
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
177
0
        cb(ffn_inp, "ffn_inp", il);
178
179
0
        cur = build_norm(ffn_inp,
180
0
                model.layers[il].ffn_norm, NULL,
181
0
                LLM_NORM_RMS, il);
182
0
        cb(cur, "ffn_norm", il);
183
184
        // feed-forward network
185
0
        if (model.layers[il].ffn_gate_inp == nullptr) {
186
            // dense branch
187
0
            cur = build_ffn(cur,
188
0
                    model.layers[il].ffn_up,   model.layers[il].ffn_up_b,   NULL,
189
0
                    model.layers[il].ffn_gate, model.layers[il].ffn_gate_b, NULL,
190
0
                    model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
191
0
                    NULL,
192
0
                    LLM_FFN_SILU, LLM_FFN_PAR, il);
193
0
            cb(cur, "ffn_out", il);
194
0
        } else {
195
            // MoE branch
196
0
            cur = build_moe_ffn(cur,
197
0
                    model.layers[il].ffn_gate_inp,
198
0
                    model.layers[il].ffn_up_exps,
199
0
                    model.layers[il].ffn_gate_exps,
200
0
                    model.layers[il].ffn_down_exps,
201
0
                    model.layers[il].ffn_exp_probs_b,
202
0
                    n_expert, n_expert_used,
203
0
                    LLM_FFN_SILU, true,
204
0
                    hparams.expert_weights_scale,
205
0
                    LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID,
206
0
                    il);
207
0
            cb(cur, "ffn_moe_out", il);
208
0
        }
209
210
0
        cur = ggml_add(ctx0, cur, ffn_inp);
211
212
0
        cur = build_cvec(cur, il);
213
0
        cb(cur, "l_out", il);
214
215
        // input for next layer
216
0
        inpL = cur;
217
0
    }
218
219
0
    cur = inpL;
220
221
0
    cur = build_norm(cur,
222
0
            model.output_norm, NULL,
223
0
            LLM_NORM_RMS, -1);
224
225
0
    cb(cur, "result_norm", -1);
226
0
    res->t_embd = cur;
227
228
    // lm_head
229
0
    cur = build_lora_mm(model.output, cur, model.output_s);
230
231
0
    cb(cur, "result_output", -1);
232
0
    res->t_logits = cur;
233
234
0
    ggml_build_forward_expand(gf, cur);
235
0
}