Coverage Report

Created: 2026-08-22 07:18

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
void llama_model_plm::load_arch_hparams(llama_model_loader & ml) {
4
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
5
0
    ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv);
6
7
0
    switch (hparams.n_layer()) {
8
0
        case 32: type = LLM_TYPE_1_8B; break;
9
0
        default: type = LLM_TYPE_UNKNOWN;
10
0
    }
11
0
}
12
13
0
void llama_model_plm::load_arch_tensors(llama_model_loader &) {
14
0
    LLAMA_LOAD_LOCALS;
15
16
0
    const int64_t n_embd_head_qk_rope = hparams.n_rot();
17
0
    const int64_t n_embd_head_qk_nope = hparams.n_embd_head_k() - hparams.n_rot();
18
0
    const int64_t kv_lora_rank = hparams.n_lora_kv;
19
20
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
21
22
    // output
23
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
24
    // output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, 0);
25
0
    output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
26
27
0
    for (int i = 0; i < n_layer; ++i) {
28
0
        auto & layer = layers[i];
29
30
0
        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
31
32
0
        layer.wq        = create_tensor(tn(LLM_TENSOR_ATTN_Q,   "weight", i), {n_embd, n_embd_head_k * n_head}, 0);
33
0
        layer.wkv_a_mqa = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_MQA, "weight", i), {n_embd, kv_lora_rank + (n_embd_head_qk_rope)}, 0);
34
0
        layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0);
35
0
        layer.wkv_b     = create_tensor(tn(LLM_TENSOR_ATTN_KV_B,     "weight", i), {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)}, 0);
36
0
        layer.wo        = create_tensor(tn(LLM_TENSOR_ATTN_OUT,      "weight", i), {              n_head * (                      n_embd_head_v), n_embd}, 0);
37
38
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
39
0
        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
40
0
        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
41
0
    }
42
0
}
43
44
0
std::unique_ptr<llm_graph_context> llama_model_plm::build_arch_graph(const llm_graph_params & params) const {
45
0
    return std::make_unique<graph>(*this, params);
46
0
}
47
48
0
llama_model_plm::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
49
0
    const float kq_scale = 1.0f/sqrtf(float(hparams.n_embd_head_k()));
50
51
0
    const uint32_t n_embd_head_qk_rope = hparams.n_rot();
52
0
    const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k() - hparams.n_rot();
53
54
0
    const uint32_t kv_lora_rank = hparams.n_lora_kv;
55
56
0
    ggml_tensor * cur;
57
0
    ggml_tensor * inpL;
58
59
    // {n_embd, n_tokens}
60
0
    inpL = build_inp_embd(model.tok_embd);
61
62
    // inp_pos - contains the positions
63
0
    ggml_tensor * inp_pos = build_inp_pos();
64
65
0
    auto * inp_attn = build_attn_inp_kv();
66
67
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
68
69
0
    for (int il = 0; il < n_layer; ++il) {
70
0
        ggml_tensor * inpSA = inpL;
71
72
        // norm
73
0
        cur = build_norm(inpL,
74
0
                model.layers[il].attn_norm, NULL,
75
0
                LLM_NORM_RMS, il);
76
0
        cb(cur, "attn_norm", il);
77
78
        // self_attention
79
0
        {
80
0
            ggml_tensor * q = NULL;
81
0
            q = ggml_mul_mat(ctx0, model.layers[il].wq, cur);
82
0
            cb(q, "q", il);
83
84
            // {n_embd_head_k, n_head, n_tokens}, RoPE is applied to the trailing dims only
85
0
            q = ggml_reshape_3d(ctx0, q, hparams.n_embd_head_k(), n_head, n_tokens);
86
0
            cb(q, "q", il);
87
88
            // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens}
89
0
            ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur);
90
0
            cb(kv_pe_compresseed, "kv_pe_compresseed", il);
91
92
            // split into {kv_lora_rank, n_tokens}
93
0
            ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens,
94
0
                    kv_pe_compresseed->nb[1],
95
0
                    0);
96
0
            cb(kv_compressed, "kv_compressed", il);
97
98
            // and {n_embd_head_qk_rope, n_tokens}
99
0
            ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens,
100
0
                    kv_pe_compresseed->nb[1],
101
0
                    kv_pe_compresseed->nb[1],
102
0
                    ggml_row_size(kv_pe_compresseed->type, kv_lora_rank));
103
0
            cb(k_pe, "k_pe", il);
104
105
0
            kv_compressed = build_norm(kv_compressed,
106
0
                    model.layers[il].attn_kv_a_norm, NULL,
107
0
                    LLM_NORM_RMS, il);
108
0
            cb(kv_compressed, "kv_compressed", il);
109
110
            // {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}
111
0
            ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed);
112
0
            cb(kv, "kv", il);
113
114
            // split into {n_head * n_embd_head_qk_nope, n_tokens}
115
0
            ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens,
116
0
                    ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v()),
117
0
                    ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v())),
118
0
                    0);
119
0
            cb(k_nope, "k_nope", il);
120
121
            // and {n_head * n_embd_head_v, n_tokens}
122
0
            ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v(), n_head, n_tokens,
123
0
                    ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v())),
124
0
                    ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v())*n_head),
125
0
                    ggml_row_size(kv->type, (n_embd_head_qk_nope)));
126
0
            cb(v_states, "v_states", il);
127
128
0
            v_states = ggml_cont(ctx0, v_states);
129
0
            cb(v_states, "v_states", il);
130
131
0
            v_states = ggml_view_2d(ctx0, v_states, hparams.n_embd_head_v() * n_head, n_tokens,
132
0
                    ggml_row_size(kv->type, hparams.n_embd_head_v() * n_head),
133
0
                    0);
134
0
            cb(v_states, "v_states", il);
135
136
0
            q = ggml_rope_ext(
137
0
                    ctx0, q, inp_pos, nullptr,
138
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
139
0
                    ext_factor, attn_factor, beta_fast, beta_slow
140
0
                    );
141
0
            q = ggml_rope_set_offset(q, n_embd_head_qk_nope);
142
0
            cb(q, "q_rope", il);
143
144
            // shared RoPE key
145
0
            k_pe = ggml_rope_ext(
146
0
                    ctx0, k_pe, inp_pos, nullptr,
147
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
148
0
                    ext_factor, attn_factor, beta_fast, beta_slow
149
0
                    );
150
0
            cb(k_pe, "k_pe", il);
151
152
0
            ggml_tensor * q_states = q;
153
0
            cb(q_states, "q_states", il);
154
155
0
            ggml_tensor * k_states = ggml_concat(ctx0, k_nope,
156
0
                    ggml_repeat_4d(ctx0, k_pe, n_embd_head_qk_rope, n_head, n_tokens, 1), 0);
157
0
            cb(k_states, "k_states", il);
158
159
0
            cur = build_attn(inp_attn,
160
0
                    model.layers[il].wo, NULL, model.layers[il].wo_s,
161
0
                    q_states, k_states, v_states, nullptr, nullptr, nullptr, kq_scale, il);
162
0
        }
163
0
        if (il == n_layer - 1 && inp_out_ids) {
164
0
            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
165
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
166
0
        }
167
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
168
0
        cb(ffn_inp, "ffn_inp", il);
169
170
0
        cur = build_norm(ffn_inp,
171
0
                model.layers[il].ffn_norm, NULL,
172
0
                LLM_NORM_RMS, il);
173
0
        cb(cur, "ffn_norm", il);
174
175
0
        cur = build_ffn(cur,
176
0
                model.layers[il].ffn_up,   NULL, NULL,
177
0
                NULL, NULL, NULL,
178
0
                model.layers[il].ffn_down, NULL, NULL,
179
0
                NULL,
180
0
                LLM_FFN_RELU_SQR, LLM_FFN_SEQ, il);
181
0
        cb(cur, "ffn_out", il);
182
183
0
        cur = ggml_add(ctx0, cur, ffn_inp);
184
185
0
        cur = build_cvec(cur, il);
186
0
        cb(cur, "l_out", il);
187
188
        // input for next layer
189
0
        inpL = cur;
190
0
    }
191
0
    cur = inpL;
192
193
0
    cur = build_norm(cur,
194
0
            model.output_norm, NULL,
195
0
            LLM_NORM_RMS, -1);
196
197
0
    cb(cur, "result_norm", -1);
198
0
    res->t_embd = cur;
199
200
0
    cur = build_lora_mm(model.output, cur, model.output_s);
201
202
0
    cb(cur, "result_output", -1);
203
0
    res->t_logits = cur;
204
205
0
    ggml_build_forward_expand(gf, cur);
206
0
}