Coverage Report

Created: 2025-11-24 06:10

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