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/phi2.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_phi2::load_arch_hparams(llama_model_loader & ml) {
4
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_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
        default: type = LLM_TYPE_UNKNOWN;
10
0
    }
11
0
}
12
13
0
void llama_model_phi2::load_arch_tensors(llama_model_loader &) {
14
0
    LLAMA_LOAD_LOCALS;
15
16
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
17
18
    // output
19
0
    output_norm   = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
20
0
    output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"),   {n_embd}, 0);
21
0
    output        = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, 0);
22
0
    output_b      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "bias"),   {n_vocab}, 0);
23
24
0
    for (int i = 0; i < n_layer; ++i) {
25
0
        auto & layer = layers[i];
26
27
0
        layer.attn_norm   = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
28
0
        layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i),   {n_embd}, 0);
29
30
0
        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);
31
32
0
        layer.wo   = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
33
0
        layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i),   {n_embd}, 0);
34
35
0
        layer.ffn_down   = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
36
0
        layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i),   {n_embd}, 0);
37
38
0
        layer.ffn_up     = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, n_ff}, 0);
39
0
        layer.ffn_up_b   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "bias", i),   {n_ff}, 0);
40
0
    }
41
0
}
42
43
0
std::unique_ptr<llm_graph_context> llama_model_phi2::build_arch_graph(const llm_graph_params & params) const {
44
0
    return std::make_unique<graph>(*this, params);
45
0
}
46
47
0
llama_model_phi2::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
48
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
49
50
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
51
52
0
    ggml_tensor * cur;
53
0
    ggml_tensor * attn_norm_output;
54
0
    ggml_tensor * ffn_output;
55
0
    ggml_tensor * inpL;
56
57
0
    inpL = build_inp_embd(model.tok_embd);
58
59
    // inp_pos - contains the positions
60
0
    ggml_tensor * inp_pos = build_inp_pos();
61
62
0
    auto * inp_attn = build_attn_inp_kv();
63
64
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
65
66
0
    for (int il = 0; il < n_layer; ++il) {
67
0
        attn_norm_output = build_norm(inpL,
68
0
                model.layers[il].attn_norm,
69
0
                model.layers[il].attn_norm_b,
70
0
                LLM_NORM, il);
71
0
        cb(attn_norm_output, "attn_norm", il);
72
73
        // self-attention
74
0
        {
75
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], attn_norm_output,
76
0
                    n_embd_head, n_head, n_head_kv, il);
77
0
            Qcur = ggml_rope_ext(
78
0
                    ctx0, Qcur, inp_pos, nullptr,
79
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
80
0
                    ext_factor, attn_factor, beta_fast, beta_slow
81
0
                    );
82
83
0
            Kcur = ggml_rope_ext(
84
0
                    ctx0, Kcur, inp_pos, nullptr,
85
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
86
0
                    ext_factor, attn_factor, beta_fast, beta_slow
87
0
                    );
88
89
0
            cb(Qcur, "Qcur", il);
90
0
            cb(Kcur, "Kcur", il);
91
0
            cb(Vcur, "Vcur", il);
92
93
            // with phi2, we scale the Q to avoid precision issues
94
            // ref: https://github.com/ml-explore/mlx-examples/blob/08e862336ade809bc37d1035f94b359e7d1a5152/phi2/phi2.py#L64-L66
95
0
            Qcur = ggml_scale(ctx0, Qcur, 1.0f/sqrtf(float(n_embd_head)));
96
97
0
            cur = build_attn(inp_attn,
98
0
                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
99
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
100
0
        }
101
0
        if (il == n_layer - 1 && inp_out_ids) {
102
0
            cur              = ggml_get_rows(ctx0,              cur, inp_out_ids);
103
0
            inpL             = ggml_get_rows(ctx0,             inpL, inp_out_ids);
104
0
            attn_norm_output = ggml_get_rows(ctx0, attn_norm_output, inp_out_ids);
105
0
        }
106
        // FF
107
0
        {
108
0
            ffn_output = build_ffn(attn_norm_output,
109
0
                    model.layers[il].ffn_up,   model.layers[il].ffn_up_b,   NULL,
110
0
                    NULL,                      NULL,                        NULL,
111
0
                    model.layers[il].ffn_down, model.layers[il].ffn_down_b, NULL,
112
0
                    NULL,
113
0
                    LLM_FFN_GELU, LLM_FFN_SEQ, il);
114
0
            cb(ffn_output, "ffn_out", il);
115
0
        }
116
0
        cur = ggml_add(ctx0, cur, ffn_output);
117
0
        cur = ggml_add(ctx0, cur, inpL);
118
119
0
        cur = build_cvec(cur, il);
120
0
        cb(cur, "l_out", il);
121
122
        // input for next layer
123
0
        inpL = cur;
124
0
    }
125
0
    cur = build_norm(inpL,
126
0
            model.output_norm,
127
0
            model.output_norm_b,
128
0
            LLM_NORM, -1);
129
130
0
    cb(cur, "result_norm", -1);
131
0
    res->t_embd = cur;
132
133
0
    cur = build_lora_mm(model.output, cur, model.output_s);
134
0
    cb(cur, "result_output_no_bias", -1);
135
136
0
    cur = ggml_add(ctx0, cur, model.output_b);
137
138
0
    cb(cur, "result_output", -1);
139
0
    res->t_logits = cur;
140
141
0
    ggml_build_forward_expand(gf, cur);
142
0
}