Coverage Report

Created: 2026-02-26 07:05

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/src/models/qwen3vl-moe.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
llm_build_qwen3vlmoe::llm_build_qwen3vlmoe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
4
0
    const size_t n_deepstack_layers = hparams.n_deepstack_layers;
5
6
0
    const int64_t n_embd      = hparams.n_embd;
7
0
    const int64_t n_embd_head = hparams.n_embd_head_v;
8
9
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
10
0
    GGML_ASSERT(n_embd_head == hparams.n_rot);
11
12
0
    ggml_tensor * cur;
13
0
    ggml_tensor * inpL;
14
15
0
    inpL = build_inp_embd(model.tok_embd);
16
17
0
    int sections[4];
18
0
    std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections);
19
20
    // inp_pos - contains the positions
21
0
    ggml_tensor * inp_pos = build_inp_pos();
22
23
0
    auto * inp_attn = build_attn_inp_kv();
24
25
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
26
27
0
    for (int il = 0; il < n_layer; ++il) {
28
0
        ggml_tensor * inpSA = inpL;
29
30
        // norm
31
0
        cur = build_norm(inpL,
32
0
                model.layers[il].attn_norm, NULL,
33
0
                LLM_NORM_RMS, il);
34
0
        cb(cur, "attn_norm", il);
35
36
        // self_attention
37
0
        {
38
            // compute Q and K and RoPE them
39
0
            ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur);
40
0
            cb(Qcur, "Qcur", il);
41
42
0
            ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur);
43
0
            cb(Kcur, "Kcur", il);
44
45
0
            ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur);
46
0
            cb(Vcur, "Vcur", il);
47
48
0
            Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head,    n_tokens);
49
0
            Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
50
0
            Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
51
52
0
            Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
53
0
            cb(Qcur, "Qcur_normed", il);
54
55
0
            Qcur = ggml_rope_multi(
56
0
                    ctx0, Qcur, inp_pos, nullptr,
57
0
                    n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
58
0
                    ext_factor, attn_factor, beta_fast, beta_slow
59
0
                    );
60
61
0
            Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
62
0
            cb(Kcur, "Kcur_normed", il);
63
64
0
            Kcur = ggml_rope_multi(
65
0
                    ctx0, Kcur, inp_pos, nullptr,
66
0
                    n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale,
67
0
                    ext_factor, attn_factor, beta_fast, beta_slow
68
0
                    );
69
70
0
            cb(Qcur, "Qcur", il);
71
0
            cb(Kcur, "Kcur", il);
72
0
            cb(Vcur, "Vcur", il);
73
74
0
            cur = build_attn(inp_attn,
75
0
                    model.layers[il].wo, model.layers[il].bo,
76
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il);
77
0
        }
78
79
0
        if (il == n_layer - 1 && inp_out_ids) {
80
0
            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
81
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
82
0
        }
83
84
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
85
0
        cb(ffn_inp, "ffn_inp", il);
86
87
        // MoE branch
88
0
        cur = build_norm(ffn_inp,
89
0
                model.layers[il].ffn_norm, NULL,
90
0
                LLM_NORM_RMS, il);
91
0
        cb(cur, "ffn_norm", il);
92
93
0
        ggml_tensor * moe_out =
94
0
            build_moe_ffn(cur,
95
0
                    model.layers[il].ffn_gate_inp,
96
0
                    model.layers[il].ffn_up_exps,
97
0
                    model.layers[il].ffn_gate_exps,
98
0
                    model.layers[il].ffn_down_exps,
99
0
                    nullptr,
100
0
                    n_expert, n_expert_used,
101
0
                    LLM_FFN_SILU, true,
102
0
                    false, 0.0,
103
0
                    LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
104
0
                    il);
105
0
        cb(moe_out, "ffn_moe_out", il);
106
0
        cur = moe_out;
107
108
0
        cur = ggml_add(ctx0, cur, ffn_inp);
109
110
0
        cur = build_cvec(cur, il);
111
0
        cb(cur, "l_out", il);
112
113
0
        if (il < (int) n_deepstack_layers) {
114
0
            ggml_tensor * ds = ggml_view_2d(ctx0, res->t_inp_embd, n_embd, n_tokens, res->t_inp_embd->nb[1], (il + 1) * n_embd * sizeof(float));
115
0
            cur = ggml_add(ctx0, cur, ds);
116
0
            cb(cur, "deepstack_out", il);
117
0
        }
118
119
        // input for next layer
120
0
        inpL = cur;
121
0
    }
122
123
0
    cur = inpL;
124
125
0
    cur = build_norm(cur,
126
0
            model.output_norm, NULL,
127
0
            LLM_NORM_RMS, -1);
128
129
0
    cb(cur, "result_norm", -1);
130
0
    res->t_embd = cur;
131
132
    // lm_head
133
0
    cur = build_lora_mm(model.output, cur);
134
135
0
    cb(cur, "result_output", -1);
136
0
    res->t_logits = cur;
137
138
0
    ggml_build_forward_expand(gf, cur);
139
0
}
140