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/chameleon.cpp
Line
Count
Source
1
#include "models.h"
2
#include <float.h>
3
4
0
void llama_model_chameleon::load_arch_hparams(llama_model_loader & ml) {
5
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
6
0
    hparams.f_norm_eps = 1e-5;  // eps for qk-norm, torch default
7
0
    ml.get_key(LLM_KV_SWIN_NORM, hparams.swin_norm, false);
8
9
0
    switch (hparams.n_layer()) {
10
0
        case 32: type = LLM_TYPE_7B; break;
11
0
        case 48: type = LLM_TYPE_34B; break;
12
0
        default: type = LLM_TYPE_UNKNOWN;
13
0
   }
14
0
}
15
16
0
void llama_model_chameleon::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      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
24
    // if output is NULL, init from the input tok embed
25
0
    if (output == NULL) {
26
0
        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
27
0
    }
28
29
0
    for (int i = 0; i < n_layer; ++i) {
30
0
        auto & layer = layers[i];
31
32
0
        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
33
0
        layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, 0);
34
0
        layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, 0);
35
0
        layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i),  {n_embd_head_k, n_head}, TENSOR_NOT_REQUIRED);
36
0
        layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i),  {n_embd_head_k, n_head_kv}, TENSOR_NOT_REQUIRED);
37
38
0
        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);
39
0
        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
40
41
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
42
43
0
        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);
44
0
        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
45
0
        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
46
0
    }
47
0
}
48
49
0
std::unique_ptr<llm_graph_context> llama_model_chameleon::build_arch_graph(const llm_graph_params & params) const {
50
0
    return std::make_unique<graph>(*this, params);
51
0
}
52
53
0
llama_model_chameleon::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
54
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
55
56
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
57
0
    GGML_ASSERT(n_embd_head == n_rot);
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();
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
        ggml_tensor * inpSA = inpL;
73
74
        // norm
75
0
        if (hparams.swin_norm) {
76
0
            cur = inpL;
77
0
        } else {
78
0
            cur = build_norm(inpL,
79
0
                    model.layers[il].attn_norm, NULL,
80
0
                    LLM_NORM_RMS, il);
81
0
            cb(cur, "attn_norm", il);
82
0
        }
83
84
        // self-attention
85
0
        {
86
            // compute Q and K and RoPE them
87
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
88
0
                    n_embd_head, n_head, n_head_kv, il);
89
90
0
            if (model.layers[il].attn_q_norm) {
91
0
                Qcur = build_norm(Qcur,
92
0
                        model.layers[il].attn_q_norm,
93
0
                        model.layers[il].attn_q_norm_b,
94
0
                        LLM_NORM, il);
95
0
                cb(Qcur, "Qcur", il);
96
0
            }
97
98
0
            if (model.layers[il].attn_k_norm) {
99
0
                Kcur = build_norm(Kcur,
100
0
                        model.layers[il].attn_k_norm,
101
0
                        model.layers[il].attn_k_norm_b,
102
0
                        LLM_NORM, il);
103
0
                cb(Kcur, "Kcur", il);
104
0
            }
105
106
0
            Qcur = ggml_rope_ext(
107
0
                    ctx0, Qcur, inp_pos, nullptr,
108
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
109
0
                    ext_factor, attn_factor, beta_fast, beta_slow
110
0
                    );
111
112
0
            Kcur = ggml_rope_ext(
113
0
                    ctx0, Kcur, inp_pos, nullptr,
114
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
115
0
                    ext_factor, attn_factor, beta_fast, beta_slow
116
0
                    );
117
118
0
            cb(Qcur, "Qcur", il);
119
0
            cb(Kcur, "Kcur", il);
120
0
            cb(Vcur, "Vcur", il);
121
122
0
            cur = build_attn(inp_attn,
123
0
                    model.layers[il].wo, nullptr, model.layers[il].wo_s,
124
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
125
0
        }
126
127
0
        if (il == n_layer - 1 && inp_out_ids) {
128
0
            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
129
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
130
0
        }
131
132
0
        if (hparams.swin_norm) {
133
0
            cur = build_norm(cur,
134
0
                    model.layers[il].attn_norm, NULL,
135
0
                    LLM_NORM_RMS, il);
136
0
        }
137
138
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
139
0
        cb(ffn_inp, "ffn_inp", il);
140
141
        // feed-forward network
142
0
        if (!hparams.swin_norm) {
143
0
            cur = build_norm(ffn_inp,
144
0
                    model.layers[il].ffn_norm, NULL,
145
0
                    LLM_NORM_RMS, il);
146
0
            cb(cur, "ffn_norm", il);
147
0
        }
148
149
0
        cur = build_ffn(cur,
150
0
                model.layers[il].ffn_up,   NULL, NULL,
151
0
                model.layers[il].ffn_gate, NULL, NULL,
152
0
                model.layers[il].ffn_down, NULL, NULL,
153
0
                NULL,
154
0
                LLM_FFN_SILU, LLM_FFN_PAR, il);
155
0
        cb(cur, "ffn_out", il);
156
157
0
        if (hparams.swin_norm) {
158
0
            cur = build_norm(cur,
159
0
                    model.layers[il].ffn_norm, NULL,
160
0
                    LLM_NORM_RMS, il);
161
0
            cb(cur, "ffn_norm", il);
162
0
        }
163
164
0
        cur = ggml_add(ctx0, cur, ffn_inp);
165
0
        cb(cur, "ffn_out", il);
166
167
0
        cur = build_cvec(cur, il);
168
0
        cb(cur, "l_out", il);
169
170
        // input for next layer
171
0
        inpL = cur;
172
0
    }
173
174
0
    cur = inpL;
175
176
0
    cur = build_norm(cur,
177
0
            model.output_norm, NULL,
178
0
            LLM_NORM_RMS, -1);
179
180
0
    cb(cur, "result_norm", -1);
181
0
    res->t_embd = cur;
182
183
    // lm_head
184
0
    cur = build_lora_mm(model.output, cur, model.output_s);
185
0
    cb(cur, "result_output_with_img_logits", -1);
186
187
    // TODO: this suppresses the output of image tokens, which is required to enable text-only outputs.
188
    // Needs to be removed once image outputs are supported.
189
0
    int img_token_end_idx = 8196;
190
0
    int img_token_start_idx = 4;
191
0
    int num_img_tokens = img_token_end_idx - img_token_start_idx;
192
    // creates 1d tensor of size num_img_tokens and values -FLT_MAX,
193
    // which ensures that text token values are always at least larger than image token values
194
0
    ggml_tensor * img_logits = ggml_new_tensor_1d(ctx0, GGML_TYPE_F32, num_img_tokens);
195
0
    img_logits = ggml_clamp(ctx0, img_logits, -FLT_MAX, -FLT_MAX);
196
0
    cb(img_logits, "img_logits", -1);
197
198
0
    cur = ggml_set_1d(ctx0, cur, img_logits, ggml_element_size(cur) * img_token_start_idx);
199
200
0
    cb(cur, "result_output", -1);
201
0
    res->t_logits = cur;
202
203
0
    ggml_build_forward_expand(gf, cur);
204
0
}