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/gptneox.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_gptneox::load_arch_hparams(llama_model_loader & ml) {
4
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps);
5
0
    ml.get_key(LLM_KV_USE_PARALLEL_RESIDUAL,   hparams.use_par_res);
6
7
0
    switch (hparams.n_layer()) {
8
0
        case 6:
9
0
            switch (hparams.n_ff()) {
10
0
                case 512:  type = LLM_TYPE_14M; break;
11
0
                case 2048: type = LLM_TYPE_70M; break;
12
0
                default:   type = LLM_TYPE_UNKNOWN;
13
0
            } break;
14
0
        case 12:
15
0
            switch (hparams.n_ff()) {
16
0
                case 3072: type = LLM_TYPE_160M; break;
17
0
                default: type = LLM_TYPE_UNKNOWN;
18
0
            } break;
19
0
        case 16:
20
0
            switch (hparams.n_ff()) {
21
0
                case 8192: type = LLM_TYPE_1B; break;
22
0
                default: type = LLM_TYPE_UNKNOWN;
23
0
            } break;
24
0
        case 24:
25
0
            switch (hparams.n_ff()) {
26
0
                case 4096: type = LLM_TYPE_410M; break;
27
0
                case 8192: type = LLM_TYPE_1_4B; break;
28
0
                default: type = LLM_TYPE_UNKNOWN;
29
0
            } break;
30
0
        case 32:
31
0
            switch (hparams.n_ff()) {
32
0
                case 10240: type = LLM_TYPE_2_8B; break;
33
0
                case 16384: type = LLM_TYPE_6_9B; break;
34
0
                default: type = LLM_TYPE_UNKNOWN;
35
0
            } break;
36
0
        case 36:
37
0
            switch (hparams.n_ff()) {
38
0
                case 20480: type = LLM_TYPE_12B; break;
39
0
                default: type = LLM_TYPE_UNKNOWN;
40
0
            } break;
41
0
        case 44:
42
0
            switch (hparams.n_ff()) {
43
0
                case 24576: type = LLM_TYPE_20B; break;
44
0
                default: type = LLM_TYPE_UNKNOWN;
45
0
            } break;
46
0
        default: type = LLM_TYPE_UNKNOWN;
47
0
    }
48
0
}
49
50
0
void llama_model_gptneox::load_arch_tensors(llama_model_loader &) {
51
0
    LLAMA_LOAD_LOCALS;
52
53
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
54
55
    // output
56
0
    output_norm   = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
57
0
    output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"),   {n_embd}, 0);
58
0
    output        = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, 0);
59
60
0
    for (int i = 0; i < n_layer; ++i) {
61
0
        auto & layer = layers[i];
62
63
0
        layer.attn_norm   = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
64
0
        layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i),   {n_embd}, 0);
65
66
0
        layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0);
67
0
        layer.wqkv_b = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0);
68
69
0
        layer.wo   = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
70
0
        layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i),   {n_embd}, 0);
71
72
0
        layer.ffn_norm   = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
73
0
        layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i),   {n_embd}, 0);
74
75
0
        layer.ffn_down   = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
76
0
        layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i),   {n_embd}, 0);
77
78
0
        layer.ffn_up     = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, n_ff}, 0);
79
0
        layer.ffn_up_b   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "bias", i),   {n_ff}, 0);
80
0
    }
81
0
}
82
83
0
std::unique_ptr<llm_graph_context> llama_model_gptneox::build_arch_graph(const llm_graph_params & params) const {
84
0
    return std::make_unique<graph>(*this, params);
85
0
}
86
87
0
llama_model_gptneox::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
88
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
89
90
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
91
92
0
    ggml_tensor * cur;
93
0
    ggml_tensor * inpL;
94
95
0
    inpL = build_inp_embd(model.tok_embd);
96
97
    // inp_pos - contains the positions
98
0
    ggml_tensor * inp_pos = build_inp_pos();
99
100
0
    auto * inp_attn = build_attn_inp_kv();
101
102
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
103
104
0
    for (int il = 0; il < n_layer; ++il) {
105
0
        cur = build_norm(inpL,
106
0
                model.layers[il].attn_norm,
107
0
                model.layers[il].attn_norm_b,
108
0
                LLM_NORM, il);
109
0
        cb(cur, "attn_norm", il);
110
111
        // self-attention
112
0
        {
113
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
114
0
                    n_embd_head, n_head, n_head_kv, il);
115
116
0
            Qcur = ggml_rope_ext(
117
0
                    ctx0, Qcur, inp_pos, nullptr,
118
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
119
0
                    ext_factor, attn_factor, beta_fast, beta_slow
120
0
                    );
121
122
0
            Kcur = ggml_rope_ext(
123
0
                    ctx0, Kcur, inp_pos, nullptr,
124
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
125
0
                    ext_factor, attn_factor, beta_fast, beta_slow
126
0
                    );
127
128
0
            cb(Qcur, "Qcur", il);
129
0
            cb(Kcur, "Kcur", il);
130
0
            cb(Vcur, "Vcur", il);
131
132
0
            cur = build_attn(inp_attn,
133
0
                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
134
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
135
0
        }
136
137
0
        if (il == n_layer - 1 && inp_out_ids) {
138
0
            cur  = ggml_get_rows(ctx0,  cur, inp_out_ids);
139
0
            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
140
0
        }
141
142
        // ffn
143
0
        if (hparams.use_par_res) {
144
            // attention and ffn are computed in parallel
145
            // x = x + attn(ln1(x)) + ffn(ln2(x))
146
147
0
            ggml_tensor * attn_out = cur;
148
149
0
            cur = build_norm(inpL,
150
0
                    model.layers[il].ffn_norm,
151
0
                    model.layers[il].ffn_norm_b,
152
0
                    LLM_NORM, il);
153
0
            cb(cur, "ffn_norm", il);
154
155
0
            cur = build_ffn(cur,
156
0
                    model.layers[il].ffn_up,   model.layers[il].ffn_up_b,   NULL,
157
0
                    NULL,                      NULL,                        NULL,
158
0
                    model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
159
0
                    NULL,
160
0
                    LLM_FFN_GELU, LLM_FFN_SEQ, il);
161
0
            cb(cur, "ffn_out", il);
162
163
0
            cur = ggml_add(ctx0, cur, inpL);
164
0
            cb(cur, "ffn_out", il);
165
166
0
            cur = ggml_add(ctx0, cur, attn_out);
167
168
0
            cur = build_cvec(cur, il);
169
0
            cb(cur, "l_out", il);
170
171
            // input for next layer
172
0
            inpL = cur;
173
0
        } else {
174
            // attention and ffn are computed sequentially
175
            // x = x + attn(ln1(x))
176
            // x = x + ffn(ln2(x))
177
178
0
            ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpL);
179
0
            cb(ffn_inp, "ffn_inp", il);
180
181
0
            cur = build_norm(ffn_inp,
182
0
                    model.layers[il].ffn_norm,
183
0
                    model.layers[il].ffn_norm_b,
184
0
                    LLM_NORM, il);
185
0
            cb(cur, "ffn_norm", il);
186
187
0
            cur = build_ffn(cur,
188
0
                    model.layers[il].ffn_up,   model.layers[il].ffn_up_b,   NULL,
189
0
                    NULL,                      NULL,                        NULL,
190
0
                    model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
191
0
                    NULL,
192
0
                    LLM_FFN_GELU, LLM_FFN_SEQ, il);
193
0
            cb(cur, "ffn_out", il);
194
195
0
            cur = ggml_add(ctx0, cur, ffn_inp);
196
197
0
            cur = build_cvec(cur, il);
198
0
            cb(cur, "l_out", il);
199
200
            // input for next layer
201
0
            inpL = cur;
202
0
        }
203
0
    }
204
205
0
    cur = build_norm(inpL,
206
0
            model.output_norm,
207
0
            model.output_norm_b,
208
0
            LLM_NORM, -1);
209
210
0
    cb(cur, "result_norm", -1);
211
0
    res->t_embd = cur;
212
213
0
    cur = build_lora_mm(model.output, cur, model.output_s);
214
215
0
    cb(cur, "result_output", -1);
216
0
    res->t_logits = cur;
217
218
0
    ggml_build_forward_expand(gf, cur);
219
0
}