Coverage Report

Created: 2026-07-16 06:35

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/src/models/cogvlm.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_cogvlm::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
    switch (hparams.n_layer()) {
7
0
        case 32: type = LLM_TYPE_13B; break;
8
0
        default: type = LLM_TYPE_UNKNOWN;
9
0
    }
10
0
}
11
12
0
void llama_model_cogvlm::load_arch_tensors(llama_model_loader &) {
13
0
    LLAMA_LOAD_LOCALS;
14
15
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
16
17
    // output
18
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
19
0
    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
20
21
    // if output is NULL, init from the input tok embed
22
0
    if (output == NULL) {
23
0
        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
24
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
0
        layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd_head_k * n_head * 3}, 0);
31
0
        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
32
33
0
        layer.visexp_attn_wqkv = create_tensor(tn(LLM_TENSOR_VISEXP_ATTN_QKV, "weight", i), {n_embd, n_embd_head_k * n_head * 3}, 0);
34
0
        layer.visexp_attn_wo = create_tensor(tn(LLM_TENSOR_VISEXP_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
35
36
0
        layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
37
38
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
39
0
        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);
40
0
        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
41
0
        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
42
43
0
        layer.visexp_ffn_gate = create_tensor(tn(LLM_TENSOR_VISEXP_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);
44
0
        layer.visexp_ffn_down = create_tensor(tn(LLM_TENSOR_VISEXP_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
45
0
        layer.visexp_ffn_up   = create_tensor(tn(LLM_TENSOR_VISEXP_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
46
0
    }
47
0
}
48
49
0
std::unique_ptr<llm_graph_context> llama_model_cogvlm::build_arch_graph(const llm_graph_params & params) const {
50
0
    return std::make_unique<graph>(*this, params);
51
0
}
52
53
llama_model_cogvlm::graph::graph(const llama_model & model, const llm_graph_params & params) :
54
0
    llm_graph_context(params) {
55
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
56
0
    const float   kq_scale    = 1.0f / sqrtf(float(n_embd_head));
57
58
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
59
0
    GGML_ASSERT(n_embd_head == n_rot);
60
61
0
    ggml_tensor * inpL;
62
0
    ggml_tensor * cur;
63
64
0
    inpL = build_inp_embd(model.tok_embd);
65
66
0
    ggml_tensor * inp_pos = build_inp_pos();
67
68
0
    auto * inp_attn = build_attn_inp_kv();
69
70
    // check ubatch to see if we have input tokens (text)
71
    // or an input embedding vector (image)
72
0
    bool is_text;
73
0
    if (ubatch.token) {
74
0
        is_text = true;
75
0
    } else {
76
0
        is_text = false;
77
0
    }
78
79
0
    for (int il = 0; il < n_layer; ++il) {
80
        // get either the text or image weight tensors
81
0
        ggml_tensor *wqkv, *wo, *wo_s;
82
0
        ggml_tensor *ffn_gate, *ffn_down, *ffn_up;
83
84
0
        if (is_text) {
85
0
            wqkv     = model.layers[il].wqkv;
86
0
            wo       = model.layers[il].wo;
87
0
            wo_s     = model.layers[il].wo_s;
88
0
            ffn_gate = model.layers[il].ffn_gate;
89
0
            ffn_down = model.layers[il].ffn_down;
90
0
            ffn_up   = model.layers[il].ffn_up;
91
0
        } else {
92
0
            wqkv     = model.layers[il].visexp_attn_wqkv;
93
0
            wo       = model.layers[il].visexp_attn_wo;
94
0
            wo_s     = nullptr;
95
0
            ffn_gate = model.layers[il].visexp_ffn_gate;
96
0
            ffn_down = model.layers[il].visexp_ffn_down;
97
0
            ffn_up   = model.layers[il].visexp_ffn_up;
98
0
        }
99
100
0
        ggml_tensor * inpSA = inpL;
101
0
        cur = build_norm(inpSA, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
102
103
        // build self attention
104
0
        {
105
0
            ggml_tensor * qkv = build_lora_mm(wqkv, cur);
106
107
            // split qkv into Q, K, V along the first dimension
108
0
            ggml_tensor * Qcur =
109
0
                ggml_view_3d(ctx0, qkv, n_embd_head, n_head, n_tokens, n_embd_head * sizeof(float), qkv->nb[1], 0);
110
0
            ggml_tensor * Kcur = ggml_view_3d(ctx0, qkv, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float),
111
0
                                              qkv->nb[1], n_embd * ggml_element_size(qkv));
112
0
            ggml_tensor * Vcur = ggml_view_3d(ctx0, qkv, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float),
113
0
                                              qkv->nb[1], 2 * n_embd * ggml_element_size(qkv));
114
115
0
            Qcur = ggml_rope(ctx0, Qcur, inp_pos, n_embd_head, rope_type);
116
0
            Kcur = ggml_rope(ctx0, Kcur, inp_pos, n_embd_head, rope_type);
117
118
0
            cur = build_attn(inp_attn,
119
0
                wo, nullptr, wo_s,
120
0
                Qcur, Kcur, Vcur,
121
0
                nullptr, nullptr, nullptr,
122
0
                kq_scale, il);
123
0
            cb(cur, "attn_out", il);
124
0
        }
125
126
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
127
0
        cb(ffn_inp, "ffn_inp", il);
128
129
0
        cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
130
0
        cb(cur, "ffn_norm", il);
131
132
0
        cur = build_ffn(cur,
133
0
                ffn_up, NULL, NULL,
134
0
                ffn_gate, NULL, NULL,
135
0
                ffn_down, NULL, NULL,
136
0
                NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
137
138
0
        cur = ggml_add(ctx0, cur, ffn_inp);
139
0
        cb(cur, "ffn_out", il);
140
141
0
        cur = build_cvec(cur, il);
142
0
        cb(cur, "l_out", il);
143
144
        // input for next layer
145
0
        inpL = cur;
146
0
    }
147
148
0
    cur = inpL;
149
150
0
    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
151
0
    cb(cur, "result_norm", -1);
152
0
    res->t_embd = cur;
153
154
0
    cur = build_lora_mm(model.output, cur, model.output_s);
155
0
    cb(cur, "result_output", -1);
156
0
    res->t_logits = cur;
157
0
    ggml_build_forward_expand(gf, cur);
158
0
}