char-fork/llama.cpp

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

Sigbjørn Skjæretmodels : fix incorrect uses of get_key_or_arr (#28868)97e4ca735

char/aliceai-t5-moe
8.3 KiB200 linesraw
1#include "models.h"
2
3void llama_model_gemma4_assistant::load_arch_hparams(llama_model_loader & ml) {
4    hparams.n_embd_inp_impl = hparams.n_embd_out();
5
6    hparams.swa_type = LLAMA_SWA_TYPE_STANDARD;
7    ml.get_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, hparams.is_swa_impl);
8
9    uint32_t n_kv_shared_layers = 0;
10    ml.get_key(LLM_KV_ATTENTION_SHARED_KV_LAYERS, n_kv_shared_layers, false);
11
12    hparams.f_attention_scale = 1.0f;
13
14    ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA,           hparams.rope_freq_base_train_swa, false);
15    ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW,     hparams.n_swa);
16    ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS,  hparams.f_norm_rms_eps);
17    ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH_SWA,     hparams.n_embd_head_k_swa);
18    ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH_SWA,   hparams.n_embd_head_v_swa);
19}
20
21void llama_model_gemma4_assistant::load_arch_tensors(llama_model_loader &) {
22    LLAMA_LOAD_LOCALS;
23
24    if (n_embd_head_k != n_embd_head_v) {
25        throw std::runtime_error("Gemma 4 assistant requires n_embd_head_k == n_embd_head_v");
26    }
27    if (hparams.n_embd_head_k_swa != hparams.n_embd_head_v_swa) {
28        throw std::runtime_error("Gemma 4 assistant requires n_embd_head_k_swa == n_embd_head_v_swa");
29    }
30    if (hparams.n_embd_out() == n_embd) {
31        throw std::runtime_error("Gemma 4 assistant requires embedding_length_out to carry the target hidden size");
32    }
33
34    tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0);
35    output   = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED);
36
37    output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0);
38
39    create_tensor(tn(LLM_TENSOR_MASKED_EMBD_CENTROIDS, "weight"), {}, TENSOR_NOT_REQUIRED);
40    create_tensor(tn(LLM_TENSOR_MASKED_EMBD_ORDERING),  {}, TENSOR_NOT_REQUIRED);
41
42    const int64_t n_embd_backbone = hparams.n_embd_inp();
43    nextn_proj_post = create_tensor(tn(LLM_TENSOR_NEXTN_PROJ_POST, "weight"), { n_embd, n_embd_backbone }, 0);
44
45    int rope_freqs_flag = 0;
46
47    for (int i = 0; i < n_layer_nextn; ++i) {
48        auto & layer = layers[i];
49
50        const int64_t n_head      = hparams.n_head(i);
51        const int64_t n_embd_head = hparams.n_embd_head_k(i);
52        const int64_t n_ff        = hparams.n_ff(i);
53
54        if (i == 0) {
55            nextn_proj_pre = create_tensor(tn(LLM_TENSOR_NEXTN_PROJ_PRE, "weight", i), { 2*n_embd_backbone, n_embd }, 0);
56        }
57
58        layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0);
59        layer.wq        = create_tensor(tn(LLM_TENSOR_ATTN_Q,    "weight", i), { n_embd, n_embd_head*n_head }, 0);
60        layer.wo        = create_tensor(tn(LLM_TENSOR_ATTN_OUT,  "weight", i), { n_embd_head*n_head, n_embd }, 0);
61
62        layer.attn_q_norm    = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM,    "weight", i), { n_embd_head }, 0);
63        layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), { n_embd }, 0);
64
65        layer.out_scale = create_tensor(tn(LLM_TENSOR_LAYER_OUT_SCALE, "weight", i), { 1u }, 0);
66
67        if (!hparams.is_swa(i)) {
68            layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), { n_embd_head/2 }, rope_freqs_flag);
69            rope_freqs_flag = TENSOR_DUPLICATED;
70        }
71
72        layer.ffn_norm      = create_tensor(tn(LLM_TENSOR_FFN_NORM,      "weight", i), { n_embd }, 0);
73        layer.ffn_gate      = create_tensor(tn(LLM_TENSOR_FFN_GATE,      "weight", i), { n_embd, n_ff }, 0);
74        layer.ffn_up        = create_tensor(tn(LLM_TENSOR_FFN_UP,        "weight", i), { n_embd, n_ff }, 0);
75        layer.ffn_down      = create_tensor(tn(LLM_TENSOR_FFN_DOWN,      "weight", i), { n_ff, n_embd }, 0);
76        layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), { n_embd }, 0);
77    }
78}
79
80std::unique_ptr<llm_graph_context> llama_model_gemma4_assistant::build_arch_graph(const llm_graph_params & params) const {
81    return std::make_unique<graph>(*this, params);
82}
83
84llama_model_gemma4_assistant::graph::graph(const llama_model & model, const llm_graph_params & params) :
85        llm_graph_context(params) {
86    const int64_t n_embd_backbone = hparams.n_embd_inp();
87
88    ggml_tensor * inp_tokens;
89    ggml_tensor * inp_h;
90    {
91        auto inp = std::make_unique<llm_graph_input_embd>(n_embd_backbone);
92
93        inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, ubatch.n_tokens);
94        cb(inp->tokens, "inp_tokens", -1);
95        ggml_set_input(inp->tokens);
96        inp_tokens = inp->tokens;
97        res->t_inp_tokens = inp->tokens;
98
99        inp->embd = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, n_embd_backbone, ubatch.n_tokens);
100        cb(inp->embd, "inp_h", -1);
101        ggml_set_input(inp->embd);
102        inp_h = inp->embd;
103        res->t_inp_embd = inp->embd;
104
105        res->add_input(std::move(inp));
106    }
107
108    GGML_ASSERT(cparams.ctx_other != nullptr);
109    const auto * model_other = llama_get_model(cparams.ctx_other);
110
111    ggml_tensor * x = ggml_get_rows(ctx0, model_other->tok_embd, inp_tokens);
112    x = ggml_scale(ctx0, x, sqrtf((float) n_embd_backbone));
113    cb(x, "inp_embd_target", -1);
114
115    ggml_tensor * xh = ggml_concat(ctx0, x, inp_h, 0);
116    cb(xh, "inp_xh", -1);
117
118    ggml_tensor * cur = ggml_mul_mat(ctx0, model.nextn_proj_pre, xh);
119    cb(cur, "pre_proj", -1);
120
121    auto *        inp_attn    = build_attn_inp_kv_iswa();
122    ggml_tensor * inp_pos     = build_inp_pos();
123    ggml_tensor * inp_out_ids = build_inp_out_ids();
124
125    ggml_tensor * inpL = cur;
126
127    for (int il = 0; il < n_layer_nextn; ++il) {
128        const bool is_swa = hparams.is_swa(il);
129
130        const int64_t n_embd_head = hparams.n_embd_head_k(il);
131        const int64_t n_head      = hparams.n_head(il);
132
133        const float freq_base_l  = model.get_rope_freq_base(cparams, il);
134        const float freq_scale_l = model.get_rope_freq_scale(cparams, il);
135        const int   n_rot_l      = hparams.n_rot(il);
136
137        ggml_tensor * cur_norm = build_norm(inpL, model.layers[il].attn_norm, nullptr, LLM_NORM_RMS, il);
138        cb(cur_norm, "attn_norm", il);
139
140        ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur_norm);
141        Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens);
142        Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, nullptr, LLM_NORM_RMS, il);
143        cb(Qcur, "Qcur_normed", il);
144
145        ggml_tensor * freq_factors = is_swa ? nullptr : model.layers[il].rope_freqs;
146        Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, freq_factors, n_rot_l, rope_type, n_ctx_orig,
147                             freq_base_l, freq_scale_l, ext_factor, attn_factor, beta_fast, beta_slow);
148        cb(Qcur, "Qcur_pos", il);
149
150        cur = build_attn(inp_attn, model.layers[il].wo, nullptr, nullptr,
151                Qcur, nullptr, nullptr, nullptr, nullptr, nullptr, hparams.f_attention_scale, il);
152
153        if (il == n_layer_nextn - 1 && inp_out_ids) {
154            cur  = ggml_get_rows(ctx0, cur,  inp_out_ids);
155            inpL = ggml_get_rows(ctx0, inpL, inp_out_ids);
156        }
157
158        cur = build_norm(cur, model.layers[il].attn_post_norm, nullptr, LLM_NORM_RMS, il);
159        cb(cur, "attn_post_norm", il);
160
161        ggml_tensor * attn_out = ggml_add(ctx0, cur, inpL);
162        cb(attn_out, "attn_out", il);
163
164        cur = build_norm(attn_out, model.layers[il].ffn_norm, nullptr, LLM_NORM_RMS, il);
165        cb(cur, "ffn_norm", il);
166
167        cur = build_ffn(cur,
168                model.layers[il].ffn_up,   nullptr, nullptr,
169                model.layers[il].ffn_gate, nullptr, nullptr,
170                model.layers[il].ffn_down, nullptr, nullptr,
171                nullptr,
172                LLM_FFN_GELU, LLM_FFN_PAR, il);
173        cb(cur, "ffn_out", il);
174
175        cur = build_norm(cur, model.layers[il].ffn_post_norm, nullptr, LLM_NORM_RMS, -1);
176        cb(cur, "ffn_post_norm", il);
177
178        cur = ggml_add(ctx0, cur, attn_out);
179
180        cur = ggml_mul(ctx0, cur, model.layers[il].out_scale);
181        cb(cur, "out_scaled", il);
182
183        inpL = cur;
184    }
185    cur = inpL;
186
187    cur = build_norm(cur, model.output_norm, nullptr, LLM_NORM_RMS, -1);
188    cb(cur, "result_norm", -1);
189
190    ggml_tensor * logits = build_lora_mm(model.output, cur);
191    cb(logits, "result_output", -1);
192    res->t_logits = logits;
193
194    ggml_tensor * h_next = ggml_mul_mat(ctx0, model.nextn_proj_post, cur);
195    cb(h_next, "h_nextn", -1);
196    res->t_h_nextn = h_next;
197
198    ggml_build_forward_expand(gf, logits);
199    ggml_build_forward_expand(gf, h_next);
200}