Coverage Report

Created: 2026-01-11 07:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/src/models/phi3.cpp
Line
Count
Source
1
#include "models.h"
2
3
template<bool iswa>
4
0
llm_build_phi3<iswa>::llm_build_phi3(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
5
0
    const int64_t n_embd_head = hparams.n_embd_head_v;
6
0
    const int64_t n_embd_gqa = hparams.n_embd_v_gqa();
7
8
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k);
9
10
0
    ggml_tensor * cur;
11
0
    ggml_tensor * inpL;
12
13
0
    inpL = build_inp_embd(model.tok_embd);
14
15
    // inp_pos - contains the positions
16
0
    ggml_tensor * inp_pos = build_inp_pos();
17
18
0
    using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
19
0
    inp_attn_type * inp_attn = nullptr;
20
21
0
    if constexpr (iswa) {
22
0
        inp_attn = build_attn_inp_kv_iswa();
23
0
    } else {
24
0
        inp_attn = build_attn_inp_kv();
25
0
    }
26
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
27
28
0
    for (int il = 0; il < n_layer; ++il) {
29
0
        auto * residual = inpL;
30
31
        // self-attention
32
0
        {
33
            // rope freq factors for 128k context
34
0
            ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
35
36
0
            ggml_tensor* attn_norm_output = build_norm(inpL,
37
0
                    model.layers[il].attn_norm,
38
0
                    model.layers[il].attn_norm_b,
39
0
                    LLM_NORM_RMS, il);
40
0
            cb(attn_norm_output, "attn_norm", il);
41
42
0
            ggml_tensor * Qcur = nullptr;
43
0
            ggml_tensor * Kcur = nullptr;
44
0
            ggml_tensor * Vcur = nullptr;
45
46
0
            if (model.layers[il].wqkv) {
47
0
                cur = build_lora_mm(model.layers[il].wqkv, attn_norm_output);
48
0
                cb(cur, "wqkv", il);
49
50
0
                Qcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head,    n_tokens, n_embd_head * sizeof(float), cur->nb[1], 0 * sizeof(float) * (n_embd));
51
0
                Kcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float), cur->nb[1], 1 * sizeof(float) * (n_embd));
52
0
                Vcur = ggml_view_3d(ctx0, cur, n_embd_head, n_head_kv, n_tokens, n_embd_head * sizeof(float), cur->nb[1], 1 * sizeof(float) * (n_embd + n_embd_gqa));
53
0
                }
54
0
                else {
55
0
                Qcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wq, attn_norm_output), model.layers[il].bq);
56
0
                Kcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wk, attn_norm_output), model.layers[il].bk);
57
0
                Vcur = ggml_add(ctx0, build_lora_mm(model.layers[il].wv, attn_norm_output), model.layers[il].bv);
58
59
0
                Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head,    n_tokens);
60
0
                Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens);
61
0
                Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens);
62
0
            }
63
0
            Qcur = ggml_rope_ext(
64
0
                    ctx0, Qcur, inp_pos, rope_factors,
65
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
66
0
                    ext_factor, attn_factor, beta_fast, beta_slow
67
0
                    );
68
69
0
            Kcur = ggml_rope_ext(
70
0
                    ctx0, Kcur, inp_pos, rope_factors,
71
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
72
0
                    ext_factor, attn_factor, beta_fast, beta_slow
73
0
                    );
74
75
0
            cb(Qcur, "Qcur", il);
76
0
            cb(Kcur, "Kcur", il);
77
0
            cb(Vcur, "Vcur", il);
78
79
0
            Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head)));
80
0
            cb(Qcur, "Qcur", il);
81
82
0
            cur = build_attn(inp_attn,
83
0
                    model.layers[il].wo, model.layers[il].bo,
84
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
85
0
        }
86
0
        if (il == n_layer - 1 && inp_out_ids) {
87
0
            cur      = ggml_get_rows(ctx0, cur,      inp_out_ids);
88
0
            residual = ggml_get_rows(ctx0, residual, inp_out_ids);
89
0
        }
90
0
        cur = ggml_add(ctx0, cur, residual);
91
0
        residual = cur;
92
93
0
        cur = build_norm(cur,
94
0
                model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
95
0
                LLM_NORM_RMS, il);
96
0
        cb(cur, "ffn_norm", il);
97
98
        // feed-forward network
99
0
        if (model.layers[il].ffn_gate_inp == nullptr) {
100
0
            cur = build_ffn(cur,
101
0
                    model.layers[il].ffn_up,   NULL, NULL,
102
0
                    NULL,                      NULL, NULL,
103
0
                    model.layers[il].ffn_down, NULL, NULL,
104
0
                    NULL,
105
0
                    LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
106
0
            cb(cur, "ffn_out", il);
107
0
        } else {
108
            // MoE branch
109
0
            cur = build_moe_ffn(cur,
110
0
                    model.layers[il].ffn_gate_inp,
111
0
                    model.layers[il].ffn_up_exps,
112
0
                    model.layers[il].ffn_gate_exps,
113
0
                    model.layers[il].ffn_down_exps,
114
0
                    nullptr,
115
0
                    n_expert, n_expert_used,
116
0
                    LLM_FFN_SILU, true,
117
0
                    false, 0.0,
118
0
                    LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
119
0
                    il);
120
0
            cb(cur, "ffn_moe_out", il);
121
0
        }
122
0
        cur = ggml_add(ctx0, residual, cur);
123
124
0
        cur = build_cvec(cur, il);
125
0
        cb(cur, "l_out", il);
126
127
        // input for next layer
128
0
        inpL = cur;
129
0
    }
130
0
    cur = build_norm(inpL,
131
0
            model.output_norm,
132
0
            model.output_norm_b,
133
0
            LLM_NORM_RMS, -1);
134
135
0
    cb(cur, "result_norm", -1);
136
0
    res->t_embd = cur;
137
138
0
    cur = build_lora_mm(model.output, cur);
139
140
0
    if (model.output_b != nullptr) {
141
0
        cb(cur, "result_output_no_bias", -1);
142
0
        cur = ggml_add(ctx0, cur, model.output_b);
143
0
    }
144
0
    cb(cur, "result_output", -1);
145
0
    res->t_logits = cur;
146
147
0
    ggml_build_forward_expand(gf, cur);
148
0
}
Unexecuted instantiation: llm_build_phi3<false>::llm_build_phi3(llama_model const&, llm_graph_params const&)
Unexecuted instantiation: llm_build_phi3<true>::llm_build_phi3(llama_model const&, llm_graph_params const&)
149
150
// Explicit template instantiations
151
template struct llm_build_phi3<false>;
152
template struct llm_build_phi3<true>;