char-fork/llama.cpp

git clone https://git.t4t.associates/char-fork/llama.cpp

Georgi Gerganovhparams : refactor `hparams.n_layer` (#24060)7acb4e8cd

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