Coverage Report

Created: 2025-11-28 06:57

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/src/models/minicpm3.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
llm_build_minicpm3::llm_build_minicpm3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
4
    //TODO: if the model varies, these parameters need to be read from the model
5
0
    const int64_t n_embd_base = 256;
6
0
    const float scale_embd  = 12.0f;
7
0
    const float scale_depth = 1.4f;
8
0
    const float kq_scale = 1.0f / sqrtf(float(hparams.n_embd_head_k));
9
10
0
    const uint32_t n_embd_head_qk_rope = hparams.n_rot;
11
0
    const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k - hparams.n_rot;
12
0
    const uint32_t kv_lora_rank = hparams.n_lora_kv;
13
14
0
    ggml_tensor * cur;
15
0
    ggml_tensor * inpL;
16
17
0
    inpL = build_inp_embd(model.tok_embd);
18
19
    // scale the input embeddings
20
0
    inpL = ggml_scale(ctx0, inpL, scale_embd);
21
0
    cb(inpL, "inp_scaled", -1);
22
23
    // inp_pos - contains the positions
24
0
    ggml_tensor * inp_pos = build_inp_pos();
25
26
0
    auto * inp_attn = build_attn_inp_kv();
27
28
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
29
30
0
    for (int il = 0; il < n_layer; ++il) {
31
0
        ggml_tensor * inpSA = inpL;
32
33
0
        ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
34
35
        // norm
36
0
        cur = build_norm(inpL,
37
0
                model.layers[il].attn_norm, NULL,
38
0
                LLM_NORM_RMS, il);
39
0
        cb(cur, "attn_norm", il);
40
41
        // self_attention
42
0
        {
43
0
            ggml_tensor * q = NULL;
44
            // {n_embd, q_lora_rank} * {n_embd, n_tokens} -> {q_lora_rank, n_tokens}
45
0
            q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur);
46
0
            cb(q, "q", il);
47
48
0
            q = build_norm(q,
49
0
                    model.layers[il].attn_q_a_norm, NULL,
50
0
                    LLM_NORM_RMS, il);
51
0
            cb(q, "q", il);
52
53
            // {q_lora_rank, n_head * hparams.n_embd_head_k} * {q_lora_rank, n_tokens} -> {n_head * hparams.n_embd_head_k, n_tokens}
54
0
            q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q);
55
0
            cb(q, "q", il);
56
57
            // split into {n_head * n_embd_head_qk_nope, n_tokens}
58
0
            ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens,
59
0
                    ggml_row_size(q->type, hparams.n_embd_head_k),
60
0
                    ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
61
0
                    0);
62
0
            cb(q_nope, "q_nope", il);
63
64
            // and {n_head * n_embd_head_qk_rope, n_tokens}
65
0
            ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens,
66
0
                    ggml_row_size(q->type, hparams.n_embd_head_k),
67
0
                    ggml_row_size(q->type, hparams.n_embd_head_k * n_head),
68
0
                    ggml_row_size(q->type, n_embd_head_qk_nope));
69
0
            cb(q_pe, "q_pe", il);
70
71
            // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
72
0
            ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
73
0
            cb(kv_pe_compresseed, "kv_pe_compresseed", il);
74
75
            // split into {kv_lora_rank, n_tokens}
76
0
            ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,
77
0
                    kv_pe_compresseed->nb[1],
78
0
                    0);
79
0
            cb(kv_compressed, "kv_compressed", il);
80
81
            // and {n_embd_head_qk_rope, n_tokens}
82
0
            ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,
83
0
                    kv_pe_compresseed->nb[1],
84
0
                    kv_pe_compresseed->nb[1],
85
0
                    ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));
86
0
            cb(k_pe, "k_pe", il);
87
88
0
            kv_compressed = build_norm(kv_compressed,
89
0
                    model.layers[il].attn_kv_a_norm, NULL,
90
0
                    LLM_NORM_RMS, il);
91
0
            cb(kv_compressed, "kv_compressed", il);
92
93
            // {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)} * {kv_lora_rank, n_tokens} -> {n_head * (n_embd_head_qk_nope + n_embd_head_v), n_tokens}
94
0
            ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
95
0
            cb(kv, "kv", il);
96
97
            // split into {n_head * n_embd_head_qk_nope, n_tokens}
98
0
            ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
99
0
                    ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v),
100
0
                    ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v)),
101
0
                    0);
102
0
            cb(k_nope, "k_nope", il);
103
104
            // and {n_head * n_embd_head_v, n_tokens}
105
0
            ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v, n_head, n_tokens,
106
0
                    ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)),
107
0
                    ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v)*n_head),
108
0
                    ggml_row_size(kv->type, (n_embd_head_qk_nope)));
109
0
            cb(v_states, "v_states", il);
110
111
0
            v_states = ggml_cont(ctx0, v_states);
112
0
            cb(v_states, "v_states", il);
113
114
0
            q_pe = ggml_rope_ext(
115
0
                    ctx0, q_pe, inp_pos, rope_factors,
116
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
117
0
                    ext_factor, attn_factor, beta_fast, beta_slow
118
0
                    );
119
0
            cb(q_pe, "q_pe", il);
120
121
            // shared RoPE key
122
0
            k_pe = ggml_rope_ext(
123
0
                    ctx0, k_pe, inp_pos, rope_factors,
124
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
125
0
                    ext_factor, attn_factor, beta_fast, beta_slow
126
0
                    );
127
0
            cb(k_pe, "k_pe", il);
128
129
0
            ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0);
130
0
            cb(q_states, "q_states", il);
131
132
0
            ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0);
133
0
            cb(k_states, "k_states", il);
134
135
0
            cur = build_attn(inp_attn,
136
0
                    model.layers[il].wo, NULL,
137
0
                    q_states, k_states, v_states, nullptr, nullptr, nullptr, kq_scale, il);
138
0
        }
139
0
        if (il == n_layer - 1 && inp_out_ids) {
140
0
            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
141
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
142
0
        }
143
        // scale_res - scale the hidden states for residual connection
144
0
        const float scale_res = scale_depth/sqrtf(float(n_layer)); // TODO: is this correct?
145
0
        cur = ggml_scale(ctx0, cur, scale_res);
146
0
        cb(cur, "hidden_scaled", il);
147
148
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
149
0
        cb(ffn_inp, "ffn_inp", il);
150
151
        // feed-forward network
152
0
        {
153
0
            cur = build_norm(ffn_inp,
154
0
                    model.layers[il].ffn_norm, NULL,
155
0
                    LLM_NORM_RMS, il);
156
0
            cb(cur, "ffn_norm", il);
157
158
0
            cur = build_ffn(cur,
159
0
                    model.layers[il].ffn_up,   NULL, NULL,
160
0
                    model.layers[il].ffn_gate, NULL, NULL,
161
0
                    model.layers[il].ffn_down, NULL, NULL,
162
0
                    NULL,
163
0
                    LLM_FFN_SILU, LLM_FFN_PAR, il);
164
0
            cb(cur, "ffn_out", il);
165
0
        }
166
        // scale the hidden states for residual connection
167
0
        cur = ggml_scale(ctx0, cur, scale_res);
168
0
        cb(cur, "hidden_scaled_ffn", 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
0
    cur = inpL;
179
180
0
    cur = build_norm(cur,
181
0
            model.output_norm, NULL,
182
0
            LLM_NORM_RMS, -1);
183
184
0
    cb(cur, "result_norm", -1);
185
0
    res->t_embd = cur;
186
187
    // lm_head scaling
188
0
    const float scale_lmhead = float(n_embd_base)/float(n_embd);
189
0
    cur = ggml_scale(ctx0, cur, scale_lmhead);
190
0
    cb(cur, "lmhead_scaling", -1);
191
192
    // lm_head
193
0
    cur = build_lora_mm(model.output, cur);
194
195
0
    cb(cur, "result_output", -1);
196
0
    res->t_logits = cur;
197
198
0
    ggml_build_forward_expand(gf, cur);
199
0
}