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/exaone-moe.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_exaone_moe::load_arch_hparams(llama_model_loader & ml) {
4
0
    hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
5
0
    hparams.n_swa = 128;
6
0
    uint32_t swa_period = 4;
7
0
    ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false);
8
0
    hparams.set_swa_pattern(swa_period);
9
0
    hparams.rope_freq_base_train_swa  = hparams.rope_freq_base_train;
10
0
    hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;
11
12
0
    ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA,                hparams.rope_freq_base_train_swa, false);
13
0
    ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW,          hparams.n_swa);
14
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS,       hparams.f_norm_rms_eps);
15
0
    ml.get_key(LLM_KV_EXPERT_SHARED_COUNT,               hparams.n_expert_shared, false);
16
0
    ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH,        hparams.n_ff_exp);
17
0
    ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false);
18
0
    ml.get_key(LLM_KV_EXPERT_GATING_FUNC,                hparams.expert_gating_func);
19
0
    ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE,              hparams.expert_weights_scale, false);
20
0
    ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM,               hparams.expert_weights_norm, false);
21
0
    ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT,         hparams.n_layer_dense_lead, false);
22
23
0
    ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.n_layer_nextn, false);
24
0
    GGML_ASSERT(hparams.n_layer_nextn < hparams.n_layer_all && "n_layer_nextn must be < n_layer_impl");
25
26
0
    switch (hparams.n_layer()) {
27
0
        case 32: type = LLM_TYPE_30B_A3B; break;
28
0
        case 48: type = LLM_TYPE_235B_A22B; break;
29
0
        default: type = LLM_TYPE_UNKNOWN;
30
0
    }
31
0
}
32
33
0
void llama_model_exaone_moe::load_arch_tensors(llama_model_loader &) {
34
0
    LLAMA_LOAD_LOCALS;
35
36
0
    const int64_t n_ff_exp       = hparams.n_ff_exp;
37
0
    const int64_t n_ff_shexp     = hparams.n_ff_shexp > 0 ? hparams.n_ff_shexp : n_ff_exp;
38
0
    const int64_t head_dim       = hparams.n_embd_head_k();
39
0
    const int64_t n_qo_dim       = n_head * head_dim;
40
0
    const int64_t n_kv_dim       = n_head_kv * head_dim;
41
42
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
43
44
    // output
45
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
46
0
    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, 0);
47
48
0
    if (output == NULL) {
49
0
        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
50
0
    }
51
52
0
    for (int i = 0; i < n_layer_all; ++i) {
53
0
        int flags = 0;
54
0
        if (i >= n_layer) {
55
            // skip all tensors in the NextN layers
56
0
            flags |= TENSOR_SKIP;
57
0
        }
58
59
0
        auto & layer = layers[i];
60
0
        create_tensor_qkv(layer, i, n_embd, n_qo_dim, n_kv_dim, n_kv_dim, flags);
61
0
        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_qo_dim, n_embd}, flags);
62
63
0
        layer.rope_freqs   = create_tensor(tn(LLM_TENSOR_ROPE_FREQS,  "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0) | flags);
64
65
0
        layer.attn_norm    = create_tensor(tn(LLM_TENSOR_ATTN_NORM,   "weight", i), {n_embd}, flags);
66
0
        layer.attn_q_norm  = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, flags);
67
0
        layer.attn_k_norm  = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, flags);
68
69
0
        layer.ffn_norm     = create_tensor(tn(LLM_TENSOR_FFN_NORM,    "weight", i), {n_embd}, flags);
70
71
        // dense layers for first n_layer_dense_lead layers or nextn_predict_layers layers at the end
72
0
        if (i < (int) hparams.n_layer_dense_lead || (i >= n_layer)) {
73
0
            layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, flags);
74
0
            layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, flags);
75
0
            layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, n_ff}, flags);
76
0
        } else {
77
0
            layer.ffn_gate_inp    = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP,  "weight", i), {n_embd, n_expert}, flags);
78
0
            layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED | flags);
79
80
0
            if (n_expert == 0) {
81
0
                throw std::runtime_error("n_expert must be > 0");
82
0
            }
83
0
            if (n_expert_used == 0) {
84
0
                throw std::runtime_error("n_expert_used must be > 0");
85
0
            }
86
87
0
            layer.ffn_gate_exps  = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS,  "weight", i), {n_embd, n_ff_exp, n_expert}, flags);
88
0
            layer.ffn_down_exps  = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS,  "weight", i), {n_ff_exp, n_embd, n_expert}, flags);
89
0
            layer.ffn_up_exps    = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,    "weight", i), {n_embd, n_ff_exp, n_expert}, flags);
90
91
0
            layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_shexp}, flags);
92
0
            layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, flags);
93
0
            layer.ffn_up_shexp   = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP,   "weight", i), {n_embd, n_ff_shexp}, flags);
94
0
        }
95
96
        // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers
97
0
        if (i >= n_layer) {
98
0
            layer.nextn.eh_proj          = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), {2 * n_embd, n_embd}, flags);
99
0
            layer.nextn.enorm            = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM,   "weight", i), {n_embd}, flags);
100
0
            layer.nextn.hnorm            = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM,   "weight", i), {n_embd}, flags);
101
102
0
            layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), {n_embd}, flags | TENSOR_NOT_REQUIRED);
103
0
            layer.nextn.embed_tokens     = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS,     "weight", i), {n_embd, n_vocab}, flags | TENSOR_NOT_REQUIRED);
104
0
            layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), {n_embd, n_vocab}, flags | TENSOR_NOT_REQUIRED);
105
0
        }
106
0
    }
107
0
}
108
109
0
std::unique_ptr<llm_graph_context> llama_model_exaone_moe::build_arch_graph(const llm_graph_params & params) const {
110
0
    return std::make_unique<graph>(*this, params);
111
0
}
112
113
llama_model_exaone_moe::graph::graph(const llama_model & model, const llm_graph_params & params) :
114
0
    llm_graph_context(params) {
115
0
    const int64_t n_embd_head = hparams.n_embd_head_k();
116
117
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_v());
118
0
    GGML_ASSERT(n_embd_head == n_rot);
119
120
0
    ggml_tensor * cur;
121
0
    ggml_tensor * inpL;
122
123
0
    inpL = build_inp_embd(model.tok_embd);
124
125
    // inp_pos - contains the positions
126
0
    ggml_tensor * inp_pos = build_inp_pos();
127
128
0
    auto * inp_attn_iswa = build_attn_inp_kv_iswa();
129
130
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
131
132
0
    for (int il = 0; il < n_layer; ++il) {
133
0
        ggml_tensor * inpSA = inpL;
134
135
        // use RoPE for SWA layers
136
0
        const bool is_local_layer = hparams.is_swa(il);
137
138
        // norm
139
0
        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
140
0
        cb(cur, "attn_norm", il);
141
142
        // self-attention
143
0
        {
144
0
            ggml_tensor * rope_factors = model.get_rope_factors(cparams, il);
145
146
            // compute Q and K and RoPE them
147
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
148
0
                    n_embd_head, n_head, n_head_kv, il);
149
150
0
            Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
151
0
            Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
152
0
            cb(Qcur, "Qcur_normed", il);
153
0
            cb(Kcur, "Kcur_normed", il);
154
155
0
            if (is_local_layer) {
156
0
                Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base,
157
0
                                     freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
158
159
0
                Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, rope_factors, n_rot, rope_type, n_ctx_orig, freq_base,
160
0
                                     freq_scale, ext_factor, attn_factor, beta_fast, beta_slow);
161
0
            }
162
0
            cb(Qcur, "Qcur", il);
163
0
            cb(Kcur, "Kcur", il);
164
0
            cb(Vcur, "Vcur", il);
165
166
0
            cur = build_attn(inp_attn_iswa,
167
0
                model.layers[il].wo, NULL, model.layers[il].wo_s,
168
0
                Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
169
0
            cb(cur, "attn_out", il);
170
0
        }
171
0
        if (il == n_layer - 1 && inp_out_ids) {
172
0
            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);
173
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
174
0
        }
175
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
176
0
        cb(ffn_inp, "ffn_inp", il);
177
178
        // norm
179
0
        cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
180
0
        cb(cur, "ffn_norm", il);
181
182
        // feed-forward network
183
0
        if (model.layers[il].ffn_gate_inp == nullptr) {
184
            // dense branch
185
0
            cur = build_ffn(cur,
186
0
                    model.layers[il].ffn_up, NULL, NULL,
187
0
                    model.layers[il].ffn_gate, NULL, NULL,
188
0
                    model.layers[il].ffn_down, NULL, NULL, NULL,
189
0
                    LLM_FFN_SILU, LLM_FFN_PAR, il);
190
0
            cb(cur, "ffn_out", il);
191
0
        } else {
192
            // MoE branch
193
0
            ggml_tensor * moe_out = build_moe_ffn(cur,
194
0
                model.layers[il].ffn_gate_inp,
195
0
                model.layers[il].ffn_up_exps,
196
0
                model.layers[il].ffn_gate_exps,
197
0
                model.layers[il].ffn_down_exps,
198
0
                model.layers[il].ffn_exp_probs_b,
199
0
                n_expert, n_expert_used,
200
0
                LLM_FFN_SILU, hparams.expert_weights_norm,
201
0
                hparams.expert_weights_scale,
202
0
                (llama_expert_gating_func_type) hparams.expert_gating_func,
203
0
                il);
204
0
            cb(moe_out, "ffn_moe_out", il);
205
206
            // FFN shared expert
207
0
            {
208
0
                ggml_tensor * ffn_shexp =
209
0
                    build_ffn(cur,
210
0
                        model.layers[il].ffn_up_shexp, NULL, NULL,
211
0
                        model.layers[il].ffn_gate_shexp, NULL, NULL,
212
0
                        model.layers[il].ffn_down_shexp, NULL, NULL,
213
0
                        NULL, LLM_FFN_SILU, LLM_FFN_PAR, il);
214
0
                cb(ffn_shexp, "ffn_shexp", il);
215
216
0
                cur = ggml_add(ctx0, moe_out, ffn_shexp);
217
0
                cb(cur, "ffn_out", il);
218
0
            }
219
0
        }
220
221
0
        cur = ggml_add(ctx0, cur, ffn_inp);
222
223
0
        cur = build_cvec(cur, il);
224
0
        cb(cur, "l_out", il);
225
226
        // input for next layer
227
0
        inpL = cur;
228
0
    }
229
0
    cur = inpL;
230
231
    // final norm
232
0
    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
233
234
0
    cb(cur, "result_norm", -1);
235
0
    res->t_embd = cur;
236
237
    // lm_head
238
0
    cur = build_lora_mm(model.output, cur, model.output_s);
239
240
0
    cb(cur, "result_output", -1);
241
0
    res->t_logits = cur;
242
243
0
    ggml_build_forward_expand(gf, cur);
244
0
}