/src/llama.cpp/src/models/mellum.cpp
Line | Count | Source |
1 | | #include "models.h" |
2 | | |
3 | 0 | void llama_model_mellum::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_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
6 | 0 | ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); |
7 | |
|
8 | 0 | if (hparams.n_swa > 0) { |
9 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
10 | |
|
11 | 0 | uint32_t swa_period = 4; |
12 | 0 | const auto res = ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
13 | 0 | if (res) { |
14 | 0 | hparams.set_swa_pattern(swa_period); |
15 | 0 | } else { |
16 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, hparams.is_swa_impl, hparams.n_layer()); |
17 | 0 | } |
18 | |
|
19 | 0 | hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train; |
20 | 0 | hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train; |
21 | |
|
22 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
23 | 0 | } else { |
24 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_NONE; |
25 | 0 | } |
26 | |
|
27 | 0 | switch (hparams.n_layer()) { |
28 | 0 | case 28: type = LLM_TYPE_12B_A2_5B; break; |
29 | 0 | default: type = LLM_TYPE_UNKNOWN; |
30 | 0 | } |
31 | 0 | } |
32 | | |
33 | 0 | void llama_model_mellum::load_arch_tensors(llama_model_loader &) { |
34 | 0 | LLAMA_LOAD_LOCALS; |
35 | |
|
36 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
37 | | |
38 | | // output |
39 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
40 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
41 | |
|
42 | 0 | for (int i = 0; i < n_layer; ++i) { |
43 | 0 | auto & layer = layers[i]; |
44 | |
|
45 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
46 | |
|
47 | 0 | create_tensor_qkv(layer, i, n_embd, n_embd_head_k * n_head, n_embd_gqa, n_embd_gqa, 0); |
48 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
49 | |
|
50 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
51 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
52 | |
|
53 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
54 | |
|
55 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
56 | |
|
57 | 0 | if (n_expert == 0) { |
58 | 0 | throw std::runtime_error("n_expert must be > 0 for Mellum"); |
59 | 0 | } |
60 | 0 | if (n_expert_used == 0) { |
61 | 0 | throw std::runtime_error("n_expert_used must be > 0 for Mellum"); |
62 | 0 | } |
63 | | |
64 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used; |
65 | |
|
66 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
67 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0); |
68 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
69 | 0 | } |
70 | 0 | } |
71 | | |
72 | 0 | std::unique_ptr<llm_graph_context> llama_model_mellum::build_arch_graph(const llm_graph_params & params) const { |
73 | 0 | if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) { |
74 | 0 | return std::make_unique<graph<true>>(*this, params); |
75 | 0 | } |
76 | 0 | return std::make_unique<graph<false>>(*this, params); |
77 | 0 | } |
78 | | |
79 | | template <bool iswa> |
80 | 0 | llama_model_mellum::graph<iswa>::graph(const llama_model & model, const llm_graph_params & params) : llm_graph_context(params) { |
81 | 0 | const int64_t n_embd_head = hparams.n_embd_head_v(); |
82 | |
|
83 | 0 | GGML_ASSERT(n_embd_head == hparams.n_embd_head_k()); |
84 | 0 | GGML_ASSERT(n_embd_head == n_rot); |
85 | |
|
86 | 0 | ggml_tensor * cur; |
87 | 0 | ggml_tensor * inpL; |
88 | |
|
89 | 0 | inpL = build_inp_embd(model.tok_embd); |
90 | | |
91 | | // inp_pos - contains the positions |
92 | 0 | ggml_tensor * inp_pos = build_inp_pos(); |
93 | |
|
94 | 0 | using inp_attn_type = std::conditional_t<iswa, llm_graph_input_attn_kv_iswa, llm_graph_input_attn_kv>; |
95 | 0 | inp_attn_type * inp_attn = nullptr; |
96 | |
|
97 | 0 | if constexpr (iswa) { |
98 | 0 | inp_attn = build_attn_inp_kv_iswa(); |
99 | 0 | } else { |
100 | 0 | inp_attn = build_attn_inp_kv(); |
101 | 0 | } |
102 | |
|
103 | 0 | ggml_tensor * inp_out_ids = build_inp_out_ids(); |
104 | |
|
105 | 0 | for (int il = 0; il < n_layer; ++il) { |
106 | 0 | ggml_tensor * inpSA = inpL; |
107 | | |
108 | | // norm |
109 | 0 | cur = build_norm(inpL, |
110 | 0 | model.layers[il].attn_norm, nullptr, |
111 | 0 | LLM_NORM_RMS, il); |
112 | 0 | cb(cur, "attn_norm", il); |
113 | | |
114 | | // self_attention |
115 | 0 | { |
116 | | // compute Q and K and RoPE them |
117 | 0 | auto [Qcur, Kcur, Vcur] = build_qkv(model.layers[il], cur, |
118 | 0 | n_embd_head, n_head, n_head_kv, il); |
119 | |
|
120 | 0 | Qcur = build_norm(Qcur, model.layers[il].attn_q_norm, nullptr, LLM_NORM_RMS, il); |
121 | 0 | cb(Qcur, "Qcur_normed", il); |
122 | |
|
123 | 0 | Kcur = build_norm(Kcur, model.layers[il].attn_k_norm, nullptr, LLM_NORM_RMS, il); |
124 | 0 | cb(Kcur, "Kcur_normed", il); |
125 | |
|
126 | 0 | const bool is_swa = hparams.is_swa(il); |
127 | |
|
128 | 0 | if (is_swa) { |
129 | | // For sliding window layers, use regular rope with no yarn rope scaling. |
130 | | // This is achieved here by setting freq_scale and attn_factor to 1. |
131 | | // We also set ext_factor to 0 to avoid a few unnecessary computations. |
132 | 0 | Qcur = ggml_rope_ext( |
133 | 0 | ctx0, Qcur, inp_pos, nullptr, |
134 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, 1.0, |
135 | 0 | 0.0, 1.0, beta_fast, beta_slow |
136 | 0 | ); |
137 | |
|
138 | 0 | Kcur = ggml_rope_ext( |
139 | 0 | ctx0, Kcur, inp_pos, nullptr, |
140 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, 1.0, |
141 | 0 | 0.0, 1.0, beta_fast, beta_slow |
142 | 0 | ); |
143 | 0 | } else { |
144 | 0 | Qcur = ggml_rope_ext( |
145 | 0 | ctx0, Qcur, inp_pos, nullptr, |
146 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
147 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
148 | 0 | ); |
149 | |
|
150 | 0 | Kcur = ggml_rope_ext( |
151 | 0 | ctx0, Kcur, inp_pos, nullptr, |
152 | 0 | n_rot, rope_type, n_ctx_orig, freq_base, freq_scale, |
153 | 0 | ext_factor, attn_factor, beta_fast, beta_slow |
154 | 0 | ); |
155 | 0 | } |
156 | |
|
157 | 0 | cb(Qcur, "Qcur", il); |
158 | 0 | cb(Kcur, "Kcur", il); |
159 | 0 | cb(Vcur, "Vcur", il); |
160 | |
|
161 | 0 | cur = build_attn(inp_attn, |
162 | 0 | model.layers[il].wo, model.layers[il].wo_b, model.layers[il].wo_s, |
163 | 0 | Qcur, Kcur, Vcur, nullptr, nullptr, nullptr, 1.0f/sqrtf(float(n_embd_head)), il); |
164 | 0 | } |
165 | 0 | if (il == n_layer - 1 && inp_out_ids) { |
166 | 0 | cur = ggml_get_rows(ctx0, cur, inp_out_ids); |
167 | 0 | inpSA = ggml_get_rows(ctx0, inpSA, inp_out_ids); |
168 | 0 | } |
169 | 0 | ggml_tensor * ffn_inp = ggml_add(ctx0, cur, inpSA); |
170 | 0 | cb(ffn_inp, "ffn_inp", il); |
171 | | |
172 | | // MoE |
173 | 0 | cur = build_norm(ffn_inp, |
174 | 0 | model.layers[il].ffn_norm, nullptr, |
175 | 0 | LLM_NORM_RMS, il); |
176 | 0 | cb(cur, "ffn_norm", il); |
177 | |
|
178 | 0 | ggml_tensor * moe_out = |
179 | 0 | build_moe_ffn(cur, |
180 | 0 | model.layers[il].ffn_gate_inp, |
181 | 0 | model.layers[il].ffn_up_exps, |
182 | 0 | model.layers[il].ffn_gate_exps, |
183 | 0 | model.layers[il].ffn_down_exps, |
184 | 0 | nullptr, |
185 | 0 | n_expert, n_expert_used, |
186 | 0 | LLM_FFN_SILU, true, |
187 | 0 | hparams.expert_weights_scale, |
188 | 0 | LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX, |
189 | 0 | il, |
190 | 0 | nullptr, nullptr, |
191 | 0 | model.layers[il].ffn_up_exps_s, |
192 | 0 | model.layers[il].ffn_gate_exps_s, |
193 | 0 | model.layers[il].ffn_down_exps_s); |
194 | 0 | cb(moe_out, "ffn_moe_out", il); |
195 | 0 | cur = moe_out; |
196 | |
|
197 | 0 | cur = ggml_add(ctx0, cur, ffn_inp); |
198 | 0 | cb(cur, "ffn_out", il); |
199 | |
|
200 | 0 | cur = build_cvec(cur, il); |
201 | 0 | cb(cur, "l_out", il); |
202 | | |
203 | | // input for next layer |
204 | 0 | inpL = cur; |
205 | 0 | } |
206 | 0 | cur = inpL; |
207 | |
|
208 | 0 | cur = build_norm(cur, |
209 | 0 | model.output_norm, nullptr, |
210 | 0 | LLM_NORM_RMS, -1); |
211 | |
|
212 | 0 | cb(cur, "result_norm", -1); |
213 | 0 | res->t_embd = cur; |
214 | | |
215 | | // lm_head |
216 | 0 | cur = build_lora_mm(model.output, cur, model.output_s); |
217 | |
|
218 | 0 | cb(cur, "result_output", -1); |
219 | 0 | res->t_logits = cur; |
220 | |
|
221 | 0 | ggml_build_forward_expand(gf, cur); |
222 | 0 | } Unexecuted instantiation: llama_model_mellum::graph<false>::graph(llama_model const&, llm_graph_params const&) Unexecuted instantiation: llama_model_mellum::graph<true>::graph(llama_model const&, llm_graph_params const&) |
223 | | |
224 | | template struct llama_model_mellum::graph<false>; |
225 | | template struct llama_model_mellum::graph<true>; |