/src/llama.cpp/src/models/minicpm3.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_minicpm3::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_ATTENTION_Q_LORA_RANK, hparams.n_lora_q); |
6 | 0 | ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv); |
7 | |
|
8 | 0 | switch (hparams.n_layer()) { |
9 | 0 | case 62: type = LLM_TYPE_4B; break; |
10 | 0 | default: type = LLM_TYPE_UNKNOWN; |
11 | 0 | } |
12 | 0 | } |
13 | | |
14 | 0 | void llama_model_minicpm3::load_arch_tensors(llama_model_loader &) { |
15 | 0 | LLAMA_LOAD_LOCALS; |
16 | |
|
17 | 0 | const int64_t n_embd_head_qk_rope = hparams.n_rot(); |
18 | 0 | const int64_t n_embd_head_qk_nope = hparams.n_embd_head_k() - hparams.n_rot(); |
19 | |
|
20 | 0 | const int64_t q_lora_rank = hparams.n_lora_q; |
21 | 0 | const int64_t kv_lora_rank = hparams.n_lora_kv; |
22 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
23 | | |
24 | | // output |
25 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
26 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
27 | | |
28 | | // if output is NULL, init from the input tok embed |
29 | 0 | if (output == NULL) { |
30 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
31 | 0 | } |
32 | |
|
33 | 0 | for (int i = 0; i < n_layer; ++i) { |
34 | 0 | auto & layer = layers[i]; |
35 | |
|
36 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
37 | 0 | layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, 0); |
38 | |
|
39 | 0 | layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0); |
40 | |
|
41 | 0 | layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, 0); |
42 | 0 | layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k}, 0); |
43 | |
|
44 | 0 | layer.wkv_a_mqa = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_MQA, "weight", i), {n_embd, kv_lora_rank + (n_embd_head_qk_rope)}, 0); |
45 | 0 | layer.wkv_b = create_tensor(tn(LLM_TENSOR_ATTN_KV_B, "weight", i), {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)}, 0); |
46 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_head * ( n_embd_head_v), n_embd}, 0); |
47 | |
|
48 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
49 | |
|
50 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
51 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
52 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
53 | |
|
54 | 0 | layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), { n_embd_head_qk_rope/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
55 | 0 | layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), { n_embd_head_qk_rope/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
56 | 0 | } |
57 | 0 | } |
58 | | |
59 | 0 | std::unique_ptr<llm_graph_context> llama_model_minicpm3::build_arch_graph(const llm_graph_params & params) const { |
60 | 0 | return std::make_unique<graph>(*this, params); |
61 | 0 | } |
62 | | |
63 | 0 | llama_model_minicpm3::graph::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
64 | | //TODO: if the model varies, these parameters need to be read from the model |
65 | 0 | const int64_t n_embd_base = 256; |
66 | 0 | const float scale_embd = 12.0f; |
67 | 0 | const float scale_depth = 1.4f; |
68 | 0 | const float kq_scale = 1.0f / sqrtf(float(hparams.n_embd_head_k())); |
69 | |
|
70 | 0 | const uint32_t n_embd_head_qk_rope = hparams.n_rot(); |
71 | 0 | const uint32_t n_embd_head_qk_nope = hparams.n_embd_head_k() - hparams.n_rot(); |
72 | |
|
73 | 0 | const uint32_t kv_lora_rank = hparams.n_lora_kv; |
74 | |
|
75 | 0 | ggml_tensor * cur; |
76 | 0 | ggml_tensor * inpL; |
77 | |
|
78 | 0 | inpL = build_inp_embd(model.tok_embd); |
79 | | |
80 | | // scale the input embeddings |
81 | 0 | inpL = ggml_scale(ctx0, inpL, scale_embd); |
82 | 0 | cb(inpL, "inp_scaled", -1); |
83 | | |
84 | | // inp_pos - contains the positions |
85 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
86 | |
|
87 | 0 | auto * inp_attn = build_attn_inp_kv(); |
88 | |
|
89 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
90 | |
|
91 | 0 | for (int il = 0; il < n_layer; ++il) { |
92 | 0 | ggml_tensor * inpSA = inpL; |
93 | |
|
94 | 0 | ggml_tensor * rope_factors = model.get_rope_factors(cparams, il); |
95 | | |
96 | | // norm |
97 | 0 | cur = build_norm(inpL, |
98 | 0 | model.layers[il].attn_norm, NULL, |
99 | 0 | LLM_NORM_RMS, il); |
100 | 0 | cb(cur, "attn_norm", il); |
101 | | |
102 | | // self_attention |
103 | 0 | { |
104 | 0 | ggml_tensor * q = NULL; |
105 | | // {n_embd, q_lora_rank} * {n_embd, n_tokens} -> {q_lora_rank, n_tokens} |
106 | 0 | q = ggml_mul_mat(ctx0, model.layers[il].wq_a, cur); |
107 | 0 | cb(q, "q", il); |
108 | |
|
109 | 0 | q = build_norm(q, |
110 | 0 | model.layers[il].attn_q_a_norm, NULL, |
111 | 0 | LLM_NORM_RMS, il); |
112 | 0 | cb(q, "q", il); |
113 | | |
114 | | // {q_lora_rank, n_head * hparams.n_embd_head_k()} * {q_lora_rank, n_tokens} -> {n_head * hparams.n_embd_head_k(), n_tokens} |
115 | 0 | q = ggml_mul_mat(ctx0, model.layers[il].wq_b, q); |
116 | 0 | cb(q, "q", il); |
117 | | |
118 | | // split into {n_head * n_embd_head_qk_nope, n_tokens} |
119 | 0 | ggml_tensor * q_nope = ggml_view_3d(ctx0, q, n_embd_head_qk_nope, n_head, n_tokens, |
120 | 0 | ggml_row_size(q->type, hparams.n_embd_head_k()), |
121 | 0 | ggml_row_size(q->type, hparams.n_embd_head_k() * n_head), |
122 | 0 | 0); |
123 | 0 | cb(q_nope, "q_nope", il); |
124 | | |
125 | | // and {n_head * n_embd_head_qk_rope, n_tokens} |
126 | 0 | ggml_tensor * q_pe = ggml_view_3d(ctx0, q, n_embd_head_qk_rope, n_head, n_tokens, |
127 | 0 | ggml_row_size(q->type, hparams.n_embd_head_k()), |
128 | 0 | ggml_row_size(q->type, hparams.n_embd_head_k() * n_head), |
129 | 0 | ggml_row_size(q->type, n_embd_head_qk_nope)); |
130 | 0 | cb(q_pe, "q_pe", il); |
131 | | |
132 | | // {n_embd, kv_lora_rank + n_embd_head_qk_rope} * {n_embd, n_tokens} -> {kv_lora_rank + n_embd_head_qk_rope, n_tokens} |
133 | 0 | ggml_tensor * kv_pe_compresseed = ggml_mul_mat(ctx0, model.layers[il].wkv_a_mqa, cur); |
134 | 0 | cb(kv_pe_compresseed, "kv_pe_compresseed", il); |
135 | | |
136 | | // split into {kv_lora_rank, n_tokens} |
137 | 0 | ggml_tensor * kv_compressed = ggml_view_2d(ctx0, kv_pe_compresseed, kv_lora_rank, n_tokens, |
138 | 0 | kv_pe_compresseed->nb[1], |
139 | 0 | 0); |
140 | 0 | cb(kv_compressed, "kv_compressed", il); |
141 | | |
142 | | // and {n_embd_head_qk_rope, n_tokens} |
143 | 0 | ggml_tensor * k_pe = ggml_view_3d(ctx0, kv_pe_compresseed, n_embd_head_qk_rope, 1, n_tokens, |
144 | 0 | kv_pe_compresseed->nb[1], |
145 | 0 | kv_pe_compresseed->nb[1], |
146 | 0 | ggml_row_size(kv_pe_compresseed->type, kv_lora_rank)); |
147 | 0 | cb(k_pe, "k_pe", il); |
148 | |
|
149 | 0 | kv_compressed = build_norm(kv_compressed, |
150 | 0 | model.layers[il].attn_kv_a_norm, NULL, |
151 | 0 | LLM_NORM_RMS, il); |
152 | 0 | cb(kv_compressed, "kv_compressed", il); |
153 | | |
154 | | // {kv_lora_rank, n_head * (n_embd_head_qk_nope + n_embd_head_v)} * {kv_lora_rank, n_tokens} -> {n_head * (n_embd_head_qk_nope + n_embd_head_v), n_tokens} |
155 | 0 | ggml_tensor * kv = ggml_mul_mat(ctx0, model.layers[il].wkv_b, kv_compressed); |
156 | 0 | cb(kv, "kv", il); |
157 | | |
158 | | // split into {n_head * n_embd_head_qk_nope, n_tokens} |
159 | 0 | ggml_tensor * k_nope = ggml_view_3d(ctx0, kv, n_embd_head_qk_nope, n_head, n_tokens, |
160 | 0 | ggml_row_size(kv->type, n_embd_head_qk_nope + hparams.n_embd_head_v()), |
161 | 0 | ggml_row_size(kv->type, n_head * (n_embd_head_qk_nope + hparams.n_embd_head_v())), |
162 | 0 | 0); |
163 | 0 | cb(k_nope, "k_nope", il); |
164 | | |
165 | | // and {n_head * n_embd_head_v, n_tokens} |
166 | 0 | ggml_tensor * v_states = ggml_view_3d(ctx0, kv, hparams.n_embd_head_v(), n_head, n_tokens, |
167 | 0 | ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v())), |
168 | 0 | ggml_row_size(kv->type, (n_embd_head_qk_nope + hparams.n_embd_head_v())*n_head), |
169 | 0 | ggml_row_size(kv->type, (n_embd_head_qk_nope))); |
170 | 0 | cb(v_states, "v_states", il); |
171 | |
|
172 | 0 | v_states = ggml_cont(ctx0, v_states); |
173 | 0 | cb(v_states, "v_states", il); |
174 | |
|
175 | 0 | q_pe = ggml_rope_ext( |
176 | 0 | ctx0, q_pe, inp_pos, rope_factors, |
177 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
178 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
179 | 0 | ); |
180 | 0 | cb(q_pe, "q_pe", il); |
181 | | |
182 | | // shared RoPE key |
183 | 0 | k_pe = ggml_rope_ext( |
184 | 0 | ctx0, k_pe, inp_pos, rope_factors, |
185 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
186 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
187 | 0 | ); |
188 | 0 | cb(k_pe, "k_pe", il); |
189 | |
|
190 | 0 | ggml_tensor * q_states = ggml_concat(ctx0, q_nope, q_pe, 0); |
191 | 0 | cb(q_states, "q_states", il); |
192 | |
|
193 | 0 | ggml_tensor * k_states = ggml_concat(ctx0, k_nope, ggml_repeat(ctx0, k_pe, q_pe), 0); |
194 | 0 | cb(k_states, "k_states", il); |
195 | |
|
196 | 0 | cur = build_attn(inp_attn, |
197 | 0 | model.layers[il].wo, NULL, model.layers[il].wo_s, |
198 | 0 | q_states, k_states, v_states, nullptr, nullptr, nullptr, kq_scale, il); |
199 | 0 | } |
200 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
201 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
202 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
203 | 0 | } |
204 | | // scale_res - scale the hidden states for residual connection |
205 | 0 | const float scale_res = scale_depth/sqrtf(float(n_layer)); // TODO: is this correct? |
206 | 0 | cur = ggml_scale(ctx0, cur, scale_res); |
207 | 0 | cb(cur, "hidden_scaled", il); |
208 | |
|
209 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
210 | 0 | cb(ffn_inp, "ffn_inp", il); |
211 | | |
212 | | // feed-forward network |
213 | 0 | { |
214 | 0 | cur = build_norm(ffn_inp, |
215 | 0 | model.layers[il].ffn_norm, NULL, |
216 | 0 | LLM_NORM_RMS, il); |
217 | 0 | cb(cur, "ffn_norm", il); |
218 | |
|
219 | 0 | cur = build_ffn(cur, |
220 | 0 | model.layers[il].ffn_up, NULL, NULL, |
221 | 0 | model.layers[il].ffn_gate, NULL, NULL, |
222 | 0 | model.layers[il].ffn_down, NULL, NULL, |
223 | 0 | NULL, |
224 | 0 | LLM_FFN_SILU, LLM_FFN_PAR, il); |
225 | 0 | cb(cur, "ffn_out", il); |
226 | 0 | } |
227 | | // scale the hidden states for residual connection |
228 | 0 | cur = ggml_scale(ctx0, cur, scale_res); |
229 | 0 | cb(cur, "hidden_scaled_ffn", il); |
230 | |
|
231 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
232 | |
|
233 | 0 | cur = build_cvec(cur, il); |
234 | 0 | cb(cur, "l_out", il); |
235 | | |
236 | | // input for next layer |
237 | 0 | inpL = cur; |
238 | 0 | } |
239 | 0 | cur = inpL; |
240 | |
|
241 | 0 | cur = build_norm(cur, |
242 | 0 | model.output_norm, NULL, |
243 | 0 | LLM_NORM_RMS, -1); |
244 | |
|
245 | 0 | cb(cur, "result_norm", -1); |
246 | 0 | res->t_embd = cur; |
247 | | |
248 | | // lm_head scaling |
249 | 0 | const float scale_lmhead = float(n_embd_base)/float(n_embd); |
250 | 0 | cur = ggml_scale(ctx0, cur, scale_lmhead); |
251 | 0 | cb(cur, "lmhead_scaling", -1); |
252 | | |
253 | | // lm_head |
254 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
255 | |
|
256 | 0 | cb(cur, "result_output", -1); |
257 | 0 | res->t_logits = cur; |
258 | |
|
259 | 0 | ggml_build_forward_expand(gf, cur); |
260 | 0 | } |