/src/llama.cpp/src/models/deepseek2.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | | |
4 | | |
5 | | llm_build_deepseek2::llm_build_deepseek2(const llama_model & model, const llm_graph_params & params) : |
6 | 0 | llm_graph_context(params) { |
7 | | // lite variants include DeepSeek-V2-Lite, GigaChat3-10B-A1.8B |
8 | 0 | bool is_lite = (hparams.n_layer == 27 || hparams.n_layer == 26); |
9 | |
|
10 | 0 | const bool is_mla = (hparams.n_embd_head_k_mla != 0 && hparams.n_embd_head_v_mla != 0); |
11 | | |
12 | | // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA |
13 | 0 | const int64_t n_embd_head_k = is_mla ? hparams.n_embd_head_k_mla : hparams.n_embd_head_k; |
14 | 0 | const int64_t n_embd_head_v = is_mla ? hparams.n_embd_head_v_mla : hparams.n_embd_head_v; |
15 | |
|
16 | 0 | const int64_t n_embd_head_qk_rope = hparams.n_rot; |
17 | 0 | const int64_t n_embd_head_qk_nope = n_embd_head_k - n_embd_head_qk_rope; |
18 | |
|
19 | 0 | const uint32_t kv_lora_rank = hparams.n_lora_kv; |
20 | | |
21 | | // We have to pre-scale kq_scale and attn_factor to make the YaRN RoPE work correctly. |
22 | | // See https://github.com/ggerganov/llama.cpp/discussions/7416 for detailed explanation. |
23 | 0 | const float mscale = attn_factor * (1.0f + hparams.rope_yarn_log_mul * logf(1.0f / freq_scale)); |
24 | 0 | const float kq_scale = 1.0f * mscale * mscale / sqrtf(float(n_embd_head_k)); |
25 | 0 | const float attn_factor = 1.0f / (1.0f + 0.1f * logf(1.0f / freq_scale)); |
26 | |
|
27 | 0 | ggml_tensor * cur; |
28 | 0 | ggml_tensor * inpL; |
29 | | |
30 | | // {n_embd, n_tokens} |
31 | 0 | inpL = build_inp_embd(model.tok_embd); |
32 | | |
33 | | // inp_pos - contains the positions |
34 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
35 | |
|
36 | 0 | auto * inp_attn = build_attn_inp_kv(); |
37 | |
|
38 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
39 | |
|
40 | 0 | for (int il = 0; il < n_layer; ++il) { |
41 | 0 | ggml_tensor * inpSA = inpL; |
42 | | |
43 | | // norm |
44 | 0 | cur = build_norm(inpL, model.layers[il].attn_norm, NULL, LLM_NORM_RMS, il); |
45 | 0 | cb(cur, "attn_norm", il); |
46 | | |
47 | | // self_attention |
48 | 0 | { |
49 | 0 | ggml_tensor * q = NULL; |
50 | 0 | if (!is_lite) { |
51 | 0 | q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur); |
52 | 0 | cb(q, "q", il); |
53 | |
|
54 | 0 | q = build_norm(q, model.layers[il].attn_q_a_norm, nullptr, LLM_NORM_RMS, il); |
55 | 0 | cb(q, "q", il); |
56 | |
|
57 | 0 | q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q); |
58 | 0 | cb(q, "q", il); |
59 | 0 | } else { |
60 | 0 | q = ggml_mul_mat(ctx0, model.layers[il].wq, cur); |
61 | 0 | cb(q, "q", il); |
62 | 0 | } |
63 | | // split into {n_embd_head_qk_nope, n_head, n_tokens} |
64 | 0 | ggml_tensor * q_nope = |
65 | 0 | ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens, ggml_row_size(q->type, n_embd_head_k), |
66 | 0 | ggml_row_size(q->type, n_embd_head_k) * n_head, 0); |
67 | 0 | cb(q_nope, "q_nope", il); |
68 | | |
69 | | // and {n_embd_head_qk_rope, n_head, n_tokens} |
70 | 0 | ggml_tensor * q_pe = ggml_view_3d( |
71 | 0 | ctx0, q, n_embd_head_qk_rope, n_head, n_tokens, ggml_row_size(q->type, n_embd_head_k), |
72 | 0 | ggml_row_size(q->type, n_embd_head_k) * n_head, ggml_row_size(q->type, n_embd_head_qk_nope)); |
73 | 0 | cb(q_pe, "q_pe", il); |
74 | |
|
75 | 0 | ggml_tensor * kv_cmpr_pe = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur); |
76 | 0 | cb(kv_cmpr_pe, "kv_cmpr_pe", il); |
77 | | |
78 | | // split into {kv_lora_rank, n_tokens} |
79 | 0 | ggml_tensor * kv_cmpr = |
80 | 0 | ggml_view_2d(ctx0, kv_cmpr_pe, kv_lora_rank, n_tokens, |
81 | 0 | ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope), 0); |
82 | 0 | cb(kv_cmpr, "kv_cmpr", il); |
83 | | |
84 | | // and {n_embd_head_qk_rope, 1, n_tokens} |
85 | 0 | ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_cmpr_pe, n_embd_head_qk_rope, 1, n_tokens, |
86 | 0 | ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope), |
87 | 0 | ggml_row_size(kv_cmpr_pe->type, kv_lora_rank + n_embd_head_qk_rope), |
88 | 0 | ggml_row_size(kv_cmpr_pe->type, kv_lora_rank)); |
89 | 0 | cb(k_pe, "k_pe", il); |
90 | |
|
91 | 0 | q_pe = ggml_rope_ext(ctx0, q_pe, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
92 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
93 | 0 | cb(q_pe, "q_pe", il); |
94 | |
|
95 | 0 | k_pe = ggml_rope_ext(ctx0, k_pe, inp_pos, nullptr, n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
96 | 0 | ext_factor, attn_factor, beta_fast, beta_slow); |
97 | 0 | cb(k_pe, "k_pe", il); |
98 | |
|
99 | 0 | kv_cmpr = build_norm(kv_cmpr, model.layers[il].attn_kv_a_norm, nullptr, LLM_NORM_RMS, il); |
100 | 0 | cb(kv_cmpr, "kv_cmpr", il); |
101 | |
|
102 | 0 | if (is_mla) { |
103 | | // {n_embd_head_qk_nope, n_tokens, n_head} |
104 | 0 | q_nope = ggml_permute(ctx0, q_nope, 0, 2, 1, 3); |
105 | 0 | cb(q_nope, "q_nope_perm", il); |
106 | | |
107 | | // {n_embd_head_qk_nope, kv_lora_rank, n_head} x {n_embd_head_qk_nope, n_tokens, n_head} |
108 | 0 | ggml_tensor * q_nope_absorbed = ggml_mul_mat(ctx0, model.layers[il].wk_b, q_nope); |
109 | 0 | cb(q_nope_absorbed, "q_nope_absorbed", il); |
110 | | |
111 | | // {kv_lora_rank, n_head, n_tokens} |
112 | 0 | q_nope_absorbed = ggml_permute(ctx0, q_nope_absorbed, 0, 2, 1, 3); |
113 | 0 | cb(q_nope_absorbed, "q_nope_absorbed_perm", il); |
114 | | |
115 | | // {n_embd_head_qk_rope + kv_lora_rank, n_head, n_tokens} |
116 | | // note: rope must go first for in-place context shifting in build_rope_shift() |
117 | 0 | ggml_tensor * Qcur = ggml_concat(ctx0, q_pe, q_nope_absorbed, 0); |
118 | 0 | cb(Qcur, "Qcur", il); |
119 | |
|
120 | 0 | kv_cmpr = ggml_reshape_3d(ctx0, kv_cmpr, kv_lora_rank, 1, n_tokens); |
121 | 0 | cb(kv_cmpr, "kv_cmpr_reshape", il); |
122 | | |
123 | | // {n_embd_head_qk_rope + kv_lora_rank, 1, n_tokens} |
124 | 0 | ggml_tensor * Kcur = ggml_concat(ctx0, k_pe, kv_cmpr, 0); |
125 | 0 | cb(Kcur, "Kcur", il); |
126 | | |
127 | | // {kv_lora_rank, 1, n_tokens} |
128 | 0 | ggml_tensor * Vcur = kv_cmpr; |
129 | 0 | cb(Vcur, "Vcur", il); |
130 | | |
131 | | // note: MLA with the absorption optimzation converts into MQA (ie: GQA with 1 group) |
132 | 0 | cur = build_attn(inp_attn, |
133 | 0 | model.layers[il].wo, NULL, |
134 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, model.layers[il].wv_b, kq_scale, il); |
135 | 0 | } else { |
136 | 0 | ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_cmpr); |
137 | 0 | cb(kv, "kv", il); |
138 | | |
139 | | // split into {n_embd_head_qk_nope, n_head, n_tokens} |
140 | 0 | ggml_tensor * k_nope = |
141 | 0 | ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens, |
142 | 0 | ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v), |
143 | 0 | ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v) * n_head, 0); |
144 | 0 | cb(k_nope, "k_nope_view", il); |
145 | | |
146 | | // and {n_embd_head_v, n_head, n_tokens} |
147 | 0 | ggml_tensor * Vcur = ggml_view_3d(ctx0, kv, n_embd_head_v, n_head, n_tokens, |
148 | 0 | ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v), |
149 | 0 | ggml_row_size(kv->type, n_embd_head_qk_nope + n_embd_head_v) * n_head, |
150 | 0 | ggml_row_size(kv->type, n_embd_head_qk_nope)); |
151 | 0 | cb(Vcur, "Vcur_view", il); |
152 | |
|
153 | 0 | Vcur = ggml_cont(ctx0, Vcur); |
154 | 0 | cb(Vcur, "Vcur_cont", il); |
155 | | |
156 | | // note: rope must go first for in-place context shifting in build_rope_shift() |
157 | 0 | ggml_tensor * Qcur = ggml_concat(ctx0, q_pe, q_nope, 0); |
158 | 0 | cb(Qcur, "Qcur", il); |
159 | |
|
160 | 0 | ggml_tensor * Kcur = ggml_concat(ctx0, ggml_repeat(ctx0, k_pe, q_pe), k_nope, 0); |
161 | 0 | cb(Kcur, "Kcur", il); |
162 | | |
163 | | // note: MLA without the absorption optimization converts into MHA (ie: GQA with full n_head groups) |
164 | 0 | cur = build_attn(inp_attn, |
165 | 0 | model.layers[il].wo, NULL, |
166 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, kq_scale, il); |
167 | 0 | } |
168 | 0 | } |
169 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
170 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
171 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
172 | 0 | } |
173 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
174 | 0 | cb(ffn_inp, "ffn_inp", il); |
175 | |
|
176 | 0 | cur = build_norm(ffn_inp, model.layers[il].ffn_norm, NULL, LLM_NORM_RMS, il); |
177 | 0 | cb(cur, "ffn_norm", il); |
178 | |
|
179 | 0 | if ((uint32_t) il < hparams.n_layer_dense_lead) { |
180 | 0 | cur = build_ffn(cur, |
181 | 0 | model.layers[il].ffn_up, NULL, NULL, |
182 | 0 | model.layers[il].ffn_gate, NULL, NULL, |
183 | 0 | model.layers[il].ffn_down, NULL, NULL, |
184 | 0 | NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); |
185 | 0 | cb(cur, "ffn_out", il); |
186 | 0 | } else { |
187 | | // MoE branch |
188 | 0 | ggml_tensor * moe_out = build_moe_ffn(cur, |
189 | 0 | model.layers[il].ffn_gate_inp, |
190 | 0 | model.layers[il].ffn_up_exps, |
191 | 0 | model.layers[il].ffn_gate_exps, |
192 | 0 | model.layers[il].ffn_down_exps, |
193 | 0 | model.layers[il].ffn_exp_probs_b, |
194 | 0 | n_expert, n_expert_used, |
195 | 0 | LLM_FFN_SILU, hparams.expert_weights_norm, |
196 | 0 | true, hparams.expert_weights_scale, |
197 | 0 | (llama_expert_gating_func_type) hparams.expert_gating_func, |
198 | 0 | il); |
199 | 0 | cb(moe_out, "ffn_moe_out", il); |
200 | | |
201 | | // FFN shared expert |
202 | 0 | { |
203 | 0 | ggml_tensor * ffn_shexp = |
204 | 0 | build_ffn(cur, |
205 | 0 | model.layers[il].ffn_up_shexp, NULL, NULL, |
206 | 0 | model.layers[il].ffn_gate_shexp, NULL, NULL, |
207 | 0 | model.layers[il].ffn_down_shexp, NULL, NULL, |
208 | 0 | NULL, LLM_FFN_SILU, LLM_FFN_PAR, il); |
209 | 0 | cb(ffn_shexp, "ffn_shexp", il); |
210 | |
|
211 | 0 | cur = ggml_add(ctx0, moe_out, ffn_shexp); |
212 | 0 | cb(cur, "ffn_out", il); |
213 | 0 | } |
214 | 0 | } |
215 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
216 | |
|
217 | 0 | cur = build_cvec(cur, il); |
218 | 0 | cb(cur, "l_out", il); |
219 | | |
220 | | // input for next layer |
221 | 0 | inpL = cur; |
222 | 0 | } |
223 | 0 | cur = inpL; |
224 | |
|
225 | 0 | cur = build_norm(cur, model.output_norm, NULL, LLM_NORM_RMS, -1); |
226 | |
|
227 | 0 | cb(cur, "result_norm", -1); |
228 | 0 | res->t_embd = cur; |
229 | | |
230 | | // lm_head |
231 | 0 | cur = ggml_mul_mat(ctx0, model.output, cur); |
232 | |
|
233 | 0 | cb(cur, "result_output", -1); |
234 | 0 | res->t_logits = cur; |
235 | |
|
236 | 0 | ggml_build_forward_expand(gf, cur); |
237 | 0 | } |