Coverage Report

Created: 2026-04-12 06:40

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/src/models/minimax-m2.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
llm_build_minimax_m2::llm_build_minimax_m2(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
4
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
5
6
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
7
    // GGML_ASSERT(n_embd_head == n_rot); this is wrong in case of minimax, head_dim = 128, n_rot = 64
8
9
0
    ggml_tensor * cur;
10
0
    ggml_tensor * inpL;
11
12
0
    inpL = build_inp_embd(model.tok_embd);
13
14
0
    ggml_tensor * inp_pos = build_inp_pos();
15
0
    auto inp_attn = build_attn_inp_kv();
16
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
17
18
0
    for (int il = 0; il < n_layer; ++il) {
19
0
        ggml_tensor * inpSA = inpL;
20
21
0
        cur = inpL;
22
23
        // self_attention
24
0
        {
25
0
            cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
26
0
            cb(cur, "attn_norm", il);
27
28
            // compute Q and K and RoPE them
29
0
            ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
30
0
            cb(Qcur, "Qcur", il);
31
32
0
            ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
33
0
            cb(Kcur, "Kcur", il);
34
35
0
            ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
36
0
            cb(Vcur, "Vcur", il);
37
38
0
            Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL,
39
0
                    LLM_NORM_RMS, il);
40
0
            cb(Qcur, "Qcur_normed", il);
41
42
0
            Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL,
43
0
                    LLM_NORM_RMS, il);
44
0
            cb(Kcur, "Kcur_normed", il);
45
46
0
            Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head,    n_tokens);
47
0
            Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
48
0
            Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
49
50
0
            Qcur = ggml_rope_ext(
51
0
                ctx0, Qcur, inp_pos, nullptr,
52
0
                n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
53
0
                ext_factor, attn_factor, beta_fast, beta_slow
54
0
                );
55
56
0
            Kcur = ggml_rope_ext(
57
0
                ctx0, Kcur, inp_pos, nullptr,
58
0
                n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
59
0
                ext_factor, attn_factor, beta_fast, beta_slow
60
0
                );
61
62
0
            cb(Qcur, "Qcur", il);
63
0
            cb(Kcur, "Kcur", il);
64
0
            cb(Vcur, "Vcur", il);
65
66
0
            cur = build_attn(inp_attn,
67
0
                    model.layers[il].wo, NULL,
68
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
69
0
        }
70
71
0
        if (il == n_layer - 1 && inp_out_ids) {
72
0
            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
73
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
74
0
        }
75
76
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
77
0
        cb(ffn_inp, "ffn_inp", il);
78
79
        // MoE branch
80
0
        cur = build_norm(ffn_inp,
81
0
                model.layers[il].ffn_norm, NULL,
82
0
                LLM_NORM_RMS, il);
83
0
        cb(cur, "ffn_norm", il);
84
85
0
        cur = build_moe_ffn(cur,
86
0
                model.layers[il].ffn_gate_inp,
87
0
                model.layers[il].ffn_up_exps,
88
0
                model.layers[il].ffn_gate_exps,
89
0
                model.layers[il].ffn_down_exps,
90
0
                model.layers[il].ffn_exp_probs_b,
91
0
                n_expert, n_expert_used,
92
0
                LLM_FFN_SILU, true,
93
0
                hparams.expert_weights_scale,
94
0
                (llama_expert_gating_func_type) hparams.expert_gating_func,
95
0
                il);
96
0
        cb(cur, "ffn_moe_out", il);
97
98
0
        cur = ggml_add(ctx0, cur, ffn_inp);
99
100
0
        cur = build_cvec(cur, il);
101
0
        cb(cur, "l_out", il);
102
103
        // input for next layer
104
0
        inpL = cur;
105
0
    }
106
107
0
    cur = inpL;
108
109
0
    cur = build_norm(cur,
110
0
            model.output_norm, NULL,
111
0
            LLM_NORM_RMS, -1);
112
113
0
    cb(cur, "result_norm", -1);
114
0
    res->t_embd = cur;
115
116
    // lm_head
117
0
    cur = build_lora_mm(model.output, cur);
118
119
0
    cb(cur, "result_output", -1);
120
0
    res->t_logits = cur;
121
122
0
    ggml_build_forward_expand(gf, cur);
123
0
}