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/jamba.cpp
Line
Count
Source
1
#include "models.h"
2
3
0
void llama_model_jamba::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
9
0
    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps);
10
11
0
    for (uint32_t i = 0; i < hparams.n_layer(); ++i) {
12
0
        hparams.is_recr_impl[i] = hparams.n_head_kv(i) == 0;
13
0
    }
14
15
0
    switch (hparams.n_layer()) {
16
        // TODO: Jamba layers are a bit heterogeneous, so naming this is hard.
17
0
        case 12: // 900M  8x???M
18
0
        case 32: // 51B  16x?B
19
0
        default: type = LLM_TYPE_UNKNOWN;
20
0
    }
21
0
}
22
23
0
void llama_model_jamba::load_arch_tensors(llama_model_loader &) {
24
0
    LLAMA_LOAD_LOCALS;
25
26
0
    const int64_t d_conv  = hparams.ssm_d_conv;
27
0
    const int64_t d_inner = hparams.ssm_d_inner;
28
0
    const int64_t d_state = hparams.ssm_d_state;
29
0
    const int64_t dt_rank = hparams.ssm_dt_rank;
30
31
    // only an expansion factor of 2 is supported for now
32
0
    GGML_ASSERT(2 * n_embd == d_inner);
33
34
0
    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
35
36
    // output
37
0
    {
38
0
        output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
39
40
0
        output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
41
        // if output is NULL, init from the input tok embed, duplicated to allow offloading
42
0
        if (output == NULL) {
43
0
            output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
44
0
        }
45
0
    }
46
47
0
    for (int i = 0; i < n_layer; ++i) {
48
0
        const int64_t n_head_kv = hparams.n_head_kv(i);
49
0
        const int64_t n_embd_gqa = hparams.n_embd_v_gqa(i);
50
51
0
        auto & layer = layers[i];
52
53
        // norm
54
0
        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
55
56
0
        if (n_head_kv == 0) {
57
            // Mamba layer
58
0
            layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2*d_inner}, 0);
59
60
0
            layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner}, 0);
61
0
            layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner}, 0);
62
63
0
            layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {d_inner, dt_rank + 2*d_state}, 0);
64
65
0
            layer.ssm_dt_norm = create_tensor(tn(LLM_TENSOR_SSM_DT_NORM, "weight", i), {dt_rank}, 0);
66
67
0
            layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_rank, d_inner}, 0);
68
0
            layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {d_inner}, 0);
69
70
0
            layer.ssm_b_norm = create_tensor(tn(LLM_TENSOR_SSM_B_NORM, "weight", i), {d_state}, 0);
71
0
            layer.ssm_c_norm = create_tensor(tn(LLM_TENSOR_SSM_C_NORM, "weight", i), {d_state}, 0);
72
73
            // no "weight" suffix for these
74
0
            layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {d_state, d_inner}, 0);
75
0
            layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {d_inner}, 0);
76
77
            // out_proj
78
0
            layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0);
79
0
        } else {
80
            // Attention layers
81
82
0
            create_tensor_qkv(layer, i, n_embd, n_embd, n_embd_gqa, n_embd_gqa, 0);
83
0
            layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0);
84
0
        }
85
86
0
        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
87
88
0
        layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, TENSOR_NOT_REQUIRED);
89
90
0
        if (layer.ffn_gate_inp) {
91
            // MoE
92
0
            layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0);
93
0
            layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0);
94
0
            layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {n_embd, n_ff, n_expert}, 0);
95
0
        } else {
96
            // FFN (no MoE)
97
0
            layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0);
98
0
            layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0);
99
0
            layer.ffn_up   = create_tensor(tn(LLM_TENSOR_FFN_UP,   "weight", i), {n_embd, n_ff}, 0);
100
0
        }
101
0
    }
102
0
}
103
104
0
std::unique_ptr<llm_graph_context> llama_model_jamba::build_arch_graph(const llm_graph_params & params) const {
105
0
    return std::make_unique<graph>(*this, params);
106
0
}
107
108
0
llama_model_jamba::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_build_mamba_base(params) {
109
0
    const int64_t n_embd_head = hparams.n_embd_head_v();
110
111
0
    ggml_tensor * cur;
112
0
    ggml_tensor * inpL;
113
114
    // {n_embd, n_tokens}
115
0
    inpL = build_inp_embd(model.tok_embd);
116
117
0
    auto * inp_hybrid = build_inp_mem_hybrid();
118
119
0
    ggml_tensor * inp_out_ids = build_inp_out_ids();
120
121
0
    for (int il = 0; il < n_layer; ++il) {
122
0
        const int64_t n_head_kv = hparams.n_head_kv(il);
123
124
0
        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
125
0
        cb(cur, "attn_norm", il);
126
127
0
        if (n_head_kv == 0) {
128
0
            cur = build_mamba_layer(inp_hybrid->get_recr(), cur, model, ubatch, il);
129
0
        } else {
130
            // Attention
131
132
0
            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
133
0
                    n_embd_head, n_head, n_head_kv, il);
134
135
            // No RoPE :)
136
0
            cur = build_attn(inp_hybrid->get_attn(),
137
0
                    model.layers[il].wo, NULL, model.layers[il].wo_s,
138
0
                    Qcur, Kcur, Vcur, NULL, NULL, NULL, 1.0f/sqrtf(float(n_embd_head)), il);
139
0
        }
140
0
        if (il == n_layer - 1 && inp_out_ids) {
141
0
            cur  = ggml_get_rows(ctx0,  cur, inp_out_ids);
142
0
            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
143
0
        }
144
        // residual
145
0
        struct ggml_tensor * ffn_inp = ggml_add(ctx0, inpL, cur);
146
0
        cb(cur, "ffn_inp", il);
147
148
0
        cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
149
0
        cb(cur, "ffn_norm", il);
150
151
        // feed-forward network
152
0
        if (model.layers[il].ffn_gate_inp == nullptr) {
153
            // FFN
154
0
            cur = build_ffn(cur,
155
0
                    model.layers[il].ffn_up,   NULL, NULL,
156
0
                    model.layers[il].ffn_gate, NULL, NULL,
157
0
                    model.layers[il].ffn_down, NULL, NULL,
158
0
                    NULL,
159
0
                    LLM_FFN_SILU, LLM_FFN_PAR, il);
160
0
            cb(cur, "ffn_out", il);
161
0
        } else {
162
            // MoE branch
163
0
            cur = build_moe_ffn(cur,
164
0
                    model.layers[il].ffn_gate_inp,
165
0
                    model.layers[il].ffn_up_exps,
166
0
                    model.layers[il].ffn_gate_exps,
167
0
                    model.layers[il].ffn_down_exps,
168
0
                    nullptr,
169
0
                    n_expert, n_expert_used,
170
0
                    LLM_FFN_SILU, false,
171
0
                    hparams.expert_weights_scale,
172
0
                    LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
173
0
                    il);
174
0
            cb(cur, "ffn_moe_out", il);
175
0
        }
176
        // residual
177
0
        cur = ggml_add(ctx0, ffn_inp, cur);
178
179
0
        cur = build_cvec(cur, il);
180
0
        cb(cur, "l_out", il);
181
182
        // input for next layer
183
0
        inpL = cur;
184
0
    }
185
    // final rmsnorm
186
0
    cur = build_norm(inpL, model.output_norm, NULL, LLM_NORM_RMS, -1);
187
188
0
    cb(cur, "result_norm", -1);
189
0
    res->t_embd = cur;
190
191
    // lm_head
192
0
    cur = build_lora_mm(model.output, cur, model.output_s);
193
194
0
    cb(cur, "result_output", -1);
195
0
    res->t_logits = cur;
196
197
0
    ggml_build_forward_expand(gf, cur);
198
0
}