/src/llama.cpp/src/models/glm4-moe.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | llm_build_glm4_moe::llm_build_glm4_moe(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
4 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v; |
5 | |
|
6 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
7 | |
|
8 | 0 | int sections[4]; |
9 | 0 | std::copy(std::begin(hparams.rope_sections), std::begin(hparams.rope_sections) + 4, sections); |
10 | |
|
11 | 0 | ggml_tensor * cur; |
12 | 0 | ggml_tensor * inpL; |
13 | |
|
14 | 0 | inpL = build_inp_embd(model.tok_embd); |
15 | |
|
16 | 0 | bool use_mrope = hparams.use_mrope(); |
17 | 0 | if (ubatch.embd && !use_mrope) { |
18 | | // unfortunately, we need to forcefully stop here, to avoid users complaining about wrong results |
19 | 0 | GGML_ABORT("This GGUF does not support multimodal. Please reconvert it."); |
20 | 0 | } |
21 | | |
22 | | // inp_pos - contains the positions |
23 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
24 | |
|
25 | 0 | auto * inp_attn = build_attn_inp_kv(); |
26 | |
|
27 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
28 | | |
29 | | // Only process up to last layer (skip final NextN layer) |
30 | | // Final layer tensors are loaded but not processed in forward pass |
31 | 0 | const int n_transformer_layers = n_layer - hparams.nextn_predict_layers; |
32 | 0 | for (int il = 0; il < n_transformer_layers; ++il) { |
33 | 0 | ggml_tensor * inpSA = inpL; |
34 | | |
35 | | // Pre-attention norm |
36 | 0 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
37 | 0 | cb(cur, "attn_norm", il); |
38 | | |
39 | | // self-attention |
40 | 0 | { |
41 | 0 | ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur); |
42 | 0 | if (model.layers[il].bq) { |
43 | 0 | Qcur = ggml_add(ctx0, Qcur, model.layers[il].bq); |
44 | 0 | } |
45 | 0 | cb(Qcur, "Qcur", il); |
46 | |
|
47 | 0 | ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur); |
48 | 0 | if (model.layers[il].bk) { |
49 | 0 | Kcur = ggml_add(ctx0, Kcur, model.layers[il].bk); |
50 | 0 | } |
51 | 0 | cb(Kcur, "Kcur", il); |
52 | |
|
53 | 0 | ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur); |
54 | 0 | if (model.layers[il].bv) { |
55 | 0 | Vcur = ggml_add(ctx0, Vcur, model.layers[il].bv); |
56 | 0 | } |
57 | 0 | cb(Vcur, "Vcur", il); |
58 | |
|
59 | 0 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
60 | 0 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); |
61 | 0 | Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); |
62 | | |
63 | | // Apply Q/K norm if available (GLM-4.5 355B variant) |
64 | 0 | if (model.layers[il].attn_q_norm) { |
65 | 0 | Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il); |
66 | 0 | cb(Qcur, "Qcur_normed", il); |
67 | 0 | } |
68 | 0 | if (model.layers[il].attn_k_norm) { |
69 | 0 | Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il); |
70 | 0 | cb(Kcur, "Kcur_normed", il); |
71 | 0 | } |
72 | |
|
73 | 0 | if (use_mrope) { |
74 | 0 | Qcur = ggml_rope_multi(ctx0, Qcur, inp_pos, nullptr, |
75 | 0 | n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale, |
76 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
77 | |
|
78 | 0 | Kcur = ggml_rope_multi(ctx0, Kcur, inp_pos, nullptr, |
79 | 0 | n_rot, sections, rope_type, n_ctx_orig, freq_base, freq_scale, |
80 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
81 | 0 | } else { |
82 | | // Normal RoPE |
83 | 0 | Qcur = ggml_rope_ext(ctx0, Qcur, inp_pos, nullptr, n_rot, |
84 | 0 | rope_type, n_ctx_orig, freq_base, freq_scale, |
85 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
86 | |
|
87 | 0 | Kcur = ggml_rope_ext(ctx0, Kcur, inp_pos, nullptr, n_rot, |
88 | 0 | rope_type, n_ctx_orig, freq_base, freq_scale, |
89 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
90 | 0 | } |
91 | |
|
92 | 0 | cb(Qcur, "Qcur", il); |
93 | 0 | cb(Kcur, "Kcur", il); |
94 | 0 | cb(Vcur, "Vcur", il); |
95 | |
|
96 | 0 | cur = build_attn(inp_attn, |
97 | 0 | model.layers[il].wo, NULL, |
98 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il); |
99 | 0 | } |
100 | 0 | if (il == n_transformer_layers - 1 && inp_out_ids) { |
101 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
102 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
103 | 0 | } |
104 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
105 | 0 | cb(ffn_inp, "ffn_inp", il); |
106 | | |
107 | | // Post-attention norm |
108 | 0 | cur = build_norm(ffn_inp, model.layers[il].attn_post_norm, NULL, LLM_NORM_RMS, il); |
109 | 0 | cb(cur, "post_attn_norm", il); |
110 | | |
111 | | // Check if this is a dense layer (n_layer_dense_lead=1, so layer 0 is dense) |
112 | 0 | if (static_cast<uint32_t>(il) < hparams.n_layer_dense_lead) { |
113 | | // Dense FFN layer |
114 | 0 | cur = build_ffn(cur, |
115 | 0 | model.layers[il].ffn_up, NULL, NULL, |
116 | 0 | model.layers[il].ffn_gate, NULL, NULL, |
117 | 0 | model.layers[il].ffn_down, NULL, NULL, |
118 | 0 | NULL, |
119 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
120 | 0 | cb(cur, "ffn_out", il); |
121 | 0 | } else { |
122 | | // Process routed experts using existing MoE infrastructure |
123 | 0 | ggml_tensor * routed_out = build_moe_ffn(cur, |
124 | 0 | model.layers[il].ffn_gate_inp, |
125 | 0 | model.layers[il].ffn_up_exps, |
126 | 0 | model.layers[il].ffn_gate_exps, |
127 | 0 | model.layers[il].ffn_down_exps, |
128 | 0 | model.layers[il].ffn_exp_probs_b, |
129 | 0 | n_expert, n_expert_used, |
130 | 0 | LLM_FFN_SILU, hparams.expert_weights_norm, |
131 | 0 | true, hparams.expert_weights_scale, |
132 | 0 | (llama_expert_gating_func_type) hparams.expert_gating_func, |
133 | 0 | il); |
134 | 0 | cb(routed_out, "ffn_moe_out", il); |
135 | | |
136 | | // Process shared expert on original input |
137 | 0 | ggml_tensor * shared_out = build_ffn(cur, |
138 | 0 | model.layers[il].ffn_up_shexp, NULL, NULL, |
139 | 0 | model.layers[il].ffn_gate_shexp, NULL, NULL, |
140 | 0 | model.layers[il].ffn_down_shexp, NULL, NULL, |
141 | 0 | NULL, |
142 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
143 | 0 | cb(shared_out, "ffn_shexp_out", il); |
144 | | |
145 | | // Final output: routed_output + shared_output |
146 | 0 | cur = ggml_add(ctx0, routed_out, shared_out); |
147 | 0 | cb(cur, "ffn_out", il); |
148 | 0 | } |
149 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
150 | |
|
151 | 0 | cur = build_cvec(cur, il); |
152 | 0 | cb(cur, "l_out", il); |
153 | | |
154 | | // input for next layer |
155 | 0 | inpL = cur; |
156 | 0 | } |
157 | 0 | cur = inpL; |
158 | 0 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
159 | |
|
160 | 0 | cb(cur, "result_norm", -1); |
161 | 0 | res->t_embd = cur; |
162 | | |
163 | | // lm_head |
164 | 0 | cur = build_lora_mm(model.output, cur); |
165 | |
|
166 | 0 | cb(cur, "result_output", -1); |
167 | 0 | res->t_logits = cur; |
168 | |
|
169 | 0 | ggml_build_forward_expand(gf, cur); |
170 | 0 | } |