Coverage Report

Created: 2026-06-22 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/src/models/starcoder2.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_starcoder2::load_arch_hparams(llama_model_loader & ml) {
4
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
5
6
0
    switch (hparams.n_layer()) {
7
0
        case 30: type = LLM_TYPE_3B; break;
8
0
        case 32: type = LLM_TYPE_7B; break;
9
0
        case 40: type = LLM_TYPE_15B; break;
10
0
        case 52: type = LLM_TYPE_20B; break; // granite
11
0
        case 88: type = LLM_TYPE_34B; break; // granite
12
0
        default: type = LLM_TYPE_UNKNOWN;
13
0
    }
14
0
}
15
16
0
void llama_model_starcoder2::load_arch_tensors(llama_model_loader &) {
17
0
    LLAMA_LOAD_LOCALS;
18
19
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
20
21
    // output
22
0
    output_norm   = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
23
0
    output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"),   {n_embd}, 0);
24
25
0
    output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
26
    // if output is NULL, init from the input tok embed
27
0
    if (output == NULL) {
28
0
        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
29
0
    }
30
31
0
    for (int i = 0; i < n_layer; ++i) {
32
0
        auto & layer = layers[i];
33
34
0
        layer.attn_norm   = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
35
0
        layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i),   {n_embd}, 0);
36
37
0
        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);
38
0
        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
39
40
        // optional bias tensors
41
0
        layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
42
43
0
        layer.ffn_norm   = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
44
0
        layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i),   {n_embd}, 0);
45
46
0
        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
47
0
        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
48
49
        // optional bias tensors
50
0
        layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0);
51
0
        layer.ffn_up_b   = create_tensor(tn(LLM_TENSOR_FFN_UP ,  "bias", i), {  n_ff}, 0);
52
0
    }
53
0
}
54
55
0
std::unique_ptr<llm_graph_context> llama_model_starcoder2::build_arch_graph(const llm_graph_params & params) const {
56
0
    return std::make_unique<graph>(*this, params);
57
0
}
58
59
0
llama_model_starcoder2::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
60
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
61
62
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
63
0
    GGML_ASSERT(n_embd_head == n_rot);
64
65
0
    ggml_tensor * cur;
66
0
    ggml_tensor * inpL;
67
68
0
    inpL = build_inp_embd(model.tok_embd);
69
70
    // inp_pos - contains the positions
71
0
    ggml_tensor * inp_pos = build_inp_pos();
72
73
0
    auto * inp_attn = build_attn_inp_kv();
74
75
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
76
77
0
    for (int il = 0; il < n_layer; ++il) {
78
0
        ggml_tensor * inpSA = inpL;
79
80
        // norm
81
0
        cur = build_norm(inpL,
82
0
                model.layers[il].attn_norm, model.layers[il].attn_norm_b,
83
0
                LLM_NORM, il);
84
0
        cb(cur, "attn_norm", il);
85
86
        // self-attention
87
0
        {
88
            // compute Q and K and RoPE them
89
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
90
0
                    n_embd_head, n_head, n_head_kv, il);
91
92
0
            Qcur = ggml_rope_ext(
93
0
                    ctx0, Qcur, inp_pos, nullptr,
94
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
95
0
                    ext_factor, attn_factor, beta_fast, beta_slow
96
0
                    );
97
98
0
            Kcur = ggml_rope_ext(
99
0
                    ctx0, Kcur, inp_pos, nullptr,
100
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
101
0
                    ext_factor, attn_factor, beta_fast, beta_slow
102
0
                    );
103
104
0
            cb(Qcur, "Qcur", il);
105
0
            cb(Kcur, "Kcur", il);
106
0
            cb(Vcur, "Vcur", il);
107
108
0
            cur = build_attn(inp_attn,
109
0
                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
110
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
111
0
        }
112
0
        if (il == n_layer - 1 && inp_out_ids) {
113
0
            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
114
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
115
0
        }
116
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
117
0
        cb(ffn_inp, "ffn_inp", il);
118
119
        // feed-forward network
120
121
0
        cur = build_norm(ffn_inp,
122
0
                model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
123
0
                LLM_NORM, il);
124
0
        cb(cur, "ffn_norm", il);
125
126
0
        cur = build_ffn(cur,
127
0
                model.layers[il].ffn_up,   model.layers[il].ffn_up_b,   NULL,
128
0
                NULL,                      NULL,                        NULL,
129
0
                model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
130
0
                NULL,
131
0
                LLM_FFN_GELU, LLM_FFN_SEQ, il);
132
0
        cb(cur, "ffn_out", il);
133
134
0
        cur = ggml_add(ctx0, cur, ffn_inp);
135
136
0
        cur = build_cvec(cur, il);
137
0
        cb(cur, "l_out", il);
138
139
        // input for next layer
140
0
        inpL = cur;
141
0
    }
142
0
    cur = inpL;
143
144
0
    cur = build_norm(cur,
145
0
            model.output_norm, model.output_norm_b,
146
0
            LLM_NORM, -1);
147
148
0
    cb(cur, "result_norm", -1);
149
0
    res->t_embd = cur;
150
151
    // lm_head
152
0
    cur = build_lora_mm(model.output, cur, model.output_s);
153
154
0
    cb(cur, "result_output", -1);
155
0
    res->t_logits = cur;
156
157
0
    ggml_build_forward_expand(gf, cur);
158
0
}