/src/llama.cpp/src/models/afmoe.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_afmoe::load_arch_hparams(llama_model_loader & ml) { |
4 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
5 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
6 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
7 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared); |
8 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false); |
9 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
10 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false); |
11 | 0 | ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); |
12 | | |
13 | | // Set up interleaved sliding window attention (ISWA) |
14 | | // Pattern: 3 sliding - 1 full (global_attn_every_n_layers = 4) |
15 | 0 | if (hparams.n_swa > 0) { |
16 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
17 | 0 | uint32_t swa_period = 4; |
18 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
19 | 0 | hparams.set_swa_pattern(swa_period); |
20 | |
|
21 | 0 | hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train; |
22 | 0 | hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train; |
23 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
24 | 0 | } else { |
25 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_NONE; |
26 | 0 | } |
27 | | |
28 | | // Default to sigmoid if not set |
29 | 0 | if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) { |
30 | 0 | hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID; |
31 | 0 | } |
32 | |
|
33 | 0 | switch (hparams.n_layer()) { |
34 | 0 | case 56: type = LLM_TYPE_6B; break; |
35 | 0 | case 32: type = LLM_TYPE_26B; break; |
36 | 0 | default: type = LLM_TYPE_UNKNOWN; |
37 | 0 | } |
38 | 0 | } |
39 | | |
40 | 0 | void llama_model_afmoe::load_arch_tensors(llama_model_loader &) { |
41 | 0 | LLAMA_LOAD_LOCALS; |
42 | 0 | const int64_t n_expert_shared = hparams.n_expert_shared; |
43 | |
|
44 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
45 | | |
46 | | // output |
47 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
48 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
49 | | |
50 | | // if output is NULL, init from the input tok embed |
51 | 0 | if (output == NULL) { |
52 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
53 | 0 | } |
54 | |
|
55 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
56 | |
|
57 | 0 | for (int i = 0; i < n_layer; ++i) { |
58 | 0 | auto & layer = layers[i]; |
59 | | |
60 | | // dual attention normalization |
61 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
62 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0); |
63 | | |
64 | | // attention projections |
65 | 0 | create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_k_gqa, n_embd_v_gqa, 0); |
66 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
67 | | |
68 | | // Q/K normalization |
69 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
70 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
71 | | |
72 | | // attention gating |
73 | 0 | layer.wqkv_gate = create_tensor(tn(LLM_TENSOR_ATTN_GATE, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
74 | | |
75 | | // dual ffn normalization |
76 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
77 | 0 | layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0); |
78 | |
|
79 | 0 | if (static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead) { |
80 | | // MoE layers |
81 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
82 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, 0); |
83 | | |
84 | | // grouped expert weights |
85 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0); |
86 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0); |
87 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0); |
88 | | |
89 | | // shared expert |
90 | 0 | if (n_expert_shared > 0) { |
91 | 0 | const int64_t n_ff_shexp = n_ff_exp * n_expert_shared; |
92 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_shexp}, 0); |
93 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, 0); |
94 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_shexp}, 0); |
95 | 0 | } |
96 | 0 | } else { |
97 | | // Dense layers |
98 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
99 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
100 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
101 | 0 | } |
102 | 0 | } |
103 | 0 | } |
104 | | |
105 | 0 | std::unique_ptr<llm_graph_context> llama_model_afmoe::build_arch_graph(const llm_graph_params & params) const { |
106 | 0 | return std::make_unique<graph>(*this, params); |
107 | 0 | } |
108 | | |
109 | 0 | llama_model_afmoe::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
110 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
111 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
112 | |
|
113 | 0 | ggml_tensor * cur; |
114 | 0 | ggml_tensor * inpL; |
115 | |
|
116 | 0 | inpL = build_inp_embd(model.tok_embd); |
117 | | |
118 | | // MuP scaling: embeddings * sqrt(hidden_size) |
119 | | // mup_enabled = true, hidden_size = 1024, scale = 32.0 |
120 | 0 | inpL = ggml_scale(ctx0, inpL, sqrtf(float(n_embd))); |
121 | 0 | cb(inpL, "inp_embd_scaled", -1); |
122 | | |
123 | | // inp_pos - contains the positions |
124 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
125 | 0 | auto * inp_attn = build_attn_inp_kv_iswa(); |
126 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
127 | |
|
128 | 0 | const float kq_scale = 1.0f/sqrtf(float(n_embd_head)); |
129 | |
|
130 | 0 | for (int il = 0; il < n_layer; ++il) { |
131 | 0 | const float freq_base_l = model.get_rope_freq_base (cparams, il); |
132 | 0 | const float freq_scale_l = model.get_rope_freq_scale(cparams, il); |
133 | |
|
134 | 0 | ggml_tensor * inpSA = inpL; |
135 | | |
136 | | // This overlaps with SWA layers in current models, so get_rope_freq_base/scale may be superfluous |
137 | 0 | const bool use_rope = hparams.n_no_rope_layer_step > 0 && |
138 | 0 | (il + 1) % hparams.n_no_rope_layer_step != 0; |
139 | | |
140 | | // dual attention normalization (pre) |
141 | 0 | cur = build_norm(inpL, |
142 | 0 | model.layers[il].attn_norm, NULL, |
143 | 0 | LLM_NORM_RMS, il); |
144 | 0 | cb(cur, "attn_norm", il); |
145 | | |
146 | | // self-attention |
147 | 0 | { |
148 | 0 | ggml_tensor * attn_inp = cur; // save input for gate computation |
149 | |
|
150 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
151 | 0 | n_embd_head, n_head, n_head_kv, il); |
152 | | |
153 | | // compute gate from input |
154 | 0 | ggml_tensor * gate = build_lora_mm(model.layers[il].wqkv_gate, attn_inp); |
155 | 0 | cb(gate, "attn_gate_proj", il); |
156 | | |
157 | | // Q/K normalization |
158 | 0 | Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il); |
159 | 0 | Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il); |
160 | 0 | cb(Qcur, "Qcur_normed", il); |
161 | 0 | cb(Kcur, "Kcur_normed", il); |
162 | |
|
163 | 0 | if (use_rope) { |
164 | 0 | Qcur = ggml_rope_ext( |
165 | 0 | ctx0, Qcur, inp_pos, nullptr, |
166 | 0 | n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
167 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
168 | 0 | cb(Qcur, "Qcur_rope", il); |
169 | |
|
170 | 0 | Kcur = ggml_rope_ext( |
171 | 0 | ctx0, Kcur, inp_pos, nullptr, |
172 | 0 | n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
173 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
174 | 0 | cb(Kcur, "Kcur_rope", il); |
175 | 0 | } |
176 | |
|
177 | 0 | cur = build_attn(inp_attn, |
178 | 0 | NULL, NULL, NULL, // wo will be applied after gating |
179 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il); |
180 | 0 | cb(cur, "attn_out", il); |
181 | | |
182 | | // attention gating: attn_out * sigmoid(gate) BEFORE o_proj |
183 | 0 | gate = ggml_sigmoid(ctx0, gate); |
184 | 0 | cb(gate, "attn_gate_sig", il); |
185 | 0 | cur = ggml_mul(ctx0, cur, gate); |
186 | 0 | cb(cur, "attn_gated", il); |
187 | | |
188 | | // now apply output projection |
189 | 0 | cur = build_lora_mm(model.layers[il].wo, cur, model.layers[il].wo_s); |
190 | 0 | cb(cur, "attn_o_proj", il); |
191 | 0 | } |
192 | | |
193 | | // dual attention normalization (post) |
194 | 0 | cur = build_norm(cur, |
195 | 0 | model.layers[il].attn_post_norm, NULL, |
196 | 0 | LLM_NORM_RMS, il); |
197 | 0 | cb(cur, "attn_post_norm", il); |
198 | |
|
199 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
200 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
201 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
202 | 0 | } |
203 | |
|
204 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
205 | 0 | cb(ffn_inp, "ffn_inp", il); |
206 | | |
207 | | // dual ffn normalization (pre) |
208 | 0 | cur = build_norm(ffn_inp, |
209 | 0 | model.layers[il].ffn_norm, NULL, |
210 | 0 | LLM_NORM_RMS, il); |
211 | 0 | cb(cur, "ffn_norm", il); |
212 | | |
213 | | // MoE or dense FFN |
214 | 0 | if ((uint32_t)il >= hparams.n_layer_dense_lead) { |
215 | | // MoE layer with sigmoid routing, normalization, and scaling |
216 | 0 | ggml_tensor * moe_out = build_moe_ffn(cur, |
217 | 0 | model.layers[il].ffn_gate_inp, |
218 | 0 | model.layers[il].ffn_up_exps, |
219 | 0 | model.layers[il].ffn_gate_exps, |
220 | 0 | model.layers[il].ffn_down_exps, |
221 | 0 | model.layers[il].ffn_exp_probs_b, |
222 | 0 | n_expert, n_expert_used, |
223 | 0 | LLM_FFN_SILU, |
224 | 0 | hparams.expert_weights_norm, // norm_w (route_norm=True) |
225 | 0 | hparams.expert_weights_scale, // w_scale (route_scale=2.826) |
226 | 0 | (llama_expert_gating_func_type) hparams.expert_gating_func, |
227 | 0 | il); |
228 | 0 | cb(moe_out, "ffn_moe_out", il); |
229 | | |
230 | | // shared expert |
231 | 0 | if (hparams.n_expert_shared > 0) { |
232 | 0 | ggml_tensor * ffn_shexp = build_ffn(cur, |
233 | 0 | model.layers[il].ffn_up_shexp, NULL, NULL, |
234 | 0 | model.layers[il].ffn_gate_shexp, NULL, NULL, |
235 | 0 | model.layers[il].ffn_down_shexp, NULL, NULL, |
236 | 0 | NULL, |
237 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
238 | 0 | cb(ffn_shexp, "ffn_shexp", il); |
239 | |
|
240 | 0 | cur = ggml_add(ctx0, moe_out, ffn_shexp); |
241 | 0 | cb(cur, "ffn_out", il); |
242 | 0 | } else { |
243 | 0 | cur = moe_out; |
244 | 0 | } |
245 | 0 | } else { |
246 | | // dense layer |
247 | 0 | cur = build_ffn(cur, |
248 | 0 | model.layers[il].ffn_up, NULL, NULL, |
249 | 0 | model.layers[il].ffn_gate, NULL, NULL, |
250 | 0 | model.layers[il].ffn_down, NULL, NULL, |
251 | 0 | NULL, |
252 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
253 | 0 | cb(cur, "ffn_out", il); |
254 | 0 | } |
255 | | |
256 | | // dual ffn normalization (post) |
257 | 0 | cur = build_norm(cur, |
258 | 0 | model.layers[il].ffn_post_norm, NULL, |
259 | 0 | LLM_NORM_RMS, il); |
260 | 0 | cb(cur, "ffn_post_norm", il); |
261 | |
|
262 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
263 | 0 | cur = build_cvec(cur, il); |
264 | 0 | cb(cur, "l_out", il); |
265 | | |
266 | | // input for next layer |
267 | 0 | inpL = cur; |
268 | 0 | } |
269 | |
|
270 | 0 | cur = inpL; |
271 | |
|
272 | 0 | cur = build_norm(cur, |
273 | 0 | model.output_norm, NULL, |
274 | 0 | LLM_NORM_RMS, -1); |
275 | 0 | cb(cur, "result_norm", -1); |
276 | |
|
277 | 0 | res->t_embd = cur; |
278 | | |
279 | | // lm_head |
280 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
281 | 0 | cb(cur, "result_output", -1); |
282 | 0 | res->t_logits = cur; |
283 | |
|
284 | 0 | ggml_build_forward_expand(gf, cur); |
285 | 0 | } |