Coverage Report

Created: 2026-01-10 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/src/models/smallthinker.cpp
Line
Count
Source
1
#include "models.h"
2
3
template <bool iswa>
4
0
llm_build_smallthinker<iswa>::llm_build_smallthinker(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
0
    GGML_ASSERT(n_embd_head == hparams.n_rot);
9
10
0
    ggml_tensor * cur;
11
0
    ggml_tensor * inpL;
12
13
0
    inpL = build_inp_embd(model.tok_embd);
14
15
    // inp_pos - contains the positions
16
0
    ggml_tensor * inp_pos = build_inp_pos();
17
18
0
    using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
19
0
    inp_attn_type * inp_attn = nullptr;
20
21
0
    if constexpr (iswa) {
22
0
        inp_attn = build_attn_inp_kv_iswa();
23
0
    } else {
24
0
        inp_attn = build_attn_inp_kv();
25
0
    }
26
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
27
28
0
    for (int il = 0; il < n_layer; ++il) {
29
0
        const float freq_base_l  = model.get_rope_freq_base (cparams, il);
30
0
        const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
31
32
0
        ggml_tensor * inpSA  = inpL;
33
34
        // This overlaps with SWA layers in current models, so get_rope_freq_base/scale may be superfluous
35
0
        const bool use_rope = hparams.n_no_rope_layer_step == n_layer ||
36
0
                              il % hparams.n_no_rope_layer_step != 0;
37
38
0
        ggml_tensor * probs = build_lora_mm(model.layers[il].ffn_gate_inp, inpL);  // [n_expert, n_tokens]
39
0
        cb(probs, "ffn_moe_logits", il);
40
41
        // norm
42
0
        cur = build_norm(inpL,model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
43
0
        cb(cur, "attn_norm", il);
44
45
        // self_attention
46
0
        {
47
            // compute Q and K and RoPE them
48
0
            struct ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
49
0
            cb(Qcur, "Qcur", il);
50
51
0
            struct ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
52
0
            cb(Kcur, "Kcur", il);
53
54
0
            struct ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
55
0
            cb(Vcur, "Vcur", il);
56
57
0
            Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
58
0
            Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
59
0
            Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
60
61
0
            if (use_rope) {
62
0
                Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
63
0
                                    ext_factor, attn_factor, beta_fast, beta_slow);
64
65
0
                Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
66
0
                                    ext_factor, attn_factor, beta_fast, beta_slow);
67
0
            }
68
0
            cb(Qcur, "Qcur", il);
69
0
            cb(Kcur, "Kcur", il);
70
71
0
            cur = build_attn(inp_attn,
72
0
                    model.layers[il].wo, model.layers[il].bo,
73
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
74
0
        }
75
0
        if (il == n_layer - 1 && inp_out_ids) {
76
0
            cur = ggml_get_rows(ctx0, cur, inp_out_ids);
77
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
78
0
            probs = ggml_get_rows(ctx0, probs, inp_out_ids);
79
0
        }
80
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
81
0
        cb(ffn_inp, "ffn_inp", il);
82
83
        // MoE branch
84
0
        cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
85
0
        cb(cur, "ffn_norm", il);
86
87
0
        ggml_tensor * ffn_out =
88
0
            build_moe_ffn(cur,
89
0
                    nullptr,
90
0
                    model.layers[il].ffn_up_exps,
91
0
                    model.layers[il].ffn_gate_exps,
92
0
                    model.layers[il].ffn_down_exps,
93
0
                    nullptr,
94
0
                    n_expert, n_expert_used,
95
0
                    LLM_FFN_RELU, true,
96
0
                    false, 0.0,
97
0
                    static_cast<llama_expert_gating_func_type>(hparams.expert_gating_func),
98
0
                    il, probs);
99
100
0
        cb(ffn_out, "ffn_out", il);
101
0
        cur = ffn_out;
102
103
0
        cur = ggml_add(ctx0, cur, ffn_inp);
104
0
        cur = build_cvec(cur, il);
105
0
        cb(cur, "l_out", il);
106
107
        // input for next layer
108
0
        inpL = cur;
109
0
    }
110
0
    cur = inpL;
111
112
0
    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
113
0
    cb(cur, "result_norm", -1);
114
0
    res->t_embd = cur;
115
116
    // lm_head
117
0
    cur = build_lora_mm(model.output, cur);
118
0
    cb(cur, "result_output", -1);
119
0
    res->t_logits = cur;
120
121
0
    ggml_build_forward_expand(gf, cur);
122
0
}
Unexecuted instantiation: llm_build_smallthinker<false>::llm_build_smallthinker(llama_model const&, llm_graph_params const&)
Unexecuted instantiation: llm_build_smallthinker<true>::llm_build_smallthinker(llama_model const&, llm_graph_params const&)
123
124
// Explicit template instantiations
125
template struct llm_build_smallthinker<false>;
126
template struct llm_build_smallthinker<true>;