char-fork/llama.cpp

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

Yaniss Amazouzmodel: add NVIDIA Nemotron-3-Puzzle-75B-A9B (NemotronHPuzzle) support (#25444)c61b98b87

char/aliceai-t5-moe
7.8 KiB193 linesraw
1#include "models.h"
2
3void llama_model_grovemoe::load_arch_hparams(llama_model_loader & ml) {
4    ml.get_key_or_arr(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp_arr, hparams.n_layer_all);
5    ml.get_key(LLM_KV_EXPERT_CHUNK_FEED_FORWARD_LENGTH,  hparams.n_ff_chexp, false);
6    ml.get_key(LLM_KV_EXPERT_GROUP_SCALE,                hparams.expert_group_scale);
7    ml.get_key(LLM_KV_EXPERTS_PER_GROUP,                 hparams.n_group_experts);
8    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS,       hparams.f_norm_rms_eps);
9
10    switch (hparams.n_layer()) {
11        case 48: type = LLM_TYPE_30B_A3B; break;
12        default: type = LLM_TYPE_UNKNOWN;
13    }
14}
15
16void llama_model_grovemoe::load_arch_tensors(llama_model_loader &) {
17    LLAMA_LOAD_LOCALS;
18
19    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0);
20
21    // output
22    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0);
23    output      = create_tensor(tn(LLM_TENSOR_OUTPUT,      "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED);
24    // if output is NULL, init from the input tok embed
25    if (output == NULL) {
26        output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED);
27    }
28
29    GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for GROVEMOE");
30    GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for GROVEMOE");
31    GGML_ASSERT(hparams.n_group_experts > 0 && "n_group_experts must be > 0 for GROVEMOE");
32
33    for (int i = 0; i < n_layer; ++i) {
34        auto & layer = layers[i];
35
36        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0);
37
38        create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_gqa, n_embd_gqa, 0);
39        layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0);
40
41        layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0);
42        layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0);
43
44        layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0);
45
46        layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0);
47
48        // MoE branch
49        const int64_t n_ff_exp = hparams.n_ff_exp() ? hparams.n_ff_exp() : n_ff / n_expert_used;
50        const int64_t n_ff_chexp = hparams.n_ff_chexp ? hparams.n_ff_chexp : n_embd_head_k;
51        const int64_t n_chunk_expert = n_expert / hparams.n_group_experts;
52
53        layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {  n_embd, n_ff_exp, n_expert}, 0);
54        layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp,   n_embd, n_expert}, 0);
55        layer.ffn_up_exps   = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS,   "weight", i), {  n_embd, n_ff_exp, n_expert}, 0);
56
57        layer.ffn_gate_chexps = create_tensor(tn(LLM_TENSOR_FFN_GATE_CHEXPS, "weight", i), {  n_embd, n_ff_chexp, n_chunk_expert}, 0);
58        layer.ffn_down_chexps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_CHEXPS, "weight", i), {n_ff_chexp,   n_embd, n_chunk_expert}, 0);
59        layer.ffn_up_chexps   = create_tensor(tn(LLM_TENSOR_FFN_UP_CHEXPS,   "weight", i), {  n_embd, n_ff_chexp, n_chunk_expert}, 0);
60    }
61}
62
63std::unique_ptr<llm_graph_context> llama_model_grovemoe::build_arch_graph(const llm_graph_params & params) const {
64    return std::make_unique<graph>(*this, params);
65}
66
67llama_model_grovemoe::graph::graph(const llama_model & model, const llm_graph_params & params) :
68    llm_graph_context(params) {
69    const int64_t n_embd_head    = hparams.n_embd_head_v();
70    const int64_t n_chunk_expert = n_expert / hparams.n_group_experts;
71
72    GGML_ASSERT(n_embd_head == hparams.n_embd_head_k());
73    GGML_ASSERT(n_embd_head == n_rot);
74
75    ggml_tensor * cur;
76    ggml_tensor * inpL;
77
78    inpL = build_inp_embd(model.tok_embd);
79
80    // inp_pos - contains the positions
81    ggml_tensor * inp_pos = build_inp_pos();
82
83    auto * inp_attn = build_attn_inp_kv();
84
85    ggml_tensor * inp_out_ids = build_inp_out_ids();
86
87    for (int il = 0; il < n_layer; ++il) {
88        ggml_tensor * inpSA = inpL;
89
90        // norm
91        cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il);
92        cb(cur, "attn_norm", il);
93
94        // self_attention
95        {
96            // compute Q and K and RoPE them
97            auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur,
98                    n_embd_head, n_head, n_head_kv, il);
99
100            Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il);
101            cb(Qcur, "Qcur_normed", il);
102
103            Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
104                                 ext_factor, attn_factor, beta_fast, beta_slow);
105
106            Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il);
107            cb(Kcur, "Kcur_normed", il);
108
109            Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale,
110                                 ext_factor, attn_factor, beta_fast, beta_slow);
111
112            cb(Qcur, "Qcur", il);
113            cb(Kcur, "Kcur", il);
114            cb(Vcur, "Vcur", il);
115
116            cur = build_attn(inp_attn,
117                    model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s,
118                    Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f / sqrtf(float(n_embd_head)), il);
119        }
120
121        if (il == n_layer - 1 && inp_out_ids) {
122            cur   = ggml_get_rows(ctx0, cur, inp_out_ids);
123            inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids);
124        }
125
126        ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA);
127        cb(ffn_inp, "ffn_inp", il);
128
129        // MoE branch
130        cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il);
131        cb(cur, "ffn_norm", il);
132
133        ggml_tensor * probs = build_lora_mm(model.layers[il].ffn_gate_inp, cur);  // [n_expert, n_tokens]
134        cb(probs, "ffn_moe_logits", il);
135
136        ggml_tensor * moe_out =
137            build_moe_ffn(cur,
138                nullptr,
139                model.layers[il].ffn_up_exps,
140                model.layers[il].ffn_gate_exps,
141                model.layers[il].ffn_down_exps,
142                nullptr,
143                n_expert, n_expert_used,
144                LLM_FFN_SILU, true,
145                hparams.expert_weights_scale,
146                LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
147                il,
148                probs);
149        cb(moe_out, "ffn_moe_out", il);
150        cur = moe_out;
151
152        // TODO: Only do the expert selection and weights once
153        moe_out = build_moe_ffn(cur,
154                    nullptr,
155                    model.layers[il].ffn_up_chexps,
156                    model.layers[il].ffn_gate_chexps,
157                    model.layers[il].ffn_down_chexps,
158                    nullptr,
159                    n_chunk_expert, n_expert_used > n_chunk_expert ? n_chunk_expert : n_expert_used,
160                    LLM_FFN_SILU, true,
161                    hparams.expert_weights_scale,
162                    LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX,
163                    il,
164                    probs);
165        cb(moe_out, "ffn_adj_moe_out", il);
166
167        cur = ggml_add(ctx0, cur, ggml_scale(ctx0, moe_out, hparams.expert_group_scale));
168        cb(cur, "ffn_final_moe_out", il);
169
170        cur = ggml_add(ctx0, cur, ffn_inp);
171
172        cur = build_cvec(cur, il);
173        cb(cur, "l_out", il);
174
175        // input for next layer
176        inpL = cur;
177    }
178
179    cur = inpL;
180
181    cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1);
182
183    cb(cur, "result_norm", -1);
184    res->t_embd = cur;
185
186    // lm_head
187    cur = build_lora_mm(model.output, cur, model.output_s);
188
189    cb(cur, "result_output", -1);
190    res->t_logits = cur;
191
192    ggml_build_forward_expand(gf, cur);
193}