Coverage Report

Created: 2026-06-13 06:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/src/models/cohere2.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_cohere2::load_arch_hparams(llama_model_loader & ml) {
4
0
    hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
5
0
    uint32_t swa_period = 4;
6
0
    ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false);
7
0
    hparams.set_swa_pattern(swa_period);
8
9
0
    hparams.rope_freq_base_train_swa  = hparams.rope_freq_base_train;
10
0
    hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;
11
12
0
    ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA,       hparams.rope_freq_base_train_swa, false);
13
0
    ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa);
14
0
    ml.get_key(LLM_KV_LOGIT_SCALE,              hparams.f_logit_scale);
15
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS,  hparams.f_norm_eps);
16
17
0
    switch (hparams.n_layer()) {
18
0
        case 32: type = LLM_TYPE_8B; break;
19
0
        default: type = LLM_TYPE_UNKNOWN;
20
0
    }
21
0
}
22
23
0
void llama_model_cohere2::load_arch_tensors(llama_model_loader &) {
24
0
    LLAMA_LOAD_LOCALS;
25
26
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
27
28
    // output
29
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
30
    // init output from the input tok embed
31
0
    output      = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab },
32
0
                                      TENSOR_DUPLICATED);
33
34
0
    for (int i = 0; i < n_layer; ++i) {
35
0
        auto & layer = layers[i];
36
37
0
        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
38
39
0
        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);
40
0
        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);
41
42
0
        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, 0);
43
0
        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);
44
0
        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0);
45
0
    }
46
0
}
47
48
0
std::unique_ptr<llm_graph_context> llama_model_cohere2::build_arch_graph(const llm_graph_params & params) const {
49
0
    return std::make_unique<graph>(*this, params);
50
0
}
51
52
0
llama_model_cohere2::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
53
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
54
55
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
56
57
0
    const float f_logit_scale = hparams.f_logit_scale;
58
59
0
    ggml_tensor * cur;
60
0
    ggml_tensor * inpL;
61
62
0
    inpL = build_inp_embd(model.tok_embd);
63
64
    // inp_pos - contains the positions
65
0
    ggml_tensor * inp_pos = build_inp_pos();
66
67
0
    auto * inp_attn = build_attn_inp_kv_iswa();
68
69
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
70
71
0
    for (int il = 0; il < n_layer; ++il) {
72
0
        const bool is_swa = hparams.is_swa(il);
73
        // UNUSED:
74
        // const float freq_base_l  = model.get_rope_freq_base (cparams, il);
75
        // const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
76
77
        // norm
78
0
        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM, il);
79
0
        cb(cur, "attn_norm", il);
80
0
        ggml_tensor * ffn_inp = cur;
81
82
        // self-attention
83
0
        {
84
            // rope freq factors for 128k context
85
0
            ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
86
87
            // compute Q and K and RoPE them
88
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
89
0
                    n_embd_head, n_head, n_head_kv, il);
90
91
0
            if (is_swa) {
92
0
                Qcur = ggml_rope_ext(
93
0
                        ctx0, Qcur, inp_pos, rope_factors,
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, rope_factors,
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
0
            }
104
105
0
            cb(Qcur, "Qcur", il);
106
0
            cb(Kcur, "Kcur", il);
107
0
            cb(Vcur, "Vcur", 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, 1.0f/sqrtf(float(n_embd_head)), il);
112
0
        }
113
114
0
        if (il == n_layer - 1 && inp_out_ids) {
115
0
            cur     = ggml_get_rows(ctx0, cur, inp_out_ids);
116
0
            inpL    = ggml_get_rows(ctx0, inpL, inp_out_ids);
117
0
            ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids);
118
0
        }
119
120
0
        ggml_tensor * attn_out = cur;
121
122
        // feed-forward network
123
0
        {
124
0
            cur = build_ffn(ffn_inp,
125
0
                    model.layers[il].ffn_up, NULL, NULL,
126
0
                    model.layers[il].ffn_gate, NULL, NULL,
127
0
                    model.layers[il].ffn_down, NULL, NULL,
128
0
                    NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
129
0
            cb(cur, "ffn_out", il);
130
0
        }
131
132
        // add together residual + FFN + self-attention
133
0
        cur = ggml_add(ctx0, cur, inpL);
134
0
        cur = ggml_add(ctx0, cur, attn_out);
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
143
0
    cur = inpL;
144
145
0
    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM, -1);
146
147
0
    cb(cur, "result_norm", -1);
148
0
    res->t_embd = cur;
149
150
    // lm_head
151
0
    cur = build_lora_mm(model.output, cur, model.output_s);
152
153
0
    if (f_logit_scale) {
154
0
        cur = ggml_scale(ctx0, cur, f_logit_scale);
155
0
    }
156
157
0
    cb(cur, "result_output", -1);
158
0
    res->t_logits = cur;
159
160
0
    ggml_build_forward_expand(gf, cur);
161
0
}