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/qwen2vl.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_qwen2vl::load_arch_hparams(llama_model_loader & ml) {
4
0
    ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, true);
5
0
}
6
// fall through
7
8
0
void llama_model_qwen2vl::load_arch_tensors(llama_model_loader &) {
9
0
    LLAMA_LOAD_LOCALS;
10
11
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
12
13
    // output
14
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
15
0
    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
16
0
    output_b    = create_tensor(tn(LLM_TENSOR_OUTPUT,      "bias"),   {n_vocab}, TENSOR_NOT_REQUIRED);
17
    // if output is NULL, init from the input tok embed
18
0
    if (output == NULL) {
19
0
        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
20
0
    }
21
22
0
    for (int i = 0; i < n_layer; ++i) {
23
0
        auto & layer = layers[i];
24
25
0
        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
26
27
0
        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);
28
0
        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
29
30
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
31
32
0
        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);
33
0
        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
34
0
        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
35
0
    }
36
0
}
37
38
0
std::unique_ptr<llm_graph_context> llama_model_qwen2vl::build_arch_graph(const llm_graph_params & params) const {
39
0
    return std::make_unique<graph>(*this, params);
40
0
}
41
42
0
llama_model_qwen2vl::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
43
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
44
45
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
46
0
    GGML_ASSERT(n_embd_head == n_rot);
47
48
0
    ggml_tensor * cur;
49
0
    ggml_tensor * inpL;
50
51
0
    inpL = build_inp_embd(model.tok_embd);
52
53
    // inp_pos - contains the positions
54
0
    ggml_tensor * inp_pos = build_inp_pos();
55
56
0
    auto * inp_attn = build_attn_inp_kv();
57
58
0
    int sections[4];
59
0
    std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);
60
61
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
62
63
0
    for (int il = 0; il < n_layer; ++il) {
64
0
        ggml_tensor * inpSA = inpL;
65
66
        // norm
67
0
        cur = build_norm(inpL,
68
0
                model.layers[il].attn_norm, NULL,
69
0
                LLM_NORM_RMS, il);
70
0
        cb(cur, "attn_norm", il);
71
72
        // self-attention
73
0
        {
74
            // compute Q and K and RoPE them
75
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
76
0
                    n_embd_head, n_head, n_head_kv, il);
77
78
0
            Qcur = ggml_rope_multi(
79
0
                    ctx0, Qcur, inp_pos, nullptr,
80
0
                    n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
81
0
                    ext_factor, attn_factor, beta_fast, beta_slow
82
0
                    );
83
84
0
            Kcur = ggml_rope_multi(
85
0
                    ctx0, Kcur, inp_pos, nullptr,
86
0
                    n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
87
0
                    ext_factor, attn_factor, beta_fast, beta_slow
88
0
                    );
89
90
0
            cb(Qcur, "Qcur", il);
91
0
            cb(Kcur, "Kcur", il);
92
0
            cb(Vcur, "Vcur", il);
93
94
0
            cur = build_attn(inp_attn,
95
0
                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
96
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
97
0
        }
98
0
        if (il == n_layer - 1 && inp_out_ids) {
99
0
            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
100
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
101
0
        }
102
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
103
0
        cb(ffn_inp, "ffn_inp", il);
104
105
        // feed-forward network
106
0
        cur = build_norm(ffn_inp,
107
0
                model.layers[il].ffn_norm, NULL,
108
0
                LLM_NORM_RMS, il);
109
0
        cb(cur, "ffn_norm", il);
110
111
0
        cur = build_ffn(cur,
112
0
                model.layers[il].ffn_up,   NULL, NULL,
113
0
                model.layers[il].ffn_gate, NULL, NULL,
114
0
                model.layers[il].ffn_down, NULL, NULL,
115
0
                NULL,
116
0
                LLM_FFN_SILU, LLM_FFN_PAR, il);
117
0
        cb(cur, "ffn_out", il);
118
119
0
        cur = ggml_add(ctx0, cur, ffn_inp);
120
121
0
        cur = build_cvec(cur, il);
122
0
        cb(cur, "l_out", il);
123
124
        // input for next layer
125
0
        inpL = cur;
126
0
    }
127
0
    cur = inpL;
128
129
0
    cur = build_norm(cur,
130
0
            model.output_norm, NULL,
131
0
            LLM_NORM_RMS, -1);
132
133
0
    cb(cur, "result_norm", -1);
134
0
    res->t_embd = cur;
135
136
    // lm_head
137
0
    cur = build_lora_mm(model.output, cur, model.output_s);
138
139
0
    cb(cur, "result_output", -1);
140
0
    res->t_logits = cur;
141
142
0
    ggml_build_forward_expand(gf, cur);
143
0
}