Coverage Report

Created: 2026-06-22 06:47

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
0
void llama_model_phi3::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 24: type = LLM_TYPE_1B; break;
8
0
        case 32: type = LLM_TYPE_3B; break;
9
0
        case 40: type = LLM_TYPE_14B; break;
10
0
        default: type = LLM_TYPE_UNKNOWN;
11
0
    }
12
13
0
    const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false);
14
15
0
    if (found_swa && hparams.n_swa > 0) {
16
0
        LLAMA_LOG_WARN("%s: Phi SWA is currently disabled - results might be suboptimal for some models (see %s)\n",
17
0
                __func__, "https://github.com/ggml-org/llama.cpp/pull/13676");
18
19
        // TODO: fix conversion scripts to correctly populate `n_swa` and `n_swa_pattern`
20
0
        hparams.swa_type = LLAMA_SWA_TYPE_NONE;
21
22
0
        hparams.n_swa         = 0;
23
0
        hparams.set_swa_pattern(1);
24
0
    }
25
0
}
26
27
0
void llama_model_phi3::load_arch_tensors(llama_model_loader &) {
28
0
    LLAMA_LOAD_LOCALS;
29
30
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
31
32
    // output
33
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
34
0
    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
35
36
    // if output is NULL, init from the input tok embed
37
0
    if (output == NULL) {
38
0
        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
39
0
    }
40
41
0
    for (int i = 0; i < n_layer; ++i) {
42
0
        auto & layer = layers[i];
43
44
0
        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
45
46
0
        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, TENSOR_NOT_REQUIRED);
47
0
        layer.wo   = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0);
48
49
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0);
50
51
0
        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0);
52
0
        layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, 2 * n_ff }, 0);
53
54
0
        layer.rope_long  = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG,  "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
55
0
        layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0));
56
0
    }
57
0
}
58
59
0
std::unique_ptr<llm_graph_context> llama_model_phi3::build_arch_graph(const llm_graph_params & params) const {
60
0
    if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) {
61
0
        return std::make_unique<graph<true>> (*this, params);
62
0
    } else {
63
0
        return std::make_unique<graph<false>>(*this, params);
64
0
    }
65
0
}
66
67
template<bool iswa>
68
0
llama_model_phi3::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
69
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
70
71
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
72
73
0
    ggml_tensor * cur;
74
0
    ggml_tensor * inpL;
75
76
0
    inpL = build_inp_embd(model.tok_embd);
77
78
    // inp_pos - contains the positions
79
0
    ggml_tensor * inp_pos = build_inp_pos();
80
81
0
    using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>;
82
0
    inp_attn_type * inp_attn = nullptr;
83
84
0
    if constexpr (iswa) {
85
0
        inp_attn = build_attn_inp_kv_iswa();
86
0
    } else {
87
0
        inp_attn = build_attn_inp_kv();
88
0
    }
89
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
90
91
0
    for (int il = 0; il < n_layer; ++il) {
92
0
        auto * residual = inpL;
93
94
        // self-attention
95
0
        {
96
            // rope freq factors for 128k context
97
0
            ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
98
99
0
            ggml_tensor* attn_norm_output = build_norm(inpL,
100
0
                    model.layers[il].attn_norm,
101
0
                    model.layers[il].attn_norm_b,
102
0
                    LLM_NORM_RMS, il);
103
0
            cb(attn_norm_output, "attn_norm", il);
104
105
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], attn_norm_output,
106
0
                    n_embd_head, n_head, n_head_kv, il);
107
0
            Qcur = ggml_rope_ext(
108
0
                    ctx0, Qcur, inp_pos, rope_factors,
109
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
110
0
                    ext_factor, attn_factor, beta_fast, beta_slow
111
0
                    );
112
113
0
            Kcur = ggml_rope_ext(
114
0
                    ctx0, Kcur, inp_pos, rope_factors,
115
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
116
0
                    ext_factor, attn_factor, beta_fast, beta_slow
117
0
                    );
118
119
0
            cb(Qcur, "Qcur", il);
120
0
            cb(Kcur, "Kcur", il);
121
0
            cb(Vcur, "Vcur", il);
122
123
0
            Qcur = ggml_scale(ctx0, Qcur, 1.0f / sqrtf(float(n_embd_head)));
124
0
            cb(Qcur, "Qcur", il);
125
126
0
            cur = build_attn(inp_attn,
127
0
                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
128
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
129
0
        }
130
0
        if (il == n_layer - 1 && inp_out_ids) {
131
0
            cur      = ggml_get_rows(ctx0, cur,      inp_out_ids);
132
0
            residual = ggml_get_rows(ctx0, residual, inp_out_ids);
133
0
        }
134
0
        cur = ggml_add(ctx0, cur, residual);
135
0
        residual = cur;
136
137
0
        cur = build_norm(cur,
138
0
                model.layers[il].ffn_norm, model.layers[il].ffn_norm_b,
139
0
                LLM_NORM_RMS, il);
140
0
        cb(cur, "ffn_norm", il);
141
142
        // feed-forward network
143
0
        if (model.layers[il].ffn_gate_inp == nullptr) {
144
0
            cur = build_ffn(cur,
145
0
                    model.layers[il].ffn_up,   NULL, NULL,
146
0
                    NULL,                      NULL, NULL,
147
0
                    model.layers[il].ffn_down, NULL, NULL,
148
0
                    NULL,
149
0
                    LLM_FFN_SWIGLU, LLM_FFN_SEQ, il);
150
0
            cb(cur, "ffn_out", il);
151
0
        } else {
152
            // MoE branch
153
0
            cur = build_moe_ffn(cur,
154
0
                    model.layers[il].ffn_gate_inp,
155
0
                    model.layers[il].ffn_up_exps,
156
0
                    model.layers[il].ffn_gate_exps,
157
0
                    model.layers[il].ffn_down_exps,
158
0
                    nullptr,
159
0
                    n_expert, n_expert_used,
160
0
                    LLM_FFN_SILU, true,
161
0
                    hparams.expert_weights_scale,
162
0
                    LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
163
0
                    il);
164
0
            cb(cur, "ffn_moe_out", il);
165
0
        }
166
0
        cur = ggml_add(ctx0, residual, cur);
167
168
0
        cur = build_cvec(cur, il);
169
0
        cb(cur, "l_out", il);
170
171
        // input for next layer
172
0
        inpL = cur;
173
0
    }
174
0
    cur = build_norm(inpL,
175
0
            model.output_norm,
176
0
            model.output_norm_b,
177
0
            LLM_NORM_RMS, -1);
178
179
0
    cb(cur, "result_norm", -1);
180
0
    res->t_embd = cur;
181
182
0
    cur = build_lora_mm(model.output, cur, model.output_s);
183
184
0
    if (model.output_b != nullptr) {
185
0
        cb(cur, "result_output_no_bias", -1);
186
0
        cur = ggml_add(ctx0, cur, model.output_b);
187
0
    }
188
0
    cb(cur, "result_output", -1);
189
0
    res->t_logits = cur;
190
191
0
    ggml_build_forward_expand(gf, cur);
192
0
}
Unexecuted instantiation: llama_model_phi3::graph<false>::graph(llama_model const&, llm_graph_params const&)
Unexecuted instantiation: llama_model_phi3::graph<true>::graph(llama_model const&, llm_graph_params const&)
193
194
// Explicit template instantiations
195
template struct llama_model_phi3::graph<false>;
196
template struct llama_model_phi3::graph<true>;