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/apertus.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_apertus::load_arch_hparams(llama_model_loader & ml) {
4
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
5
6
0
    ml.get_key_or_arr(LLM_KV_XIELU_ALPHA_N, hparams.xielu_alpha_n, hparams.n_layer());
7
0
    ml.get_key_or_arr(LLM_KV_XIELU_ALPHA_P, hparams.xielu_alpha_p, hparams.n_layer());
8
0
    ml.get_key_or_arr(LLM_KV_XIELU_BETA,    hparams.xielu_beta,    hparams.n_layer());
9
0
    ml.get_key_or_arr(LLM_KV_XIELU_EPS,     hparams.xielu_eps,     hparams.n_layer());
10
11
0
    switch (hparams.n_layer()) {
12
0
        case 32: type = LLM_TYPE_8B; break;
13
0
        default: type = LLM_TYPE_UNKNOWN;
14
0
    }
15
0
}
16
17
0
void llama_model_apertus::load_arch_tensors(llama_model_loader &) {
18
0
    LLAMA_LOAD_LOCALS;
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
0
    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), { n_embd, n_vocab }, 0);
25
26
0
    for (int i = 0; i < n_layer; ++i) {
27
0
        auto & layer = layers[i];
28
29
0
        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
30
31
0
        if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) {
32
0
            layer.rope_long  = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG,  "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
33
0
            layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
34
0
        } else {
35
0
            layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
36
0
        }
37
38
0
        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_gqa, n_embd_gqa, 0);
39
0
        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0);
40
41
        // optional bias tensors
42
0
        layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED);
43
44
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
45
0
        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);
46
0
        layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0);
47
48
        // Q and K layernorms for Apertus
49
0
        layer.attn_q_norm   = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), { n_embd_head_k }, 0);
50
0
        layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias",   i), { n_embd_head_k }, TENSOR_NOT_REQUIRED);
51
0
        layer.attn_k_norm   = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), { n_embd_head_k }, 0);
52
0
        layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias",   i), { n_embd_head_k }, TENSOR_NOT_REQUIRED);
53
0
    }
54
0
}
55
56
0
std::unique_ptr<llm_graph_context> llama_model_apertus::build_arch_graph(const llm_graph_params & params) const {
57
0
    return std::make_unique<graph>(*this, params);
58
0
}
59
60
0
llama_model_apertus::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
61
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
62
63
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
64
0
    GGML_ASSERT(n_embd_head == n_rot);
65
66
0
    ggml_tensor * cur;
67
0
    ggml_tensor * inpL;
68
69
0
    inpL = build_inp_embd(model.tok_embd);
70
71
0
    ggml_tensor * inp_pos  = build_inp_pos();
72
0
    auto *        inp_attn = build_attn_inp_kv();
73
74
0
    const float kq_scale =
75
0
        hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
76
77
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
78
79
0
    for (int il = 0; il < n_layer; ++il) {
80
0
        ggml_tensor * inpSA = inpL;
81
82
0
        cur = build_norm(inpL, model.layers[il].attn_norm, nullptr, LLM_NORM_RMS, il);
83
0
        cb(cur, "attn_norm", il);
84
85
        // self-attention
86
0
        {
87
0
            ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
88
89
            // compute Q and K and RoPE them
90
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
91
0
                    n_embd_head, n_head, n_head_kv, il);
92
93
0
            Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
94
0
            cb(Qcur, "Qcur_normed", il);
95
96
0
            Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
97
0
            cb(Kcur, "Kcur_normed", il);
98
99
0
            Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
100
0
                                 ext_factor, attn_factor, beta_fast, beta_slow);
101
102
0
            Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
103
0
                                 ext_factor, attn_factor, beta_fast, beta_slow);
104
105
0
            cb(Qcur, "Qcur_pos", il);
106
0
            cb(Kcur, "Kcur_pos", il);
107
0
            cb(Vcur, "Vcur_pos", il);
108
109
0
            cur = build_attn(inp_attn,
110
0
                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
111
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
112
0
            cb(cur, "attn_out", il);
113
0
        }
114
115
0
        if (il == n_layer - 1 && inp_out_ids) {
116
0
            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);
117
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
118
0
        }
119
120
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
121
0
        cb(ffn_inp, "ffn_inp", il);
122
123
        // feed-forward network with xIELU activation
124
0
        {
125
0
            cur = build_norm(ffn_inp, model.layers[il].ffn_norm, nullptr, LLM_NORM_RMS, il);
126
0
            cb(cur, "ffn_norm", il);
127
128
            // Up projection
129
0
            ggml_tensor * up = build_lora_mm(model.layers[il].ffn_up, cur);
130
0
            cb(up, "ffn_up", il);
131
132
0
            float alpha_n_val = hparams.xielu_alpha_n[il];
133
0
            float alpha_p_val = hparams.xielu_alpha_p[il];
134
0
            float beta_val    = hparams.xielu_beta[il];
135
0
            float eps_val     = hparams.xielu_eps[il];
136
137
            // Apply xIELU activation
138
0
            ggml_tensor * activated = ggml_xielu(ctx0, up, alpha_n_val, alpha_p_val, beta_val, eps_val);
139
0
            cb(activated, "ffn_xielu", il);
140
141
            // Down projection
142
0
            cur = build_lora_mm(model.layers[il].ffn_down, activated);
143
0
            cb(cur, "ffn_down", il);
144
0
        }
145
146
0
        cur = ggml_add(ctx0, cur, ffn_inp);
147
0
        cb(cur, "ffn_out", il);
148
149
0
        cur = build_cvec(cur, il);
150
0
        cb(cur, "l_out", il);
151
152
        // input for next layer
153
0
        inpL = cur;
154
0
    }
155
156
0
    cur = inpL;
157
158
0
    cur = build_norm(cur, model.output_norm, nullptr, LLM_NORM_RMS, -1);
159
160
0
    cb(cur, "result_norm", -1);
161
0
    res->t_embd = cur;
162
163
    // lm_head
164
0
    cur = build_lora_mm(model.output, cur, model.output_s);
165
166
0
    cb(cur, "result_output", -1);
167
0
    res->t_logits = cur;
168
169
0
    ggml_build_forward_expand(gf, cur);
170
0
}