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/nemotron-h.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_nemotron_h::load_arch_hparams(llama_model_loader & ml) {
4
0
    ml.get_key(LLM_KV_SSM_CONV_KERNEL,    hparams.ssm_d_conv);
5
0
    ml.get_key(LLM_KV_SSM_INNER_SIZE,     hparams.ssm_d_inner);
6
0
    ml.get_key(LLM_KV_SSM_STATE_SIZE,     hparams.ssm_d_state);
7
0
    ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank);
8
0
    ml.get_key(LLM_KV_SSM_GROUP_COUNT,    hparams.ssm_n_group);
9
10
    // A layer is recurrent IFF the n_head_kv value is set to 0 and
11
    // the n_ff value is set to 0
12
0
    for (uint32_t i = 0; i < hparams.n_layer(); ++i) {
13
0
        hparams.is_recr_impl[i] = (hparams.n_head_kv(i) == 0 && hparams.n_ff(i) == 0);
14
0
    }
15
16
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
17
18
0
    ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH,        hparams.n_ff_exp,        false);
19
0
    ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp,      false);
20
0
    ml.get_key(LLM_KV_EXPERT_SHARED_COUNT,               hparams.n_expert_shared, false);
21
0
    ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM,               hparams.expert_weights_norm, false);
22
0
    ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE,              hparams.expert_weights_scale, false);
23
0
    ml.get_key(LLM_KV_MOE_LATENT_SIZE,                   hparams.moe_latent_size, false);
24
25
0
    switch (hparams.n_layer()) {
26
0
        case 52: type = LLM_TYPE_31B_A3_5B; break; // Nemotron-H_MOE 31B
27
0
        case 56: type = LLM_TYPE_9B; break;
28
0
        case 88: type = LLM_TYPE_120B_A12B; break;
29
0
        default: type = LLM_TYPE_UNKNOWN;
30
0
    }
31
0
}
32
33
0
void llama_model_nemotron_h::load_arch_tensors(llama_model_loader &) {
34
0
    LLAMA_LOAD_LOCALS;
35
36
    // mamba2 Mixer SSM params
37
    // NOTE: int64_t for tensor dimensions
38
0
    const int64_t d_conv     = hparams.ssm_d_conv;
39
0
    const int64_t d_inner    = hparams.ssm_d_inner;
40
0
    const int64_t d_state    = hparams.ssm_d_state;
41
0
    const int64_t n_ssm_head = hparams.ssm_dt_rank;
42
0
    const int64_t n_group    = hparams.ssm_n_group;
43
0
    const int64_t d_in_proj  = 2*d_inner + 2*n_group*d_state + n_ssm_head;
44
0
    const int64_t moe_n_embd = hparams.moe_latent_size > 0 ? hparams.moe_latent_size : n_embd;
45
46
    // embeddings
47
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
48
49
    // output
50
0
    {
51
0
        output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
52
0
        output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
53
        // if output is NULL, init from the input tok embed, duplicated to allow offloading
54
0
        if (output == NULL) {
55
0
            output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
56
0
        }
57
0
    }
58
59
0
    for (int i = 0; i < n_layer; ++i) {
60
0
        auto & layer = layers[i];
61
62
        // all blocks use the attn norm
63
0
        layer.attn_norm  = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
64
65
0
        if (hparams.is_recr(i)) {
66
            // ssm layers
67
0
            layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, 0);
68
69
0
            layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, 0);
70
0
            layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, TENSOR_NOT_REQUIRED);
71
72
0
            layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_ssm_head}, 0);
73
74
            // no "weight" suffix for these
75
0
            layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_ssm_head}, 0);
76
0
            layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_ssm_head}, 0);
77
78
0
            layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, 0);
79
80
            // out_proj
81
0
            layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
82
0
        } else if (hparams.n_ff(i) == 0) {
83
            // attention layers (with optional bias)
84
0
            const int64_t n_head_i = hparams.n_head(i);
85
0
            const int64_t n_embd_k_gqa_i = hparams.n_embd_k_gqa(i);
86
0
            const int64_t n_embd_v_gqa_i = hparams.n_embd_v_gqa(i);
87
0
            create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head_i, n_embd_k_gqa_i, n_embd_v_gqa_i, 0);
88
0
            layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head_i, n_embd}, 0);
89
0
            layer.wo_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED);
90
0
        }  else {
91
0
            if (n_expert != 0) {
92
0
                const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used;
93
0
                const int64_t n_ff_shexp = hparams.n_ff_shexp;
94
95
0
                layer.ffn_gate_inp    = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP,  "weight", i), { n_embd, n_expert}, 0);
96
0
                layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert         }, 0);
97
98
                // MoE branch
99
0
                layer.ffn_latent_down = create_tensor(tn(LLM_TENSOR_FFN_LATENT_DOWN, "weight", i), {n_embd, moe_n_embd}, TENSOR_NOT_REQUIRED);
100
0
                layer.ffn_latent_up   = create_tensor(tn(LLM_TENSOR_FFN_LATENT_UP,   "weight", i), {moe_n_embd, n_embd}, TENSOR_NOT_REQUIRED);
101
102
0
                layer.ffn_down_exps   = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp,   moe_n_embd, n_expert}, 0);
103
0
                layer.ffn_up_exps     = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {moe_n_embd, n_ff_exp, n_expert}, 0);
104
105
                // Shared expert branch
106
0
                layer.ffn_down_shexp  = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, 0);
107
0
                layer.ffn_up_shexp    = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP,   "weight", i), {n_embd, n_ff_shexp}, 0);
108
109
0
            } else {
110
                // mlp layers
111
0
                layer.ffn_down   = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {  hparams.n_ff(i), n_embd}, 0);
112
0
                layer.ffn_up     = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd,   hparams.n_ff(i)}, 0);
113
0
                layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias",   i), {n_embd}, TENSOR_NOT_REQUIRED);
114
0
                layer.ffn_up_b   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "bias",   i), {hparams.n_ff(i)}, TENSOR_NOT_REQUIRED);
115
0
            }
116
0
        }
117
0
    }
118
0
}
119
120
0
std::unique_ptr<llm_graph_context> llama_model_nemotron_h::build_arch_graph(const llm_graph_params & params) const {
121
0
    return std::make_unique<graph>(*this, params);
122
0
}
123
124
llama_model_nemotron_h::graph::graph(const llama_model & model, const llm_graph_params & params) :
125
0
    llm_build_mamba_base(params) {
126
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
127
0
    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
128
129
0
    ggml_tensor * cur;
130
0
    ggml_tensor * inpL;
131
132
0
    inpL = build_inp_embd(model.tok_embd);
133
0
    ggml_build_forward_expand(gf, inpL);
134
135
0
    auto * inp = build_inp_mem_hybrid();
136
137
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
138
139
0
    for (int il = 0; il < n_layer; ++il) {
140
0
        struct ggml_tensor * inpSA = inpL;
141
142
        // norm
143
0
        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
144
0
        cb(cur, "attn_norm", il);
145
146
0
        if (hparams.is_recr(il)) {
147
            // ssm layer //
148
0
            cur = build_mamba2_layer(inp->get_recr(), cur, model, ubatch, il);
149
0
        } else if (hparams.n_ff(il) == 0) {
150
            // attention layer //
151
0
            cur = build_attention_layer(cur, inp->get_attn(), model, n_embd_head, il);
152
0
        } else {
153
0
            cur = build_ffn_layer(cur, model, il);
154
0
        }
155
156
0
        if (il == n_layer - 1 && inp_out_ids) {
157
0
            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);
158
0
            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
159
0
        }
160
161
        // add residual
162
0
        cur = ggml_add(ctx0, cur, inpSA);
163
0
        cb(cur, "nemotron_h_block_out", il);
164
165
        // input for next layer
166
0
        inpL = cur;
167
0
    }
168
169
0
    cur = inpL;
170
171
0
    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
172
173
0
    cb(cur, "result_norm", -1);
174
0
    res->t_embd = cur;
175
176
    // lm_head
177
0
    cur = build_lora_mm(model.output, cur, model.output_s);
178
0
    cb(cur, "result_output", -1);
179
0
    res->t_logits = cur;
180
181
0
    ggml_build_forward_expand(gf, cur);
182
0
}
183
184
ggml_tensor * llama_model_nemotron_h::graph::build_attention_layer(ggml_tensor *             cur,
185
                                                          llm_graph_input_attn_kv * inp_attn,
186
                                                          const llama_model &       model,
187
                                                                int64_t             n_embd_head,
188
0
                                                                int                 il) {
189
0
    auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, n_embd_head, hparams.n_head(il), hparams.n_head_kv(il), il);
190
191
0
    const float kq_scale =
192
0
        hparams.f_attention_scale == 0.0f ? 1.0f / sqrtf(float(n_embd_head)) : hparams.f_attention_scale;
193
0
    cur = build_attn(inp_attn,
194
0
            model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
195
0
            Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il);
196
0
    cb(cur, "attn_out", il);
197
0
    return cur;
198
0
}
199
200
0
ggml_tensor * llama_model_nemotron_h::graph::build_ffn_layer(ggml_tensor * cur, const llama_model & model, int il) {
201
0
    if (model.layers[il].ffn_gate_inp == nullptr) {
202
0
        cur = build_ffn(cur,
203
0
                model.layers[il].ffn_up,   model.layers[il].ffn_up_b,   model.layers[il].ffn_up_s,
204
0
                NULL,                      NULL,                        NULL,
205
0
                model.layers[il].ffn_down, model.layers[il].ffn_down_b, model.layers[il].ffn_down_s,
206
0
                NULL,
207
0
                LLM_FFN_RELU_SQR, LLM_FFN_PAR, il);
208
0
        cb(cur, "ffn_out", il);
209
0
    } else {
210
0
        ggml_tensor * inp_emb    = cur;
211
0
        ggml_tensor * inp_latent = cur;
212
213
0
        if (model.layers[il].ffn_latent_down) {
214
0
            inp_latent = ggml_mul_mat(ctx0, model.layers[il].ffn_latent_down, cur);
215
0
        }
216
217
0
        ggml_tensor * router_logits = build_lora_mm(model.layers[il].ffn_gate_inp, cur);
218
0
        cb(router_logits, "ffn_moe_logits", il);
219
220
0
        ggml_tensor * moe_out =
221
0
            build_moe_ffn(inp_latent,
222
0
                    model.layers[il].ffn_gate_inp,
223
0
                    model.layers[il].ffn_up_exps,
224
0
                    nullptr, // no gate
225
0
                    model.layers[il].ffn_down_exps,
226
0
                    model.layers[il].ffn_exp_probs_b,
227
0
                    n_expert, n_expert_used,
228
0
                    LLM_FFN_RELU_SQR, hparams.expert_weights_norm,
229
0
                    hparams.expert_weights_scale,
230
0
                    LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID,
231
0
                    il,
232
0
                    router_logits, nullptr,
233
0
                    model.layers[il].ffn_up_exps_s,
234
0
                    nullptr, // no gate
235
0
                    model.layers[il].ffn_down_exps_s);
236
0
        cb(moe_out, "ffn_moe_out", il);
237
238
0
        if (model.layers[il].ffn_latent_up) {
239
0
            moe_out = ggml_mul_mat(ctx0, model.layers[il].ffn_latent_up, moe_out);
240
0
        }
241
242
0
        ggml_tensor * ffn_shexp = build_ffn(inp_emb,
243
0
                    model.layers[il].ffn_up_shexp,   NULL, model.layers[il].ffn_up_shexp_s,
244
0
                    NULL /* no gate */           ,   NULL, NULL,
245
0
                    model.layers[il].ffn_down_shexp, NULL, model.layers[il].ffn_down_shexp_s,
246
0
                    NULL,
247
0
                    LLM_FFN_RELU_SQR, LLM_FFN_PAR, il);
248
0
        cb(ffn_shexp, "ffn_shexp", il);
249
250
0
        cur = ggml_add(ctx0, moe_out, ffn_shexp);
251
0
        cb(cur, "ffn_out", il);
252
0
    }
253
254
0
    cur = build_cvec(cur, il);
255
0
    cb(cur, "l_out", il);
256
257
0
    return cur;
258
0
}