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/openai-moe.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_openai_moe::load_arch_hparams(llama_model_loader & ml) {
4
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
5
0
    ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH,  hparams.n_ff_exp);
6
0
    ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW,    hparams.n_swa);
7
8
0
    hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
9
0
    uint32_t swa_period = 2;
10
0
    ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false);
11
0
    hparams.set_swa_pattern(swa_period);
12
13
0
    hparams.rope_freq_base_train_swa  = hparams.rope_freq_base_train;
14
0
    hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train;
15
0
    ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false);
16
17
0
    switch (hparams.n_layer()) {
18
0
        case 24: type = LLM_TYPE_20B; break;
19
0
        case 36: type = LLM_TYPE_120B; break;
20
0
        default: type = LLM_TYPE_UNKNOWN;
21
0
    }
22
0
}
23
24
0
void llama_model_openai_moe::load_arch_tensors(llama_model_loader &) {
25
0
    LLAMA_LOAD_LOCALS;
26
27
0
    const int64_t n_ff_exp = hparams.n_ff_exp;
28
29
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
30
31
    // output
32
0
    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
33
0
    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, 0);
34
35
0
    for (int i = 0; i < n_layer; ++i) {
36
0
        auto & layer = layers[i];
37
38
0
        layer.attn_norm      = create_tensor(tn(LLM_TENSOR_ATTN_NORM,      "weight", i), {n_embd}, 0);
39
0
        layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0);
40
41
0
        create_tensor_qkv(layer, i, n_embd, n_head * n_rot, n_head_kv * n_rot, n_head_kv * n_rot, 0);
42
0
        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_rot, n_embd}, 0);
43
44
0
        layer.attn_sinks = create_tensor(tn(LLM_TENSOR_ATTN_SINKS, "weight", i), {n_head}, 0);
45
46
0
        layer.ffn_gate_inp  = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP,  "weight", i), {  n_embd, n_expert}, 0);
47
0
        layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {  n_embd, n_ff_exp, n_expert}, 0);
48
0
        layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp,   n_embd, n_expert}, 0);
49
0
        layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {  n_embd, n_ff_exp, n_expert}, 0);
50
51
0
        layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0);
52
53
0
        layer.ffn_gate_inp_b  = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP,  "bias", i), {n_expert}, 0);
54
0
        layer.ffn_gate_exps_b = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "bias", i), {n_ff_exp, n_expert}, 0);
55
0
        layer.ffn_down_exps_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "bias", i), {  n_embd, n_expert}, 0);
56
0
        layer.ffn_up_exps_b   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "bias", i), {n_ff_exp, n_expert}, 0);
57
0
    }
58
0
}
59
60
0
std::unique_ptr<llm_graph_context> llama_model_openai_moe::build_arch_graph(const llm_graph_params & params) const {
61
0
    return std::make_unique<graph>(*this, params);
62
0
}
63
64
0
llama_model_openai_moe::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
65
0
    ggml_tensor * cur;
66
0
    ggml_tensor * inpL;
67
68
0
    inpL = build_inp_embd(model.tok_embd);
69
70
    // inp_pos - contains the positions
71
0
    ggml_tensor * inp_pos = build_inp_pos();
72
73
0
    auto * inp_attn = build_attn_inp_kv_iswa();
74
75
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
76
77
0
    for (int il = 0; il < n_layer; ++il) {
78
0
        res->t_layer_inp[il] = inpL;
79
80
0
        const float freq_base_l  = model.get_rope_freq_base (cparams, il);
81
0
        const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
82
83
0
        ggml_tensor * inpSA = inpL;
84
85
        // norm
86
0
        cur = build_norm(inpL,
87
0
                model.layers[il].attn_norm, nullptr,
88
0
                LLM_NORM_RMS, il);
89
0
        cb(cur, "attn_norm", il);
90
91
        // self-attention
92
0
        {
93
            // compute Q and K and RoPE them
94
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
95
0
                    n_rot, n_head, n_head_kv, il);
96
97
0
            Qcur = ggml_rope_ext(
98
0
                    ctx0, Qcur, inp_pos, nullptr,
99
0
                    n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
100
0
                    ext_factor, attn_factor, beta_fast, beta_slow
101
0
                    );
102
103
0
            Kcur = ggml_rope_ext(
104
0
                    ctx0, Kcur, inp_pos, nullptr,
105
0
                    n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l,
106
0
                    ext_factor, attn_factor, beta_fast, beta_slow
107
0
                    );
108
109
0
            cb(Qcur, "Qcur", il);
110
0
            cb(Kcur, "Kcur", il);
111
0
            cb(Vcur, "Vcur", il);
112
113
0
            cur = build_attn(inp_attn,
114
0
                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
115
0
                    Qcur, Kcur, Vcur, nullptr, model.layers[il].attn_sinks, nullptr, 1.0f/sqrtf(float(n_rot)), il);
116
117
0
            cb(cur, "attn_out", il);
118
0
        }
119
0
        if (il == n_layer - 1) {
120
            // skip computing output for unused tokens
121
0
            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
122
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
123
0
        }
124
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
125
0
        cb(ffn_inp, "ffn_inp", il);
126
127
0
        cur = ffn_inp;
128
0
        cur = build_norm(cur,
129
0
                model.layers[il].attn_post_norm, nullptr,
130
0
                LLM_NORM_RMS, il);
131
0
        cb(cur, "attn_post_norm", il);
132
133
        // MoE branch
134
0
        cur = build_moe_ffn(cur,
135
0
                model.layers[il].ffn_gate_inp,  model.layers[il].ffn_gate_inp_b,
136
0
                model.layers[il].ffn_up_exps,   model.layers[il].ffn_up_exps_b,
137
0
                model.layers[il].ffn_gate_exps, model.layers[il].ffn_gate_exps_b,
138
0
                model.layers[il].ffn_down_exps, model.layers[il].ffn_down_exps_b,
139
0
                nullptr,
140
0
                n_expert, n_expert_used,
141
0
                LLM_FFN_SWIGLU_OAI_MOE, false,
142
0
                hparams.expert_weights_scale,
143
0
                LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX_WEIGHT,
144
0
                il);
145
0
        cb(cur, "ffn_moe_out", il);
146
147
0
        cur = ggml_add(ctx0, cur, ffn_inp);
148
149
0
        cur = build_cvec(cur, il);
150
0
        cb(cur, "l_out", il);
151
152
        // input for next layer
153
0
        inpL = cur;
154
0
    }
155
0
    cur = inpL;
156
157
0
    cur = build_norm(cur,
158
0
            model.output_norm, NULL,
159
0
            LLM_NORM_RMS, -1);
160
161
0
    cb(cur, "result_norm", -1);
162
0
    res->t_embd = cur;
163
164
    // lm_head
165
0
    cur = build_lora_mm(model.output, cur, model.output_s);
166
167
0
    cb(cur, "result_output", -1);
168
0
    res->t_logits = cur;
169
170
0
    ggml_build_forward_expand(gf, cur);
171
0
}