Coverage Report

Created: 2026-02-26 07:05

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