/src/llama.cpp/src/llama-hparams.h
Line | Count | Source |
1 | | #pragma once |
2 | | |
3 | | #include "llama.h" |
4 | | |
5 | | #include <array> |
6 | | #include <cassert> |
7 | | |
8 | | // bump if necessary |
9 | 0 | #define LLAMA_MAX_LAYERS 512 |
10 | | #define LLAMA_MAX_EXPERTS 512 // Qwen3 Next |
11 | | |
12 | | enum llama_expert_gating_func_type { |
13 | | LLAMA_EXPERT_GATING_FUNC_TYPE_NONE = 0, |
14 | | LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX = 1, |
15 | | LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID = 2, |
16 | | LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX_WEIGHT = 3, // applied to the router weights instead of the logits |
17 | | LLAMA_EXPERT_GATING_FUNC_TYPE_SQRT_SOFTPLUS = 4, |
18 | | }; |
19 | | |
20 | | enum llama_swa_type { |
21 | | LLAMA_SWA_TYPE_NONE = 0, |
22 | | LLAMA_SWA_TYPE_STANDARD = 1, |
23 | | LLAMA_SWA_TYPE_CHUNKED = 2, |
24 | | LLAMA_SWA_TYPE_SYMMETRIC = 3, |
25 | | }; |
26 | | |
27 | | // forward declaration; full definition in llama-graph.h |
28 | | enum llm_ffn_op_type : int; |
29 | | |
30 | | struct llama_hparams_posnet { |
31 | | uint32_t n_embd; |
32 | | uint32_t n_layer; |
33 | | }; |
34 | | |
35 | | struct llama_hparams_convnext { |
36 | | uint32_t n_embd; |
37 | | uint32_t n_layer; |
38 | | }; |
39 | | |
40 | | struct llama_hparams { |
41 | | // note: use the `_impl` suffix to avoid name conflict between members and getters |
42 | | // for example: n_embd_out() vs n_embd_out_impl |
43 | | |
44 | | bool vocab_only; |
45 | | bool no_alloc; |
46 | | bool rope_finetuned; |
47 | | bool use_par_res; |
48 | | bool swin_norm; |
49 | | bool norm_before_residual = false; |
50 | | |
51 | | uint32_t n_ctx_train; // context size the model was trained on |
52 | | uint32_t n_embd; |
53 | | uint32_t n_layer_all; |
54 | | uint32_t n_layer_nextn = 0; |
55 | | uint32_t n_expert = 0; |
56 | | uint32_t n_expert_used = 0; |
57 | | uint32_t n_rel_attn_bkts = 0; |
58 | | |
59 | | // TODO: this needs to be reworked |
60 | | int32_t n_layer_kv_from_start = -1; // if non-negative, the first n_layer_kv_from_start layers have KV cache |
61 | | |
62 | | // different head size for full_attention and SWA layers |
63 | | uint32_t n_embd_head_k_full; // dimension of keys (d_k). d_q is assumed to be the same, but there are n_head q heads, and only n_head_kv k-v heads |
64 | | uint32_t n_embd_head_v_full; // dimension of values (d_v) aka n_embd_head |
65 | | uint32_t n_embd_head_k_swa; |
66 | | uint32_t n_embd_head_v_swa; |
67 | | |
68 | | // different RoPE dimensions for full_attention and SWA layers |
69 | | uint32_t n_rot_full; |
70 | | uint32_t n_rot_swa; |
71 | | |
72 | | // note: deepseek2 using MLA converts into MQA with larger heads, then decompresses to MHA |
73 | | uint32_t n_embd_head_k_mla_impl = 0; |
74 | | uint32_t n_embd_head_v_mla_impl = 0; |
75 | | |
76 | | // for WavTokenizer |
77 | | struct llama_hparams_posnet posnet; |
78 | | struct llama_hparams_convnext convnext; |
79 | | |
80 | | uint32_t n_shortconv_l_cache = 0; |
81 | | |
82 | | std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_arr; |
83 | | std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_kv_arr; |
84 | | std::array<uint32_t, LLAMA_MAX_LAYERS> n_ff_arr; |
85 | | |
86 | | uint32_t n_layer_dense_lead = 0; |
87 | | uint32_t n_lora_q = 0; |
88 | | uint32_t n_lora_kv = 0; |
89 | | uint32_t n_ff_exp = 0; |
90 | | uint32_t n_ff_shexp = 0; |
91 | | uint32_t n_ff_chexp = 0; |
92 | | uint32_t n_expert_shared = 0; |
93 | | uint32_t n_norm_groups = 0; |
94 | | uint32_t n_expert_groups = 0; |
95 | | uint32_t n_group_used = 0; |
96 | | uint32_t n_group_experts = 0; |
97 | | |
98 | | float expert_group_scale = 0.05f; |
99 | | float expert_weights_scale = 0.0f; |
100 | | bool expert_weights_norm = false; |
101 | | uint32_t expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_NONE; |
102 | | uint32_t moe_every_n_layers = 0; |
103 | | uint32_t moe_latent_size = 0; |
104 | | |
105 | | float f_norm_eps; |
106 | | float f_norm_rms_eps; |
107 | | float f_norm_group_eps; |
108 | | |
109 | | float f_attn_logit_softcapping = 50.0f; |
110 | | float f_router_logit_softcapping = 30.0f; |
111 | | float f_final_logit_softcapping = 30.0f; |
112 | | |
113 | | // for RWKV |
114 | | uint32_t rescale_every_n_layers = 0; |
115 | | uint32_t time_mix_extra_dim = 0; |
116 | | uint32_t time_decay_extra_dim = 0; |
117 | | uint32_t wkv_head_size = 0; |
118 | | uint32_t token_shift_count = 2; |
119 | | uint32_t n_lora_decay = 0; |
120 | | uint32_t n_lora_iclr = 0; |
121 | | uint32_t n_lora_value_res_mix = 0; |
122 | | uint32_t n_lora_gate = 0; |
123 | | |
124 | | float rope_attn_factor = 1.0f; |
125 | | float rope_freq_base_train; |
126 | | float rope_freq_base_train_swa = 10000.0f; |
127 | | float rope_freq_scale_train; |
128 | | float rope_freq_scale_train_swa = 1.0f; |
129 | | float rope_scaling_alpha = 0.0f; // NTK-aware alpha for XDRoPE |
130 | | |
131 | | uint32_t n_ctx_orig_yarn; |
132 | | float rope_yarn_log_mul = 0.0f; |
133 | | |
134 | | float yarn_ext_factor = -1.0f; |
135 | | float yarn_attn_factor = 1.0f; |
136 | | float yarn_beta_fast = 32.0f; |
137 | | float yarn_beta_slow = 1.0f; |
138 | | |
139 | | std::array<int, 4> rope_sections; |
140 | | |
141 | | // Sliding Window Attention (SWA) |
142 | | llama_swa_type swa_type = LLAMA_SWA_TYPE_NONE; |
143 | | // the size of the sliding window (0 - no SWA) |
144 | | uint32_t n_swa = 0; |
145 | | |
146 | | // if is_swa_impl[il] == 1, then layer il is SWA |
147 | | // if is_swa_impl[il] == 0, then layer il is dense (i.e. non-SWA) |
148 | | // by default, all layers are dense |
149 | | // note: using uint32_t type for compatibility reason |
150 | | std::array<uint32_t, LLAMA_MAX_LAYERS> is_swa_impl; |
151 | | |
152 | | // for hybrid state space models |
153 | | std::array<uint32_t, LLAMA_MAX_LAYERS> is_recr_impl; |
154 | | |
155 | | // for State Space Models |
156 | | uint32_t ssm_d_conv = 0; |
157 | | uint32_t ssm_d_inner = 0; |
158 | | uint32_t ssm_d_state = 0; |
159 | | uint32_t ssm_dt_rank = 0; |
160 | | uint32_t ssm_n_group = 0; |
161 | | |
162 | | // for Kimi Linear KDA |
163 | | uint32_t n_embd_head_kda = 0; |
164 | | |
165 | | bool ssm_dt_b_c_rms = false; |
166 | | |
167 | | float f_clamp_kqv = 0.0f; |
168 | | float f_max_alibi_bias = 0.0f; |
169 | | float f_logit_scale = 0.0f; |
170 | | |
171 | | // Additional scale factors (Granite/Granite MoE) |
172 | | float f_residual_scale = 0.0f; |
173 | | float f_embedding_scale = 0.0f; |
174 | | float f_attention_scale = 0.0f; |
175 | | |
176 | | // grok-2 |
177 | | float f_attn_out_scale = 0.0f; |
178 | | uint32_t attn_temp_length = 0; |
179 | | |
180 | | float f_attn_value_scale = 0.0f; |
181 | | |
182 | | bool causal_attn = true; |
183 | | bool use_alibi = false; |
184 | | bool attn_soft_cap = false; |
185 | | bool use_kq_norm = false; |
186 | | |
187 | | // for Classifiers |
188 | | uint32_t n_cls_out = 1; |
189 | | |
190 | | // input embedding dimension (0 = use n_embd) |
191 | | uint32_t n_embd_inp_impl = 0; |
192 | | |
193 | | // encoder input embedding dimension (0 = use n_embd_inp()) |
194 | | // e.g. the eagle3 encoder fuses target_layers * target_hidden features |
195 | | uint32_t n_embd_inp_enc_impl = 0; |
196 | | |
197 | | // output embedding dimension (0 = use n_embd) |
198 | | uint32_t n_embd_out_impl = 0; |
199 | | |
200 | | // llama4 smallthinker |
201 | | uint32_t n_moe_layer_step = 0; |
202 | | uint32_t n_no_rope_layer_step = 4; |
203 | | uint32_t n_attn_temp_floor_scale = 0; |
204 | | float f_attn_temp_scale = 0.0f; |
205 | | float f_attn_temp_offset = 0.0f; // offset position index |
206 | | |
207 | | // gemma3n altup |
208 | | uint32_t n_altup = 4; // altup_num_inputs |
209 | | uint32_t i_altup_act = 0; // altup_active_idx |
210 | | uint32_t laurel_rank = 64; |
211 | | uint32_t n_embd_altup = 256; |
212 | | |
213 | | // needed for sentence-transformers dense layers |
214 | | uint32_t dense_2_feat_in = 0; // in_features of the 2_Dense |
215 | | uint32_t dense_2_feat_out = 0; // out_features of the 2_Dense |
216 | | uint32_t dense_3_feat_in = 0; // in_features of the 3_Dense |
217 | | uint32_t dense_3_feat_out = 0; // out_features of the 3_Dense |
218 | | |
219 | | // xIELU |
220 | | std::array<float, LLAMA_MAX_LAYERS> xielu_alpha_n; |
221 | | std::array<float, LLAMA_MAX_LAYERS> xielu_alpha_p; |
222 | | std::array<float, LLAMA_MAX_LAYERS> xielu_beta; |
223 | | std::array<float, LLAMA_MAX_LAYERS> xielu_eps; |
224 | | |
225 | | // DSA (deepseek sparse attention) |
226 | | uint32_t indexer_n_head = 0; |
227 | | uint32_t indexer_head_size = 0; |
228 | | uint32_t indexer_top_k = 0; |
229 | | |
230 | | // DeepSeek-V4 |
231 | | uint32_t dsv4_o_group_count = 0; |
232 | | uint32_t dsv4_o_lora_rank = 0; |
233 | | uint32_t dsv4_hc_mult = 0; |
234 | | uint32_t dsv4_hc_sinkhorn_iters = 0; |
235 | | uint32_t dsv4_hash_layer_count = 0; |
236 | | float dsv4_compress_rope_base = 0.0f; |
237 | | float dsv4_hc_eps = 0.0f; |
238 | | std::array<uint32_t, LLAMA_MAX_LAYERS> dsv4_compress_ratios; |
239 | | |
240 | | // qwen3vl deepstack |
241 | | // When parsed from GGUF, this implies the first N layers consume the first |
242 | | // N deepstack embeddings. Use deepstack_mapping_arr if you need a more |
243 | | // complex mapping. If using deepstack_mapping_arr, also make sure to set |
244 | | // n_deepstack_layers to the number of unique deepstack layers so that |
245 | | // n_embd_imp is accurate (see granite.cpp). |
246 | | // TODO: can be expressed via the `new n_embd_inp_impl` and remove this param |
247 | | uint32_t n_deepstack_layers = 0; |
248 | | |
249 | | // deepstack layer array (Granite4 Vision) |
250 | | // -1 => no deepstack |
251 | | // >=0 => input embedding index for deepstack injection |
252 | | std::array<int32_t, LLAMA_MAX_LAYERS> deepstack_mapping_arr; |
253 | | |
254 | | // gemma4 per-layer embedding |
255 | | uint32_t n_embd_per_layer = 0; |
256 | | |
257 | | // needed by encoder-decoder models (e.g. T5, FLAN-T5) |
258 | | // ref: https://github.com/ggml-org/llama.cpp/pull/8141 |
259 | | llama_token dec_start_token_id = LLAMA_TOKEN_NULL; |
260 | | uint32_t dec_n_layer = 0; |
261 | | |
262 | | enum llama_pooling_type pooling_type = LLAMA_POOLING_TYPE_NONE; |
263 | | enum llama_rope_type rope_type = LLAMA_ROPE_TYPE_NONE; |
264 | | enum llama_rope_scaling_type rope_scaling_type_train = LLAMA_ROPE_SCALING_TYPE_NONE; |
265 | | |
266 | | |
267 | | // Resolved FFN gated activation flavor for archs that read |
268 | | // `<arch>.hidden_activation` from the GGUF (e.g. ModernBert derivatives). |
269 | | // Defaults to LLM_FFN_NONE (sentinel = 0); the mapping from the GGUF |
270 | | // string to a real op is done at hparam-load time via |
271 | | // llm_ffn_op_type_from_string() in llama-model.cpp, mirroring how |
272 | | // rope_scaling_type_train is handled. |
273 | | enum llm_ffn_op_type llm_ffn_op; |
274 | | |
275 | | // Step35: optional per-layer clamps for (Swi)GLU |
276 | | std::array<float, LLAMA_MAX_LAYERS> swiglu_clamp_exp; // clamping for expert FFN |
277 | | std::array<float, LLAMA_MAX_LAYERS> swiglu_clamp_shexp; // shared expert |
278 | | |
279 | | // this value n_pattern means that every nth layer is dense (i.e. non-SWA) |
280 | | // dense_first means whether the pattern is start with a dense layer |
281 | | // note that if n_pattern == 0, all layers are SWA |
282 | | // if n_pattern == 1, all layers are dense |
283 | | // example 1: n_pattern = 3, dense_first = false |
284 | | // il == 0: swa |
285 | | // il == 1: swa |
286 | | // il == 2: dense |
287 | | // il == 3: swa |
288 | | // il == 4: swa |
289 | | // il == 5: dense |
290 | | // il == 6: swa |
291 | | // etc ... |
292 | | // example 2: n_pattern = 2, dense_first = true |
293 | | // il == 0: dense |
294 | | // il == 1: swa |
295 | | // il == 2: dense |
296 | | // il == 3: swa |
297 | | // etc ... |
298 | | void set_swa_pattern(uint32_t n_pattern, bool dense_first = false); |
299 | | |
300 | | // return true if one of the layers is SWA |
301 | | bool is_swa_any() const; |
302 | | |
303 | | bool is_swa(uint32_t il) const; |
304 | | |
305 | | void set_recr_pattern(uint32_t n_pattern, bool dense_first = false); |
306 | | |
307 | | // whether or not the given layer is recurrent (for hybrid models) |
308 | | bool is_recr(uint32_t il) const; |
309 | | |
310 | | uint32_t n_head(uint32_t il = 0) const; |
311 | | |
312 | | uint32_t n_head_kv(uint32_t il = 0) const; |
313 | | |
314 | | uint32_t n_ff(uint32_t il = 0) const; |
315 | | |
316 | | uint32_t n_gqa(uint32_t il = 0) const; |
317 | | |
318 | | uint32_t n_rot(uint32_t il = 0) const; |
319 | | |
320 | | // dimension of main + auxiliary input embeddings |
321 | | uint32_t n_embd_inp() const; |
322 | | |
323 | | // dimension of the encoder input embeddings |
324 | | uint32_t n_embd_inp_enc() const; |
325 | | |
326 | | // dimension of output embeddings |
327 | | uint32_t n_embd_out() const; |
328 | | |
329 | | // dimension of key/value embeddings for each head (per layer) |
330 | | uint32_t n_embd_head_k(uint32_t il = 0) const; |
331 | | uint32_t n_embd_head_v(uint32_t il = 0) const; |
332 | | |
333 | | // dimension of key embeddings across all k-v heads |
334 | | uint32_t n_embd_k_gqa(uint32_t il = 0) const; |
335 | | |
336 | | // dimension of value embeddings across all k-v heads |
337 | | uint32_t n_embd_v_gqa(uint32_t il = 0) const; |
338 | | |
339 | | // true if any layer has a different n_embd_k_gqa/n_embd_v_gqa |
340 | | bool is_n_embd_k_gqa_variable() const; |
341 | | bool is_n_embd_v_gqa_variable() const; |
342 | | |
343 | | // return the maximum n_embd_k_gqa/n_embd_v_gqa across all layers |
344 | | uint32_t n_embd_k_gqa_max() const; |
345 | | uint32_t n_embd_v_gqa_max() const; |
346 | | |
347 | | // dimension of the rolling state embeddings |
348 | | // corresponds to Mamba's conv_states size or RWKV's token_shift states size |
349 | | uint32_t n_embd_r() const; |
350 | | |
351 | | // dimension of the recurrent state embeddings |
352 | | uint32_t n_embd_s() const; |
353 | | |
354 | | uint32_t n_pos_per_embd() const; |
355 | | |
356 | | // note: currently only support if either all or none of the layers are MLA |
357 | | bool is_mla() const; |
358 | | |
359 | | uint32_t n_embd_head_k_mla() const; |
360 | | uint32_t n_embd_head_v_mla() const; |
361 | | |
362 | | bool has_kv(uint32_t il) const; |
363 | | |
364 | | // number of effective layers (excludes nextn layers) |
365 | | uint32_t n_layer() const; |
366 | | |
367 | | // note that this function uses different SWA parameters from those in the hparams |
368 | | // note: inlined on purpose for performance reasons |
369 | | // TODO: think of a better place for this function |
370 | | // TODO: pack the SWA params in a struct? |
371 | 0 | static bool is_masked_swa(uint32_t n_swa, llama_swa_type swa_type, llama_pos p0, llama_pos p1) { |
372 | 0 | assert(p0 >= 0 && p1 >= 0); |
373 | |
|
374 | 0 | switch (swa_type) { |
375 | 0 | case LLAMA_SWA_TYPE_NONE: |
376 | 0 | { |
377 | 0 | } break; |
378 | 0 | case LLAMA_SWA_TYPE_STANDARD: |
379 | 0 | { |
380 | 0 | if (p1 - p0 >= (int32_t) n_swa) { |
381 | 0 | return true; |
382 | 0 | } |
383 | 0 | } break; |
384 | 0 | case LLAMA_SWA_TYPE_CHUNKED: |
385 | 0 | { |
386 | 0 | const llama_pos pos_chunk_start = (p1 / n_swa) * n_swa; |
387 | |
|
388 | 0 | if (p0 < pos_chunk_start) { |
389 | 0 | return true; |
390 | 0 | } |
391 | 0 | } break; |
392 | 0 | case LLAMA_SWA_TYPE_SYMMETRIC: |
393 | 0 | { |
394 | 0 | const int32_t half_n_swa = (int32_t) n_swa / 2; |
395 | 0 | const int32_t pos_diff = p1 - p0; |
396 | | |
397 | | // Mask if outside the symmetric window |
398 | 0 | if (pos_diff < -half_n_swa || pos_diff > half_n_swa) { |
399 | 0 | return true; |
400 | 0 | } |
401 | 0 | } break; |
402 | 0 | } |
403 | | |
404 | 0 | return false; |
405 | 0 | } |
406 | | |
407 | | |
408 | | bool use_mrope() const; |
409 | | }; |
410 | | |
411 | | static_assert(std::is_trivially_copyable<llama_hparams>::value, "llama_hparams must be trivially copyable"); |