/src/llama.cpp/src/models/cohere2moe.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_cohere2moe::load_arch_hparams(llama_model_loader & ml) { |
4 | 0 | const bool found_norm = ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps, false); |
5 | 0 | const bool found_norm_rms = ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps, false); |
6 | 0 | if (!found_norm && !found_norm_rms) { |
7 | 0 | throw std::runtime_error("missing Cohere2 MoE norm epsilon"); |
8 | 0 | } |
9 | 0 | if (!found_norm_rms) { |
10 | 0 | hparams.f_norm_rms_eps = 0.0f; |
11 | 0 | } |
12 | |
|
13 | 0 | ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa); |
14 | 0 | ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale); |
15 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead); |
16 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
17 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false); |
18 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared, false); |
19 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false); |
20 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
21 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false); |
22 | |
|
23 | 0 | ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.n_layer_nextn, false); |
24 | 0 | GGML_ASSERT(hparams.n_layer_nextn < hparams.n_layer_all && "n_layer_nextn must be < n_layer"); |
25 | |
|
26 | 0 | if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) { |
27 | 0 | hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID; |
28 | 0 | } |
29 | |
|
30 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
31 | 0 | uint32_t swa_period = 4; |
32 | 0 | if (ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false)) { |
33 | 0 | hparams.set_swa_pattern(swa_period, true); |
34 | 0 | } else { |
35 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, hparams.is_swa_impl, hparams.n_layer()); |
36 | 0 | } |
37 | |
|
38 | 0 | hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train; |
39 | 0 | hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train; |
40 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
41 | |
|
42 | 0 | switch (hparams.n_layer()) { |
43 | 0 | case 49: type = LLM_TYPE_30B_A3B; break; |
44 | 0 | default: type = LLM_TYPE_UNKNOWN; |
45 | 0 | } |
46 | 0 | } |
47 | | |
48 | 0 | void llama_model_cohere2moe::load_arch_tensors(llama_model_loader & ml) { |
49 | 0 | LLAMA_LOAD_LOCALS; |
50 | |
|
51 | 0 | const bool mtp_only = (hparams.n_layer_nextn > 0) && (ml.get_weight("blk.0.attn_norm.weight") == nullptr); |
52 | | // Trunk-only: the GGUF declares MTP layers in metadata but the actual MTP |
53 | | // tensors live in a separate file. Mark MTP tensors NOT_REQUIRED so the |
54 | | // trunk loads cleanly. |
55 | 0 | const std::string mtp_probe = "blk." + std::to_string(n_layer) + ".nextn.eh_proj.weight"; |
56 | 0 | const bool trunk_only = (hparams.n_layer_nextn > 0) && (ml.get_weight(mtp_probe.c_str()) == nullptr); |
57 | 0 | const int trunk_flags = mtp_only ? TENSOR_NOT_REQUIRED : 0; |
58 | 0 | const int mtp_flags = trunk_only ? TENSOR_NOT_REQUIRED : 0; |
59 | |
|
60 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0); |
61 | | |
62 | | // output |
63 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0); |
64 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED); |
65 | | |
66 | | // if output is NULL, init from the input tok embed |
67 | 0 | if (output == NULL) { |
68 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED); |
69 | 0 | } |
70 | |
|
71 | 0 | if (n_expert == 0) { |
72 | 0 | throw std::runtime_error("n_expert must be > 0 for Cohere2Moe"); |
73 | 0 | } |
74 | 0 | if (n_expert_used == 0) { |
75 | 0 | throw std::runtime_error("n_expert_used must be > 0 for Cohere2Moe"); |
76 | 0 | } |
77 | | |
78 | 0 | auto load_block_trunk = [&](int i, int flags) { |
79 | 0 | auto & layer = layers[i]; |
80 | |
|
81 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, flags); |
82 | |
|
83 | 0 | create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_gqa, n_embd_gqa, flags); |
84 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, flags); |
85 | |
|
86 | 0 | if (static_cast<uint32_t>(i) < hparams.n_layer_dense_lead) { |
87 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, flags); |
88 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, flags); |
89 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, flags); |
90 | 0 | } else { |
91 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff; |
92 | |
|
93 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, flags); |
94 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, flags); |
95 | 0 | create_tensor_gate_up_exps(layer, i, n_embd, n_ff_exp, n_expert, flags); |
96 | |
|
97 | 0 | if (hparams.n_expert_shared > 0) { |
98 | 0 | const int64_t n_ff_shexp = hparams.n_ff_shexp ? hparams.n_ff_shexp : n_ff_exp * hparams.n_expert_shared; |
99 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags); |
100 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_shexp, n_embd }, flags); |
101 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags); |
102 | 0 | } |
103 | 0 | } |
104 | 0 | }; |
105 | |
|
106 | 0 | auto load_block_mtp = [&](int i, int flags) { |
107 | 0 | auto & layer = layers[i]; |
108 | | |
109 | | // MTP block looks like a full-attention Cohere2 MoE decoder block. |
110 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, flags); |
111 | |
|
112 | 0 | create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_gqa, n_embd_gqa, flags); |
113 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, flags); |
114 | |
|
115 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff; |
116 | | |
117 | | // Routed experts |
118 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, flags); |
119 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, flags); |
120 | 0 | create_tensor_gate_up_exps(layer, i, n_embd, n_ff_exp, n_expert, flags); |
121 | |
|
122 | 0 | if (hparams.n_expert_shared > 0) { |
123 | 0 | const int64_t n_ff_shexp = hparams.n_ff_shexp ? hparams.n_ff_shexp : n_ff_exp * hparams.n_expert_shared; |
124 | | |
125 | | // Shared experts |
126 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags); |
127 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_shexp, n_embd }, flags); |
128 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags); |
129 | 0 | } |
130 | | |
131 | | // NextN-specific tensors that define the MTP block. |
132 | 0 | layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags); |
133 | 0 | layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags); |
134 | 0 | layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags); |
135 | 0 | layer.nextn.embed_tokens = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED); |
136 | 0 | layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED); |
137 | 0 | layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, TENSOR_NOT_REQUIRED); |
138 | 0 | }; |
139 | |
|
140 | 0 | for (int i = 0; i < n_layer; ++i) { |
141 | 0 | load_block_trunk(i, trunk_flags); |
142 | 0 | } |
143 | | // MTP/NextN layers are loaded as extra decoder blocks. |
144 | 0 | for (int i = n_layer; i < n_layer_all; ++i) { |
145 | 0 | load_block_mtp(i, mtp_flags); |
146 | 0 | } |
147 | 0 | } |
148 | | |
149 | 0 | std::unique_ptr<llm_graph_context> llama_model_cohere2moe::build_arch_graph(const llm_graph_params & params) const { |
150 | 0 | if (params.gtype == LLM_GRAPH_TYPE_DECODER_MTP) { |
151 | 0 | return std::make_unique<graph_mtp>(*this, params); |
152 | 0 | } |
153 | 0 | return std::make_unique<graph>(*this, params); |
154 | 0 | } |
155 | | |
156 | 0 | llama_model_cohere2moe::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
157 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
158 | |
|
159 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
160 | 0 | GGML_ASSERT(n_embd_head == n_rot); |
161 | |
|
162 | 0 | const llm_norm_type cohere2moe_norm_type = hparams.f_norm_rms_eps == 0.0f ? LLM_NORM : LLM_NORM_RMS; |
163 | 0 | const float f_logit_scale = hparams.f_logit_scale; |
164 | 0 | ggml_tensor * cur; |
165 | 0 | ggml_tensor * inpL = build_inp_embd(model.tok_embd); |
166 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
167 | |
|
168 | 0 | auto * inp_attn = build_attn_inp_kv_iswa(); |
169 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
170 | | |
171 | | // MTP/NextN layers are loaded as extra decoder blocks but not executed in the main pass. |
172 | 0 | for (int il = 0; il < n_layer; ++il) { |
173 | 0 | const bool is_swa = hparams.is_swa(il); |
174 | | // Dense-prefix full-attention layers use RoPE; later layers follow the SWA pattern. |
175 | 0 | const bool force_rope = static_cast<uint32_t>(il) < hparams.n_layer_dense_lead; |
176 | |
|
177 | 0 | cur = build_norm(inpL, model.layers[il].attn_norm, nullptr, cohere2moe_norm_type, il); |
178 | 0 | cb(cur, "attn_norm", il); |
179 | |
|
180 | 0 | ggml_tensor * ffn_inp = cur; |
181 | |
|
182 | 0 | { |
183 | 0 | const auto & layer = model.layers[il]; |
184 | |
|
185 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(layer, cur, |
186 | 0 | n_embd_head, n_head, n_head_kv, il); |
187 | |
|
188 | 0 | if (is_swa || force_rope) { |
189 | 0 | ggml_tensor * rope_factors = model.get_rope_factors(cparams, il); |
190 | |
|
191 | 0 | Qcur = ggml_rope_ext( |
192 | 0 | ctx0, Qcur, inp_pos, rope_factors, |
193 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
194 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
195 | |
|
196 | 0 | Kcur = ggml_rope_ext( |
197 | 0 | ctx0, Kcur, inp_pos, rope_factors, |
198 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
199 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
200 | 0 | } |
201 | |
|
202 | 0 | cb(Qcur, "Qcur", il); |
203 | 0 | cb(Kcur, "Kcur", il); |
204 | 0 | cb(Vcur, "Vcur", il); |
205 | |
|
206 | 0 | cur = build_attn(inp_attn, |
207 | 0 | layer.wo, layer.wo_b, layer.wo_s, |
208 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, |
209 | 0 | 1.0f / sqrtf(float(n_embd_head)), il); |
210 | 0 | } |
211 | |
|
212 | 0 | if (il == n_layer - 1 && inp_out_ids && cparams.embeddings_nextn_masked) { |
213 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
214 | 0 | inpL = ggml_get_rows(ctx0, inpL, inp_out_ids); |
215 | 0 | ffn_inp = ggml_get_rows(ctx0, ffn_inp, inp_out_ids); |
216 | 0 | } |
217 | |
|
218 | 0 | ggml_tensor * attn_out = cur; |
219 | |
|
220 | 0 | const auto & layer = model.layers[il]; |
221 | |
|
222 | 0 | if (layer.ffn_gate_inp == nullptr) { |
223 | 0 | cur = build_ffn(ffn_inp, |
224 | 0 | layer.ffn_up, nullptr, layer.ffn_up_s, |
225 | 0 | layer.ffn_gate, nullptr, layer.ffn_gate_s, |
226 | 0 | layer.ffn_down, nullptr, layer.ffn_down_s, |
227 | 0 | nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il); |
228 | 0 | cb(cur, "ffn_out", il); |
229 | 0 | } else { |
230 | 0 | cur = build_moe_ffn(ffn_inp, |
231 | 0 | layer.ffn_gate_inp, |
232 | 0 | layer.ffn_up_exps, |
233 | 0 | layer.ffn_gate_exps, |
234 | 0 | layer.ffn_down_exps, |
235 | 0 | nullptr, |
236 | 0 | n_expert, n_expert_used, |
237 | 0 | LLM_FFN_SILU, hparams.expert_weights_norm, |
238 | 0 | hparams.expert_weights_scale, |
239 | 0 | (llama_expert_gating_func_type) hparams.expert_gating_func, |
240 | 0 | il, |
241 | 0 | nullptr, layer.ffn_gate_up_exps, |
242 | 0 | layer.ffn_up_exps_s, |
243 | 0 | layer.ffn_gate_exps_s, |
244 | 0 | layer.ffn_down_exps_s); |
245 | 0 | cb(cur, "ffn_moe_out", il); |
246 | |
|
247 | 0 | if (layer.ffn_up_shexp) { |
248 | 0 | ggml_tensor * ffn_shexp = build_ffn(ffn_inp, |
249 | 0 | layer.ffn_up_shexp, nullptr, layer.ffn_up_shexp_s, |
250 | 0 | layer.ffn_gate_shexp, nullptr, layer.ffn_gate_shexp_s, |
251 | 0 | layer.ffn_down_shexp, nullptr, layer.ffn_down_shexp_s, |
252 | 0 | nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il); |
253 | 0 | cb(ffn_shexp, "ffn_shexp", il); |
254 | |
|
255 | 0 | cur = ggml_add(ctx0, cur, ffn_shexp); |
256 | 0 | cur = ggml_scale(ctx0, cur, 0.5f); |
257 | 0 | cb(cur, "ffn_out", il); |
258 | 0 | } |
259 | 0 | } |
260 | |
|
261 | 0 | cur = ggml_add(ctx0, cur, inpL); |
262 | 0 | cur = ggml_add(ctx0, cur, attn_out); |
263 | |
|
264 | 0 | cur = build_cvec(cur, il); |
265 | 0 | cb(cur, "l_out", il); |
266 | |
|
267 | 0 | inpL = cur; |
268 | 0 | } |
269 | |
|
270 | 0 | cur = inpL; |
271 | 0 | cur = build_norm(cur, model.output_norm, nullptr, cohere2moe_norm_type, -1); |
272 | |
|
273 | 0 | cb(cur, "h_nextn", -1); |
274 | 0 | res->t_h_nextn = cur; |
275 | |
|
276 | 0 | if (!cparams.embeddings_nextn_masked && inp_out_ids) { |
277 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
278 | 0 | } |
279 | |
|
280 | 0 | cb(cur, "result_norm", -1); |
281 | 0 | res->t_embd = cur; |
282 | |
|
283 | 0 | cur = build_lora_mm(model.output, cur); |
284 | |
|
285 | 0 | if (f_logit_scale) { |
286 | 0 | cur = ggml_scale(ctx0, cur, f_logit_scale); |
287 | 0 | } |
288 | |
|
289 | 0 | cb(cur, "result_output", -1); |
290 | 0 | res->t_logits = cur; |
291 | |
|
292 | 0 | ggml_build_forward_expand(gf, cur); |
293 | 0 | } |
294 | | |
295 | 0 | llama_model_cohere2moe::graph_mtp::graph_mtp(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
296 | 0 | GGML_ASSERT(hparams.n_layer_nextn > 0 && "COHERE2MOE MTP requires n_layer_nextn > 0"); |
297 | 0 | GGML_ASSERT(hparams.n_layer_nextn == 1 && "COHERE2MOE MTP currently only supports a single MTP block"); |
298 | |
|
299 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
300 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
301 | 0 | GGML_ASSERT(n_embd_head == n_rot); |
302 | |
|
303 | 0 | const int il = hparams.n_layer(); |
304 | 0 | const auto & layer = model.layers[il]; |
305 | 0 | GGML_ASSERT(layer.nextn.eh_proj && "MTP block missing nextn.eh_proj"); |
306 | 0 | GGML_ASSERT(layer.nextn.enorm && "MTP block missing nextn.enorm"); |
307 | 0 | GGML_ASSERT(layer.nextn.hnorm && "MTP block missing nextn.hnorm"); |
308 | 0 | GGML_ASSERT(layer.ffn_gate_inp && "MTP block missing ffn_gate_inp"); |
309 | |
|
310 | 0 | const llm_norm_type cohere2moe_norm_type = hparams.f_norm_rms_eps == 0.0f ? LLM_NORM : LLM_NORM_RMS; |
311 | | |
312 | | // TODO: extract in a common llm_graph_context::build_inp_embd_h() |
313 | 0 | auto inp = std::make_unique<llm_graph_input_embd_h>(hparams.n_embd); |
314 | |
|
315 | 0 | inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens); |
316 | 0 | ggml_set_input(inp->tokens); |
317 | |
|
318 | 0 | inp->embd = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, hparams.n_embd_inp(), n_tokens); |
319 | 0 | ggml_set_input(inp->embd); |
320 | | |
321 | | // TODO: make static using `ggml_build_forward_select()` |
322 | | // see llm_graph_context::build_inp_embd() for reference |
323 | 0 | ggml_tensor * tok_embd; |
324 | 0 | if (ubatch.token) { |
325 | 0 | ggml_tensor * tok_embd_w = layer.nextn.embed_tokens ? layer.nextn.embed_tokens : model.tok_embd; |
326 | 0 | tok_embd = ggml_get_rows(ctx0, tok_embd_w, inp->tokens); |
327 | 0 | } else { |
328 | 0 | tok_embd = inp->embd; |
329 | 0 | } |
330 | 0 | cb(tok_embd, "mtp_tok_embd", il); |
331 | |
|
332 | 0 | inp->h = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, hparams.n_embd, n_tokens); |
333 | 0 | ggml_set_input(inp->h); |
334 | 0 | ggml_set_name(inp->h, "mtp_h_input"); |
335 | |
|
336 | 0 | ggml_tensor * h_embd = inp->h; |
337 | |
|
338 | 0 | res->add_input(std::move(inp)); |
339 | |
|
340 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
341 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
342 | 0 | auto * inp_attn = build_attn_inp_kv_iswa(); |
343 | |
|
344 | 0 | ggml_tensor * h_norm = build_norm(h_embd, layer.nextn.hnorm, nullptr, cohere2moe_norm_type, il); |
345 | 0 | cb(h_norm, "mtp_hnorm", il); |
346 | |
|
347 | 0 | ggml_tensor * e_norm = build_norm(tok_embd, layer.nextn.enorm, nullptr, cohere2moe_norm_type, il); |
348 | 0 | cb(e_norm, "mtp_enorm", il); |
349 | |
|
350 | 0 | ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, /*dim=*/ 0); |
351 | 0 | cb(concat, "mtp_concat", il); |
352 | |
|
353 | 0 | ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s); |
354 | 0 | cb(cur, "mtp_eh_proj", il); |
355 | |
|
356 | 0 | ggml_tensor * inpL = cur; |
357 | |
|
358 | 0 | cur = build_norm(cur, layer.attn_norm, nullptr, cohere2moe_norm_type, il); |
359 | 0 | cb(cur, "mtp_attn_norm", il); |
360 | 0 | ggml_tensor * ffn_inp = cur; |
361 | |
|
362 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(layer, cur, n_embd_head, n_head, n_head_kv, il); |
363 | 0 | ggml_tensor * rope_factors = model.get_rope_factors(cparams, il); |
364 | 0 | Qcur = ggml_rope_ext( |
365 | 0 | ctx0, Qcur, inp_pos, rope_factors, |
366 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
367 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
368 | 0 | Kcur = ggml_rope_ext( |
369 | 0 | ctx0, Kcur, inp_pos, rope_factors, |
370 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
371 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
372 | |
|
373 | 0 | cb(Qcur, "mtp_Qcur", il); |
374 | 0 | cb(Kcur, "mtp_Kcur", il); |
375 | 0 | cb(Vcur, "mtp_Vcur", il); |
376 | |
|
377 | 0 | cur = build_attn(inp_attn, |
378 | 0 | layer.wo, layer.wo_b, layer.wo_s, |
379 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, |
380 | 0 | 1.0f / sqrtf(float(n_embd_head)), il); |
381 | 0 | cb(cur, "mtp_attn_out", il); |
382 | |
|
383 | 0 | ggml_tensor * attn_out = cur; |
384 | |
|
385 | 0 | cur = build_moe_ffn(ffn_inp, |
386 | 0 | layer.ffn_gate_inp, |
387 | 0 | layer.ffn_up_exps, |
388 | 0 | layer.ffn_gate_exps, |
389 | 0 | layer.ffn_down_exps, |
390 | 0 | nullptr, |
391 | 0 | n_expert, n_expert_used, |
392 | 0 | LLM_FFN_SILU, hparams.expert_weights_norm, |
393 | 0 | hparams.expert_weights_scale, |
394 | 0 | (llama_expert_gating_func_type) hparams.expert_gating_func, |
395 | 0 | il, |
396 | 0 | nullptr, layer.ffn_gate_up_exps, |
397 | 0 | layer.ffn_up_exps_s, |
398 | 0 | layer.ffn_gate_exps_s, |
399 | 0 | layer.ffn_down_exps_s); |
400 | 0 | cb(cur, "mtp_ffn_moe_out", il); |
401 | |
|
402 | 0 | if (layer.ffn_up_shexp) { |
403 | 0 | ggml_tensor * ffn_shexp = build_ffn(ffn_inp, |
404 | 0 | layer.ffn_up_shexp, nullptr, layer.ffn_up_shexp_s, |
405 | 0 | layer.ffn_gate_shexp, nullptr, layer.ffn_gate_shexp_s, |
406 | 0 | layer.ffn_down_shexp, nullptr, layer.ffn_down_shexp_s, |
407 | 0 | nullptr, LLM_FFN_SILU, LLM_FFN_PAR, il); |
408 | 0 | cb(ffn_shexp, "mtp_ffn_shexp", il); |
409 | |
|
410 | 0 | cur = ggml_add(ctx0, cur, ffn_shexp); |
411 | 0 | cur = ggml_scale(ctx0, cur, 0.5f); |
412 | 0 | cb(cur, "mtp_ffn_out", il); |
413 | 0 | } |
414 | |
|
415 | 0 | cur = ggml_add(ctx0, cur, inpL); |
416 | 0 | cur = ggml_add(ctx0, cur, attn_out); |
417 | 0 | cb(cur, "mtp_post_ffn", il); |
418 | |
|
419 | 0 | ggml_tensor * head_norm_w = layer.nextn.shared_head_norm |
420 | 0 | ? layer.nextn.shared_head_norm |
421 | 0 | : model.output_norm; |
422 | 0 | GGML_ASSERT(head_norm_w && "COHERE2MOE MTP: missing both nextn.shared_head_norm and output_norm"); |
423 | 0 | cur = build_norm(cur, head_norm_w, nullptr, cohere2moe_norm_type, -1); |
424 | |
|
425 | 0 | cb(cur, "h_nextn", -1); |
426 | 0 | res->t_h_nextn = cur; |
427 | |
|
428 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
429 | 0 | cb(cur, "mtp_shared_head_norm", -1); |
430 | |
|
431 | 0 | ggml_tensor * head_w = layer.nextn.shared_head_head ? layer.nextn.shared_head_head : model.output; |
432 | 0 | GGML_ASSERT(head_w && "COHERE2MOE MTP: missing LM head (nextn.shared_head_head or model.output)"); |
433 | 0 | cur = build_lora_mm(head_w, cur, layer.nextn.shared_head_head ? layer.nextn.shared_head_head_s : nullptr); |
434 | |
|
435 | 0 | if (hparams.f_logit_scale) { |
436 | 0 | cur = ggml_scale(ctx0, cur, hparams.f_logit_scale); |
437 | 0 | } |
438 | |
|
439 | 0 | cb(cur, "result_output", -1); |
440 | 0 | res->t_logits = cur; |
441 | |
|
442 | 0 | ggml_build_forward_expand(gf, cur); |
443 | 0 | } |