/src/llama.cpp/src/models/glm4-moe.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_glm4_moe::load_arch_hparams(llama_model_loader & ml) { |
4 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
5 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
6 | 0 | ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, false); |
7 | | |
8 | | // MoE parameters |
9 | 0 | ml.get_key(LLM_KV_EXPERT_COUNT, hparams.n_expert); |
10 | 0 | ml.get_key(LLM_KV_EXPERT_USED_COUNT, hparams.n_expert_used); |
11 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared); |
12 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
13 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
14 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false); |
15 | | |
16 | | // Expert gating function (GLM-4.5 uses sigmoid) |
17 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false); |
18 | 0 | if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) { |
19 | 0 | hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID; |
20 | 0 | } |
21 | | |
22 | | // NextN/MTP parameters |
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_impl"); |
25 | |
|
26 | 0 | switch (hparams.n_layer()) { |
27 | 0 | case 46: type = LLM_TYPE_106B_A12B; break; // GLM-4.5-Air |
28 | 0 | case 48: type = LLM_TYPE_102B_A12B; break; // Solar Open |
29 | 0 | case 92: type = LLM_TYPE_355B_A32B; break; // GLM-4.5 |
30 | 0 | default: type = LLM_TYPE_UNKNOWN; |
31 | 0 | } |
32 | 0 | } |
33 | | |
34 | 0 | void llama_model_glm4_moe::load_arch_tensors(llama_model_loader &) { |
35 | 0 | LLAMA_LOAD_LOCALS; |
36 | 0 | const int64_t n_expert_shared = hparams.n_expert_shared; |
37 | | |
38 | |
|
39 | 0 | GGML_ASSERT(hparams.n_expert > 0 && "n_expert must be > 0 for GLM4_MOE MoE layers"); |
40 | 0 | GGML_ASSERT(hparams.n_expert_used > 0 && "n_expert_used must be > 0 for GLM4_MOE MoE layers"); |
41 | |
|
42 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0); |
43 | | |
44 | | // output |
45 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0); |
46 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED); |
47 | | // if output is NULL, init from the input tok embed |
48 | 0 | if (output == NULL) { |
49 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED); |
50 | 0 | } |
51 | | |
52 | | // Load ALL tensors including NextN layer to satisfy total tensor count |
53 | | // but only PROCESS up to last layer (skipping final NextN layer) in forward pass |
54 | 0 | for (int i = 0; i < n_layer_all; ++i) { |
55 | 0 | int flags = 0; |
56 | 0 | if (i >= n_layer) { |
57 | | // skip all tensors in the NextN layers |
58 | 0 | flags |= TENSOR_SKIP; |
59 | 0 | } |
60 | |
|
61 | 0 | auto & layer = layers[i]; |
62 | |
|
63 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, flags); |
64 | | |
65 | | // GLM-style attention with bias terms |
66 | 0 | create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, flags); |
67 | |
|
68 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, flags); |
69 | | |
70 | | // K/Q norm tensors (optional for GLM-4.5 355B variant) |
71 | 0 | layer.attn_q_norm = create_tensor( |
72 | 0 | tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED | flags); |
73 | 0 | layer.attn_k_norm = create_tensor( |
74 | 0 | tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED | flags); |
75 | |
|
76 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), { n_embd }, flags); |
77 | | |
78 | | // Check if this layer uses MoE or dense FFN based on n_layer_dense_lead |
79 | | // GLM 4.5 uses hybrid architecture: layer 0 is dense, layers 1+ are MoE |
80 | 0 | const bool use_moe = (static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead); |
81 | |
|
82 | 0 | if (use_moe) { |
83 | | // MoE layers |
84 | 0 | layer.ffn_gate_inp = |
85 | 0 | create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, flags); |
86 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), { n_expert }, flags); |
87 | | |
88 | | // MoE branch |
89 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used; |
90 | |
|
91 | 0 | layer.ffn_gate_exps = create_tensor( |
92 | 0 | tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, flags); |
93 | 0 | layer.ffn_down_exps = create_tensor( |
94 | 0 | tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, flags); |
95 | 0 | layer.ffn_up_exps = create_tensor( |
96 | 0 | tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, flags); |
97 | | |
98 | | // Shared expert |
99 | 0 | if (n_expert_shared > 0) { |
100 | 0 | const int64_t n_ff_shexp = n_ff_exp * n_expert_shared; |
101 | 0 | layer.ffn_gate_shexp = create_tensor( |
102 | 0 | tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags); |
103 | 0 | layer.ffn_down_shexp = create_tensor( |
104 | 0 | tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_shexp, n_embd }, flags); |
105 | 0 | layer.ffn_up_shexp = create_tensor( |
106 | 0 | tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags); |
107 | 0 | } |
108 | 0 | } else { |
109 | | // Dense layers (first k layers) - GLM uses separate gate/up projections |
110 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, flags); |
111 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, flags); |
112 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, flags); |
113 | 0 | } |
114 | | |
115 | | // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers |
116 | 0 | if (i >= n_layer) { |
117 | 0 | layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags); |
118 | 0 | layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags); |
119 | 0 | layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags); |
120 | | |
121 | | // Optional tensors |
122 | 0 | layer.nextn.embed_tokens = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED); |
123 | 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); |
124 | 0 | layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, flags | TENSOR_NOT_REQUIRED); |
125 | 0 | } |
126 | 0 | } |
127 | 0 | } |
128 | | |
129 | 0 | std::unique_ptr<llm_graph_context> llama_model_glm4_moe::build_arch_graph(const llm_graph_params & params) const { |
130 | 0 | return std::make_unique<graph>(*this, params); |
131 | 0 | } |
132 | | |
133 | 0 | llama_model_glm4_moe::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
134 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
135 | |
|
136 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
137 | |
|
138 | 0 | int sections[4]; |
139 | 0 | std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections); |
140 | |
|
141 | 0 | ggml_tensor * cur; |
142 | 0 | ggml_tensor * inpL; |
143 | |
|
144 | 0 | inpL = build_inp_embd(model.tok_embd); |
145 | |
|
146 | 0 | bool use_mrope = hparams.use_mrope(); |
147 | 0 | if (ubatch.embd && !use_mrope) { |
148 | | // unfortunately, we need to forcefully stop here, to avoid users complaining about wrong results |
149 | 0 | GGML_ABORT("This GGUF does not support multimodal. Please reconvert it."); |
150 | 0 | } |
151 | | |
152 | | // inp_pos - contains the positions |
153 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
154 | |
|
155 | 0 | auto * inp_attn = build_attn_inp_kv(); |
156 | |
|
157 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
158 | | |
159 | | // Only process up to last layer (skip final NextN layer) |
160 | | // Final layer tensors are loaded but not processed in forward pass |
161 | 0 | for (int il = 0; il < n_layer; ++il) { |
162 | 0 | ggml_tensor * inpSA = inpL; |
163 | | |
164 | | // Pre-attention norm |
165 | 0 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
166 | 0 | cb(cur, "attn_norm", il); |
167 | | |
168 | | // self-attention |
169 | 0 | { |
170 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
171 | 0 | n_embd_head, n_head, n_head_kv, il); |
172 | | |
173 | | // Apply Q/K norm if available (GLM-4.5 355B variant) |
174 | 0 | if (model.layers[il].attn_q_norm) { |
175 | 0 | Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il); |
176 | 0 | cb(Qcur, "Qcur_normed", il); |
177 | 0 | } |
178 | 0 | if (model.layers[il].attn_k_norm) { |
179 | 0 | Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il); |
180 | 0 | cb(Kcur, "Kcur_normed", il); |
181 | 0 | } |
182 | |
|
183 | 0 | if (use_mrope) { |
184 | 0 | Qcur = ggml_rope_multi(ctx0, Qcur, inp_pos, nullptr, |
185 | 0 | n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale, |
186 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
187 | |
|
188 | 0 | Kcur = ggml_rope_multi(ctx0, Kcur, inp_pos, nullptr, |
189 | 0 | n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale, |
190 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
191 | 0 | } else { |
192 | | // Normal RoPE |
193 | 0 | Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, |
194 | 0 | rope_type, n_ctx_orig, freq_base, freq_scale, |
195 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
196 | |
|
197 | 0 | Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, |
198 | 0 | 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 | model.layers[il].wo, NULL, model.layers[il].wo_s, |
208 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il); |
209 | 0 | } |
210 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
211 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
212 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
213 | 0 | } |
214 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
215 | 0 | cb(ffn_inp, "ffn_inp", il); |
216 | | |
217 | | // Post-attention norm |
218 | 0 | cur = build_norm(ffn_inp, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il); |
219 | 0 | cb(cur, "post_attn_norm", il); |
220 | | |
221 | | // Check if this is a dense layer (n_layer_dense_lead=1, so layer 0 is dense) |
222 | 0 | if (static_cast<uint32_t>(il) < hparams.n_layer_dense_lead) { |
223 | | // Dense FFN layer |
224 | 0 | cur = build_ffn(cur, |
225 | 0 | model.layers[il].ffn_up, NULL, NULL, |
226 | 0 | model.layers[il].ffn_gate, NULL, NULL, |
227 | 0 | model.layers[il].ffn_down, NULL, NULL, |
228 | 0 | NULL, |
229 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
230 | 0 | cb(cur, "ffn_out", il); |
231 | 0 | } else { |
232 | | // Process routed experts using existing MoE infrastructure |
233 | 0 | ggml_tensor * routed_out = build_moe_ffn(cur, |
234 | 0 | model.layers[il].ffn_gate_inp, |
235 | 0 | model.layers[il].ffn_up_exps, |
236 | 0 | model.layers[il].ffn_gate_exps, |
237 | 0 | model.layers[il].ffn_down_exps, |
238 | 0 | model.layers[il].ffn_exp_probs_b, |
239 | 0 | n_expert, n_expert_used, |
240 | 0 | LLM_FFN_SILU, hparams.expert_weights_norm, |
241 | 0 | hparams.expert_weights_scale, |
242 | 0 | (llama_expert_gating_func_type) hparams.expert_gating_func, |
243 | 0 | il); |
244 | 0 | cb(routed_out, "ffn_moe_out", il); |
245 | | |
246 | | // Process shared expert on original input |
247 | 0 | ggml_tensor * shared_out = build_ffn(cur, |
248 | 0 | model.layers[il].ffn_up_shexp, NULL, NULL, |
249 | 0 | model.layers[il].ffn_gate_shexp, NULL, NULL, |
250 | 0 | model.layers[il].ffn_down_shexp, NULL, NULL, |
251 | 0 | NULL, |
252 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
253 | 0 | cb(shared_out, "ffn_shexp_out", il); |
254 | | |
255 | | // Final output: routed_output + shared_output |
256 | 0 | cur = ggml_add(ctx0, routed_out, shared_out); |
257 | 0 | cb(cur, "ffn_out", il); |
258 | 0 | } |
259 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
260 | |
|
261 | 0 | cur = build_cvec(cur, il); |
262 | 0 | cb(cur, "l_out", il); |
263 | | |
264 | | // input for next layer |
265 | 0 | inpL = cur; |
266 | 0 | } |
267 | 0 | cur = inpL; |
268 | 0 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
269 | |
|
270 | 0 | cb(cur, "result_norm", -1); |
271 | 0 | res->t_embd = cur; |
272 | | |
273 | | // lm_head |
274 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
275 | |
|
276 | 0 | cb(cur, "result_output", -1); |
277 | 0 | res->t_logits = cur; |
278 | |
|
279 | 0 | ggml_build_forward_expand(gf, cur); |
280 | 0 | } |