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/ernie4-5.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_ernie4_5::load_arch_hparams(llama_model_loader & ml) {
4
    // paddleocr need mrope_section
5
0
    ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, false);
6
7
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
8
0
    if (arch == LLM_ARCH_ERNIE4_5_MOE) {
9
0
        ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH,        hparams.n_ff_exp);
10
0
        ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);
11
0
        ml.get_key(LLM_KV_INTERLEAVE_MOE_LAYER_STEP,         hparams.n_moe_layer_step);
12
0
        ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT,         hparams.n_layer_dense_lead, false);
13
0
    }
14
15
0
    switch (hparams.n_layer()) {
16
0
        case 18: type = LLM_TYPE_0_3B; break;
17
0
        case 28: type = LLM_TYPE_21B_A3B; break;
18
0
        case 54: type = LLM_TYPE_300B_A47B; break;
19
0
        default: type = LLM_TYPE_UNKNOWN;
20
0
    }
21
0
}
22
23
0
void llama_model_ernie4_5::load_arch_tensors(llama_model_loader &) {
24
0
    LLAMA_LOAD_LOCALS;
25
26
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
27
28
    // output
29
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
30
0
    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
31
    // if output is NULL, init from the input tok embed
32
0
    if (output == NULL) {
33
0
        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
34
0
    }
35
36
0
    for (int i = 0; i < n_layer; ++i) {
37
0
        auto & layer = layers[i];
38
39
0
        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
40
41
0
        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_gqa, n_embd_gqa, 0);
42
0
        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
43
44
        // optional bias tensors
45
0
        layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
46
47
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
48
49
0
        if (arch == LLM_ARCH_ERNIE4_5_MOE && static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead) { // MoE layers
50
0
            int n_ff_exp = hparams.n_ff_exp;
51
52
0
            layer.ffn_gate_inp  = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP,  "weight", i), {n_embd, n_expert}, 0);
53
0
            layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED);
54
0
            layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd,   n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED);
55
0
            layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {  n_ff_exp, n_embd, n_expert}, 0);
56
0
            layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {n_embd,   n_ff_exp, n_expert}, 0);
57
58
            // Shared expert (if present)
59
0
            if (hparams.n_ff_shexp > 0) {
60
0
                layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {    n_embd, hparams.n_ff_shexp}, 0);
61
0
                layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd    }, 0);
62
0
                layer.ffn_up_shexp   = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP,   "weight", i), {    n_embd, hparams.n_ff_shexp}, 0);
63
0
            }
64
0
        } else { // Dense layers
65
0
            layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd,   n_ff}, 0);
66
0
            layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  n_ff, n_embd}, 0);
67
0
            layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   n_ff}, 0);
68
0
        }
69
0
    }
70
0
}
71
72
0
std::unique_ptr<llm_graph_context> llama_model_ernie4_5::build_arch_graph(const llm_graph_params & params) const {
73
0
    return std::make_unique<graph>(*this, params);
74
0
}
75
76
llama_model_ernie4_5::graph::graph(const llama_model & model, const llm_graph_params & params) :
77
0
    llm_graph_context(params) {
78
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
79
80
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
81
0
    GGML_ASSERT(n_embd_head == n_rot);
82
83
0
    ggml_tensor * cur;
84
0
    ggml_tensor * inpL;
85
86
0
    inpL = build_inp_embd(model.tok_embd);
87
88
    // inp_pos - contains the positions
89
0
    ggml_tensor * inp_pos = build_inp_pos();
90
91
0
    auto * inp_attn = build_attn_inp_kv();
92
93
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
94
95
0
    for (int il = 0; il < n_layer; ++il) {
96
0
        ggml_tensor * inpSA = inpL;
97
98
        // norm
99
0
        {
100
0
            cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
101
0
            cb(cur, "attn_norm", il);
102
0
        }
103
        // self-attention
104
0
        {
105
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
106
0
                    n_embd_head, n_head, n_head_kv, il);
107
108
0
            Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
109
0
                                 ext_factor, attn_factor, beta_fast, beta_slow);
110
111
0
            Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
112
0
                                 ext_factor, attn_factor, beta_fast, beta_slow);
113
114
0
            cb(Qcur, "Qcur", il);
115
0
            cb(Kcur, "Kcur", il);
116
0
            cb(Vcur, "Vcur", il);
117
118
0
            cur = build_attn(inp_attn,
119
0
                    model.layers[il].wo, NULL, model.layers[il].wo_s,
120
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
121
0
        }
122
0
        if (il == n_layer - 1) {
123
            // skip computing output for unused tokens
124
0
            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);
125
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
126
0
        }
127
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
128
0
        cb(ffn_inp, "ffn_inp", il);
129
130
        // feed-forward network
131
0
        {
132
0
            cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
133
0
            cb(cur, "ffn_norm", il);
134
135
0
            cur = build_ffn(cur,
136
0
                    model.layers[il].ffn_up, NULL, NULL,
137
0
                    model.layers[il].ffn_gate, NULL, NULL,
138
0
                    model.layers[il].ffn_down, NULL, NULL,
139
0
                    NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
140
0
            cb(cur, "ffn_out", il);
141
0
        }
142
0
        cur = ggml_add(ctx0, cur, ffn_inp);
143
144
0
        cur = build_cvec(cur, il);
145
0
        cb(cur, "l_out", il);
146
147
        // input for next layer
148
0
        inpL = cur;
149
0
    }
150
0
    cur = inpL;
151
152
0
    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
153
154
0
    cb(cur, "result_norm", -1);
155
0
    res->t_embd = cur;
156
157
    // lm_head
158
0
    cur = build_lora_mm(model.output, cur, model.output_s);
159
160
0
    cb(cur, "result_output", -1);
161
0
    res->t_logits = cur;
162
163
0
    ggml_build_forward_expand(gf, cur);
164
0
}