Coverage Report

Created: 2025-12-28 06:25

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