Coverage Report

Created: 2026-06-13 06:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/llama.cpp/src/models/grok.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_grok::load_arch_hparams(llama_model_loader & ml) {
4
    // defaults for old GGUFs
5
0
    hparams.yarn_beta_fast = 8.0f;
6
0
    hparams.f_logit_scale = 0.5773502691896257f;
7
0
    hparams.f_embedding_scale = 78.38367176906169f;
8
0
    hparams.f_attn_out_scale = 0.08838834764831845f;
9
0
    hparams.f_attn_logit_softcapping = 30.0f;
10
0
    hparams.f_router_logit_softcapping = 30.0f;
11
    // no final_logit_softcapping in grok-1
12
0
    hparams.f_final_logit_softcapping = 0.0f;
13
14
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS,  hparams.f_norm_rms_eps);
15
0
    ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH,   hparams.n_ff_exp, false);
16
0
    ml.get_key(LLM_KV_LOGIT_SCALE,                  hparams.f_logit_scale, false);
17
0
    ml.get_key(LLM_KV_EMBEDDING_SCALE,              hparams.f_embedding_scale, false);
18
0
    ml.get_key(LLM_KV_ATTENTION_OUTPUT_SCALE,       hparams.f_attn_out_scale, false);
19
0
    ml.get_key(LLM_KV_ATTN_LOGIT_SOFTCAPPING,       hparams.f_attn_logit_softcapping, false);
20
0
    ml.get_key(LLM_KV_ROUTER_LOGIT_SOFTCAPPING,     hparams.f_router_logit_softcapping, false);
21
0
    ml.get_key(LLM_KV_FINAL_LOGIT_SOFTCAPPING,      hparams.f_final_logit_softcapping, false);
22
23
0
    ml.get_key(LLM_KV_ATTENTION_TEMPERATURE_LENGTH,  hparams.attn_temp_length, false);
24
0
    ml.get_key(LLM_KV_ROPE_SCALING_YARN_EXT_FACTOR,  hparams.yarn_ext_factor, false);
25
0
    ml.get_key(LLM_KV_ROPE_SCALING_YARN_ATTN_FACTOR, hparams.yarn_attn_factor, false);
26
0
    ml.get_key(LLM_KV_ROPE_SCALING_YARN_BETA_FAST,   hparams.yarn_beta_fast, false);
27
0
    ml.get_key(LLM_KV_ROPE_SCALING_YARN_BETA_SLOW,   hparams.yarn_beta_slow, false);
28
29
0
    switch (hparams.n_layer()) {
30
0
        case 64: type = LLM_TYPE_314B; break;
31
0
        default: type = LLM_TYPE_UNKNOWN;
32
0
    }
33
0
}
34
35
0
void llama_model_grok::load_arch_tensors(llama_model_loader &) {
36
0
    LLAMA_LOAD_LOCALS;
37
38
0
    if (n_expert == 0) {
39
0
        throw std::runtime_error(arch_name() + " model cannot have zero experts");
40
0
    }
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}, TENSOR_NOT_REQUIRED);
47
48
    // if output is NULL, init from the input tok embed
49
0
    if (output == NULL) {
50
0
        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
51
0
    }
52
53
0
    const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff/* / n_expert_used*/; // grok-1 n_ff_exp == n_ff
54
0
    for (int i = 0; i < n_layer; ++i) {
55
0
        auto & layer = layers[i];
56
57
0
        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
58
59
0
        create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);
60
0
        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
61
62
0
        layer.attn_out_norm   = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0);
63
64
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
65
66
0
        layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
67
0
        layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff,   n_embd}, TENSOR_NOT_REQUIRED);
68
0
        layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED);
69
70
0
        layer.ffn_gate_inp  = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP,  "weight", i), {n_embd, n_expert}, 0);
71
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);
72
0
        layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd,   n_expert}, 0);
73
0
        layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {n_embd,   n_ff_exp, n_expert}, 0);
74
75
0
        layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED);
76
0
        if (!layer.ffn_post_norm) {
77
0
            layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0);
78
0
        }
79
0
    }
80
0
}
81
82
0
std::unique_ptr<llm_graph_context> llama_model_grok::build_arch_graph(const llm_graph_params & params) const {
83
0
    return std::make_unique<graph>(*this, params);
84
0
}
85
86
0
llama_model_grok::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) {
87
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
88
89
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
90
0
    GGML_ASSERT(n_embd_head == n_rot);
91
92
0
    ggml_tensor * cur;
93
0
    ggml_tensor * inpL;
94
95
0
    inpL = build_inp_embd(model.tok_embd);
96
97
    // inp_pos - contains the positions
98
0
    ggml_tensor * inp_pos = build_inp_pos();
99
100
0
    auto * inp_attn = build_attn_inp_kv();
101
102
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
103
104
0
    for (int il = 0; il < n_layer; ++il) {
105
0
        ggml_tensor * inpSA = inpL;
106
107
        // norm
108
0
        cur = build_norm(inpL,
109
0
                model.layers[il].attn_norm, NULL,
110
0
                LLM_NORM_RMS, il);
111
0
        cb(cur, "attn_norm", il);
112
113
        // self-attention
114
0
        {
115
            // compute Q and K and RoPE them
116
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
117
0
                    n_embd_head, n_head, n_head_kv, il);
118
119
0
            Qcur = ggml_rope_ext(
120
0
                    ctx0, Qcur, inp_pos, nullptr,
121
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
122
0
                    ext_factor, attn_factor, beta_fast, beta_slow
123
0
                    );
124
125
0
            Kcur = ggml_rope_ext(
126
0
                    ctx0, Kcur, inp_pos, nullptr,
127
0
                    n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
128
0
                    ext_factor, attn_factor, beta_fast, beta_slow
129
0
                    );
130
131
0
            cb(Qcur, "Qcur", il);
132
0
            cb(Kcur, "Kcur", il);
133
0
            cb(Vcur, "Vcur", il);
134
135
0
            cur = build_attn(inp_attn,
136
0
                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
137
0
                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f, il);
138
0
        }
139
0
        if (il == n_layer - 1 && inp_out_ids) {
140
0
            cur   = ggml_get_rows(ctx0,   cur, inp_out_ids);
141
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
142
0
        }
143
0
        cur = build_norm(cur,
144
0
                model.layers[il].attn_out_norm, NULL,
145
0
                LLM_NORM_RMS, il);
146
0
        cb(cur, "attn_out_norm", il);
147
148
0
        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
149
0
        cb(ffn_inp, "ffn_inp", il);
150
151
        // feed-forward network
152
0
        cur = build_norm(ffn_inp,
153
0
                model.layers[il].ffn_norm, NULL,
154
0
                LLM_NORM_RMS, il);
155
0
        cb(cur, "ffn_norm", il);
156
157
        // MoE branch
158
0
        ggml_tensor * moe_out = build_moe_ffn(cur,
159
0
                model.layers[il].ffn_gate_inp,
160
0
                model.layers[il].ffn_up_exps,
161
0
                model.layers[il].ffn_gate_exps,
162
0
                model.layers[il].ffn_down_exps,
163
0
                nullptr,
164
0
                n_expert, n_expert_used,
165
0
                LLM_FFN_GELU, true,
166
0
                hparams.expert_weights_scale,
167
0
                LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
168
0
                il);
169
0
        cb(moe_out, "ffn_moe_out", il);
170
171
0
        if (model.layers[il].ffn_up) {
172
0
            ggml_tensor * ffn_out = build_ffn(cur,
173
0
                    model.layers[il].ffn_up,   NULL, NULL,
174
0
                    model.layers[il].ffn_gate, NULL, NULL,
175
0
                    model.layers[il].ffn_down, NULL, NULL,
176
0
                    NULL,
177
0
                    LLM_FFN_GELU, LLM_FFN_PAR, il);
178
0
            cb(ffn_out, "ffn_out", il);
179
180
0
            cur = ggml_scale(ctx0, ggml_add(ctx0, ffn_out, moe_out), std::sqrt(2) / 2);
181
0
            cb(cur, "ffn_out", il);
182
0
        } else {
183
0
            cur = moe_out;
184
0
        }
185
0
        cur = build_norm(cur,
186
0
                model.layers[il].ffn_post_norm, NULL,
187
0
                LLM_NORM_RMS, il);
188
0
        cb(cur, "ffn_post_norm", il);
189
190
0
        cur = ggml_add(ctx0, cur, ffn_inp);
191
0
        cb(cur, "ffn_out", il);
192
193
0
        cur = build_cvec(cur, il);
194
0
        cb(cur, "l_out", il);
195
196
        // input for next layer
197
0
        inpL = cur;
198
0
    }
199
0
    cur = inpL;
200
201
0
    cur = build_norm(cur,
202
0
            model.output_norm, NULL,
203
0
            LLM_NORM_RMS, -1);
204
205
0
    cb(cur, "result_norm", -1);
206
0
    res->t_embd = cur;
207
208
    // lm_head
209
0
    cur = build_lora_mm(model.output, cur, model.output_s);
210
211
0
    cur = ggml_scale(ctx0, cur, hparams.f_logit_scale);
212
213
    // final logit soft-capping
214
0
    if (hparams.f_final_logit_softcapping) {
215
0
        cur = ggml_scale(ctx0, cur, 1.0f / hparams.f_final_logit_softcapping);
216
0
        cur = ggml_tanh(ctx0, cur);
217
0
        cur = ggml_scale(ctx0, cur, hparams.f_final_logit_softcapping);
218
0
    }
219
0
    cb(cur, "result_output", -1);
220
0
    res->t_logits = cur;
221
222
0
    ggml_build_forward_expand(gf, cur);
223
0
}