/src/llama.cpp/src/models/glm-dsa.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | | #include "llama-kv-cache-dsa.h" |
4 | | |
5 | | // https://huggingface.co/zai-org/GLM-5.2/blob/main/config.json#L26 |
6 | | const std::array<uint32_t, LLAMA_MAX_LAYERS> GLM_5_2_DEFAULT_INDEXER_TYPES = { |
7 | | 1, 1, |
8 | | 1, 0, 0, 0, |
9 | | 1, 0, 0, 0, |
10 | | 1, 0, 0, 0, |
11 | | 1, 0, 0, 0, |
12 | | 1, 0, 0, 0, |
13 | | 1, 0, 0, 0, |
14 | | 1, 0, 0, 0, |
15 | | 1, 0, 0, 0, |
16 | | 1, 0, 0, 0, |
17 | | 1, 0, 0, 0, |
18 | | 1, 0, 0, 0, |
19 | | 1, 0, 0, 0, |
20 | | 1, 0, 0, 0, |
21 | | 1, 0, 0, 0, |
22 | | 1, 0, 0, 0, |
23 | | 1, 0, 0, 0, |
24 | | 1, 0, 0, 0, |
25 | | 1, 0, 0, 0, |
26 | | 1, 0, 0, 0, |
27 | | }; |
28 | | |
29 | 0 | void llama_model_glm_dsa::load_arch_hparams(llama_model_loader & ml) { |
30 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
31 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
32 | 0 | ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, false); |
33 | | |
34 | | // MoE parameters |
35 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared); |
36 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
37 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
38 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false); |
39 | | |
40 | | // deepseek MLA parameters |
41 | 0 | ml.get_key(LLM_KV_ATTENTION_Q_LORA_RANK, hparams.n_lora_q); |
42 | 0 | ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv); |
43 | 0 | ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH_MLA, hparams.n_embd_head_k_mla_impl, false); |
44 | 0 | ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH_MLA, hparams.n_embd_head_v_mla_impl, false); |
45 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
46 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared); |
47 | | |
48 | | // DSA parameters |
49 | 0 | ml.get_key(LLM_KV_ATTENTION_INDEXER_HEAD_COUNT, hparams.indexer_n_head); |
50 | 0 | ml.get_key(LLM_KV_ATTENTION_INDEXER_KEY_LENGTH, hparams.indexer_head_size); |
51 | 0 | ml.get_key(LLM_KV_ATTENTION_INDEXER_TOP_K, hparams.indexer_top_k); |
52 | | |
53 | | // Expert gating function (GLM-4.5 uses sigmoid) |
54 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false); |
55 | 0 | if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) { |
56 | 0 | hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID; |
57 | 0 | } |
58 | | |
59 | | // NextN/MTP parameters |
60 | 0 | ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.n_layer_nextn, false); |
61 | 0 | GGML_ASSERT(hparams.n_layer_nextn < hparams.n_layer_all && "n_layer_nextn must be < n_layer_all"); |
62 | | |
63 | | // BC for GLM 5, 5.1 (full indexers) without indexer_types metadata |
64 | 0 | const bool is_pre_5_2 = hparams.n_ctx_train < 1048576; |
65 | 0 | if (is_pre_5_2) { |
66 | 0 | std::fill(hparams.is_indexer_full_impl.begin(), hparams.is_indexer_full_impl.end(), 1); |
67 | 0 | } else { |
68 | 0 | hparams.is_indexer_full_impl = GLM_5_2_DEFAULT_INDEXER_TYPES; |
69 | 0 | } |
70 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_INDEXER_TYPES, hparams.is_indexer_full_impl, hparams.n_layer(), false); |
71 | |
|
72 | 0 | switch (hparams.n_layer()) { |
73 | 0 | case 78: // GGUF with NextN/MTP metadata: n_layer() excludes the nextn layer |
74 | 0 | case 79: |
75 | 0 | type = LLM_TYPE_744B_A40B; break; |
76 | 0 | default: type = LLM_TYPE_UNKNOWN; |
77 | 0 | } |
78 | 0 | } |
79 | | |
80 | 0 | void llama_model_glm_dsa::load_arch_tensors(llama_model_loader & ml) { |
81 | 0 | LLAMA_LOAD_LOCALS; |
82 | 0 | const int64_t n_expert_shared = hparams.n_expert_shared; |
83 | | |
84 | | // MTP-only: the GGUF carries only the NextN/MTP block(s) (user split target/draft). |
85 | 0 | const bool mtp_only = (hparams.n_layer_nextn > 0) && (ml.get_weight("blk.0.attn_norm.weight") == nullptr); |
86 | | // Trunk-only: the GGUF declares MTP layers in metadata but the actual MTP |
87 | | // tensors live in a separate file (or were stripped at conversion). Mark |
88 | | // MTP tensors NOT_REQUIRED so the trunk loads cleanly. |
89 | 0 | const std::string mtp_probe = "blk." + std::to_string(n_layer) + ".nextn.eh_proj.weight"; |
90 | 0 | const bool trunk_only = (hparams.n_layer_nextn > 0) && (ml.get_weight(mtp_probe.c_str()) == nullptr); |
91 | 0 | const int trunk_flags = mtp_only ? TENSOR_NOT_REQUIRED : 0; |
92 | 0 | int mtp_flags = trunk_only ? TENSOR_NOT_REQUIRED : 0; |
93 | |
|
94 | 0 | if (!ml.load_mtp) { |
95 | 0 | mtp_flags |= TENSOR_SKIP; |
96 | 0 | } |
97 | |
|
98 | 0 | const bool is_mla = hparams.is_mla(); |
99 | 0 | if (!is_mla) { |
100 | 0 | throw std::runtime_error("GLM_DSA architecture requires MLA"); |
101 | 0 | } |
102 | | |
103 | | // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA |
104 | 0 | const int64_t n_embd_head_k_mla = hparams.n_embd_head_k_mla(); |
105 | 0 | const int64_t n_embd_head_v_mla = hparams.n_embd_head_v_mla(); |
106 | |
|
107 | 0 | const int64_t n_embd_head_qk_rope = hparams.n_rot(); |
108 | 0 | const int64_t n_embd_head_qk_nope = n_embd_head_k_mla - n_embd_head_qk_rope; |
109 | |
|
110 | 0 | const int64_t q_lora_rank = hparams.n_lora_q; |
111 | 0 | const int64_t kv_lora_rank = hparams.n_lora_kv; |
112 | |
|
113 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
114 | |
|
115 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
116 | | |
117 | | // output |
118 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
119 | | // try to load output.weight, if not found, use token_embd (tied embeddings) |
120 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
121 | 0 | if (!output) { |
122 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
123 | 0 | } |
124 | |
|
125 | 0 | for (int i = 0; i < n_layer_all; ++i) { |
126 | | // NextN/MTP layers (i >= n_layer) are full decoder blocks used by the |
127 | | // LLM_GRAPH_TYPE_DECODER_MTP draft head; load them like qwen35moe/step35/hy_v3. |
128 | 0 | const int flags = (i >= n_layer) ? mtp_flags : trunk_flags; |
129 | |
|
130 | 0 | auto & layer = layers[i]; |
131 | |
|
132 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, flags); |
133 | 0 | layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, flags); |
134 | 0 | layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, flags); |
135 | |
|
136 | 0 | layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, flags); |
137 | 0 | layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k_mla}, flags); |
138 | |
|
139 | 0 | layer.wkv_a_mqa = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_MQA, "weight", i), {n_embd, kv_lora_rank + n_embd_head_qk_rope}, flags); |
140 | | |
141 | | // note: only old legacy GGUF files will have the unsplit wkv_b tensor in |
142 | 0 | layer.wk_b = create_tensor(tn(LLM_TENSOR_ATTN_K_B, "weight", i), {n_embd_head_qk_nope, kv_lora_rank, n_head}, flags); |
143 | 0 | layer.wv_b = create_tensor(tn(LLM_TENSOR_ATTN_V_B, "weight", i), {kv_lora_rank, n_embd_head_v_mla, n_head}, flags); |
144 | |
|
145 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_embd_head_v_mla, n_embd}, flags); |
146 | |
|
147 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, flags); |
148 | | |
149 | | // DSA indexer |
150 | 0 | layer.indexer_k_norm = create_tensor(tn(LLM_TENSOR_INDEXER_K_NORM, "weight", i), {hparams.indexer_head_size}, flags | TENSOR_NOT_REQUIRED); |
151 | 0 | layer.indexer_k_norm_b = create_tensor(tn(LLM_TENSOR_INDEXER_K_NORM, "bias", i), {hparams.indexer_head_size}, flags | TENSOR_NOT_REQUIRED); |
152 | 0 | layer.indexer_proj = create_tensor(tn(LLM_TENSOR_INDEXER_PROJ, "weight", i), {n_embd, hparams.indexer_n_head}, flags | TENSOR_NOT_REQUIRED); |
153 | 0 | layer.indexer_attn_k = create_tensor(tn(LLM_TENSOR_INDEXER_ATTN_K, "weight", i), {n_embd, hparams.indexer_head_size}, flags | TENSOR_NOT_REQUIRED); |
154 | 0 | layer.indexer_attn_q_b = create_tensor(tn(LLM_TENSOR_INDEXER_ATTN_Q_B, "weight", i), {q_lora_rank, hparams.indexer_n_head * hparams.indexer_head_size}, flags | TENSOR_NOT_REQUIRED); |
155 | 0 | if (i < (int) hparams.n_layer_dense_lead) { |
156 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, flags); |
157 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, flags); |
158 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, flags); |
159 | 0 | } else { |
160 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, flags); |
161 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED); |
162 | |
|
163 | 0 | if (n_expert == 0) { |
164 | 0 | throw std::runtime_error("n_expert must be > 0"); |
165 | 0 | } |
166 | 0 | if (n_expert_used == 0) { |
167 | 0 | throw std::runtime_error("n_expert_used must be > 0"); |
168 | 0 | } |
169 | | |
170 | | // MoE branch |
171 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, flags); |
172 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, flags); |
173 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, flags); |
174 | | |
175 | | // Shared expert branch |
176 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, flags); |
177 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, flags); |
178 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, flags); |
179 | 0 | } |
180 | | |
181 | | // NextN/MTP tensors - the NextN-specific wiring around the extra decoder block |
182 | 0 | if (i >= n_layer) { |
183 | 0 | layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags); |
184 | 0 | layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags); |
185 | 0 | layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags); |
186 | | |
187 | | // Optional tensors |
188 | 0 | layer.nextn.embed_tokens = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED); |
189 | 0 | layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED); |
190 | 0 | layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, flags | TENSOR_NOT_REQUIRED); |
191 | 0 | } |
192 | 0 | } |
193 | 0 | } |
194 | | |
195 | 0 | std::unique_ptr<llm_graph_context> llama_model_glm_dsa::build_arch_graph(const llm_graph_params & params) const { |
196 | 0 | if (params.gtype == LLM_GRAPH_TYPE_DECODER_MTP) { |
197 | 0 | return std::make_unique<graph_mtp>(*this, params); |
198 | 0 | } |
199 | 0 | return std::make_unique<graph>(*this, params); |
200 | 0 | } |
201 | | |
202 | | llama_model_glm_dsa::graph::graph(const llama_model & model, const llm_graph_params & params) : |
203 | 0 | llm_graph_context(params) { |
204 | 0 | const bool is_mla = hparams.is_mla(); |
205 | 0 | GGML_ASSERT(is_mla); |
206 | | |
207 | | // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA |
208 | 0 | const int64_t n_embd_head_k = hparams.n_embd_head_k_mla(); |
209 | 0 | const int64_t n_embd_head_v = hparams.n_embd_head_v_mla(); |
210 | 0 | GGML_UNUSED(n_embd_head_v); |
211 | |
|
212 | 0 | const int64_t n_embd_head_qk_rope = hparams.n_rot(); |
213 | 0 | const int64_t n_embd_head_qk_nope = n_embd_head_k - n_embd_head_qk_rope; |
214 | |
|
215 | 0 | const int64_t n_indexer_head = hparams.indexer_n_head; |
216 | 0 | const int64_t n_embd_indexer_head = hparams.indexer_head_size; |
217 | 0 | const uint32_t n_indexer_top_k = hparams.indexer_top_k; |
218 | | |
219 | | // the indexer head layout is [rope | nope] |
220 | 0 | GGML_ASSERT(hparams.n_rot() <= n_embd_indexer_head); |
221 | |
|
222 | 0 | const uint32_t kv_lora_rank = hparams.n_lora_kv; |
223 | | |
224 | | // We have to pre-scale kq_scale and attn_factor to make the YaRN RoPE work correctly. |
225 | | // See https://github.com/ggml-org/llama.cpp/discussions/7416 for detailed explanation. |
226 | | // And also: https://github.com/ggml-org/llama.cpp/pull/17945 [TAG_DEEPSEEK2_YARN_LOG_MUL_FIX] |
227 | | |
228 | | // first cancel the adjustment from llama_hparams::yarn_attn_factor_adjust to get the original attn_factor |
229 | 0 | GGML_ASSERT(ext_factor >= 0.0f); |
230 | 0 | const float attn_factor_org = attn_factor * (1.0f + 0.1f * logf(1.0f / freq_scale)); |
231 | | |
232 | | // use the original attn_factor to pre-scale the kq_scale |
233 | 0 | const float mscale = attn_factor_org * (1.0f + 0.1f * hparams.rope_yarn_log_mul * logf(1.0f / freq_scale)); |
234 | 0 | const float kq_scale = 1.0f * mscale * mscale / sqrtf(float(n_embd_head_k)); |
235 | |
|
236 | 0 | ggml_tensor * cur; |
237 | 0 | ggml_tensor * inpL; |
238 | | |
239 | | // {n_embd, n_tokens} |
240 | 0 | inpL = build_inp_embd(model.tok_embd); |
241 | | |
242 | | // inp_pos - contains the positions |
243 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
244 | |
|
245 | 0 | llm_graph_input_attn_k_dsa * inp_attn_dsa = build_attn_inp_k_dsa(); |
246 | |
|
247 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
248 | | |
249 | | // Difference vs Deepseek 3.2: shared indexer layers reuse the top_k from the previous full indexer layers |
250 | | // See https://huggingface.co/zai-org/GLM-5.2/blob/main/config.json#L30 |
251 | 0 | ggml_tensor * prev_top_k = nullptr; |
252 | 0 | for (int il = 0; il < n_layer; ++il) { |
253 | 0 | ggml_tensor * inpSA = inpL; |
254 | | |
255 | | // norm |
256 | 0 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
257 | 0 | cb(cur, "attn_norm", il); |
258 | | |
259 | | // self_attention |
260 | 0 | { |
261 | 0 | ggml_tensor * qr = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur); |
262 | 0 | cb(qr, "qr", il); |
263 | |
|
264 | 0 | qr = build_norm(qr, model.layers[il].attn_q_a_norm, nullptr, LLM_NORM_RMS, il); |
265 | 0 | cb(qr, "qr", il); |
266 | |
|
267 | 0 | ggml_tensor * top_k = nullptr; |
268 | | |
269 | | // lightning indexer |
270 | 0 | if (hparams.is_indexer_full(il)) { |
271 | | // "full" layer |
272 | 0 | ggml_tensor * indexer_q = ggml_mul_mat(ctx0, model.layers[il].indexer_attn_q_b, qr); |
273 | 0 | cb(indexer_q, "indexer_q", il); |
274 | | |
275 | | // {n_embd_indexer_head, n_indexer_head, n_tokens} |
276 | 0 | indexer_q = ggml_reshape_3d(ctx0, indexer_q, n_embd_indexer_head, n_indexer_head, n_tokens); |
277 | 0 | indexer_q = ggml_rope_ext(ctx0, indexer_q, inp_pos, nullptr, n_rot, |
278 | 0 | LLAMA_ROPE_TYPE_NORM, n_ctx_orig, freq_base, freq_scale, |
279 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
280 | 0 | cb(indexer_q, "indexer_q", il); |
281 | |
|
282 | 0 | ggml_tensor * indexer_k = ggml_mul_mat(ctx0, model.layers[il].indexer_attn_k, cur); |
283 | 0 | cb(indexer_k, "indexer_k", il); |
284 | |
|
285 | 0 | indexer_k = build_norm(indexer_k, model.layers[il].indexer_k_norm, model.layers[il].indexer_k_norm_b, LLM_NORM, il); |
286 | 0 | cb(indexer_k, "indexer_k", il); |
287 | | |
288 | | // {n_embd_indexer_head, 1, n_tokens} |
289 | 0 | indexer_k = ggml_reshape_3d(ctx0, indexer_k, n_embd_indexer_head, 1, n_tokens); |
290 | 0 | indexer_k = ggml_rope_ext(ctx0, indexer_k, inp_pos, nullptr, n_rot, |
291 | 0 | LLAMA_ROPE_TYPE_NORM, n_ctx_orig, freq_base, freq_scale, |
292 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
293 | 0 | cb(indexer_k, "indexer_k", il); |
294 | | |
295 | | // perform Hadamard transform on indexer q and k |
296 | 0 | indexer_q = ggml_mul_mat(ctx0, inp_attn_dsa->self_k_rot_lid, indexer_q); |
297 | 0 | cb(indexer_q, "indexer_q", il); |
298 | 0 | indexer_k = ggml_mul_mat(ctx0, inp_attn_dsa->self_k_rot_lid, indexer_k); |
299 | 0 | cb(indexer_k, "indexer_k", il); |
300 | | |
301 | | // store indexer keys to KV cache |
302 | 0 | const auto * mctx_lid = inp_attn_dsa->mctx->get_lid(); |
303 | 0 | const auto & k_idxs_lid = inp_attn_dsa->get_k_idxs_lid(); |
304 | 0 | ggml_build_forward_expand(gf, mctx_lid->cpy_k(ctx0, indexer_k, k_idxs_lid, il)); |
305 | | |
306 | | // prepare indexer weights |
307 | 0 | ggml_tensor * indexer_weights = ggml_mul_mat(ctx0, model.layers[il].indexer_proj, cur); |
308 | 0 | cb(indexer_weights, "indexer_weights", il); |
309 | | |
310 | | // get cached indexer keys |
311 | 0 | indexer_k = mctx_lid->get_k(ctx0, il); |
312 | | |
313 | | // split the batch into streams if needed |
314 | 0 | const auto n_stream = indexer_k->ne[3]; |
315 | 0 | indexer_q = ggml_view_4d(ctx0, indexer_q, indexer_q->ne[0], indexer_q->ne[1], indexer_q->ne[2]/n_stream, n_stream, indexer_q->nb[1], indexer_q->nb[2], indexer_q->nb[3]/n_stream, 0); |
316 | 0 | indexer_weights = ggml_view_4d(ctx0, indexer_weights, indexer_weights->ne[0], indexer_weights->ne[1]/n_stream, indexer_weights->ne[2], n_stream, indexer_weights->nb[1], indexer_weights->nb[2]/n_stream, indexer_weights->nb[3]/n_stream, 0); |
317 | | |
318 | | // pre-scale weights to avoid scaling operations on huge indexer_score tensor |
319 | 0 | indexer_weights = ggml_scale(ctx0, indexer_weights, 1.0f / sqrtf(float(n_embd_indexer_head * n_indexer_head))); |
320 | 0 | cb(indexer_weights, "indexer_weights", il); |
321 | |
|
322 | 0 | ggml_tensor * indexer_score = nullptr; |
323 | 0 | if (cparams.fused_lid) { |
324 | 0 | indexer_score = ggml_lightning_indexer(ctx0, indexer_q, indexer_k, indexer_weights, inp_attn_dsa->get_kq_mask_lid()); |
325 | 0 | cb(indexer_score, "indexer_score", il); |
326 | 0 | res->add_fused_node({LLM_FUSED_OP_LIGHTNING_INDEXER, indexer_score, il}); |
327 | 0 | } else { |
328 | | // calculate indexer kq |
329 | 0 | indexer_q = ggml_permute(ctx0, indexer_q, 0, 2, 1, 3); |
330 | 0 | cb(indexer_q, "indexer_q", il); |
331 | 0 | indexer_k = ggml_permute(ctx0, indexer_k, 0, 2, 1, 3); |
332 | 0 | cb(indexer_k, "indexer_k", il); |
333 | |
|
334 | 0 | ggml_tensor * indexer_kq = ggml_mul_mat(ctx0, indexer_k, indexer_q); |
335 | 0 | cb(indexer_kq, "indexer_kq", il); |
336 | | |
337 | | // ReLU requires contiguous tensors |
338 | 0 | indexer_kq = ggml_cont(ctx0, ggml_permute(ctx0, indexer_kq, 2, 1, 0, 3)); |
339 | 0 | cb(indexer_kq, "indexer_kq", il); |
340 | | |
341 | | // apply ReLU |
342 | 0 | indexer_score = ggml_relu(ctx0, indexer_kq); |
343 | 0 | cb(indexer_score, "indexer_score", il); |
344 | | |
345 | | // multiply scores by indexer weights |
346 | 0 | indexer_score = ggml_mul(ctx0, indexer_score, indexer_weights); |
347 | 0 | cb(indexer_score, "indexer_score", il); |
348 | | |
349 | | // sum by q n_indexer_head dimension |
350 | 0 | indexer_score = ggml_sum_rows(ctx0, indexer_score); |
351 | 0 | cb(indexer_score, "indexer_score", il); |
352 | | |
353 | | // permute result to match KQ mask |
354 | 0 | indexer_score = ggml_cont(ctx0, ggml_permute(ctx0, indexer_score, 2, 1, 0, 3)); |
355 | 0 | cb(indexer_score, "indexer_score", il); |
356 | | |
357 | | // mask indexer scores |
358 | 0 | ggml_tensor * indexer_kq_mask = inp_attn_dsa->get_kq_mask_lid(); |
359 | 0 | indexer_score = ggml_add(ctx0, indexer_score, indexer_kq_mask); |
360 | 0 | cb(indexer_score, "indexer_score", il); |
361 | 0 | } |
362 | | |
363 | | // get indices of top k indexer scores |
364 | 0 | uint32_t n_top_k = indexer_score->ne[0] < n_indexer_top_k ? indexer_score->ne[0] : n_indexer_top_k; |
365 | 0 | top_k = ggml_cont(ctx0, ggml_top_k(ctx0, indexer_score, n_top_k)); |
366 | 0 | prev_top_k = top_k; |
367 | 0 | cb(top_k, "top_k", il); |
368 | 0 | } else { |
369 | | // "shared" indexer layer - reuse top-k from a previous full layer |
370 | 0 | GGML_ASSERT(prev_top_k != nullptr && "shared indexer layer must follow a previous full indexer layer"); |
371 | 0 | top_k = prev_top_k; |
372 | 0 | cb(top_k, "top_k", il); |
373 | 0 | } |
374 | |
|
375 | 0 | ggml_tensor * q = ggml_mul_mat(ctx0, model.layers[il].wq_b, qr); |
376 | 0 | cb(q, "q", il); |
377 | | |
378 | | // split into {n_embd_head_qk_nope, n_head, n_tokens} |
379 | 0 | ggml_tensor * q_nope = |
380 | 0 | ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens, ggml_row_size(q->type, n_embd_head_k), |
381 | 0 | ggml_row_size(q->type, n_embd_head_k) * n_head, 0); |
382 | 0 | cb(q_nope, "q_nope", il); |
383 | | |
384 | | // and {n_embd_head_qk_rope, n_head, n_tokens} |
385 | 0 | ggml_tensor * q_pe = ggml_view_3d( |
386 | 0 | ctx0, q, n_embd_head_qk_rope, n_head, n_tokens, ggml_row_size(q->type, n_embd_head_k), |
387 | 0 | ggml_row_size(q->type, n_embd_head_k) * n_head, ggml_row_size(q->type, n_embd_head_qk_nope)); |
388 | 0 | cb(q_pe, "q_pe", il); |
389 | |
|
390 | 0 | ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur); |
391 | 0 | cb(kv_cmpr_pe, "kv_cmpr_pe", il); |
392 | | |
393 | | // split into {kv_lora_rank, n_tokens} |
394 | 0 | ggml_tensor * kv_cmpr = |
395 | 0 | ggml_view_2d(ctx0, kv_cmpr_pe, kv_lora_rank, n_tokens, |
396 | 0 | ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope), 0); |
397 | 0 | cb(kv_cmpr, "kv_cmpr", il); |
398 | | |
399 | | // and {n_embd_head_qk_rope, 1, n_tokens} |
400 | 0 | ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_cmpr_pe, n_embd_head_qk_rope, 1, n_tokens, |
401 | 0 | ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope), |
402 | 0 | ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope), |
403 | 0 | ggml_row_size(kv_cmpr_pe->type, kv_lora_rank)); |
404 | 0 | cb(k_pe, "k_pe", il); |
405 | |
|
406 | 0 | q_pe = ggml_rope_ext(ctx0, q_pe, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
407 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
408 | 0 | cb(q_pe, "q_pe", il); |
409 | |
|
410 | 0 | k_pe = ggml_rope_ext(ctx0, k_pe, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
411 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
412 | 0 | cb(k_pe, "k_pe", il); |
413 | |
|
414 | 0 | kv_cmpr = build_norm(kv_cmpr, model.layers[il].attn_kv_a_norm, nullptr, LLM_NORM_RMS, il); |
415 | 0 | cb(kv_cmpr, "kv_cmpr", il); |
416 | | |
417 | | // MLA attention |
418 | 0 | { |
419 | | // {n_embd_head_qk_nope, n_tokens, n_head} |
420 | 0 | q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3); |
421 | 0 | cb(q_nope, "q_nope_perm", il); |
422 | | |
423 | | // {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head} |
424 | 0 | ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, model.layers[il].wk_b, q_nope); |
425 | 0 | cb(q_nope_absorbed, "q_nope_absorbed", il); |
426 | | |
427 | | // {kv_lora_rank, n_head, n_tokens} |
428 | 0 | q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3); |
429 | 0 | cb(q_nope_absorbed, "q_nope_absorbed_perm", il); |
430 | | |
431 | | // {n_embd_head_qk_rope + kv_lora_rank, n_head, n_tokens} |
432 | | // note: rope must go first for in-place context shifting in build_rope_shift() |
433 | 0 | ggml_tensor * Qcur = ggml_concat(ctx0, q_nope_absorbed, q_pe, 0); |
434 | 0 | cb(Qcur, "Qcur", il); |
435 | |
|
436 | 0 | kv_cmpr = ggml_reshape_3d(ctx0, kv_cmpr, kv_lora_rank, 1, n_tokens); |
437 | 0 | cb(kv_cmpr, "kv_cmpr_reshape", il); |
438 | | |
439 | | // {n_embd_head_qk_rope + kv_lora_rank, 1, n_tokens} |
440 | 0 | ggml_tensor * Kcur = ggml_concat(ctx0, kv_cmpr, k_pe, 0); |
441 | 0 | cb(Kcur, "Kcur", il); |
442 | | |
443 | | // {kv_lora_rank, 1, n_tokens} |
444 | 0 | ggml_tensor * Vcur = kv_cmpr; |
445 | 0 | cb(Vcur, "Vcur", il); |
446 | | |
447 | | // note: MLA with the absorption optimization converts into MQA (ie: GQA with 1 group) |
448 | 0 | cur = build_attn(inp_attn_dsa, |
449 | 0 | model.layers[il].wo, NULL, model.layers[il].wo_s, |
450 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, model.layers[il].wv_b, top_k, kq_scale, il); |
451 | 0 | } |
452 | 0 | } |
453 | | // when unmasked nextn embeddings are requested, t_h_nextn must keep all rows, |
454 | | // so the early output masking has to be skipped (it is applied after the final norm instead) |
455 | 0 | if (il == n_layer - 1 && inp_out_ids && (!cparams.embeddings_nextn || cparams.embeddings_nextn_masked)) { |
456 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
457 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
458 | 0 | } |
459 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
460 | 0 | cb(ffn_inp, "ffn_inp", il); |
461 | |
|
462 | 0 | cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); |
463 | 0 | cb(cur, "ffn_norm", il); |
464 | |
|
465 | 0 | if ((uint32_t) il < hparams.n_layer_dense_lead) { |
466 | 0 | cur = build_ffn(cur, |
467 | 0 | model.layers[il].ffn_up, NULL, model.layers[il].ffn_up_s, |
468 | 0 | model.layers[il].ffn_gate, NULL, model.layers[il].ffn_gate_s, |
469 | 0 | model.layers[il].ffn_down, NULL, model.layers[il].ffn_down_s, |
470 | 0 | NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); |
471 | 0 | cb(cur, "ffn_out", il); |
472 | 0 | } else { |
473 | | // MoE branch |
474 | 0 | ggml_tensor * moe_out = build_moe_ffn(cur, |
475 | 0 | model.layers[il].ffn_gate_inp, |
476 | 0 | model.layers[il].ffn_up_exps, |
477 | 0 | model.layers[il].ffn_gate_exps, |
478 | 0 | model.layers[il].ffn_down_exps, |
479 | 0 | model.layers[il].ffn_exp_probs_b, |
480 | 0 | n_expert, n_expert_used, |
481 | 0 | LLM_FFN_SILU, hparams.expert_weights_norm, |
482 | 0 | hparams.expert_weights_scale, |
483 | 0 | (llama_expert_gating_func_type) hparams.expert_gating_func, |
484 | 0 | il, |
485 | 0 | nullptr, |
486 | 0 | model.layers[il].ffn_gate_up_exps, |
487 | 0 | model.layers[il].ffn_up_exps_s, |
488 | 0 | model.layers[il].ffn_gate_exps_s, |
489 | 0 | model.layers[il].ffn_down_exps_s); |
490 | 0 | cb(moe_out, "ffn_moe_out", il); |
491 | | |
492 | | // FFN shared expert |
493 | 0 | { |
494 | 0 | ggml_tensor * ffn_shexp = |
495 | 0 | build_ffn(cur, |
496 | 0 | model.layers[il].ffn_up_shexp, NULL, model.layers[il].ffn_up_shexp_s, |
497 | 0 | model.layers[il].ffn_gate_shexp, NULL, model.layers[il].ffn_gate_shexp_s, |
498 | 0 | model.layers[il].ffn_down_shexp, NULL, model.layers[il].ffn_down_shexp_s, |
499 | 0 | NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); |
500 | 0 | cb(ffn_shexp, "ffn_shexp", il); |
501 | |
|
502 | 0 | cur = ggml_add(ctx0, moe_out, ffn_shexp); |
503 | 0 | cb(cur, "ffn_out", il); |
504 | 0 | } |
505 | 0 | } |
506 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
507 | |
|
508 | 0 | cur = build_cvec(cur, il); |
509 | 0 | cb(cur, "l_out", il); |
510 | | |
511 | | // input for next layer |
512 | 0 | inpL = cur; |
513 | 0 | } |
514 | 0 | cur = inpL; |
515 | |
|
516 | 0 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
517 | | |
518 | | // post-norm hidden state feeds the NextN/MTP draft head |
519 | 0 | cb(cur, "h_nextn", -1); |
520 | 0 | res->t_h_nextn = cur; |
521 | |
|
522 | 0 | if (cparams.embeddings_nextn && !cparams.embeddings_nextn_masked && inp_out_ids) { |
523 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
524 | 0 | } |
525 | |
|
526 | 0 | cb(cur, "result_norm", -1); |
527 | 0 | res->t_embd = cur; |
528 | | |
529 | | // lm_head |
530 | 0 | cur = ggml_mul_mat(ctx0, model.output, cur); |
531 | |
|
532 | 0 | cb(cur, "result_output", -1); |
533 | 0 | res->t_logits = cur; |
534 | |
|
535 | 0 | ggml_build_forward_expand(gf, cur); |
536 | 0 | } |
537 | | |
538 | | // LLM_GRAPH_TYPE_DECODER_MTP draft head for GLM-5.2 (GLM_DSA). |
539 | | // Semantics mirror the deepseek-family NextN/MTP layer: |
540 | | // enorm(embed) + hnorm(prev_hidden) -> concat(e, h) -> eh_proj -> |
541 | | // full glm_dsa decoder block (dense MLA attention + sigmoid-gated MoE FFN |
542 | | // with shared expert, exactly as the trunk deepseek2 graph builds it) -> |
543 | | // shared_head_norm (fallback output_norm) -> shared LM head. |
544 | | // The DSA indexer is not used at runtime (same as the trunk graph). |
545 | | llama_model_glm_dsa::graph_mtp::graph_mtp(const llama_model & model, const llm_graph_params & params) |
546 | 0 | : llm_graph_context(params) { |
547 | 0 | GGML_ASSERT(hparams.n_layer_nextn > 0 && "GLM_DSA MTP requires n_layer_nextn > 0"); |
548 | 0 | GGML_ASSERT(hparams.n_layer_nextn == 1 && "GLM_DSA MTP currently only supports a single MTP block"); |
549 | 0 | GGML_ASSERT(hparams.is_mla() && "GLM_DSA MTP requires MLA"); |
550 | |
|
551 | 0 | const int il = hparams.n_layer() + cparams.nextn_layer_offset; |
552 | 0 | GGML_ASSERT(cparams.nextn_layer_offset >= 0 && |
553 | 0 | cparams.nextn_layer_offset < (int) hparams.n_layer_nextn && |
554 | 0 | "nextn_layer_offset out of range [0, n_layer_nextn)"); |
555 | 0 | const auto & layer = model.layers[il]; |
556 | |
|
557 | 0 | GGML_ASSERT(layer.nextn.eh_proj && "MTP block missing nextn.eh_proj"); |
558 | 0 | GGML_ASSERT(layer.nextn.enorm && "MTP block missing nextn.enorm"); |
559 | 0 | GGML_ASSERT(layer.nextn.hnorm && "MTP block missing nextn.hnorm"); |
560 | 0 | GGML_ASSERT(layer.ffn_gate_inp && "MTP block missing ffn_gate_inp"); |
561 | | |
562 | | // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA |
563 | 0 | const int64_t n_embd_head_k = hparams.n_embd_head_k_mla(); |
564 | |
|
565 | 0 | const int64_t n_embd_head_qk_rope = hparams.n_rot(); |
566 | 0 | const int64_t n_embd_head_qk_nope = n_embd_head_k - n_embd_head_qk_rope; |
567 | |
|
568 | 0 | const uint32_t kv_lora_rank = hparams.n_lora_kv; |
569 | | |
570 | | // We have to pre-scale kq_scale and attn_factor to make the YaRN RoPE work correctly. |
571 | | // See the deepseek2 trunk graph for the detailed explanation - this must match it EXACTLY. |
572 | 0 | GGML_ASSERT(ext_factor >= 0.0f); |
573 | 0 | const float attn_factor_org = attn_factor * (1.0f + 0.1f * logf(1.0f / freq_scale)); |
574 | |
|
575 | 0 | const float mscale = attn_factor_org * (1.0f + 0.1f * hparams.rope_yarn_log_mul * logf(1.0f / freq_scale)); |
576 | 0 | const float kq_scale = 1.0f * mscale * mscale / sqrtf(float(n_embd_head_k)); |
577 | | |
578 | | // TODO: extract in a common llm_graph_context::build_inp_embd_h() |
579 | 0 | auto inp = std::make_unique<llm_graph_input_embd_h>(hparams.n_embd); |
580 | |
|
581 | 0 | inp->tokens = ggml_new_tensor_1d(ctx0, GGML_TYPE_I32, n_tokens); |
582 | 0 | ggml_set_input(inp->tokens); |
583 | |
|
584 | 0 | inp->embd = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, hparams.n_embd_inp(), n_tokens); |
585 | 0 | ggml_set_input(inp->embd); |
586 | |
|
587 | 0 | ggml_tensor * tok_embd; |
588 | 0 | if (ubatch.token) { |
589 | 0 | ggml_tensor * tok_embd_w = layer.nextn.embed_tokens ? layer.nextn.embed_tokens : model.tok_embd; |
590 | |
|
591 | 0 | tok_embd = ggml_get_rows(ctx0, tok_embd_w, inp->tokens); |
592 | 0 | } else { |
593 | 0 | tok_embd = inp->embd; |
594 | 0 | } |
595 | 0 | cb(tok_embd, "mtp_tok_embd", il); |
596 | |
|
597 | 0 | inp->h = ggml_new_tensor_2d(ctx0, GGML_TYPE_F32, hparams.n_embd, n_tokens); |
598 | 0 | ggml_set_input(inp->h); |
599 | 0 | ggml_set_name(inp->h, "mtp_h_input"); |
600 | |
|
601 | 0 | ggml_tensor * h_embd = inp->h; |
602 | |
|
603 | 0 | res->add_input(std::move(inp)); |
604 | |
|
605 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
606 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
607 | | |
608 | | // MLA with the absorption optimization uses a K-only cache (V is a view of K) |
609 | 0 | auto * inp_attn = build_attn_inp_k(); |
610 | |
|
611 | 0 | ggml_tensor * h_norm = build_norm(h_embd, layer.nextn.hnorm, nullptr, LLM_NORM_RMS, il); |
612 | 0 | cb(h_norm, "mtp_hnorm", il); |
613 | |
|
614 | 0 | ggml_tensor * e_norm = build_norm(tok_embd, layer.nextn.enorm, nullptr, LLM_NORM_RMS, il); |
615 | 0 | cb(e_norm, "mtp_enorm", il); |
616 | |
|
617 | 0 | ggml_tensor * concat = ggml_concat(ctx0, e_norm, h_norm, /*dim=*/ 0); |
618 | 0 | cb(concat, "mtp_concat", il); |
619 | |
|
620 | 0 | ggml_tensor * cur = build_lora_mm(layer.nextn.eh_proj, concat, layer.nextn.eh_proj_s); |
621 | 0 | cb(cur, "mtp_eh_proj", il); |
622 | |
|
623 | 0 | ggml_tensor * inpSA = cur; |
624 | |
|
625 | 0 | cur = build_norm(cur, layer.attn_norm, nullptr, LLM_NORM_RMS, il); |
626 | 0 | cb(cur, "mtp_attn_norm", il); |
627 | | |
628 | | // self-attention: dense MLA, same construction as the deepseek2 trunk graph |
629 | 0 | { |
630 | 0 | ggml_tensor * q = ggml_mul_mat(ctx0, layer.wq_a, cur); |
631 | 0 | cb(q, "mtp_q", il); |
632 | |
|
633 | 0 | q = build_norm(q, layer.attn_q_a_norm, nullptr, LLM_NORM_RMS, il); |
634 | 0 | cb(q, "mtp_q", il); |
635 | |
|
636 | 0 | q = ggml_mul_mat(ctx0, layer.wq_b, q); |
637 | 0 | cb(q, "mtp_q", il); |
638 | | |
639 | | // split into {n_embd_head_qk_nope, n_head, n_tokens} |
640 | 0 | ggml_tensor * q_nope = |
641 | 0 | ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens, ggml_row_size(q->type, n_embd_head_k), |
642 | 0 | ggml_row_size(q->type, n_embd_head_k) * n_head, 0); |
643 | 0 | cb(q_nope, "mtp_q_nope", il); |
644 | | |
645 | | // and {n_embd_head_qk_rope, n_head, n_tokens} |
646 | 0 | ggml_tensor * q_pe = ggml_view_3d( |
647 | 0 | ctx0, q, n_embd_head_qk_rope, n_head, n_tokens, ggml_row_size(q->type, n_embd_head_k), |
648 | 0 | ggml_row_size(q->type, n_embd_head_k) * n_head, ggml_row_size(q->type, n_embd_head_qk_nope)); |
649 | 0 | cb(q_pe, "mtp_q_pe", il); |
650 | |
|
651 | 0 | ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, layer.wkv_a_mqa, cur); |
652 | 0 | cb(kv_cmpr_pe, "mtp_kv_cmpr_pe", il); |
653 | | |
654 | | // split into {kv_lora_rank, n_tokens} |
655 | 0 | ggml_tensor * kv_cmpr = |
656 | 0 | ggml_view_2d(ctx0, kv_cmpr_pe, kv_lora_rank, n_tokens, |
657 | 0 | ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope), 0); |
658 | 0 | cb(kv_cmpr, "mtp_kv_cmpr", il); |
659 | | |
660 | | // and {n_embd_head_qk_rope, 1, n_tokens} |
661 | 0 | ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_cmpr_pe, n_embd_head_qk_rope, 1, n_tokens, |
662 | 0 | ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope), |
663 | 0 | ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope), |
664 | 0 | ggml_row_size(kv_cmpr_pe->type, kv_lora_rank)); |
665 | 0 | cb(k_pe, "mtp_k_pe", il); |
666 | |
|
667 | 0 | q_pe = ggml_rope_ext(ctx0, q_pe, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
668 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
669 | 0 | cb(q_pe, "mtp_q_pe", il); |
670 | |
|
671 | 0 | k_pe = ggml_rope_ext(ctx0, k_pe, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
672 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
673 | 0 | cb(k_pe, "mtp_k_pe", il); |
674 | |
|
675 | 0 | kv_cmpr = build_norm(kv_cmpr, layer.attn_kv_a_norm, nullptr, LLM_NORM_RMS, il); |
676 | 0 | cb(kv_cmpr, "mtp_kv_cmpr", il); |
677 | | |
678 | | // {n_embd_head_qk_nope, n_tokens, n_head} |
679 | 0 | q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3); |
680 | 0 | cb(q_nope, "mtp_q_nope_perm", il); |
681 | | |
682 | | // {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head} |
683 | 0 | ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, layer.wk_b, q_nope); |
684 | 0 | cb(q_nope_absorbed, "mtp_q_nope_absorbed", il); |
685 | | |
686 | | // {kv_lora_rank, n_head, n_tokens} |
687 | 0 | q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3); |
688 | 0 | cb(q_nope_absorbed, "mtp_q_nope_absorbed_perm", il); |
689 | | |
690 | | // {n_embd_head_qk_rope + kv_lora_rank, n_head, n_tokens} |
691 | | // note: rope must go first for in-place context shifting in build_rope_shift() |
692 | 0 | ggml_tensor * Qcur = ggml_concat(ctx0, q_nope_absorbed, q_pe, 0); |
693 | 0 | cb(Qcur, "mtp_Qcur", il); |
694 | |
|
695 | 0 | kv_cmpr = ggml_reshape_3d(ctx0, kv_cmpr, kv_lora_rank, 1, n_tokens); |
696 | 0 | cb(kv_cmpr, "mtp_kv_cmpr_reshape", il); |
697 | | |
698 | | // {n_embd_head_qk_rope + kv_lora_rank, 1, n_tokens} |
699 | 0 | ggml_tensor * Kcur = ggml_concat(ctx0, kv_cmpr, k_pe, 0); |
700 | 0 | cb(Kcur, "mtp_Kcur", il); |
701 | | |
702 | | // {kv_lora_rank, 1, n_tokens} |
703 | 0 | ggml_tensor * Vcur = kv_cmpr; |
704 | 0 | cb(Vcur, "mtp_Vcur", il); |
705 | | |
706 | | // note: MLA with the absorption optimization converts into MQA (ie: GQA with 1 group) |
707 | 0 | cur = build_attn(inp_attn, |
708 | 0 | layer.wo, NULL, layer.wo_s, |
709 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, layer.wv_b, kq_scale, il); |
710 | 0 | cb(cur, "mtp_attn_out", il); |
711 | 0 | } |
712 | |
|
713 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
714 | 0 | cb(ffn_inp, "mtp_ffn_inp", il); |
715 | |
|
716 | 0 | cur = build_norm(ffn_inp, layer.ffn_norm, NULL, LLM_NORM_RMS, il); |
717 | 0 | cb(cur, "mtp_ffn_norm", il); |
718 | | |
719 | | // MoE FFN with shared expert - same construction as the deepseek2 trunk graph |
720 | 0 | ggml_tensor * moe_out = build_moe_ffn(cur, |
721 | 0 | layer.ffn_gate_inp, |
722 | 0 | layer.ffn_up_exps, |
723 | 0 | layer.ffn_gate_exps, |
724 | 0 | layer.ffn_down_exps, |
725 | 0 | layer.ffn_exp_probs_b, |
726 | 0 | n_expert, n_expert_used, |
727 | 0 | LLM_FFN_SILU, hparams.expert_weights_norm, |
728 | 0 | hparams.expert_weights_scale, |
729 | 0 | (llama_expert_gating_func_type) hparams.expert_gating_func, |
730 | 0 | il, |
731 | 0 | nullptr, |
732 | 0 | layer.ffn_gate_up_exps, |
733 | 0 | layer.ffn_up_exps_s, |
734 | 0 | layer.ffn_gate_exps_s, |
735 | 0 | layer.ffn_down_exps_s); |
736 | 0 | cb(moe_out, "mtp_ffn_moe_out", il); |
737 | | |
738 | | // FFN shared expert |
739 | 0 | ggml_tensor * ffn_shexp = |
740 | 0 | build_ffn(cur, |
741 | 0 | layer.ffn_up_shexp, NULL, layer.ffn_up_shexp_s, |
742 | 0 | layer.ffn_gate_shexp, NULL, layer.ffn_gate_shexp_s, |
743 | 0 | layer.ffn_down_shexp, NULL, layer.ffn_down_shexp_s, |
744 | 0 | NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); |
745 | 0 | cb(ffn_shexp, "mtp_ffn_shexp", il); |
746 | |
|
747 | 0 | cur = ggml_add(ctx0, moe_out, ffn_shexp); |
748 | 0 | cb(cur, "mtp_ffn_out", il); |
749 | |
|
750 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
751 | 0 | cb(cur, "mtp_post_ffn", il); |
752 | | |
753 | | // shared_head_norm applied after the decoder block, before the shared LM head. |
754 | | // The post-norm hidden state seeds the next MTP step. |
755 | 0 | ggml_tensor * head_norm_w = layer.nextn.shared_head_norm |
756 | 0 | ? layer.nextn.shared_head_norm |
757 | 0 | : model.output_norm; |
758 | 0 | GGML_ASSERT(head_norm_w && "GLM_DSA MTP: missing both nextn.shared_head_norm and output_norm"); |
759 | 0 | cur = build_norm(cur, head_norm_w, nullptr, LLM_NORM_RMS, -1); |
760 | |
|
761 | 0 | cb(cur, "h_nextn", -1); |
762 | 0 | res->t_h_nextn = cur; |
763 | |
|
764 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
765 | 0 | cb(cur, "mtp_shared_head_norm", -1); |
766 | |
|
767 | 0 | ggml_tensor * head_w = layer.nextn.shared_head_head ? layer.nextn.shared_head_head : model.output; |
768 | 0 | ggml_tensor * head_s = layer.nextn.shared_head_head ? layer.nextn.shared_head_head_s : model.output_s; |
769 | 0 | GGML_ASSERT(head_w && "GLM_DSA MTP: missing LM head (nextn.shared_head_head or model.output)"); |
770 | 0 | cur = build_lora_mm(head_w, cur, head_s); |
771 | 0 | cb(cur, "result_output", -1); |
772 | |
|
773 | 0 | res->t_logits = cur; |
774 | 0 | ggml_build_forward_expand(gf, cur); |
775 | 0 | } |