/src/llama.cpp/src/llama-model.cpp
Line | Count | Source |
1 | | #include "llama-model.h" |
2 | | |
3 | | #include "ggml.h" |
4 | | #include "llama-impl.h" |
5 | | #include "llama-mmap.h" |
6 | | #include "llama-cparams.h" |
7 | | #include "llama-model-loader.h" |
8 | | |
9 | | #include "llama-kv-cache.h" |
10 | | #include "llama-kv-cache-iswa.h" |
11 | | #include "llama-memory-hybrid.h" |
12 | | #include "llama-memory-hybrid-iswa.h" |
13 | | #include "llama-memory-recurrent.h" |
14 | | |
15 | | #include "ggml-cpp.h" |
16 | | |
17 | | #include "models/models.h" |
18 | | |
19 | | #include <algorithm> |
20 | | #include <cassert> |
21 | | #include <cfloat> |
22 | | #include <cstdint> |
23 | | #include <cstring> |
24 | | #include <cmath> |
25 | | #include <functional> |
26 | | #include <map> |
27 | | #include <regex> |
28 | | #include <sstream> |
29 | | #include <stdexcept> |
30 | | |
31 | 0 | const char * llm_type_name(llm_type type) { |
32 | 0 | switch (type) { |
33 | 0 | case LLM_TYPE_14M: return "14M"; |
34 | 0 | case LLM_TYPE_17M: return "17M"; |
35 | 0 | case LLM_TYPE_22M: return "22M"; |
36 | 0 | case LLM_TYPE_33M: return "33M"; |
37 | 0 | case LLM_TYPE_47M: return "47M"; |
38 | 0 | case LLM_TYPE_60M: return "60M"; |
39 | 0 | case LLM_TYPE_70M: return "70M"; |
40 | 0 | case LLM_TYPE_80M: return "80M"; |
41 | 0 | case LLM_TYPE_109M: return "109M"; |
42 | 0 | case LLM_TYPE_137M: return "137M"; |
43 | 0 | case LLM_TYPE_140M: return "140M"; |
44 | 0 | case LLM_TYPE_149M: return "149M"; |
45 | 0 | case LLM_TYPE_160M: return "160M"; |
46 | 0 | case LLM_TYPE_190M: return "190M"; |
47 | 0 | case LLM_TYPE_220M: return "220M"; |
48 | 0 | case LLM_TYPE_250M: return "250M"; |
49 | 0 | case LLM_TYPE_256M: return "256M"; |
50 | 0 | case LLM_TYPE_270M: return "270M"; |
51 | 0 | case LLM_TYPE_335M: return "335M"; |
52 | 0 | case LLM_TYPE_350M: return "350M"; |
53 | 0 | case LLM_TYPE_360M: return "360M"; |
54 | 0 | case LLM_TYPE_395M: return "395M"; |
55 | 0 | case LLM_TYPE_410M: return "410M"; |
56 | 0 | case LLM_TYPE_450M: return "450M"; |
57 | 0 | case LLM_TYPE_475M: return "475M"; |
58 | 0 | case LLM_TYPE_558M: return "558M"; |
59 | 0 | case LLM_TYPE_700M: return "700M"; |
60 | 0 | case LLM_TYPE_770M: return "770M"; |
61 | 0 | case LLM_TYPE_780M: return "780M"; |
62 | 0 | case LLM_TYPE_950M: return "950M"; |
63 | 0 | case LLM_TYPE_0_3B: return "0.3B"; |
64 | 0 | case LLM_TYPE_0_5B: return "0.5B"; |
65 | 0 | case LLM_TYPE_0_6B: return "0.6B"; |
66 | 0 | case LLM_TYPE_0_8B: return "0.8B"; |
67 | 0 | case LLM_TYPE_1B: return "1B"; |
68 | 0 | case LLM_TYPE_1_2B: return "1.2B"; |
69 | 0 | case LLM_TYPE_1_3B: return "1.3B"; |
70 | 0 | case LLM_TYPE_1_4B: return "1.4B"; |
71 | 0 | case LLM_TYPE_1_5B: return "1.5B"; |
72 | 0 | case LLM_TYPE_1_6B: return "1.6B"; |
73 | 0 | case LLM_TYPE_1_7B: return "1.7B"; |
74 | 0 | case LLM_TYPE_1_8B: return "1.8B"; |
75 | 0 | case LLM_TYPE_2B: return "2B"; |
76 | 0 | case LLM_TYPE_2_6B: return "2.6B"; |
77 | 0 | case LLM_TYPE_2_8B: return "2.8B"; |
78 | 0 | case LLM_TYPE_2_9B: return "2.9B"; |
79 | 0 | case LLM_TYPE_3B: return "3B"; |
80 | 0 | case LLM_TYPE_4B: return "4B"; |
81 | 0 | case LLM_TYPE_6B: return "6B"; |
82 | 0 | case LLM_TYPE_6_9B: return "6.9B"; |
83 | 0 | case LLM_TYPE_7B: return "7B"; |
84 | 0 | case LLM_TYPE_8B: return "8B"; |
85 | 0 | case LLM_TYPE_9B: return "9B"; |
86 | 0 | case LLM_TYPE_11B: return "11B"; |
87 | 0 | case LLM_TYPE_12B: return "12B"; |
88 | 0 | case LLM_TYPE_13B: return "13B"; |
89 | 0 | case LLM_TYPE_14B: return "14B"; |
90 | 0 | case LLM_TYPE_15B: return "15B"; |
91 | 0 | case LLM_TYPE_16B: return "16B"; |
92 | 0 | case LLM_TYPE_20B: return "20B"; |
93 | 0 | case LLM_TYPE_26B: return "26B"; |
94 | 0 | case LLM_TYPE_27B: return "27B"; |
95 | 0 | case LLM_TYPE_30B: return "30B"; |
96 | 0 | case LLM_TYPE_32B: return "32B"; |
97 | 0 | case LLM_TYPE_34B: return "34B"; |
98 | 0 | case LLM_TYPE_35B: return "35B"; |
99 | 0 | case LLM_TYPE_36B: return "36B"; |
100 | 0 | case LLM_TYPE_40B: return "40B"; |
101 | 0 | case LLM_TYPE_65B: return "65B"; |
102 | 0 | case LLM_TYPE_70B: return "70B"; |
103 | 0 | case LLM_TYPE_120B: return "120B"; |
104 | 0 | case LLM_TYPE_142B: return "142B"; |
105 | 0 | case LLM_TYPE_236B: return "236B"; |
106 | 0 | case LLM_TYPE_290B: return "290B"; |
107 | 0 | case LLM_TYPE_314B: return "314B"; |
108 | 0 | case LLM_TYPE_405B: return "405B"; |
109 | 0 | case LLM_TYPE_671B: return "671B"; |
110 | 0 | case LLM_TYPE_SMALL: return "0.1B"; |
111 | 0 | case LLM_TYPE_MEDIUM: return "0.4B"; |
112 | 0 | case LLM_TYPE_LARGE: return "0.8B"; |
113 | 0 | case LLM_TYPE_XL: return "1.5B"; |
114 | 0 | case LLM_TYPE_A1_7B: return "A1.7B"; |
115 | 0 | case LLM_TYPE_A2_7B: return "A2.7B"; |
116 | 0 | case LLM_TYPE_8x7B: return "8x7B"; |
117 | 0 | case LLM_TYPE_8x22B: return "8x22B"; |
118 | 0 | case LLM_TYPE_16x12B: return "16x12B"; |
119 | 0 | case LLM_TYPE_16x3_8B: return "16x3.8B"; |
120 | 0 | case LLM_TYPE_10B_128x3_66B: return "10B+128x3.66B"; |
121 | 0 | case LLM_TYPE_57B_A14B: return "57B.A14B"; |
122 | 0 | case LLM_TYPE_17B_16E: return "17Bx16E (Scout)"; |
123 | 0 | case LLM_TYPE_17B_128E: return "17Bx128E (Maverick)"; |
124 | 0 | case LLM_TYPE_A13B: return "A13B"; |
125 | 0 | case LLM_TYPE_7B_A1B: return "7B.A1B"; |
126 | 0 | case LLM_TYPE_8B_A1B: return "8B.A1B"; |
127 | 0 | case LLM_TYPE_16B_A1B: return "16B.A1B"; |
128 | 0 | case LLM_TYPE_21B_A3B: return "21B.A3B"; |
129 | 0 | case LLM_TYPE_24B_A2B: return "24B.A2B"; |
130 | 0 | case LLM_TYPE_30B_A3B: return "30B.A3B"; |
131 | 0 | case LLM_TYPE_31B_A3_5B: return "31B.A3.5B"; |
132 | 0 | case LLM_TYPE_35B_A3B: return "35B.A3B"; |
133 | 0 | case LLM_TYPE_48B_A3B: return "48B.A3B"; |
134 | 0 | case LLM_TYPE_80B_A3B: return "80B.A3B"; |
135 | 0 | case LLM_TYPE_100B_A6B: return "100B.A6B"; |
136 | 0 | case LLM_TYPE_102B_A12B: return "102B.A12B"; |
137 | 0 | case LLM_TYPE_106B_A12B: return "106B.A12B"; |
138 | 0 | case LLM_TYPE_120B_A12B: return "120B.A12B"; |
139 | 0 | case LLM_TYPE_122B_A10B: return "122B.A10B"; |
140 | 0 | case LLM_TYPE_196B_A11B: return "196B.A11B"; |
141 | 0 | case LLM_TYPE_230B_A10B: return "230B.A10B"; |
142 | 0 | case LLM_TYPE_235B_A22B: return "235B.A22B"; |
143 | 0 | case LLM_TYPE_300B_A47B: return "300B.A47B"; |
144 | 0 | case LLM_TYPE_310B_A15B: return "310B.A15B"; |
145 | 0 | case LLM_TYPE_355B_A32B: return "355B.A32B"; |
146 | 0 | case LLM_TYPE_397B_A17B: return "397B.A17B"; |
147 | 0 | case LLM_TYPE_744B_A40B: return "744B.A40B"; |
148 | 0 | case LLM_TYPE_E2B: return "E2B"; |
149 | 0 | case LLM_TYPE_E4B: return "E4B"; |
150 | 0 | default: return "?B"; |
151 | 0 | } |
152 | 0 | } |
153 | | |
154 | 0 | static const char * llama_expert_gating_func_name(llama_expert_gating_func_type type) { |
155 | 0 | switch (type) { |
156 | 0 | case LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX: return "softmax"; |
157 | 0 | case LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID: return "sigmoid"; |
158 | 0 | default: return "unknown"; |
159 | 0 | } |
160 | 0 | } |
161 | | |
162 | | static const std::map<llama_rope_scaling_type, const char *> LLAMA_ROPE_SCALING_TYPES = { |
163 | | { LLAMA_ROPE_SCALING_TYPE_NONE, "none" }, |
164 | | { LLAMA_ROPE_SCALING_TYPE_LINEAR, "linear" }, |
165 | | { LLAMA_ROPE_SCALING_TYPE_YARN, "yarn" }, |
166 | | { LLAMA_ROPE_SCALING_TYPE_LONGROPE, "longrope" }, |
167 | | }; |
168 | | |
169 | 0 | std::string llama_rope_scaling_type_name(llama_rope_scaling_type rope_scaling_type) { |
170 | 0 | return LLAMA_ROPE_SCALING_TYPES.at(rope_scaling_type); |
171 | 0 | } |
172 | | |
173 | 0 | static llama_rope_scaling_type llama_rope_scaling_type_from_string(const std::string & name) { |
174 | 0 | for (const auto & kv : LLAMA_ROPE_SCALING_TYPES) { |
175 | 0 | if (kv.second == name) { |
176 | 0 | return (llama_rope_scaling_type) kv.first; |
177 | 0 | } |
178 | 0 | } |
179 | | |
180 | 0 | return LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED; |
181 | 0 | } |
182 | | |
183 | | // CPU: ACCEL -> GPU host -> CPU extra -> CPU |
184 | 0 | static buft_list_t make_cpu_buft_list(const std::vector<ggml_backend_dev_t> & devices, bool use_extra_bufts, bool no_host) { |
185 | 0 | buft_list_t buft_list; |
186 | | |
187 | | // add ACCEL buffer types |
188 | 0 | for (size_t i = 0; i < ggml_backend_dev_count(); ++i) { |
189 | 0 | ggml_backend_dev_t dev = ggml_backend_dev_get(i); |
190 | 0 | if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_ACCEL) { |
191 | 0 | auto * buft = ggml_backend_dev_buffer_type(dev); |
192 | | // skip |
193 | 0 | if (buft != ggml_backend_cpu_buffer_type()) { |
194 | 0 | buft_list.emplace_back(dev, buft); |
195 | 0 | } |
196 | 0 | } |
197 | 0 | } |
198 | | |
199 | | // add a host buffer type |
200 | | // storing the tensors in a host buffer is useful when the processing of large batches |
201 | | // is offloaded to a GPU device, since it reduces the time spent on data transfers |
202 | | // generally, this will be done using the first device in the list |
203 | | // a better approach would be to handle this on a weight-by-weight basis using the offload_op |
204 | | // function of the device to determine if it would benefit from being stored in a host buffer |
205 | 0 | if (!no_host) { |
206 | 0 | for (auto * dev : devices) { |
207 | 0 | ggml_backend_buffer_type_t buft = ggml_backend_dev_host_buffer_type(dev); |
208 | 0 | if (buft) { |
209 | 0 | buft_list.emplace_back(dev, buft); |
210 | 0 | break; |
211 | 0 | } |
212 | 0 | } |
213 | 0 | } |
214 | | |
215 | | // add extra buffer types |
216 | 0 | if (use_extra_bufts) { |
217 | 0 | auto * cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU); |
218 | 0 | if (cpu_dev == nullptr) { |
219 | 0 | throw std::runtime_error(format("%s: no CPU backend found", __func__)); |
220 | 0 | } |
221 | | |
222 | 0 | auto * cpu_reg = ggml_backend_dev_backend_reg(cpu_dev); |
223 | 0 | auto ggml_backend_dev_get_extra_bufts_fn = (ggml_backend_dev_get_extra_bufts_t) |
224 | 0 | ggml_backend_reg_get_proc_address(cpu_reg, "ggml_backend_dev_get_extra_bufts"); |
225 | 0 | if (ggml_backend_dev_get_extra_bufts_fn) { |
226 | 0 | ggml_backend_buffer_type_t * extra_bufts = ggml_backend_dev_get_extra_bufts_fn(cpu_dev); |
227 | 0 | while (extra_bufts && *extra_bufts) { |
228 | 0 | buft_list.emplace_back(cpu_dev, *extra_bufts); |
229 | 0 | ++extra_bufts; |
230 | 0 | } |
231 | 0 | } |
232 | 0 | } |
233 | | |
234 | | // add the CPU buffer type |
235 | 0 | for (size_t i = 0; i < ggml_backend_dev_count(); ++i) { |
236 | 0 | ggml_backend_dev_t dev = ggml_backend_dev_get(i); |
237 | 0 | if (ggml_backend_dev_type(dev) == GGML_BACKEND_DEVICE_TYPE_CPU) { |
238 | 0 | buft_list.emplace_back(dev, ggml_backend_dev_buffer_type(dev)); |
239 | 0 | } |
240 | 0 | } |
241 | |
|
242 | 0 | return buft_list; |
243 | 0 | } |
244 | | |
245 | | // GPU: split if LLAMA_SPLIT_MODE_ROW -> GPU |
246 | 0 | static buft_list_t make_gpu_buft_list(ggml_backend_dev_t dev, llama_split_mode split_mode, const float * tensor_split) { |
247 | 0 | buft_list_t buft_list; |
248 | | |
249 | | // add the device split buffer type if requested and available |
250 | 0 | if (split_mode == LLAMA_SPLIT_MODE_ROW) { |
251 | 0 | ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(dev); |
252 | 0 | auto ggml_backend_split_buffer_type_fn = (ggml_backend_split_buffer_type_t) |
253 | 0 | ggml_backend_reg_get_proc_address(reg, "ggml_backend_split_buffer_type"); |
254 | 0 | if (ggml_backend_split_buffer_type_fn) { |
255 | 0 | size_t dev_index = [&]() { |
256 | 0 | auto * reg = ggml_backend_dev_backend_reg(dev); |
257 | 0 | for (size_t i = 0; i < ggml_backend_reg_dev_count(reg); ++i) { |
258 | 0 | if (ggml_backend_reg_dev_get(reg, i) == dev) { |
259 | 0 | return i; |
260 | 0 | } |
261 | 0 | } |
262 | 0 | throw std::runtime_error(format("device %s not found in its backend reg", ggml_backend_dev_name(dev))); |
263 | 0 | }(); |
264 | 0 | auto * buft = ggml_backend_split_buffer_type_fn(dev_index, tensor_split); |
265 | 0 | if (buft != nullptr) { |
266 | 0 | buft_list.emplace_back(dev, buft); |
267 | 0 | } |
268 | 0 | } |
269 | 0 | } |
270 | | |
271 | | // add the device default buffer type |
272 | 0 | buft_list.emplace_back(dev, ggml_backend_dev_buffer_type(dev)); |
273 | | |
274 | | // add the device extra buffer type (if any) |
275 | 0 | ggml_backend_reg_t reg = ggml_backend_dev_backend_reg(dev); |
276 | 0 | auto ggml_backend_dev_get_extra_bufts_fn = (ggml_backend_dev_get_extra_bufts_t) |
277 | 0 | ggml_backend_reg_get_proc_address(reg, "ggml_backend_dev_get_extra_bufts"); |
278 | |
|
279 | 0 | if (ggml_backend_dev_get_extra_bufts_fn) { |
280 | 0 | ggml_backend_buffer_type_t * extra_bufts = ggml_backend_dev_get_extra_bufts_fn(dev); |
281 | 0 | while (extra_bufts && *extra_bufts) { |
282 | 0 | buft_list.emplace_back(dev, *extra_bufts); |
283 | 0 | ++extra_bufts; |
284 | 0 | } |
285 | 0 | } |
286 | |
|
287 | 0 | return buft_list; |
288 | 0 | } |
289 | | |
290 | | struct llama_model::impl { |
291 | 0 | impl() = default; |
292 | 0 | ~impl() = default; |
293 | | |
294 | | uint64_t n_elements = 0; |
295 | | |
296 | | size_t n_bytes = 0; |
297 | | |
298 | | std::string desc_str; |
299 | | |
300 | | // model memory mapped files |
301 | | llama_mmaps mappings; |
302 | | |
303 | | // objects representing data potentially being locked in memory |
304 | | llama_mlocks mlock_bufs; |
305 | | llama_mlocks mlock_mmaps; |
306 | | |
307 | | // contexts where the model tensors metadata is stored as well as the corresponding buffers: |
308 | | std::vector<std::pair<ggml_context_ptr, std::vector<ggml_backend_buffer_ptr>>> ctxs_bufs; |
309 | | |
310 | | buft_list_t cpu_buft_list; |
311 | | std::map<ggml_backend_dev_t, buft_list_t> gpu_buft_list; |
312 | | |
313 | | struct layer_dev { |
314 | | ggml_backend_dev_t dev; |
315 | | buft_list_t * buft_list; |
316 | | }; |
317 | | |
318 | | layer_dev dev_input = {}; |
319 | | layer_dev dev_output = {}; |
320 | | std::vector<layer_dev> dev_layer; |
321 | | |
322 | | bool has_tensor_overrides; |
323 | | }; |
324 | | |
325 | 0 | llama_model::llama_model(const llama_model_params & params) : params(params), pimpl(std::make_unique<impl>()) { |
326 | 0 | pimpl->has_tensor_overrides = params.tensor_buft_overrides && params.tensor_buft_overrides[0].pattern; |
327 | 0 | } |
328 | | |
329 | 0 | llama_model::~llama_model() { |
330 | 0 | for (auto * lora : loras) { |
331 | 0 | delete lora; |
332 | 0 | } |
333 | 0 | } |
334 | | |
335 | 0 | void llama_model::load_stats(llama_model_loader & ml) { |
336 | 0 | pimpl->n_elements = ml.n_elements; |
337 | 0 | pimpl->n_bytes = ml.n_bytes; |
338 | 0 | } |
339 | | |
340 | 0 | void llama_model::load_arch(llama_model_loader & ml) { |
341 | 0 | arch = ml.get_arch(); |
342 | 0 | if (arch == LLM_ARCH_UNKNOWN) { |
343 | 0 | throw std::runtime_error("unknown model architecture: '" + ml.get_arch_name() + "'"); |
344 | 0 | } |
345 | 0 | } |
346 | | |
347 | 0 | void llama_model::load_hparams(llama_model_loader & ml) { |
348 | 0 | const gguf_context * ctx = ml.metadata; |
349 | | |
350 | | // get metadata as string |
351 | 0 | for (int i = 0; i < gguf_get_n_kv(ctx); i++) { |
352 | 0 | gguf_type type = gguf_get_kv_type(ctx, i); |
353 | 0 | if (type == GGUF_TYPE_ARRAY) { |
354 | 0 | continue; |
355 | 0 | } |
356 | 0 | const char * name = gguf_get_key(ctx, i); |
357 | 0 | const std::string value = gguf_kv_to_str(ctx, i); |
358 | 0 | gguf_kv.emplace(name, value); |
359 | 0 | } |
360 | | |
361 | | // get general kv |
362 | 0 | ml.get_key(LLM_KV_GENERAL_NAME, name, false); |
363 | | |
364 | | // everything past this point is not vocab-related |
365 | | // for CLIP models, we only need to load tensors, no hparams |
366 | 0 | if (hparams.vocab_only || ml.get_arch() == LLM_ARCH_CLIP) { |
367 | 0 | return; |
368 | 0 | } |
369 | | |
370 | 0 | ml.get_key(LLM_KV_CONTEXT_LENGTH, hparams.n_ctx_train); |
371 | 0 | ml.get_key(LLM_KV_EMBEDDING_LENGTH, hparams.n_embd); |
372 | 0 | ml.get_key(LLM_KV_EMBEDDING_LENGTH_OUT, hparams.n_embd_out_impl, false); |
373 | 0 | ml.get_key(LLM_KV_BLOCK_COUNT, hparams.n_layer); |
374 | 0 | ml.get_key(LLM_KV_EXPERT_COUNT, hparams.n_expert, false); |
375 | 0 | ml.get_key(LLM_KV_EXPERT_USED_COUNT, hparams.n_expert_used, false); |
376 | 0 | ml.get_key(LLM_KV_EXPERT_GROUP_COUNT, hparams.n_expert_groups, false); |
377 | 0 | ml.get_key(LLM_KV_EXPERT_GROUP_USED_COUNT, hparams.n_group_used, false); |
378 | |
|
379 | 0 | if (arch == LLM_ARCH_WAVTOKENIZER_DEC) { |
380 | 0 | ml.get_key(LLM_KV_FEATURES_LENGTH, hparams.n_embd); |
381 | 0 | ml.get_key(LLM_KV_EMBEDDING_LENGTH, hparams.n_embd_out_impl); |
382 | |
|
383 | 0 | ml.get_key(LLM_KV_POSNET_EMBEDDING_LENGTH, hparams.posnet.n_embd); |
384 | 0 | ml.get_key(LLM_KV_POSNET_BLOCK_COUNT, hparams.posnet.n_layer); |
385 | |
|
386 | 0 | ml.get_key(LLM_KV_CONVNEXT_EMBEDDING_LENGTH, hparams.convnext.n_embd); |
387 | 0 | ml.get_key(LLM_KV_CONVNEXT_BLOCK_COUNT, hparams.convnext.n_layer); |
388 | 0 | } |
389 | |
|
390 | 0 | GGML_ASSERT(hparams.n_expert <= LLAMA_MAX_EXPERTS); |
391 | 0 | GGML_ASSERT(hparams.n_expert_used <= hparams.n_expert); |
392 | 0 | if (hparams.n_expert > 0) { |
393 | 0 | GGML_ASSERT(hparams.n_expert_used > 0); |
394 | 0 | GGML_ASSERT(hparams.n_expert_groups < hparams.n_expert); |
395 | 0 | if (hparams.n_expert_groups > 1) { |
396 | 0 | GGML_ASSERT(hparams.n_expert % hparams.n_expert_groups == 0); |
397 | 0 | GGML_ASSERT(hparams.n_group_used > 0); |
398 | 0 | GGML_ASSERT(hparams.n_group_used < hparams.n_expert_groups); |
399 | 0 | } |
400 | 0 | } else { |
401 | 0 | GGML_ASSERT(hparams.n_expert_used == 0); |
402 | 0 | GGML_ASSERT(hparams.n_expert_groups == 0); |
403 | 0 | } |
404 | |
|
405 | 0 | std::fill(hparams.n_head_arr.begin(), hparams.n_head_arr.end(), 0); |
406 | 0 | std::fill(hparams.n_head_kv_arr.begin(), hparams.n_head_kv_arr.end(), 0); |
407 | 0 | std::fill(hparams.n_ff_arr.begin(), hparams.n_ff_arr.end(), 0); |
408 | 0 | std::fill( |
409 | 0 | hparams.recurrent_layer_arr.begin(), |
410 | 0 | hparams.recurrent_layer_arr.end(), |
411 | 0 | llm_arch_is_recurrent(ml.get_arch())); |
412 | |
|
413 | 0 | std::fill(hparams.rope_sections.begin(), hparams.rope_sections.end(), 0); |
414 | 0 | std::fill(hparams.swa_layers.begin(), hparams.swa_layers.end(), 0); |
415 | |
|
416 | 0 | std::fill(hparams.xielu_alpha_n.begin(), hparams.xielu_alpha_n.end(), 0.0f); |
417 | 0 | std::fill(hparams.xielu_alpha_p.begin(), hparams.xielu_alpha_p.end(), 0.0f); |
418 | 0 | std::fill(hparams.xielu_beta.begin(), hparams.xielu_beta.end(), 0.0f); |
419 | 0 | std::fill(hparams.xielu_eps.begin(), hparams.xielu_eps.end(), 0.0f); |
420 | 0 | std::fill(hparams.swiglu_clamp_exp.begin(), hparams.swiglu_clamp_exp.end(), 0.0f); |
421 | 0 | std::fill(hparams.swiglu_clamp_shexp.begin(), hparams.swiglu_clamp_shexp.end(), 0.0f); |
422 | |
|
423 | 0 | ml.get_key_or_arr(LLM_KV_FEED_FORWARD_LENGTH, hparams.n_ff_arr, hparams.n_layer, false); |
424 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_HEAD_COUNT, hparams.n_head_arr, hparams.n_layer, false); |
425 | | |
426 | | // n_head_kv is optional, default to n_head |
427 | 0 | hparams.n_head_kv_arr = hparams.n_head_arr; |
428 | |
|
429 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_HEAD_COUNT_KV, hparams.n_head_kv_arr, hparams.n_layer, false); |
430 | |
|
431 | 0 | bool rope_finetuned = false; |
432 | 0 | ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false); |
433 | 0 | hparams.rope_finetuned = rope_finetuned; |
434 | |
|
435 | 0 | hparams.n_ctx_orig_yarn = hparams.n_ctx_train; |
436 | 0 | ml.get_key(LLM_KV_ROPE_SCALING_ORIG_CTX_LEN, hparams.n_ctx_orig_yarn, false); |
437 | | |
438 | | // rope_freq_base (optional) |
439 | 0 | hparams.rope_freq_base_train = 10000.0f; |
440 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE, hparams.rope_freq_base_train, false); |
441 | |
|
442 | 0 | std::string rope_scaling("linear"); |
443 | 0 | ml.get_key(LLM_KV_ROPE_SCALING_TYPE, rope_scaling, false); |
444 | 0 | hparams.rope_scaling_type_train = llama_rope_scaling_type_from_string(rope_scaling); |
445 | 0 | GGML_ASSERT(hparams.rope_scaling_type_train != LLAMA_ROPE_SCALING_TYPE_UNSPECIFIED); |
446 | | |
447 | | // TODO: Handle SWA metadata similarly when models start implementing it |
448 | | // rope_freq_scale (inverse of the kv) is optional |
449 | 0 | float ropescale = 0.0f; |
450 | 0 | if (!ml.get_key(LLM_KV_ROPE_SCALING_FACTOR, ropescale, false)) { |
451 | | // try the old key name |
452 | 0 | ml.get_key(LLM_KV_ROPE_SCALE_LINEAR, ropescale, false); |
453 | 0 | } |
454 | 0 | hparams.rope_freq_scale_train = ropescale == 0.0f ? 1.0f : 1.0f/ropescale; |
455 | |
|
456 | 0 | ml.get_key(LLM_KV_ROPE_SCALING_ATTN_FACTOR, hparams.rope_attn_factor, false); |
457 | | |
458 | | // non-transformer models do not have attention heads |
459 | 0 | if (hparams.n_head() > 0) { |
460 | | // gpt-neox n_rot = rotary_pct * (n_embd / n_head) |
461 | | // gpt-j n_rot = rotary_dim |
462 | |
|
463 | 0 | hparams.n_embd_head_k_full = hparams.n_embd / hparams.n_head(); |
464 | 0 | ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH, hparams.n_embd_head_k_full, false); |
465 | |
|
466 | 0 | hparams.n_embd_head_v_full = hparams.n_embd / hparams.n_head(); |
467 | 0 | ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH, hparams.n_embd_head_v_full, false); |
468 | | |
469 | | // sanity check for n_rot (optional) |
470 | 0 | hparams.n_rot_full = hparams.n_embd_head_k_full; |
471 | |
|
472 | 0 | ml.get_key(LLM_KV_ROPE_DIMENSION_COUNT, hparams.n_rot_full, false); |
473 | |
|
474 | 0 | if (arch == LLM_ARCH_LLAMA || arch == LLM_ARCH_DECI || arch == LLM_ARCH_FALCON || arch == LLM_ARCH_LLAMA_EMBED) { |
475 | 0 | if (hparams.n_rot_full != hparams.n_embd_head_k_full) { |
476 | 0 | throw std::runtime_error(format("invalid n_rot: %u, expected %u", hparams.n_rot_full, hparams.n_embd_head_k_full)); |
477 | 0 | } |
478 | 0 | } |
479 | 0 | } else { |
480 | 0 | hparams.n_rot_full = 0; |
481 | 0 | hparams.n_embd_head_k_full = 0; |
482 | 0 | hparams.n_embd_head_v_full = 0; |
483 | 0 | } |
484 | | |
485 | | // head size and n_rot for SWA layers |
486 | 0 | { |
487 | 0 | hparams.n_embd_head_k_swa = hparams.n_embd_head_k_full; |
488 | 0 | hparams.n_embd_head_v_swa = hparams.n_embd_head_v_full; |
489 | 0 | ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH_SWA, hparams.n_embd_head_k_swa, false); |
490 | 0 | ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH_SWA, hparams.n_embd_head_v_swa, false); |
491 | |
|
492 | 0 | hparams.n_rot_swa = hparams.n_rot_full; |
493 | 0 | ml.get_key(LLM_KV_ROPE_DIMENSION_COUNT_SWA, hparams.n_rot_swa, false); |
494 | 0 | } |
495 | | |
496 | | // for differentiating model types |
497 | 0 | uint32_t n_vocab = 0; |
498 | 0 | ml.get_key(LLM_KV_VOCAB_SIZE, n_vocab, false) || ml.get_arr_n(LLM_KV_TOKENIZER_LIST, n_vocab, false); |
499 | | |
500 | | // for classifier models |
501 | 0 | ml.get_arr(LLM_KV_CLASSIFIER_OUTPUT_LABELS, classifier_labels, false); |
502 | 0 | if (!classifier_labels.empty()) { |
503 | 0 | hparams.n_cls_out = classifier_labels.size(); |
504 | 0 | } |
505 | | |
506 | | // arch-specific KVs |
507 | 0 | switch (arch) { |
508 | 0 | case LLM_ARCH_LLAMA: |
509 | 0 | case LLM_ARCH_LLAMA_EMBED: |
510 | 0 | { |
511 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
512 | |
|
513 | 0 | if (hparams.n_expert == 8) { |
514 | 0 | switch (hparams.n_layer) { |
515 | 0 | case 32: type = LLM_TYPE_8x7B; break; |
516 | 0 | case 56: type = LLM_TYPE_8x22B; break; |
517 | 0 | default: type = LLM_TYPE_UNKNOWN; |
518 | 0 | } |
519 | 0 | } else { |
520 | 0 | switch (hparams.n_layer) { |
521 | 0 | case 16: type = LLM_TYPE_1B; break; // Llama 3.2 1B |
522 | 0 | case 22: type = LLM_TYPE_1B; break; |
523 | 0 | case 26: type = LLM_TYPE_3B; break; |
524 | 0 | case 28: type = LLM_TYPE_3B; break; // Llama 3.2 3B |
525 | 0 | case 30: type = LLM_TYPE_256M; break; // smoldocling 256M |
526 | | // granite uses a vocab with len 49152 |
527 | 0 | case 32: type = n_vocab == 49152 ? LLM_TYPE_3B : (n_vocab < 40000 ? LLM_TYPE_7B : LLM_TYPE_8B); break; |
528 | 0 | case 36: type = LLM_TYPE_8B; break; // granite |
529 | 0 | case 40: type = LLM_TYPE_13B; break; |
530 | 0 | case 48: type = LLM_TYPE_34B; break; |
531 | 0 | case 60: type = LLM_TYPE_30B; break; |
532 | 0 | case 80: type = hparams.n_head() == hparams.n_head_kv() ? LLM_TYPE_65B : LLM_TYPE_70B; break; |
533 | 0 | default: type = LLM_TYPE_UNKNOWN; |
534 | 0 | } |
535 | 0 | } |
536 | 0 | } break; |
537 | 0 | case LLM_ARCH_LLAMA4: |
538 | 0 | { |
539 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
540 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
541 | 0 | ml.get_key(LLM_KV_INTERLEAVE_MOE_LAYER_STEP, hparams.n_moe_layer_step); |
542 | |
|
543 | 0 | const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); |
544 | 0 | if (found_swa && hparams.n_swa == 0) { |
545 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_NONE; |
546 | 0 | hparams.n_no_rope_layer_step = hparams.n_layer; // always use rope |
547 | 0 | } else { |
548 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_CHUNKED; |
549 | 0 | hparams.n_swa = 8192; |
550 | 0 | hparams.n_attn_temp_floor_scale = 8192; |
551 | 0 | hparams.f_attn_temp_scale = 0.1f; |
552 | 0 | hparams.f_attn_temp_offset = 1.0f; |
553 | 0 | uint32_t swa_period = 4; // pattern: 3 chunked - 1 full |
554 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
555 | 0 | hparams.set_swa_pattern(swa_period); |
556 | |
|
557 | 0 | hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train; |
558 | 0 | hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train; |
559 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
560 | 0 | } |
561 | |
|
562 | 0 | switch (hparams.n_expert) { |
563 | 0 | case 0: { |
564 | | // MobileLLM (no MoE) |
565 | 0 | switch (hparams.n_embd) { |
566 | 0 | case 2048: type = LLM_TYPE_140M; break; |
567 | 0 | case 4096: type = LLM_TYPE_360M; break; |
568 | 0 | case 6144: type = LLM_TYPE_950M; break; |
569 | 0 | default: type = LLM_TYPE_UNKNOWN; |
570 | 0 | } |
571 | 0 | } break; |
572 | 0 | case 16: type = LLM_TYPE_17B_16E; break; |
573 | 0 | case 128: type = LLM_TYPE_17B_128E; break; |
574 | 0 | default: type = LLM_TYPE_UNKNOWN; |
575 | 0 | } |
576 | | |
577 | 0 | hparams.use_kq_norm = type != LLM_TYPE_17B_128E; |
578 | 0 | } break; |
579 | 0 | case LLM_ARCH_ARCEE: |
580 | 0 | { |
581 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
582 | | |
583 | | // Arcee uses the same structure as Llama |
584 | 0 | switch (hparams.n_layer) { |
585 | 0 | case 36: type = LLM_TYPE_4B; break; |
586 | 0 | default: type = LLM_TYPE_UNKNOWN; |
587 | 0 | } |
588 | 0 | } break; |
589 | 0 | case LLM_ARCH_AFMOE: |
590 | 0 | { |
591 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
592 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
593 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
594 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared); |
595 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false); |
596 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
597 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false); |
598 | 0 | ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); |
599 | | |
600 | | // Set up interleaved sliding window attention (ISWA) |
601 | | // Pattern: 3 sliding - 1 full (global_attn_every_n_layers = 4) |
602 | 0 | if (hparams.n_swa > 0) { |
603 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
604 | 0 | uint32_t swa_period = 4; |
605 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
606 | 0 | hparams.set_swa_pattern(swa_period); |
607 | |
|
608 | 0 | hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train; |
609 | 0 | hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train; |
610 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
611 | 0 | } else { |
612 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_NONE; |
613 | 0 | } |
614 | | |
615 | | // Default to sigmoid if not set |
616 | 0 | if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) { |
617 | 0 | hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID; |
618 | 0 | } |
619 | |
|
620 | 0 | switch (hparams.n_layer) { |
621 | 0 | case 56: type = LLM_TYPE_6B; break; |
622 | 0 | case 32: type = LLM_TYPE_26B; break; |
623 | 0 | default: type = LLM_TYPE_UNKNOWN; |
624 | 0 | } |
625 | 0 | } break; |
626 | 0 | case LLM_ARCH_DECI: |
627 | 0 | { |
628 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
629 | 0 | switch (hparams.n_layer) { |
630 | 0 | case 32: type = LLM_TYPE_7B; break; |
631 | 0 | case 80: type = LLM_TYPE_70B; break; |
632 | 0 | case 162: type = LLM_TYPE_405B; break; |
633 | 0 | default: type = LLM_TYPE_UNKNOWN; |
634 | 0 | } |
635 | 0 | } break; |
636 | 0 | case LLM_ARCH_MINICPM: |
637 | 0 | { |
638 | | // Backward-compatible defaults for older MiniCPM GGUFs |
639 | 0 | hparams.f_embedding_scale = 12.0f; |
640 | 0 | hparams.f_residual_scale = 1.4f / sqrtf(float(hparams.n_layer)); |
641 | 0 | hparams.f_logit_scale = hparams.n_embd ? (256.0f / float(hparams.n_embd)) : 1.0f; |
642 | |
|
643 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
644 | | |
645 | | // Optional KV reads, override defaults if present in newer GGUF exports |
646 | 0 | ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale, /*required=*/false); |
647 | 0 | ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale, /*required=*/false); |
648 | 0 | ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale, /*required=*/false); |
649 | | |
650 | | // MiniCPM uses rope by default, unlike Granite which uses it as a switch |
651 | 0 | hparams.rope_finetuned = true; |
652 | |
|
653 | 0 | switch (hparams.n_layer) { |
654 | 0 | case 52: type = LLM_TYPE_1B; break; |
655 | 0 | case 40: type = LLM_TYPE_2B; break; |
656 | 0 | default: type = LLM_TYPE_UNKNOWN; |
657 | 0 | } |
658 | 0 | } break; |
659 | 0 | case LLM_ARCH_MINICPM3: |
660 | 0 | { |
661 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
662 | 0 | ml.get_key(LLM_KV_ATTENTION_Q_LORA_RANK, hparams.n_lora_q); |
663 | 0 | ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv); |
664 | |
|
665 | 0 | switch (hparams.n_layer) { |
666 | 0 | case 62: type = LLM_TYPE_4B; break; |
667 | 0 | default: type = LLM_TYPE_UNKNOWN; |
668 | 0 | } |
669 | 0 | } break; |
670 | 0 | case LLM_ARCH_GROK: |
671 | 0 | { |
672 | | // defaults for old GGUFs |
673 | 0 | hparams.yarn_beta_fast = 8.0f; |
674 | 0 | hparams.f_logit_scale = 0.5773502691896257f; |
675 | 0 | hparams.f_embedding_scale = 78.38367176906169f; |
676 | 0 | hparams.f_attn_out_scale = 0.08838834764831845f; |
677 | 0 | hparams.f_attn_logit_softcapping = 30.0f; |
678 | 0 | hparams.f_router_logit_softcapping = 30.0f; |
679 | | // no final_logit_softcapping in grok-1 |
680 | 0 | hparams.f_final_logit_softcapping = 0.0f; |
681 | |
|
682 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
683 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false); |
684 | 0 | ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale, false); |
685 | 0 | ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale, false); |
686 | 0 | ml.get_key(LLM_KV_ATTENTION_OUTPUT_SCALE, hparams.f_attn_out_scale, false); |
687 | 0 | ml.get_key(LLM_KV_ATTN_LOGIT_SOFTCAPPING, hparams.f_attn_logit_softcapping, false); |
688 | 0 | ml.get_key(LLM_KV_ROUTER_LOGIT_SOFTCAPPING, hparams.f_router_logit_softcapping, false); |
689 | 0 | ml.get_key(LLM_KV_FINAL_LOGIT_SOFTCAPPING, hparams.f_final_logit_softcapping, false); |
690 | |
|
691 | 0 | ml.get_key(LLM_KV_ATTENTION_TEMPERATURE_LENGTH, hparams.attn_temp_length, false); |
692 | 0 | ml.get_key(LLM_KV_ROPE_SCALING_YARN_EXT_FACTOR, hparams.yarn_ext_factor, false); |
693 | 0 | ml.get_key(LLM_KV_ROPE_SCALING_YARN_ATTN_FACTOR, hparams.yarn_attn_factor, false); |
694 | 0 | ml.get_key(LLM_KV_ROPE_SCALING_YARN_BETA_FAST, hparams.yarn_beta_fast, false); |
695 | 0 | ml.get_key(LLM_KV_ROPE_SCALING_YARN_BETA_SLOW, hparams.yarn_beta_slow, false); |
696 | |
|
697 | 0 | switch (hparams.n_layer) { |
698 | 0 | case 64: type = LLM_TYPE_314B; break; |
699 | 0 | default: type = LLM_TYPE_UNKNOWN; |
700 | 0 | } |
701 | 0 | } break; |
702 | 0 | case LLM_ARCH_FALCON: |
703 | 0 | { |
704 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
705 | |
|
706 | 0 | switch (hparams.n_layer) { |
707 | 0 | case 32: type = LLM_TYPE_7B; break; |
708 | 0 | case 60: type = LLM_TYPE_40B; break; |
709 | 0 | default: type = LLM_TYPE_UNKNOWN; |
710 | 0 | } |
711 | 0 | } break; |
712 | 0 | case LLM_ARCH_BAICHUAN: |
713 | 0 | { |
714 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
715 | 0 | switch (hparams.n_layer) { |
716 | 0 | case 32: type = LLM_TYPE_7B; break; |
717 | 0 | case 40: type = LLM_TYPE_13B; break; |
718 | 0 | default: type = LLM_TYPE_UNKNOWN; |
719 | 0 | } |
720 | | |
721 | 0 | if (type == LLM_TYPE_13B) { |
722 | | // TODO: become GGUF KV parameter |
723 | 0 | hparams.f_max_alibi_bias = 8.0f; |
724 | 0 | } |
725 | 0 | } break; |
726 | 0 | case LLM_ARCH_STARCODER: |
727 | 0 | { |
728 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
729 | 0 | switch (hparams.n_layer) { |
730 | 0 | case 24: type = LLM_TYPE_1B; break; |
731 | 0 | case 36: type = LLM_TYPE_3B; break; |
732 | 0 | case 42: type = LLM_TYPE_7B; break; |
733 | 0 | case 40: type = LLM_TYPE_15B; break; |
734 | 0 | default: type = LLM_TYPE_UNKNOWN; |
735 | 0 | } |
736 | 0 | } break; |
737 | 0 | case LLM_ARCH_REFACT: |
738 | 0 | { |
739 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
740 | 0 | switch (hparams.n_layer) { |
741 | 0 | case 32: type = LLM_TYPE_1B; break; |
742 | 0 | default: type = LLM_TYPE_UNKNOWN; |
743 | 0 | } |
744 | | |
745 | | // TODO: become GGUF KV parameter |
746 | 0 | hparams.f_max_alibi_bias = 8.0f; |
747 | 0 | } break; |
748 | 0 | case LLM_ARCH_BERT: |
749 | 0 | { |
750 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
751 | 0 | ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn, false); |
752 | 0 | ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false); |
753 | |
|
754 | 0 | switch (hparams.n_layer) { |
755 | 0 | case 3: |
756 | 0 | type = LLM_TYPE_17M; break; // bge-micro |
757 | 0 | case 6: |
758 | 0 | type = LLM_TYPE_22M; break; // MiniLM-L6 |
759 | 0 | case 12: |
760 | 0 | switch (hparams.n_embd) { |
761 | 0 | case 384: type = LLM_TYPE_33M; break; // MiniLM-L12, bge-small |
762 | 0 | case 768: type = LLM_TYPE_109M; break; // bge-base |
763 | 0 | default: type = LLM_TYPE_UNKNOWN; |
764 | 0 | } break; |
765 | 0 | case 24: |
766 | 0 | type = LLM_TYPE_335M; break; // bge-large |
767 | 0 | default: type = LLM_TYPE_UNKNOWN; |
768 | 0 | } |
769 | 0 | } break; |
770 | 0 | case LLM_ARCH_MODERN_BERT: |
771 | 0 | { |
772 | 0 | const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); |
773 | 0 | if (found_swa && hparams.n_swa > 0) { |
774 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_SYMMETRIC; |
775 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
776 | 0 | uint32_t swa_period = 3; |
777 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
778 | 0 | hparams.set_swa_pattern(swa_period, true); |
779 | 0 | } else { |
780 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_NONE; |
781 | 0 | } |
782 | |
|
783 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
784 | 0 | ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn, false); |
785 | 0 | ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false); |
786 | |
|
787 | 0 | switch (hparams.n_layer) { |
788 | 0 | case 12: |
789 | 0 | type = LLM_TYPE_47M; break; // granite-embedding-small |
790 | 0 | case 22: |
791 | 0 | type = LLM_TYPE_149M; break; // modern-bert-base |
792 | 0 | case 28: |
793 | 0 | type = LLM_TYPE_395M; break; // modern-bert-large |
794 | 0 | default: type = LLM_TYPE_UNKNOWN; |
795 | 0 | } |
796 | 0 | } break; |
797 | 0 | case LLM_ARCH_JINA_BERT_V2: |
798 | 0 | { |
799 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
800 | 0 | ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn, false); |
801 | 0 | ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false); |
802 | 0 | hparams.f_max_alibi_bias = 8.0f; |
803 | |
|
804 | 0 | switch (hparams.n_layer) { |
805 | 0 | case 4: type = LLM_TYPE_33M; break; // jina-embeddings-small |
806 | 0 | case 12: type = LLM_TYPE_137M; break; // jina-embeddings-base |
807 | 0 | default: type = LLM_TYPE_UNKNOWN; |
808 | 0 | } |
809 | 0 | } break; |
810 | 0 | case LLM_ARCH_JINA_BERT_V3: |
811 | 0 | { |
812 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
813 | 0 | ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn, false); |
814 | 0 | ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false); |
815 | |
|
816 | 0 | switch (hparams.n_layer) { |
817 | 0 | case 24: |
818 | 0 | type = LLM_TYPE_558M; break; |
819 | 0 | default: type = LLM_TYPE_UNKNOWN; |
820 | 0 | } |
821 | 0 | } break; |
822 | 0 | case LLM_ARCH_NOMIC_BERT: |
823 | 0 | case LLM_ARCH_NOMIC_BERT_MOE: |
824 | 0 | { |
825 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
826 | 0 | ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn, false); |
827 | 0 | ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false); |
828 | 0 | ml.get_key(LLM_KV_MOE_EVERY_N_LAYERS, hparams.moe_every_n_layers, 0); |
829 | |
|
830 | 0 | if (hparams.n_layer == 12 && hparams.n_embd == 768) { |
831 | 0 | if (arch == LLM_ARCH_NOMIC_BERT) { |
832 | 0 | type = LLM_TYPE_137M; |
833 | 0 | } else if (arch == LLM_ARCH_NOMIC_BERT_MOE && hparams.moe_every_n_layers == 2) { |
834 | 0 | type = LLM_TYPE_475M; |
835 | 0 | } |
836 | 0 | } |
837 | 0 | } break; |
838 | 0 | case LLM_ARCH_NEO_BERT: |
839 | 0 | { |
840 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
841 | 0 | ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn, false); |
842 | 0 | ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false); |
843 | |
|
844 | 0 | if (hparams.n_layer == 28) { |
845 | 0 | type = LLM_TYPE_250M; |
846 | 0 | } |
847 | 0 | } break; |
848 | 0 | case LLM_ARCH_EUROBERT: |
849 | 0 | { |
850 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
851 | 0 | ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn, false); |
852 | 0 | ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false); |
853 | |
|
854 | 0 | if (hparams.n_layer == 12) { |
855 | 0 | type = LLM_TYPE_SMALL; // 0.2B |
856 | 0 | } |
857 | 0 | } break; |
858 | 0 | case LLM_ARCH_BLOOM: |
859 | 0 | { |
860 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
861 | |
|
862 | 0 | switch (hparams.n_layer) { |
863 | 0 | case 24: type = LLM_TYPE_1B; break; |
864 | 0 | case 30: |
865 | 0 | switch (hparams.n_embd) { |
866 | 0 | case 2560: type = LLM_TYPE_3B; break; |
867 | 0 | case 4096: type = LLM_TYPE_7B; break; |
868 | 0 | default: type = LLM_TYPE_UNKNOWN; |
869 | 0 | } break; |
870 | 0 | default: type = LLM_TYPE_UNKNOWN; |
871 | 0 | } |
872 | | |
873 | | // TODO: become GGUF KV parameter |
874 | 0 | hparams.f_max_alibi_bias = 8.0f; |
875 | 0 | } break; |
876 | 0 | case LLM_ARCH_MPT: |
877 | 0 | { |
878 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
879 | 0 | ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv, false); |
880 | 0 | ml.get_key(LLM_KV_ATTENTION_MAX_ALIBI_BIAS, hparams.f_max_alibi_bias, false); |
881 | |
|
882 | 0 | switch (hparams.n_layer) { |
883 | 0 | case 32: type = LLM_TYPE_7B; break; |
884 | 0 | case 48: type = LLM_TYPE_30B; break; |
885 | 0 | default: type = LLM_TYPE_UNKNOWN; |
886 | 0 | } |
887 | 0 | } break; |
888 | 0 | case LLM_ARCH_STABLELM: |
889 | 0 | { |
890 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
891 | |
|
892 | 0 | switch (hparams.n_layer) { |
893 | 0 | case 24: type = LLM_TYPE_1B; break; |
894 | 0 | case 32: type = LLM_TYPE_3B; break; |
895 | 0 | case 40: type = LLM_TYPE_12B; break; |
896 | 0 | default: type = LLM_TYPE_UNKNOWN; |
897 | 0 | } |
898 | 0 | } break; |
899 | 0 | case LLM_ARCH_QWEN: |
900 | 0 | { |
901 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
902 | |
|
903 | 0 | switch (hparams.n_layer) { |
904 | 0 | case 32: type = LLM_TYPE_7B; break; |
905 | 0 | case 40: type = LLM_TYPE_13B; break; |
906 | 0 | default: type = LLM_TYPE_UNKNOWN; |
907 | 0 | } |
908 | 0 | } break; |
909 | 0 | case LLM_ARCH_QWEN2VL: |
910 | 0 | { |
911 | 0 | ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, true); |
912 | 0 | } |
913 | | // fall through |
914 | 0 | case LLM_ARCH_QWEN2: |
915 | 0 | { |
916 | 0 | ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false); |
917 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
918 | 0 | switch (hparams.n_layer) { |
919 | 0 | case 24: type = hparams.n_embd == 1024 ? LLM_TYPE_0_5B : LLM_TYPE_1B; break; |
920 | 0 | case 28: type = hparams.n_embd == 1536 ? LLM_TYPE_1_5B : LLM_TYPE_7B; break; |
921 | 0 | case 32: type = LLM_TYPE_7B; break; |
922 | 0 | case 36: type = LLM_TYPE_3B; break; |
923 | 0 | case 40: type = hparams.n_head() == 20 ? LLM_TYPE_4B : LLM_TYPE_13B; break; |
924 | 0 | case 48: type = LLM_TYPE_14B; break; |
925 | 0 | case 64: type = LLM_TYPE_32B; break; |
926 | 0 | case 80: type = LLM_TYPE_70B; break; |
927 | 0 | default: type = LLM_TYPE_UNKNOWN; |
928 | 0 | } |
929 | 0 | } break; |
930 | 0 | case LLM_ARCH_DREAM: |
931 | 0 | { |
932 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
933 | | // Dream models are primarily 7B with 28 layers |
934 | 0 | switch (hparams.n_layer) { |
935 | 0 | case 28: |
936 | 0 | type = LLM_TYPE_7B; |
937 | 0 | break; |
938 | 0 | default: |
939 | 0 | type = LLM_TYPE_UNKNOWN; |
940 | 0 | } |
941 | | // Set non-causal attention for diffusion models |
942 | 0 | hparams.causal_attn = false; |
943 | 0 | } |
944 | 0 | break; |
945 | 0 | case LLM_ARCH_LLADA: |
946 | 0 | { |
947 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
948 | | // LLaDA-8B has 32 layers, similar to LLaMA but for diffusion |
949 | 0 | switch (hparams.n_layer) { |
950 | 0 | case 32: |
951 | 0 | type = LLM_TYPE_8B; |
952 | 0 | break; |
953 | 0 | default: |
954 | 0 | type = LLM_TYPE_UNKNOWN; |
955 | 0 | } |
956 | | // Set non-causal attention for diffusion models |
957 | 0 | hparams.causal_attn = false; |
958 | 0 | } |
959 | 0 | break; |
960 | 0 | case LLM_ARCH_LLADA_MOE: |
961 | 0 | { |
962 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false); |
963 | |
|
964 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
965 | | // diffusion language model uses non-causal attention |
966 | 0 | hparams.causal_attn = false; |
967 | 0 | switch (hparams.n_layer) { |
968 | 0 | case 16: type = LLM_TYPE_A1_7B; break; |
969 | 0 | default: type = LLM_TYPE_UNKNOWN; |
970 | 0 | } |
971 | 0 | } break; |
972 | 0 | case LLM_ARCH_RND1: |
973 | 0 | { |
974 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false); |
975 | |
|
976 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
977 | 0 | switch (hparams.n_layer) { |
978 | 0 | case 48: type = LLM_TYPE_30B_A3B; break; |
979 | 0 | default: type = LLM_TYPE_UNKNOWN; |
980 | 0 | } |
981 | | // Set non-causal attention for diffusion models |
982 | 0 | hparams.causal_attn = false; |
983 | 0 | } break; |
984 | 0 | case LLM_ARCH_QWEN2MOE: |
985 | 0 | { |
986 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false); |
987 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false); |
988 | |
|
989 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
990 | 0 | switch (hparams.n_layer) { |
991 | 0 | case 24: type = LLM_TYPE_A2_7B; break; |
992 | 0 | case 28: type = LLM_TYPE_57B_A14B; break; |
993 | 0 | default: type = LLM_TYPE_UNKNOWN; |
994 | 0 | } |
995 | 0 | } break; |
996 | 0 | case LLM_ARCH_QWEN3: |
997 | 0 | { |
998 | 0 | ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false); |
999 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1000 | 0 | switch (hparams.n_layer) { |
1001 | 0 | case 28: type = hparams.n_embd == 1024 ? LLM_TYPE_0_6B : LLM_TYPE_1_7B; break; |
1002 | 0 | case 36: type = hparams.n_embd == 2560 ? LLM_TYPE_4B : LLM_TYPE_8B; break; |
1003 | 0 | case 40: type = LLM_TYPE_14B; break; |
1004 | 0 | case 64: type = LLM_TYPE_32B; break; |
1005 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1006 | 0 | } |
1007 | 0 | } break; |
1008 | 0 | case LLM_ARCH_MAINCODER: |
1009 | 0 | { |
1010 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1011 | 0 | switch (hparams.n_layer) { |
1012 | 0 | case 32: type = LLM_TYPE_1B; break; |
1013 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1014 | 0 | } |
1015 | 0 | } break; |
1016 | 0 | case LLM_ARCH_QWEN3VL: |
1017 | 0 | { |
1018 | 0 | ml.get_key(LLM_KV_NUM_DEEPSTACK_LAYERS, hparams.n_deepstack_layers, false); |
1019 | 0 | ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, true); |
1020 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1021 | 0 | switch (hparams.n_layer) { |
1022 | 0 | case 28: type = LLM_TYPE_1_7B; break; |
1023 | 0 | case 36: type = hparams.n_embd == 2560 ? LLM_TYPE_4B : LLM_TYPE_8B; break; |
1024 | 0 | case 64: type = LLM_TYPE_32B; break; |
1025 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1026 | 0 | } |
1027 | 0 | } break; |
1028 | 0 | case LLM_ARCH_QWEN3MOE: |
1029 | 0 | { |
1030 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false); |
1031 | |
|
1032 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1033 | 0 | switch (hparams.n_layer) { |
1034 | 0 | case 48: type = LLM_TYPE_30B_A3B; break; |
1035 | 0 | case 94: type = LLM_TYPE_235B_A22B; break; |
1036 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1037 | 0 | } |
1038 | 0 | } break; |
1039 | 0 | case LLM_ARCH_QWEN3VLMOE: |
1040 | 0 | { |
1041 | 0 | ml.get_key(LLM_KV_NUM_DEEPSTACK_LAYERS, hparams.n_deepstack_layers, false); |
1042 | 0 | ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, true); |
1043 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false); |
1044 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1045 | 0 | switch (hparams.n_layer) { |
1046 | 0 | case 48: type = LLM_TYPE_30B_A3B; break; |
1047 | 0 | case 94: type = LLM_TYPE_235B_A22B; break; |
1048 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1049 | 0 | } |
1050 | 0 | } break; |
1051 | 0 | case LLM_ARCH_PHI2: |
1052 | 0 | { |
1053 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
1054 | |
|
1055 | 0 | switch (hparams.n_layer) { |
1056 | 0 | case 24: type = LLM_TYPE_1B; break; |
1057 | 0 | case 32: type = LLM_TYPE_3B; break; |
1058 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1059 | 0 | } |
1060 | 0 | } break; |
1061 | 0 | case LLM_ARCH_PHI3: |
1062 | 0 | { |
1063 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1064 | |
|
1065 | 0 | switch (hparams.n_layer) { |
1066 | 0 | case 24: type = LLM_TYPE_1B; break; |
1067 | 0 | case 32: type = LLM_TYPE_3B; break; |
1068 | 0 | case 40: type = LLM_TYPE_14B; break; |
1069 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1070 | 0 | } |
1071 | | |
1072 | 0 | const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); |
1073 | |
|
1074 | 0 | if (found_swa && hparams.n_swa > 0) { |
1075 | 0 | LLAMA_LOG_WARN("%s: Phi SWA is currently disabled - results might be suboptimal for some models (see %s)\n", |
1076 | 0 | __func__, "https://github.com/ggml-org/llama.cpp/pull/13676"); |
1077 | | |
1078 | | // TODO: fix conversion scripts to correctly populate `n_swa` and `n_swa_pattern` |
1079 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_NONE; |
1080 | |
|
1081 | 0 | hparams.n_swa = 0; |
1082 | 0 | hparams.set_swa_pattern(1); |
1083 | 0 | } |
1084 | 0 | } break; |
1085 | 0 | case LLM_ARCH_PHIMOE: |
1086 | 0 | { |
1087 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1088 | |
|
1089 | 0 | switch (hparams.n_layer) { |
1090 | 0 | case 32: type = LLM_TYPE_16x3_8B; break; |
1091 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1092 | 0 | } |
1093 | 0 | } break; |
1094 | 0 | case LLM_ARCH_PLAMO: |
1095 | 0 | { |
1096 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1097 | |
|
1098 | 0 | switch (hparams.n_layer) { |
1099 | 0 | case 40: type = LLM_TYPE_13B; break; |
1100 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1101 | 0 | } |
1102 | 0 | } break; |
1103 | 0 | case LLM_ARCH_PLAMO2: |
1104 | 0 | { |
1105 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1106 | | |
1107 | | // Load Mamba SSM parameters |
1108 | 0 | ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv); |
1109 | 0 | ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner); |
1110 | 0 | ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state); |
1111 | 0 | ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank); |
1112 | 0 | ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group); |
1113 | |
|
1114 | 0 | for (uint32_t i = 0; i < hparams.n_layer; ++i) { |
1115 | 0 | hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0; |
1116 | 0 | } |
1117 | |
|
1118 | 0 | switch (hparams.n_layer) { |
1119 | 0 | case 16: type = LLM_TYPE_1B; break; |
1120 | 0 | case 32: |
1121 | 0 | if (hparams.n_embd == 2048) { |
1122 | 0 | type = LLM_TYPE_2B; |
1123 | 0 | } else if (hparams.n_embd == 4096) { |
1124 | 0 | type = LLM_TYPE_8B; |
1125 | 0 | } |
1126 | 0 | break; |
1127 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1128 | 0 | } |
1129 | 0 | } break; |
1130 | 0 | case LLM_ARCH_PLAMO3: |
1131 | 0 | { |
1132 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1133 | 0 | const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); |
1134 | 0 | if (found_swa && hparams.n_swa > 0) { |
1135 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
1136 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
1137 | 0 | uint32_t swa_period = 8; |
1138 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
1139 | 0 | hparams.set_swa_pattern(swa_period); |
1140 | 0 | } else { |
1141 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_NONE; |
1142 | 0 | } |
1143 | |
|
1144 | 0 | switch (hparams.n_layer) { |
1145 | 0 | case 24: type = LLM_TYPE_2B; break; |
1146 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1147 | 0 | } |
1148 | 0 | } break; |
1149 | 0 | case LLM_ARCH_GPT2: |
1150 | 0 | { |
1151 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
1152 | 0 | switch (hparams.n_layer) { |
1153 | 0 | case 12: type = LLM_TYPE_SMALL; break; |
1154 | 0 | case 24: type = LLM_TYPE_MEDIUM; break; |
1155 | 0 | case 36: type = LLM_TYPE_LARGE; break; |
1156 | 0 | case 48: type = LLM_TYPE_XL; break; |
1157 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1158 | 0 | } |
1159 | 0 | } break; |
1160 | 0 | case LLM_ARCH_CODESHELL: |
1161 | 0 | { |
1162 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
1163 | 0 | switch (hparams.n_layer) { |
1164 | 0 | case 42: type = LLM_TYPE_7B; break; |
1165 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1166 | 0 | } |
1167 | 0 | } break; |
1168 | 0 | case LLM_ARCH_ORION: |
1169 | 0 | { |
1170 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
1171 | |
|
1172 | 0 | switch (hparams.n_layer) { |
1173 | 0 | case 40: type = LLM_TYPE_14B; break; |
1174 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1175 | 0 | } |
1176 | 0 | } break; |
1177 | 0 | case LLM_ARCH_INTERNLM2: |
1178 | 0 | { |
1179 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1180 | 0 | switch (hparams.n_layer) { |
1181 | 0 | case 32: type = LLM_TYPE_7B; break; |
1182 | 0 | case 48: type = LLM_TYPE_20B; break; |
1183 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1184 | 0 | } |
1185 | 0 | } break; |
1186 | 0 | case LLM_ARCH_GEMMA: |
1187 | 0 | { |
1188 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1189 | |
|
1190 | 0 | switch (hparams.n_layer) { |
1191 | 0 | case 18: type = LLM_TYPE_2B; break; |
1192 | 0 | case 28: type = LLM_TYPE_7B; break; |
1193 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1194 | 0 | } |
1195 | 0 | } break; |
1196 | 0 | case LLM_ARCH_GEMMA2: |
1197 | 0 | { |
1198 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
1199 | 0 | hparams.n_swa = 4096; // default value of gemma 2 |
1200 | 0 | uint32_t swa_period = 2; |
1201 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
1202 | 0 | hparams.set_swa_pattern(swa_period); |
1203 | 0 | hparams.attn_soft_cap = true; |
1204 | 0 | hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train; |
1205 | 0 | hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train; |
1206 | |
|
1207 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
1208 | 0 | ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); |
1209 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1210 | 0 | ml.get_key(LLM_KV_ATTN_LOGIT_SOFTCAPPING, hparams.f_attn_logit_softcapping, false); |
1211 | 0 | ml.get_key(LLM_KV_FINAL_LOGIT_SOFTCAPPING, hparams.f_final_logit_softcapping, false); |
1212 | |
|
1213 | 0 | switch (hparams.n_layer) { |
1214 | 0 | case 26: type = LLM_TYPE_2B; break; |
1215 | 0 | case 42: type = LLM_TYPE_9B; break; |
1216 | 0 | case 46: type = LLM_TYPE_27B; break; |
1217 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1218 | 0 | } |
1219 | | |
1220 | | // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/config.py#L173 |
1221 | 0 | hparams.f_attention_scale = type == LLM_TYPE_27B |
1222 | 0 | ? 1.0f / std::sqrt(float(hparams.n_embd / hparams.n_head(0))) |
1223 | 0 | : 1.0f / std::sqrt(float(hparams.n_embd_head_k())); |
1224 | 0 | } break; |
1225 | 0 | case LLM_ARCH_GEMMA3: |
1226 | 0 | { |
1227 | 0 | const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); |
1228 | 0 | if (found_swa && hparams.n_swa > 0) { |
1229 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
1230 | 0 | uint32_t swa_period = 6; |
1231 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
1232 | 0 | hparams.set_swa_pattern(swa_period); |
1233 | |
|
1234 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
1235 | 0 | } else { |
1236 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_NONE; |
1237 | 0 | } |
1238 | |
|
1239 | 0 | hparams.f_final_logit_softcapping = 0.0f; |
1240 | 0 | ml.get_key(LLM_KV_FINAL_LOGIT_SOFTCAPPING, hparams.f_final_logit_softcapping, false); |
1241 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1242 | |
|
1243 | 0 | switch (hparams.n_layer) { |
1244 | 0 | case 18: type = LLM_TYPE_270M; break; |
1245 | 0 | case 26: type = LLM_TYPE_1B; break; |
1246 | 0 | case 32: type = LLM_TYPE_8B; break; // Rnj-1 |
1247 | 0 | case 34: type = LLM_TYPE_4B; break; |
1248 | 0 | case 48: type = LLM_TYPE_12B; break; |
1249 | 0 | case 62: type = LLM_TYPE_27B; break; |
1250 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1251 | 0 | } |
1252 | | |
1253 | | // ref: https://github.com/google/gemma_pytorch/blob/014acb7ac4563a5f77c76d7ff98f31b568c16508/gemma/config.py#L289 |
1254 | 0 | hparams.f_attention_scale = type == LLM_TYPE_27B |
1255 | 0 | ? 1.0f / std::sqrt(float(hparams.n_embd / hparams.n_head(0))) |
1256 | 0 | : 1.0f / std::sqrt(float(hparams.n_embd_head_k())); |
1257 | 0 | } break; |
1258 | 0 | case LLM_ARCH_GEMMA3N: |
1259 | 0 | { |
1260 | 0 | uint32_t swa_period = 5; |
1261 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
1262 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
1263 | 0 | hparams.set_swa_pattern(swa_period); |
1264 | |
|
1265 | 0 | hparams.n_layer_kv_from_start = 20; |
1266 | 0 | hparams.f_attention_scale = 1.0f; |
1267 | |
|
1268 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
1269 | 0 | ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa); |
1270 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1271 | |
|
1272 | 0 | switch (hparams.n_layer) { |
1273 | 0 | case 30: type = LLM_TYPE_E2B; break; |
1274 | 0 | case 35: type = LLM_TYPE_E4B; break; |
1275 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1276 | 0 | } |
1277 | 0 | } break; |
1278 | 0 | case LLM_ARCH_GEMMA_EMBEDDING: |
1279 | 0 | { |
1280 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_SYMMETRIC; |
1281 | 0 | uint32_t swa_period = 6; |
1282 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
1283 | 0 | hparams.set_swa_pattern(swa_period); |
1284 | |
|
1285 | 0 | hparams.causal_attn = false; // embeddings do not use causal attention |
1286 | |
|
1287 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
1288 | 0 | ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa); |
1289 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1290 | 0 | ml.get_key(LLM_KV_POOLING_TYPE, hparams.pooling_type, false); |
1291 | | |
1292 | | //applied only if model converted with --sentence-transformers-dense-modules |
1293 | 0 | ml.get_key(LLM_KV_DENSE_2_FEAT_IN, hparams.dense_2_feat_in, false); |
1294 | 0 | ml.get_key(LLM_KV_DENSE_2_FEAT_OUT, hparams.dense_2_feat_out, false); |
1295 | 0 | ml.get_key(LLM_KV_DENSE_3_FEAT_IN, hparams.dense_3_feat_in, false); |
1296 | 0 | ml.get_key(LLM_KV_DENSE_3_FEAT_OUT, hparams.dense_3_feat_out, false); |
1297 | |
|
1298 | 0 | GGML_ASSERT((hparams.dense_2_feat_in == 0 || hparams.dense_2_feat_in == hparams.n_embd) && "dense_2_feat_in must be equal to n_embd"); |
1299 | 0 | GGML_ASSERT((hparams.dense_3_feat_out == 0 || hparams.dense_3_feat_out == hparams.n_embd) && "dense_3_feat_out must be equal to n_embd"); |
1300 | |
|
1301 | 0 | switch (hparams.n_layer) { |
1302 | 0 | case 24: type = LLM_TYPE_0_3B; break; |
1303 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1304 | 0 | } |
1305 | 0 | hparams.f_attention_scale = 1.0f / std::sqrt(float(hparams.n_embd_head_k())); |
1306 | |
|
1307 | 0 | } break; |
1308 | 0 | case LLM_ARCH_STARCODER2: |
1309 | 0 | { |
1310 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
1311 | 0 | switch (hparams.n_layer) { |
1312 | 0 | case 30: type = LLM_TYPE_3B; break; |
1313 | 0 | case 32: type = LLM_TYPE_7B; break; |
1314 | 0 | case 40: type = LLM_TYPE_15B; break; |
1315 | 0 | case 52: type = LLM_TYPE_20B; break; // granite |
1316 | 0 | case 88: type = LLM_TYPE_34B; break; // granite |
1317 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1318 | 0 | } |
1319 | 0 | } break; |
1320 | 0 | case LLM_ARCH_MAMBA: |
1321 | 0 | { |
1322 | 0 | ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv); |
1323 | 0 | ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner); |
1324 | 0 | ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state); |
1325 | 0 | ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank); |
1326 | 0 | ml.get_key(LLM_KV_SSM_DT_B_C_RMS, hparams.ssm_dt_b_c_rms, false); |
1327 | |
|
1328 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1329 | |
|
1330 | 0 | switch (hparams.n_layer) { |
1331 | 0 | case 24: |
1332 | 0 | switch (hparams.n_embd) { |
1333 | 0 | case 768: type = LLM_TYPE_SMALL; break; |
1334 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1335 | 0 | } break; |
1336 | 0 | case 48: |
1337 | 0 | switch (hparams.n_embd) { |
1338 | 0 | case 1024: type = LLM_TYPE_MEDIUM; break; |
1339 | 0 | case 1536: type = LLM_TYPE_LARGE; break; |
1340 | 0 | case 2048: type = LLM_TYPE_XL; break; |
1341 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1342 | 0 | } break; |
1343 | 0 | case 64: |
1344 | 0 | switch (hparams.n_embd) { |
1345 | 0 | case 2560: type = LLM_TYPE_3B; break; |
1346 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1347 | 0 | } break; |
1348 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1349 | 0 | } |
1350 | 0 | } break; |
1351 | 0 | case LLM_ARCH_MAMBA2: |
1352 | 0 | { |
1353 | 0 | ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv); |
1354 | 0 | ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner); |
1355 | 0 | ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state); |
1356 | 0 | ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank); |
1357 | 0 | ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group); |
1358 | |
|
1359 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1360 | |
|
1361 | 0 | switch (hparams.n_layer) { |
1362 | 0 | case 24: |
1363 | 0 | switch (hparams.n_embd) { |
1364 | 0 | case 768: type = LLM_TYPE_SMALL; break; |
1365 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1366 | 0 | } break; |
1367 | 0 | case 48: |
1368 | 0 | switch (hparams.n_embd) { |
1369 | 0 | case 1024: type = LLM_TYPE_MEDIUM; break; |
1370 | 0 | case 1536: type = LLM_TYPE_LARGE; break; |
1371 | 0 | case 2048: type = LLM_TYPE_XL; break; |
1372 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1373 | 0 | } break; |
1374 | 0 | case 64: |
1375 | 0 | switch (hparams.n_embd) { |
1376 | 0 | case 2560: type = LLM_TYPE_3B; break; |
1377 | 0 | case 4096: type = LLM_TYPE_7B; break; |
1378 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1379 | 0 | } break; |
1380 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1381 | 0 | } |
1382 | 0 | } break; |
1383 | 0 | case LLM_ARCH_JAMBA: |
1384 | 0 | { |
1385 | 0 | ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv); |
1386 | 0 | ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner); |
1387 | 0 | ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state); |
1388 | 0 | ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank); |
1389 | |
|
1390 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1391 | |
|
1392 | 0 | for (uint32_t i = 0; i < hparams.n_layer; ++i) { |
1393 | 0 | hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0; |
1394 | 0 | } |
1395 | |
|
1396 | 0 | switch (hparams.n_layer) { |
1397 | | // TODO: Jamba layers are a bit heterogeneous, so naming this is hard. |
1398 | 0 | case 12: // 900M 8x???M |
1399 | 0 | case 32: // 51B 16x?B |
1400 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1401 | 0 | } |
1402 | 0 | } break; |
1403 | 0 | case LLM_ARCH_XVERSE: |
1404 | 0 | { |
1405 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1406 | 0 | switch (hparams.n_layer) { |
1407 | 0 | case 32: type = LLM_TYPE_7B; break; |
1408 | 0 | case 40: type = LLM_TYPE_13B; break; |
1409 | 0 | case 80: type = LLM_TYPE_65B; break; |
1410 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1411 | 0 | } |
1412 | 0 | } break; |
1413 | 0 | case LLM_ARCH_COMMAND_R: |
1414 | 0 | { |
1415 | 0 | ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale, false); |
1416 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
1417 | 0 | switch (hparams.n_layer) { |
1418 | 0 | case 40: type = LLM_TYPE_35B; break; |
1419 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1420 | 0 | } |
1421 | 0 | } break; |
1422 | 0 | case LLM_ARCH_COHERE2: |
1423 | 0 | { |
1424 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
1425 | 0 | uint32_t swa_period = 4; |
1426 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
1427 | 0 | hparams.set_swa_pattern(swa_period); |
1428 | 0 | hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train; |
1429 | 0 | hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train; |
1430 | |
|
1431 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
1432 | 0 | ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa); |
1433 | 0 | ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale); |
1434 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
1435 | 0 | switch (hparams.n_layer) { |
1436 | 0 | case 32: type = LLM_TYPE_8B; break; |
1437 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1438 | 0 | } |
1439 | 0 | } break; |
1440 | 0 | case LLM_ARCH_DBRX: |
1441 | 0 | { |
1442 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
1443 | 0 | ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv); |
1444 | |
|
1445 | 0 | switch (hparams.n_layer) { |
1446 | 0 | case 40: type = LLM_TYPE_16x12B; break; |
1447 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1448 | 0 | } |
1449 | 0 | } break; |
1450 | 0 | case LLM_ARCH_OLMO: |
1451 | 0 | { |
1452 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
1453 | 0 | ml.get_key(LLM_KV_ATTENTION_CLAMP_KQV, hparams.f_clamp_kqv, false); |
1454 | |
|
1455 | 0 | switch (hparams.n_layer) { |
1456 | 0 | case 22: type = LLM_TYPE_1B; break; |
1457 | 0 | case 32: type = LLM_TYPE_7B; break; |
1458 | 0 | case 80: type = LLM_TYPE_70B; break; |
1459 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1460 | 0 | } |
1461 | 0 | } break; |
1462 | 0 | case LLM_ARCH_OLMO2: |
1463 | 0 | { |
1464 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1465 | |
|
1466 | 0 | const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); |
1467 | 0 | if (found_swa && hparams.n_swa > 0) { |
1468 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
1469 | 0 | uint32_t swa_period = 4; |
1470 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
1471 | 0 | hparams.set_swa_pattern(swa_period); |
1472 | |
|
1473 | 0 | hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train; |
1474 | 0 | hparams.rope_freq_scale_train_swa = 1.0; // See olmo2.cpp |
1475 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
1476 | 0 | } else { |
1477 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_NONE; |
1478 | 0 | } |
1479 | |
|
1480 | 0 | switch (hparams.n_layer) { |
1481 | 0 | case 16: type = LLM_TYPE_1B; break; |
1482 | 0 | case 32: type = LLM_TYPE_7B; break; |
1483 | 0 | case 40: type = LLM_TYPE_13B; break; |
1484 | 0 | case 64: type = LLM_TYPE_32B; break; |
1485 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1486 | 0 | } |
1487 | 0 | } break; |
1488 | 0 | case LLM_ARCH_SEED_OSS: |
1489 | 0 | { |
1490 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1491 | 0 | switch (hparams.n_layer) { |
1492 | 0 | case 64: type = LLM_TYPE_36B; break; |
1493 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1494 | 0 | } |
1495 | 0 | } break; |
1496 | 0 | case LLM_ARCH_OLMOE: |
1497 | 0 | { |
1498 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1499 | 0 | switch (hparams.n_layer) { |
1500 | 0 | case 16: type = LLM_TYPE_A1_7B; break; |
1501 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1502 | 0 | } |
1503 | 0 | } break; |
1504 | 0 | case LLM_ARCH_OPENELM: |
1505 | 0 | { |
1506 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1507 | |
|
1508 | 0 | switch (hparams.n_layer) { |
1509 | 0 | case 16: type = LLM_TYPE_270M; break; |
1510 | 0 | case 20: type = LLM_TYPE_450M; break; |
1511 | 0 | case 28: type = LLM_TYPE_1B; break; |
1512 | 0 | case 36: type = LLM_TYPE_3B; break; |
1513 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1514 | 0 | } |
1515 | 0 | } break; |
1516 | 0 | case LLM_ARCH_GPTNEOX: |
1517 | 0 | { |
1518 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
1519 | 0 | ml.get_key(LLM_KV_USE_PARALLEL_RESIDUAL, hparams.use_par_res); |
1520 | 0 | switch (hparams.n_layer) { |
1521 | 0 | case 6: |
1522 | 0 | switch (hparams.n_ff()) { |
1523 | 0 | case 512: type = LLM_TYPE_14M; break; |
1524 | 0 | case 2048: type = LLM_TYPE_70M; break; |
1525 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1526 | 0 | } break; |
1527 | 0 | case 12: |
1528 | 0 | switch (hparams.n_ff()) { |
1529 | 0 | case 3072: type = LLM_TYPE_160M; break; |
1530 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1531 | 0 | } break; |
1532 | 0 | case 16: |
1533 | 0 | switch (hparams.n_ff()) { |
1534 | 0 | case 8192: type = LLM_TYPE_1B; break; |
1535 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1536 | 0 | } break; |
1537 | 0 | case 24: |
1538 | 0 | switch (hparams.n_ff()) { |
1539 | 0 | case 4096: type = LLM_TYPE_410M; break; |
1540 | 0 | case 8192: type = LLM_TYPE_1_4B; break; |
1541 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1542 | 0 | } break; |
1543 | 0 | case 32: |
1544 | 0 | switch (hparams.n_ff()) { |
1545 | 0 | case 10240: type = LLM_TYPE_2_8B; break; |
1546 | 0 | case 16384: type = LLM_TYPE_6_9B; break; |
1547 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1548 | 0 | } break; |
1549 | 0 | case 36: |
1550 | 0 | switch (hparams.n_ff()) { |
1551 | 0 | case 20480: type = LLM_TYPE_12B; break; |
1552 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1553 | 0 | } break; |
1554 | 0 | case 44: |
1555 | 0 | switch (hparams.n_ff()) { |
1556 | 0 | case 24576: type = LLM_TYPE_20B; break; |
1557 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1558 | 0 | } break; |
1559 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1560 | 0 | } |
1561 | 0 | } break; |
1562 | 0 | case LLM_ARCH_ARCTIC: |
1563 | 0 | { |
1564 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1565 | |
|
1566 | 0 | if (hparams.n_expert == 128) { |
1567 | 0 | switch (hparams.n_layer) { |
1568 | 0 | case 35: type = LLM_TYPE_10B_128x3_66B; break; |
1569 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1570 | 0 | } |
1571 | 0 | } else { |
1572 | 0 | type = LLM_TYPE_UNKNOWN; |
1573 | 0 | } |
1574 | 0 | } break; |
1575 | 0 | case LLM_ARCH_DEEPSEEK: |
1576 | 0 | { |
1577 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1578 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
1579 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
1580 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared); |
1581 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
1582 | |
|
1583 | 0 | switch (hparams.n_ff_exp) { |
1584 | 0 | case 1408: type = LLM_TYPE_16B; break; |
1585 | 0 | case 1792: type = LLM_TYPE_20B; break; |
1586 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1587 | 0 | } |
1588 | 0 | } break; |
1589 | 0 | case LLM_ARCH_DEEPSEEK2: |
1590 | 0 | case LLM_ARCH_MISTRAL4: |
1591 | 0 | { |
1592 | | // lite variants include DeepSeek-V2-Lite, GigaChat3-10B-A1.8B, Kanana-2-30B-A3B |
1593 | 0 | const bool is_lite = (hparams.n_layer == 27 || hparams.n_layer == 26 || (hparams.n_layer == 48 && n_vocab == 128256)); |
1594 | |
|
1595 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1596 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
1597 | 0 | if (!is_lite) { |
1598 | 0 | ml.get_key(LLM_KV_ATTENTION_Q_LORA_RANK, hparams.n_lora_q); |
1599 | 0 | } |
1600 | 0 | ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv); |
1601 | 0 | ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH_MLA, hparams.n_embd_head_k_mla_impl, false); |
1602 | 0 | ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH_MLA, hparams.n_embd_head_v_mla_impl, false); |
1603 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
1604 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared); |
1605 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
1606 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false); |
1607 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false); |
1608 | 0 | if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) { |
1609 | | // for compatibility with existing DeepSeek V2 and V2.5 GGUFs |
1610 | | // that have no expert_gating_func model parameter set |
1611 | 0 | if ((hparams.n_layer == 47 || hparams.n_layer == 48) && n_vocab == 154880) { |
1612 | | // GLM 4.7 Lite |
1613 | 0 | hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID; |
1614 | 0 | } else { |
1615 | 0 | hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX; |
1616 | 0 | } |
1617 | 0 | } |
1618 | |
|
1619 | 0 | if (ml.get_key(LLM_KV_ROPE_SCALING_YARN_LOG_MUL, hparams.rope_yarn_log_mul, 0.0f)) { |
1620 | | // [TAG_DEEPSEEK2_YARN_LOG_MUL_FIX] |
1621 | | // cancel the factor from the convert script |
1622 | 0 | hparams.rope_yarn_log_mul /= 0.1f; |
1623 | 0 | } |
1624 | | |
1625 | | // (optional) temperature tuning - used by mistral-large |
1626 | 0 | ml.get_key(LLM_KV_ATTENTION_TEMPERATURE_SCALE, hparams.f_attn_temp_scale, false); |
1627 | 0 | ml.get_key(LLM_KV_ATTENTION_TEMPERATURE_LENGTH, hparams.n_attn_temp_floor_scale, false); |
1628 | |
|
1629 | 0 | hparams.f_attn_temp_offset = 0.0f; |
1630 | |
|
1631 | 0 | switch (hparams.n_layer) { |
1632 | 0 | case 27: type = LLM_TYPE_16B; break; |
1633 | 0 | case 47: type = LLM_TYPE_30B_A3B; break; |
1634 | 0 | case 60: type = LLM_TYPE_236B; break; |
1635 | 0 | case 61: type = LLM_TYPE_671B; break; |
1636 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1637 | 0 | } |
1638 | 0 | } break; |
1639 | 0 | case LLM_ARCH_PLM: |
1640 | 0 | { |
1641 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1642 | 0 | ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv); |
1643 | 0 | switch (hparams.n_layer) { |
1644 | 0 | case 32: type = LLM_TYPE_1_8B; break; |
1645 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1646 | 0 | } |
1647 | 0 | } break; |
1648 | 0 | case LLM_ARCH_CHATGLM: |
1649 | 0 | { |
1650 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1651 | 0 | switch (hparams.n_layer) { |
1652 | 0 | case 28: { |
1653 | 0 | if (hparams.n_head(0) == 16) { |
1654 | 0 | type = LLM_TYPE_1_5B; |
1655 | 0 | } else { |
1656 | 0 | type = LLM_TYPE_6B; |
1657 | 0 | } |
1658 | 0 | } break; |
1659 | 0 | case 40: { |
1660 | 0 | if (hparams.n_head(0) == 24) { |
1661 | 0 | type = LLM_TYPE_4B; |
1662 | 0 | } else { |
1663 | 0 | type = LLM_TYPE_9B; |
1664 | 0 | } |
1665 | 0 | } break; |
1666 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1667 | 0 | } |
1668 | 0 | } break; |
1669 | 0 | case LLM_ARCH_GLM4: |
1670 | 0 | { |
1671 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1672 | 0 | ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, false); |
1673 | | |
1674 | | // NextN/MTP parameters (GLM-OCR) |
1675 | 0 | ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.nextn_predict_layers, false); |
1676 | 0 | GGML_ASSERT(hparams.nextn_predict_layers < hparams.n_layer && "nextn_predict_layers must be < n_layer"); |
1677 | | |
1678 | | // TODO: when MTP is implemented, this should probably be updated if needed |
1679 | 0 | hparams.n_layer_kv_from_start = hparams.n_layer - hparams.nextn_predict_layers; |
1680 | |
|
1681 | 0 | switch (hparams.n_layer) { |
1682 | 0 | case 17: type = LLM_TYPE_1B; break; // GLM-OCR |
1683 | 0 | case 40: type = LLM_TYPE_9B; break; |
1684 | 0 | case 61: type = LLM_TYPE_32B; break; |
1685 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1686 | 0 | } |
1687 | 0 | } break; |
1688 | 0 | case LLM_ARCH_GLM4_MOE: |
1689 | 0 | { |
1690 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
1691 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1692 | 0 | ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, false); |
1693 | | |
1694 | | // MoE parameters |
1695 | 0 | ml.get_key(LLM_KV_EXPERT_COUNT, hparams.n_expert); |
1696 | 0 | ml.get_key(LLM_KV_EXPERT_USED_COUNT, hparams.n_expert_used); |
1697 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared); |
1698 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
1699 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
1700 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false); |
1701 | | |
1702 | | // Expert gating function (GLM-4.5 uses sigmoid) |
1703 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false); |
1704 | 0 | if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) { |
1705 | 0 | hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID; |
1706 | 0 | } |
1707 | | |
1708 | | // NextN/MTP parameters |
1709 | 0 | ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.nextn_predict_layers, false); |
1710 | 0 | GGML_ASSERT(hparams.nextn_predict_layers < hparams.n_layer && "nextn_predict_layers must be < n_layer"); |
1711 | | |
1712 | | // TODO: when MTP is implemented, this should probably be updated if needed |
1713 | 0 | hparams.n_layer_kv_from_start = hparams.n_layer - hparams.nextn_predict_layers; |
1714 | |
|
1715 | 0 | switch (hparams.n_layer) { |
1716 | 0 | case 47: type = LLM_TYPE_106B_A12B; break; // GLM-4.5-Air (46 layers + 1 NextN layer) |
1717 | 0 | case 48: type = LLM_TYPE_102B_A12B; break; // Solar Open |
1718 | 0 | case 93: type = LLM_TYPE_355B_A32B; break; // GLM-4.5 (92 layers + 1 NextN layer) |
1719 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1720 | 0 | } |
1721 | 0 | } break; |
1722 | 0 | case LLM_ARCH_GLM_DSA: |
1723 | 0 | { |
1724 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
1725 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1726 | 0 | ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, false); |
1727 | | |
1728 | | // MoE parameters |
1729 | 0 | ml.get_key(LLM_KV_EXPERT_COUNT, hparams.n_expert); |
1730 | 0 | ml.get_key(LLM_KV_EXPERT_USED_COUNT, hparams.n_expert_used); |
1731 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared); |
1732 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
1733 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
1734 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false); |
1735 | | |
1736 | | // deepseek MLA parameters |
1737 | 0 | ml.get_key(LLM_KV_ATTENTION_Q_LORA_RANK, hparams.n_lora_q); |
1738 | 0 | ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv); |
1739 | 0 | ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH_MLA, hparams.n_embd_head_k_mla_impl, false); |
1740 | 0 | ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH_MLA, hparams.n_embd_head_v_mla_impl, false); |
1741 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
1742 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared); |
1743 | | |
1744 | | // DSA parameters |
1745 | 0 | ml.get_key(LLM_KV_ATTENTION_INDEXER_HEAD_COUNT, hparams.indexer_n_head); |
1746 | 0 | ml.get_key(LLM_KV_ATTENTION_INDEXER_KEY_LENGTH, hparams.indexer_head_size); |
1747 | 0 | ml.get_key(LLM_KV_ATTENTION_INDEXER_TOP_K, hparams.indexer_top_k); |
1748 | | |
1749 | | // Expert gating function (GLM-4.5 uses sigmoid) |
1750 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false); |
1751 | 0 | if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) { |
1752 | 0 | hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID; |
1753 | 0 | } |
1754 | | |
1755 | | // NextN/MTP parameters |
1756 | 0 | ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.nextn_predict_layers, false); |
1757 | 0 | GGML_ASSERT(hparams.nextn_predict_layers < hparams.n_layer && "nextn_predict_layers must be < n_layer"); |
1758 | | |
1759 | | // TODO: when MTP is implemented, this should probably be updated if needed |
1760 | 0 | hparams.n_layer_kv_from_start = hparams.n_layer - hparams.nextn_predict_layers; |
1761 | |
|
1762 | 0 | switch (hparams.n_layer) { |
1763 | 0 | case 79: type = LLM_TYPE_744B_A40B; break; |
1764 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1765 | 0 | } |
1766 | 0 | } break; |
1767 | 0 | case LLM_ARCH_BITNET: |
1768 | 0 | { |
1769 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1770 | |
|
1771 | 0 | switch (hparams.n_layer) { |
1772 | 0 | case 26: type = LLM_TYPE_3B; break; |
1773 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1774 | 0 | } |
1775 | 0 | } break; |
1776 | 0 | case LLM_ARCH_T5: |
1777 | 0 | { |
1778 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1779 | 0 | ml.get_key(LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, hparams.n_rel_attn_bkts); |
1780 | |
|
1781 | 0 | uint32_t dec_start_token_id; |
1782 | 0 | if (ml.get_key(LLM_KV_DECODER_START_TOKEN_ID, dec_start_token_id, false)) { |
1783 | 0 | hparams.dec_start_token_id = dec_start_token_id; |
1784 | 0 | } |
1785 | |
|
1786 | 0 | hparams.dec_n_layer = hparams.n_layer; |
1787 | 0 | ml.get_key(LLM_KV_DECODER_BLOCK_COUNT, hparams.dec_n_layer, false); |
1788 | |
|
1789 | 0 | switch (hparams.n_layer) { |
1790 | 0 | case 6: type = LLM_TYPE_60M; break; // t5-small |
1791 | 0 | case 8: type = LLM_TYPE_80M; break; // flan-t5-small |
1792 | 0 | case 12: |
1793 | 0 | switch (hparams.n_ff()) { |
1794 | 0 | case 3072: type = LLM_TYPE_220M; break; // t5-base |
1795 | 0 | case 2048: type = LLM_TYPE_250M; break; // flan-t5-base |
1796 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1797 | 0 | } break; |
1798 | 0 | case 24: |
1799 | 0 | switch (hparams.n_ff()) { |
1800 | 0 | case 4096: type = LLM_TYPE_770M; break; // t5-large |
1801 | 0 | case 2816: type = LLM_TYPE_780M; break; // flan-t5-large |
1802 | 0 | case 16384: type = LLM_TYPE_3B; break; // t5-3b |
1803 | 0 | case 5120: type = LLM_TYPE_3B; break; // flan-t5-xl |
1804 | 0 | case 65536: type = LLM_TYPE_11B; break; // t5-11b |
1805 | 0 | case 10240: type = LLM_TYPE_11B; break; // flan-t5-xxl |
1806 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1807 | 0 | } break; |
1808 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1809 | 0 | } |
1810 | 0 | } break; |
1811 | 0 | case LLM_ARCH_T5ENCODER: |
1812 | 0 | { |
1813 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1814 | 0 | ml.get_key(LLM_KV_ATTENTION_RELATIVE_BUCKETS_COUNT, hparams.n_rel_attn_bkts); |
1815 | 0 | type = LLM_TYPE_UNKNOWN; |
1816 | 0 | } break; |
1817 | 0 | case LLM_ARCH_JAIS: |
1818 | 0 | { |
1819 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
1820 | 0 | ml.get_key(LLM_KV_ATTENTION_MAX_ALIBI_BIAS, hparams.f_max_alibi_bias, false); |
1821 | |
|
1822 | 0 | switch (hparams.n_layer) { |
1823 | 0 | case 24: type = LLM_TYPE_1_3B; break; |
1824 | 0 | case 40: type = LLM_TYPE_13B; break; |
1825 | | /* TODO: add variants */ |
1826 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1827 | 0 | } |
1828 | 0 | } break; |
1829 | 0 | case LLM_ARCH_JAIS2: |
1830 | 0 | { |
1831 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
1832 | |
|
1833 | 0 | switch (hparams.n_layer) { |
1834 | 0 | case 32: type = LLM_TYPE_8B; break; |
1835 | 0 | case 68: type = LLM_TYPE_70B; break; |
1836 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1837 | 0 | } |
1838 | 0 | } break; |
1839 | 0 | case LLM_ARCH_NEMOTRON: |
1840 | 0 | { |
1841 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
1842 | 0 | switch (hparams.n_layer) { |
1843 | 0 | case 32: type = LLM_TYPE_4B; break; |
1844 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1845 | 0 | } |
1846 | 0 | } break; |
1847 | 0 | case LLM_ARCH_NEMOTRON_H: |
1848 | 0 | case LLM_ARCH_NEMOTRON_H_MOE: |
1849 | 0 | { |
1850 | 0 | ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv); |
1851 | 0 | ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner); |
1852 | 0 | ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state); |
1853 | 0 | ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank); |
1854 | 0 | ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group); |
1855 | | |
1856 | | // A layer is recurrent IFF the n_head_kv value is set to 0 and |
1857 | | // the n_ff value is set to 0 |
1858 | 0 | for (uint32_t i = 0; i < hparams.n_layer; ++i) { |
1859 | 0 | hparams.recurrent_layer_arr[i] = (hparams.n_head_kv(i) == 0 && hparams.n_ff(i) == 0); |
1860 | 0 | } |
1861 | |
|
1862 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1863 | |
|
1864 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false); |
1865 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false); |
1866 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared, false); |
1867 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false); |
1868 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
1869 | 0 | ml.get_key(LLM_KV_MOE_LATENT_SIZE, hparams.moe_latent_size, false); |
1870 | |
|
1871 | 0 | switch (hparams.n_layer) { |
1872 | 0 | case 52: type = LLM_TYPE_31B_A3_5B; break; // Nemotron-H_MOE 31B |
1873 | 0 | case 56: type = LLM_TYPE_9B; break; |
1874 | 0 | case 88: type = LLM_TYPE_120B_A12B; break; |
1875 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1876 | 0 | } |
1877 | 0 | } break; |
1878 | 0 | case LLM_ARCH_EXAONE: |
1879 | 0 | { |
1880 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1881 | |
|
1882 | 0 | switch (hparams.n_layer) { |
1883 | 0 | case 32: type = LLM_TYPE_8B; break; |
1884 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1885 | 0 | } |
1886 | 0 | } break; |
1887 | 0 | case LLM_ARCH_EXAONE4: |
1888 | 0 | { |
1889 | 0 | if (hparams.n_layer == 64) { // 32B |
1890 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
1891 | 0 | hparams.n_swa = 4096; |
1892 | 0 | uint32_t swa_period = 4; |
1893 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
1894 | 0 | hparams.set_swa_pattern(swa_period); |
1895 | |
|
1896 | 0 | hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train; |
1897 | 0 | hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train; |
1898 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
1899 | 0 | } |
1900 | |
|
1901 | 0 | ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); |
1902 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1903 | |
|
1904 | 0 | switch (hparams.n_layer) { |
1905 | 0 | case 30: type = LLM_TYPE_1_2B; break; |
1906 | 0 | case 64: type = LLM_TYPE_32B; break; |
1907 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1908 | 0 | } |
1909 | 0 | } break; |
1910 | 0 | case LLM_ARCH_EXAONE_MOE: |
1911 | 0 | { |
1912 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
1913 | 0 | hparams.n_swa = 128; |
1914 | 0 | uint32_t swa_period = 4; |
1915 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
1916 | 0 | hparams.set_swa_pattern(swa_period); |
1917 | 0 | hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train; |
1918 | 0 | hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train; |
1919 | |
|
1920 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
1921 | 0 | ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa); |
1922 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
1923 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared, false); |
1924 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
1925 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false); |
1926 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func); |
1927 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
1928 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false); |
1929 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
1930 | |
|
1931 | 0 | ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.nextn_predict_layers, false); |
1932 | 0 | GGML_ASSERT(hparams.nextn_predict_layers < hparams.n_layer && "nextn_predict_layers must be < n_layer"); |
1933 | |
|
1934 | 0 | switch (hparams.n_layer) { |
1935 | 0 | case 32: type = LLM_TYPE_30B_A3B; break; |
1936 | 0 | case 48: |
1937 | 0 | case 49: type = LLM_TYPE_235B_A22B; break; |
1938 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1939 | 0 | } |
1940 | 0 | } break; |
1941 | 0 | case LLM_ARCH_RWKV6: |
1942 | 0 | case LLM_ARCH_RWKV6QWEN2: |
1943 | 0 | { |
1944 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps, false); |
1945 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps, false); |
1946 | 0 | ml.get_key(LLM_KV_WKV_HEAD_SIZE, hparams.wkv_head_size); |
1947 | 0 | ml.get_key(LLM_KV_TIME_MIX_EXTRA_DIM, hparams.time_mix_extra_dim); |
1948 | 0 | ml.get_key(LLM_KV_TIME_DECAY_EXTRA_DIM, hparams.time_decay_extra_dim); |
1949 | 0 | ml.get_key(LLM_KV_RESCALE_EVERY_N_LAYERS, hparams.rescale_every_n_layers, false); |
1950 | 0 | ml.get_key(LLM_KV_TOKEN_SHIFT_COUNT, hparams.token_shift_count, false); |
1951 | |
|
1952 | 0 | switch (hparams.n_layer) { |
1953 | 0 | case 24: type = LLM_TYPE_1_6B; break; |
1954 | 0 | case 32: |
1955 | 0 | switch (hparams.n_embd) { |
1956 | 0 | case 2560: type = LLM_TYPE_3B; break; |
1957 | 0 | case 4096: type = LLM_TYPE_7B; break; |
1958 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1959 | 0 | } break; |
1960 | 0 | case 61: type = LLM_TYPE_14B; break; |
1961 | 0 | case 64: type = LLM_TYPE_32B; break; |
1962 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1963 | 0 | } |
1964 | 0 | } break; |
1965 | 0 | case LLM_ARCH_RWKV7: |
1966 | 0 | case LLM_ARCH_ARWKV7: |
1967 | 0 | { |
1968 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps, false); |
1969 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps, false); |
1970 | 0 | ml.get_key(LLM_KV_WKV_HEAD_SIZE, hparams.wkv_head_size); |
1971 | 0 | ml.get_key(LLM_KV_ATTENTION_DECAY_LORA_RANK, hparams.n_lora_decay); |
1972 | 0 | ml.get_key(LLM_KV_ATTENTION_ICLR_LORA_RANK, hparams.n_lora_iclr); |
1973 | 0 | ml.get_key(LLM_KV_ATTENTION_VALUE_RESIDUAL_MIX_LORA_RANK, hparams.n_lora_value_res_mix); |
1974 | 0 | ml.get_key(LLM_KV_ATTENTION_GATE_LORA_RANK, hparams.n_lora_gate, false); |
1975 | 0 | ml.get_key(LLM_KV_TOKEN_SHIFT_COUNT, hparams.token_shift_count, false); |
1976 | |
|
1977 | 0 | switch (hparams.n_layer) { |
1978 | 0 | case 12: |
1979 | 0 | switch (hparams.n_embd) { |
1980 | 0 | case 768: type = LLM_TYPE_190M; break; |
1981 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1982 | 0 | } break; |
1983 | 0 | case 24: |
1984 | 0 | switch (hparams.n_embd) { |
1985 | 0 | case 1024: type = LLM_TYPE_450M; break; |
1986 | 0 | case 2048: type = LLM_TYPE_1_5B; break; |
1987 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1988 | 0 | } break; |
1989 | 0 | case 28: |
1990 | 0 | switch (hparams.n_embd) { |
1991 | 0 | case 1536: type = LLM_TYPE_1_5B; break; |
1992 | 0 | case 3584: type = LLM_TYPE_7B; break; |
1993 | 0 | default: type = LLM_TYPE_UNKNOWN; |
1994 | 0 | } break; |
1995 | 0 | case 32: |
1996 | 0 | switch (hparams.n_embd) { |
1997 | 0 | case 2560: type = LLM_TYPE_2_9B; break; |
1998 | 0 | case 4096: type = LLM_TYPE_7B; break; |
1999 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2000 | 0 | } break; |
2001 | 0 | case 61: |
2002 | 0 | switch (hparams.n_embd) { |
2003 | 0 | case 4096: type = LLM_TYPE_14B; break; |
2004 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2005 | 0 | } break; |
2006 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2007 | 0 | } |
2008 | 0 | } break; |
2009 | 0 | case LLM_ARCH_GRANITE: |
2010 | 0 | case LLM_ARCH_GRANITE_MOE: |
2011 | 0 | { |
2012 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2013 | 0 | ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale); |
2014 | 0 | ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale, false); |
2015 | 0 | ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale, false); |
2016 | 0 | ml.get_key(LLM_KV_ATTENTION_SCALE, hparams.f_attention_scale, false); |
2017 | | |
2018 | | // Granite uses rope_finetuned as a switch for rope, so default to true |
2019 | 0 | bool rope_finetuned = true; |
2020 | 0 | ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false); |
2021 | 0 | hparams.rope_finetuned = rope_finetuned; |
2022 | |
|
2023 | 0 | switch (hparams.n_layer) { |
2024 | 0 | case 32: type = LLM_TYPE_3B; break; |
2025 | 0 | case 40: type = LLM_TYPE_3B; break; |
2026 | | // Add additional layer/vocab/etc checks here for other model sizes |
2027 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2028 | 0 | } |
2029 | | |
2030 | | // For Granite MoE Shared |
2031 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, /* required */ false); |
2032 | 0 | } break; |
2033 | 0 | case LLM_ARCH_GRANITE_HYBRID: |
2034 | 0 | { |
2035 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2036 | 0 | ml.get_key(LLM_KV_LOGIT_SCALE, hparams.f_logit_scale, /* required */ false); |
2037 | 0 | ml.get_key(LLM_KV_RESIDUAL_SCALE, hparams.f_residual_scale, /* required */ false); |
2038 | 0 | ml.get_key(LLM_KV_EMBEDDING_SCALE, hparams.f_embedding_scale, /* required */ false); |
2039 | 0 | ml.get_key(LLM_KV_ATTENTION_SCALE, hparams.f_attention_scale, /* required */ false); |
2040 | |
|
2041 | 0 | ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv); |
2042 | 0 | ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner); |
2043 | 0 | ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state); |
2044 | 0 | ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank); |
2045 | 0 | ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group); |
2046 | | |
2047 | | // Granite uses rope_finetuned as a switch for rope, so default to true |
2048 | 0 | bool rope_finetuned = true; |
2049 | 0 | ml.get_key(LLM_KV_ROPE_SCALING_FINETUNED, rope_finetuned, false); |
2050 | 0 | hparams.rope_finetuned = rope_finetuned; |
2051 | | |
2052 | | // A layer is recurrent IFF the n_head_kv value is set to 0 |
2053 | 0 | for (uint32_t i = 0; i < hparams.n_layer; ++i) { |
2054 | 0 | hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0; |
2055 | 0 | } |
2056 | |
|
2057 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2058 | |
|
2059 | 0 | switch (hparams.n_embd) { |
2060 | 0 | case 768: type = LLM_TYPE_350M; break; |
2061 | 0 | case 1536: type = (hparams.n_ff() == 512 ? LLM_TYPE_7B_A1B : LLM_TYPE_1B); break; |
2062 | 0 | case 2048: case 2560: type = LLM_TYPE_3B; break; |
2063 | 0 | case 4096: type = LLM_TYPE_32B; break; |
2064 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2065 | 0 | } |
2066 | | |
2067 | | // For Granite MoE Shared |
2068 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, /* required */ false); |
2069 | 0 | } break; |
2070 | 0 | case LLM_ARCH_CHAMELEON: |
2071 | 0 | { |
2072 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2073 | 0 | hparams.f_norm_eps = 1e-5; // eps for qk-norm, torch default |
2074 | 0 | ml.get_key(LLM_KV_SWIN_NORM, hparams.swin_norm, false); |
2075 | |
|
2076 | 0 | switch (hparams.n_layer) { |
2077 | 0 | case 32: type = LLM_TYPE_7B; break; |
2078 | 0 | case 48: type = LLM_TYPE_34B; break; |
2079 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2080 | 0 | } |
2081 | 0 | } break; |
2082 | 0 | case LLM_ARCH_WAVTOKENIZER_DEC: |
2083 | 0 | { |
2084 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_EPS, hparams.f_norm_eps); |
2085 | 0 | ml.get_key(LLM_KV_ATTENTION_GROUPNORM_EPS, hparams.f_norm_group_eps); |
2086 | 0 | ml.get_key(LLM_KV_ATTENTION_GROUPNORM_GROUPS, hparams.n_norm_groups); |
2087 | 0 | ml.get_key(LLM_KV_ATTENTION_CAUSAL, hparams.causal_attn, false); |
2088 | 0 | } break; |
2089 | 0 | case LLM_ARCH_BAILINGMOE: |
2090 | 0 | { |
2091 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2092 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
2093 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
2094 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared); |
2095 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
2096 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false); |
2097 | |
|
2098 | 0 | switch (hparams.n_layer) { |
2099 | 0 | case 28: type = LLM_TYPE_16B; break; |
2100 | 0 | case 88: type = LLM_TYPE_290B; break; |
2101 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2102 | 0 | } |
2103 | 0 | } break; |
2104 | 0 | case LLM_ARCH_BAILINGMOE2: |
2105 | 0 | { |
2106 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2107 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
2108 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
2109 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false); |
2110 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared); |
2111 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
2112 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false); |
2113 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func); |
2114 | 0 | ml.get_key(LLM_KV_NEXTN_PREDICT_LAYERS, hparams.nextn_predict_layers, false); |
2115 | 0 | GGML_ASSERT(hparams.nextn_predict_layers < hparams.n_layer && "nextn_predict_layers must be < n_layer"); |
2116 | | |
2117 | | // TODO: when MTP is implemented, this should probably be updated if needed |
2118 | 0 | hparams.n_layer_kv_from_start = hparams.n_layer - hparams.nextn_predict_layers; |
2119 | |
|
2120 | 0 | switch (hparams.n_layer) { |
2121 | 0 | case 20: type = LLM_TYPE_16B_A1B; break; |
2122 | 0 | case 21: type = LLM_TYPE_16B_A1B; break; |
2123 | 0 | case 32: type = LLM_TYPE_100B_A6B; break; |
2124 | 0 | case 33: type = LLM_TYPE_100B_A6B; break; |
2125 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2126 | 0 | } |
2127 | 0 | } break; |
2128 | 0 | case LLM_ARCH_DOTS1: |
2129 | 0 | { |
2130 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2131 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
2132 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
2133 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared); |
2134 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
2135 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false); |
2136 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false); |
2137 | 0 | switch (hparams.n_layer) { |
2138 | 0 | case 62: type = LLM_TYPE_142B; break; |
2139 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2140 | 0 | } |
2141 | 0 | } break; |
2142 | 0 | case LLM_ARCH_ERNIE4_5: |
2143 | 0 | case LLM_ARCH_ERNIE4_5_MOE: |
2144 | 0 | case LLM_ARCH_PADDLEOCR: |
2145 | 0 | { |
2146 | | // paddleocr need mrope_section |
2147 | 0 | ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, false); |
2148 | |
|
2149 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2150 | 0 | if (arch == LLM_ARCH_ERNIE4_5_MOE) { |
2151 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
2152 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false); |
2153 | 0 | ml.get_key(LLM_KV_INTERLEAVE_MOE_LAYER_STEP, hparams.n_moe_layer_step); |
2154 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
2155 | 0 | } |
2156 | |
|
2157 | 0 | switch (hparams.n_layer) { |
2158 | 0 | case 18: type = LLM_TYPE_0_3B; break; |
2159 | 0 | case 28: type = LLM_TYPE_21B_A3B; break; |
2160 | 0 | case 54: type = LLM_TYPE_300B_A47B; break; |
2161 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2162 | 0 | } |
2163 | 0 | } break; |
2164 | 0 | case LLM_ARCH_FALCON_H1: |
2165 | 0 | { |
2166 | | // Common parameters |
2167 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2168 | | |
2169 | | // SSM parameters |
2170 | 0 | ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv); |
2171 | 0 | ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner); |
2172 | 0 | ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state); |
2173 | 0 | ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank); |
2174 | 0 | ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group); |
2175 | |
|
2176 | 0 | std::fill(hparams.recurrent_layer_arr.begin(), hparams.recurrent_layer_arr.end(), true); |
2177 | |
|
2178 | 0 | switch (hparams.n_layer) { |
2179 | 0 | case 36: |
2180 | 0 | type = LLM_TYPE_0_5B; break; |
2181 | 0 | case 24: |
2182 | 0 | type = LLM_TYPE_1_5B; break; |
2183 | 0 | case 66: |
2184 | 0 | type = LLM_TYPE_1B; break; |
2185 | 0 | case 32: |
2186 | 0 | type = LLM_TYPE_3B; break; |
2187 | 0 | case 44: |
2188 | 0 | type = LLM_TYPE_7B; break; |
2189 | 0 | case 72: |
2190 | 0 | type = LLM_TYPE_34B; break; |
2191 | 0 | default: |
2192 | 0 | type = LLM_TYPE_UNKNOWN; |
2193 | 0 | } |
2194 | 0 | } break; |
2195 | 0 | case LLM_ARCH_HUNYUAN_MOE: |
2196 | 0 | { |
2197 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2198 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
2199 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false); |
2200 | |
|
2201 | 0 | switch (hparams.n_layer) { |
2202 | 0 | case 32: type = LLM_TYPE_A13B; break; |
2203 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2204 | 0 | } |
2205 | 0 | } break; |
2206 | 0 | case LLM_ARCH_HUNYUAN_DENSE: |
2207 | 0 | { |
2208 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2209 | |
|
2210 | 0 | switch (hparams.n_embd) { |
2211 | 0 | case 1024: type = LLM_TYPE_0_5B; break; |
2212 | 0 | case 2048: type = LLM_TYPE_1_8B; break; |
2213 | 0 | case 3072: type = LLM_TYPE_4B; break; |
2214 | 0 | case 4096: type = LLM_TYPE_7B; break; |
2215 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2216 | 0 | } |
2217 | 0 | } break; |
2218 | 0 | case LLM_ARCH_SMOLLM3: |
2219 | 0 | { |
2220 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2221 | 0 | hparams.n_no_rope_layer_step = 4; |
2222 | |
|
2223 | 0 | switch (hparams.n_layer) { |
2224 | 0 | case 36: type = LLM_TYPE_3B; break; |
2225 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2226 | 0 | } |
2227 | 0 | } break; |
2228 | 0 | case LLM_ARCH_OPENAI_MOE: |
2229 | 0 | { |
2230 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2231 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
2232 | 0 | ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa); |
2233 | |
|
2234 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
2235 | 0 | uint32_t swa_period = 2; |
2236 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
2237 | 0 | hparams.set_swa_pattern(swa_period); |
2238 | |
|
2239 | 0 | hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train; |
2240 | 0 | hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train; |
2241 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
2242 | |
|
2243 | 0 | switch (hparams.n_layer) { |
2244 | 0 | case 24: type = LLM_TYPE_20B; break; |
2245 | 0 | case 36: type = LLM_TYPE_120B; break; |
2246 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2247 | 0 | } |
2248 | 0 | } break; |
2249 | 0 | case LLM_ARCH_LFM2: |
2250 | 0 | { |
2251 | 0 | ml.get_key(LLM_KV_SHORTCONV_L_CACHE, hparams.n_shortconv_l_cache); |
2252 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2253 | 0 | for (uint32_t il = 0; il < hparams.n_layer; ++il) { |
2254 | 0 | hparams.recurrent_layer_arr[il] = hparams.n_head_kv(il) == 0; |
2255 | 0 | } |
2256 | 0 | hparams.n_layer_dense_lead = hparams.n_layer; |
2257 | 0 | switch (hparams.n_ff()) { |
2258 | 0 | case 4608: type = LLM_TYPE_350M; break; |
2259 | 0 | case 6912: type = LLM_TYPE_700M; break; |
2260 | 0 | case 8192: type = LLM_TYPE_1_2B; break; |
2261 | 0 | case 10752: type = LLM_TYPE_2_6B; break; |
2262 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2263 | 0 | } |
2264 | 0 | if (const auto is_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); is_swa && hparams.n_swa > 0) { |
2265 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
2266 | 0 | for (uint32_t il = 0; il < hparams.n_layer; ++il) { |
2267 | 0 | hparams.swa_layers[il] = !hparams.recurrent_layer_arr[il]; |
2268 | 0 | } |
2269 | 0 | } |
2270 | 0 | } break; |
2271 | 0 | case LLM_ARCH_LFM2MOE: |
2272 | 0 | { |
2273 | 0 | ml.get_key(LLM_KV_SHORTCONV_L_CACHE, hparams.n_shortconv_l_cache); |
2274 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2275 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
2276 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
2277 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func); |
2278 | |
|
2279 | 0 | for (uint32_t il = 0; il < hparams.n_layer; ++il) { |
2280 | 0 | hparams.recurrent_layer_arr[il] = hparams.n_head_kv(il) == 0; |
2281 | 0 | } |
2282 | |
|
2283 | 0 | switch (hparams.n_layer) { |
2284 | 0 | case 24: type = LLM_TYPE_8B_A1B; break; |
2285 | 0 | case 40: type = LLM_TYPE_24B_A2B; break; |
2286 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2287 | 0 | } |
2288 | 0 | } break; |
2289 | 0 | case LLM_ARCH_SMALLTHINKER: |
2290 | 0 | { |
2291 | 0 | const bool found_swa = ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa, false); |
2292 | |
|
2293 | 0 | if (found_swa && hparams.n_swa > 0) { |
2294 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
2295 | 0 | hparams.n_swa = 4096; |
2296 | 0 | uint32_t swa_period = 4; |
2297 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, swa_period, false); |
2298 | 0 | hparams.set_swa_pattern(swa_period, true); |
2299 | |
|
2300 | 0 | hparams.rope_freq_base_train_swa = hparams.rope_freq_base_train; |
2301 | 0 | hparams.rope_freq_scale_train_swa = hparams.rope_freq_scale_train; |
2302 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
2303 | 0 | } else { |
2304 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_NONE; |
2305 | 0 | hparams.n_no_rope_layer_step = hparams.n_layer; |
2306 | 0 | } |
2307 | |
|
2308 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false); |
2309 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2310 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false); |
2311 | |
|
2312 | 0 | switch (hparams.n_layer) { |
2313 | 0 | case 32: type = LLM_TYPE_4B; break; |
2314 | 0 | case 52: type = LLM_TYPE_20B; break; |
2315 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2316 | 0 | } |
2317 | 0 | } break; |
2318 | 0 | case LLM_ARCH_GROVEMOE: |
2319 | 0 | { |
2320 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
2321 | 0 | ml.get_key(LLM_KV_EXPERT_CHUNK_FEED_FORWARD_LENGTH, hparams.n_ff_chexp, false); |
2322 | 0 | ml.get_key(LLM_KV_EXPERT_GROUP_SCALE, hparams.expert_group_scale); |
2323 | 0 | ml.get_key(LLM_KV_EXPERTS_PER_GROUP, hparams.n_group_experts); |
2324 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2325 | |
|
2326 | 0 | switch (hparams.n_layer) { |
2327 | 0 | case 48: type = LLM_TYPE_30B_A3B; break; |
2328 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2329 | 0 | } |
2330 | 0 | } break; |
2331 | 0 | case LLM_ARCH_APERTUS: |
2332 | 0 | { |
2333 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2334 | 0 | ml.get_key_or_arr(LLM_KV_XIELU_ALPHA_N, hparams.xielu_alpha_n, hparams.n_layer); |
2335 | 0 | ml.get_key_or_arr(LLM_KV_XIELU_ALPHA_P, hparams.xielu_alpha_p, hparams.n_layer); |
2336 | 0 | ml.get_key_or_arr(LLM_KV_XIELU_BETA, hparams.xielu_beta, hparams.n_layer); |
2337 | 0 | ml.get_key_or_arr(LLM_KV_XIELU_EPS, hparams.xielu_eps, hparams.n_layer); |
2338 | |
|
2339 | 0 | switch (hparams.n_layer) { |
2340 | 0 | case 32: type = LLM_TYPE_8B; break; |
2341 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2342 | 0 | } |
2343 | 0 | } break; |
2344 | 0 | case LLM_ARCH_MINIMAX_M2: |
2345 | 0 | { |
2346 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2347 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
2348 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false); |
2349 | |
|
2350 | 0 | switch (hparams.n_layer) { |
2351 | 0 | case 62: type = LLM_TYPE_230B_A10B; break; |
2352 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2353 | 0 | } |
2354 | 0 | } break; |
2355 | 0 | case LLM_ARCH_COGVLM: |
2356 | 0 | { |
2357 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2358 | 0 | switch (hparams.n_layer) { |
2359 | 0 | case 32: type = LLM_TYPE_13B; break; |
2360 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2361 | 0 | } |
2362 | 0 | } break; |
2363 | 0 | case LLM_ARCH_PANGU_EMBED: |
2364 | 0 | { |
2365 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2366 | 0 | switch (hparams.n_layer) { |
2367 | 0 | case 26: type = LLM_TYPE_1B; break; // openPangu-Embedded-1B-V1.1 |
2368 | 0 | case 34: type = LLM_TYPE_7B; break; // openPangu-Embedded-7B-V1.1 |
2369 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2370 | 0 | } |
2371 | 0 | } break; |
2372 | 0 | case LLM_ARCH_QWEN3NEXT: |
2373 | 0 | { |
2374 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false); |
2375 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false); |
2376 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2377 | | |
2378 | | // Load linear attention (gated delta net) parameters |
2379 | 0 | ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv); |
2380 | 0 | ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner); |
2381 | 0 | ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state); |
2382 | 0 | ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank); |
2383 | 0 | ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group); |
2384 | | |
2385 | | // Mark recurrent layers (linear attention layers) |
2386 | 0 | { |
2387 | 0 | uint32_t full_attn_interval = 4; |
2388 | 0 | ml.get_key(LLM_KV_FULL_ATTENTION_INTERVAL, full_attn_interval, false); |
2389 | 0 | for (uint32_t i = 0; i < hparams.n_layer; ++i) { |
2390 | 0 | hparams.recurrent_layer_arr[i] = ((i + 1) % full_attn_interval != 0); |
2391 | 0 | } |
2392 | 0 | } |
2393 | |
|
2394 | 0 | switch (hparams.n_layer) { |
2395 | 0 | case 48: type = LLM_TYPE_80B_A3B; break; |
2396 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2397 | 0 | } |
2398 | 0 | } break; |
2399 | 0 | case LLM_ARCH_QWEN35: |
2400 | 0 | { |
2401 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2402 | 0 | ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, true); |
2403 | | |
2404 | | // Load linear attention (gated delta net) parameters |
2405 | 0 | ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv); |
2406 | 0 | ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner); |
2407 | 0 | ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state); |
2408 | 0 | ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank); |
2409 | 0 | ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group); |
2410 | | |
2411 | | // Mark recurrent layers (linear attention layers) |
2412 | 0 | { |
2413 | 0 | uint32_t full_attn_interval = 4; |
2414 | 0 | ml.get_key(LLM_KV_FULL_ATTENTION_INTERVAL, full_attn_interval, false); |
2415 | 0 | for (uint32_t i = 0; i < hparams.n_layer; ++i) { |
2416 | 0 | hparams.recurrent_layer_arr[i] = ((i + 1) % full_attn_interval != 0); |
2417 | 0 | } |
2418 | 0 | } |
2419 | |
|
2420 | 0 | switch (hparams.n_layer) { |
2421 | 0 | case 24: type = hparams.n_embd == 1024 ? LLM_TYPE_0_8B : LLM_TYPE_2B; break; |
2422 | 0 | case 32: type = hparams.n_embd == 2560 ? LLM_TYPE_4B : LLM_TYPE_9B; break; |
2423 | 0 | case 64: type = LLM_TYPE_27B; break; |
2424 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2425 | 0 | } |
2426 | 0 | } break; |
2427 | 0 | case LLM_ARCH_QWEN35MOE: |
2428 | 0 | { |
2429 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp, false); |
2430 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false); |
2431 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2432 | |
|
2433 | 0 | ml.get_key_or_arr(LLM_KV_ROPE_DIMENSION_SECTIONS, hparams.rope_sections, 4, true); |
2434 | | |
2435 | | // Load linear attention (gated delta net) parameters |
2436 | 0 | ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv); |
2437 | 0 | ml.get_key(LLM_KV_SSM_INNER_SIZE, hparams.ssm_d_inner); |
2438 | 0 | ml.get_key(LLM_KV_SSM_STATE_SIZE, hparams.ssm_d_state); |
2439 | 0 | ml.get_key(LLM_KV_SSM_TIME_STEP_RANK, hparams.ssm_dt_rank); |
2440 | 0 | ml.get_key(LLM_KV_SSM_GROUP_COUNT, hparams.ssm_n_group); |
2441 | | |
2442 | | // Mark recurrent layers (linear attention layers) |
2443 | 0 | { |
2444 | 0 | uint32_t full_attn_interval = 4; |
2445 | 0 | ml.get_key(LLM_KV_FULL_ATTENTION_INTERVAL, full_attn_interval, false); |
2446 | 0 | for (uint32_t i = 0; i < hparams.n_layer; ++i) { |
2447 | 0 | hparams.recurrent_layer_arr[i] = ((i + 1) % full_attn_interval != 0); |
2448 | 0 | } |
2449 | 0 | } |
2450 | |
|
2451 | 0 | switch (hparams.n_layer) { |
2452 | 0 | case 40: type = LLM_TYPE_35B_A3B; break; |
2453 | 0 | case 48: type = LLM_TYPE_122B_A10B; break; |
2454 | 0 | case 60: type = LLM_TYPE_397B_A17B; break; |
2455 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2456 | 0 | } |
2457 | 0 | } break; |
2458 | 0 | case LLM_ARCH_MISTRAL3: |
2459 | 0 | { |
2460 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2461 | 0 | ml.get_key(LLM_KV_ATTENTION_TEMPERATURE_SCALE, hparams.f_attn_temp_scale, false); |
2462 | |
|
2463 | 0 | ml.get_key(LLM_KV_ROPE_SCALING_YARN_BETA_FAST, hparams.yarn_beta_fast, false); |
2464 | 0 | ml.get_key(LLM_KV_ROPE_SCALING_YARN_BETA_SLOW, hparams.yarn_beta_slow, false); |
2465 | 0 | ml.get_key(LLM_KV_ROPE_SCALING_YARN_LOG_MUL, hparams.rope_yarn_log_mul, 0.0f); |
2466 | |
|
2467 | 0 | hparams.f_attn_temp_offset = 0.0f; |
2468 | | |
2469 | | // TODO: maybe add n_attn_temp_floor_scale as a separate KV? |
2470 | 0 | if (hparams.f_attn_temp_scale != 0.0f) { |
2471 | 0 | hparams.n_attn_temp_floor_scale = hparams.n_ctx_orig_yarn; |
2472 | 0 | if (hparams.n_attn_temp_floor_scale == 0) { |
2473 | 0 | throw std::runtime_error("invalid n_ctx_orig_yarn for attention temperature scaling"); |
2474 | 0 | } |
2475 | 0 | } |
2476 | | |
2477 | 0 | switch (hparams.n_layer) { |
2478 | 0 | case 26: type = LLM_TYPE_3B; break; |
2479 | 0 | case 34: type = LLM_TYPE_8B; break; |
2480 | 0 | case 40: type = LLM_TYPE_14B; break; |
2481 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2482 | 0 | } |
2483 | 0 | } break; |
2484 | 0 | case LLM_ARCH_MIMO2: |
2485 | 0 | { |
2486 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2487 | |
|
2488 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
2489 | |
|
2490 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
2491 | 0 | ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa); |
2492 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
2493 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, hparams.swa_layers, hparams.n_layer); |
2494 | |
|
2495 | 0 | switch (hparams.n_layer) { |
2496 | 0 | case 48: type = LLM_TYPE_310B_A15B; break; |
2497 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2498 | 0 | } |
2499 | 0 | } break; |
2500 | 0 | case LLM_ARCH_KIMI_LINEAR: |
2501 | 0 | { |
2502 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2503 | 0 | ml.get_key(LLM_KV_ATTENTION_KEY_LENGTH_MLA, hparams.n_embd_head_k_mla_impl); |
2504 | 0 | ml.get_key(LLM_KV_ATTENTION_VALUE_LENGTH_MLA, hparams.n_embd_head_v_mla_impl); |
2505 | 0 | ml.get_key(LLM_KV_ATTENTION_KV_LORA_RANK, hparams.n_lora_kv); |
2506 | 0 | ml.get_key(LLM_KV_SSM_CONV_KERNEL, hparams.ssm_d_conv); |
2507 | 0 | ml.get_key(LLM_KV_KDA_HEAD_DIM, hparams.n_embd_head_kda); |
2508 | | |
2509 | | // MLA qk_rope_head_dim (for reference) |
2510 | | // qk_rope_head_dim = 64, qk_nope_head_dim = 128, qk_head_dim = 192 |
2511 | | |
2512 | | // Mark KDA layers as recurrent using n_head_kv pattern (like Jamba) |
2513 | | // Set n_head_kv = 0 for KDA layers (recurrent), n_head_kv = n_head for MLA layers (attention) |
2514 | 0 | for (uint32_t i = 0; i < hparams.n_layer; ++i) { |
2515 | 0 | hparams.recurrent_layer_arr[i] = hparams.n_head_kv(i) == 0; // KDA layers are recurrent |
2516 | 0 | } |
2517 | | |
2518 | | // MoE parameters - Kimi uses moe_intermediate_size = 1024 |
2519 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
2520 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_COUNT, hparams.n_expert_shared); |
2521 | 0 | ml.get_key(LLM_KV_LEADING_DENSE_BLOCK_COUNT, hparams.n_layer_dense_lead, false); |
2522 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
2523 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func); |
2524 | |
|
2525 | 0 | switch (hparams.n_layer) { |
2526 | 0 | case 27: type = LLM_TYPE_48B_A3B; break; // Kimi-Linear-48B-A3B |
2527 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2528 | 0 | } |
2529 | 0 | } break; |
2530 | 0 | case LLM_ARCH_STEP35: |
2531 | 0 | { |
2532 | 0 | ml.get_key(LLM_KV_ATTENTION_LAYERNORM_RMS_EPS, hparams.f_norm_rms_eps); |
2533 | |
|
2534 | 0 | hparams.swa_type = LLAMA_SWA_TYPE_STANDARD; |
2535 | | |
2536 | | // full_attention layer only use half of the RoPE dimensions |
2537 | 0 | hparams.n_rot_full = hparams.n_rot_full / 2; |
2538 | | |
2539 | | // MoE + SWA parameters |
2540 | 0 | ml.get_key(LLM_KV_EXPERT_FEED_FORWARD_LENGTH, hparams.n_ff_exp); |
2541 | 0 | ml.get_key(LLM_KV_EXPERT_SHARED_FEED_FORWARD_LENGTH, hparams.n_ff_shexp, false); |
2542 | 0 | ml.get_key(LLM_KV_EXPERT_GATING_FUNC, hparams.expert_gating_func, false); |
2543 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_SCALE, hparams.expert_weights_scale, false); |
2544 | 0 | ml.get_key(LLM_KV_EXPERT_WEIGHTS_NORM, hparams.expert_weights_norm, false); |
2545 | | |
2546 | | // Step35 uses sigmoid gating by default (if not set in GGUF) |
2547 | 0 | if (hparams.expert_gating_func == LLAMA_EXPERT_GATING_FUNC_TYPE_NONE) { |
2548 | 0 | hparams.expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID; |
2549 | 0 | } |
2550 | |
|
2551 | 0 | ml.get_key(LLM_KV_ATTENTION_SLIDING_WINDOW, hparams.n_swa); |
2552 | 0 | ml.get_key(LLM_KV_ROPE_FREQ_BASE_SWA, hparams.rope_freq_base_train_swa, false); |
2553 | 0 | ml.get_key_or_arr(LLM_KV_ATTENTION_SLIDING_WINDOW_PATTERN, hparams.swa_layers, hparams.n_layer); |
2554 | 0 | ml.get_key_or_arr(LLM_KV_SWIGLU_CLAMP_EXP, hparams.swiglu_clamp_exp, hparams.n_layer, false); |
2555 | 0 | ml.get_key_or_arr(LLM_KV_SWIGLU_CLAMP_SHEXP, hparams.swiglu_clamp_shexp, hparams.n_layer, false); |
2556 | |
|
2557 | 0 | switch (hparams.n_layer) { |
2558 | 0 | case 45: type = LLM_TYPE_196B_A11B; break; |
2559 | 0 | default: type = LLM_TYPE_UNKNOWN; |
2560 | 0 | } |
2561 | 0 | } break; |
2562 | 0 | default: throw std::runtime_error("unsupported model architecture: " + arch_name()); |
2563 | 0 | } |
2564 | | |
2565 | 0 | pimpl->n_bytes = ml.n_bytes; |
2566 | |
|
2567 | 0 | pimpl->desc_str = arch_name() + " " + type_name() + " " + ml.ftype_name(); |
2568 | |
|
2569 | 0 | if (hparams.f_max_alibi_bias > 0.0f) { |
2570 | 0 | hparams.use_alibi = true; |
2571 | 0 | } |
2572 | |
|
2573 | 0 | hparams.rope_type = llama_model_rope_type(this); |
2574 | 0 | } |
2575 | | |
2576 | 0 | void llama_model::load_vocab(llama_model_loader & ml) { |
2577 | 0 | const auto kv = LLM_KV(arch); |
2578 | |
|
2579 | 0 | vocab.load(ml, kv); |
2580 | 0 | } |
2581 | | |
2582 | 0 | bool llama_model::load_tensors(llama_model_loader & ml) { |
2583 | 0 | const auto & split_mode = params.split_mode; |
2584 | 0 | const auto & use_mlock = params.use_mlock; |
2585 | 0 | const auto & tensor_split = params.tensor_split; |
2586 | |
|
2587 | 0 | const int n_layer = hparams.n_layer; |
2588 | 0 | const int n_gpu_layers = this->n_gpu_layers(); |
2589 | |
|
2590 | 0 | const bool use_mmap_buffer = true; |
2591 | |
|
2592 | 0 | LLAMA_LOG_INFO("%s: loading model tensors, this can take a while... (mmap = %s, direct_io = %s)\n", |
2593 | 0 | __func__, ml.use_mmap ? "true" : "false", ml.use_direct_io ? "true" : "false"); |
2594 | | |
2595 | | // build a list of buffer types for the CPU and GPU devices |
2596 | 0 | pimpl->cpu_buft_list = make_cpu_buft_list(devices, params.use_extra_bufts, params.no_host); |
2597 | 0 | for (auto * dev : devices) { |
2598 | 0 | buft_list_t buft_list = make_gpu_buft_list(dev, split_mode, tensor_split); |
2599 | | // add CPU buffer types as a fallback |
2600 | 0 | buft_list.insert(buft_list.end(), pimpl->cpu_buft_list.begin(), pimpl->cpu_buft_list.end()); |
2601 | 0 | pimpl->gpu_buft_list.emplace(dev, std::move(buft_list)); |
2602 | 0 | } |
2603 | |
|
2604 | 0 | ggml_backend_dev_t cpu_dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU); |
2605 | 0 | if (cpu_dev == nullptr) { |
2606 | 0 | throw std::runtime_error(format("%s: no CPU backend found", __func__)); |
2607 | 0 | } |
2608 | | |
2609 | | // calculate the split points |
2610 | 0 | bool all_zero = tensor_split == nullptr || std::all_of(tensor_split, tensor_split + n_devices(), [](float x) { return x == 0.0f; }); |
2611 | 0 | std::vector<float> splits(n_devices()); |
2612 | 0 | if (all_zero) { |
2613 | | // default split, by free memory |
2614 | 0 | for (size_t i = 0; i < n_devices(); ++i) { |
2615 | 0 | ggml_backend_dev_t dev = devices[i]; |
2616 | 0 | size_t total; |
2617 | 0 | size_t free; |
2618 | 0 | ggml_backend_dev_memory(dev, &free, &total); |
2619 | | |
2620 | | // devices can return 0 bytes for free and total memory if they do not |
2621 | | // have any to report. in this case, we will use the host memory as a fallback |
2622 | | // fixes: https://github.com/ggml-org/llama.cpp/issues/18577 |
2623 | 0 | if (free == 0 && total == 0) { |
2624 | 0 | ggml_backend_dev_memory(cpu_dev, &free, &total); |
2625 | 0 | } |
2626 | 0 | splits[i] = free; |
2627 | 0 | } |
2628 | 0 | } else { |
2629 | 0 | std::copy(tensor_split, tensor_split + n_devices(), splits.begin()); |
2630 | 0 | } |
2631 | | |
2632 | | // sum and normalize the splits to get the split points |
2633 | 0 | float split_sum = 0.0f; |
2634 | 0 | for (size_t i = 0; i < n_devices(); ++i) { |
2635 | 0 | split_sum += splits[i]; |
2636 | 0 | splits[i] = split_sum; |
2637 | 0 | } |
2638 | 0 | for (size_t i = 0; i < n_devices(); ++i) { |
2639 | 0 | splits[i] /= split_sum; |
2640 | 0 | } |
2641 | |
|
2642 | 0 | const int i_gpu_start = std::max(int(hparams.n_layer) + 1 - n_gpu_layers, 0); |
2643 | 0 | const int act_gpu_layers = devices.empty() ? 0 : std::min(n_gpu_layers, int(n_layer) + 1); |
2644 | 0 | auto get_layer_buft_list = [&](int il) -> llama_model::impl::layer_dev { |
2645 | 0 | const bool is_swa = il < int(hparams.n_layer) && hparams.is_swa(il); |
2646 | 0 | if (il < i_gpu_start || (il - i_gpu_start) >= act_gpu_layers) { |
2647 | 0 | LLAMA_LOG_DEBUG("load_tensors: layer %3d assigned to device %s, is_swa = %d\n", il, ggml_backend_dev_name(cpu_dev), is_swa); |
2648 | 0 | return {cpu_dev, &pimpl->cpu_buft_list}; |
2649 | 0 | } |
2650 | 0 | const int layer_gpu = std::upper_bound(splits.begin(), splits.begin() + n_devices(), float(il - i_gpu_start)/act_gpu_layers) - splits.begin(); |
2651 | 0 | auto * dev = devices.at(layer_gpu); |
2652 | 0 | LLAMA_LOG_DEBUG("load_tensors: layer %3d assigned to device %s, is_swa = %d\n", il, ggml_backend_dev_name(dev), is_swa); |
2653 | 0 | return {dev, &pimpl->gpu_buft_list.at(dev)}; |
2654 | 0 | }; |
2655 | | |
2656 | | // assign the input layer |
2657 | | // there is very little benefit to offloading the input layer, so always keep it on the CPU |
2658 | 0 | pimpl->dev_input = { cpu_dev, &pimpl->cpu_buft_list }; |
2659 | | |
2660 | | // assign the repeating layers to the devices according to the splits |
2661 | 0 | pimpl->dev_layer.resize(n_layer); |
2662 | 0 | for (int il = 0; il < n_layer; ++il) { |
2663 | 0 | pimpl->dev_layer[il] = get_layer_buft_list(il); |
2664 | 0 | } |
2665 | | |
2666 | | // assign the output layer |
2667 | 0 | pimpl->dev_output = get_layer_buft_list(n_layer); |
2668 | |
|
2669 | 0 | const auto TENSOR_DUPLICATED = llama_model_loader::TENSOR_DUPLICATED; |
2670 | 0 | const auto TENSOR_NOT_REQUIRED = llama_model_loader::TENSOR_NOT_REQUIRED; |
2671 | 0 | const auto TENSOR_SKIP = llama_model_loader::TENSOR_SKIP; |
2672 | 0 | const auto TENSOR_SKIP_IF_VIRTUAL = llama_model_loader::TENSOR_SKIP_IF_VIRTUAL; |
2673 | | |
2674 | | // create tensors for the weights |
2675 | 0 | { |
2676 | | // note: cast to int64_t since we will use these for the tensor dimensions |
2677 | 0 | const int64_t n_head = hparams.n_head(); |
2678 | 0 | const int64_t n_head_kv = hparams.n_head_kv(); |
2679 | 0 | const int64_t n_embd = hparams.n_embd; |
2680 | 0 | const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa(); |
2681 | 0 | const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa(); |
2682 | 0 | const int64_t n_embd_head_k = hparams.n_embd_head_k(); |
2683 | 0 | const int64_t n_embd_head_v = hparams.n_embd_head_v(); |
2684 | 0 | const int64_t n_ff = hparams.n_ff(); |
2685 | 0 | const int64_t n_embd_gqa = n_embd_v_gqa; |
2686 | 0 | const int64_t n_vocab = vocab.n_tokens(); |
2687 | 0 | const int64_t n_token_types = vocab.n_token_types(); |
2688 | 0 | const int64_t n_rot = hparams.n_rot(); |
2689 | 0 | const int64_t n_expert = hparams.n_expert; |
2690 | 0 | const int64_t n_expert_used = hparams.n_expert_used; |
2691 | 0 | const int64_t n_ctx_train = hparams.n_ctx_train; |
2692 | |
|
2693 | 0 | if (n_expert > 0 && hparams.n_expert_used == 0) { |
2694 | 0 | throw std::runtime_error("model has expert layers but no expert layers are used"); |
2695 | 0 | } |
2696 | | |
2697 | 0 | auto create_tensor = [&](const LLM_TN_IMPL & tn, const std::initializer_list<int64_t> & ne, int flags) -> ggml_tensor * { |
2698 | 0 | const buft_list_t * buft_list_layer = tn.bid == -1 ? nullptr : pimpl->dev_layer.at(tn.bid).buft_list; |
2699 | 0 | return ml.create_tensor( |
2700 | 0 | hparams, &pimpl->cpu_buft_list, pimpl->dev_input.buft_list, pimpl->dev_output.buft_list, buft_list_layer, |
2701 | 0 | tn, ne, flags); |
2702 | 0 | }; |
2703 | |
|
2704 | 0 | layers.resize(n_layer); |
2705 | | |
2706 | | // TODO: move to a separate function |
2707 | 0 | const auto tn = LLM_TN(arch); |
2708 | | |
2709 | | // helper: try merged gate_up_exps first, fall back to separate gate and up |
2710 | 0 | auto create_tensor_gate_up_exps = [&](llama_layer & layer, int bid, int64_t n_embd_, int64_t n_ff_, int64_t n_expert_, int flags) { |
2711 | 0 | layer.ffn_gate_up_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_UP_EXPS, "weight", bid), {n_embd_, n_ff_ * 2, n_expert_}, TENSOR_NOT_REQUIRED); |
2712 | 0 | if (layer.ffn_gate_up_exps == nullptr) { |
2713 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", bid), {n_embd_, n_ff_, n_expert_}, flags); |
2714 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", bid), {n_embd_, n_ff_, n_expert_}, flags); |
2715 | 0 | } |
2716 | 0 | }; |
2717 | 0 | switch (arch) { |
2718 | 0 | case LLM_ARCH_LLAMA: |
2719 | 0 | case LLM_ARCH_REFACT: |
2720 | 0 | case LLM_ARCH_MINICPM: |
2721 | 0 | case LLM_ARCH_GRANITE: |
2722 | 0 | case LLM_ARCH_GRANITE_MOE: |
2723 | 0 | case LLM_ARCH_MISTRAL3: |
2724 | 0 | case LLM_ARCH_LLAMA_EMBED: |
2725 | 0 | { |
2726 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
2727 | | |
2728 | | // output |
2729 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
2730 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
2731 | | |
2732 | | // if output is NULL, init from the input tok embed |
2733 | 0 | if (output == NULL) { |
2734 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
2735 | 0 | } |
2736 | |
|
2737 | 0 | for (int i = 0; i < n_layer; ++i) { |
2738 | 0 | auto & layer = layers[i]; |
2739 | |
|
2740 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
2741 | |
|
2742 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
2743 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
2744 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
2745 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
2746 | | |
2747 | | // optional bias tensors |
2748 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
2749 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED); |
2750 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED); |
2751 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
2752 | |
|
2753 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
2754 | |
|
2755 | 0 | if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) { |
2756 | 0 | layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
2757 | 0 | layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
2758 | 0 | } |
2759 | 0 | else { |
2760 | 0 | layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
2761 | 0 | } |
2762 | |
|
2763 | 0 | if (n_expert == 0) { |
2764 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
2765 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
2766 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
2767 | | |
2768 | | // optional MLP bias |
2769 | 0 | layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED); |
2770 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
2771 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED); |
2772 | 0 | } else { |
2773 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
2774 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED); |
2775 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0); |
2776 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
2777 | | |
2778 | | // For Granite MoE Shared |
2779 | 0 | if (hparams.n_ff_shexp > 0) { |
2780 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0); |
2781 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0); |
2782 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0); |
2783 | 0 | } |
2784 | 0 | } |
2785 | 0 | } |
2786 | 0 | } break; |
2787 | 0 | case LLM_ARCH_LLADA: |
2788 | 0 | { |
2789 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0); |
2790 | | |
2791 | | // output |
2792 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0); |
2793 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED); |
2794 | | |
2795 | | // if output is NULL, init from the input tok embed |
2796 | 0 | if (output == NULL) { |
2797 | 0 | output = |
2798 | 0 | create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED); |
2799 | 0 | } |
2800 | |
|
2801 | 0 | for (int i = 0; i < n_layer; ++i) { |
2802 | 0 | auto & layer = layers[i]; |
2803 | |
|
2804 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0); |
2805 | | |
2806 | | // Use separate Q, K, V projections without bias, matching LLaDALlamaBlock |
2807 | 0 | layer.wq = |
2808 | 0 | create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, 0); |
2809 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_k_gqa }, 0); |
2810 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_v_gqa }, 0); |
2811 | | // No bias for QKV projections as per config: include_bias=false, include_qkv_bias=false |
2812 | 0 | layer.wo = |
2813 | 0 | create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0); |
2814 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED); |
2815 | |
|
2816 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0); |
2817 | |
|
2818 | 0 | layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), { n_rot / 2 }, |
2819 | 0 | TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
2820 | |
|
2821 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, 0); |
2822 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0); |
2823 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0); |
2824 | | |
2825 | | // optional MLP bias |
2826 | 0 | layer.ffn_gate_b = |
2827 | 0 | create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), { n_ff }, TENSOR_NOT_REQUIRED); |
2828 | 0 | layer.ffn_down_b = |
2829 | 0 | create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED); |
2830 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), { n_ff }, TENSOR_NOT_REQUIRED); |
2831 | 0 | } |
2832 | 0 | } |
2833 | 0 | break; |
2834 | 0 | case LLM_ARCH_LLADA_MOE: |
2835 | 0 | { |
2836 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
2837 | | |
2838 | | // output |
2839 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
2840 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
2841 | |
|
2842 | 0 | GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for llada-moe"); |
2843 | 0 | GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for llada-moe"); |
2844 | |
|
2845 | 0 | for (int i = 0; i < n_layer; ++i) { |
2846 | 0 | auto & layer = layers[i]; |
2847 | |
|
2848 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
2849 | |
|
2850 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
2851 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
2852 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
2853 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
2854 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
2855 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
2856 | |
|
2857 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
2858 | |
|
2859 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
2860 | |
|
2861 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used; |
2862 | |
|
2863 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
2864 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0); |
2865 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
2866 | 0 | } |
2867 | 0 | } break; |
2868 | 0 | case LLM_ARCH_LLAMA4: |
2869 | 0 | { |
2870 | 0 | if (n_expert == 0) { |
2871 | 0 | throw std::runtime_error(arch_name() + " model cannot have zero experts"); |
2872 | 0 | } |
2873 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
2874 | | |
2875 | | // output |
2876 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
2877 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
2878 | | |
2879 | | // if output is NULL, init from the input tok embed |
2880 | 0 | if (output == NULL) { |
2881 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
2882 | 0 | } |
2883 | |
|
2884 | 0 | for (int i = 0; i < n_layer; ++i) { |
2885 | 0 | const bool is_moe_layer = hparams.n_moe_layer_step > 0 && (i + 1) % hparams.n_moe_layer_step == 0; |
2886 | |
|
2887 | 0 | auto & layer = layers[i]; |
2888 | |
|
2889 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
2890 | |
|
2891 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
2892 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
2893 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
2894 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
2895 | |
|
2896 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
2897 | |
|
2898 | 0 | layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
2899 | |
|
2900 | 0 | if (is_moe_layer) { |
2901 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
2902 | |
|
2903 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
2904 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0); |
2905 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert}, 0); |
2906 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0); |
2907 | | |
2908 | | // Shared expert |
2909 | 0 | const int64_t n_ff_shexp = n_ff_exp; |
2910 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0); |
2911 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd }, 0); |
2912 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0); |
2913 | 0 | } else { |
2914 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
2915 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
2916 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
2917 | 0 | } |
2918 | 0 | } |
2919 | 0 | } break; |
2920 | 0 | case LLM_ARCH_DECI: |
2921 | 0 | { |
2922 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
2923 | | |
2924 | | // output |
2925 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
2926 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
2927 | | |
2928 | | // if output is NULL, init from the input tok embed |
2929 | 0 | if (output == NULL) { |
2930 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
2931 | 0 | } |
2932 | |
|
2933 | 0 | for (int i = 0; i < n_layer; ++i) { |
2934 | 0 | auto & layer = layers[i]; |
2935 | 0 | const int64_t n_embd_k_gqa = hparams.n_embd_k_gqa(i); |
2936 | 0 | const int64_t n_embd_v_gqa = hparams.n_embd_v_gqa(i); |
2937 | 0 | const int64_t n_embd_gqa = hparams.n_embd_v_gqa(i); |
2938 | 0 | const int64_t n_ff = hparams.n_ff(i); |
2939 | 0 | const int64_t n_head = hparams.n_head(i); |
2940 | 0 | const int64_t n_head_kv = hparams.n_head_kv(i); |
2941 | |
|
2942 | 0 | if (n_head_kv == 0 && n_head > 0) { |
2943 | | // linear attention for DeciLMCausalModel |
2944 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
2945 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
2946 | 0 | } |
2947 | 0 | else if (n_head_kv > 0) { |
2948 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
2949 | |
|
2950 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
2951 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
2952 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
2953 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
2954 | 0 | } |
2955 | | |
2956 | | // optional bias tensors |
2957 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
2958 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED); |
2959 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED); |
2960 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
2961 | |
|
2962 | 0 | if (n_ff > 0) { |
2963 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
2964 | 0 | } |
2965 | |
|
2966 | 0 | if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) { |
2967 | 0 | layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
2968 | 0 | layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
2969 | 0 | } |
2970 | 0 | else { |
2971 | 0 | layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
2972 | 0 | } |
2973 | |
|
2974 | 0 | if (n_ff > 0) { |
2975 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
2976 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
2977 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
2978 | 0 | } |
2979 | | |
2980 | | // optional MLP bias |
2981 | 0 | layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED); |
2982 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
2983 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED); |
2984 | 0 | } |
2985 | 0 | } break; |
2986 | 0 | case LLM_ARCH_MINICPM3: |
2987 | 0 | { |
2988 | 0 | const int64_t n_embd_head_qk_rope = hparams.n_rot(); |
2989 | 0 | const int64_t n_embd_head_qk_nope = hparams.n_embd_head_k() - hparams.n_rot(); |
2990 | |
|
2991 | 0 | const int64_t q_lora_rank = hparams.n_lora_q; |
2992 | 0 | const int64_t kv_lora_rank = hparams.n_lora_kv; |
2993 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
2994 | | |
2995 | | // output |
2996 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
2997 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
2998 | | |
2999 | | // if output is NULL, init from the input tok embed |
3000 | 0 | if (output == NULL) { |
3001 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
3002 | 0 | } |
3003 | |
|
3004 | 0 | for (int i = 0; i < n_layer; ++i) { |
3005 | 0 | auto & layer = layers[i]; |
3006 | |
|
3007 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3008 | 0 | layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, 0); |
3009 | |
|
3010 | 0 | layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0); |
3011 | |
|
3012 | 0 | layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, 0); |
3013 | 0 | layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k}, 0); |
3014 | |
|
3015 | 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); |
3016 | 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); |
3017 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_head * ( n_embd_head_v), n_embd}, 0); |
3018 | |
|
3019 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
3020 | |
|
3021 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
3022 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
3023 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
3024 | |
|
3025 | 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)); |
3026 | 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)); |
3027 | 0 | } |
3028 | 0 | } break; |
3029 | 0 | case LLM_ARCH_GROK: |
3030 | 0 | { |
3031 | 0 | if (n_expert == 0) { |
3032 | 0 | throw std::runtime_error(arch_name() + " model cannot have zero experts"); |
3033 | 0 | } |
3034 | | |
3035 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3036 | | |
3037 | | // output |
3038 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3039 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
3040 | | |
3041 | | // if output is NULL, init from the input tok embed |
3042 | 0 | if (output == NULL) { |
3043 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
3044 | 0 | } |
3045 | |
|
3046 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff/* / n_expert_used*/; // grok-1 n_ff_exp == n_ff |
3047 | 0 | for (int i = 0; i < n_layer; ++i) { |
3048 | 0 | auto & layer = layers[i]; |
3049 | |
|
3050 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3051 | |
|
3052 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
3053 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
3054 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
3055 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3056 | |
|
3057 | 0 | layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0); |
3058 | |
|
3059 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
3060 | |
|
3061 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED); |
3062 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, TENSOR_NOT_REQUIRED); |
3063 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED); |
3064 | |
|
3065 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
3066 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED); |
3067 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0); |
3068 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0); |
3069 | |
|
3070 | 0 | layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3071 | 0 | if (!layer.ffn_post_norm) { |
3072 | 0 | layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0); |
3073 | 0 | } |
3074 | 0 | } |
3075 | 0 | } break; |
3076 | 0 | case LLM_ARCH_DBRX: |
3077 | 0 | { |
3078 | 0 | if (n_expert == 0) { |
3079 | 0 | throw std::runtime_error("DBRX model cannot have zero experts"); |
3080 | 0 | } |
3081 | | |
3082 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3083 | | |
3084 | | // output |
3085 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3086 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
3087 | |
|
3088 | 0 | for (int i = 0; i < n_layer; ++i) { |
3089 | 0 | auto & layer = layers[i]; |
3090 | |
|
3091 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3092 | |
|
3093 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0); |
3094 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3095 | |
|
3096 | 0 | layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0); |
3097 | |
|
3098 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
3099 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
3100 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0); |
3101 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
3102 | 0 | } |
3103 | 0 | } break; |
3104 | 0 | case LLM_ARCH_BAICHUAN: |
3105 | 0 | { |
3106 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3107 | 0 | { |
3108 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3109 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
3110 | 0 | } |
3111 | |
|
3112 | 0 | for (int i = 0; i < n_layer; ++i) { |
3113 | 0 | auto & layer = layers[i]; |
3114 | |
|
3115 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3116 | |
|
3117 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
3118 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
3119 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
3120 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3121 | |
|
3122 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
3123 | |
|
3124 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
3125 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
3126 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
3127 | 0 | } |
3128 | 0 | } break; |
3129 | 0 | case LLM_ARCH_FALCON: |
3130 | 0 | { |
3131 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3132 | | |
3133 | | // output |
3134 | 0 | { |
3135 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3136 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
3137 | |
|
3138 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
3139 | 0 | if (!output) { |
3140 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // needs to be on GPU |
3141 | 0 | } |
3142 | 0 | } |
3143 | |
|
3144 | 0 | for (int i = 0; i < n_layer; ++i) { |
3145 | 0 | auto & layer = layers[i]; |
3146 | |
|
3147 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3148 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
3149 | |
|
3150 | 0 | layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3151 | 0 | layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3152 | |
|
3153 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0); |
3154 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3155 | |
|
3156 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
3157 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
3158 | 0 | } |
3159 | 0 | } break; |
3160 | 0 | case LLM_ARCH_STARCODER: |
3161 | 0 | { |
3162 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3163 | 0 | pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0); |
3164 | | |
3165 | | // output |
3166 | 0 | { |
3167 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3168 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
3169 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
3170 | 0 | if (!output) { |
3171 | | // needs to be on GPU |
3172 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
3173 | 0 | } |
3174 | |
|
3175 | 0 | } |
3176 | |
|
3177 | 0 | for (int i = 0; i < n_layer; ++i) { |
3178 | 0 | auto & layer = layers[i]; |
3179 | |
|
3180 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3181 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
3182 | |
|
3183 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0); |
3184 | 0 | layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0); |
3185 | |
|
3186 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3187 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); |
3188 | |
|
3189 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
3190 | 0 | layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0); |
3191 | |
|
3192 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
3193 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0); |
3194 | |
|
3195 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
3196 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0); |
3197 | 0 | } |
3198 | 0 | } break; |
3199 | 0 | case LLM_ARCH_BERT: |
3200 | 0 | case LLM_ARCH_NOMIC_BERT: |
3201 | 0 | case LLM_ARCH_NOMIC_BERT_MOE: |
3202 | 0 | case LLM_ARCH_JINA_BERT_V3: |
3203 | 0 | { |
3204 | 0 | if (n_token_types == 0) { |
3205 | 0 | throw std::runtime_error(arch_name() + " model needs to define token type count"); |
3206 | 0 | } |
3207 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3208 | 0 | type_embd = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_token_types}, TENSOR_NOT_REQUIRED); |
3209 | |
|
3210 | 0 | if (arch == LLM_ARCH_BERT) { |
3211 | 0 | pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0); |
3212 | |
|
3213 | 0 | cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, n_embd}, TENSOR_NOT_REQUIRED); |
3214 | 0 | cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {n_embd}, TENSOR_NOT_REQUIRED); |
3215 | |
|
3216 | 0 | cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED); |
3217 | 0 | cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"), {hparams.n_cls_out}, TENSOR_NOT_REQUIRED); |
3218 | 0 | } |
3219 | |
|
3220 | 0 | tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0); |
3221 | 0 | tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0); |
3222 | |
|
3223 | 0 | for (int i = 0; i < n_layer; ++i) { |
3224 | 0 | auto & layer = layers[i]; |
3225 | |
|
3226 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED); |
3227 | 0 | layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED); |
3228 | |
|
3229 | 0 | if (!layer.wqkv) { |
3230 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
3231 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0); |
3232 | |
|
3233 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
3234 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0); |
3235 | |
|
3236 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
3237 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0); |
3238 | 0 | } |
3239 | |
|
3240 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3241 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3242 | |
|
3243 | 0 | layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0); |
3244 | 0 | layer.attn_out_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "bias", i), {n_embd}, 0); |
3245 | |
|
3246 | 0 | if (hparams.moe_every_n_layers > 0 && i % hparams.moe_every_n_layers == 1) { |
3247 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff, n_expert}, 0); |
3248 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0); |
3249 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
3250 | 0 | } else { |
3251 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
3252 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED); |
3253 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
3254 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3255 | |
|
3256 | 0 | if (arch == LLM_ARCH_NOMIC_BERT) { |
3257 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
3258 | 0 | } |
3259 | 0 | } |
3260 | |
|
3261 | 0 | layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, 0); |
3262 | 0 | layer.layer_out_norm_b = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "bias", i), {n_embd}, 0); |
3263 | 0 | } |
3264 | 0 | } break; |
3265 | 0 | case LLM_ARCH_MODERN_BERT: |
3266 | 0 | { |
3267 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3268 | 0 | tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0); |
3269 | |
|
3270 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3271 | |
|
3272 | 0 | for(int i = 0; i < n_layer; ++i) { |
3273 | 0 | auto& layer = layers[i]; |
3274 | |
|
3275 | 0 | if ( i != 0 ) { |
3276 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3277 | 0 | } else{ |
3278 | | // layer 0 uses identity |
3279 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3280 | 0 | } |
3281 | | |
3282 | |
|
3283 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, 3 * n_embd }, 0); |
3284 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3285 | |
|
3286 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, 2 * n_ff}, 0); |
3287 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
3288 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
3289 | 0 | } |
3290 | |
|
3291 | 0 | cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED); |
3292 | 0 | cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"), {hparams.n_cls_out}, TENSOR_NOT_REQUIRED); |
3293 | 0 | cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, n_embd}, TENSOR_NOT_REQUIRED); |
3294 | 0 | cls_norm = create_tensor(tn(LLM_TENSOR_CLS_NORM, "weight"), {n_embd}, TENSOR_NOT_REQUIRED); |
3295 | |
|
3296 | 0 | } break; |
3297 | 0 | case LLM_ARCH_NEO_BERT: |
3298 | 0 | { |
3299 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3300 | |
|
3301 | 0 | cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, n_embd}, TENSOR_NOT_REQUIRED); |
3302 | 0 | cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {n_embd}, TENSOR_NOT_REQUIRED); |
3303 | |
|
3304 | 0 | cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED); |
3305 | 0 | cls_out_b = create_tensor(tn(LLM_TENSOR_CLS_OUT, "bias"), {hparams.n_cls_out}, TENSOR_NOT_REQUIRED); |
3306 | |
|
3307 | 0 | output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3308 | |
|
3309 | 0 | for (int i = 0; i < n_layer; ++i) { |
3310 | 0 | auto & layer = layers[i]; |
3311 | |
|
3312 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3313 | |
|
3314 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0); |
3315 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3316 | |
|
3317 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
3318 | |
|
3319 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff*2}, 0); |
3320 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
3321 | 0 | } |
3322 | 0 | } break; |
3323 | 0 | case LLM_ARCH_EUROBERT: |
3324 | 0 | { |
3325 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3326 | |
|
3327 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3328 | |
|
3329 | 0 | for (int i = 0; i < n_layer; ++i) { |
3330 | 0 | auto & layer = layers[i]; |
3331 | |
|
3332 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3333 | |
|
3334 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
3335 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
3336 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
3337 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3338 | |
|
3339 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
3340 | |
|
3341 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
3342 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
3343 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
3344 | 0 | } |
3345 | 0 | } break; |
3346 | 0 | case LLM_ARCH_JINA_BERT_V2: |
3347 | 0 | { |
3348 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); // word_embeddings |
3349 | 0 | type_embd = create_tensor(tn(LLM_TENSOR_TOKEN_TYPES, "weight"), {n_embd, n_token_types}, 0); // token_type_embeddings |
3350 | |
|
3351 | 0 | tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0); // LayerNorm |
3352 | 0 | tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0); //LayerNorm bias |
3353 | |
|
3354 | 0 | cls = create_tensor(tn(LLM_TENSOR_CLS, "weight"), {n_embd, 1}, TENSOR_NOT_REQUIRED); |
3355 | 0 | cls_b = create_tensor(tn(LLM_TENSOR_CLS, "bias"), {1}, TENSOR_NOT_REQUIRED); |
3356 | 0 | for (int i = 0; i < n_layer; ++i) { |
3357 | 0 | auto & layer = layers[i]; // JinaBertLayer |
3358 | |
|
3359 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
3360 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0); |
3361 | |
|
3362 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3363 | 0 | layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3364 | |
|
3365 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
3366 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0); |
3367 | |
|
3368 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3369 | 0 | layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3370 | |
|
3371 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
3372 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0); |
3373 | |
|
3374 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); //output_dens |
3375 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); //output_dens |
3376 | |
|
3377 | 0 | layer.attn_out_norm = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "weight", i), {n_embd}, 0); //output_norm |
3378 | 0 | layer.attn_out_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_OUT_NORM, "bias", i), {n_embd}, 0); |
3379 | |
|
3380 | 0 | layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3381 | 0 | layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3382 | |
|
3383 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED); |
3384 | |
|
3385 | 0 | const auto tn_ffn_up_weight = tn(LLM_TENSOR_FFN_UP, "weight", i); |
3386 | 0 | ggml_tensor * t_ffn_up = ml.get_tensor_meta(tn_ffn_up_weight.str().c_str()); |
3387 | 0 | const int64_t n_ffn_up = t_ffn_up ? t_ffn_up->ne[1] : n_ff; |
3388 | |
|
3389 | 0 | GGML_ASSERT(n_ffn_up == n_ff || n_ffn_up == n_ff * 2); |
3390 | 0 | layer.ffn_up = create_tensor(tn_ffn_up_weight, {n_embd, n_ffn_up}, 0); |
3391 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ffn_up}, TENSOR_NOT_REQUIRED); |
3392 | |
|
3393 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
3394 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0); |
3395 | |
|
3396 | 0 | layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, 0); |
3397 | 0 | layer.layer_out_norm_b = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "bias", i), {n_embd}, 0); |
3398 | 0 | } |
3399 | 0 | } break; |
3400 | 0 | case LLM_ARCH_BLOOM: |
3401 | 0 | { |
3402 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3403 | 0 | tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0); |
3404 | 0 | tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0); |
3405 | | |
3406 | | // output |
3407 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3408 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
3409 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
3410 | | |
3411 | | // if output is NULL, init from the input tok embed |
3412 | 0 | if (output == NULL) { |
3413 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
3414 | 0 | } |
3415 | |
|
3416 | 0 | for (int i = 0; i < n_layer; ++i) { |
3417 | 0 | auto & layer = layers[i]; |
3418 | |
|
3419 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3420 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
3421 | |
|
3422 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0); |
3423 | 0 | layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0); |
3424 | |
|
3425 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3426 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); |
3427 | |
|
3428 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
3429 | 0 | layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0); |
3430 | |
|
3431 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
3432 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0); |
3433 | |
|
3434 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
3435 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0); |
3436 | 0 | } |
3437 | 0 | } break; |
3438 | 0 | case LLM_ARCH_MPT: |
3439 | 0 | { |
3440 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3441 | 0 | pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, TENSOR_NOT_REQUIRED); |
3442 | | |
3443 | | // output |
3444 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3445 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, TENSOR_NOT_REQUIRED); |
3446 | |
|
3447 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
3448 | 0 | if (!output) { |
3449 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // needs to be on GPU |
3450 | 0 | } |
3451 | |
|
3452 | 0 | for (int i = 0; i < n_layer; ++i) { |
3453 | 0 | auto & layer = layers[i]; |
3454 | |
|
3455 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3456 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3457 | |
|
3458 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0); |
3459 | 0 | layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED); |
3460 | |
|
3461 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3462 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3463 | |
|
3464 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
3465 | 0 | layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3466 | |
|
3467 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
3468 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3469 | |
|
3470 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
3471 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED); |
3472 | | |
3473 | | // FIXME test-llama-archs crashes if q_norm is created |
3474 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED | TENSOR_SKIP_IF_VIRTUAL); |
3475 | 0 | layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED | TENSOR_SKIP_IF_VIRTUAL); |
3476 | |
|
3477 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3478 | 0 | layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3479 | | |
3480 | | // AWQ ScaleActivation layer |
3481 | 0 | layer.ffn_act = create_tensor(tn(LLM_TENSOR_FFN_ACT, "scales", i), {n_ff}, TENSOR_NOT_REQUIRED); |
3482 | 0 | } |
3483 | 0 | } break; |
3484 | 0 | case LLM_ARCH_STABLELM: |
3485 | 0 | { |
3486 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3487 | | |
3488 | | // output |
3489 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
3490 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3491 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
3492 | |
|
3493 | 0 | for (int i = 0; i < n_layer; ++i) { |
3494 | 0 | auto & layer = layers[i]; |
3495 | |
|
3496 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3497 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
3498 | |
|
3499 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
3500 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
3501 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
3502 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3503 | | |
3504 | | // optional bias tensors, present in Stable LM 2 1.6B |
3505 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3506 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED); |
3507 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED); |
3508 | | |
3509 | | // optional q and k layernorms, present in StableLM 2 12B |
3510 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, TENSOR_NOT_REQUIRED); |
3511 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, TENSOR_NOT_REQUIRED); |
3512 | | |
3513 | | // optional FFN norm, not present in StableLM 2 12B which uses parallel residual |
3514 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3515 | 0 | layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3516 | |
|
3517 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
3518 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
3519 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
3520 | 0 | } |
3521 | 0 | } break; |
3522 | 0 | case LLM_ARCH_QWEN: |
3523 | 0 | { |
3524 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3525 | | |
3526 | | // output |
3527 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3528 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
3529 | |
|
3530 | 0 | for (int i = 0; i < n_layer; ++i) { |
3531 | 0 | auto & layer = layers[i]; |
3532 | |
|
3533 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3534 | |
|
3535 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd*3}, 0); |
3536 | 0 | layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd*3}, 0); |
3537 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3538 | |
|
3539 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
3540 | |
|
3541 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff/2}, 0); |
3542 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff/2, n_embd}, 0); |
3543 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff/2}, 0); |
3544 | 0 | } |
3545 | 0 | } break; |
3546 | 0 | case LLM_ARCH_QWEN2: |
3547 | 0 | case LLM_ARCH_QWEN2VL: |
3548 | 0 | case LLM_ARCH_DREAM: |
3549 | 0 | { |
3550 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3551 | | |
3552 | | // output |
3553 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3554 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
3555 | 0 | output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {n_vocab}, TENSOR_NOT_REQUIRED); |
3556 | | // if output is NULL, init from the input tok embed |
3557 | 0 | if (output == NULL) { |
3558 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
3559 | 0 | } |
3560 | |
|
3561 | 0 | for (int i = 0; i < n_layer; ++i) { |
3562 | 0 | auto & layer = layers[i]; |
3563 | |
|
3564 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3565 | |
|
3566 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
3567 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
3568 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
3569 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3570 | | |
3571 | | // optional bias tensors |
3572 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3573 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED); |
3574 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED); |
3575 | |
|
3576 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
3577 | |
|
3578 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
3579 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
3580 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
3581 | 0 | } |
3582 | 0 | } break; |
3583 | 0 | case LLM_ARCH_QWEN2MOE: |
3584 | 0 | { |
3585 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3586 | | |
3587 | | // output |
3588 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3589 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
3590 | |
|
3591 | 0 | for (int i = 0; i < n_layer; ++i) { |
3592 | 0 | auto & layer = layers[i]; |
3593 | |
|
3594 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3595 | |
|
3596 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
3597 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
3598 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
3599 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3600 | | |
3601 | | // optional bias tensors |
3602 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
3603 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED); |
3604 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED); |
3605 | |
|
3606 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
3607 | |
|
3608 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
3609 | |
|
3610 | 0 | if (n_expert == 0) { |
3611 | 0 | throw std::runtime_error("n_expert must be > 0 for QWEN2MOE"); |
3612 | 0 | } |
3613 | 0 | if (n_expert_used == 0) { |
3614 | 0 | throw std::runtime_error("n_expert_used must be > 0 for QWEN2MOE"); |
3615 | 0 | } |
3616 | | |
3617 | | // MoE branch |
3618 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used; |
3619 | |
|
3620 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
3621 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0); |
3622 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
3623 | | |
3624 | | // Shared expert branch |
3625 | 0 | const int64_t n_ff_shexp = hparams.n_ff_shexp ? hparams.n_ff_shexp : n_ff; |
3626 | |
|
3627 | 0 | layer.ffn_gate_inp_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP_SHEXP, "weight", i), {n_embd}, 0); |
3628 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0); |
3629 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, 0); |
3630 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp}, 0); |
3631 | 0 | } |
3632 | 0 | } break; |
3633 | 0 | case LLM_ARCH_QWEN3: |
3634 | 0 | case LLM_ARCH_QWEN3VL: |
3635 | 0 | { |
3636 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3637 | | |
3638 | | // output |
3639 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3640 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
3641 | | // if output is NULL, init from the input tok embed |
3642 | 0 | if (output == NULL) { |
3643 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
3644 | 0 | } |
3645 | | |
3646 | | // output rerank head |
3647 | 0 | cls_out = create_tensor(tn(LLM_TENSOR_CLS_OUT, "weight"), {n_embd, hparams.n_cls_out}, TENSOR_NOT_REQUIRED); |
3648 | |
|
3649 | 0 | for (int i = 0; i < n_layer; ++i) { |
3650 | 0 | auto & layer = layers[i]; |
3651 | |
|
3652 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3653 | |
|
3654 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
3655 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
3656 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
3657 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
3658 | |
|
3659 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
3660 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
3661 | |
|
3662 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
3663 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
3664 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
3665 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
3666 | 0 | } |
3667 | 0 | } break; |
3668 | 0 | case LLM_ARCH_QWEN3MOE: |
3669 | 0 | case LLM_ARCH_QWEN3VLMOE: |
3670 | 0 | case LLM_ARCH_RND1: |
3671 | 0 | { |
3672 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3673 | | |
3674 | | // output |
3675 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3676 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
3677 | | // if output is NULL, init from the input tok embed |
3678 | 0 | if (output == NULL) { |
3679 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
3680 | 0 | } |
3681 | |
|
3682 | 0 | for (int i = 0; i < n_layer; ++i) { |
3683 | 0 | auto & layer = layers[i]; |
3684 | |
|
3685 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3686 | |
|
3687 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
3688 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
3689 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
3690 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
3691 | |
|
3692 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
3693 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
3694 | |
|
3695 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
3696 | |
|
3697 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
3698 | |
|
3699 | 0 | if (n_expert == 0) { |
3700 | 0 | throw std::runtime_error("n_expert must be > 0 for QWEN3MOE"); |
3701 | 0 | } |
3702 | 0 | if (n_expert_used == 0) { |
3703 | 0 | throw std::runtime_error("n_expert_used must be > 0 for QWEN3MOE"); |
3704 | 0 | } |
3705 | | |
3706 | | // MoE branch |
3707 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used; |
3708 | |
|
3709 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
3710 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0); |
3711 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
3712 | 0 | } |
3713 | 0 | } break; |
3714 | 0 | case LLM_ARCH_PHI2: |
3715 | 0 | { |
3716 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3717 | | |
3718 | | // output |
3719 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3720 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
3721 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
3722 | 0 | output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {n_vocab}, 0); |
3723 | |
|
3724 | 0 | for (int i = 0; i < n_layer; ++i) { |
3725 | 0 | auto & layer = layers[i]; |
3726 | |
|
3727 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3728 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
3729 | |
|
3730 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED); |
3731 | 0 | layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED); |
3732 | |
|
3733 | 0 | if (layer.wqkv == nullptr) { |
3734 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
3735 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0); |
3736 | |
|
3737 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
3738 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0); |
3739 | |
|
3740 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
3741 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0); |
3742 | 0 | } |
3743 | |
|
3744 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3745 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); |
3746 | |
|
3747 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
3748 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0); |
3749 | |
|
3750 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
3751 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0); |
3752 | 0 | } |
3753 | 0 | } break; |
3754 | 0 | case LLM_ARCH_PHI3: |
3755 | 0 | { |
3756 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0); |
3757 | | |
3758 | | // output |
3759 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0); |
3760 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
3761 | | |
3762 | | // if output is NULL, init from the input tok embed |
3763 | 0 | if (output == NULL) { |
3764 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
3765 | 0 | } |
3766 | |
|
3767 | 0 | for (int i = 0; i < n_layer; ++i) { |
3768 | 0 | auto & layer = layers[i]; |
3769 | |
|
3770 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0); |
3771 | |
|
3772 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), { n_embd, n_embd + 2 * n_embd_gqa }, TENSOR_NOT_REQUIRED); |
3773 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0); |
3774 | |
|
3775 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0); |
3776 | |
|
3777 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0); |
3778 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, 2 * n_ff }, 0); |
3779 | |
|
3780 | 0 | layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
3781 | 0 | layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
3782 | 0 | } |
3783 | 0 | } break; |
3784 | 0 | case LLM_ARCH_PHIMOE: |
3785 | 0 | { |
3786 | 0 | const int64_t n_embd_head = n_embd / n_head; |
3787 | |
|
3788 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0); |
3789 | | |
3790 | | // output |
3791 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0); |
3792 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
3793 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, 0); |
3794 | 0 | output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), { n_vocab }, 0); |
3795 | |
|
3796 | 0 | for (int i = 0; i < n_layer; ++i) { |
3797 | 0 | auto & layer = layers[i]; |
3798 | |
|
3799 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0); |
3800 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), { n_embd }, 0); |
3801 | |
|
3802 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), { n_embd, n_embd + 2 * n_embd_gqa }, TENSOR_NOT_REQUIRED); |
3803 | 0 | if (layer.wqkv == nullptr) { |
3804 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
3805 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0); |
3806 | |
|
3807 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
3808 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0); |
3809 | |
|
3810 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
3811 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0); |
3812 | 0 | } |
3813 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0); |
3814 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), { n_embd }, 0); |
3815 | |
|
3816 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0); |
3817 | 0 | layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), { n_embd }, 0); |
3818 | |
|
3819 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
3820 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
3821 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0); |
3822 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
3823 | |
|
3824 | 0 | layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), { n_embd_head/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
3825 | 0 | layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), { n_embd_head/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
3826 | 0 | } |
3827 | 0 | } break; |
3828 | 0 | case LLM_ARCH_PLAMO: |
3829 | 0 | { |
3830 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3831 | | |
3832 | | // output |
3833 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3834 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
3835 | |
|
3836 | 0 | for (int i = 0; i < n_layer; ++i) { |
3837 | 0 | auto & layer = layers[i]; |
3838 | |
|
3839 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3840 | |
|
3841 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
3842 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
3843 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
3844 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3845 | |
|
3846 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
3847 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
3848 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
3849 | 0 | } |
3850 | 0 | } break; |
3851 | 0 | case LLM_ARCH_PLAMO2: |
3852 | 0 | { |
3853 | | // mamba parameters |
3854 | 0 | const uint32_t d_conv = hparams.ssm_d_conv; |
3855 | 0 | const uint32_t d_state = hparams.ssm_d_state; |
3856 | 0 | const uint32_t num_heads = hparams.ssm_dt_rank; |
3857 | 0 | const uint32_t intermediate_size = hparams.ssm_d_inner; |
3858 | 0 | const int64_t dt_dim = std::max(64, int(hparams.n_embd / 16)); |
3859 | | |
3860 | | // attention parameters |
3861 | 0 | const uint32_t qk_dim = hparams.n_embd_head_k(); |
3862 | 0 | const uint32_t v_dim = hparams.n_embd_head_v(); |
3863 | |
|
3864 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3865 | | |
3866 | | // output |
3867 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3868 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
3869 | | // if output is NULL, init from the input tok embed |
3870 | 0 | if (output == NULL) { |
3871 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
3872 | 0 | } |
3873 | |
|
3874 | 0 | for (int i = 0; i < n_layer; ++i) { |
3875 | 0 | auto & layer = layers[i]; |
3876 | 0 | bool is_mamba_layer = hparams.is_recurrent(i); |
3877 | |
|
3878 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3879 | |
|
3880 | 0 | if (is_mamba_layer) { |
3881 | 0 | layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2 * intermediate_size}, 0); |
3882 | 0 | layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, intermediate_size}, 0); |
3883 | |
|
3884 | 0 | layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {intermediate_size, dt_dim + 2*d_state}, 0); |
3885 | 0 | layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_dim, num_heads}, 0); |
3886 | 0 | layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {num_heads}, 0); |
3887 | |
|
3888 | 0 | layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {num_heads}, 0); |
3889 | 0 | layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {num_heads}, 0); |
3890 | |
|
3891 | 0 | layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {intermediate_size, n_embd}, 0); |
3892 | |
|
3893 | 0 | layer.ssm_dt_norm = create_tensor(tn(LLM_TENSOR_SSM_DT_NORM, i), {dt_dim}, 0); |
3894 | 0 | layer.ssm_b_norm = create_tensor(tn(LLM_TENSOR_SSM_B_NORM, i), {d_state}, 0); |
3895 | 0 | layer.ssm_c_norm = create_tensor(tn(LLM_TENSOR_SSM_C_NORM, i), {d_state}, 0); |
3896 | 0 | } else { |
3897 | 0 | const int64_t num_attention_heads = hparams.n_head(i); |
3898 | 0 | const int64_t q_num_heads = num_attention_heads; |
3899 | 0 | const int64_t num_key_value_heads = hparams.n_head_kv(i); |
3900 | 0 | const int64_t k_num_heads = num_key_value_heads; |
3901 | 0 | const int64_t v_num_heads = num_key_value_heads; |
3902 | 0 | const int64_t q_proj_dim = q_num_heads * qk_dim; |
3903 | 0 | const int64_t k_proj_dim = k_num_heads * qk_dim; |
3904 | 0 | const int64_t v_proj_dim = v_num_heads * v_dim; |
3905 | |
|
3906 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, q_proj_dim + k_proj_dim + v_proj_dim}, 0); |
3907 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {qk_dim, num_attention_heads}, 0); |
3908 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {qk_dim, k_num_heads}, 0); |
3909 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {q_num_heads * v_dim, n_embd}, 0); |
3910 | 0 | } |
3911 | | |
3912 | | // All layers have post-attention norm, FFN norm, and FFN tensors |
3913 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, i), {n_embd}, 0); |
3914 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
3915 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
3916 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, 0); |
3917 | 0 | layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, i), {n_embd}, 0); |
3918 | 0 | } |
3919 | 0 | } break; |
3920 | 0 | case LLM_ARCH_PLAMO3: |
3921 | 0 | { |
3922 | 0 | const int64_t head_dim_q = hparams.n_embd_head_k(); |
3923 | 0 | const int64_t head_dim_v = hparams.n_embd_head_v(); |
3924 | |
|
3925 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3926 | |
|
3927 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3928 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
3929 | 0 | if (output == NULL) { |
3930 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
3931 | 0 | } |
3932 | |
|
3933 | 0 | for (int i = 0; i < n_layer; ++i) { |
3934 | 0 | auto & layer = layers[i]; |
3935 | |
|
3936 | 0 | const int64_t num_attention_heads = hparams.n_head(i); |
3937 | 0 | const int64_t num_key_value_heads = hparams.n_head_kv(i); |
3938 | 0 | const int64_t q_proj_dim = num_attention_heads * head_dim_q; |
3939 | 0 | const int64_t k_proj_dim = num_key_value_heads * head_dim_q; |
3940 | 0 | const int64_t v_proj_dim = num_key_value_heads * head_dim_v; |
3941 | 0 | const int64_t n_ff_cur = hparams.n_ff(i); |
3942 | |
|
3943 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3944 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), |
3945 | 0 | {n_embd,q_proj_dim + k_proj_dim + v_proj_dim}, 0); |
3946 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {head_dim_q}, 0); |
3947 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {head_dim_q}, 0); |
3948 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {num_attention_heads * head_dim_v, n_embd}, 0); |
3949 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, i), {n_embd}, 0); |
3950 | |
|
3951 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
3952 | 0 | layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, i), {n_embd}, 0); |
3953 | |
|
3954 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff_cur * 2}, 0); |
3955 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff_cur, n_embd}, 0); |
3956 | 0 | } |
3957 | 0 | } break; |
3958 | 0 | case LLM_ARCH_GPT2: |
3959 | 0 | { |
3960 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
3961 | 0 | pos_embd = create_tensor(tn(LLM_TENSOR_POS_EMBD, "weight"), {n_embd, n_ctx_train}, 0); |
3962 | | |
3963 | | // output |
3964 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
3965 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
3966 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
3967 | | |
3968 | | // if output is NULL, init from the input tok embed |
3969 | 0 | if (output == NULL) { |
3970 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
3971 | 0 | } |
3972 | |
|
3973 | 0 | for (int i = 0; i < n_layer; ++i) { |
3974 | 0 | auto & layer = layers[i]; |
3975 | |
|
3976 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
3977 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
3978 | |
|
3979 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0); |
3980 | 0 | layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0); |
3981 | |
|
3982 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
3983 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); |
3984 | |
|
3985 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
3986 | 0 | layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0); |
3987 | |
|
3988 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
3989 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0); |
3990 | |
|
3991 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
3992 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0); |
3993 | 0 | } |
3994 | 0 | } break; |
3995 | 0 | case LLM_ARCH_CODESHELL: |
3996 | 0 | { |
3997 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
3998 | | |
3999 | | // if tok embd is NULL, init from output |
4000 | 0 | if (tok_embd == NULL) { |
4001 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
4002 | 0 | } |
4003 | | |
4004 | | // output |
4005 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4006 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
4007 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
4008 | |
|
4009 | 0 | for (int i = 0; i < n_layer; ++i) { |
4010 | 0 | auto & layer = layers[i]; |
4011 | |
|
4012 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4013 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
4014 | |
|
4015 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0); |
4016 | 0 | layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0); |
4017 | |
|
4018 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
4019 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); |
4020 | |
|
4021 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4022 | 0 | layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0); |
4023 | |
|
4024 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
4025 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0); |
4026 | |
|
4027 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4028 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0); |
4029 | 0 | } |
4030 | 0 | } break; |
4031 | 0 | case LLM_ARCH_ORION: |
4032 | 0 | { |
4033 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4034 | |
|
4035 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4036 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
4037 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
4038 | |
|
4039 | 0 | for (int i = 0; i < n_layer; ++i) { |
4040 | 0 | auto & layer = layers[i]; |
4041 | |
|
4042 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4043 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
4044 | |
|
4045 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
4046 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
4047 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
4048 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
4049 | |
|
4050 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4051 | 0 | layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0); |
4052 | |
|
4053 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
4054 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
4055 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4056 | 0 | } |
4057 | 0 | } break; |
4058 | 0 | case LLM_ARCH_INTERNLM2: |
4059 | 0 | { |
4060 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4061 | | |
4062 | | // output |
4063 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4064 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
4065 | |
|
4066 | 0 | for (int i = 0; i < n_layer; ++i) { |
4067 | 0 | auto & layer = layers[i]; |
4068 | |
|
4069 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4070 | | // layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0); |
4071 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
4072 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
4073 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
4074 | |
|
4075 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
4076 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4077 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
4078 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
4079 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4080 | 0 | } |
4081 | 0 | } break; |
4082 | 0 | case LLM_ARCH_GEMMA: |
4083 | 0 | { |
4084 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4085 | | |
4086 | | // output |
4087 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4088 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // same as tok_embd, duplicated to allow offloading |
4089 | |
|
4090 | 0 | for (int i = 0; i < n_layer; ++i) { |
4091 | 0 | auto & layer = layers[i]; |
4092 | |
|
4093 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4094 | |
|
4095 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
4096 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
4097 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
4098 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
4099 | |
|
4100 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4101 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
4102 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4103 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
4104 | 0 | } |
4105 | 0 | } break; |
4106 | 0 | case LLM_ARCH_GEMMA2: |
4107 | 0 | { |
4108 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4109 | | |
4110 | | // output |
4111 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4112 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); // same as tok_embd, duplicated to allow offloading |
4113 | |
|
4114 | 0 | for (int i = 0; i < n_layer; ++i) { |
4115 | 0 | auto & layer = layers[i]; |
4116 | |
|
4117 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4118 | |
|
4119 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
4120 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
4121 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
4122 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
4123 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0); |
4124 | |
|
4125 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4126 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
4127 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4128 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
4129 | 0 | layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0); |
4130 | 0 | } |
4131 | 0 | } break; |
4132 | 0 | case LLM_ARCH_GEMMA3: |
4133 | 0 | case LLM_ARCH_GEMMA_EMBEDDING: |
4134 | 0 | { |
4135 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4136 | | |
4137 | | // output |
4138 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4139 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
4140 | | |
4141 | | // if output is NULL, init from the input tok embed |
4142 | 0 | if (output == NULL) { |
4143 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
4144 | 0 | } |
4145 | | |
4146 | | // Dense linear weights |
4147 | 0 | dense_2_out_layers = create_tensor(tn(LLM_TENSOR_DENSE_2_OUT, "weight"), {n_embd, hparams.dense_2_feat_out}, TENSOR_NOT_REQUIRED); |
4148 | 0 | dense_3_out_layers = create_tensor(tn(LLM_TENSOR_DENSE_3_OUT, "weight"), {hparams.dense_3_feat_in, n_embd}, TENSOR_NOT_REQUIRED); |
4149 | | |
4150 | |
|
4151 | 0 | for (int i = 0; i < n_layer; ++i) { |
4152 | 0 | auto & layer = layers[i]; |
4153 | |
|
4154 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4155 | |
|
4156 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
4157 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
4158 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
4159 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
4160 | |
|
4161 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0); |
4162 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
4163 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
4164 | |
|
4165 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4166 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
4167 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4168 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
4169 | 0 | layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0); |
4170 | 0 | } |
4171 | 0 | } break; |
4172 | 0 | case LLM_ARCH_GEMMA3N: |
4173 | 0 | { |
4174 | 0 | const int64_t n_altup = hparams.n_altup; |
4175 | 0 | const int64_t laurel_rank = hparams.laurel_rank; |
4176 | 0 | const int64_t n_embd_altup = hparams.n_embd_altup; |
4177 | |
|
4178 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
4179 | | // if output is NULL, init from the input tok embed |
4180 | 0 | if (output == NULL) { |
4181 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
4182 | 0 | } |
4183 | |
|
4184 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4185 | 0 | tok_embd_per_layer = create_tensor(tn(LLM_TENSOR_PER_LAYER_TOKEN_EMBD, "weight"), {n_embd_altup * n_layer, n_vocab}, 0); |
4186 | |
|
4187 | 0 | altup_proj = create_tensor(tn(LLM_TENSOR_ALTUP_PROJ, "weight"), {n_embd, n_embd, n_altup - 1}, 0); |
4188 | 0 | altup_unembd_proj = create_tensor(tn(LLM_TENSOR_ALTUP_UNEMBD_PROJ, "weight"), {n_embd, n_embd, n_altup - 1}, 0); |
4189 | 0 | per_layer_model_proj = create_tensor(tn(LLM_TENSOR_PER_LAYER_MODEL_PROJ, "weight"), {n_embd, n_embd_altup * n_layer}, 0); |
4190 | 0 | per_layer_proj_norm = create_tensor(tn(LLM_TENSOR_PER_LAYER_PROJ_NORM, "weight"), {n_embd_altup}, 0); |
4191 | |
|
4192 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4193 | |
|
4194 | 0 | for (int i = 0; i < n_layer; ++i) { |
4195 | 0 | auto & layer = layers[i]; |
4196 | |
|
4197 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4198 | |
|
4199 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
4200 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
4201 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
4202 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
4203 | |
|
4204 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
4205 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
4206 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0); |
4207 | |
|
4208 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4209 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
4210 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4211 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
4212 | 0 | layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0); |
4213 | | |
4214 | | // altup & laurel |
4215 | 0 | layer.per_layer_inp_gate = create_tensor(tn(LLM_TENSOR_PER_LAYER_INP_GATE, "weight", i), {n_embd, n_embd_altup}, 0); |
4216 | 0 | layer.per_layer_proj = create_tensor(tn(LLM_TENSOR_PER_LAYER_PROJ, "weight", i), {n_embd_altup, n_embd}, 0); |
4217 | 0 | layer.per_layer_post_norm = create_tensor(tn(LLM_TENSOR_PER_LAYER_POST_NORM, "weight", i), {n_embd}, 0); |
4218 | 0 | layer.altup_correct_coef = create_tensor(tn(LLM_TENSOR_ALTUP_CORRECT_COEF, "weight", i), {n_altup, n_altup}, 0); |
4219 | 0 | layer.altup_correct_scale = create_tensor(tn(LLM_TENSOR_ALTUP_CORRECT_SCALE, "weight", i), {n_embd}, 0); |
4220 | 0 | layer.altup_predict_coef = create_tensor(tn(LLM_TENSOR_ALTUP_PREDICT_COEF, "weight", i), {n_altup, n_altup * n_altup}, 0); |
4221 | 0 | layer.altup_router = create_tensor(tn(LLM_TENSOR_ALTUP_ROUTER, "weight", i), {n_embd, n_altup}, 0); |
4222 | 0 | layer.altup_router_norm = create_tensor(tn(LLM_TENSOR_ALTUP_ROUTER_NORM, "weight", i), {n_embd}, 0); |
4223 | 0 | layer.laurel_l = create_tensor(tn(LLM_TENSOR_LAUREL_L, "weight", i), {n_embd, laurel_rank}, 0); |
4224 | 0 | layer.laurel_r = create_tensor(tn(LLM_TENSOR_LAUREL_R, "weight", i), {laurel_rank, n_embd}, 0); |
4225 | 0 | layer.laurel_post_norm = create_tensor(tn(LLM_TENSOR_LAUREL_POST_NORM, "weight", i), {n_embd}, 0); |
4226 | 0 | } |
4227 | 0 | } break; |
4228 | 0 | case LLM_ARCH_STARCODER2: |
4229 | 0 | { |
4230 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4231 | | |
4232 | | // output |
4233 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4234 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
4235 | |
|
4236 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
4237 | | // if output is NULL, init from the input tok embed |
4238 | 0 | if (output == NULL) { |
4239 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
4240 | 0 | } |
4241 | |
|
4242 | 0 | for (int i = 0; i < n_layer; ++i) { |
4243 | 0 | auto & layer = layers[i]; |
4244 | |
|
4245 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4246 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
4247 | |
|
4248 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
4249 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
4250 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
4251 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
4252 | | |
4253 | | // optional bias tensors |
4254 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0); |
4255 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0); |
4256 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0); |
4257 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); |
4258 | |
|
4259 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4260 | 0 | layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0); |
4261 | |
|
4262 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
4263 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4264 | | |
4265 | | // optional bias tensors |
4266 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0); |
4267 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP , "bias", i), { n_ff}, 0); |
4268 | 0 | } |
4269 | 0 | } break; |
4270 | 0 | case LLM_ARCH_MAMBA: |
4271 | 0 | { |
4272 | 0 | const int64_t d_conv = hparams.ssm_d_conv; |
4273 | 0 | const int64_t d_inner = hparams.ssm_d_inner; |
4274 | 0 | const int64_t d_state = hparams.ssm_d_state; |
4275 | 0 | const int64_t dt_rank = hparams.ssm_dt_rank; |
4276 | | |
4277 | | // only an expansion factor of 2 is supported for now |
4278 | 0 | if (2 * n_embd != d_inner) { |
4279 | 0 | throw std::runtime_error("only an expansion factor of 2 is supported for now"); |
4280 | 0 | } |
4281 | | |
4282 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4283 | | |
4284 | | // output |
4285 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4286 | |
|
4287 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
4288 | | // if output is NULL, init from the input tok embed, duplicated to allow offloading |
4289 | 0 | if (output == NULL) { |
4290 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
4291 | 0 | } |
4292 | |
|
4293 | 0 | for (int i = 0; i < n_layer; ++i) { |
4294 | 0 | auto & layer = layers[i]; |
4295 | | |
4296 | | // norm |
4297 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4298 | |
|
4299 | 0 | layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2*d_inner}, 0); |
4300 | |
|
4301 | 0 | layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner}, 0); |
4302 | 0 | layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner}, 0); |
4303 | |
|
4304 | 0 | layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {d_inner, dt_rank + 2*d_state}, 0); |
4305 | |
|
4306 | 0 | layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_rank, d_inner}, 0); |
4307 | 0 | layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {d_inner}, 0); |
4308 | | |
4309 | | // no "weight" suffix for these |
4310 | 0 | layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {d_state, d_inner}, 0); |
4311 | 0 | layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {d_inner}, 0); |
4312 | | |
4313 | | // out_proj |
4314 | 0 | layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0); |
4315 | 0 | } |
4316 | 0 | } break; |
4317 | 0 | case LLM_ARCH_MAMBA2: |
4318 | 0 | { |
4319 | 0 | const int64_t d_conv = hparams.ssm_d_conv; |
4320 | 0 | const int64_t d_inner = hparams.ssm_d_inner; |
4321 | 0 | const int64_t d_state = hparams.ssm_d_state; |
4322 | 0 | const int64_t n_head = hparams.ssm_dt_rank; |
4323 | 0 | const int64_t n_group = hparams.ssm_n_group; |
4324 | 0 | const int64_t d_in_proj = 2*d_inner + 2*n_group*d_state + n_head; |
4325 | | |
4326 | | // only an expansion factor of 2 is supported for now |
4327 | 0 | GGML_ASSERT(2 * n_embd == d_inner); |
4328 | |
|
4329 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4330 | | |
4331 | | // output |
4332 | 0 | { |
4333 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4334 | |
|
4335 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
4336 | | // if output is NULL, init from the input tok embed, duplicated to allow offloading |
4337 | 0 | if (output == NULL) { |
4338 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
4339 | 0 | } |
4340 | 0 | } |
4341 | |
|
4342 | 0 | for (int i = 0; i < n_layer; ++i) { |
4343 | 0 | auto & layer = layers[i]; |
4344 | | |
4345 | | // norm |
4346 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4347 | |
|
4348 | 0 | layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, 0); |
4349 | |
|
4350 | 0 | layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, 0); |
4351 | 0 | layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, 0); |
4352 | |
|
4353 | 0 | layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_head}, 0); |
4354 | | |
4355 | | // no "weight" suffix for these |
4356 | 0 | layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_head}, 0); |
4357 | 0 | layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_head}, 0); |
4358 | |
|
4359 | 0 | layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, 0); |
4360 | | |
4361 | | // out_proj |
4362 | 0 | layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0); |
4363 | 0 | } |
4364 | 0 | } break; |
4365 | 0 | case LLM_ARCH_JAMBA: |
4366 | 0 | { |
4367 | 0 | const int64_t d_conv = hparams.ssm_d_conv; |
4368 | 0 | const int64_t d_inner = hparams.ssm_d_inner; |
4369 | 0 | const int64_t d_state = hparams.ssm_d_state; |
4370 | 0 | const int64_t dt_rank = hparams.ssm_dt_rank; |
4371 | | |
4372 | | // only an expansion factor of 2 is supported for now |
4373 | 0 | GGML_ASSERT(2 * n_embd == d_inner); |
4374 | |
|
4375 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4376 | | |
4377 | | // output |
4378 | 0 | { |
4379 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4380 | |
|
4381 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
4382 | | // if output is NULL, init from the input tok embed, duplicated to allow offloading |
4383 | 0 | if (output == NULL) { |
4384 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
4385 | 0 | } |
4386 | 0 | } |
4387 | |
|
4388 | 0 | for (int i = 0; i < n_layer; ++i) { |
4389 | 0 | const int64_t n_head_kv = hparams.n_head_kv(i); |
4390 | 0 | const int64_t n_embd_gqa = hparams.n_embd_v_gqa(i); |
4391 | |
|
4392 | 0 | auto & layer = layers[i]; |
4393 | | |
4394 | | // norm |
4395 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4396 | |
|
4397 | 0 | if (n_head_kv == 0) { |
4398 | | // Mamba layer |
4399 | 0 | layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, 2*d_inner}, 0); |
4400 | |
|
4401 | 0 | layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner}, 0); |
4402 | 0 | layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner}, 0); |
4403 | |
|
4404 | 0 | layer.ssm_x = create_tensor(tn(LLM_TENSOR_SSM_X, "weight", i), {d_inner, dt_rank + 2*d_state}, 0); |
4405 | |
|
4406 | 0 | layer.ssm_dt_norm = create_tensor(tn(LLM_TENSOR_SSM_DT_NORM, "weight", i), {dt_rank}, 0); |
4407 | |
|
4408 | 0 | layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "weight", i), {dt_rank, d_inner}, 0); |
4409 | 0 | layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {d_inner}, 0); |
4410 | |
|
4411 | 0 | layer.ssm_b_norm = create_tensor(tn(LLM_TENSOR_SSM_B_NORM, "weight", i), {d_state}, 0); |
4412 | 0 | layer.ssm_c_norm = create_tensor(tn(LLM_TENSOR_SSM_C_NORM, "weight", i), {d_state}, 0); |
4413 | | |
4414 | | // no "weight" suffix for these |
4415 | 0 | layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {d_state, d_inner}, 0); |
4416 | 0 | layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {d_inner}, 0); |
4417 | | |
4418 | | // out_proj |
4419 | 0 | layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0); |
4420 | 0 | } else { |
4421 | | // Attention layers |
4422 | |
|
4423 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
4424 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
4425 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
4426 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
4427 | 0 | } |
4428 | |
|
4429 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4430 | |
|
4431 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, TENSOR_NOT_REQUIRED); |
4432 | |
|
4433 | 0 | if (layer.ffn_gate_inp) { |
4434 | | // MoE |
4435 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
4436 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0); |
4437 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
4438 | 0 | } else { |
4439 | | // FFN (no MoE) |
4440 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
4441 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
4442 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4443 | 0 | } |
4444 | 0 | } |
4445 | 0 | } break; |
4446 | 0 | case LLM_ARCH_GRANITE_HYBRID: |
4447 | 0 | { |
4448 | | // mamba2 Mixer SSM params |
4449 | | // NOTE: int64_t for tensor dimensions |
4450 | 0 | const int64_t d_conv = hparams.ssm_d_conv; |
4451 | 0 | const int64_t d_inner = hparams.ssm_d_inner; |
4452 | 0 | const int64_t d_state = hparams.ssm_d_state; |
4453 | 0 | const int64_t n_ssm_head = hparams.ssm_dt_rank; |
4454 | 0 | const int64_t n_group = hparams.ssm_n_group; |
4455 | 0 | const int64_t d_in_proj = 2*d_inner + 2*n_group*d_state + n_ssm_head; |
4456 | | |
4457 | | // only an expansion factor of 2 is supported for now |
4458 | 0 | GGML_ASSERT(2 * n_embd == d_inner); |
4459 | | |
4460 | | // embeddings |
4461 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4462 | | |
4463 | | // output |
4464 | 0 | { |
4465 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4466 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
4467 | | // if output is NULL, init from the input tok embed, duplicated to allow offloading |
4468 | 0 | if (output == NULL) { |
4469 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
4470 | 0 | } |
4471 | 0 | } |
4472 | |
|
4473 | 0 | for (int i = 0; i < n_layer; ++i) { |
4474 | 0 | auto & layer = layers[i]; |
4475 | | |
4476 | | // norm |
4477 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4478 | |
|
4479 | 0 | if (hparams.is_recurrent(i)) { |
4480 | | // ssm layers |
4481 | 0 | layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, 0); |
4482 | |
|
4483 | 0 | layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, 0); |
4484 | 0 | layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, TENSOR_NOT_REQUIRED); |
4485 | |
|
4486 | 0 | layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_ssm_head}, 0); |
4487 | | |
4488 | | // no "weight" suffix for these |
4489 | 0 | layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_ssm_head}, 0); |
4490 | 0 | layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_ssm_head}, 0); |
4491 | |
|
4492 | 0 | layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, 0); |
4493 | | |
4494 | | // out_proj |
4495 | 0 | layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0); |
4496 | 0 | } else { |
4497 | | // attention layers (with optional bias) |
4498 | 0 | const int64_t n_head_i = hparams.n_head(i); |
4499 | 0 | const int64_t n_embd_k_gqa_i = hparams.n_embd_k_gqa(i); |
4500 | 0 | const int64_t n_embd_v_gqa_i = hparams.n_embd_v_gqa(i); |
4501 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head_i}, 0); |
4502 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa_i}, 0); |
4503 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa_i}, 0); |
4504 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head_i, n_embd}, 0); |
4505 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
4506 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_k_gqa_i}, TENSOR_NOT_REQUIRED); |
4507 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_v_gqa_i}, TENSOR_NOT_REQUIRED); |
4508 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
4509 | 0 | } |
4510 | | |
4511 | | // feed forward (w/ optional biases) |
4512 | 0 | if (n_expert > 0) { |
4513 | | // MoE FFN |
4514 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4515 | 0 | layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
4516 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
4517 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, TENSOR_NOT_REQUIRED); |
4518 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0); |
4519 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
4520 | | |
4521 | | // For Granite MoE Shared |
4522 | 0 | if (hparams.n_ff_shexp > 0) { |
4523 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0); |
4524 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, 0); |
4525 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, 0); |
4526 | 0 | } |
4527 | 0 | } else { |
4528 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4529 | 0 | layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
4530 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
4531 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
4532 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4533 | 0 | layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED); |
4534 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
4535 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED); |
4536 | 0 | } |
4537 | 0 | } |
4538 | 0 | } break; |
4539 | 0 | case LLM_ARCH_XVERSE: |
4540 | 0 | { |
4541 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4542 | |
|
4543 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4544 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
4545 | |
|
4546 | 0 | for (int i = 0; i < n_layer; ++i) { |
4547 | 0 | auto & layer = layers[i]; |
4548 | |
|
4549 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4550 | |
|
4551 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
4552 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
4553 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
4554 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
4555 | |
|
4556 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4557 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
4558 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
4559 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4560 | 0 | } |
4561 | 0 | } break; |
4562 | 0 | case LLM_ARCH_COMMAND_R: |
4563 | 0 | { |
4564 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4565 | | |
4566 | | // output |
4567 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4568 | | // init output from the input tok embed |
4569 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
4570 | |
|
4571 | 0 | for (int i = 0; i < n_layer; ++i) { |
4572 | 0 | auto & layer = layers[i]; |
4573 | |
|
4574 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4575 | |
|
4576 | 0 | if (n_layer >= 64){ |
4577 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, 0); |
4578 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, 0); |
4579 | 0 | } |
4580 | |
|
4581 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
4582 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
4583 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
4584 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
4585 | |
|
4586 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
4587 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
4588 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4589 | 0 | } |
4590 | 0 | } break; |
4591 | 0 | case LLM_ARCH_COHERE2: |
4592 | 0 | { |
4593 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0); |
4594 | | |
4595 | | // output |
4596 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0); |
4597 | | // init output from the input tok embed |
4598 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, |
4599 | 0 | TENSOR_DUPLICATED); |
4600 | |
|
4601 | 0 | for (int i = 0; i < n_layer; ++i) { |
4602 | 0 | auto & layer = layers[i]; |
4603 | |
|
4604 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0); |
4605 | |
|
4606 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd }, 0); |
4607 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_gqa }, 0); |
4608 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_gqa }, 0); |
4609 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd, n_embd }, 0); |
4610 | |
|
4611 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, 0); |
4612 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0); |
4613 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0); |
4614 | 0 | } |
4615 | 0 | } |
4616 | 0 | break; |
4617 | 0 | case LLM_ARCH_OLMO: // adapted from LLM_ARCH_LLAMA with norm params removed |
4618 | 0 | { |
4619 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4620 | | |
4621 | | // output |
4622 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
4623 | | // if output is NULL, init from the input tok embed |
4624 | 0 | if (output == NULL) { |
4625 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
4626 | 0 | } |
4627 | |
|
4628 | 0 | for (int i = 0; i < n_layer; ++i) { |
4629 | 0 | auto & layer = layers[i]; |
4630 | |
|
4631 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
4632 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
4633 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
4634 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
4635 | |
|
4636 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
4637 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
4638 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4639 | 0 | } |
4640 | 0 | } break; |
4641 | 0 | case LLM_ARCH_OLMO2: |
4642 | 0 | { |
4643 | 0 | const int64_t n_embd_head = n_embd / n_head; |
4644 | |
|
4645 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4646 | | |
4647 | | // output |
4648 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4649 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
4650 | |
|
4651 | 0 | for (int i = 0; i < n_layer; ++i) { |
4652 | 0 | auto & layer = layers[i]; |
4653 | |
|
4654 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
4655 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
4656 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
4657 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
4658 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, 0); |
4659 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_head_kv * n_embd_head}, 0); |
4660 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0); |
4661 | |
|
4662 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
4663 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4664 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
4665 | 0 | layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0); |
4666 | 0 | } |
4667 | 0 | } break; |
4668 | 0 | case LLM_ARCH_SEED_OSS: |
4669 | 0 | { |
4670 | 0 | const uint32_t head_dim = hparams.n_embd_head_k(); |
4671 | 0 | const int64_t n_qo_dim = n_head * head_dim; |
4672 | 0 | const int64_t n_kv_dim = n_head_kv * head_dim; |
4673 | |
|
4674 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4675 | | |
4676 | | // output |
4677 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4678 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
4679 | | // if output is NULL, init from the input tok embed |
4680 | 0 | if (output == NULL) { |
4681 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
4682 | 0 | } |
4683 | |
|
4684 | 0 | for (int i = 0; i < n_layer; ++i) { |
4685 | 0 | auto & layer = layers[i]; |
4686 | |
|
4687 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_qo_dim}, 0); |
4688 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_kv_dim}, 0); |
4689 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_kv_dim}, 0); |
4690 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_qo_dim, n_embd}, 0); |
4691 | |
|
4692 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_qo_dim}, TENSOR_NOT_REQUIRED); |
4693 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_kv_dim}, TENSOR_NOT_REQUIRED); |
4694 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_kv_dim}, TENSOR_NOT_REQUIRED); |
4695 | |
|
4696 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4697 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0); |
4698 | |
|
4699 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
4700 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4701 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
4702 | 0 | } |
4703 | 0 | } break; |
4704 | | |
4705 | 0 | case LLM_ARCH_OLMOE: |
4706 | 0 | { |
4707 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4708 | | |
4709 | | // output |
4710 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4711 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
4712 | |
|
4713 | 0 | for (int i = 0; i < n_layer; ++i) { |
4714 | 0 | auto & layer = layers[i]; |
4715 | |
|
4716 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4717 | |
|
4718 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
4719 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
4720 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
4721 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
4722 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd}, 0); |
4723 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd}, 0); |
4724 | |
|
4725 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4726 | |
|
4727 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
4728 | |
|
4729 | 0 | if (n_expert == 0) { |
4730 | 0 | throw std::runtime_error("n_expert must be > 0"); |
4731 | 0 | } |
4732 | 0 | if (n_expert_used == 0) { |
4733 | 0 | throw std::runtime_error("n_expert_used must be > 0"); |
4734 | 0 | } |
4735 | | |
4736 | | // MoE branch |
4737 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
4738 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0); |
4739 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
4740 | 0 | } |
4741 | 0 | } break; |
4742 | 0 | case LLM_ARCH_OPENELM: |
4743 | 0 | { |
4744 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4745 | | |
4746 | | // output |
4747 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4748 | | // init output from the input tok embed |
4749 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
4750 | |
|
4751 | 0 | for (int i = 0; i < n_layer; ++i) { |
4752 | 0 | const int64_t n_head = hparams.n_head(i); |
4753 | 0 | const int64_t n_head_qkv = 2*hparams.n_head_kv(i) + n_head; |
4754 | 0 | const int64_t n_ff = hparams.n_ff(i); |
4755 | |
|
4756 | 0 | auto & layer = layers[i]; |
4757 | |
|
4758 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4759 | |
|
4760 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_head_qkv*n_embd_head_k}, 0); |
4761 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
4762 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
4763 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head*n_embd_head_k, n_embd}, 0); |
4764 | |
|
4765 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4766 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
4767 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
4768 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4769 | 0 | } |
4770 | 0 | } break; |
4771 | 0 | case LLM_ARCH_GPTNEOX: |
4772 | 0 | { |
4773 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4774 | | |
4775 | | // output |
4776 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4777 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
4778 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
4779 | |
|
4780 | 0 | for (int i = 0; i < n_layer; ++i) { |
4781 | 0 | auto & layer = layers[i]; |
4782 | |
|
4783 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4784 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
4785 | |
|
4786 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0); |
4787 | 0 | layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0); |
4788 | |
|
4789 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
4790 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); |
4791 | |
|
4792 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4793 | 0 | layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0); |
4794 | |
|
4795 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
4796 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0); |
4797 | |
|
4798 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4799 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0); |
4800 | 0 | } |
4801 | 0 | } break; |
4802 | 0 | case LLM_ARCH_ARCTIC: |
4803 | 0 | { |
4804 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4805 | | |
4806 | | // output |
4807 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4808 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
4809 | | |
4810 | | // if output is NULL, init from the input tok embed |
4811 | 0 | if (output == NULL) { |
4812 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
4813 | 0 | } |
4814 | |
|
4815 | 0 | for (int i = 0; i < n_layer; ++i) { |
4816 | 0 | auto & layer = layers[i]; |
4817 | |
|
4818 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4819 | |
|
4820 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
4821 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
4822 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
4823 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
4824 | |
|
4825 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4826 | |
|
4827 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_embd}, 0); |
4828 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_embd, n_embd}, 0); |
4829 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_embd}, 0); |
4830 | |
|
4831 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
4832 | 0 | layer.ffn_norm_exps = create_tensor(tn(LLM_TENSOR_FFN_NORM_EXPS, "weight", i), {n_embd}, 0); |
4833 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, false); |
4834 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0); |
4835 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
4836 | 0 | } |
4837 | 0 | } break; |
4838 | 0 | case LLM_ARCH_DEEPSEEK: |
4839 | 0 | { |
4840 | |
|
4841 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
4842 | 0 | const int64_t n_expert_shared = hparams.n_expert_shared; |
4843 | |
|
4844 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4845 | | |
4846 | | // output |
4847 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4848 | | // try to load output.weight, if not found, use token_embd (tied embeddings) |
4849 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
4850 | 0 | if (!output) { |
4851 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
4852 | 0 | } |
4853 | |
|
4854 | 0 | for (int i = 0; i < n_layer; ++i) { |
4855 | 0 | auto & layer = layers[i]; |
4856 | |
|
4857 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4858 | |
|
4859 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
4860 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
4861 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
4862 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
4863 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4864 | |
|
4865 | 0 | if (i < (int) hparams.n_layer_dense_lead) { |
4866 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
4867 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
4868 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4869 | 0 | } else { |
4870 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
4871 | |
|
4872 | 0 | if (n_expert == 0) { |
4873 | 0 | throw std::runtime_error("n_expert must be > 0"); |
4874 | 0 | } |
4875 | 0 | if (n_expert_used == 0) { |
4876 | 0 | throw std::runtime_error("n_expert_used must be > 0"); |
4877 | 0 | } |
4878 | | |
4879 | | // MoE branch |
4880 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
4881 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0); |
4882 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
4883 | | |
4884 | | // Shared expert branch |
4885 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0); |
4886 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0); |
4887 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0); |
4888 | 0 | } |
4889 | 0 | } |
4890 | 0 | } break; |
4891 | 0 | case LLM_ARCH_DEEPSEEK2: |
4892 | 0 | case LLM_ARCH_MISTRAL4: |
4893 | 0 | { |
4894 | 0 | const bool is_mla = hparams.is_mla(); |
4895 | | |
4896 | | // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA |
4897 | 0 | const int64_t n_embd_head_k_mla = hparams.n_embd_head_k_mla(); |
4898 | 0 | const int64_t n_embd_head_v_mla = hparams.n_embd_head_v_mla(); |
4899 | |
|
4900 | 0 | const int64_t n_embd_head_qk_rope = hparams.n_rot(); |
4901 | 0 | const int64_t n_embd_head_qk_nope = n_embd_head_k_mla - n_embd_head_qk_rope; |
4902 | 0 | GGML_ASSERT(n_embd_head_qk_nope >= 1); |
4903 | |
|
4904 | 0 | const int64_t q_lora_rank = hparams.n_lora_q; |
4905 | 0 | const int64_t kv_lora_rank = hparams.n_lora_kv; |
4906 | |
|
4907 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
4908 | 0 | const int64_t n_expert_shared = hparams.n_expert_shared; |
4909 | |
|
4910 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4911 | | |
4912 | | // output |
4913 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4914 | | // try to load output.weight, if not found, use token_embd (tied embeddings) |
4915 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
4916 | 0 | if (!output) { |
4917 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
4918 | 0 | } |
4919 | |
|
4920 | 0 | for (int i = 0; i < n_layer; ++i) { |
4921 | 0 | auto & layer = layers[i]; |
4922 | |
|
4923 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4924 | 0 | if (q_lora_rank > 0) { |
4925 | 0 | layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, 0); |
4926 | 0 | } |
4927 | |
|
4928 | 0 | layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0); |
4929 | |
|
4930 | 0 | if (q_lora_rank > 0) { |
4931 | 0 | layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, 0); |
4932 | 0 | layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k_mla}, 0); |
4933 | 0 | } else { |
4934 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_embd_head_k_mla}, 0); |
4935 | 0 | } |
4936 | |
|
4937 | 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); |
4938 | | |
4939 | | // note: only old legacy GGUF files will have the unsplit wkv_b tensor in |
4940 | 0 | if (is_mla) { |
4941 | 0 | layer.wk_b = create_tensor(tn(LLM_TENSOR_ATTN_K_B, "weight", i), {n_embd_head_qk_nope, kv_lora_rank, n_head}, 0); |
4942 | 0 | layer.wv_b = create_tensor(tn(LLM_TENSOR_ATTN_V_B, "weight", i), {kv_lora_rank, n_embd_head_v_mla, n_head}, 0); |
4943 | 0 | } else { |
4944 | 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_mla)}, 0); |
4945 | 0 | } |
4946 | |
|
4947 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_embd_head_v_mla, n_embd}, 0); |
4948 | |
|
4949 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
4950 | |
|
4951 | 0 | if (i < (int) hparams.n_layer_dense_lead) { |
4952 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
4953 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
4954 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
4955 | 0 | } else { |
4956 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
4957 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED); |
4958 | |
|
4959 | 0 | if (n_expert == 0) { |
4960 | 0 | throw std::runtime_error("n_expert must be > 0"); |
4961 | 0 | } |
4962 | 0 | if (n_expert_used == 0) { |
4963 | 0 | throw std::runtime_error("n_expert_used must be > 0"); |
4964 | 0 | } |
4965 | | |
4966 | | // MoE branch |
4967 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0); |
4968 | 0 | create_tensor_gate_up_exps(layer, i, n_embd, n_ff_exp, n_expert, 0); |
4969 | | |
4970 | | // Shared expert branch |
4971 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0); |
4972 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0); |
4973 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0); |
4974 | 0 | } |
4975 | 0 | } |
4976 | 0 | } break; |
4977 | 0 | case LLM_ARCH_PLM: |
4978 | 0 | { |
4979 | 0 | const int64_t n_embd_head_qk_rope = hparams.n_rot(); |
4980 | 0 | const int64_t n_embd_head_qk_nope = hparams.n_embd_head_k() - hparams.n_rot(); |
4981 | 0 | const int64_t kv_lora_rank = hparams.n_lora_kv; |
4982 | |
|
4983 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
4984 | | |
4985 | | // output |
4986 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
4987 | | // output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
4988 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
4989 | |
|
4990 | 0 | for (int i = 0; i < n_layer; ++i) { |
4991 | 0 | auto & layer = layers[i]; |
4992 | |
|
4993 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
4994 | |
|
4995 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
4996 | 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); |
4997 | 0 | layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0); |
4998 | 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); |
4999 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_head * ( n_embd_head_v), n_embd}, 0); |
5000 | |
|
5001 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
5002 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
5003 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
5004 | 0 | } |
5005 | 0 | } break; |
5006 | 0 | case LLM_ARCH_BITNET: |
5007 | 0 | { |
5008 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
5009 | | |
5010 | | // output |
5011 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5012 | |
|
5013 | 0 | for (int i = 0; i < n_layer; ++i) { |
5014 | 0 | auto & layer = layers[i]; |
5015 | |
|
5016 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
5017 | 0 | layer.attn_sub_norm = create_tensor(tn(LLM_TENSOR_ATTN_SUB_NORM, "weight", i), {n_embd}, 0); |
5018 | |
|
5019 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
5020 | 0 | layer.wq_s = create_tensor(tn(LLM_TENSOR_ATTN_Q, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
5021 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
5022 | 0 | layer.wk_s = create_tensor(tn(LLM_TENSOR_ATTN_K, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
5023 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
5024 | 0 | layer.wv_s = create_tensor(tn(LLM_TENSOR_ATTN_V, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
5025 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
5026 | 0 | layer.wo_s = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
5027 | |
|
5028 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
5029 | 0 | layer.ffn_sub_norm = create_tensor(tn(LLM_TENSOR_FFN_SUB_NORM, "weight", i), {n_ff}, 0); |
5030 | |
|
5031 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
5032 | 0 | layer.ffn_gate_s = create_tensor(tn(LLM_TENSOR_FFN_GATE, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
5033 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
5034 | 0 | layer.ffn_down_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
5035 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
5036 | 0 | layer.ffn_up_s = create_tensor(tn(LLM_TENSOR_FFN_UP, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
5037 | 0 | } |
5038 | 0 | } break; |
5039 | 0 | case LLM_ARCH_T5: |
5040 | 0 | { |
5041 | 0 | const auto n_rel_attn_bkts = hparams.n_rel_attn_bkts; |
5042 | |
|
5043 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
5044 | | |
5045 | | // output |
5046 | 0 | output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5047 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_DEC_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5048 | |
|
5049 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
5050 | | // if output is NULL, init from the input tok embed |
5051 | 0 | if (output == NULL) { |
5052 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
5053 | 0 | } |
5054 | | |
5055 | | // n_layer: number of encoder_layers |
5056 | | // dec_n_layer: number of decoder_layers |
5057 | 0 | const int dec_n_layer = hparams.dec_n_layer; |
5058 | 0 | if (dec_n_layer > n_layer) { |
5059 | 0 | layers.resize(dec_n_layer); |
5060 | 0 | } |
5061 | | |
5062 | | // load encoder layers |
5063 | 0 | for (int i = 0; i < n_layer; ++i) { |
5064 | 0 | auto & layer = layers[i]; |
5065 | |
|
5066 | 0 | layer.attn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_NORM, "weight", i), {n_embd}, 0); |
5067 | 0 | layer.attn_rel_b_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_REL_B, "weight", i), {n_head, n_rel_attn_bkts}, TENSOR_NOT_REQUIRED); |
5068 | |
|
5069 | 0 | layer.wq_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
5070 | 0 | layer.wk_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
5071 | 0 | layer.wv_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
5072 | 0 | layer.wo_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0); |
5073 | |
|
5074 | 0 | layer.ffn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_NORM, "weight", i), {n_embd}, 0); |
5075 | 0 | layer.ffn_gate_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED); |
5076 | 0 | layer.ffn_down_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
5077 | 0 | layer.ffn_up_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
5078 | 0 | } |
5079 | | |
5080 | | // load decoder layers |
5081 | 0 | for (int i = 0; i < dec_n_layer; ++i) { |
5082 | 0 | auto & layer = layers[i]; |
5083 | |
|
5084 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_DEC_ATTN_NORM, "weight", i), {n_embd}, 0); |
5085 | 0 | layer.attn_rel_b = create_tensor(tn(LLM_TENSOR_DEC_ATTN_REL_B, "weight", i), {n_head, n_rel_attn_bkts}, TENSOR_NOT_REQUIRED); |
5086 | |
|
5087 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_DEC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
5088 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_DEC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
5089 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_DEC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
5090 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_DEC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0); |
5091 | |
|
5092 | 0 | layer.attn_norm_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_NORM, "weight", i), {n_embd}, 0); |
5093 | | // this tensor seems to be unused in HF transformers implementation |
5094 | 0 | layer.attn_rel_b_cross = create_tensor( |
5095 | 0 | tn(LLM_TENSOR_DEC_CROSS_ATTN_REL_B, "weight", i), {n_head, n_rel_attn_bkts}, TENSOR_NOT_REQUIRED | TENSOR_SKIP_IF_VIRTUAL); |
5096 | |
|
5097 | 0 | layer.wq_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
5098 | 0 | layer.wk_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
5099 | 0 | layer.wv_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
5100 | 0 | layer.wo_cross = create_tensor(tn(LLM_TENSOR_DEC_CROSS_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0); |
5101 | |
|
5102 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_DEC_FFN_NORM, "weight", i), {n_embd}, 0); |
5103 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_DEC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED); |
5104 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_DEC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
5105 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_DEC_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
5106 | 0 | } |
5107 | 0 | } break; |
5108 | 0 | case LLM_ARCH_T5ENCODER: |
5109 | 0 | { |
5110 | 0 | const auto n_rel_attn_bkts = hparams.n_rel_attn_bkts; |
5111 | |
|
5112 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
5113 | | |
5114 | | // output |
5115 | 0 | output_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5116 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
5117 | | // if output is NULL, init from the input tok embed |
5118 | 0 | if (output == NULL) { |
5119 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
5120 | 0 | } |
5121 | |
|
5122 | 0 | for (int i = 0; i < n_layer; ++i) { |
5123 | 0 | auto & layer = layers[i]; |
5124 | |
|
5125 | 0 | layer.attn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_NORM, "weight", i), {n_embd}, 0); |
5126 | 0 | layer.attn_rel_b_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_REL_B, "weight", i), {n_head, n_rel_attn_bkts}, TENSOR_NOT_REQUIRED); |
5127 | |
|
5128 | 0 | layer.wq_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_Q, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
5129 | 0 | layer.wk_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
5130 | 0 | layer.wv_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
5131 | 0 | layer.wo_enc = create_tensor(tn(LLM_TENSOR_ENC_ATTN_OUT, "weight", i), {n_embd_v_gqa, n_embd}, 0); |
5132 | |
|
5133 | 0 | layer.ffn_norm_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_NORM, "weight", i), {n_embd}, 0); |
5134 | 0 | layer.ffn_gate_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED); |
5135 | 0 | layer.ffn_down_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
5136 | 0 | layer.ffn_up_enc = create_tensor(tn(LLM_TENSOR_ENC_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
5137 | 0 | } |
5138 | 0 | } break; |
5139 | 0 | case LLM_ARCH_JAIS: |
5140 | 0 | { |
5141 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
5142 | | |
5143 | | // output |
5144 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5145 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
5146 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
5147 | |
|
5148 | 0 | for (int i = 0; i < n_layer; ++i) { |
5149 | 0 | auto & layer = layers[i]; |
5150 | |
|
5151 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
5152 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
5153 | |
|
5154 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, 0); |
5155 | 0 | layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, 0); |
5156 | |
|
5157 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
5158 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); |
5159 | |
|
5160 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
5161 | 0 | layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0); |
5162 | |
|
5163 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
5164 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0); |
5165 | |
|
5166 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
5167 | 0 | layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {n_ff}, 0); |
5168 | |
|
5169 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
5170 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0); |
5171 | 0 | } |
5172 | 0 | } break; |
5173 | 0 | case LLM_ARCH_JAIS2: |
5174 | 0 | { |
5175 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
5176 | | |
5177 | | // output |
5178 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5179 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
5180 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
5181 | 0 | if (!output) { |
5182 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
5183 | 0 | } |
5184 | |
|
5185 | 0 | for (int i = 0; i < n_layer; ++i) { |
5186 | 0 | auto & layer = layers[i]; |
5187 | |
|
5188 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
5189 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
5190 | |
|
5191 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
5192 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
5193 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
5194 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
5195 | | |
5196 | | // attention biases - all have shape n_embd (output dimension of projections) |
5197 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, 0); |
5198 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd}, 0); |
5199 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd}, 0); |
5200 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); |
5201 | |
|
5202 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
5203 | 0 | layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0); |
5204 | | |
5205 | | // Jais-2 uses simple MLP (no gate) with biases |
5206 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
5207 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, 0); |
5208 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
5209 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, 0); |
5210 | 0 | } |
5211 | 0 | } break; |
5212 | 0 | case LLM_ARCH_CHATGLM: |
5213 | 0 | { |
5214 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
5215 | | |
5216 | | // output |
5217 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5218 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
5219 | | // if output is NULL, init from the input tok embed |
5220 | 0 | if (output == NULL) { |
5221 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
5222 | 0 | } |
5223 | |
|
5224 | 0 | for (int i = 0; i < n_layer; ++i) { |
5225 | 0 | auto & layer = layers[i]; |
5226 | |
|
5227 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
5228 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED); |
5229 | 0 | layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, TENSOR_NOT_REQUIRED); |
5230 | |
|
5231 | 0 | if (layer.wqkv == nullptr) { |
5232 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
5233 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
5234 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
5235 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
5236 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED); |
5237 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED); |
5238 | 0 | } |
5239 | |
|
5240 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
5241 | |
|
5242 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
5243 | |
|
5244 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, 0); |
5245 | |
|
5246 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
5247 | 0 | } |
5248 | 0 | } break; |
5249 | 0 | case LLM_ARCH_GLM4: |
5250 | 0 | { |
5251 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
5252 | | |
5253 | | // output |
5254 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5255 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
5256 | | // if output is NULL, init from the input tok embed |
5257 | 0 | if (output == NULL) { |
5258 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
5259 | 0 | } |
5260 | |
|
5261 | 0 | for (int i = 0; i < n_layer; ++i) { |
5262 | 0 | int flags = 0; |
5263 | 0 | if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) { |
5264 | | // skip all tensors in the NextN layers |
5265 | 0 | flags |= TENSOR_SKIP; |
5266 | 0 | } |
5267 | |
|
5268 | 0 | auto & layer = layers[i]; |
5269 | |
|
5270 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, flags); |
5271 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, flags | TENSOR_NOT_REQUIRED); |
5272 | 0 | layer.bqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "bias", i), {n_embd + 2*n_embd_gqa}, flags | TENSOR_NOT_REQUIRED); |
5273 | |
|
5274 | 0 | if (layer.wqkv == nullptr) { |
5275 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, flags); |
5276 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, flags); |
5277 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, flags); |
5278 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, flags | TENSOR_NOT_REQUIRED); |
5279 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, flags | TENSOR_NOT_REQUIRED); |
5280 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, flags | TENSOR_NOT_REQUIRED); |
5281 | 0 | } |
5282 | |
|
5283 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, flags); |
5284 | |
|
5285 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, flags); |
5286 | |
|
5287 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, flags); |
5288 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, flags); |
5289 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff * 2}, flags); |
5290 | |
|
5291 | 0 | layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, flags); |
5292 | | |
5293 | | // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers |
5294 | 0 | if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) { |
5295 | 0 | layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags); |
5296 | 0 | layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags); |
5297 | 0 | layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags); |
5298 | | |
5299 | | // Optional tensors |
5300 | 0 | layer.nextn.embed_tokens = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED); |
5301 | 0 | layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED); |
5302 | 0 | layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, flags | TENSOR_NOT_REQUIRED); |
5303 | 0 | } |
5304 | 0 | } |
5305 | 0 | } break; |
5306 | 0 | case LLM_ARCH_GLM4_MOE: |
5307 | 0 | { |
5308 | 0 | const int64_t n_expert = hparams.n_expert; |
5309 | 0 | const int64_t n_expert_used = hparams.n_expert_used; |
5310 | 0 | const int64_t n_expert_shared = hparams.n_expert_shared; |
5311 | |
|
5312 | 0 | GGML_ASSERT(hparams.n_expert > 0 && "n_expert must be > 0 for GLM4_MOE MoE layers"); |
5313 | 0 | GGML_ASSERT(hparams.n_expert_used > 0 && "n_expert_used must be > 0 for GLM4_MOE MoE layers"); |
5314 | |
|
5315 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0); |
5316 | | |
5317 | | // output |
5318 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0); |
5319 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED); |
5320 | | // if output is NULL, init from the input tok embed |
5321 | 0 | if (output == NULL) { |
5322 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED); |
5323 | 0 | } |
5324 | | |
5325 | | // Load ALL tensors including NextN layer to satisfy total tensor count |
5326 | | // but only PROCESS up to last layer (skipping final NextN layer) in forward pass |
5327 | 0 | for (int i = 0; i < n_layer; ++i) { |
5328 | 0 | int flags = 0; |
5329 | 0 | if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) { |
5330 | | // skip all tensors in the NextN layers |
5331 | 0 | flags |= TENSOR_SKIP; |
5332 | 0 | } |
5333 | |
|
5334 | 0 | auto & layer = layers[i]; |
5335 | |
|
5336 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, flags); |
5337 | | |
5338 | | // GLM-style attention with bias terms |
5339 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, flags); |
5340 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_k_gqa }, flags); |
5341 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_v_gqa }, flags); |
5342 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), { n_embd_head_k * n_head }, TENSOR_NOT_REQUIRED | flags); |
5343 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), { n_embd_k_gqa }, TENSOR_NOT_REQUIRED | flags); |
5344 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), { n_embd_v_gqa }, TENSOR_NOT_REQUIRED | flags); |
5345 | |
|
5346 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, flags); |
5347 | | |
5348 | | // K/Q norm tensors (optional for GLM-4.5 355B variant) |
5349 | 0 | layer.attn_q_norm = create_tensor( |
5350 | 0 | tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED | flags); |
5351 | 0 | layer.attn_k_norm = create_tensor( |
5352 | 0 | tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED | flags); |
5353 | |
|
5354 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), { n_embd }, flags); |
5355 | | |
5356 | | // Check if this layer uses MoE or dense FFN based on n_layer_dense_lead |
5357 | | // GLM 4.5 uses hybrid architecture: layer 0 is dense, layers 1+ are MoE |
5358 | 0 | const bool use_moe = (static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead); |
5359 | |
|
5360 | 0 | if (use_moe) { |
5361 | | // MoE layers |
5362 | 0 | layer.ffn_gate_inp = |
5363 | 0 | create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, flags); |
5364 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), { n_expert }, flags); |
5365 | | |
5366 | | // MoE branch |
5367 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used; |
5368 | |
|
5369 | 0 | layer.ffn_gate_exps = create_tensor( |
5370 | 0 | tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, flags); |
5371 | 0 | layer.ffn_down_exps = create_tensor( |
5372 | 0 | tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, flags); |
5373 | 0 | layer.ffn_up_exps = create_tensor( |
5374 | 0 | tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, flags); |
5375 | | |
5376 | | // Shared expert |
5377 | 0 | if (n_expert_shared > 0) { |
5378 | 0 | const int64_t n_ff_shexp = n_ff_exp * n_expert_shared; |
5379 | 0 | layer.ffn_gate_shexp = create_tensor( |
5380 | 0 | tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags); |
5381 | 0 | layer.ffn_down_shexp = create_tensor( |
5382 | 0 | tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_shexp, n_embd }, flags); |
5383 | 0 | layer.ffn_up_shexp = create_tensor( |
5384 | 0 | tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp }, flags); |
5385 | 0 | } |
5386 | 0 | } else { |
5387 | | // Dense layers (first k layers) - GLM uses separate gate/up projections |
5388 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), { n_embd, n_ff }, flags); |
5389 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, flags); |
5390 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, flags); |
5391 | 0 | } |
5392 | | |
5393 | | // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers |
5394 | 0 | if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) { |
5395 | 0 | layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags); |
5396 | 0 | layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags); |
5397 | 0 | layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags); |
5398 | | |
5399 | | // Optional tensors |
5400 | 0 | layer.nextn.embed_tokens = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED); |
5401 | 0 | layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED); |
5402 | 0 | layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, flags | TENSOR_NOT_REQUIRED); |
5403 | 0 | } |
5404 | 0 | } |
5405 | 0 | } |
5406 | 0 | break; |
5407 | 0 | case LLM_ARCH_GLM_DSA: |
5408 | 0 | { |
5409 | 0 | const bool is_mla = hparams.is_mla(); |
5410 | 0 | if (!is_mla) { |
5411 | 0 | throw std::runtime_error("GLM_DSA architecture requires MLA"); |
5412 | 0 | } |
5413 | | |
5414 | | // note: these are the actual head sizes you get when treating as MHA or after "decompression" using wv_b for MLA |
5415 | 0 | const int64_t n_embd_head_k_mla = hparams.n_embd_head_k_mla(); |
5416 | 0 | const int64_t n_embd_head_v_mla = hparams.n_embd_head_v_mla(); |
5417 | |
|
5418 | 0 | const int64_t n_embd_head_qk_rope = hparams.n_rot(); |
5419 | 0 | const int64_t n_embd_head_qk_nope = n_embd_head_k_mla - n_embd_head_qk_rope; |
5420 | |
|
5421 | 0 | const int64_t q_lora_rank = hparams.n_lora_q; |
5422 | 0 | const int64_t kv_lora_rank = hparams.n_lora_kv; |
5423 | |
|
5424 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
5425 | 0 | const int64_t n_expert_shared = hparams.n_expert_shared; |
5426 | |
|
5427 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
5428 | | |
5429 | | // output |
5430 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5431 | | // try to load output.weight, if not found, use token_embd (tied embeddings) |
5432 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
5433 | 0 | if (!output) { |
5434 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
5435 | 0 | } |
5436 | |
|
5437 | 0 | for (int i = 0; i < n_layer; ++i) { |
5438 | 0 | int flags = 0; |
5439 | 0 | if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) { |
5440 | | // skip all tensors in the NextN layers |
5441 | | // TODO @ngxson : TENSOR_NOT_REQUIRED was a hack, need to remove it later |
5442 | 0 | flags |= TENSOR_SKIP | TENSOR_NOT_REQUIRED; |
5443 | 0 | } |
5444 | |
|
5445 | 0 | auto & layer = layers[i]; |
5446 | |
|
5447 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, flags); |
5448 | 0 | layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, flags); |
5449 | 0 | layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, flags); |
5450 | |
|
5451 | 0 | layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, flags); |
5452 | 0 | layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k_mla}, flags); |
5453 | |
|
5454 | 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}, flags); |
5455 | | |
5456 | | // note: only old legacy GGUF files will have the unsplit wkv_b tensor in |
5457 | 0 | layer.wk_b = create_tensor(tn(LLM_TENSOR_ATTN_K_B, "weight", i), {n_embd_head_qk_nope, kv_lora_rank, n_head}, flags); |
5458 | 0 | layer.wv_b = create_tensor(tn(LLM_TENSOR_ATTN_V_B, "weight", i), {kv_lora_rank, n_embd_head_v_mla, n_head}, flags); |
5459 | |
|
5460 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_embd_head_v_mla, n_embd}, flags); |
5461 | |
|
5462 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, flags); |
5463 | | |
5464 | | // DSA indexer |
5465 | 0 | layer.indexer_k_norm = create_tensor(tn(LLM_TENSOR_INDEXER_K_NORM, "weight", i), {hparams.indexer_head_size}, flags); |
5466 | 0 | layer.indexer_k_norm_b = create_tensor(tn(LLM_TENSOR_INDEXER_K_NORM, "bias", i), {hparams.indexer_head_size}, flags); |
5467 | 0 | layer.indexer_proj = create_tensor(tn(LLM_TENSOR_INDEXER_PROJ, "weight", i), {n_embd, hparams.indexer_n_head}, flags); |
5468 | 0 | layer.indexer_attn_k = create_tensor(tn(LLM_TENSOR_INDEXER_ATTN_K, "weight", i), {n_embd, hparams.indexer_head_size}, flags); |
5469 | 0 | layer.indexer_attn_q_b = create_tensor(tn(LLM_TENSOR_INDEXER_ATTN_Q_B, "weight", i), {q_lora_rank, hparams.indexer_n_head * hparams.indexer_head_size}, flags); |
5470 | 0 | if (i < (int) hparams.n_layer_dense_lead) { |
5471 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, flags); |
5472 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, flags); |
5473 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, flags); |
5474 | 0 | } else { |
5475 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, flags); |
5476 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED); |
5477 | |
|
5478 | 0 | if (n_expert == 0) { |
5479 | 0 | throw std::runtime_error("n_expert must be > 0"); |
5480 | 0 | } |
5481 | 0 | if (n_expert_used == 0) { |
5482 | 0 | throw std::runtime_error("n_expert_used must be > 0"); |
5483 | 0 | } |
5484 | | |
5485 | | // MoE branch |
5486 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, flags); |
5487 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, flags); |
5488 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, flags); |
5489 | | |
5490 | | // Shared expert branch |
5491 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, flags); |
5492 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, flags); |
5493 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, flags); |
5494 | 0 | } |
5495 | | |
5496 | | // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers |
5497 | 0 | if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) { |
5498 | 0 | layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags); |
5499 | 0 | layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags); |
5500 | 0 | layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags); |
5501 | | |
5502 | | // Optional tensors |
5503 | 0 | layer.nextn.embed_tokens = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED); |
5504 | 0 | layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, flags | TENSOR_NOT_REQUIRED); |
5505 | 0 | layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, flags | TENSOR_NOT_REQUIRED); |
5506 | 0 | } |
5507 | 0 | } |
5508 | 0 | } break; |
5509 | 0 | case LLM_ARCH_NEMOTRON: |
5510 | 0 | { |
5511 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
5512 | | |
5513 | | // output |
5514 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5515 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
5516 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
5517 | |
|
5518 | 0 | for (int i = 0; i < n_layer; ++i) { |
5519 | 0 | auto & layer = layers[i]; |
5520 | |
|
5521 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
5522 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
5523 | |
|
5524 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
5525 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
5526 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
5527 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
5528 | | |
5529 | | // optional bias tensors |
5530 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
5531 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED); |
5532 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED); |
5533 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
5534 | |
|
5535 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
5536 | 0 | layer.ffn_norm_b = create_tensor(tn(LLM_TENSOR_FFN_NORM, "bias", i), {n_embd}, 0); |
5537 | |
|
5538 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
5539 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
5540 | | |
5541 | | // optional MLP bias |
5542 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
5543 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {n_ff}, TENSOR_NOT_REQUIRED); |
5544 | 0 | } |
5545 | 0 | } break; |
5546 | 0 | case LLM_ARCH_NEMOTRON_H: |
5547 | 0 | case LLM_ARCH_NEMOTRON_H_MOE: |
5548 | 0 | { |
5549 | | // mamba2 Mixer SSM params |
5550 | | // NOTE: int64_t for tensor dimensions |
5551 | 0 | const int64_t d_conv = hparams.ssm_d_conv; |
5552 | 0 | const int64_t d_inner = hparams.ssm_d_inner; |
5553 | 0 | const int64_t d_state = hparams.ssm_d_state; |
5554 | 0 | const int64_t n_ssm_head = hparams.ssm_dt_rank; |
5555 | 0 | const int64_t n_group = hparams.ssm_n_group; |
5556 | 0 | const int64_t d_in_proj = 2*d_inner + 2*n_group*d_state + n_ssm_head; |
5557 | 0 | const int64_t moe_n_embd = hparams.moe_latent_size > 0 ? hparams.moe_latent_size : n_embd; |
5558 | | |
5559 | | // embeddings |
5560 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
5561 | | |
5562 | | // output |
5563 | 0 | { |
5564 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5565 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
5566 | | // if output is NULL, init from the input tok embed, duplicated to allow offloading |
5567 | 0 | if (output == NULL) { |
5568 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
5569 | 0 | } |
5570 | 0 | } |
5571 | |
|
5572 | 0 | for (int i = 0; i < n_layer; ++i) { |
5573 | 0 | auto & layer = layers[i]; |
5574 | | |
5575 | | // all blocks use the attn norm |
5576 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
5577 | |
|
5578 | 0 | if (hparams.is_recurrent(i)) { |
5579 | | // ssm layers |
5580 | 0 | layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {n_embd, d_in_proj}, 0); |
5581 | |
|
5582 | 0 | layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {d_conv, d_inner + 2*n_group*d_state}, 0); |
5583 | 0 | layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {d_inner + 2*n_group*d_state}, TENSOR_NOT_REQUIRED); |
5584 | |
|
5585 | 0 | layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_ssm_head}, 0); |
5586 | | |
5587 | | // no "weight" suffix for these |
5588 | 0 | layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_ssm_head}, 0); |
5589 | 0 | layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, n_ssm_head}, 0); |
5590 | |
|
5591 | 0 | layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {d_inner / n_group, n_group}, 0); |
5592 | | |
5593 | | // out_proj |
5594 | 0 | layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {d_inner, n_embd}, 0); |
5595 | 0 | } else if (hparams.n_ff(i) == 0) { |
5596 | | // attention layers (with optional bias) |
5597 | 0 | const int64_t n_head_i = hparams.n_head(i); |
5598 | 0 | const int64_t n_embd_k_gqa_i = hparams.n_embd_k_gqa(i); |
5599 | 0 | const int64_t n_embd_v_gqa_i = hparams.n_embd_v_gqa(i); |
5600 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head_i}, 0); |
5601 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa_i}, 0); |
5602 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa_i}, 0); |
5603 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head_i, n_embd}, 0); |
5604 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
5605 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_k_gqa_i}, TENSOR_NOT_REQUIRED); |
5606 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_v_gqa_i}, TENSOR_NOT_REQUIRED); |
5607 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
5608 | 0 | } else { |
5609 | 0 | if (n_expert != 0) { |
5610 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used; |
5611 | 0 | const int64_t n_ff_shexp = hparams.n_ff_shexp; |
5612 | |
|
5613 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert}, 0); |
5614 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert }, 0); |
5615 | | |
5616 | | // MoE branch |
5617 | 0 | layer.ffn_latent_down = create_tensor(tn(LLM_TENSOR_FFN_LATENT_DOWN, "weight", i), {n_embd, moe_n_embd}, TENSOR_NOT_REQUIRED); |
5618 | 0 | layer.ffn_latent_up = create_tensor(tn(LLM_TENSOR_FFN_LATENT_UP, "weight", i), {moe_n_embd, n_embd}, TENSOR_NOT_REQUIRED); |
5619 | |
|
5620 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, moe_n_embd, n_expert}, 0); |
5621 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {moe_n_embd, n_ff_exp, n_expert}, 0); |
5622 | | |
5623 | | // Shared expert branch |
5624 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, 0); |
5625 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_shexp}, 0); |
5626 | |
|
5627 | 0 | } else { |
5628 | | // mlp layers |
5629 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { hparams.n_ff(i), n_embd}, 0); |
5630 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, hparams.n_ff(i)}, 0); |
5631 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
5632 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {hparams.n_ff(i)}, TENSOR_NOT_REQUIRED); |
5633 | 0 | } |
5634 | 0 | } |
5635 | 0 | } |
5636 | 0 | } break; |
5637 | 0 | case LLM_ARCH_EXAONE: |
5638 | 0 | { |
5639 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
5640 | | |
5641 | | // output |
5642 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5643 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
5644 | | |
5645 | | // if output is NULL, init from the input tok embed |
5646 | 0 | if (output == NULL) { |
5647 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
5648 | 0 | } |
5649 | |
|
5650 | 0 | for (int i = 0; i < n_layer; ++i) { |
5651 | 0 | auto & layer = layers[i]; |
5652 | |
|
5653 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
5654 | |
|
5655 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
5656 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
5657 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
5658 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
5659 | |
|
5660 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
5661 | 0 | layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
5662 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
5663 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
5664 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
5665 | 0 | } |
5666 | 0 | } break; |
5667 | 0 | case LLM_ARCH_EXAONE4: |
5668 | 0 | { |
5669 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
5670 | | |
5671 | | // output |
5672 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5673 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
5674 | | |
5675 | | // if output is NULL, init from the input tok embed |
5676 | 0 | if (output == NULL) { |
5677 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
5678 | 0 | } |
5679 | |
|
5680 | 0 | for (int i = 0; i < n_layer; ++i) { |
5681 | 0 | auto & layer = layers[i]; |
5682 | |
|
5683 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
5684 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
5685 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
5686 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
5687 | |
|
5688 | 0 | layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
5689 | |
|
5690 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0); |
5691 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
5692 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
5693 | |
|
5694 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
5695 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
5696 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
5697 | 0 | layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0); |
5698 | 0 | } |
5699 | 0 | } break; |
5700 | 0 | case LLM_ARCH_EXAONE_MOE: |
5701 | 0 | { |
5702 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
5703 | 0 | const int64_t n_expert = hparams.n_expert; |
5704 | 0 | const int64_t n_expert_used = hparams.n_expert_used; |
5705 | 0 | const int64_t n_ff_shexp = hparams.n_ff_shexp > 0 ? hparams.n_ff_shexp : n_ff_exp; |
5706 | 0 | const int64_t head_dim = hparams.n_embd_head_k(); |
5707 | 0 | const int64_t n_qo_dim = n_head * head_dim; |
5708 | 0 | const int64_t n_kv_dim = n_head_kv * head_dim; |
5709 | |
|
5710 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
5711 | | |
5712 | | // output |
5713 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5714 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
5715 | |
|
5716 | 0 | if (output == NULL) { |
5717 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
5718 | 0 | } |
5719 | |
|
5720 | 0 | for (int i = 0; i < n_layer; ++i) { |
5721 | 0 | int flags = 0; |
5722 | 0 | if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) { |
5723 | | // skip all tensors in the NextN layers |
5724 | 0 | flags |= TENSOR_SKIP; |
5725 | 0 | } |
5726 | |
|
5727 | 0 | auto & layer = layers[i]; |
5728 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_qo_dim}, flags); |
5729 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_kv_dim}, flags); |
5730 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_kv_dim}, flags); |
5731 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_qo_dim, n_embd}, flags); |
5732 | |
|
5733 | 0 | layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0) | flags); |
5734 | |
|
5735 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, flags); |
5736 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, flags); |
5737 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, flags); |
5738 | |
|
5739 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, flags); |
5740 | | |
5741 | | // dense layers for first n_layer_dense_lead layers or nextn_predict_layers layers at the end |
5742 | 0 | if (i < (int) hparams.n_layer_dense_lead || (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers)) { |
5743 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, flags); |
5744 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, flags); |
5745 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, flags); |
5746 | 0 | } else { |
5747 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, flags); |
5748 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED | flags); |
5749 | |
|
5750 | 0 | if (n_expert == 0) { |
5751 | 0 | throw std::runtime_error("n_expert must be > 0"); |
5752 | 0 | } |
5753 | 0 | if (n_expert_used == 0) { |
5754 | 0 | throw std::runtime_error("n_expert_used must be > 0"); |
5755 | 0 | } |
5756 | | |
5757 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, flags); |
5758 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, flags); |
5759 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, flags); |
5760 | |
|
5761 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_shexp}, flags); |
5762 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, flags); |
5763 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_shexp}, flags); |
5764 | 0 | } |
5765 | | |
5766 | | // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers |
5767 | 0 | if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) { |
5768 | 0 | layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), {2 * n_embd, n_embd}, flags); |
5769 | 0 | layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), {n_embd}, flags); |
5770 | 0 | layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), {n_embd}, flags); |
5771 | |
|
5772 | 0 | layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), {n_embd}, flags | TENSOR_NOT_REQUIRED); |
5773 | 0 | layer.nextn.embed_tokens = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), {n_embd, n_vocab}, flags | TENSOR_NOT_REQUIRED); |
5774 | 0 | layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), {n_embd, n_vocab}, flags | TENSOR_NOT_REQUIRED); |
5775 | 0 | } |
5776 | 0 | } |
5777 | 0 | } break; |
5778 | 0 | case LLM_ARCH_RWKV6: |
5779 | 0 | { |
5780 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
5781 | | |
5782 | | // Block 0, LN0 |
5783 | 0 | tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0); |
5784 | 0 | tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0); |
5785 | | |
5786 | | // output |
5787 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5788 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
5789 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
5790 | |
|
5791 | 0 | const int time_mix_extra_dim = hparams.time_mix_extra_dim; |
5792 | 0 | const int time_decay_extra_dim = hparams.time_decay_extra_dim; |
5793 | 0 | const int head_size = hparams.wkv_head_size; |
5794 | 0 | const int attn_hidden_size = n_embd; |
5795 | 0 | const int ffn_size = hparams.n_ff_arr[0]; |
5796 | |
|
5797 | 0 | for (int i = 0; i < n_layer; ++i) { |
5798 | 0 | auto & layer = layers[i]; |
5799 | |
|
5800 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
5801 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
5802 | |
|
5803 | 0 | layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, 0); |
5804 | 0 | layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, 0); |
5805 | |
|
5806 | 0 | layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, time_mix_extra_dim * 5}, 0); |
5807 | 0 | layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {time_mix_extra_dim, n_embd, 5}, 0); |
5808 | |
|
5809 | 0 | layer.time_mix_lerp_x = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_X, "weight", i), {n_embd, 1, 1}, 0); |
5810 | 0 | layer.time_mix_lerp_w = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_W, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED); |
5811 | 0 | layer.time_mix_lerp_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED); |
5812 | 0 | layer.time_mix_lerp_v = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_V, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED); |
5813 | 0 | layer.time_mix_lerp_r = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_R, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED); |
5814 | 0 | layer.time_mix_lerp_g = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_G, "weight", i), {n_embd, 1, 1}, TENSOR_NOT_REQUIRED); |
5815 | 0 | layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, TENSOR_NOT_REQUIRED); |
5816 | 0 | GGML_ASSERT(!(layer.time_mix_lerp_fused == NULL && layer.time_mix_lerp_w == NULL)); |
5817 | |
|
5818 | 0 | layer.time_mix_first = create_tensor(tn(LLM_TENSOR_TIME_MIX_FIRST, "weight", i), {head_size, n_embd / head_size}, 0); |
5819 | 0 | layer.time_mix_decay = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY, "weight", i), {n_embd}, 0); |
5820 | 0 | layer.time_mix_decay_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W1, "weight", i), {n_embd, time_decay_extra_dim}, 0); |
5821 | 0 | layer.time_mix_decay_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W2, "weight", i), {time_decay_extra_dim, attn_hidden_size}, 0); |
5822 | 0 | layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0); |
5823 | 0 | layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0); |
5824 | 0 | layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0); |
5825 | 0 | layer.time_mix_gate = create_tensor(tn(LLM_TENSOR_TIME_MIX_GATE, "weight", i), {attn_hidden_size, n_embd}, 0); |
5826 | |
|
5827 | 0 | layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, 0); |
5828 | 0 | layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, 0); |
5829 | 0 | layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0); |
5830 | |
|
5831 | 0 | layer.channel_mix_lerp_k = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, 0); |
5832 | 0 | layer.channel_mix_lerp_r = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_R, "weight", i), {n_embd, 1, 1}, 0); |
5833 | |
|
5834 | 0 | layer.channel_mix_key = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_KEY, "weight", i), {n_embd, ffn_size}, 0); |
5835 | 0 | layer.channel_mix_value = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_VALUE, "weight", i), {ffn_size, n_embd}, 0); |
5836 | 0 | layer.channel_mix_receptance = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_RECEPTANCE, "weight", i), {n_embd, n_embd}, 0); |
5837 | 0 | } |
5838 | |
|
5839 | 0 | } break; |
5840 | 0 | case LLM_ARCH_RWKV6QWEN2: |
5841 | 0 | { |
5842 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
5843 | |
|
5844 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5845 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, TENSOR_NOT_REQUIRED); |
5846 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
5847 | |
|
5848 | 0 | const int time_mix_extra_dim = hparams.time_mix_extra_dim; |
5849 | 0 | const int time_decay_extra_dim = hparams.time_decay_extra_dim; |
5850 | 0 | const int head_size = hparams.wkv_head_size; |
5851 | 0 | const int attn_hidden_size = n_embd; |
5852 | 0 | const int n_head_kv = hparams.n_head_kv(); |
5853 | 0 | int attn_key_value_size; |
5854 | 0 | if (n_head_kv == 0 || attn_hidden_size / head_size == n_head_kv) { |
5855 | 0 | attn_key_value_size = attn_hidden_size; |
5856 | 0 | } else { |
5857 | 0 | attn_key_value_size = n_head_kv * head_size; |
5858 | 0 | } |
5859 | |
|
5860 | 0 | for (int i = 0; i < n_layer; ++i) { |
5861 | 0 | auto & layer = layers[i]; |
5862 | |
|
5863 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
5864 | |
|
5865 | 0 | layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, time_mix_extra_dim * 5}, 0); |
5866 | 0 | layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {time_mix_extra_dim, n_embd, 5}, 0); |
5867 | |
|
5868 | 0 | layer.time_mix_lerp_x = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_X, "weight", i), {n_embd, 1, 1}, 0); |
5869 | 0 | layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, 0); |
5870 | |
|
5871 | 0 | layer.time_mix_first = create_tensor(tn(LLM_TENSOR_TIME_MIX_FIRST, "weight", i), {head_size, n_embd / head_size}, TENSOR_NOT_REQUIRED); |
5872 | 0 | layer.time_mix_decay = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY, "weight", i), {n_embd}, 0); |
5873 | 0 | layer.time_mix_decay_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W1, "weight", i), {n_embd, time_decay_extra_dim}, 0); |
5874 | 0 | layer.time_mix_decay_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_DECAY_W2, "weight", i), {time_decay_extra_dim, attn_hidden_size}, 0); |
5875 | 0 | layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {n_embd, attn_key_value_size}, 0); |
5876 | 0 | layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {n_embd, attn_key_value_size}, 0); |
5877 | 0 | layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0); |
5878 | 0 | layer.time_mix_gate = create_tensor(tn(LLM_TENSOR_TIME_MIX_GATE, "weight", i), {attn_hidden_size, n_embd}, 0); |
5879 | | // optional bias tensors |
5880 | 0 | layer.time_mix_key_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "bias", i), {attn_key_value_size}, TENSOR_NOT_REQUIRED); |
5881 | 0 | layer.time_mix_value_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "bias", i), {attn_key_value_size}, TENSOR_NOT_REQUIRED); |
5882 | 0 | layer.time_mix_receptance_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "bias", i), {attn_hidden_size}, TENSOR_NOT_REQUIRED); |
5883 | |
|
5884 | 0 | layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0); |
5885 | |
|
5886 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
5887 | |
|
5888 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
5889 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
5890 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
5891 | 0 | } |
5892 | 0 | } break; |
5893 | 0 | case LLM_ARCH_RWKV7: |
5894 | 0 | { |
5895 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
5896 | | |
5897 | | // Block 0, LN0 |
5898 | 0 | tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {n_embd}, 0); |
5899 | 0 | tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {n_embd}, 0); |
5900 | | |
5901 | | // output |
5902 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5903 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
5904 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
5905 | |
|
5906 | 0 | const int n_lora_decay = hparams.n_lora_decay; |
5907 | 0 | const int n_lora_iclr = hparams.n_lora_iclr; |
5908 | 0 | const int n_lora_value_res_mix = hparams.n_lora_value_res_mix; |
5909 | 0 | const int n_lora_gate = hparams.n_lora_gate; |
5910 | 0 | const int attn_hidden_size = n_embd; |
5911 | 0 | const int ffn_size = hparams.n_ff_arr[0]; |
5912 | |
|
5913 | 0 | for (int i = 0; i < n_layer; ++i) { |
5914 | 0 | auto & layer = layers[i]; |
5915 | |
|
5916 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
5917 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "bias", i), {n_embd}, 0); |
5918 | |
|
5919 | 0 | layer.attn_norm_2 = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "weight", i), {n_embd}, 0); |
5920 | 0 | layer.attn_norm_2_b = create_tensor(tn(LLM_TENSOR_ATTN_NORM_2, "bias", i), {n_embd}, 0); |
5921 | |
|
5922 | 0 | layer.time_mix_w0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W0, "weight", i), {n_embd}, 0); |
5923 | 0 | layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, n_lora_decay}, 0); |
5924 | 0 | layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {n_lora_decay, n_embd}, 0); |
5925 | |
|
5926 | 0 | layer.time_mix_a0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A0, "weight", i), {n_embd}, 0); |
5927 | 0 | layer.time_mix_a1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A1, "weight", i), {n_embd, n_lora_iclr}, 0); |
5928 | 0 | layer.time_mix_a2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A2, "weight", i), {n_lora_iclr, n_embd}, 0); |
5929 | |
|
5930 | 0 | if (i == 0) { |
5931 | | // actually not used |
5932 | 0 | layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0); |
5933 | 0 | layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_iclr}, 0); |
5934 | 0 | layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_iclr, n_embd}, 0); |
5935 | 0 | } else { |
5936 | 0 | layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0); |
5937 | 0 | layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_value_res_mix}, 0); |
5938 | 0 | layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_value_res_mix, n_embd}, 0); |
5939 | 0 | } |
5940 | |
|
5941 | 0 | layer.time_mix_g1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G1, "weight", i), {n_embd, n_lora_gate}, 0); |
5942 | 0 | layer.time_mix_g2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G2, "weight", i), {n_lora_gate, n_embd}, 0); |
5943 | |
|
5944 | 0 | layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 6}, 0); |
5945 | |
|
5946 | 0 | layer.time_mix_k_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_K, "weight", i), {attn_hidden_size}, 0); |
5947 | 0 | layer.time_mix_k_a = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_A, "weight", i), {attn_hidden_size}, 0); |
5948 | 0 | layer.time_mix_r_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_R_K, "weight", i), {attn_hidden_size}, 0); |
5949 | |
|
5950 | 0 | layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0); |
5951 | 0 | layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0); |
5952 | 0 | layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0); |
5953 | |
|
5954 | 0 | layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, 0); |
5955 | 0 | layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, 0); |
5956 | 0 | layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0); |
5957 | |
|
5958 | 0 | layer.channel_mix_lerp_k = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_LERP_K, "weight", i), {n_embd, 1, 1}, 0); |
5959 | |
|
5960 | 0 | layer.channel_mix_key = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_KEY, "weight", i), {n_embd, ffn_size}, 0); |
5961 | 0 | layer.channel_mix_value = create_tensor(tn(LLM_TENSOR_CHANNEL_MIX_VALUE, "weight", i), {ffn_size, n_embd}, 0); |
5962 | 0 | } |
5963 | |
|
5964 | 0 | } break; |
5965 | 0 | case LLM_ARCH_ARWKV7: |
5966 | 0 | { |
5967 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
5968 | | |
5969 | | // output |
5970 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
5971 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
5972 | |
|
5973 | 0 | const int n_lora_decay = hparams.n_lora_decay; |
5974 | 0 | const int n_lora_iclr = hparams.n_lora_iclr; |
5975 | 0 | const int n_lora_value_res_mix = hparams.n_lora_value_res_mix; |
5976 | 0 | const int n_lora_gate = hparams.n_lora_gate; |
5977 | 0 | const int attn_hidden_size = n_embd; |
5978 | |
|
5979 | 0 | for (int i = 0; i < n_layer; ++i) { |
5980 | 0 | auto & layer = layers[i]; |
5981 | |
|
5982 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
5983 | |
|
5984 | 0 | layer.time_mix_w0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W0, "weight", i), {n_embd}, 0); |
5985 | 0 | layer.time_mix_w1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W1, "weight", i), {n_embd, n_lora_decay}, 0); |
5986 | 0 | layer.time_mix_w2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_W2, "weight", i), {n_lora_decay, n_embd}, 0); |
5987 | |
|
5988 | 0 | layer.time_mix_a0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A0, "weight", i), {n_embd}, 0); |
5989 | 0 | layer.time_mix_a1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A1, "weight", i), {n_embd, n_lora_iclr}, 0); |
5990 | 0 | layer.time_mix_a2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_A2, "weight", i), {n_lora_iclr, n_embd}, 0); |
5991 | |
|
5992 | 0 | if (i == 0) { |
5993 | | // actually not used |
5994 | 0 | layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0); |
5995 | 0 | layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_iclr}, 0); |
5996 | 0 | layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_iclr, n_embd}, 0); |
5997 | 0 | } else { |
5998 | 0 | layer.time_mix_v0 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V0, "weight", i), {n_embd}, 0); |
5999 | 0 | layer.time_mix_v1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V1, "weight", i), {n_embd, n_lora_value_res_mix}, 0); |
6000 | 0 | layer.time_mix_v2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_V2, "weight", i), {n_lora_value_res_mix, n_embd}, 0); |
6001 | 0 | } |
6002 | |
|
6003 | 0 | layer.time_mix_g1 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G1, "weight", i), {n_embd, n_lora_gate}, TENSOR_NOT_REQUIRED); |
6004 | 0 | layer.time_mix_g2 = create_tensor(tn(LLM_TENSOR_TIME_MIX_G2, "weight", i), {n_lora_gate, n_embd}, TENSOR_NOT_REQUIRED); |
6005 | |
|
6006 | 0 | try { |
6007 | 0 | layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 6}, 0); |
6008 | 0 | } catch(std::runtime_error & e) { |
6009 | | // ARWKV models may not have gate tensors |
6010 | 0 | layer.time_mix_lerp_fused = create_tensor(tn(LLM_TENSOR_TIME_MIX_LERP_FUSED, "weight", i), {n_embd, 1, 1, 5}, 0); |
6011 | 0 | } |
6012 | |
|
6013 | 0 | layer.time_mix_k_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_K, "weight", i), {attn_hidden_size}, 0); |
6014 | 0 | layer.time_mix_k_a = create_tensor(tn(LLM_TENSOR_TIME_MIX_K_A, "weight", i), {attn_hidden_size}, 0); |
6015 | 0 | layer.time_mix_r_k = create_tensor(tn(LLM_TENSOR_TIME_MIX_R_K, "weight", i), {attn_hidden_size}, 0); |
6016 | |
|
6017 | 0 | layer.time_mix_key = create_tensor(tn(LLM_TENSOR_TIME_MIX_KEY, "weight", i), {attn_hidden_size, n_embd}, 0); |
6018 | 0 | layer.time_mix_value = create_tensor(tn(LLM_TENSOR_TIME_MIX_VALUE, "weight", i), {attn_hidden_size, n_embd}, 0); |
6019 | 0 | layer.time_mix_receptance = create_tensor(tn(LLM_TENSOR_TIME_MIX_RECEPTANCE, "weight", i), {attn_hidden_size, n_embd}, 0); |
6020 | |
|
6021 | 0 | layer.time_mix_ln = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "weight", i), {n_embd}, TENSOR_NOT_REQUIRED); |
6022 | 0 | layer.time_mix_ln_b = create_tensor(tn(LLM_TENSOR_TIME_MIX_LN, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
6023 | 0 | layer.time_mix_output = create_tensor(tn(LLM_TENSOR_TIME_MIX_OUTPUT, "weight", i), {n_embd, attn_hidden_size}, 0); |
6024 | |
|
6025 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
6026 | |
|
6027 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
6028 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
6029 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
6030 | 0 | } |
6031 | |
|
6032 | 0 | } break; |
6033 | 0 | case LLM_ARCH_CHAMELEON: |
6034 | 0 | { |
6035 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
6036 | | |
6037 | | // output |
6038 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
6039 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
6040 | | // if output is NULL, init from the input tok embed |
6041 | 0 | if (output == NULL) { |
6042 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
6043 | 0 | } |
6044 | |
|
6045 | 0 | for (int i = 0; i < n_layer; ++i) { |
6046 | 0 | auto & layer = layers[i]; |
6047 | |
|
6048 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
6049 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k, n_head}, 0); |
6050 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k, n_head_kv}, 0); |
6051 | 0 | layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), {n_embd_head_k, n_head}, TENSOR_NOT_REQUIRED); |
6052 | 0 | layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), {n_embd_head_k, n_head_kv}, TENSOR_NOT_REQUIRED); |
6053 | |
|
6054 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
6055 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
6056 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
6057 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
6058 | |
|
6059 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
6060 | |
|
6061 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
6062 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
6063 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
6064 | 0 | } |
6065 | 0 | } break; |
6066 | 0 | case LLM_ARCH_WAVTOKENIZER_DEC: |
6067 | 0 | { |
6068 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hparams.n_embd, n_vocab}, 0); |
6069 | |
|
6070 | 0 | conv1d = create_tensor(tn(LLM_TENSOR_CONV1D, "weight"), {7, hparams.n_embd, hparams.posnet.n_embd}, 0); |
6071 | 0 | conv1d_b = create_tensor(tn(LLM_TENSOR_CONV1D, "bias"), {1, hparams.posnet.n_embd}, 0); |
6072 | | |
6073 | | // posnet |
6074 | 0 | { |
6075 | 0 | const int64_t n_embd = hparams.posnet.n_embd; |
6076 | |
|
6077 | 0 | for (uint32_t i = 0; i < hparams.posnet.n_layer; ++i) { |
6078 | 0 | auto & layer = layers[i].posnet; |
6079 | | |
6080 | | // posnet: |
6081 | | // |
6082 | | // - resnet |
6083 | | // - resnet |
6084 | | // - attn |
6085 | | // - resnet |
6086 | | // - resnet |
6087 | | // - norm |
6088 | | // |
6089 | 0 | switch (i) { |
6090 | 0 | case 0: |
6091 | 0 | case 1: |
6092 | 0 | case 3: |
6093 | 0 | case 4: |
6094 | 0 | { |
6095 | 0 | layer.norm1 = create_tensor(tn(LLM_TENSOR_POS_NET_NORM1, "weight", i), {1, n_embd}, 0); |
6096 | 0 | layer.norm1_b = create_tensor(tn(LLM_TENSOR_POS_NET_NORM1, "bias", i), {1, n_embd}, 0); |
6097 | |
|
6098 | 0 | layer.conv1 = create_tensor(tn(LLM_TENSOR_POS_NET_CONV1, "weight", i), {3, n_embd, n_embd}, 0); |
6099 | 0 | layer.conv1_b = create_tensor(tn(LLM_TENSOR_POS_NET_CONV1, "bias", i), {1, n_embd}, 0); |
6100 | |
|
6101 | 0 | layer.norm2 = create_tensor(tn(LLM_TENSOR_POS_NET_NORM2, "weight", i), {1, n_embd}, 0); |
6102 | 0 | layer.norm2_b = create_tensor(tn(LLM_TENSOR_POS_NET_NORM2, "bias", i), {1, n_embd}, 0); |
6103 | |
|
6104 | 0 | layer.conv2 = create_tensor(tn(LLM_TENSOR_POS_NET_CONV2, "weight", i), {3, n_embd, n_embd}, 0); |
6105 | 0 | layer.conv2_b = create_tensor(tn(LLM_TENSOR_POS_NET_CONV2, "bias", i), {1, n_embd}, 0); |
6106 | 0 | } break; |
6107 | 0 | case 2: |
6108 | 0 | { |
6109 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "weight", i), {1, n_embd}, 0); |
6110 | 0 | layer.attn_norm_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "bias", i), {1, n_embd}, 0); |
6111 | |
|
6112 | 0 | layer.attn_q = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_Q, "weight", i), {1, n_embd, n_embd}, 0); |
6113 | 0 | layer.attn_q_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_Q, "bias", i), {1, n_embd}, 0); |
6114 | |
|
6115 | 0 | layer.attn_k = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_K, "weight", i), {1, n_embd, n_embd}, 0); |
6116 | 0 | layer.attn_k_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_K, "bias", i), {1, n_embd}, 0); |
6117 | |
|
6118 | 0 | layer.attn_v = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_V, "weight", i), {1, n_embd, n_embd}, 0); |
6119 | 0 | layer.attn_v_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_V, "bias", i), {1, n_embd}, 0); |
6120 | |
|
6121 | 0 | layer.attn_o = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_OUT, "weight", i), {1, n_embd, n_embd}, 0); |
6122 | 0 | layer.attn_o_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_OUT, "bias", i), {1, n_embd}, 0); |
6123 | 0 | } break; |
6124 | 0 | case 5: |
6125 | 0 | { |
6126 | 0 | layer.norm = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "weight", i), {1, n_embd}, 0); |
6127 | 0 | layer.norm_b = create_tensor(tn(LLM_TENSOR_POS_NET_ATTN_NORM, "bias", i), {1, n_embd}, 0); |
6128 | 0 | } break; |
6129 | 0 | default: GGML_ABORT("unknown posnet layer"); |
6130 | 0 | }; |
6131 | 0 | } |
6132 | 0 | } |
6133 | | |
6134 | 0 | GGML_ASSERT(hparams.posnet.n_embd == hparams.convnext.n_embd); |
6135 | |
|
6136 | 0 | tok_norm = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "weight"), {hparams.posnet.n_embd}, 0); |
6137 | 0 | tok_norm_b = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD_NORM, "bias"), {hparams.posnet.n_embd}, 0); |
6138 | | |
6139 | | // convnext |
6140 | 0 | { |
6141 | 0 | const int64_t n_embd = hparams.convnext.n_embd; |
6142 | |
|
6143 | 0 | for (uint32_t i = 0; i < hparams.convnext.n_layer; ++i) { |
6144 | 0 | auto & layer = layers[i].convnext; |
6145 | |
|
6146 | 0 | layer.dw = create_tensor(tn(LLM_TENSOR_CONVNEXT_DW, "weight", i), {7, 1, n_embd}, 0); |
6147 | 0 | layer.dw_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_DW, "bias", i), {1, n_embd}, 0); |
6148 | |
|
6149 | 0 | layer.norm = create_tensor(tn(LLM_TENSOR_CONVNEXT_NORM, "weight", i), {n_embd}, 0); |
6150 | 0 | layer.norm_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_NORM, "bias", i), {n_embd}, 0); |
6151 | |
|
6152 | 0 | layer.pw1 = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW1, "weight", i), {n_embd, n_ff}, 0); |
6153 | 0 | layer.pw1_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW1, "bias", i), {n_ff}, 0); |
6154 | |
|
6155 | 0 | layer.pw2 = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW2, "weight", i), {n_ff, n_embd}, 0); |
6156 | 0 | layer.pw2_b = create_tensor(tn(LLM_TENSOR_CONVNEXT_PW2, "bias", i), {n_embd}, 0); |
6157 | |
|
6158 | 0 | layer.gamma = create_tensor(tn(LLM_TENSOR_CONVNEXT_GAMMA, "weight", i), {n_embd}, 0); |
6159 | 0 | } |
6160 | | |
6161 | | // output |
6162 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
6163 | 0 | output_norm_b = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "bias"), {n_embd}, 0); |
6164 | 0 | } |
6165 | |
|
6166 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {hparams.convnext.n_embd, hparams.n_embd_out()}, 0); |
6167 | 0 | output_b = create_tensor(tn(LLM_TENSOR_OUTPUT, "bias"), {hparams.n_embd_out()}, 0); |
6168 | 0 | } break; |
6169 | 0 | case LLM_ARCH_BAILINGMOE: |
6170 | 0 | { |
6171 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
6172 | 0 | const int64_t n_expert_shared = hparams.n_expert_shared; |
6173 | |
|
6174 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
6175 | | |
6176 | | // output |
6177 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
6178 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
6179 | |
|
6180 | 0 | for (int i = 0; i < n_layer; ++i) { |
6181 | 0 | auto & layer = layers[i]; |
6182 | |
|
6183 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
6184 | |
|
6185 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_rot}, 0); |
6186 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_head_kv * n_rot}, 0); |
6187 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_head_kv * n_rot}, 0); |
6188 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_rot, n_embd}, 0); |
6189 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
6190 | |
|
6191 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
6192 | |
|
6193 | 0 | if (n_expert == 0) { |
6194 | 0 | throw std::runtime_error("n_expert must be > 0"); |
6195 | 0 | } |
6196 | 0 | if (n_expert_used == 0) { |
6197 | 0 | throw std::runtime_error("n_expert_used must be > 0"); |
6198 | 0 | } |
6199 | | |
6200 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
6201 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0); |
6202 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
6203 | |
|
6204 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0); |
6205 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0); |
6206 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0); |
6207 | 0 | } |
6208 | 0 | } break; |
6209 | 0 | case LLM_ARCH_BAILINGMOE2: |
6210 | 0 | { |
6211 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
6212 | 0 | const int64_t n_expert_shared = hparams.n_expert_shared; |
6213 | |
|
6214 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
6215 | | |
6216 | | // output |
6217 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
6218 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
6219 | |
|
6220 | 0 | GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for bailingmoe2"); |
6221 | 0 | GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for bailingmoe2"); |
6222 | |
|
6223 | 0 | for (int i = 0; i < n_layer; ++i) { |
6224 | 0 | int flags = 0; |
6225 | 0 | if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) { |
6226 | | // skip all tensors in the NextN layers |
6227 | 0 | flags |= TENSOR_SKIP; |
6228 | 0 | } |
6229 | |
|
6230 | 0 | auto & layer = layers[i]; |
6231 | |
|
6232 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, flags); |
6233 | |
|
6234 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd + 2*n_embd_gqa}, flags); |
6235 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, flags); |
6236 | |
|
6237 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, flags); |
6238 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, flags); |
6239 | |
|
6240 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, flags); |
6241 | |
|
6242 | 0 | if (static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead) { // MoE layers |
6243 | 0 | const int64_t n_ff_shexp = (hparams.n_ff_shexp ? hparams.n_ff_shexp : n_ff_exp) * n_expert_shared; |
6244 | |
|
6245 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, flags); |
6246 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED | flags); |
6247 | |
|
6248 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, flags); |
6249 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, flags); |
6250 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, flags); |
6251 | |
|
6252 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_shexp}, flags); |
6253 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, flags); |
6254 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_shexp}, flags); |
6255 | 0 | } else { // Dense layers |
6256 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, flags); |
6257 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, flags); |
6258 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, flags); |
6259 | 0 | } |
6260 | | |
6261 | | // NextN/MTP tensors (preserved but unused) - conditionally load for last nextn_predict_layers |
6262 | 0 | if (hparams.nextn_predict_layers > 0 && static_cast<uint32_t>(i) >= n_layer - hparams.nextn_predict_layers) { |
6263 | 0 | layer.nextn.eh_proj = create_tensor(tn(LLM_TENSOR_NEXTN_EH_PROJ, "weight", i), { 2 * n_embd, n_embd }, flags); |
6264 | 0 | layer.nextn.embed_tokens = create_tensor(tn(LLM_TENSOR_NEXTN_EMBED_TOKENS, "weight", i), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED | flags); |
6265 | 0 | layer.nextn.enorm = create_tensor(tn(LLM_TENSOR_NEXTN_ENORM, "weight", i), { n_embd }, flags); |
6266 | 0 | layer.nextn.hnorm = create_tensor(tn(LLM_TENSOR_NEXTN_HNORM, "weight", i), { n_embd }, flags); |
6267 | 0 | layer.nextn.shared_head_head = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_HEAD, "weight", i), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED | flags); |
6268 | 0 | layer.nextn.shared_head_norm = create_tensor(tn(LLM_TENSOR_NEXTN_SHARED_HEAD_NORM, "weight", i), { n_embd }, TENSOR_NOT_REQUIRED | flags); |
6269 | 0 | layer.layer_out_norm = create_tensor(tn(LLM_TENSOR_LAYER_OUT_NORM, "weight", i), {n_embd}, flags); |
6270 | 0 | } |
6271 | 0 | } |
6272 | 0 | } break; |
6273 | 0 | case LLM_ARCH_DOTS1: |
6274 | 0 | { |
6275 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
6276 | 0 | const int64_t n_expert_shared = hparams.n_expert_shared; |
6277 | |
|
6278 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
6279 | |
|
6280 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
6281 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
6282 | |
|
6283 | 0 | for (int i = 0; i < n_layer; ++i) { |
6284 | 0 | auto & layer = layers[i]; |
6285 | |
|
6286 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
6287 | |
|
6288 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
6289 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
6290 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
6291 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
6292 | |
|
6293 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
6294 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
6295 | |
|
6296 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
6297 | |
|
6298 | 0 | if (i < (int) hparams.n_layer_dense_lead) { |
6299 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
6300 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
6301 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
6302 | 0 | } else { |
6303 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
6304 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED); |
6305 | |
|
6306 | 0 | if (n_expert == 0) { |
6307 | 0 | throw std::runtime_error("n_expert must be > 0"); |
6308 | 0 | } |
6309 | 0 | if (n_expert_used == 0) { |
6310 | 0 | throw std::runtime_error("n_expert_used must be > 0"); |
6311 | 0 | } |
6312 | | |
6313 | | // MoE branch |
6314 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
6315 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0); |
6316 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
6317 | | |
6318 | | // Shared expert branch |
6319 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0); |
6320 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_exp * n_expert_shared, n_embd}, 0); |
6321 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_exp * n_expert_shared}, 0); |
6322 | 0 | } |
6323 | 0 | } |
6324 | 0 | } break; |
6325 | 0 | case LLM_ARCH_ARCEE: |
6326 | 0 | { |
6327 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
6328 | | |
6329 | | // output |
6330 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
6331 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
6332 | | |
6333 | | // if output is NULL, init from the input tok embed |
6334 | 0 | if (output == NULL) { |
6335 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
6336 | 0 | } |
6337 | |
|
6338 | 0 | for (int i = 0; i < n_layer; ++i) { |
6339 | 0 | auto & layer = layers[i]; |
6340 | |
|
6341 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
6342 | |
|
6343 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
6344 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
6345 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
6346 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
6347 | |
|
6348 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
6349 | |
|
6350 | 0 | layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
6351 | |
|
6352 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
6353 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
6354 | 0 | } |
6355 | 0 | } break; |
6356 | 0 | case LLM_ARCH_AFMOE: |
6357 | 0 | { |
6358 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
6359 | | |
6360 | | // output |
6361 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
6362 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
6363 | | |
6364 | | // if output is NULL, init from the input tok embed |
6365 | 0 | if (output == NULL) { |
6366 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
6367 | 0 | } |
6368 | |
|
6369 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
6370 | 0 | const int64_t n_expert_shared = hparams.n_expert_shared; |
6371 | |
|
6372 | 0 | for (int i = 0; i < n_layer; ++i) { |
6373 | 0 | auto & layer = layers[i]; |
6374 | | |
6375 | | // dual attention normalization |
6376 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
6377 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0); |
6378 | | |
6379 | | // attention projections |
6380 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
6381 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
6382 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
6383 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
6384 | | |
6385 | | // Q/K normalization |
6386 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
6387 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
6388 | | |
6389 | | // attention gating |
6390 | 0 | layer.wqkv_gate = create_tensor(tn(LLM_TENSOR_ATTN_GATE, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
6391 | | |
6392 | | // dual ffn normalization |
6393 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
6394 | 0 | layer.ffn_post_norm = create_tensor(tn(LLM_TENSOR_FFN_POST_NORM, "weight", i), {n_embd}, 0); |
6395 | |
|
6396 | 0 | if (static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead) { |
6397 | | // MoE layers |
6398 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
6399 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, 0); |
6400 | | |
6401 | | // grouped expert weights |
6402 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0); |
6403 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0); |
6404 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0); |
6405 | | |
6406 | | // shared expert |
6407 | 0 | if (n_expert_shared > 0) { |
6408 | 0 | const int64_t n_ff_shexp = n_ff_exp * n_expert_shared; |
6409 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_shexp}, 0); |
6410 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, 0); |
6411 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_shexp}, 0); |
6412 | 0 | } |
6413 | 0 | } else { |
6414 | | // Dense layers |
6415 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
6416 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
6417 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
6418 | 0 | } |
6419 | 0 | } |
6420 | 0 | } break; |
6421 | 0 | case LLM_ARCH_ERNIE4_5: |
6422 | 0 | case LLM_ARCH_ERNIE4_5_MOE: |
6423 | 0 | case LLM_ARCH_PADDLEOCR: |
6424 | 0 | { |
6425 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
6426 | | |
6427 | | // output |
6428 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
6429 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
6430 | | // if output is NULL, init from the input tok embed |
6431 | 0 | if (output == NULL) { |
6432 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
6433 | 0 | } |
6434 | |
|
6435 | 0 | for (int i = 0; i < n_layer; ++i) { |
6436 | 0 | auto & layer = layers[i]; |
6437 | |
|
6438 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
6439 | |
|
6440 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
6441 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
6442 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
6443 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
6444 | | |
6445 | | // optional bias tensors |
6446 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
6447 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED); |
6448 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, TENSOR_NOT_REQUIRED); |
6449 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, TENSOR_NOT_REQUIRED); |
6450 | |
|
6451 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
6452 | |
|
6453 | 0 | if (arch == LLM_ARCH_ERNIE4_5_MOE && static_cast<uint32_t>(i) >= hparams.n_layer_dense_lead) { // MoE layers |
6454 | 0 | int n_ff_exp = hparams.n_ff_exp; |
6455 | |
|
6456 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
6457 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED); |
6458 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED); |
6459 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert}, 0); |
6460 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0); |
6461 | | |
6462 | | // Shared expert (if present) |
6463 | 0 | if (hparams.n_ff_shexp > 0) { |
6464 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, hparams.n_ff_shexp}, 0); |
6465 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd }, 0); |
6466 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, hparams.n_ff_shexp}, 0); |
6467 | 0 | } |
6468 | 0 | } else { // Dense layers |
6469 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
6470 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
6471 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
6472 | 0 | } |
6473 | 0 | } |
6474 | 0 | } break; |
6475 | 0 | case LLM_ARCH_FALCON_H1: |
6476 | 0 | { |
6477 | | // Common |
6478 | 0 | const int64_t hidden_size = hparams.n_embd; // hidden_size |
6479 | | |
6480 | | // mamba2 Mixer SSM params |
6481 | 0 | const int64_t ssm_conv_kernel_size = hparams.ssm_d_conv; // ssm_conv_kernel_size |
6482 | 0 | const int64_t ssm_n_groups = hparams.ssm_n_group; // ssm_n_groups |
6483 | 0 | const int64_t ssm_state_size = hparams.ssm_d_state; // ssm_state_size |
6484 | 0 | const int64_t ssm_intermediate_size = hparams.ssm_d_inner; // TODO expand |
6485 | 0 | const int64_t ssm_num_heads = hparams.ssm_dt_rank; // ssm_num_heads |
6486 | 0 | const int64_t ssm_conv_dim = ssm_intermediate_size + 2 * ssm_n_groups * ssm_state_size; |
6487 | 0 | const int64_t ssm_projection_size = ssm_intermediate_size + ssm_conv_dim + ssm_num_heads; |
6488 | | |
6489 | | // attn params |
6490 | 0 | const int64_t attn_num_attention_head = hparams.n_head(0); // rename to: attn_num_attention_head |
6491 | 0 | const int64_t attn_num_key_value_head = hparams.n_head_kv(0); |
6492 | | |
6493 | | // ffn params |
6494 | 0 | const int64_t ffn_intermediate_size = hparams.n_ff(0); |
6495 | | |
6496 | | // embeddings |
6497 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hidden_size, n_vocab}, 0); |
6498 | | |
6499 | | // output |
6500 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {hidden_size, n_vocab}, TENSOR_NOT_REQUIRED); |
6501 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {hidden_size}, 0); |
6502 | | |
6503 | | // if output is NULL, init from the input tok embed |
6504 | 0 | if (output == NULL) { |
6505 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {hidden_size, n_vocab}, TENSOR_DUPLICATED); |
6506 | 0 | } |
6507 | |
|
6508 | 0 | for (int i = 0; i < n_layer; ++i) { |
6509 | 0 | auto & layer = layers[i]; |
6510 | | |
6511 | | /*SSM LAYERS*/ |
6512 | | // ssm in |
6513 | 0 | layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), {hidden_size, ssm_projection_size}, 0); |
6514 | | // ssm 1d conv |
6515 | 0 | layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), {ssm_conv_kernel_size, ssm_conv_dim}, 0); |
6516 | 0 | layer.ssm_conv1d_b = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "bias", i), {ssm_conv_dim}, TENSOR_NOT_REQUIRED); |
6517 | | // ssm_dt |
6518 | 0 | layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {ssm_num_heads}, 0); |
6519 | | // no "weight" suffix for these |
6520 | 0 | layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, ssm_num_heads}, 0); |
6521 | 0 | layer.ssm_d = create_tensor(tn(LLM_TENSOR_SSM_D, i), {1, ssm_num_heads}, 0); |
6522 | | // ssm_norm |
6523 | 0 | layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {ssm_intermediate_size / ssm_n_groups, ssm_n_groups}, TENSOR_NOT_REQUIRED); |
6524 | | // out_proj |
6525 | 0 | layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), {ssm_intermediate_size, hidden_size}, 0); |
6526 | | |
6527 | | /*ATTENTION LAYERS*/ |
6528 | | // attention layers (with optional bias) |
6529 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {hidden_size, n_embd_head_k * attn_num_attention_head}, 0); |
6530 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {hidden_size, attn_num_key_value_head * n_embd_head_k}, 0); |
6531 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {hidden_size, attn_num_key_value_head * n_embd_head_v}, 0); |
6532 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * attn_num_attention_head, hidden_size}, 0); |
6533 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED); |
6534 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {attn_num_key_value_head * n_embd_head_k}, TENSOR_NOT_REQUIRED); |
6535 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {attn_num_key_value_head * n_embd_head_v}, TENSOR_NOT_REQUIRED); |
6536 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED); |
6537 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {hidden_size}, 0); |
6538 | | |
6539 | | |
6540 | | // feed forward (w/ optional biases) |
6541 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, i), {hidden_size}, 0); |
6542 | 0 | layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
6543 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {hidden_size, ffn_intermediate_size}, 0); |
6544 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { ffn_intermediate_size, hidden_size}, 0); |
6545 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {hidden_size, ffn_intermediate_size}, 0); |
6546 | |
|
6547 | 0 | layer.ffn_gate_b = create_tensor(tn(LLM_TENSOR_FFN_GATE, "bias", i), {ffn_intermediate_size}, TENSOR_NOT_REQUIRED); |
6548 | 0 | layer.ffn_down_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "bias", i), {hidden_size}, TENSOR_NOT_REQUIRED); |
6549 | 0 | layer.ffn_up_b = create_tensor(tn(LLM_TENSOR_FFN_UP, "bias", i), {ffn_intermediate_size}, TENSOR_NOT_REQUIRED); |
6550 | 0 | } |
6551 | 0 | } break; |
6552 | 0 | case LLM_ARCH_HUNYUAN_MOE: |
6553 | 0 | { |
6554 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
6555 | | |
6556 | | // output |
6557 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
6558 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
6559 | | // if output is NULL, init from the input tok embed |
6560 | 0 | if (output == NULL) { |
6561 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
6562 | 0 | } |
6563 | |
|
6564 | 0 | for (int i = 0; i < n_layer; ++i) { |
6565 | 0 | auto & layer = layers[i]; |
6566 | 0 | const uint32_t n_ff_shexp = hparams.n_ff_shexp > 0 ? hparams.n_ff_shexp : hparams.n_ff(i); |
6567 | |
|
6568 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
6569 | |
|
6570 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
6571 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
6572 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
6573 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
6574 | |
|
6575 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
6576 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
6577 | |
|
6578 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
6579 | |
|
6580 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
6581 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
6582 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff, n_embd, n_expert}, 0); |
6583 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
6584 | |
|
6585 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_shexp}, 0); |
6586 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_shexp}, 0); |
6587 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp, n_embd}, 0); |
6588 | 0 | } |
6589 | 0 | } break; |
6590 | 0 | case LLM_ARCH_HUNYUAN_DENSE: |
6591 | 0 | { |
6592 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
6593 | | |
6594 | | // output |
6595 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
6596 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
6597 | | // if output is NULL, init from the input tok embed |
6598 | 0 | if (output == NULL) { |
6599 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
6600 | 0 | } |
6601 | |
|
6602 | 0 | for (int i = 0; i < n_layer; ++i) { |
6603 | 0 | auto & layer = layers[i]; |
6604 | |
|
6605 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
6606 | |
|
6607 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
6608 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
6609 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
6610 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
6611 | |
|
6612 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
6613 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
6614 | |
|
6615 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
6616 | |
|
6617 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
6618 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
6619 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
6620 | |
|
6621 | 0 | } |
6622 | 0 | } break; |
6623 | 0 | case LLM_ARCH_SMOLLM3: |
6624 | 0 | { |
6625 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
6626 | | |
6627 | | // output |
6628 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
6629 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
6630 | | |
6631 | | // if output is NULL, init from the input tok embed |
6632 | 0 | if (output == NULL) { |
6633 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
6634 | 0 | } |
6635 | |
|
6636 | 0 | for (int i = 0; i < n_layer; ++i) { |
6637 | 0 | auto & layer = layers[i]; |
6638 | |
|
6639 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
6640 | |
|
6641 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
6642 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
6643 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
6644 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
6645 | |
|
6646 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
6647 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
6648 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
6649 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
6650 | 0 | } |
6651 | 0 | } break; |
6652 | 0 | case LLM_ARCH_OPENAI_MOE: |
6653 | 0 | { |
6654 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
6655 | |
|
6656 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
6657 | | |
6658 | | // output |
6659 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
6660 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
6661 | |
|
6662 | 0 | for (int i = 0; i < n_layer; ++i) { |
6663 | 0 | auto & layer = layers[i]; |
6664 | |
|
6665 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
6666 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), {n_embd}, 0); |
6667 | |
|
6668 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_rot}, 0); |
6669 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_head_kv * n_rot}, 0); |
6670 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_head_kv * n_rot}, 0); |
6671 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_rot, n_embd}, 0); |
6672 | |
|
6673 | 0 | layer.attn_sinks = create_tensor(tn(LLM_TENSOR_ATTN_SINKS, "weight", i), {n_head}, 0); |
6674 | |
|
6675 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert}, 0); |
6676 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
6677 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0); |
6678 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
6679 | | |
6680 | | // bias |
6681 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_head * n_rot}, 0); |
6682 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_head_kv * n_rot}, 0); |
6683 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_head_kv * n_rot}, 0); |
6684 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); |
6685 | |
|
6686 | 0 | layer.ffn_gate_inp_b = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "bias", i), {n_expert}, 0); |
6687 | 0 | layer.ffn_gate_exps_b = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "bias", i), {n_ff_exp, n_expert}, 0); |
6688 | 0 | layer.ffn_down_exps_b = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "bias", i), { n_embd, n_expert}, 0); |
6689 | 0 | layer.ffn_up_exps_b = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "bias", i), {n_ff_exp, n_expert}, 0); |
6690 | 0 | } |
6691 | 0 | } break; |
6692 | 0 | case LLM_ARCH_LFM2: |
6693 | 0 | case LLM_ARCH_LFM2MOE: |
6694 | 0 | { |
6695 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
6696 | |
|
6697 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM_LFM2, "weight"), {n_embd}, 0); |
6698 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
6699 | |
|
6700 | 0 | if (output == NULL) { |
6701 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
6702 | 0 | } |
6703 | |
|
6704 | 0 | for (int i = 0; i < n_layer; ++i) { |
6705 | 0 | auto & layer = layers[i]; |
6706 | |
|
6707 | 0 | const bool is_moe_layer = i >= static_cast<int>(hparams.n_layer_dense_lead); |
6708 | | |
6709 | | // ffn/moe is same for transformer and conv layers |
6710 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
6711 | 0 | if (is_moe_layer) { |
6712 | 0 | GGML_ASSERT(n_expert && n_expert_used); |
6713 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
6714 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, hparams.n_ff_exp, n_expert}, 0); |
6715 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {hparams.n_ff_exp, n_embd, n_expert}, 0); |
6716 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, hparams.n_ff_exp, n_expert}, 0); |
6717 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, 0); |
6718 | 0 | } else { // dense |
6719 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
6720 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
6721 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
6722 | 0 | } |
6723 | | |
6724 | | // for operator_norm |
6725 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
6726 | |
|
6727 | 0 | if (!hparams.is_recurrent(i)) { |
6728 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
6729 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
6730 | 0 | GGML_ASSERT(n_embd_v_gqa == n_embd_k_gqa); |
6731 | |
|
6732 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd}, 0); |
6733 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, hparams.n_embd_k_gqa(i)}, 0); |
6734 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, hparams.n_embd_v_gqa(i)}, 0); |
6735 | |
|
6736 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd, n_embd}, 0); |
6737 | 0 | } else { |
6738 | 0 | layer.shortconv.conv = create_tensor(tn(LLM_TENSOR_SHORTCONV_CONV, "weight", i), {hparams.n_shortconv_l_cache, n_embd}, 0); |
6739 | 0 | layer.shortconv.in_proj = create_tensor(tn(LLM_TENSOR_SHORTCONV_INPROJ, "weight", i), {n_embd, 3 * n_embd}, 0); |
6740 | 0 | layer.shortconv.out_proj = create_tensor(tn(LLM_TENSOR_SHORTCONV_OUTPROJ, "weight", i), {n_embd, n_embd}, 0); |
6741 | 0 | } |
6742 | 0 | } |
6743 | | |
6744 | | // for LFM2-ColBert-350M |
6745 | 0 | dense_2_out_layers = create_tensor(tn(LLM_TENSOR_DENSE_2_OUT, "weight"), {n_embd, hparams.n_embd_out()}, TENSOR_NOT_REQUIRED); |
6746 | 0 | dense_2_out_layers_b = create_tensor(tn(LLM_TENSOR_DENSE_2_OUT, "bias"), {hparams.n_embd_out() }, TENSOR_NOT_REQUIRED); |
6747 | 0 | } break; |
6748 | 0 | case LLM_ARCH_SMALLTHINKER: |
6749 | 0 | { |
6750 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0); |
6751 | | |
6752 | | // output |
6753 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0); |
6754 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
6755 | | |
6756 | | // if output is NULL, init from the input tok embed |
6757 | 0 | if (output == NULL) { |
6758 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
6759 | 0 | } |
6760 | |
|
6761 | 0 | for (int i = 0; i < n_layer; ++i) { |
6762 | 0 | auto & layer = layers[i]; |
6763 | |
|
6764 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0); |
6765 | |
|
6766 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, 0); |
6767 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_gqa }, 0); |
6768 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_gqa }, 0); |
6769 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0); |
6770 | |
|
6771 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0); |
6772 | |
|
6773 | 0 | GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for SMALLTHINKER"); |
6774 | 0 | GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for SMALLTHINKER"); |
6775 | | |
6776 | | // MoE branch |
6777 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
6778 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, 0); |
6779 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0); |
6780 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, 0); |
6781 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert }, 0); |
6782 | 0 | } |
6783 | 0 | } break; |
6784 | 0 | case LLM_ARCH_GROVEMOE: |
6785 | 0 | { |
6786 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
6787 | | |
6788 | | // output |
6789 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
6790 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
6791 | | // if output is NULL, init from the input tok embed |
6792 | 0 | if (output == NULL) { |
6793 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
6794 | 0 | } |
6795 | |
|
6796 | 0 | GGML_ASSERT(n_expert > 0 && "n_expert must be > 0 for GROVEMOE"); |
6797 | 0 | GGML_ASSERT(n_expert_used > 0 && "n_expert_used must be > 0 for GROVEMOE"); |
6798 | 0 | GGML_ASSERT(hparams.n_group_experts > 0 && "n_group_experts must be > 0 for GROVEMOE"); |
6799 | |
|
6800 | 0 | for (int i = 0; i < n_layer; ++i) { |
6801 | 0 | auto & layer = layers[i]; |
6802 | |
|
6803 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
6804 | |
|
6805 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
6806 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
6807 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
6808 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
6809 | |
|
6810 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
6811 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
6812 | |
|
6813 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
6814 | |
|
6815 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
6816 | | |
6817 | | // MoE branch |
6818 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used; |
6819 | 0 | const int64_t n_ff_chexp = hparams.n_ff_chexp ? hparams.n_ff_chexp : n_embd_head_k; |
6820 | 0 | const int64_t n_chunk_expert = n_expert / hparams.n_group_experts; |
6821 | |
|
6822 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
6823 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0); |
6824 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), { n_embd, n_ff_exp, n_expert}, 0); |
6825 | |
|
6826 | 0 | layer.ffn_gate_chexps = create_tensor(tn(LLM_TENSOR_FFN_GATE_CHEXPS, "weight", i), { n_embd, n_ff_chexp, n_chunk_expert}, 0); |
6827 | 0 | layer.ffn_down_chexps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_CHEXPS, "weight", i), {n_ff_chexp, n_embd, n_chunk_expert}, 0); |
6828 | 0 | layer.ffn_up_chexps = create_tensor(tn(LLM_TENSOR_FFN_UP_CHEXPS, "weight", i), { n_embd, n_ff_chexp, n_chunk_expert}, 0); |
6829 | 0 | } |
6830 | 0 | } break; |
6831 | 0 | case LLM_ARCH_APERTUS: |
6832 | 0 | { |
6833 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0); |
6834 | | |
6835 | | // output |
6836 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0); |
6837 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, 0); |
6838 | |
|
6839 | 0 | for (int i = 0; i < n_layer; ++i) { |
6840 | 0 | auto & layer = layers[i]; |
6841 | |
|
6842 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0); |
6843 | |
|
6844 | 0 | if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) { |
6845 | 0 | layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
6846 | 0 | layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
6847 | 0 | } else { |
6848 | 0 | layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), { n_rot/2 }, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
6849 | 0 | } |
6850 | |
|
6851 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, 0); |
6852 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_gqa }, 0); |
6853 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_gqa }, 0); |
6854 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0); |
6855 | | |
6856 | | // optional bias tensors |
6857 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED); |
6858 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), { n_embd_gqa }, TENSOR_NOT_REQUIRED); |
6859 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), { n_embd_gqa }, TENSOR_NOT_REQUIRED); |
6860 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), { n_embd }, TENSOR_NOT_REQUIRED); |
6861 | |
|
6862 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), { n_embd }, 0); |
6863 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd }, 0); |
6864 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), { n_embd, n_ff }, 0); |
6865 | | |
6866 | | // Q and K layernorms for Apertus |
6867 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), { n_embd_head_k }, 0); |
6868 | 0 | layer.attn_q_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "bias", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED); |
6869 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), { n_embd_head_k }, 0); |
6870 | 0 | layer.attn_k_norm_b = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "bias", i), { n_embd_head_k }, TENSOR_NOT_REQUIRED); |
6871 | 0 | } |
6872 | 0 | } break; |
6873 | 0 | case LLM_ARCH_MINIMAX_M2: |
6874 | 0 | { |
6875 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
6876 | | |
6877 | | // output |
6878 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
6879 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
6880 | |
|
6881 | 0 | for (int i = 0; i < n_layer; ++i) { |
6882 | 0 | auto & layer = layers[i]; |
6883 | |
|
6884 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, 0); |
6885 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_gqa }, 0); |
6886 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_gqa }, 0); |
6887 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0); |
6888 | |
|
6889 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
6890 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k * n_head}, 0); |
6891 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_k_gqa}, 0); |
6892 | |
|
6893 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
6894 | |
|
6895 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
6896 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
6897 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff, n_embd, n_expert}, 0); |
6898 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff, n_expert}, 0); |
6899 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, 0); |
6900 | 0 | } |
6901 | 0 | } break; |
6902 | 0 | case LLM_ARCH_KIMI_LINEAR: |
6903 | 0 | { |
6904 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
6905 | | |
6906 | | // output |
6907 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
6908 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
6909 | |
|
6910 | 0 | for (int i = 0; i < n_layer; ++i) { |
6911 | 0 | auto & layer = layers[i]; |
6912 | |
|
6913 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
6914 | | |
6915 | | // Check for KDA specific tensors to determine layer type or if it's a mixed model |
6916 | | // Assuming KDA layer if KDA tensors are present |
6917 | | |
6918 | | // KDA uses head_dim = 128 (from linear_attn_config.head_dim) |
6919 | 0 | const int64_t n_embd_head_k_kda = hparams.n_embd_head_kda; |
6920 | 0 | const int64_t n_embd_head_v_kda = hparams.n_embd_head_kda; |
6921 | 0 | const int64_t ssm_d_conv = hparams.ssm_d_conv; |
6922 | |
|
6923 | 0 | if (hparams.is_recurrent(i)) { |
6924 | | // Conv1d weights: try 4D first, then 3D (quantization may remove trailing 1) |
6925 | | // 4D: [d_conv, 1, d_inner, 1], 3D: [d_conv, 1, d_inner] |
6926 | 0 | layer.ssm_q_conv = create_tensor(tn(LLM_TENSOR_SSM_CONV1D_Q, "weight", i), {ssm_d_conv, 1, n_embd_head_k_kda * n_head, 1}, TENSOR_NOT_REQUIRED); |
6927 | 0 | if (!layer.ssm_q_conv) { |
6928 | 0 | layer.ssm_q_conv = create_tensor(tn(LLM_TENSOR_SSM_CONV1D_Q, "weight", i), {ssm_d_conv, 1, n_embd_head_k_kda * n_head}, 0); |
6929 | 0 | } |
6930 | | |
6931 | | // KDA Layer - Conv1d weights may be 3D or 4D |
6932 | 0 | layer.ssm_k_conv = create_tensor(tn(LLM_TENSOR_SSM_CONV1D_K, "weight", i), {ssm_d_conv, 1, n_embd_head_k_kda * n_head, 1}, TENSOR_NOT_REQUIRED); |
6933 | 0 | if (!layer.ssm_k_conv) { |
6934 | 0 | layer.ssm_k_conv = create_tensor(tn(LLM_TENSOR_SSM_CONV1D_K, "weight", i), {ssm_d_conv, 1, n_embd_head_k_kda * n_head}, 0); |
6935 | 0 | } |
6936 | 0 | layer.ssm_v_conv = create_tensor(tn(LLM_TENSOR_SSM_CONV1D_V, "weight", i), {ssm_d_conv, 1, n_embd_head_v_kda * n_head, 1}, TENSOR_NOT_REQUIRED); |
6937 | 0 | if (!layer.ssm_v_conv) { |
6938 | 0 | layer.ssm_v_conv = create_tensor(tn(LLM_TENSOR_SSM_CONV1D_V, "weight", i), {ssm_d_conv, 1, n_embd_head_v_kda * n_head}, 0); |
6939 | 0 | } |
6940 | | |
6941 | | // q, k, v projections |
6942 | | // Python: q_proj, k_proj, v_proj |
6943 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k_kda * n_head}, 0); |
6944 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_head_k_kda * n_head}, 0); |
6945 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_head_v_kda * n_head}, 0); |
6946 | | |
6947 | | // KDA specific projections |
6948 | | // f_a_proj, f_b_proj |
6949 | 0 | layer.ssm_f_a = create_tensor(tn(LLM_TENSOR_SSM_F_A, "weight", i), {n_embd, n_embd_head_k_kda}, 0); // head_dim |
6950 | 0 | layer.ssm_f_b = create_tensor(tn(LLM_TENSOR_SSM_F_B, "weight", i), {n_embd_head_k_kda, n_embd_head_k_kda * n_head}, 0); // projection_size |
6951 | | |
6952 | | // b_proj (beta mixing coefficient) |
6953 | 0 | layer.ssm_beta = create_tensor(tn(LLM_TENSOR_SSM_BETA, "weight", i), {n_embd, n_head}, 0); |
6954 | | |
6955 | | // A_log - Shape in GGUF: [1, num_heads, 1, 1] (4D) or [1, num_heads] (2D after quantization) Note: -exp(A_log) is applied in convert_hf_to_gguf.py |
6956 | 0 | layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_head, 1, 1}, TENSOR_NOT_REQUIRED); |
6957 | 0 | if (!layer.ssm_a) { |
6958 | 0 | layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A, i), {1, n_head}, 0); |
6959 | 0 | } |
6960 | | |
6961 | | // dt_bias - shape [n_embd_head_k_kda * n_head] = [4096] |
6962 | 0 | layer.ssm_dt_b = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), {n_embd_head_k_kda * n_head}, 0); |
6963 | | |
6964 | | // g_a_proj, g_b_proj (output gate) |
6965 | 0 | layer.ssm_g_a = create_tensor(tn(LLM_TENSOR_SSM_G_A, "weight", i), {n_embd, n_embd_head_k_kda}, 0); |
6966 | 0 | layer.ssm_g_b = create_tensor(tn(LLM_TENSOR_SSM_G_B, "weight", i), {n_embd_head_k_kda, n_embd_head_k_kda * n_head}, 0); |
6967 | | |
6968 | | // o_norm (reusing SSM_NORM) |
6969 | 0 | layer.ssm_o_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), {n_embd_head_k_kda}, 0); // FusedRMSNormGated |
6970 | | |
6971 | | // o_proj |
6972 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_v_kda * n_head, n_embd}, 0); |
6973 | |
|
6974 | 0 | } else { |
6975 | | // MLA Layer - use MLA-specific head dimensions |
6976 | 0 | const int64_t q_lora_rank = hparams.n_lora_q; |
6977 | 0 | const int64_t kv_lora_rank = hparams.n_lora_kv; |
6978 | 0 | const int64_t n_embd_head_k_mla = hparams.n_embd_head_k_mla(); |
6979 | 0 | const int64_t n_embd_head_v_mla = hparams.n_embd_head_v_mla(); |
6980 | |
|
6981 | 0 | layer.attn_q_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_A_NORM, "weight", i), {q_lora_rank}, TENSOR_NOT_REQUIRED); |
6982 | 0 | layer.attn_kv_a_norm = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_NORM, "weight", i), {kv_lora_rank}, 0); |
6983 | |
|
6984 | 0 | if (layer.attn_q_a_norm) { |
6985 | 0 | layer.wq_a = create_tensor(tn(LLM_TENSOR_ATTN_Q_A, "weight", i), {n_embd, q_lora_rank}, 0); |
6986 | 0 | layer.wq_b = create_tensor(tn(LLM_TENSOR_ATTN_Q_B, "weight", i), {q_lora_rank, n_head * n_embd_head_k_mla}, 0); |
6987 | 0 | } else { |
6988 | | // Kimi MLA without Q compression: wq = [n_embd, n_head * n_embd_head_k_mla] |
6989 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_head * n_embd_head_k_mla}, 0); |
6990 | 0 | } |
6991 | | |
6992 | | // Kimi: qk_rope_head_dim = 64 (actual RoPE dimension for MLA) |
6993 | | // Note: hparams.n_rot may be 72 (from conversion) but actual is 64 |
6994 | 0 | const int64_t qk_rope_head_dim = hparams.n_rot(); // From config: qk_rope_head_dim |
6995 | 0 | layer.wkv_a_mqa = create_tensor(tn(LLM_TENSOR_ATTN_KV_A_MQA, "weight", i), {n_embd, kv_lora_rank + qk_rope_head_dim}, 0); |
6996 | | // Support Legacy GGUFs that don't split wkv_b (MLA KV cache disabled) |
6997 | 0 | layer.wkv_b = create_tensor(tn(LLM_TENSOR_ATTN_KV_B, "weight", i), |
6998 | 0 | {kv_lora_rank, n_head * (n_embd_head_k_mla - qk_rope_head_dim + n_embd_head_v_mla)}, TENSOR_NOT_REQUIRED | TENSOR_SKIP_IF_VIRTUAL); |
6999 | 0 | if (!layer.wkv_b) { // MLA KV cache enabled |
7000 | 0 | layer.wk_b = create_tensor(tn(LLM_TENSOR_ATTN_K_B, "weight", i), {n_embd_head_k_mla - qk_rope_head_dim, kv_lora_rank, n_head}, 0); |
7001 | 0 | layer.wv_b = create_tensor(tn(LLM_TENSOR_ATTN_V_B, "weight", i), {kv_lora_rank, n_embd_head_v_mla, n_head}, 0); |
7002 | 0 | } |
7003 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_head * n_embd_head_v_mla, n_embd}, 0); |
7004 | 0 | } |
7005 | |
|
7006 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
7007 | | |
7008 | | // MoE intermediate size (different from dense FFN) |
7009 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
7010 | | |
7011 | | // Kimi uses n_layer_dense_lead to determine which layers use dense FFN vs MoE |
7012 | | // first_k_dense_replace = 1 means layer 0 uses dense FFN, layers 1+ use MoE |
7013 | 0 | if (i < (int) hparams.n_layer_dense_lead) { |
7014 | | // Dense FFN layer - use normal n_ff |
7015 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
7016 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), {n_ff, n_embd}, 0); |
7017 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
7018 | 0 | } else { |
7019 | | // MoE layer - use n_ff_exp (1024) instead of n_ff (9216) |
7020 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, 0); |
7021 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0); |
7022 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, 0); |
7023 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, 0); |
7024 | | |
7025 | | // Shared experts use moe_intermediate_size * num_shared_experts |
7026 | | // Kimi: shared_expert_intermediate_size = 1024 * 1 = 1024 |
7027 | | // Tensors are 2D: [n_embd, n_ff_shexp] or [n_ff_shexp, n_embd] |
7028 | 0 | const int64_t n_ff_shexp_actual = n_ff_exp * (hparams.n_expert_shared > 0 ? hparams.n_expert_shared : 1); |
7029 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, n_ff_shexp_actual}, TENSOR_NOT_REQUIRED); |
7030 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {n_ff_shexp_actual, n_embd}, TENSOR_NOT_REQUIRED); |
7031 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, n_ff_shexp_actual}, TENSOR_NOT_REQUIRED); |
7032 | |
|
7033 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, 0); |
7034 | 0 | } |
7035 | 0 | } |
7036 | 0 | } break; |
7037 | 0 | case LLM_ARCH_COGVLM: |
7038 | 0 | { |
7039 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
7040 | | |
7041 | | // output |
7042 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
7043 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
7044 | | |
7045 | | // if output is NULL, init from the input tok embed |
7046 | 0 | if (output == NULL) { |
7047 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
7048 | 0 | } |
7049 | |
|
7050 | 0 | for (int i = 0; i < n_layer; ++i) { |
7051 | 0 | auto & layer = layers[i]; |
7052 | |
|
7053 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
7054 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), {n_embd, n_embd_head_k * n_head * 3}, 0); |
7055 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
7056 | |
|
7057 | 0 | layer.visexp_attn_wqkv = create_tensor(tn(LLM_TENSOR_VISEXP_ATTN_QKV, "weight", i), {n_embd, n_embd_head_k * n_head * 3}, 0); |
7058 | 0 | layer.visexp_attn_wo = create_tensor(tn(LLM_TENSOR_VISEXP_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
7059 | |
|
7060 | 0 | layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
7061 | |
|
7062 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
7063 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
7064 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
7065 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
7066 | |
|
7067 | 0 | layer.visexp_ffn_gate = create_tensor(tn(LLM_TENSOR_VISEXP_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
7068 | 0 | layer.visexp_ffn_down = create_tensor(tn(LLM_TENSOR_VISEXP_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
7069 | 0 | layer.visexp_ffn_up = create_tensor(tn(LLM_TENSOR_VISEXP_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
7070 | 0 | } |
7071 | 0 | } break; |
7072 | 0 | case LLM_ARCH_PANGU_EMBED: |
7073 | 0 | { |
7074 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
7075 | | |
7076 | | // output |
7077 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
7078 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
7079 | | |
7080 | | // if output is NULL, init from the input tok embed |
7081 | 0 | if (output == NULL) { |
7082 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
7083 | 0 | } |
7084 | |
|
7085 | 0 | for (int i = 0; i < n_layer; ++i) { |
7086 | 0 | auto & layer = layers[i]; |
7087 | |
|
7088 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
7089 | | |
7090 | | // weight tensors |
7091 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
7092 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
7093 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
7094 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
7095 | | |
7096 | | // bias tensors |
7097 | 0 | layer.bq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "bias", i), {n_embd_head_k * n_head}, 0); |
7098 | 0 | layer.bk = create_tensor(tn(LLM_TENSOR_ATTN_K, "bias", i), {n_embd_gqa}, 0); |
7099 | 0 | layer.bv = create_tensor(tn(LLM_TENSOR_ATTN_V, "bias", i), {n_embd_gqa}, 0); |
7100 | 0 | layer.bo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "bias", i), {n_embd}, 0); |
7101 | |
|
7102 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
7103 | |
|
7104 | 0 | if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) { |
7105 | 0 | layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
7106 | 0 | layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
7107 | 0 | } else { |
7108 | 0 | layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
7109 | 0 | } |
7110 | |
|
7111 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
7112 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
7113 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
7114 | 0 | } |
7115 | 0 | } break; |
7116 | 0 | case LLM_ARCH_QWEN3NEXT: |
7117 | 0 | { |
7118 | 0 | if (n_expert == 0) { |
7119 | 0 | throw std::runtime_error(arch_name() + " model cannot have zero experts"); |
7120 | 0 | } |
7121 | | |
7122 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0); |
7123 | | |
7124 | | // output |
7125 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0); |
7126 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED); |
7127 | | |
7128 | | // if output is NULL, init from the input tok embed |
7129 | 0 | if (output == NULL) { |
7130 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED); |
7131 | 0 | } |
7132 | |
|
7133 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used; |
7134 | | |
7135 | | // Calculate dimensions from hyperparameters |
7136 | 0 | const int64_t head_k_dim = hparams.ssm_d_state; |
7137 | 0 | const int64_t head_v_dim = hparams.ssm_d_state; |
7138 | 0 | const int64_t n_k_heads = hparams.ssm_n_group; |
7139 | 0 | const int64_t n_v_heads = hparams.ssm_dt_rank; |
7140 | 0 | const int64_t key_dim = head_k_dim * n_k_heads; |
7141 | 0 | const int64_t value_dim = head_v_dim * n_v_heads; |
7142 | 0 | const int64_t conv_dim = key_dim * 2 + value_dim; |
7143 | | |
7144 | | // Calculate projection sizes |
7145 | 0 | const int64_t qkvz_dim = key_dim * 2 + value_dim * 2; |
7146 | 0 | const int64_t ba_dim = n_v_heads * 2; |
7147 | |
|
7148 | 0 | for (int i = 0; i < n_layer; ++i) { |
7149 | 0 | auto & layer = layers[i]; |
7150 | 0 | const uint32_t n_ff_shexp = hparams.n_ff_shexp > 0 ? hparams.n_ff_shexp : hparams.n_ff(i); |
7151 | |
|
7152 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0); |
7153 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), { n_embd }, 0); |
7154 | |
|
7155 | 0 | if (!hparams.is_recurrent(i)) { |
7156 | | // Attention layers |
7157 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head * 2 }, 0); |
7158 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_k_gqa }, 0); |
7159 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_v_gqa }, 0); |
7160 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0); |
7161 | | |
7162 | | // Q/K normalization for attention layers |
7163 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), { n_embd_head_k }, 0); |
7164 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), { n_embd_head_k }, 0); |
7165 | 0 | } else { |
7166 | | // Linear attention (gated delta net) specific tensors |
7167 | | // Create tensors with calculated dimensions |
7168 | | // note: ssm_in is used by legacy GGUF |
7169 | 0 | layer.ssm_in = create_tensor(tn(LLM_TENSOR_SSM_IN, "weight", i), { n_embd, qkvz_dim }, TENSOR_NOT_REQUIRED); |
7170 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), { n_embd, key_dim * 2 + value_dim }, TENSOR_NOT_REQUIRED); |
7171 | 0 | layer.wqkv_gate = create_tensor(tn(LLM_TENSOR_ATTN_GATE, "weight", i), { n_embd, value_dim }, TENSOR_NOT_REQUIRED); |
7172 | 0 | layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), { hparams.ssm_d_conv, conv_dim }, 0); |
7173 | 0 | layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), { hparams.ssm_dt_rank }, 0); |
7174 | 0 | layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A_NOSCAN, i), { hparams.ssm_dt_rank }, 0); |
7175 | 0 | layer.ssm_beta_alpha = create_tensor(tn(LLM_TENSOR_SSM_BETA_ALPHA, "weight", i), { n_embd, ba_dim }, 0); |
7176 | 0 | layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), { head_v_dim }, 0); |
7177 | 0 | layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), { value_dim, n_embd }, 0); |
7178 | 0 | } |
7179 | |
|
7180 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, 0); |
7181 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, 0); |
7182 | 0 | create_tensor_gate_up_exps(layer, i, n_embd, n_ff_exp, n_expert, 0); |
7183 | | |
7184 | | // Shared experts |
7185 | 0 | layer.ffn_gate_inp_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP_SHEXP, "weight", i), { n_embd }, 0); |
7186 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp }, 0); |
7187 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp }, 0); |
7188 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_shexp, n_embd }, 0); |
7189 | 0 | } |
7190 | 0 | } break; |
7191 | 0 | case LLM_ARCH_QWEN35MOE: |
7192 | 0 | { |
7193 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0); |
7194 | | |
7195 | | // output |
7196 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0); |
7197 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED); |
7198 | | |
7199 | | // if output is NULL, init from the input tok embed |
7200 | 0 | if (output == NULL) { |
7201 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED); |
7202 | 0 | } |
7203 | |
|
7204 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp ? hparams.n_ff_exp : n_ff / n_expert_used; |
7205 | | |
7206 | | // Calculate dimensions from hyperparameters |
7207 | 0 | const int64_t head_k_dim = hparams.ssm_d_state; |
7208 | 0 | const int64_t head_v_dim = hparams.ssm_d_state; |
7209 | 0 | const int64_t n_k_heads = hparams.ssm_n_group; |
7210 | 0 | const int64_t n_v_heads = hparams.ssm_dt_rank; |
7211 | 0 | const int64_t key_dim = head_k_dim * n_k_heads; |
7212 | 0 | const int64_t value_dim = head_v_dim * n_v_heads; |
7213 | 0 | const int64_t conv_dim = key_dim * 2 + value_dim; |
7214 | |
|
7215 | 0 | for (int i = 0; i < n_layer; ++i) { |
7216 | 0 | auto & layer = layers[i]; |
7217 | |
|
7218 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0); |
7219 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), { n_embd }, 0); |
7220 | |
|
7221 | 0 | if (!hparams.is_recurrent(i)) { |
7222 | | // Attention layers |
7223 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head * 2 }, 0); |
7224 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_k_gqa }, 0); |
7225 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_v_gqa }, 0); |
7226 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0); |
7227 | | |
7228 | | // Q/K normalization for attention layers |
7229 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), { n_embd_head_k }, 0); |
7230 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), { n_embd_head_k }, 0); |
7231 | 0 | } else { |
7232 | | // Linear attention (gated delta net) specific tensors |
7233 | | // Create tensors with calculated dimensions |
7234 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), { n_embd, key_dim * 2 + value_dim }, TENSOR_NOT_REQUIRED); |
7235 | 0 | layer.wqkv_gate = create_tensor(tn(LLM_TENSOR_ATTN_GATE, "weight", i), { n_embd, value_dim }, TENSOR_NOT_REQUIRED); |
7236 | 0 | layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), { hparams.ssm_d_conv, conv_dim }, 0); |
7237 | 0 | layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), { hparams.ssm_dt_rank }, 0); |
7238 | 0 | layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A_NOSCAN, i), { hparams.ssm_dt_rank }, 0); |
7239 | 0 | layer.ssm_beta = create_tensor(tn(LLM_TENSOR_SSM_BETA, "weight", i), { n_embd, n_v_heads }, 0); |
7240 | 0 | layer.ssm_alpha = create_tensor(tn(LLM_TENSOR_SSM_ALPHA, "weight", i), { n_embd, n_v_heads }, 0); |
7241 | 0 | layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), { head_v_dim }, 0); |
7242 | 0 | layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), { value_dim, n_embd }, 0); |
7243 | 0 | } |
7244 | |
|
7245 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), { n_embd, n_expert }, 0); |
7246 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), { n_ff_exp, n_embd, n_expert }, 0); |
7247 | 0 | create_tensor_gate_up_exps(layer, i, n_embd, n_ff_exp, n_expert, 0); |
7248 | | |
7249 | | // Shared experts |
7250 | 0 | const int64_t n_ff_shexp = hparams.n_ff_shexp ? hparams.n_ff_shexp : n_ff; |
7251 | |
|
7252 | 0 | layer.ffn_gate_inp_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP_SHEXP, "weight", i), { n_embd }, 0); |
7253 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), { n_embd, n_ff_shexp }, 0); |
7254 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), { n_embd, n_ff_shexp }, 0); |
7255 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), { n_ff_shexp, n_embd }, 0); |
7256 | 0 | } |
7257 | 0 | } break; |
7258 | 0 | case LLM_ARCH_QWEN35: |
7259 | 0 | { |
7260 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, 0); |
7261 | | |
7262 | | // output |
7263 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), { n_embd }, 0); |
7264 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), { n_embd, n_vocab }, TENSOR_NOT_REQUIRED); |
7265 | | |
7266 | | // if output is NULL, init from the input tok embed |
7267 | 0 | if (output == NULL) { |
7268 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), { n_embd, n_vocab }, TENSOR_DUPLICATED); |
7269 | 0 | } |
7270 | | |
7271 | | // Calculate dimensions from hyperparameters |
7272 | 0 | const int64_t head_k_dim = hparams.ssm_d_state; |
7273 | 0 | const int64_t head_v_dim = hparams.ssm_d_state; |
7274 | 0 | const int64_t n_k_heads = hparams.ssm_n_group; |
7275 | 0 | const int64_t n_v_heads = hparams.ssm_dt_rank; |
7276 | 0 | const int64_t key_dim = head_k_dim * n_k_heads; |
7277 | 0 | const int64_t value_dim = head_v_dim * n_v_heads; |
7278 | 0 | const int64_t conv_dim = key_dim * 2 + value_dim; |
7279 | |
|
7280 | 0 | for (int i = 0; i < n_layer; ++i) { |
7281 | 0 | auto & layer = layers[i]; |
7282 | |
|
7283 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), { n_embd }, 0); |
7284 | 0 | layer.attn_post_norm = create_tensor(tn(LLM_TENSOR_ATTN_POST_NORM, "weight", i), { n_embd }, 0); |
7285 | |
|
7286 | 0 | if (!hparams.is_recurrent(i)) { |
7287 | | // Attention layers |
7288 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head * 2 }, 0); |
7289 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_k_gqa }, 0); |
7290 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_v_gqa }, 0); |
7291 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_k * n_head, n_embd }, 0); |
7292 | | |
7293 | | // Q/K normalization for attention layers |
7294 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), { n_embd_head_k }, 0); |
7295 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), { n_embd_head_k }, 0); |
7296 | 0 | } else { |
7297 | | // Linear attention (gated delta net) specific tensors |
7298 | | // Create tensors with calculated dimensions |
7299 | 0 | layer.wqkv = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "weight", i), { n_embd, key_dim * 2 + value_dim }, TENSOR_NOT_REQUIRED); |
7300 | 0 | layer.wqkv_gate = create_tensor(tn(LLM_TENSOR_ATTN_GATE, "weight", i), { n_embd, value_dim }, TENSOR_NOT_REQUIRED); |
7301 | 0 | layer.ssm_conv1d = create_tensor(tn(LLM_TENSOR_SSM_CONV1D, "weight", i), { hparams.ssm_d_conv, conv_dim }, 0); |
7302 | 0 | layer.ssm_dt = create_tensor(tn(LLM_TENSOR_SSM_DT, "bias", i), { hparams.ssm_dt_rank }, 0); |
7303 | 0 | layer.ssm_a = create_tensor(tn(LLM_TENSOR_SSM_A_NOSCAN, i), { hparams.ssm_dt_rank }, 0); |
7304 | 0 | layer.ssm_beta = create_tensor(tn(LLM_TENSOR_SSM_BETA, "weight", i), { n_embd, n_v_heads }, 0); |
7305 | 0 | layer.ssm_alpha = create_tensor(tn(LLM_TENSOR_SSM_ALPHA, "weight", i), { n_embd, n_v_heads }, 0); |
7306 | 0 | layer.ssm_norm = create_tensor(tn(LLM_TENSOR_SSM_NORM, "weight", i), { head_v_dim }, 0); |
7307 | 0 | layer.ssm_out = create_tensor(tn(LLM_TENSOR_SSM_OUT, "weight", i), { value_dim, n_embd }, 0); |
7308 | 0 | } |
7309 | |
|
7310 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
7311 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
7312 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
7313 | 0 | } |
7314 | 0 | } break; |
7315 | 0 | case LLM_ARCH_MIMO2: |
7316 | 0 | { |
7317 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
7318 | | |
7319 | | // output |
7320 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
7321 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
7322 | |
|
7323 | 0 | for (int i = 0; i < n_layer; ++i) { |
7324 | 0 | auto & layer = layers[i]; |
7325 | 0 | uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(i); |
7326 | 0 | uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(i); |
7327 | 0 | uint32_t n_head = hparams.n_head(i); |
7328 | |
|
7329 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), { n_embd, n_embd_head_k * n_head }, 0); |
7330 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), { n_embd, n_embd_k_gqa }, 0); |
7331 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), { n_embd, n_embd_v_gqa }, 0); |
7332 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), { n_embd_head_v * n_head, n_embd }, 0); |
7333 | |
|
7334 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
7335 | 0 | layer.attn_sinks = create_tensor(tn(LLM_TENSOR_ATTN_SINKS, "weight", i), {n_head}, TENSOR_NOT_REQUIRED); |
7336 | |
|
7337 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
7338 | | |
7339 | | // non-MoE branch |
7340 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED); |
7341 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, TENSOR_NOT_REQUIRED); |
7342 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED); |
7343 | | |
7344 | | // MoE branch |
7345 | 0 | int64_t n_ff_exp = hparams.n_ff_exp; |
7346 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, TENSOR_NOT_REQUIRED); |
7347 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED); |
7348 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, TENSOR_NOT_REQUIRED); |
7349 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED); |
7350 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED); |
7351 | 0 | } |
7352 | 0 | } break; |
7353 | 0 | case LLM_ARCH_STEP35: |
7354 | 0 | { |
7355 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
7356 | | |
7357 | | // output |
7358 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
7359 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, 0); |
7360 | | |
7361 | | // STEP35 supports per-layer partial RoPE dims; rope factors are stored as a single shared tensor |
7362 | | // ("rope_freqs.weight") and ggml uses only the first (n_rot_l/2) entries per layer. |
7363 | 0 | uint32_t n_rot_max = 0; |
7364 | 0 | for (int i = 0; i < n_layer; ++i) { |
7365 | 0 | n_rot_max = std::max(n_rot_max, hparams.n_rot(i)); |
7366 | 0 | } |
7367 | 0 | if (n_rot_max == 0) { |
7368 | 0 | n_rot_max = n_rot; |
7369 | 0 | } |
7370 | |
|
7371 | 0 | for (int i = 0; i < n_layer; ++i) { |
7372 | 0 | auto & layer = layers[i]; |
7373 | |
|
7374 | 0 | const uint32_t n_head_l = hparams.n_head(i); |
7375 | 0 | const uint32_t n_embd_k_gqa = hparams.n_embd_k_gqa(i); |
7376 | 0 | const uint32_t n_embd_v_gqa = hparams.n_embd_v_gqa(i); |
7377 | |
|
7378 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
7379 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, TENSOR_NOT_REQUIRED); |
7380 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, TENSOR_NOT_REQUIRED); |
7381 | | |
7382 | | // optional rope factors (llama3) / longrope tensors |
7383 | 0 | if (hparams.rope_scaling_type_train == LLAMA_ROPE_SCALING_TYPE_LONGROPE) { |
7384 | 0 | layer.rope_long = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_LONG, "weight", i), {n_rot_max/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
7385 | 0 | layer.rope_short = create_tensor(tn(LLM_TENSOR_ROPE_FACTORS_SHORT, "weight", i), {n_rot_max/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
7386 | 0 | } else { |
7387 | 0 | layer.rope_freqs = create_tensor(tn(LLM_TENSOR_ROPE_FREQS, "weight", i), {n_rot_max/2}, TENSOR_NOT_REQUIRED | (i != 0 ? TENSOR_DUPLICATED : 0)); |
7388 | 0 | } |
7389 | |
|
7390 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head_l}, 0); |
7391 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_k_gqa}, 0); |
7392 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_v_gqa}, 0); |
7393 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_v * n_head_l, n_embd}, 0); |
7394 | | |
7395 | | // head-wise attention gate (Step35 self_attn.g_proj) |
7396 | 0 | layer.wqkv_gate = create_tensor(tn(LLM_TENSOR_ATTN_GATE, "weight", i), {n_embd, n_head_l}, TENSOR_NOT_REQUIRED); |
7397 | |
|
7398 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
7399 | | |
7400 | | // dense MLP (leading dense blocks) |
7401 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED); |
7402 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, TENSOR_NOT_REQUIRED); |
7403 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, TENSOR_NOT_REQUIRED); |
7404 | | |
7405 | | // MoE routed experts + selection bias (router_bias) |
7406 | 0 | const int64_t n_ff_exp = hparams.n_ff_exp; |
7407 | 0 | layer.ffn_gate_inp = create_tensor(tn(LLM_TENSOR_FFN_GATE_INP, "weight", i), {n_embd, n_expert}, TENSOR_NOT_REQUIRED); |
7408 | 0 | layer.ffn_gate_exps = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED); |
7409 | 0 | layer.ffn_down_exps = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "weight", i), {n_ff_exp, n_embd, n_expert}, TENSOR_NOT_REQUIRED); |
7410 | 0 | layer.ffn_up_exps = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "weight", i), {n_embd, n_ff_exp, n_expert}, TENSOR_NOT_REQUIRED); |
7411 | 0 | layer.ffn_exp_probs_b = create_tensor(tn(LLM_TENSOR_FFN_EXP_PROBS_B, "bias", i), {n_expert}, TENSOR_NOT_REQUIRED); |
7412 | | |
7413 | | // shared expert MLP |
7414 | 0 | layer.ffn_gate_shexp = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, TENSOR_NOT_REQUIRED); |
7415 | 0 | layer.ffn_up_shexp = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "weight", i), {n_embd, hparams.n_ff_shexp}, TENSOR_NOT_REQUIRED); |
7416 | 0 | layer.ffn_down_shexp = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "weight", i), {hparams.n_ff_shexp, n_embd}, TENSOR_NOT_REQUIRED); |
7417 | 0 | } |
7418 | 0 | } break; |
7419 | 0 | case LLM_ARCH_MAINCODER: |
7420 | 0 | { |
7421 | 0 | tok_embd = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, 0); |
7422 | | |
7423 | | // output |
7424 | 0 | output_norm = create_tensor(tn(LLM_TENSOR_OUTPUT_NORM, "weight"), {n_embd}, 0); |
7425 | 0 | output = create_tensor(tn(LLM_TENSOR_OUTPUT, "weight"), {n_embd, n_vocab}, TENSOR_NOT_REQUIRED); |
7426 | | // if output is NULL, init from the input tok embed |
7427 | 0 | if (output == NULL) { |
7428 | 0 | output = create_tensor(tn(LLM_TENSOR_TOKEN_EMBD, "weight"), {n_embd, n_vocab}, TENSOR_DUPLICATED); |
7429 | 0 | } |
7430 | |
|
7431 | 0 | for (int i = 0; i < n_layer; ++i) { |
7432 | 0 | auto & layer = layers[i]; |
7433 | |
|
7434 | 0 | layer.attn_norm = create_tensor(tn(LLM_TENSOR_ATTN_NORM, "weight", i), {n_embd}, 0); |
7435 | |
|
7436 | 0 | layer.wq = create_tensor(tn(LLM_TENSOR_ATTN_Q, "weight", i), {n_embd, n_embd_head_k * n_head}, 0); |
7437 | 0 | layer.wk = create_tensor(tn(LLM_TENSOR_ATTN_K, "weight", i), {n_embd, n_embd_gqa}, 0); |
7438 | 0 | layer.wv = create_tensor(tn(LLM_TENSOR_ATTN_V, "weight", i), {n_embd, n_embd_gqa}, 0); |
7439 | 0 | layer.wo = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "weight", i), {n_embd_head_k * n_head, n_embd}, 0); |
7440 | |
|
7441 | 0 | layer.attn_k_norm = create_tensor(tn(LLM_TENSOR_ATTN_K_NORM, "weight", i), {n_embd_head_k}, 0); |
7442 | 0 | layer.attn_q_norm = create_tensor(tn(LLM_TENSOR_ATTN_Q_NORM, "weight", i), {n_embd_head_k}, 0); |
7443 | |
|
7444 | 0 | layer.ffn_norm = create_tensor(tn(LLM_TENSOR_FFN_NORM, "weight", i), {n_embd}, 0); |
7445 | 0 | layer.ffn_gate = create_tensor(tn(LLM_TENSOR_FFN_GATE, "weight", i), {n_embd, n_ff}, 0); |
7446 | 0 | layer.ffn_down = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "weight", i), { n_ff, n_embd}, 0); |
7447 | 0 | layer.ffn_up = create_tensor(tn(LLM_TENSOR_FFN_UP, "weight", i), {n_embd, n_ff}, 0); |
7448 | 0 | } |
7449 | 0 | } break; |
7450 | 0 | default: |
7451 | 0 | throw std::runtime_error("unknown architecture"); |
7452 | 0 | } |
7453 | | |
7454 | | // generic pass: load optional per-tensor/per-expert ".scale" tensors (e.g. NVFP4 scale2) |
7455 | | // this avoids having to add scale loading to every architecture |
7456 | 0 | for (int i = 0; i < n_layer; ++i) { |
7457 | 0 | auto & layer = layers[i]; |
7458 | | |
7459 | | // attention weight scales (per-tensor, shape {1}) |
7460 | 0 | if (!layer.wq_s && layer.wq) { |
7461 | 0 | layer.wq_s = create_tensor(tn(LLM_TENSOR_ATTN_Q, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
7462 | 0 | } |
7463 | 0 | if (!layer.wk_s && layer.wk) { |
7464 | 0 | layer.wk_s = create_tensor(tn(LLM_TENSOR_ATTN_K, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
7465 | 0 | } |
7466 | 0 | if (!layer.wv_s && layer.wv) { |
7467 | 0 | layer.wv_s = create_tensor(tn(LLM_TENSOR_ATTN_V, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
7468 | 0 | } |
7469 | 0 | if (!layer.wo_s && layer.wo) { |
7470 | 0 | layer.wo_s = create_tensor(tn(LLM_TENSOR_ATTN_OUT, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
7471 | 0 | } |
7472 | 0 | if (!layer.wqkv_s && layer.wqkv) { |
7473 | 0 | layer.wqkv_s = create_tensor(tn(LLM_TENSOR_ATTN_QKV, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
7474 | 0 | } |
7475 | 0 | if (!layer.wqkv_gate_s && layer.wqkv_gate) { |
7476 | 0 | layer.wqkv_gate_s = create_tensor(tn(LLM_TENSOR_ATTN_GATE, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
7477 | 0 | } |
7478 | | |
7479 | | // dense FFN weight scales (per-tensor, shape {1}) |
7480 | 0 | if (!layer.ffn_gate_s && layer.ffn_gate) { |
7481 | 0 | layer.ffn_gate_s = create_tensor(tn(LLM_TENSOR_FFN_GATE, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
7482 | 0 | } |
7483 | 0 | if (!layer.ffn_down_s && layer.ffn_down) { |
7484 | 0 | layer.ffn_down_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
7485 | 0 | } |
7486 | 0 | if (!layer.ffn_up_s && layer.ffn_up) { |
7487 | 0 | layer.ffn_up_s = create_tensor(tn(LLM_TENSOR_FFN_UP, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
7488 | 0 | } |
7489 | 0 | if (!layer.ffn_gate_shexp_s && layer.ffn_gate_shexp) { |
7490 | 0 | layer.ffn_gate_shexp_s = create_tensor(tn(LLM_TENSOR_FFN_GATE_SHEXP, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
7491 | 0 | } |
7492 | 0 | if (!layer.ffn_down_shexp_s && layer.ffn_down_shexp) { |
7493 | 0 | layer.ffn_down_shexp_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN_SHEXP, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
7494 | 0 | } |
7495 | 0 | if (!layer.ffn_up_shexp_s && layer.ffn_up_shexp) { |
7496 | 0 | layer.ffn_up_shexp_s = create_tensor(tn(LLM_TENSOR_FFN_UP_SHEXP, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
7497 | 0 | } |
7498 | | |
7499 | | // MoE expert weight scales (per-expert, shape {n_expert}) |
7500 | 0 | if (!layer.ffn_gate_exps_s && layer.ffn_gate_exps) { |
7501 | 0 | layer.ffn_gate_exps_s = create_tensor(tn(LLM_TENSOR_FFN_GATE_EXPS, "scale", i), {n_expert}, TENSOR_NOT_REQUIRED); |
7502 | 0 | } |
7503 | 0 | if (!layer.ffn_down_exps_s && layer.ffn_down_exps) { |
7504 | 0 | layer.ffn_down_exps_s = create_tensor(tn(LLM_TENSOR_FFN_DOWN_EXPS, "scale", i), {n_expert}, TENSOR_NOT_REQUIRED); |
7505 | 0 | } |
7506 | 0 | if (!layer.ffn_up_exps_s && layer.ffn_up_exps) { |
7507 | 0 | layer.ffn_up_exps_s = create_tensor(tn(LLM_TENSOR_FFN_UP_EXPS, "scale", i), {n_expert}, TENSOR_NOT_REQUIRED); |
7508 | 0 | } |
7509 | | |
7510 | | // recurrent / linear-attention weight scales (per-tensor, shape {1}) |
7511 | 0 | if (!layer.ssm_in_s && layer.ssm_in) { |
7512 | 0 | layer.ssm_in_s = create_tensor(tn(LLM_TENSOR_SSM_IN, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
7513 | 0 | } |
7514 | 0 | if (!layer.ssm_out_s && layer.ssm_out) { |
7515 | 0 | layer.ssm_out_s = create_tensor(tn(LLM_TENSOR_SSM_OUT, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
7516 | 0 | } |
7517 | 0 | if (!layer.ssm_alpha_s && layer.ssm_alpha) { |
7518 | 0 | layer.ssm_alpha_s = create_tensor(tn(LLM_TENSOR_SSM_ALPHA, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
7519 | 0 | } |
7520 | 0 | if (!layer.ssm_beta_s && layer.ssm_beta) { |
7521 | 0 | layer.ssm_beta_s = create_tensor(tn(LLM_TENSOR_SSM_BETA, "scale", i), {1}, TENSOR_NOT_REQUIRED); |
7522 | 0 | } |
7523 | 0 | } |
7524 | 0 | } |
7525 | | |
7526 | 0 | ml.done_getting_tensors(); |
7527 | |
|
7528 | 0 | ml.init_mappings(true, use_mlock ? &pimpl->mlock_mmaps : nullptr); |
7529 | 0 | pimpl->mappings.reserve(ml.mappings.size()); |
7530 | | |
7531 | | // create the backend buffers |
7532 | 0 | std::vector<std::pair<ggml_context *, llama_buf_map>> ctx_buf_maps; |
7533 | 0 | ctx_buf_maps.reserve(ml.ctx_map.size()); |
7534 | | |
7535 | | // Ensure we have enough capacity for the maximum backend buffer we will potentially create |
7536 | 0 | const size_t n_max_backend_buffer = ml.ctx_map.size() * ml.files.size(); |
7537 | 0 | pimpl->ctxs_bufs.reserve(n_max_backend_buffer); |
7538 | |
|
7539 | 0 | for (auto & [buft, ctx_ptr] : ml.ctx_map) { |
7540 | 0 | ggml_context * ctx = ctx_ptr.get(); |
7541 | | |
7542 | | // skip contexts without tensors |
7543 | 0 | if (ggml_get_first_tensor(ctx) == nullptr) { |
7544 | 0 | continue; |
7545 | 0 | } |
7546 | | |
7547 | 0 | llama_buf_map buf_map; |
7548 | 0 | buf_map.reserve(n_max_backend_buffer); |
7549 | | |
7550 | | // check if it is possible to use buffer_from_host_ptr with this buffer type |
7551 | 0 | ggml_backend_dev_t dev = ggml_backend_buft_get_device(buft); |
7552 | 0 | if (!dev) { |
7553 | | // FIXME: workaround for CPU backend buft having a NULL device |
7554 | 0 | dev = ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU); |
7555 | 0 | if (!dev) { |
7556 | 0 | throw std::runtime_error(format("%s: no CPU backend found", __func__)); |
7557 | 0 | } |
7558 | 0 | } |
7559 | 0 | ggml_backend_dev_props props; |
7560 | 0 | ggml_backend_dev_get_props(dev, &props); |
7561 | 0 | bool buffer_from_host_ptr_supported = props.caps.buffer_from_host_ptr; |
7562 | 0 | bool is_default_buft = buft == ggml_backend_dev_buffer_type(dev); |
7563 | |
|
7564 | 0 | std::vector<ggml_backend_buffer_ptr> bufs; |
7565 | 0 | if (ml.use_mmap && use_mmap_buffer && buffer_from_host_ptr_supported && is_default_buft) { |
7566 | 0 | GGML_ASSERT(!ml.no_alloc); |
7567 | 0 | for (uint32_t idx = 0; idx < ml.files.size(); idx++) { |
7568 | | // only the mmap region containing the tensors in the model is mapped to the backend buffer |
7569 | | // this is important for metal with apple silicon: if the entire model could be mapped to a metal buffer, |
7570 | | // then we could just use metal for all layers |
7571 | | // this allows using partial offloading when the model size exceeds the metal buffer size, but not the RAM size |
7572 | 0 | void * addr = nullptr; |
7573 | 0 | size_t first, last; // NOLINT |
7574 | 0 | ml.get_mapping_range(&first, &last, &addr, idx, ctx); |
7575 | 0 | if (first >= last) { |
7576 | 0 | continue; |
7577 | 0 | } |
7578 | 0 | const size_t max_size = ggml_get_max_tensor_size(ctx); |
7579 | 0 | ggml_backend_buffer_t buf = ggml_backend_dev_buffer_from_host_ptr(dev, (char *) addr + first, last - first, max_size); |
7580 | 0 | if (buf == nullptr) { |
7581 | 0 | throw std::runtime_error(format("unable to allocate %s buffer", ggml_backend_buft_name(buft))); |
7582 | 0 | } |
7583 | 0 | bufs.emplace_back(buf); |
7584 | 0 | buf_map.emplace(idx, buf); |
7585 | 0 | } |
7586 | 0 | } else { |
7587 | 0 | ggml_backend_buffer_t buf; |
7588 | 0 | if (ml.no_alloc) { |
7589 | 0 | buf = ggml_backend_buft_alloc_buffer(buft, /*size =*/ 0); // dummy buffer |
7590 | 0 | for (ggml_tensor * t = ggml_get_first_tensor(ctx); t != nullptr; t = ggml_get_next_tensor(ctx, t)) { |
7591 | 0 | t->buffer = buf; // set dummy buffer for weights so that the backend scheduler won't try to allocate them |
7592 | 0 | } |
7593 | 0 | } else { |
7594 | 0 | buf = ggml_backend_alloc_ctx_tensors_from_buft(ctx, buft); // real buffer |
7595 | 0 | } |
7596 | 0 | if (buf == nullptr) { |
7597 | 0 | throw std::runtime_error(format("unable to allocate %s buffer", ggml_backend_buft_name(buft))); |
7598 | 0 | } |
7599 | 0 | if (use_mlock && ggml_backend_buffer_is_host(buf)) { |
7600 | 0 | pimpl->mlock_bufs.emplace_back(new llama_mlock); |
7601 | 0 | auto & mlock_buf = pimpl->mlock_bufs.back(); |
7602 | 0 | mlock_buf->init (ggml_backend_buffer_get_base(buf)); |
7603 | 0 | mlock_buf->grow_to(ggml_backend_buffer_get_size(buf)); |
7604 | 0 | } |
7605 | 0 | bufs.emplace_back(buf); |
7606 | 0 | for (uint32_t idx = 0; idx < ml.files.size(); idx++) { |
7607 | 0 | buf_map.emplace(idx, buf); |
7608 | 0 | } |
7609 | 0 | } |
7610 | 0 | pimpl->ctxs_bufs.emplace_back(std::move(ctx_ptr), std::move(bufs)); |
7611 | |
|
7612 | 0 | for (auto & buf : buf_map) { |
7613 | | // indicate that this buffer contains weights |
7614 | | // this is used by ggml_backend_sched to improve op scheduling: ops that use a weight are preferably scheduled to the backend that contains the weight |
7615 | 0 | ggml_backend_buffer_set_usage(buf.second, GGML_BACKEND_BUFFER_USAGE_WEIGHTS); |
7616 | 0 | } |
7617 | |
|
7618 | 0 | ctx_buf_maps.emplace_back(ctx, buf_map); |
7619 | 0 | } |
7620 | | |
7621 | 0 | if (llama_supports_gpu_offload()) { |
7622 | 0 | const int n_gpu = std::min(n_gpu_layers, int(hparams.n_layer)); |
7623 | |
|
7624 | 0 | int n_repeating = n_gpu; |
7625 | 0 | if (n_repeating > 0) { |
7626 | 0 | LLAMA_LOG_INFO("%s: offloading output layer to GPU\n", __func__); |
7627 | 0 | n_repeating--; |
7628 | 0 | } |
7629 | 0 | LLAMA_LOG_INFO("%s: offloading %d repeating layers to GPU\n", __func__, n_repeating); |
7630 | |
|
7631 | 0 | const int max_backend_supported_layers = hparams.n_layer + 1; |
7632 | 0 | const int max_offloadable_layers = hparams.n_layer + 1; |
7633 | |
|
7634 | 0 | LLAMA_LOG_INFO("%s: offloaded %d/%d layers to GPU\n", __func__, std::min(n_gpu_layers, max_offloadable_layers), max_backend_supported_layers); |
7635 | 0 | } |
7636 | | |
7637 | | // print memory requirements per buffer type |
7638 | 0 | for (auto & [_, bufs] : pimpl->ctxs_bufs) { |
7639 | 0 | for (auto & buf: bufs) { |
7640 | 0 | LLAMA_LOG_INFO("%s: %12s model buffer size = %8.2f MiB\n", |
7641 | 0 | __func__, ggml_backend_buffer_name(buf.get()), ggml_backend_buffer_get_size(buf.get()) / 1024.0 / 1024.0); |
7642 | 0 | } |
7643 | 0 | } |
7644 | | |
7645 | | // populate tensors_by_name |
7646 | 0 | for (auto & [ctx, _] : pimpl->ctxs_bufs) { |
7647 | 0 | for (auto * cur = ggml_get_first_tensor(ctx.get()); cur != NULL; cur = ggml_get_next_tensor(ctx.get(), cur)) { |
7648 | 0 | tensors_by_name.emplace_back(ggml_get_name(cur), cur); |
7649 | 0 | } |
7650 | 0 | } |
7651 | |
|
7652 | 0 | if (ml.no_alloc) { |
7653 | 0 | return true; |
7654 | 0 | } |
7655 | | |
7656 | | // load tensor data |
7657 | 0 | for (auto & [ctx, buf_map] : ctx_buf_maps) { |
7658 | 0 | if (!ml.load_all_data(ctx, buf_map, use_mlock ? &pimpl->mlock_mmaps : NULL, params.progress_callback, params.progress_callback_user_data)) { |
7659 | 0 | return false; |
7660 | 0 | } |
7661 | 0 | } |
7662 | | |
7663 | 0 | if (use_mmap_buffer) { |
7664 | 0 | for (auto & mapping : ml.mappings) { |
7665 | 0 | pimpl->mappings.emplace_back(std::move(mapping)); |
7666 | 0 | } |
7667 | 0 | } |
7668 | |
|
7669 | 0 | return true; |
7670 | 0 | } |
7671 | | |
7672 | 0 | std::string llama_model::arch_name() const { |
7673 | 0 | return llm_arch_name(arch); |
7674 | 0 | } |
7675 | | |
7676 | 0 | std::string llama_model::type_name() const { |
7677 | 0 | return llm_type_name(type); |
7678 | 0 | } |
7679 | | |
7680 | 0 | std::string llama_model::desc() const { |
7681 | 0 | return pimpl->desc_str; |
7682 | 0 | } |
7683 | | |
7684 | 0 | size_t llama_model::size() const { |
7685 | 0 | return pimpl->n_bytes; |
7686 | 0 | } |
7687 | | |
7688 | 0 | size_t llama_model::n_tensors() const { |
7689 | 0 | return tensors_by_name.size(); |
7690 | 0 | } |
7691 | | |
7692 | 0 | size_t llama_model::n_devices() const { |
7693 | 0 | return devices.size(); |
7694 | 0 | } |
7695 | | |
7696 | 0 | uint32_t llama_model::n_gpu_layers() const { |
7697 | 0 | return params.n_gpu_layers >= 0 ? params.n_gpu_layers : hparams.n_layer + 1; |
7698 | 0 | } |
7699 | | |
7700 | 0 | llama_split_mode llama_model::split_mode() const { |
7701 | 0 | return params.split_mode; |
7702 | 0 | } |
7703 | | |
7704 | 0 | std::map<ggml_backend_buffer_type_t, size_t> llama_model::memory_breakdown() const { |
7705 | 0 | std::map<ggml_backend_buffer_type_t, size_t> ret; |
7706 | 0 | for (const auto & [ctx, bufs] : pimpl->ctxs_bufs) { |
7707 | 0 | if (hparams.no_alloc) { |
7708 | 0 | GGML_ASSERT(bufs.size() == 1); |
7709 | 0 | ggml_backend_buffer_t buf = bufs[0].get(); |
7710 | 0 | GGML_ASSERT(ggml_backend_buffer_get_base(buf) == nullptr); |
7711 | 0 | ggml_backend_buffer_type_t buft = ggml_backend_buffer_get_type(buf); |
7712 | 0 | ret[buft] += ggml_backend_alloc_ctx_tensors_from_buft_size(ctx.get(), buft); |
7713 | 0 | } else { |
7714 | 0 | for (const auto & buf : bufs) { |
7715 | | // GGML_ASSERT(ggml_backend_buffer_get_base(buf.get()) != nullptr); // multi_buffer does not have a defined base |
7716 | 0 | ret[ggml_backend_buffer_get_type(buf.get())] += ggml_backend_buffer_get_size(buf.get()); |
7717 | 0 | } |
7718 | 0 | } |
7719 | 0 | } |
7720 | 0 | return ret; |
7721 | 0 | } |
7722 | | |
7723 | 0 | uint64_t llama_model::n_elements() const { |
7724 | 0 | return pimpl->n_elements; |
7725 | 0 | } |
7726 | | |
7727 | 0 | void llama_model::print_info() const { |
7728 | 0 | const std::string rope_scaling_type = llama_rope_scaling_type_name(hparams.rope_scaling_type_train); |
7729 | |
|
7730 | 0 | auto print_f = [](const std::function<uint32_t(uint32_t)> & f, uint32_t n) { |
7731 | 0 | bool is_var = false; |
7732 | |
|
7733 | 0 | std::vector<uint32_t> v; |
7734 | 0 | for (uint32_t i = 0; i < n; ++i) { |
7735 | 0 | v.push_back(f(i)); |
7736 | 0 | if (v[i] != v[0]) { |
7737 | 0 | is_var = true; |
7738 | 0 | } |
7739 | 0 | } |
7740 | |
|
7741 | 0 | std::stringstream ss; |
7742 | |
|
7743 | 0 | if (is_var) { |
7744 | 0 | ss << "["; |
7745 | 0 | for (uint32_t i = 0; i < n; ++i) { |
7746 | 0 | ss << v[i]; |
7747 | 0 | if (i < n - 1) { |
7748 | 0 | ss << ", "; |
7749 | 0 | } |
7750 | 0 | } |
7751 | 0 | ss << "]"; |
7752 | 0 | } else { |
7753 | 0 | ss << v[0]; |
7754 | 0 | } |
7755 | |
|
7756 | 0 | return ss.str(); |
7757 | 0 | }; |
7758 | | |
7759 | | // hparams |
7760 | 0 | LLAMA_LOG_INFO("%s: arch = %s\n", __func__, arch_name().c_str()); |
7761 | 0 | LLAMA_LOG_INFO("%s: vocab_only = %d\n", __func__, hparams.vocab_only); |
7762 | 0 | LLAMA_LOG_INFO("%s: no_alloc = %d\n", __func__, hparams.no_alloc); |
7763 | |
|
7764 | 0 | if (!hparams.vocab_only) { |
7765 | 0 | LLAMA_LOG_INFO("%s: n_ctx_train = %u\n", __func__, hparams.n_ctx_train); |
7766 | 0 | LLAMA_LOG_INFO("%s: n_embd = %u\n", __func__, hparams.n_embd); |
7767 | 0 | LLAMA_LOG_INFO("%s: n_embd_inp = %u\n", __func__, hparams.n_embd_inp()); |
7768 | 0 | LLAMA_LOG_INFO("%s: n_layer = %u\n", __func__, hparams.n_layer); |
7769 | 0 | LLAMA_LOG_INFO("%s: n_head = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_head(il); }, hparams.n_layer).c_str()); |
7770 | 0 | LLAMA_LOG_INFO("%s: n_head_kv = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_head_kv(il); }, hparams.n_layer).c_str()); |
7771 | 0 | LLAMA_LOG_INFO("%s: n_rot = %u\n", __func__, hparams.n_rot_full); |
7772 | 0 | LLAMA_LOG_INFO("%s: n_swa = %u\n", __func__, hparams.n_swa); |
7773 | 0 | LLAMA_LOG_INFO("%s: is_swa_any = %u\n", __func__, hparams.is_swa_any()); |
7774 | 0 | LLAMA_LOG_INFO("%s: n_embd_head_k = %u\n", __func__, hparams.n_embd_head_k_full); |
7775 | 0 | LLAMA_LOG_INFO("%s: n_embd_head_v = %u\n", __func__, hparams.n_embd_head_v_full); |
7776 | 0 | LLAMA_LOG_INFO("%s: n_gqa = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_gqa(il); }, hparams.n_layer).c_str()); |
7777 | 0 | LLAMA_LOG_INFO("%s: n_embd_k_gqa = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_embd_k_gqa(il); }, hparams.n_layer).c_str()); |
7778 | 0 | LLAMA_LOG_INFO("%s: n_embd_v_gqa = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_embd_v_gqa(il); }, hparams.n_layer).c_str()); |
7779 | 0 | LLAMA_LOG_INFO("%s: f_norm_eps = %.1e\n", __func__, hparams.f_norm_eps); |
7780 | 0 | LLAMA_LOG_INFO("%s: f_norm_rms_eps = %.1e\n", __func__, hparams.f_norm_rms_eps); |
7781 | 0 | LLAMA_LOG_INFO("%s: f_clamp_kqv = %.1e\n", __func__, hparams.f_clamp_kqv); |
7782 | 0 | LLAMA_LOG_INFO("%s: f_max_alibi_bias = %.1e\n", __func__, hparams.f_max_alibi_bias); |
7783 | 0 | LLAMA_LOG_INFO("%s: f_logit_scale = %.1e\n", __func__, hparams.f_logit_scale); |
7784 | 0 | LLAMA_LOG_INFO("%s: f_attn_scale = %.1e\n", __func__, hparams.f_attention_scale); |
7785 | 0 | LLAMA_LOG_INFO("%s: n_ff = %s\n", __func__, print_f([&](uint32_t il) { return hparams.n_ff(il); }, hparams.n_layer).c_str()); |
7786 | 0 | LLAMA_LOG_INFO("%s: n_expert = %u\n", __func__, hparams.n_expert); |
7787 | 0 | LLAMA_LOG_INFO("%s: n_expert_used = %u\n", __func__, hparams.n_expert_used); |
7788 | 0 | LLAMA_LOG_INFO("%s: n_expert_groups = %d\n", __func__, hparams.n_expert_groups); |
7789 | 0 | LLAMA_LOG_INFO("%s: n_group_used = %d\n", __func__, hparams.n_group_used); |
7790 | 0 | LLAMA_LOG_INFO("%s: causal attn = %d\n", __func__, hparams.causal_attn); |
7791 | 0 | LLAMA_LOG_INFO("%s: pooling type = %d\n", __func__, hparams.pooling_type); |
7792 | 0 | LLAMA_LOG_INFO("%s: rope type = %d\n", __func__, hparams.rope_type); |
7793 | 0 | LLAMA_LOG_INFO("%s: rope scaling = %s\n", __func__, rope_scaling_type.c_str()); |
7794 | 0 | LLAMA_LOG_INFO("%s: freq_base_train = %.1f\n", __func__, hparams.rope_freq_base_train); |
7795 | 0 | LLAMA_LOG_INFO("%s: freq_scale_train = %g\n", __func__, hparams.rope_freq_scale_train); |
7796 | 0 | if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) { |
7797 | 0 | LLAMA_LOG_INFO("%s: freq_base_swa = %.1f\n", __func__, hparams.rope_freq_base_train_swa); |
7798 | 0 | LLAMA_LOG_INFO("%s: freq_scale_swa = %g\n", __func__, hparams.rope_freq_scale_train_swa); |
7799 | 0 | LLAMA_LOG_INFO("%s: n_embd_head_k_swa = %u\n", __func__, hparams.n_embd_head_k_swa); |
7800 | 0 | LLAMA_LOG_INFO("%s: n_embd_head_v_swa = %u\n", __func__, hparams.n_embd_head_v_swa); |
7801 | 0 | LLAMA_LOG_INFO("%s: n_rot_swa = %u\n", __func__, hparams.n_rot_swa); |
7802 | 0 | } |
7803 | 0 | LLAMA_LOG_INFO("%s: n_ctx_orig_yarn = %u\n", __func__, hparams.n_ctx_orig_yarn); |
7804 | 0 | LLAMA_LOG_INFO("%s: rope_yarn_log_mul = %.4f\n", __func__, hparams.rope_yarn_log_mul); |
7805 | 0 | LLAMA_LOG_INFO("%s: rope_finetuned = %s\n", __func__, hparams.rope_finetuned ? "yes" : "unknown"); |
7806 | | // MRoPE (Multi-axis Rotary Position Embedding) sections |
7807 | 0 | if (const auto & s = hparams.rope_sections; s[0] || s[1] || s[2] || s[3]) { |
7808 | 0 | LLAMA_LOG_INFO("%s: mrope sections = [%d, %d, %d, %d]\n", __func__, s[0], s[1], s[2], s[3]); |
7809 | 0 | } |
7810 | 0 | if (!classifier_labels.empty()) { |
7811 | 0 | LLAMA_LOG_INFO("%s: n_cls_out = %u\n", __func__, hparams.n_cls_out); |
7812 | |
|
7813 | 0 | size_t i = 0; |
7814 | 0 | for (auto label : classifier_labels) { |
7815 | 0 | LLAMA_LOG_INFO("%s: cls_label[%2zu] = %s\n", __func__, i++, label.c_str()); |
7816 | 0 | } |
7817 | 0 | } |
7818 | 0 | } |
7819 | |
|
7820 | 0 | if (arch == LLM_ARCH_MAMBA || |
7821 | 0 | arch == LLM_ARCH_MAMBA2 || |
7822 | 0 | arch == LLM_ARCH_JAMBA || |
7823 | 0 | arch == LLM_ARCH_FALCON_H1 || |
7824 | 0 | arch == LLM_ARCH_PLAMO2 || |
7825 | 0 | arch == LLM_ARCH_GRANITE_HYBRID || |
7826 | 0 | arch == LLM_ARCH_QWEN3NEXT || |
7827 | 0 | arch == LLM_ARCH_QWEN35 || |
7828 | 0 | arch == LLM_ARCH_QWEN35MOE || |
7829 | 0 | arch == LLM_ARCH_NEMOTRON_H || |
7830 | 0 | arch == LLM_ARCH_NEMOTRON_H_MOE) { |
7831 | 0 | LLAMA_LOG_INFO("%s: ssm_d_conv = %u\n", __func__, hparams.ssm_d_conv); |
7832 | 0 | LLAMA_LOG_INFO("%s: ssm_d_inner = %u\n", __func__, hparams.ssm_d_inner); |
7833 | 0 | LLAMA_LOG_INFO("%s: ssm_d_state = %u\n", __func__, hparams.ssm_d_state); |
7834 | 0 | LLAMA_LOG_INFO("%s: ssm_dt_rank = %u\n", __func__, hparams.ssm_dt_rank); |
7835 | 0 | LLAMA_LOG_INFO("%s: ssm_n_group = %u\n", __func__, hparams.ssm_n_group); |
7836 | 0 | LLAMA_LOG_INFO("%s: ssm_dt_b_c_rms = %d\n", __func__, hparams.ssm_dt_b_c_rms); |
7837 | 0 | } |
7838 | |
|
7839 | 0 | LLAMA_LOG_INFO("%s: model type = %s\n", __func__, type_name().c_str()); |
7840 | 0 | if (pimpl->n_elements >= 1e12) { |
7841 | 0 | LLAMA_LOG_INFO("%s: model params = %.2f T\n", __func__, pimpl->n_elements*1e-12); |
7842 | 0 | } else if (pimpl->n_elements >= 1e9) { |
7843 | 0 | LLAMA_LOG_INFO("%s: model params = %.2f B\n", __func__, pimpl->n_elements*1e-9); |
7844 | 0 | } else if (pimpl->n_elements >= 1e6) { |
7845 | 0 | LLAMA_LOG_INFO("%s: model params = %.2f M\n", __func__, pimpl->n_elements*1e-6); |
7846 | 0 | } else { |
7847 | 0 | LLAMA_LOG_INFO("%s: model params = %.2f K\n", __func__, pimpl->n_elements*1e-3); |
7848 | 0 | } |
7849 | | |
7850 | | // general kv |
7851 | 0 | LLAMA_LOG_INFO("%s: general.name = %s\n", __func__, name.c_str()); |
7852 | |
|
7853 | 0 | if (arch == LLM_ARCH_DEEPSEEK) { |
7854 | 0 | LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead); |
7855 | 0 | LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp); |
7856 | 0 | LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared); |
7857 | 0 | LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale); |
7858 | 0 | } |
7859 | |
|
7860 | 0 | if (arch == LLM_ARCH_DEEPSEEK2 || arch == LLM_ARCH_GLM_DSA || arch == LLM_ARCH_MISTRAL4) { |
7861 | 0 | LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead); |
7862 | 0 | LLAMA_LOG_INFO("%s: n_lora_q = %d\n", __func__, hparams.n_lora_q); |
7863 | 0 | LLAMA_LOG_INFO("%s: n_lora_kv = %d\n", __func__, hparams.n_lora_kv); |
7864 | 0 | LLAMA_LOG_INFO("%s: n_embd_head_k_mla = %d\n", __func__, hparams.n_embd_head_k_mla()); |
7865 | 0 | LLAMA_LOG_INFO("%s: n_embd_head_v_mla = %d\n", __func__, hparams.n_embd_head_v_mla()); |
7866 | 0 | LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp); |
7867 | 0 | LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared); |
7868 | 0 | LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale); |
7869 | 0 | LLAMA_LOG_INFO("%s: expert_weights_norm = %d\n", __func__, hparams.expert_weights_norm); |
7870 | 0 | LLAMA_LOG_INFO("%s: expert_gating_func = %s\n", __func__, llama_expert_gating_func_name((llama_expert_gating_func_type) hparams.expert_gating_func)); |
7871 | 0 | } |
7872 | |
|
7873 | 0 | if (arch == LLM_ARCH_QWEN2MOE) { |
7874 | 0 | LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp); |
7875 | 0 | LLAMA_LOG_INFO("%s: n_ff_shexp = %d\n", __func__, hparams.n_ff_shexp); |
7876 | 0 | } |
7877 | |
|
7878 | 0 | if (arch == LLM_ARCH_QWEN3MOE || arch == LLM_ARCH_OPENAI_MOE || arch == LLM_ARCH_QWEN3VLMOE || arch == LLM_ARCH_RND1) { |
7879 | 0 | LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp); |
7880 | 0 | } |
7881 | |
|
7882 | 0 | if (arch == LLM_ARCH_MINICPM || |
7883 | 0 | arch == LLM_ARCH_GRANITE || |
7884 | 0 | arch == LLM_ARCH_GRANITE_MOE || |
7885 | 0 | arch == LLM_ARCH_GRANITE_HYBRID || |
7886 | 0 | arch == LLM_ARCH_NEMOTRON_H_MOE) { |
7887 | 0 | LLAMA_LOG_INFO("%s: f_embedding_scale = %f\n", __func__, hparams.f_embedding_scale); |
7888 | 0 | LLAMA_LOG_INFO("%s: f_residual_scale = %f\n", __func__, hparams.f_residual_scale); |
7889 | 0 | LLAMA_LOG_INFO("%s: f_attention_scale = %f\n", __func__, hparams.f_attention_scale); |
7890 | 0 | LLAMA_LOG_INFO("%s: n_ff_shexp = %d\n", __func__, hparams.n_ff_shexp); |
7891 | 0 | } |
7892 | |
|
7893 | 0 | if (arch == LLM_ARCH_BAILINGMOE) { |
7894 | 0 | LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead); |
7895 | 0 | LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp); |
7896 | 0 | LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared); |
7897 | 0 | LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale); |
7898 | 0 | LLAMA_LOG_INFO("%s: expert_weights_norm = %d\n", __func__, hparams.expert_weights_norm); |
7899 | 0 | } |
7900 | |
|
7901 | 0 | if (arch == LLM_ARCH_BAILINGMOE2) { |
7902 | 0 | LLAMA_LOG_INFO("%s: n_layer_dense_lead = %d\n", __func__, hparams.n_layer_dense_lead); |
7903 | 0 | LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp); |
7904 | 0 | LLAMA_LOG_INFO("%s: n_ff_shexp = %d\n", __func__, hparams.n_ff_shexp); |
7905 | 0 | LLAMA_LOG_INFO("%s: n_expert_shared = %d\n", __func__, hparams.n_expert_shared); |
7906 | 0 | LLAMA_LOG_INFO("%s: expert_weights_scale = %.1f\n", __func__, hparams.expert_weights_scale); |
7907 | 0 | LLAMA_LOG_INFO("%s: expert_weights_norm = %d\n", __func__, hparams.expert_weights_norm); |
7908 | 0 | LLAMA_LOG_INFO("%s: expert_gating_func = %s\n", __func__, llama_expert_gating_func_name((llama_expert_gating_func_type) hparams.expert_gating_func)); |
7909 | 0 | LLAMA_LOG_INFO("%s: nextn_predict_layers = %d\n", __func__, hparams.nextn_predict_layers); |
7910 | 0 | } |
7911 | |
|
7912 | 0 | if (arch == LLM_ARCH_SMALLTHINKER || arch == LLM_ARCH_LFM2MOE) { |
7913 | 0 | LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp); |
7914 | 0 | LLAMA_LOG_INFO("%s: expert_gating_func = %s\n", __func__, llama_expert_gating_func_name((llama_expert_gating_func_type) hparams.expert_gating_func)); |
7915 | 0 | } |
7916 | |
|
7917 | 0 | if (arch == LLM_ARCH_GROVEMOE) { |
7918 | 0 | LLAMA_LOG_INFO("%s: n_ff_exp = %d\n", __func__, hparams.n_ff_exp); |
7919 | 0 | LLAMA_LOG_INFO("%s: n_ff_chexp = %d\n", __func__, hparams.n_ff_chexp); |
7920 | 0 | LLAMA_LOG_INFO("%s: n_group_experts = %d\n", __func__, hparams.n_group_experts); |
7921 | 0 | LLAMA_LOG_INFO("%s: expert_group_scale = %.2f\n", __func__, hparams.expert_group_scale); |
7922 | 0 | } |
7923 | |
|
7924 | 0 | vocab.print_info(); |
7925 | 0 | } |
7926 | | |
7927 | 0 | ggml_backend_dev_t llama_model::dev_layer(int il) const { |
7928 | 0 | return pimpl->dev_layer.at(il).dev; |
7929 | 0 | } |
7930 | | |
7931 | 0 | ggml_backend_dev_t llama_model::dev_output() const { |
7932 | 0 | return pimpl->dev_output.dev; |
7933 | 0 | } |
7934 | | |
7935 | | template<typename F> |
7936 | 0 | static bool buft_supported(ggml_backend_buffer_type_t buft, ggml_backend_dev_t dev, F & fn) { |
7937 | 0 | ggml_init_params params = { |
7938 | 0 | /*.mem_size =*/ ggml_tensor_overhead()*8, |
7939 | 0 | /*.mem_buffer =*/ NULL, |
7940 | 0 | /*.no_alloc =*/ true, |
7941 | 0 | }; |
7942 | |
|
7943 | 0 | ggml_context_ptr ctx { ggml_init(params) }; |
7944 | 0 | if (!ctx) { |
7945 | 0 | throw std::runtime_error(format("failed to create ggml context")); |
7946 | 0 | } |
7947 | | |
7948 | 0 | ggml_backend_buffer_ptr buf { ggml_backend_buft_alloc_buffer(buft, 0) }; |
7949 | 0 | ggml_tensor * op_tensor = fn(ctx.get()); |
7950 | 0 | for (int i = 0; i < GGML_MAX_SRC; i++) { |
7951 | 0 | if (op_tensor->src[i] != nullptr) { |
7952 | 0 | assert(op_tensor->src[i]->buffer == nullptr); |
7953 | 0 | op_tensor->src[i]->buffer = buf.get(); |
7954 | 0 | } |
7955 | 0 | } |
7956 | |
|
7957 | 0 | bool op_supported = ggml_backend_dev_supports_op(dev, op_tensor); |
7958 | |
|
7959 | 0 | return op_supported; |
7960 | 0 | } |
7961 | | |
7962 | | template<typename F> |
7963 | 0 | static ggml_backend_buffer_type_t select_buft(const buft_list_t & buft_list, const F & fn) { |
7964 | 0 | for (const auto & cur : buft_list) { |
7965 | 0 | ggml_backend_dev_t cur_dev = cur.first; |
7966 | 0 | ggml_backend_buffer_type_t cur_buft = cur.second; |
7967 | 0 | if (buft_supported(cur_buft, cur_dev, fn)) { |
7968 | 0 | return cur_buft; |
7969 | 0 | } |
7970 | 0 | } |
7971 | | |
7972 | 0 | throw std::runtime_error(format("no suitable buffer type found")); |
7973 | 0 | } |
7974 | | |
7975 | 0 | ggml_backend_buffer_type_t llama_model::select_buft(int il) const { |
7976 | 0 | return ::select_buft( |
7977 | 0 | *pimpl->dev_layer.at(il).buft_list, |
7978 | 0 | [&](ggml_context * ctx) { |
7979 | 0 | ggml_tensor * cur = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hparams.n_embd); |
7980 | 0 | ggml_tensor * layer_dir = ggml_new_tensor_1d(ctx, GGML_TYPE_F32, hparams.n_embd); |
7981 | 0 | return ggml_add(ctx, cur, layer_dir); |
7982 | 0 | }); |
7983 | 0 | } |
7984 | | |
7985 | 0 | bool llama_model::has_tensor_overrides() const { |
7986 | 0 | return pimpl->has_tensor_overrides; |
7987 | 0 | } |
7988 | | |
7989 | 0 | const ggml_tensor * llama_model::get_tensor(const char * name) const { |
7990 | 0 | auto it = std::find_if(tensors_by_name.begin(), tensors_by_name.end(), |
7991 | 0 | [name](const std::pair<std::string, ggml_tensor *> & it) { |
7992 | 0 | return it.first == name; |
7993 | 0 | }); |
7994 | 0 | if (it == tensors_by_name.end()) { |
7995 | 0 | return nullptr; |
7996 | 0 | } |
7997 | | |
7998 | 0 | return it->second; |
7999 | 0 | } |
8000 | | |
8001 | 0 | float llama_model::get_rope_freq_base (const llama_cparams & cparams, int il) const { |
8002 | 0 | return hparams.is_swa(il) ? hparams.rope_freq_base_train_swa : cparams.rope_freq_base; |
8003 | 0 | } |
8004 | | |
8005 | 0 | float llama_model::get_rope_freq_scale(const llama_cparams & cparams, int il) const { |
8006 | 0 | return hparams.is_swa(il) ? hparams.rope_freq_scale_train_swa : cparams.rope_freq_scale; |
8007 | 0 | } |
8008 | | |
8009 | 0 | ggml_tensor * llama_model::get_rope_factors(const llama_cparams & cparams, int il) const { |
8010 | 0 | const uint32_t n_ctx_seq = cparams.n_ctx_seq; |
8011 | | |
8012 | | // choose long/short freq factors based on the context size |
8013 | 0 | if (layers[il].rope_freqs != nullptr) { |
8014 | 0 | return layers[il].rope_freqs; |
8015 | 0 | } |
8016 | | |
8017 | 0 | if (n_ctx_seq > hparams.n_ctx_orig_yarn) { |
8018 | 0 | return layers[il].rope_long; |
8019 | 0 | } |
8020 | | |
8021 | 0 | return layers[il].rope_short; |
8022 | 0 | } |
8023 | | |
8024 | 0 | llama_memory_i * llama_model::create_memory(const llama_memory_params & params, const llama_cparams & cparams) const { |
8025 | 0 | llama_memory_i * res; |
8026 | |
|
8027 | 0 | switch (arch) { |
8028 | | // Models that need specific instantiation should be handled in the |
8029 | | // switch statement |
8030 | 0 | case LLM_ARCH_BERT: |
8031 | 0 | case LLM_ARCH_JINA_BERT_V2: |
8032 | 0 | case LLM_ARCH_JINA_BERT_V3: |
8033 | 0 | case LLM_ARCH_NOMIC_BERT: |
8034 | 0 | case LLM_ARCH_NOMIC_BERT_MOE: |
8035 | 0 | case LLM_ARCH_NEO_BERT: |
8036 | 0 | case LLM_ARCH_EUROBERT: |
8037 | 0 | case LLM_ARCH_WAVTOKENIZER_DEC: |
8038 | 0 | case LLM_ARCH_MODERN_BERT: |
8039 | 0 | case LLM_ARCH_GEMMA_EMBEDDING: |
8040 | 0 | case LLM_ARCH_DREAM: |
8041 | 0 | case LLM_ARCH_LLADA: |
8042 | 0 | case LLM_ARCH_LLADA_MOE: |
8043 | 0 | case LLM_ARCH_RND1: |
8044 | 0 | { |
8045 | 0 | res = nullptr; |
8046 | 0 | } break; |
8047 | | // Models that need standard caching should rely on recurrent/hybrid |
8048 | | // checks |
8049 | 0 | default: |
8050 | 0 | { |
8051 | 0 | if (llm_arch_is_recurrent(arch)) { |
8052 | 0 | res = new llama_memory_recurrent( |
8053 | 0 | *this, |
8054 | 0 | GGML_TYPE_F32, |
8055 | 0 | GGML_TYPE_F32, |
8056 | 0 | cparams.offload_kqv, |
8057 | 0 | std::max((uint32_t) 1, cparams.n_seq_max), |
8058 | 0 | cparams.n_seq_max, |
8059 | 0 | nullptr); |
8060 | 0 | } else if (llm_arch_is_hybrid(arch)) { |
8061 | | // The main difference between hybrid architectures is the |
8062 | | // layer filters, so pick the right one here |
8063 | 0 | llama_memory_hybrid::layer_filter_cb filter_attn = nullptr; |
8064 | 0 | llama_memory_hybrid::layer_filter_cb filter_recr = nullptr; |
8065 | 0 | if (arch == LLM_ARCH_FALCON_H1) { |
8066 | 0 | filter_attn = [&](int32_t) { return true; }; |
8067 | 0 | filter_recr = [&](int32_t) { return true; }; |
8068 | 0 | } else if (arch == LLM_ARCH_NEMOTRON_H || arch == LLM_ARCH_NEMOTRON_H_MOE) { |
8069 | 0 | filter_attn = [&](int32_t il) { |
8070 | 0 | return !hparams.is_recurrent(il) && hparams.n_ff(il) == 0; |
8071 | 0 | }; |
8072 | 0 | filter_recr = [&](int32_t il) { |
8073 | 0 | return hparams.is_recurrent(il) && hparams.n_ff(il) == 0; |
8074 | 0 | }; |
8075 | 0 | } |
8076 | |
|
8077 | 0 | if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) { |
8078 | | // Use hybrid-iswa for hybrid models with SWA |
8079 | 0 | res = new llama_memory_hybrid_iswa( |
8080 | 0 | /* model */ *this, |
8081 | 0 | /* attn_type_k */ params.type_k, |
8082 | 0 | /* attn_type_v */ params.type_v, |
8083 | 0 | /* attn_v_trans */ !cparams.flash_attn, |
8084 | 0 | /* attn_swa_full */ params.swa_full, |
8085 | 0 | /* attn_kv_size */ cparams.n_ctx_seq, |
8086 | 0 | /* attn_n_ubatch */ cparams.n_ubatch, |
8087 | 0 | /* attn_n_pad */ 1, |
8088 | 0 | /* recurrent_type_r */ GGML_TYPE_F32, |
8089 | 0 | /* recurrent_type_s */ GGML_TYPE_F32, |
8090 | 0 | /* recurrent_rs_size */ std::max((uint32_t) 1, cparams.n_seq_max), |
8091 | 0 | /* n_seq_max */ cparams.n_seq_max, |
8092 | 0 | /* offload */ cparams.offload_kqv, |
8093 | 0 | /* unified */ cparams.kv_unified, |
8094 | 0 | /* filter_attn */ std::move(filter_attn), |
8095 | 0 | /* filter_recr */ std::move(filter_recr)); |
8096 | 0 | } else { |
8097 | 0 | res = new llama_memory_hybrid( |
8098 | 0 | /* model */ *this, |
8099 | 0 | /* attn_type_k */ params.type_k, |
8100 | 0 | /* attn_type_v */ params.type_v, |
8101 | 0 | /* attn_v_trans */ !cparams.flash_attn, |
8102 | 0 | /* attn_kv_size */ cparams.n_ctx_seq, |
8103 | 0 | /* attn_n_pad */ 1, |
8104 | 0 | /* attn_n_swa */ hparams.n_swa, |
8105 | 0 | /* attn_swa_type */ hparams.swa_type, |
8106 | 0 | /* recurrent_type_k */ GGML_TYPE_F32, |
8107 | 0 | /* recurrent_type_v */ GGML_TYPE_F32, |
8108 | 0 | /* recurrent_kv_size */ std::max((uint32_t) 1, cparams.n_seq_max), |
8109 | 0 | /* n_seq_max */ cparams.n_seq_max, |
8110 | 0 | /* offload */ cparams.offload_kqv, |
8111 | 0 | /* unified */ cparams.kv_unified, |
8112 | 0 | /* filter_attn */ std::move(filter_attn), |
8113 | 0 | /* filter_recr */ std::move(filter_recr)); |
8114 | 0 | } |
8115 | 0 | } else { |
8116 | 0 | llama_memory_i::layer_reuse_cb reuse = nullptr; |
8117 | |
|
8118 | 0 | if (arch == LLM_ARCH_GEMMA3N) { |
8119 | 0 | reuse = [&](int32_t il) { |
8120 | 0 | if (il >= (int32_t) hparams.n_layer_kv_from_start) { |
8121 | 0 | return (int32_t) hparams.n_layer_kv_from_start - (hparams.is_swa(il) ? 2 : 1); |
8122 | 0 | } |
8123 | | |
8124 | 0 | return -1; |
8125 | 0 | }; |
8126 | 0 | } |
8127 | |
|
8128 | 0 | if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) { |
8129 | 0 | GGML_ASSERT(hparams.is_swa_any()); |
8130 | |
|
8131 | 0 | res = new llama_kv_cache_iswa( |
8132 | 0 | *this, |
8133 | 0 | params.type_k, |
8134 | 0 | params.type_v, |
8135 | 0 | !cparams.flash_attn, |
8136 | 0 | cparams.offload_kqv, |
8137 | 0 | params.swa_full, |
8138 | 0 | cparams.kv_unified, |
8139 | 0 | cparams.n_ctx_seq, |
8140 | 0 | cparams.n_seq_max, |
8141 | 0 | cparams.n_ubatch, |
8142 | 0 | 1, |
8143 | 0 | nullptr, |
8144 | 0 | reuse); |
8145 | 0 | } else { |
8146 | 0 | GGML_ASSERT(!hparams.is_swa_any()); |
8147 | |
|
8148 | 0 | res = new llama_kv_cache( |
8149 | 0 | *this, |
8150 | 0 | params.type_k, |
8151 | 0 | params.type_v, |
8152 | 0 | !cparams.flash_attn, |
8153 | 0 | cparams.offload_kqv, |
8154 | 0 | cparams.kv_unified, |
8155 | 0 | cparams.n_ctx_seq, |
8156 | 0 | cparams.n_seq_max, |
8157 | 0 | 1, |
8158 | 0 | hparams.n_swa, |
8159 | 0 | hparams.swa_type, |
8160 | 0 | nullptr, |
8161 | 0 | nullptr); |
8162 | 0 | } |
8163 | 0 | } |
8164 | 0 | } |
8165 | 0 | } |
8166 | | |
8167 | 0 | return res; |
8168 | 0 | } |
8169 | | |
8170 | 0 | ggml_cgraph * llama_model::build_graph(const llm_graph_params & params) const { |
8171 | 0 | std::unique_ptr<llm_graph_context> llm; |
8172 | |
|
8173 | 0 | switch (arch) { |
8174 | 0 | case LLM_ARCH_LLAMA: |
8175 | 0 | { |
8176 | 0 | llm = std::make_unique<llm_build_llama<false>>(*this, params); |
8177 | 0 | } break; |
8178 | 0 | case LLM_ARCH_LLAMA4: |
8179 | 0 | { |
8180 | 0 | if (hparams.swa_type == LLAMA_SWA_TYPE_NONE) { |
8181 | 0 | llm = std::make_unique<llm_build_llama<false>>(*this, params); |
8182 | 0 | } else { |
8183 | 0 | llm = std::make_unique<llm_build_llama_iswa>(*this, params); |
8184 | 0 | } |
8185 | 0 | } break; |
8186 | 0 | case LLM_ARCH_LLAMA_EMBED: |
8187 | 0 | { |
8188 | 0 | llm = std::make_unique<llm_build_llama<true>>(*this, params); |
8189 | 0 | } break; |
8190 | 0 | case LLM_ARCH_MAINCODER: |
8191 | 0 | { |
8192 | 0 | llm = std::make_unique<llm_build_maincoder>(*this, params); |
8193 | 0 | } break; |
8194 | 0 | case LLM_ARCH_DECI: |
8195 | 0 | { |
8196 | 0 | llm = std::make_unique<llm_build_deci>(*this, params); |
8197 | 0 | } break; |
8198 | 0 | case LLM_ARCH_BAICHUAN: |
8199 | 0 | { |
8200 | 0 | llm = std::make_unique<llm_build_baichuan>(*this, params); |
8201 | 0 | } break; |
8202 | 0 | case LLM_ARCH_FALCON: |
8203 | 0 | { |
8204 | 0 | llm = std::make_unique<llm_build_falcon>(*this, params); |
8205 | 0 | } break; |
8206 | 0 | case LLM_ARCH_GROK: |
8207 | 0 | { |
8208 | 0 | llm = std::make_unique<llm_build_grok>(*this, params); |
8209 | 0 | } break; |
8210 | 0 | case LLM_ARCH_STARCODER: |
8211 | 0 | { |
8212 | 0 | llm = std::make_unique<llm_build_starcoder>(*this, params); |
8213 | 0 | } break; |
8214 | 0 | case LLM_ARCH_REFACT: |
8215 | 0 | { |
8216 | 0 | llm = std::make_unique<llm_build_refact>(*this, params); |
8217 | 0 | } break; |
8218 | 0 | case LLM_ARCH_BERT: |
8219 | 0 | case LLM_ARCH_JINA_BERT_V2: |
8220 | 0 | case LLM_ARCH_JINA_BERT_V3: |
8221 | 0 | case LLM_ARCH_NOMIC_BERT: |
8222 | 0 | case LLM_ARCH_NOMIC_BERT_MOE: |
8223 | 0 | { |
8224 | 0 | llm = std::make_unique<llm_build_bert>(*this, params); |
8225 | 0 | } break; |
8226 | 0 | case LLM_ARCH_MODERN_BERT: |
8227 | 0 | { |
8228 | 0 | llm = std::make_unique<llm_build_modern_bert>(*this, params); |
8229 | 0 | } break; |
8230 | 0 | case LLM_ARCH_NEO_BERT: |
8231 | 0 | { |
8232 | 0 | llm = std::make_unique<llm_build_neo_bert>(*this, params); |
8233 | 0 | } break; |
8234 | 0 | case LLM_ARCH_EUROBERT: |
8235 | 0 | { |
8236 | 0 | llm = std::make_unique<llm_build_eurobert>(*this, params); |
8237 | 0 | } break; |
8238 | 0 | case LLM_ARCH_BLOOM: |
8239 | 0 | { |
8240 | 0 | llm = std::make_unique<llm_build_bloom>(*this, params); |
8241 | 0 | } break; |
8242 | 0 | case LLM_ARCH_MPT: |
8243 | 0 | { |
8244 | 0 | llm = std::make_unique<llm_build_mpt>(*this, params); |
8245 | 0 | } break; |
8246 | 0 | case LLM_ARCH_STABLELM: |
8247 | 0 | { |
8248 | 0 | llm = std::make_unique<llm_build_stablelm>(*this, params); |
8249 | 0 | } break; |
8250 | 0 | case LLM_ARCH_QWEN: |
8251 | 0 | { |
8252 | 0 | llm = std::make_unique<llm_build_qwen>(*this, params); |
8253 | 0 | } break; |
8254 | 0 | case LLM_ARCH_QWEN2: |
8255 | 0 | { |
8256 | 0 | llm = std::make_unique<llm_build_qwen2>(*this, params); |
8257 | 0 | } break; |
8258 | 0 | case LLM_ARCH_DREAM: |
8259 | 0 | { |
8260 | 0 | llm = std::make_unique<llm_build_dream>(*this, params); |
8261 | 0 | } |
8262 | 0 | break; |
8263 | 0 | case LLM_ARCH_LLADA: |
8264 | 0 | { |
8265 | 0 | llm = std::make_unique<llm_build_llada>(*this, params); |
8266 | 0 | } |
8267 | 0 | break; |
8268 | 0 | case LLM_ARCH_LLADA_MOE: |
8269 | 0 | { |
8270 | 0 | llm = std::make_unique<llm_build_llada_moe>(*this, params); |
8271 | 0 | } |
8272 | 0 | break; |
8273 | 0 | case LLM_ARCH_RND1: |
8274 | 0 | { |
8275 | 0 | llm = std::make_unique<llm_build_rnd1>(*this, params); |
8276 | 0 | } |
8277 | 0 | break; |
8278 | 0 | case LLM_ARCH_QWEN2VL: |
8279 | 0 | { |
8280 | 0 | llm = std::make_unique<llm_build_qwen2vl>(*this, params); |
8281 | 0 | } break; |
8282 | 0 | case LLM_ARCH_QWEN2MOE: |
8283 | 0 | { |
8284 | 0 | llm = std::make_unique<llm_build_qwen2moe>(*this, params); |
8285 | 0 | } break; |
8286 | 0 | case LLM_ARCH_QWEN3: |
8287 | 0 | { |
8288 | 0 | llm = std::make_unique<llm_build_qwen3>(*this, params); |
8289 | 0 | } break; |
8290 | 0 | case LLM_ARCH_QWEN3MOE: |
8291 | 0 | { |
8292 | 0 | llm = std::make_unique<llm_build_qwen3moe>(*this, params); |
8293 | 0 | } break; |
8294 | 0 | case LLM_ARCH_QWEN3VL: |
8295 | 0 | { |
8296 | 0 | llm = std::make_unique<llm_build_qwen3vl>(*this, params); |
8297 | 0 | } break; |
8298 | 0 | case LLM_ARCH_QWEN3VLMOE: |
8299 | 0 | { |
8300 | 0 | llm = std::make_unique<llm_build_qwen3vlmoe>(*this, params); |
8301 | 0 | } break; |
8302 | 0 | case LLM_ARCH_PHI2: |
8303 | 0 | { |
8304 | 0 | llm = std::make_unique<llm_build_phi2>(*this, params); |
8305 | 0 | } break; |
8306 | 0 | case LLM_ARCH_PHI3: |
8307 | 0 | case LLM_ARCH_PHIMOE: |
8308 | 0 | { |
8309 | 0 | if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) { |
8310 | 0 | llm = std::make_unique<llm_build_phi3<true>> (*this, params); |
8311 | 0 | } else { |
8312 | 0 | llm = std::make_unique<llm_build_phi3<false>>(*this, params); |
8313 | 0 | } |
8314 | 0 | } break; |
8315 | 0 | case LLM_ARCH_PLAMO: |
8316 | 0 | { |
8317 | 0 | llm = std::make_unique<llm_build_plamo>(*this, params); |
8318 | 0 | } break; |
8319 | 0 | case LLM_ARCH_PLAMO2: |
8320 | 0 | { |
8321 | 0 | llm = std::make_unique<llm_build_plamo2>(*this, params); |
8322 | 0 | } break; |
8323 | 0 | case LLM_ARCH_PLAMO3: |
8324 | 0 | { |
8325 | 0 | if (hparams.swa_type != LLAMA_SWA_TYPE_NONE) { |
8326 | 0 | llm = std::make_unique<llm_build_plamo3<true>> (*this, params); |
8327 | 0 | } else { |
8328 | 0 | llm = std::make_unique<llm_build_plamo3<false>>(*this, params); |
8329 | 0 | } |
8330 | 0 | } break; |
8331 | 0 | case LLM_ARCH_GPT2: |
8332 | 0 | { |
8333 | 0 | llm = std::make_unique<llm_build_gpt2>(*this, params); |
8334 | 0 | } break; |
8335 | 0 | case LLM_ARCH_CODESHELL: |
8336 | 0 | { |
8337 | 0 | llm = std::make_unique<llm_build_codeshell>(*this, params); |
8338 | 0 | } break; |
8339 | 0 | case LLM_ARCH_ORION: |
8340 | 0 | { |
8341 | 0 | llm = std::make_unique<llm_build_orion>(*this, params); |
8342 | 0 | } break; |
8343 | 0 | case LLM_ARCH_INTERNLM2: |
8344 | 0 | { |
8345 | 0 | llm = std::make_unique<llm_build_internlm2>(*this, params); |
8346 | 0 | } break; |
8347 | 0 | case LLM_ARCH_MINICPM3: |
8348 | 0 | { |
8349 | 0 | llm = std::make_unique<llm_build_minicpm3>(*this, params); |
8350 | 0 | } break; |
8351 | 0 | case LLM_ARCH_GEMMA: |
8352 | 0 | { |
8353 | 0 | llm = std::make_unique<llm_build_gemma>(*this, params); |
8354 | 0 | } break; |
8355 | 0 | case LLM_ARCH_GEMMA2: |
8356 | 0 | { |
8357 | 0 | llm = std::make_unique<llm_build_gemma2_iswa>(*this, params); |
8358 | 0 | } break; |
8359 | 0 | case LLM_ARCH_GEMMA3: |
8360 | 0 | { |
8361 | 0 | if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) { |
8362 | 0 | llm = std::make_unique<llm_build_gemma3<true>>(*this, params); |
8363 | 0 | } else { |
8364 | 0 | llm = std::make_unique<llm_build_gemma3<false>>(*this, params); |
8365 | 0 | } |
8366 | 0 | } break; |
8367 | 0 | case LLM_ARCH_GEMMA3N: |
8368 | 0 | { |
8369 | 0 | llm = std::make_unique<llm_build_gemma3n_iswa>(*this, params); |
8370 | 0 | } break; |
8371 | 0 | case LLM_ARCH_GEMMA_EMBEDDING: |
8372 | 0 | { |
8373 | 0 | llm = std::make_unique<llm_build_gemma_embedding>(*this, params); |
8374 | 0 | } break; |
8375 | 0 | case LLM_ARCH_STARCODER2: |
8376 | 0 | { |
8377 | 0 | llm = std::make_unique<llm_build_starcoder2>(*this, params); |
8378 | 0 | } break; |
8379 | 0 | case LLM_ARCH_MAMBA: |
8380 | 0 | case LLM_ARCH_MAMBA2: |
8381 | 0 | { |
8382 | 0 | llm = std::make_unique<llm_build_mamba>(*this, params); |
8383 | 0 | } break; |
8384 | 0 | case LLM_ARCH_JAMBA: |
8385 | 0 | { |
8386 | 0 | llm = std::make_unique<llm_build_jamba>(*this, params); |
8387 | 0 | } break; |
8388 | 0 | case LLM_ARCH_XVERSE: |
8389 | 0 | { |
8390 | 0 | llm = std::make_unique<llm_build_xverse>(*this, params); |
8391 | 0 | } break; |
8392 | 0 | case LLM_ARCH_COMMAND_R: |
8393 | 0 | { |
8394 | 0 | llm = std::make_unique<llm_build_command_r>(*this, params); |
8395 | 0 | } break; |
8396 | 0 | case LLM_ARCH_COHERE2: |
8397 | 0 | { |
8398 | 0 | llm = std::make_unique<llm_build_cohere2_iswa>(*this, params); |
8399 | 0 | } break; |
8400 | 0 | case LLM_ARCH_DBRX: |
8401 | 0 | { |
8402 | 0 | llm = std::make_unique<llm_build_dbrx>(*this, params); |
8403 | 0 | } break; |
8404 | 0 | case LLM_ARCH_OLMO: |
8405 | 0 | { |
8406 | 0 | llm = std::make_unique<llm_build_olmo>(*this, params); |
8407 | 0 | } break; |
8408 | 0 | case LLM_ARCH_OLMO2: |
8409 | 0 | { |
8410 | 0 | if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) { |
8411 | 0 | llm = std::make_unique<llm_build_olmo2<true>>(*this, params); |
8412 | 0 | } else { |
8413 | 0 | llm = std::make_unique<llm_build_olmo2<false>>(*this, params); |
8414 | 0 | } |
8415 | 0 | } break; |
8416 | 0 | case LLM_ARCH_OLMOE: |
8417 | 0 | { |
8418 | 0 | llm = std::make_unique<llm_build_olmoe>(*this, params); |
8419 | 0 | } break; |
8420 | 0 | case LLM_ARCH_OPENELM: |
8421 | 0 | { |
8422 | 0 | llm = std::make_unique<llm_build_openelm>(*this, params); |
8423 | 0 | } break; |
8424 | 0 | case LLM_ARCH_GPTNEOX: |
8425 | 0 | { |
8426 | 0 | llm = std::make_unique<llm_build_gptneox>(*this, params); |
8427 | 0 | } break; |
8428 | 0 | case LLM_ARCH_ARCTIC: |
8429 | 0 | { |
8430 | 0 | llm = std::make_unique<llm_build_arctic>(*this, params); |
8431 | 0 | } break; |
8432 | 0 | case LLM_ARCH_DEEPSEEK: |
8433 | 0 | { |
8434 | 0 | llm = std::make_unique<llm_build_deepseek>(*this, params); |
8435 | 0 | } break; |
8436 | 0 | case LLM_ARCH_DEEPSEEK2: |
8437 | 0 | case LLM_ARCH_GLM_DSA: |
8438 | 0 | case LLM_ARCH_MISTRAL4: |
8439 | 0 | { |
8440 | 0 | llm = std::make_unique<llm_build_deepseek2>(*this, params); |
8441 | 0 | } break; |
8442 | 0 | case LLM_ARCH_CHATGLM: |
8443 | 0 | { |
8444 | 0 | llm = std::make_unique<llm_build_chatglm>(*this, params); |
8445 | 0 | } break; |
8446 | 0 | case LLM_ARCH_GLM4: |
8447 | 0 | { |
8448 | 0 | llm = std::make_unique<llm_build_glm4>(*this, params); |
8449 | 0 | } break; |
8450 | 0 | case LLM_ARCH_GLM4_MOE: |
8451 | 0 | { |
8452 | 0 | llm = std::make_unique<llm_build_glm4_moe>(*this, params); |
8453 | 0 | } break; |
8454 | 0 | case LLM_ARCH_BITNET: |
8455 | 0 | { |
8456 | 0 | llm = std::make_unique<llm_build_bitnet>(*this, params); |
8457 | 0 | } break; |
8458 | 0 | case LLM_ARCH_T5: |
8459 | 0 | { |
8460 | 0 | switch (params.gtype) { |
8461 | 0 | case LLM_GRAPH_TYPE_ENCODER: |
8462 | 0 | llm = std::make_unique<llm_build_t5_enc>(*this, params); |
8463 | 0 | break; |
8464 | 0 | case LLM_GRAPH_TYPE_DEFAULT: |
8465 | 0 | case LLM_GRAPH_TYPE_DECODER: |
8466 | 0 | llm = std::make_unique<llm_build_t5_dec>(*this, params); |
8467 | 0 | break; |
8468 | 0 | default: |
8469 | 0 | GGML_ABORT("invalid graph type"); |
8470 | 0 | }; |
8471 | 0 | } break; |
8472 | 0 | case LLM_ARCH_T5ENCODER: |
8473 | 0 | { |
8474 | 0 | llm = std::make_unique<llm_build_t5_enc>(*this, params); |
8475 | 0 | } |
8476 | 0 | break; |
8477 | 0 | case LLM_ARCH_JAIS: |
8478 | 0 | { |
8479 | 0 | llm = std::make_unique<llm_build_jais>(*this, params); |
8480 | 0 | } break; |
8481 | 0 | case LLM_ARCH_JAIS2: |
8482 | 0 | { |
8483 | 0 | llm = std::make_unique<llm_build_jais2>(*this, params); |
8484 | 0 | } break; |
8485 | 0 | case LLM_ARCH_NEMOTRON: |
8486 | 0 | { |
8487 | 0 | llm = std::make_unique<llm_build_nemotron>(*this, params); |
8488 | 0 | } break; |
8489 | 0 | case LLM_ARCH_NEMOTRON_H: |
8490 | 0 | case LLM_ARCH_NEMOTRON_H_MOE: |
8491 | 0 | { |
8492 | 0 | llm = std::make_unique<llm_build_nemotron_h>(*this, params); |
8493 | 0 | } break; |
8494 | 0 | case LLM_ARCH_EXAONE: |
8495 | 0 | { |
8496 | 0 | llm = std::make_unique<llm_build_exaone>(*this, params); |
8497 | 0 | } break; |
8498 | 0 | case LLM_ARCH_EXAONE4: |
8499 | 0 | { |
8500 | 0 | if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) { |
8501 | 0 | llm = std::make_unique<llm_build_exaone4<true>>(*this, params); |
8502 | 0 | } else { |
8503 | 0 | llm = std::make_unique<llm_build_exaone4<false>>(*this, params); |
8504 | 0 | } |
8505 | 0 | } break; |
8506 | 0 | case LLM_ARCH_EXAONE_MOE: |
8507 | 0 | { |
8508 | 0 | llm = std::make_unique<llm_build_exaone_moe>(*this, params); |
8509 | 0 | } break; |
8510 | 0 | case LLM_ARCH_RWKV6: |
8511 | 0 | { |
8512 | 0 | llm = std::make_unique<llm_build_rwkv6>(*this, params); |
8513 | 0 | } break; |
8514 | 0 | case LLM_ARCH_RWKV6QWEN2: |
8515 | 0 | { |
8516 | 0 | llm = std::make_unique<llm_build_rwkv6qwen2>(*this, params); |
8517 | 0 | } break; |
8518 | 0 | case LLM_ARCH_RWKV7: |
8519 | 0 | { |
8520 | 0 | llm = std::make_unique<llm_build_rwkv7>(*this, params); |
8521 | 0 | } break; |
8522 | 0 | case LLM_ARCH_ARWKV7: |
8523 | 0 | { |
8524 | 0 | llm = std::make_unique<llm_build_arwkv7>(*this, params); |
8525 | 0 | } break; |
8526 | 0 | case LLM_ARCH_GRANITE: |
8527 | 0 | case LLM_ARCH_GRANITE_MOE: |
8528 | 0 | case LLM_ARCH_MINICPM: |
8529 | 0 | { |
8530 | 0 | llm = std::make_unique<llm_build_granite>(*this, params); |
8531 | 0 | } break; |
8532 | 0 | case LLM_ARCH_GRANITE_HYBRID: |
8533 | 0 | { |
8534 | 0 | llm = std::make_unique<llm_build_granite_hybrid>(*this, params); |
8535 | 0 | } break; |
8536 | 0 | case LLM_ARCH_CHAMELEON: |
8537 | 0 | { |
8538 | 0 | llm = std::make_unique<llm_build_chameleon>(*this, params); |
8539 | 0 | } break; |
8540 | 0 | case LLM_ARCH_WAVTOKENIZER_DEC: |
8541 | 0 | { |
8542 | 0 | llm = std::make_unique<llm_build_wavtokenizer_dec>(*this, params); |
8543 | 0 | } break; |
8544 | 0 | case LLM_ARCH_PLM: |
8545 | 0 | { |
8546 | 0 | llm = std::make_unique<llm_build_plm>(*this, params); |
8547 | 0 | } break; |
8548 | 0 | case LLM_ARCH_BAILINGMOE: |
8549 | 0 | { |
8550 | 0 | llm = std::make_unique<llm_build_bailingmoe>(*this, params); |
8551 | 0 | } break; |
8552 | 0 | case LLM_ARCH_BAILINGMOE2: |
8553 | 0 | { |
8554 | 0 | llm = std::make_unique<llm_build_bailingmoe2>(*this, params); |
8555 | 0 | } break; |
8556 | 0 | case LLM_ARCH_SEED_OSS: |
8557 | 0 | { |
8558 | 0 | llm = std::make_unique<llm_build_seed_oss>(*this, params); |
8559 | 0 | } break; |
8560 | 0 | case LLM_ARCH_DOTS1: |
8561 | 0 | { |
8562 | 0 | llm = std::make_unique<llm_build_dots1>(*this, params); |
8563 | 0 | } break; |
8564 | 0 | case LLM_ARCH_ARCEE: |
8565 | 0 | { |
8566 | 0 | llm = std::make_unique<llm_build_arcee>(*this, params); |
8567 | 0 | } break; |
8568 | 0 | case LLM_ARCH_AFMOE: |
8569 | 0 | { |
8570 | 0 | llm = std::make_unique<llm_build_afmoe>(*this, params); |
8571 | 0 | } break; |
8572 | 0 | case LLM_ARCH_ERNIE4_5: |
8573 | 0 | { |
8574 | 0 | llm = std::make_unique<llm_build_ernie4_5>(*this, params); |
8575 | 0 | } break; |
8576 | 0 | case LLM_ARCH_ERNIE4_5_MOE: |
8577 | 0 | { |
8578 | 0 | llm = std::make_unique<llm_build_ernie4_5_moe>(*this, params); |
8579 | 0 | } break; |
8580 | 0 | case LLM_ARCH_PADDLEOCR: |
8581 | 0 | { |
8582 | 0 | llm = std::make_unique<llm_build_paddleocr>(*this, params); |
8583 | 0 | } break; |
8584 | 0 | case LLM_ARCH_HUNYUAN_MOE: |
8585 | 0 | { |
8586 | 0 | llm = std::make_unique<llm_build_hunyuan_moe>(*this, params); |
8587 | 0 | } break; |
8588 | 0 | case LLM_ARCH_HUNYUAN_DENSE: |
8589 | 0 | { |
8590 | 0 | llm = std::make_unique<llm_build_hunyuan_dense>(*this, params); |
8591 | 0 | } break; |
8592 | 0 | case LLM_ARCH_SMOLLM3: |
8593 | 0 | { |
8594 | 0 | llm = std::make_unique<llm_build_smollm3>(*this, params); |
8595 | 0 | } break; |
8596 | 0 | case LLM_ARCH_OPENAI_MOE: |
8597 | 0 | { |
8598 | 0 | llm = std::make_unique<llm_build_openai_moe_iswa>(*this, params); |
8599 | 0 | } break; |
8600 | 0 | case LLM_ARCH_FALCON_H1: |
8601 | 0 | { |
8602 | 0 | llm = std::make_unique<llm_build_falcon_h1>(*this, params); |
8603 | 0 | } break; |
8604 | 0 | case LLM_ARCH_LFM2: |
8605 | 0 | case LLM_ARCH_LFM2MOE: |
8606 | 0 | { |
8607 | 0 | if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) { |
8608 | 0 | llm = std::make_unique<llm_build_lfm2<true>>(*this, params); |
8609 | 0 | } else { |
8610 | 0 | llm = std::make_unique<llm_build_lfm2<false>>(*this, params); |
8611 | 0 | } |
8612 | 0 | } break; |
8613 | 0 | case LLM_ARCH_SMALLTHINKER: |
8614 | 0 | { |
8615 | 0 | if (hparams.swa_type == LLAMA_SWA_TYPE_STANDARD) { |
8616 | 0 | llm = std::make_unique<llm_build_smallthinker<true>> (*this, params); |
8617 | 0 | } else { |
8618 | 0 | llm = std::make_unique<llm_build_smallthinker<false>>(*this, params); |
8619 | 0 | } |
8620 | 0 | } break; |
8621 | 0 | case LLM_ARCH_GROVEMOE: |
8622 | 0 | { |
8623 | 0 | llm = std::make_unique<llm_build_grovemoe>(*this, params); |
8624 | 0 | } break; |
8625 | 0 | case LLM_ARCH_APERTUS: |
8626 | 0 | { |
8627 | 0 | llm = std::make_unique<llm_build_apertus>(*this, params); |
8628 | 0 | } break; |
8629 | 0 | case LLM_ARCH_MINIMAX_M2: |
8630 | 0 | { |
8631 | 0 | llm = std::make_unique<llm_build_minimax_m2>(*this, params); |
8632 | 0 | } break; |
8633 | 0 | case LLM_ARCH_COGVLM: |
8634 | 0 | { |
8635 | 0 | llm = std::make_unique<llm_build_cogvlm>(*this, params); |
8636 | 0 | } break; |
8637 | 0 | case LLM_ARCH_PANGU_EMBED: |
8638 | 0 | { |
8639 | 0 | llm = std::make_unique<llm_build_pangu_embedded>(*this, params); |
8640 | 0 | } break; |
8641 | 0 | case LLM_ARCH_QWEN3NEXT: |
8642 | 0 | { |
8643 | 0 | llm = std::make_unique<llm_build_qwen3next>(*this, params); |
8644 | 0 | } break; |
8645 | 0 | case LLM_ARCH_QWEN35: |
8646 | 0 | { |
8647 | 0 | llm = std::make_unique<llm_build_qwen35>(*this, params); |
8648 | 0 | } break; |
8649 | 0 | case LLM_ARCH_QWEN35MOE: |
8650 | 0 | { |
8651 | 0 | llm = std::make_unique<llm_build_qwen35moe>(*this, params); |
8652 | 0 | } break; |
8653 | 0 | case LLM_ARCH_MISTRAL3: |
8654 | 0 | { |
8655 | 0 | llm = std::make_unique<llm_build_mistral3>(*this, params); |
8656 | 0 | } break; |
8657 | 0 | case LLM_ARCH_MIMO2: |
8658 | 0 | { |
8659 | 0 | llm = std::make_unique<llm_build_mimo2_iswa>(*this, params); |
8660 | 0 | } break; |
8661 | 0 | case LLM_ARCH_KIMI_LINEAR: |
8662 | 0 | { |
8663 | 0 | llm = std::make_unique<llm_build_kimi_linear>(*this, params); |
8664 | 0 | } break; |
8665 | 0 | case LLM_ARCH_STEP35: |
8666 | 0 | { |
8667 | 0 | llm = std::make_unique<llm_build_step35_iswa>(*this, params); |
8668 | 0 | } break; |
8669 | 0 | default: |
8670 | 0 | GGML_ABORT("fatal error"); |
8671 | 0 | } |
8672 | | |
8673 | | // add on pooling layer |
8674 | 0 | llm->build_pooling(cls, cls_b, cls_out, cls_out_b, cls_norm); |
8675 | | |
8676 | | // add backend sampling layers (if any) |
8677 | 0 | llm->build_sampling(); |
8678 | | |
8679 | | // if the gguf model was converted with --sentence-transformers-dense-modules |
8680 | | // there will be two additional dense projection layers |
8681 | | // dense linear projections are applied after pooling |
8682 | | // TODO: move reranking logic here and generalize |
8683 | 0 | llm->build_dense_out(dense_2_out_layers, dense_2_out_layers_b, dense_3_out_layers); |
8684 | |
|
8685 | 0 | llm->res->set_outputs(); |
8686 | |
|
8687 | 0 | return llm->res->get_gf(); |
8688 | 0 | } |
8689 | | |
8690 | | |
8691 | | // |
8692 | | // interface implementation |
8693 | | // |
8694 | | |
8695 | 0 | llama_model_params llama_model_default_params() { |
8696 | 0 | llama_model_params result = { |
8697 | 0 | /*.devices =*/ nullptr, |
8698 | 0 | /*.tensor_buft_overrides =*/ nullptr, |
8699 | 0 | /*.n_gpu_layers =*/ -1, |
8700 | 0 | /*.split_mode =*/ LLAMA_SPLIT_MODE_LAYER, |
8701 | 0 | /*.main_gpu =*/ 0, |
8702 | 0 | /*.tensor_split =*/ nullptr, |
8703 | 0 | /*.progress_callback =*/ nullptr, |
8704 | 0 | /*.progress_callback_user_data =*/ nullptr, |
8705 | 0 | /*.kv_overrides =*/ nullptr, |
8706 | 0 | /*.vocab_only =*/ false, |
8707 | 0 | /*.use_mmap =*/ true, |
8708 | 0 | /*.use_direct_io =*/ false, |
8709 | 0 | /*.use_mlock =*/ false, |
8710 | 0 | /*.check_tensors =*/ false, |
8711 | 0 | /*.use_extra_bufts =*/ true, |
8712 | 0 | /*.no_host =*/ false, |
8713 | 0 | /*.no_alloc =*/ false, |
8714 | 0 | }; |
8715 | |
|
8716 | 0 | return result; |
8717 | 0 | } |
8718 | | |
8719 | 0 | const llama_vocab * llama_model_get_vocab(const llama_model * model) { |
8720 | 0 | return &model->vocab; |
8721 | 0 | } |
8722 | | |
8723 | 0 | void llama_free_model(llama_model * model) { |
8724 | 0 | llama_model_free(model); |
8725 | 0 | } |
8726 | | |
8727 | 0 | void llama_model_free(llama_model * model) { |
8728 | 0 | delete model; |
8729 | 0 | } |
8730 | | |
8731 | 0 | int32_t llama_model_n_ctx_train(const llama_model * model) { |
8732 | 0 | return model->hparams.n_ctx_train; |
8733 | 0 | } |
8734 | | |
8735 | 0 | int32_t llama_model_n_embd(const llama_model * model) { |
8736 | 0 | return model->hparams.n_embd; |
8737 | 0 | } |
8738 | | |
8739 | 0 | int32_t llama_model_n_embd_inp(const llama_model * model) { |
8740 | 0 | return model->hparams.n_embd_inp(); |
8741 | 0 | } |
8742 | | |
8743 | 0 | int32_t llama_model_n_embd_out(const llama_model * model) { |
8744 | 0 | return model->hparams.n_embd_out(); |
8745 | 0 | } |
8746 | | |
8747 | 0 | int32_t llama_model_n_layer(const llama_model * model) { |
8748 | 0 | return model->hparams.n_layer; |
8749 | 0 | } |
8750 | | |
8751 | 0 | int32_t llama_model_n_head(const llama_model * model) { |
8752 | 0 | return model->hparams.n_head(); |
8753 | 0 | } |
8754 | | |
8755 | 0 | int32_t llama_model_n_head_kv(const llama_model * model) { |
8756 | 0 | return model->hparams.n_head_kv(); |
8757 | 0 | } |
8758 | | |
8759 | 0 | int32_t llama_model_n_swa(const llama_model * model) { |
8760 | 0 | return model->hparams.n_swa; |
8761 | 0 | } |
8762 | | |
8763 | 0 | uint32_t llama_model_n_cls_out(const struct llama_model * model) { |
8764 | 0 | return model->hparams.n_cls_out; |
8765 | 0 | } |
8766 | | |
8767 | 0 | const char * llama_model_cls_label(const struct llama_model * model, uint32_t i) { |
8768 | 0 | if (i < model->classifier_labels.size()) { |
8769 | 0 | return model->classifier_labels[i].c_str(); |
8770 | 0 | } |
8771 | | |
8772 | 0 | return nullptr; |
8773 | 0 | } |
8774 | | |
8775 | | // deprecated |
8776 | 0 | int32_t llama_n_ctx_train(const llama_model * model) { |
8777 | 0 | return llama_model_n_ctx_train(model); |
8778 | 0 | } |
8779 | | |
8780 | | // deprecated |
8781 | 0 | int32_t llama_n_embd(const llama_model * model) { |
8782 | 0 | return llama_model_n_embd(model); |
8783 | 0 | } |
8784 | | |
8785 | | // deprecated |
8786 | 0 | int32_t llama_n_layer(const llama_model * model) { |
8787 | 0 | return llama_model_n_layer(model); |
8788 | 0 | } |
8789 | | |
8790 | | // deprecated |
8791 | 0 | int32_t llama_n_head(const llama_model * model) { |
8792 | 0 | return llama_model_n_head(model); |
8793 | 0 | } |
8794 | | |
8795 | 0 | llama_rope_type llama_model_rope_type(const llama_model * model) { |
8796 | 0 | switch (model->arch) { |
8797 | | // these models do not use RoPE |
8798 | 0 | case LLM_ARCH_CLIP: |
8799 | 0 | case LLM_ARCH_GPT2: |
8800 | 0 | case LLM_ARCH_GPTJ: |
8801 | 0 | case LLM_ARCH_MPT: |
8802 | 0 | case LLM_ARCH_REFACT: |
8803 | 0 | case LLM_ARCH_BLOOM: |
8804 | 0 | case LLM_ARCH_MAMBA: |
8805 | 0 | case LLM_ARCH_MAMBA2: |
8806 | 0 | case LLM_ARCH_JAMBA: |
8807 | 0 | case LLM_ARCH_JINA_BERT_V2: |
8808 | 0 | case LLM_ARCH_T5: |
8809 | 0 | case LLM_ARCH_T5ENCODER: |
8810 | 0 | case LLM_ARCH_JAIS: |
8811 | 0 | case LLM_ARCH_RWKV6: |
8812 | 0 | case LLM_ARCH_RWKV6QWEN2: |
8813 | 0 | case LLM_ARCH_RWKV7: |
8814 | 0 | case LLM_ARCH_ARWKV7: |
8815 | 0 | case LLM_ARCH_WAVTOKENIZER_DEC: |
8816 | 0 | case LLM_ARCH_NEMOTRON_H: |
8817 | 0 | case LLM_ARCH_NEMOTRON_H_MOE: |
8818 | 0 | case LLM_ARCH_KIMI_LINEAR: |
8819 | 0 | return LLAMA_ROPE_TYPE_NONE; |
8820 | | |
8821 | | // use what we call a normal RoPE, operating on pairs of consecutive head values |
8822 | 0 | case LLM_ARCH_LLAMA: |
8823 | 0 | case LLM_ARCH_LLADA: |
8824 | 0 | case LLM_ARCH_LLAMA4: |
8825 | 0 | case LLM_ARCH_DECI: |
8826 | 0 | case LLM_ARCH_BAICHUAN: |
8827 | 0 | case LLM_ARCH_STARCODER: |
8828 | 0 | case LLM_ARCH_INTERNLM2: |
8829 | 0 | case LLM_ARCH_MINICPM: |
8830 | 0 | case LLM_ARCH_XVERSE: |
8831 | 0 | case LLM_ARCH_COMMAND_R: |
8832 | 0 | case LLM_ARCH_COHERE2: |
8833 | 0 | case LLM_ARCH_OLMO: |
8834 | 0 | case LLM_ARCH_ARCTIC: |
8835 | 0 | case LLM_ARCH_DEEPSEEK: |
8836 | 0 | case LLM_ARCH_DEEPSEEK2: |
8837 | 0 | case LLM_ARCH_PLM: |
8838 | 0 | case LLM_ARCH_CHATGLM: |
8839 | 0 | case LLM_ARCH_GRANITE: |
8840 | 0 | case LLM_ARCH_GRANITE_MOE: |
8841 | 0 | case LLM_ARCH_GRANITE_HYBRID: |
8842 | 0 | case LLM_ARCH_CHAMELEON: |
8843 | 0 | case LLM_ARCH_BAILINGMOE: |
8844 | 0 | case LLM_ARCH_NEO_BERT: |
8845 | 0 | case LLM_ARCH_SMOLLM3: |
8846 | 0 | case LLM_ARCH_ARCEE: |
8847 | 0 | case LLM_ARCH_ERNIE4_5: |
8848 | 0 | case LLM_ARCH_ERNIE4_5_MOE: |
8849 | 0 | case LLM_ARCH_MISTRAL3: |
8850 | 0 | case LLM_ARCH_MISTRAL4: |
8851 | 0 | case LLM_ARCH_LLAMA_EMBED: |
8852 | 0 | case LLM_ARCH_MAINCODER: |
8853 | 0 | case LLM_ARCH_GLM_DSA: |
8854 | 0 | return LLAMA_ROPE_TYPE_NORM; |
8855 | | |
8856 | | // the pairs of head values are offset by n_rot/2 |
8857 | 0 | case LLM_ARCH_FALCON: |
8858 | 0 | case LLM_ARCH_FALCON_H1: |
8859 | 0 | case LLM_ARCH_GROK: |
8860 | 0 | case LLM_ARCH_DBRX: |
8861 | 0 | case LLM_ARCH_BERT: |
8862 | 0 | case LLM_ARCH_JINA_BERT_V3: |
8863 | 0 | case LLM_ARCH_MODERN_BERT: |
8864 | 0 | case LLM_ARCH_NOMIC_BERT: |
8865 | 0 | case LLM_ARCH_NOMIC_BERT_MOE: |
8866 | 0 | case LLM_ARCH_EUROBERT: |
8867 | 0 | case LLM_ARCH_STABLELM: |
8868 | 0 | case LLM_ARCH_BITNET: |
8869 | 0 | case LLM_ARCH_QWEN: |
8870 | 0 | case LLM_ARCH_QWEN2: |
8871 | 0 | case LLM_ARCH_DREAM: |
8872 | 0 | case LLM_ARCH_QWEN2MOE: |
8873 | 0 | case LLM_ARCH_QWEN3: |
8874 | 0 | case LLM_ARCH_QWEN3MOE: |
8875 | 0 | case LLM_ARCH_LLADA_MOE: |
8876 | 0 | case LLM_ARCH_RND1: |
8877 | 0 | case LLM_ARCH_OLMO2: |
8878 | 0 | case LLM_ARCH_OLMOE: |
8879 | 0 | case LLM_ARCH_PHI2: |
8880 | 0 | case LLM_ARCH_PHI3: |
8881 | 0 | case LLM_ARCH_PHIMOE: |
8882 | 0 | case LLM_ARCH_PLAMO: |
8883 | 0 | case LLM_ARCH_PLAMO2: |
8884 | 0 | case LLM_ARCH_PLAMO3: |
8885 | 0 | case LLM_ARCH_GEMMA: |
8886 | 0 | case LLM_ARCH_GEMMA2: |
8887 | 0 | case LLM_ARCH_GEMMA3: |
8888 | 0 | case LLM_ARCH_GEMMA3N: |
8889 | 0 | case LLM_ARCH_GEMMA_EMBEDDING: |
8890 | 0 | case LLM_ARCH_STARCODER2: |
8891 | 0 | case LLM_ARCH_OPENELM: |
8892 | 0 | case LLM_ARCH_GPTNEOX: |
8893 | 0 | case LLM_ARCH_CODESHELL: |
8894 | 0 | case LLM_ARCH_ORION: |
8895 | 0 | case LLM_ARCH_NEMOTRON: |
8896 | 0 | case LLM_ARCH_EXAONE: |
8897 | 0 | case LLM_ARCH_EXAONE4: |
8898 | 0 | case LLM_ARCH_EXAONE_MOE: |
8899 | 0 | case LLM_ARCH_MINICPM3: |
8900 | 0 | case LLM_ARCH_BAILINGMOE2: |
8901 | 0 | case LLM_ARCH_DOTS1: |
8902 | 0 | case LLM_ARCH_HUNYUAN_MOE: |
8903 | 0 | case LLM_ARCH_JAIS2: |
8904 | 0 | case LLM_ARCH_OPENAI_MOE: |
8905 | 0 | case LLM_ARCH_HUNYUAN_DENSE: |
8906 | 0 | case LLM_ARCH_LFM2: |
8907 | 0 | case LLM_ARCH_LFM2MOE: |
8908 | 0 | case LLM_ARCH_SMALLTHINKER: |
8909 | 0 | case LLM_ARCH_SEED_OSS: |
8910 | 0 | case LLM_ARCH_GROVEMOE: |
8911 | 0 | case LLM_ARCH_APERTUS: |
8912 | 0 | case LLM_ARCH_MINIMAX_M2: |
8913 | 0 | case LLM_ARCH_COGVLM: |
8914 | 0 | case LLM_ARCH_PANGU_EMBED: |
8915 | 0 | case LLM_ARCH_AFMOE: |
8916 | 0 | case LLM_ARCH_QWEN3NEXT: |
8917 | 0 | case LLM_ARCH_MIMO2: |
8918 | 0 | case LLM_ARCH_STEP35: |
8919 | 0 | return LLAMA_ROPE_TYPE_NEOX; |
8920 | | |
8921 | 0 | case LLM_ARCH_QWEN2VL: |
8922 | 0 | case LLM_ARCH_PADDLEOCR: |
8923 | 0 | return LLAMA_ROPE_TYPE_MROPE; |
8924 | 0 | case LLM_ARCH_QWEN3VL: |
8925 | 0 | case LLM_ARCH_QWEN3VLMOE: |
8926 | 0 | case LLM_ARCH_QWEN35: |
8927 | 0 | case LLM_ARCH_QWEN35MOE: |
8928 | 0 | return LLAMA_ROPE_TYPE_IMROPE; |
8929 | | |
8930 | 0 | case LLM_ARCH_GLM4: |
8931 | 0 | return model->hparams.use_mrope() ? LLAMA_ROPE_TYPE_MROPE : LLAMA_ROPE_TYPE_NORM; |
8932 | 0 | case LLM_ARCH_GLM4_MOE: |
8933 | 0 | return model->hparams.use_mrope() ? LLAMA_ROPE_TYPE_MROPE : LLAMA_ROPE_TYPE_NEOX; |
8934 | | |
8935 | | // all model arches should be listed explicitly here |
8936 | 0 | case LLM_ARCH_UNKNOWN: |
8937 | 0 | GGML_ABORT("unknown architecture"); |
8938 | 0 | } |
8939 | | |
8940 | 0 | return LLAMA_ROPE_TYPE_NONE; |
8941 | 0 | } |
8942 | | |
8943 | 0 | float llama_model_rope_freq_scale_train(const llama_model * model) { |
8944 | 0 | return model->hparams.rope_freq_scale_train; |
8945 | 0 | } |
8946 | | |
8947 | 0 | int32_t llama_model_meta_val_str(const llama_model * model, const char * key, char * buf, size_t buf_size) { |
8948 | 0 | const auto & it = model->gguf_kv.find(key); |
8949 | 0 | if (it == model->gguf_kv.end()) { |
8950 | 0 | if (buf_size > 0) { |
8951 | 0 | buf[0] = '\0'; |
8952 | 0 | } |
8953 | 0 | return -1; |
8954 | 0 | } |
8955 | 0 | return snprintf(buf, buf_size, "%s", it->second.c_str()); |
8956 | 0 | } |
8957 | | |
8958 | 0 | int32_t llama_model_meta_count(const llama_model * model) { |
8959 | 0 | return (int)model->gguf_kv.size(); |
8960 | 0 | } |
8961 | | |
8962 | 0 | const char * llama_model_meta_key_str(llama_model_meta_key key) { |
8963 | 0 | switch (key) { |
8964 | 0 | case LLAMA_MODEL_META_KEY_SAMPLING_SEQUENCE: return "general.sampling.sequence"; |
8965 | 0 | case LLAMA_MODEL_META_KEY_SAMPLING_TOP_K: return "general.sampling.top_k"; |
8966 | 0 | case LLAMA_MODEL_META_KEY_SAMPLING_TOP_P: return "general.sampling.top_p"; |
8967 | 0 | case LLAMA_MODEL_META_KEY_SAMPLING_MIN_P: return "general.sampling.min_p"; |
8968 | 0 | case LLAMA_MODEL_META_KEY_SAMPLING_XTC_PROBABILITY: return "general.sampling.xtc_probability"; |
8969 | 0 | case LLAMA_MODEL_META_KEY_SAMPLING_XTC_THRESHOLD: return "general.sampling.xtc_threshold"; |
8970 | 0 | case LLAMA_MODEL_META_KEY_SAMPLING_TEMP: return "general.sampling.temp"; |
8971 | 0 | case LLAMA_MODEL_META_KEY_SAMPLING_PENALTY_LAST_N: return "general.sampling.penalty_last_n"; |
8972 | 0 | case LLAMA_MODEL_META_KEY_SAMPLING_PENALTY_REPEAT: return "general.sampling.penalty_repeat"; |
8973 | 0 | case LLAMA_MODEL_META_KEY_SAMPLING_MIROSTAT: return "general.sampling.mirostat"; |
8974 | 0 | case LLAMA_MODEL_META_KEY_SAMPLING_MIROSTAT_TAU: return "general.sampling.mirostat_tau"; |
8975 | 0 | case LLAMA_MODEL_META_KEY_SAMPLING_MIROSTAT_ETA: return "general.sampling.mirostat_eta"; |
8976 | 0 | default: return nullptr; |
8977 | 0 | } |
8978 | 0 | } |
8979 | | |
8980 | 0 | int32_t llama_model_meta_key_by_index(const llama_model * model, int i, char * buf, size_t buf_size) { |
8981 | 0 | if (i < 0 || i >= (int)model->gguf_kv.size()) { |
8982 | 0 | if (buf_size > 0) { |
8983 | 0 | buf[0] = '\0'; |
8984 | 0 | } |
8985 | 0 | return -1; |
8986 | 0 | } |
8987 | 0 | auto it = model->gguf_kv.begin(); |
8988 | 0 | std::advance(it, i); |
8989 | 0 | return snprintf(buf, buf_size, "%s", it->first.c_str()); |
8990 | 0 | } |
8991 | | |
8992 | 0 | int32_t llama_model_meta_val_str_by_index(const llama_model * model, int32_t i, char * buf, size_t buf_size) { |
8993 | 0 | if (i < 0 || i >= (int)model->gguf_kv.size()) { |
8994 | 0 | if (buf_size > 0) { |
8995 | 0 | buf[0] = '\0'; |
8996 | 0 | } |
8997 | 0 | return -1; |
8998 | 0 | } |
8999 | 0 | auto it = model->gguf_kv.begin(); |
9000 | 0 | std::advance(it, i); |
9001 | 0 | return snprintf(buf, buf_size, "%s", it->second.c_str()); |
9002 | 0 | } |
9003 | | |
9004 | 0 | int32_t llama_model_desc(const llama_model * model, char * buf, size_t buf_size) { |
9005 | 0 | return snprintf(buf, buf_size, "%s", model->desc().c_str()); |
9006 | 0 | } |
9007 | | |
9008 | 0 | uint64_t llama_model_size(const llama_model * model) { |
9009 | 0 | return model->size(); |
9010 | 0 | } |
9011 | | |
9012 | 0 | const char * llama_model_chat_template(const llama_model * model, const char * name) { |
9013 | 0 | const auto key = name ? LLM_KV(model->arch, name)(LLM_KV_TOKENIZER_CHAT_TEMPLATE) |
9014 | 0 | : LLM_KV(model->arch)(LLM_KV_TOKENIZER_CHAT_TEMPLATE); |
9015 | 0 | const auto & it = model->gguf_kv.find(key); |
9016 | 0 | if (it == model->gguf_kv.end()) { |
9017 | | // one-off fix for very popular models (so we are not flooded with issues) |
9018 | | // do not extend this list unless absolutely necessary |
9019 | | // Mistral-Small-2503 does not have built-in chat template |
9020 | 0 | llama_vocab_pre_type pre_type = model->vocab.get_pre_type(); |
9021 | 0 | if (!name && pre_type == LLAMA_VOCAB_PRE_TYPE_TEKKEN && model->layers.size() == 40) { |
9022 | 0 | return "mistral-v7-tekken"; |
9023 | 0 | } |
9024 | | |
9025 | 0 | return nullptr; |
9026 | 0 | } |
9027 | | |
9028 | 0 | return it->second.c_str(); |
9029 | 0 | } |
9030 | | |
9031 | 0 | uint64_t llama_model_n_params(const llama_model * model) { |
9032 | 0 | return model->n_elements(); |
9033 | 0 | } |
9034 | | |
9035 | 0 | bool llama_model_has_encoder(const llama_model * model) { |
9036 | 0 | switch (model->arch) { |
9037 | 0 | case LLM_ARCH_T5: return true; |
9038 | 0 | case LLM_ARCH_T5ENCODER: return true; |
9039 | 0 | default: return false; |
9040 | 0 | } |
9041 | 0 | } |
9042 | | |
9043 | 0 | bool llama_model_has_decoder(const llama_model * model) { |
9044 | 0 | switch (model->arch) { |
9045 | 0 | case LLM_ARCH_T5ENCODER: return false; |
9046 | 0 | default: return true; |
9047 | 0 | } |
9048 | 0 | } |
9049 | | |
9050 | 0 | llama_token llama_model_decoder_start_token(const llama_model * model) { |
9051 | 0 | return model->hparams.dec_start_token_id; |
9052 | 0 | } |
9053 | | |
9054 | 0 | bool llama_model_is_recurrent(const llama_model * model) { |
9055 | 0 | return llm_arch_is_recurrent(model->arch); |
9056 | 0 | } |
9057 | | |
9058 | 0 | bool llama_model_is_hybrid(const llama_model * model) { |
9059 | 0 | return llm_arch_is_hybrid(model->arch); |
9060 | 0 | } |
9061 | | |
9062 | 0 | bool llama_model_is_diffusion(const llama_model * model) { |
9063 | 0 | return llm_arch_is_diffusion(model->arch); |
9064 | 0 | } |
9065 | | |
9066 | 0 | const std::vector<std::pair<std::string, ggml_tensor *>> & llama_internal_get_tensor_map(const llama_model * model) { |
9067 | 0 | return model->tensors_by_name; |
9068 | 0 | } |