/src/llama.cpp/src/models/afmoe.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | llm_build_afmoe::llm_build_afmoe(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 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k); |
6 | |
|
7 | 0 | ggml_tensor * cur; |
8 | 0 | ggml_tensor * inpL; |
9 | |
|
10 | 0 | inpL = build_inp_embd(model.tok_embd); |
11 | | |
12 | | // MuP scaling: embeddings * sqrt(hidden_size) |
13 | | // mup_enabled = true, hidden_size = 1024, scale = 32.0 |
14 | 0 | inpL = ggml_scale(ctx0, inpL, sqrtf(float(n_embd))); |
15 | 0 | cb(inpL, "inp_embd_scaled", -1); |
16 | | |
17 | | // inp_pos - contains the positions |
18 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
19 | 0 | auto * inp_attn = build_attn_inp_kv_iswa(); |
20 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
21 | |
|
22 | 0 | const float kq_scale = 1.0f/sqrtf(float(n_embd_head)); |
23 | |
|
24 | 0 | for (int il = 0; il < n_layer; ++il) { |
25 | 0 | const float freq_base_l = model.get_rope_freq_base (cparams, il); |
26 | 0 | const float freq_scale_l = model.get_rope_freq_scale(cparams, il); |
27 | |
|
28 | 0 | ggml_tensor * inpSA = inpL; |
29 | | |
30 | | // This overlaps with SWA layers in current models, so get_rope_freq_base/scale may be superfluous |
31 | 0 | const bool use_rope = hparams.n_no_rope_layer_step > 0 && |
32 | 0 | (il + 1) % hparams.n_no_rope_layer_step != 0; |
33 | | |
34 | | // dual attention normalization (pre) |
35 | 0 | cur = build_norm(inpL, |
36 | 0 | model.layers[il].attn_norm, NULL, |
37 | 0 | LLM_NORM_RMS, il); |
38 | 0 | cb(cur, "attn_norm", il); |
39 | | |
40 | | // self-attention |
41 | 0 | { |
42 | 0 | ggml_tensor * attn_inp = cur; // save input for gate computation |
43 | |
|
44 | 0 | ggml_tensor * Qcur = build_lora_mm(model.layers[il].wq, cur); |
45 | 0 | cb(Qcur, "Qcur", il); |
46 | |
|
47 | 0 | ggml_tensor * Kcur = build_lora_mm(model.layers[il].wk, cur); |
48 | 0 | cb(Kcur, "Kcur", il); |
49 | |
|
50 | 0 | ggml_tensor * Vcur = build_lora_mm(model.layers[il].wv, cur); |
51 | 0 | cb(Vcur, "Vcur", il); |
52 | | |
53 | | // compute gate from input |
54 | 0 | ggml_tensor * gate = build_lora_mm(model.layers[il].wqkv_gate, attn_inp); |
55 | 0 | cb(gate, "attn_gate_proj", il); |
56 | |
|
57 | 0 | Qcur = ggml_reshape_3d(ctx0, Qcur, n_embd_head, n_head, n_tokens); |
58 | 0 | Kcur = ggml_reshape_3d(ctx0, Kcur, n_embd_head, n_head_kv, n_tokens); |
59 | | |
60 | | // Q/K normalization |
61 | 0 | Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, NULL, LLM_NORM_RMS, il); |
62 | 0 | Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, NULL, LLM_NORM_RMS, il); |
63 | 0 | cb(Qcur, "Qcur_normed", il); |
64 | 0 | cb(Kcur, "Kcur_normed", il); |
65 | |
|
66 | 0 | if (use_rope) { |
67 | 0 | Qcur = ggml_rope_ext( |
68 | 0 | ctx0, Qcur, inp_pos, nullptr, |
69 | 0 | n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
70 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
71 | 0 | cb(Qcur, "Qcur_rope", il); |
72 | |
|
73 | 0 | Kcur = ggml_rope_ext( |
74 | 0 | ctx0, Kcur, inp_pos, nullptr, |
75 | 0 | n_rot, rope_type, n_ctx_orig, freq_base_l, freq_scale_l, |
76 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
77 | 0 | cb(Kcur, "Kcur_rope", il); |
78 | 0 | } |
79 | |
|
80 | 0 | Vcur = ggml_reshape_3d(ctx0, Vcur, n_embd_head, n_head_kv, n_tokens); |
81 | |
|
82 | 0 | cur = build_attn(inp_attn, |
83 | 0 | NULL, NULL, // wo will be applied after gating |
84 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il); |
85 | 0 | cb(cur, "attn_out", il); |
86 | | |
87 | | // attention gating: attn_out * sigmoid(gate) BEFORE o_proj |
88 | 0 | gate = ggml_sigmoid(ctx0, gate); |
89 | 0 | cb(gate, "attn_gate_sig", il); |
90 | 0 | cur = ggml_mul(ctx0, cur, gate); |
91 | 0 | cb(cur, "attn_gated", il); |
92 | | |
93 | | // now apply output projection |
94 | 0 | cur = build_lora_mm(model.layers[il].wo, cur); |
95 | 0 | cb(cur, "attn_o_proj", il); |
96 | 0 | } |
97 | | |
98 | | // dual attention normalization (post) |
99 | 0 | cur = build_norm(cur, |
100 | 0 | model.layers[il].attn_post_norm, NULL, |
101 | 0 | LLM_NORM_RMS, il); |
102 | 0 | cb(cur, "attn_post_norm", il); |
103 | |
|
104 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
105 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
106 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
107 | 0 | } |
108 | |
|
109 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
110 | 0 | cb(ffn_inp, "ffn_inp", il); |
111 | | |
112 | | // dual ffn normalization (pre) |
113 | 0 | cur = build_norm(ffn_inp, |
114 | 0 | model.layers[il].ffn_norm, NULL, |
115 | 0 | LLM_NORM_RMS, il); |
116 | 0 | cb(cur, "ffn_norm", il); |
117 | | |
118 | | // MoE or dense FFN |
119 | 0 | if ((uint32_t)il >= hparams.n_layer_dense_lead) { |
120 | | // MoE layer with sigmoid routing, normalization, and scaling |
121 | 0 | ggml_tensor * moe_out = build_moe_ffn(cur, |
122 | 0 | model.layers[il].ffn_gate_inp, |
123 | 0 | model.layers[il].ffn_up_exps, |
124 | 0 | model.layers[il].ffn_gate_exps, |
125 | 0 | model.layers[il].ffn_down_exps, |
126 | 0 | model.layers[il].ffn_exp_probs_b, |
127 | 0 | n_expert, n_expert_used, |
128 | 0 | LLM_FFN_SILU, |
129 | 0 | hparams.expert_weights_norm, // norm_w (route_norm=True) |
130 | 0 | hparams.expert_weights_scale, // scale_w |
131 | 0 | hparams.expert_weights_scale, // w_scale (route_scale=2.826) |
132 | 0 | (llama_expert_gating_func_type) hparams.expert_gating_func, |
133 | 0 | il); |
134 | 0 | cb(moe_out, "ffn_moe_out", il); |
135 | | |
136 | | // shared expert |
137 | 0 | if (hparams.n_expert_shared > 0) { |
138 | 0 | ggml_tensor * ffn_shexp = build_ffn(cur, |
139 | 0 | model.layers[il].ffn_up_shexp, NULL, NULL, |
140 | 0 | model.layers[il].ffn_gate_shexp, NULL, NULL, |
141 | 0 | model.layers[il].ffn_down_shexp, NULL, NULL, |
142 | 0 | NULL, |
143 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
144 | 0 | cb(ffn_shexp, "ffn_shexp", il); |
145 | |
|
146 | 0 | cur = ggml_add(ctx0, moe_out, ffn_shexp); |
147 | 0 | cb(cur, "ffn_out", il); |
148 | 0 | } else { |
149 | 0 | cur = moe_out; |
150 | 0 | } |
151 | 0 | } else { |
152 | | // dense layer |
153 | 0 | cur = build_ffn(cur, |
154 | 0 | model.layers[il].ffn_up, NULL, NULL, |
155 | 0 | model.layers[il].ffn_gate, NULL, NULL, |
156 | 0 | model.layers[il].ffn_down, NULL, NULL, |
157 | 0 | NULL, |
158 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
159 | 0 | cb(cur, "ffn_out", il); |
160 | 0 | } |
161 | | |
162 | | // dual ffn normalization (post) |
163 | 0 | cur = build_norm(cur, |
164 | 0 | model.layers[il].ffn_post_norm, NULL, |
165 | 0 | LLM_NORM_RMS, il); |
166 | 0 | cb(cur, "ffn_post_norm", il); |
167 | |
|
168 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
169 | 0 | cur = build_cvec(cur, il); |
170 | 0 | cb(cur, "l_out", il); |
171 | | |
172 | | // input for next layer |
173 | 0 | inpL = cur; |
174 | 0 | } |
175 | |
|
176 | 0 | cur = inpL; |
177 | |
|
178 | 0 | cur = build_norm(cur, |
179 | 0 | model.output_norm, NULL, |
180 | 0 | LLM_NORM_RMS, -1); |
181 | 0 | cb(cur, "result_norm", -1); |
182 | |
|
183 | 0 | res->t_embd = cur; |
184 | | |
185 | | // lm_head |
186 | 0 | cur = build_lora_mm(model.output, cur); |
187 | 0 | cb(cur, "result_output", -1); |
188 | 0 | res->t_logits = cur; |
189 | |
|
190 | 0 | ggml_build_forward_expand(gf, cur); |
191 | 0 | } |