llama-hparams.h 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. #pragma once
  2. #include "llama.h"
  3. #include <array>
  4. // bump if necessary
  5. #define LLAMA_MAX_LAYERS 512
  6. #define LLAMA_MAX_EXPERTS 384 // Kimi-K2
  7. enum llama_expert_gating_func_type {
  8. LLAMA_EXPERT_GATING_FUNC_TYPE_NONE = 0,
  9. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX = 1,
  10. LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID = 2,
  11. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX_WEIGHT = 3, // applied to the router weights instead of the logits
  12. };
  13. enum llama_swa_type {
  14. LLAMA_SWA_TYPE_NONE = 0,
  15. LLAMA_SWA_TYPE_STANDARD = 1,
  16. LLAMA_SWA_TYPE_CHUNKED = 2,
  17. LLAMA_SWA_TYPE_SYMMETRIC = 3,
  18. };
  19. struct llama_hparams_posnet {
  20. uint32_t n_embd;
  21. uint32_t n_layer;
  22. };
  23. struct llama_hparams_convnext {
  24. uint32_t n_embd;
  25. uint32_t n_layer;
  26. };
  27. struct llama_hparams {
  28. bool vocab_only;
  29. bool rope_finetuned;
  30. bool use_par_res;
  31. bool swin_norm;
  32. uint32_t n_ctx_train; // context size the model was trained on
  33. uint32_t n_embd;
  34. uint32_t n_embd_features = 0;
  35. uint32_t n_layer;
  36. int32_t n_layer_kv_from_start = -1; // if non-negative, the first n_layer_kv_from_start layers have KV cache
  37. uint32_t n_rot;
  38. uint32_t n_embd_head_k; // 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
  39. uint32_t n_embd_head_v; // dimension of values (d_v) aka n_embd_head
  40. uint32_t n_expert = 0;
  41. uint32_t n_expert_used = 0;
  42. uint32_t n_rel_attn_bkts = 0;
  43. // note: deepseek2 using MLA converts into MQA with larger heads, then decompresses to MHA
  44. uint32_t n_embd_head_k_mla = 0;
  45. uint32_t n_embd_head_v_mla = 0;
  46. // for WavTokenizer
  47. struct llama_hparams_posnet posnet;
  48. struct llama_hparams_convnext convnext;
  49. uint32_t n_shortconv_l_cache = 0;
  50. std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_arr;
  51. std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_kv_arr;
  52. std::array<uint32_t, LLAMA_MAX_LAYERS> n_ff_arr;
  53. uint32_t n_layer_dense_lead = 0;
  54. uint32_t n_lora_q = 0;
  55. uint32_t n_lora_kv = 0;
  56. uint32_t n_ff_exp = 0;
  57. uint32_t n_ff_shexp = 0;
  58. uint32_t n_ff_chexp = 0;
  59. uint32_t n_expert_shared = 0;
  60. uint32_t n_norm_groups = 0;
  61. uint32_t n_group_experts = 0;
  62. float expert_group_scale = 0.05f;
  63. float expert_weights_scale = 0.0f;
  64. bool expert_weights_norm = false;
  65. uint32_t expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_NONE;
  66. uint32_t moe_every_n_layers = 0;
  67. uint32_t nextn_predict_layers = 0;
  68. float f_norm_eps;
  69. float f_norm_rms_eps;
  70. float f_norm_group_eps;
  71. float f_attn_logit_softcapping = 50.0f;
  72. float f_router_logit_softcapping = 30.0f;
  73. float f_final_logit_softcapping = 30.0f;
  74. // for RWKV
  75. uint32_t rescale_every_n_layers = 0;
  76. uint32_t time_mix_extra_dim = 0;
  77. uint32_t time_decay_extra_dim = 0;
  78. uint32_t wkv_head_size = 0;
  79. uint32_t token_shift_count = 2;
  80. uint32_t n_lora_decay = 0;
  81. uint32_t n_lora_iclr = 0;
  82. uint32_t n_lora_value_res_mix = 0;
  83. uint32_t n_lora_gate = 0;
  84. float rope_attn_factor = 1.0f;
  85. float rope_freq_base_train;
  86. float rope_freq_base_train_swa;
  87. float rope_freq_scale_train;
  88. float rope_freq_scale_train_swa;
  89. uint32_t n_ctx_orig_yarn;
  90. float rope_yarn_log_mul = 0.0f;
  91. float yarn_ext_factor = -1.0f;
  92. float yarn_attn_factor = 1.0f;
  93. float yarn_beta_fast = 32.0f;
  94. float yarn_beta_slow = 1.0f;
  95. std::array<int, 4> rope_sections;
  96. // Sliding Window Attention (SWA)
  97. llama_swa_type swa_type = LLAMA_SWA_TYPE_NONE;
  98. // the size of the sliding window (0 - no SWA)
  99. uint32_t n_swa = 0;
  100. // if swa_layers[il] == true, then layer il is SWA
  101. // if swa_layers[il] == false, then layer il is dense (i.e. non-SWA)
  102. // by default, all layers are dense
  103. std::array<bool, LLAMA_MAX_LAYERS> swa_layers;
  104. // for State Space Models
  105. uint32_t ssm_d_conv = 0;
  106. uint32_t ssm_d_inner = 0;
  107. uint32_t ssm_d_state = 0;
  108. uint32_t ssm_dt_rank = 0;
  109. uint32_t ssm_n_group = 0;
  110. // for hybrid state space models
  111. std::array<bool, LLAMA_MAX_LAYERS> recurrent_layer_arr;
  112. bool ssm_dt_b_c_rms = false;
  113. float f_clamp_kqv = 0.0f;
  114. float f_max_alibi_bias = 0.0f;
  115. float f_logit_scale = 0.0f;
  116. // Additional scale factors (Granite/Granite MoE)
  117. float f_residual_scale = 0.0f;
  118. float f_embedding_scale = 0.0f;
  119. float f_attention_scale = 0.0f;
  120. // grok-2
  121. float f_attn_out_scale = 0.0f;
  122. uint32_t attn_temp_length = 0;
  123. bool causal_attn = true;
  124. bool use_alibi = false;
  125. bool attn_soft_cap = false;
  126. bool use_kq_norm = false;
  127. // for Classifiers
  128. uint32_t n_cls_out = 1;
  129. // llama4 smallthinker
  130. uint32_t n_moe_layer_step = 0;
  131. uint32_t n_no_rope_layer_step = 4;
  132. uint32_t n_attn_temp_floor_scale = 8192;
  133. float f_attn_temp_scale = 0.1;
  134. // gemma3n altup
  135. uint32_t n_altup = 4; // altup_num_inputs
  136. uint32_t i_altup_act = 0; // altup_active_idx
  137. uint32_t laurel_rank = 64;
  138. uint32_t n_embd_altup = 256;
  139. // needed for sentence-transformers dense layers
  140. uint32_t dense_2_feat_in = 0; // in_features of the 2_Dense
  141. uint32_t dense_2_feat_out = 0; // out_features of the 2_Dense
  142. uint32_t dense_3_feat_in = 0; // in_features of the 3_Dense
  143. uint32_t dense_3_feat_out = 0; // out_features of the 3_Dense
  144. // xIELU
  145. std::array<float, LLAMA_MAX_LAYERS> xielu_alpha_n;
  146. std::array<float, LLAMA_MAX_LAYERS> xielu_alpha_p;
  147. std::array<float, LLAMA_MAX_LAYERS> xielu_beta;
  148. std::array<float, LLAMA_MAX_LAYERS> xielu_eps;
  149. // needed by encoder-decoder models (e.g. T5, FLAN-T5)
  150. // ref: https://github.com/ggerganov/llama.cpp/pull/8141
  151. llama_token dec_start_token_id = LLAMA_TOKEN_NULL;
  152. uint32_t dec_n_layer = 0;
  153. enum llama_pooling_type pooling_type = LLAMA_POOLING_TYPE_NONE;
  154. enum llama_rope_type rope_type = LLAMA_ROPE_TYPE_NONE;
  155. enum llama_rope_scaling_type rope_scaling_type_train = LLAMA_ROPE_SCALING_TYPE_NONE;
  156. // this value n_pattern means that every nth layer is dense (i.e. non-SWA)
  157. // dense_first means whether the pattern is start with a dense layer
  158. // note that if n_pattern == 0, all layers are SWA
  159. // if n_pattern == 1, all layers are dense
  160. // example 1: n_pattern = 3, dense_first = false
  161. // il == 0: swa
  162. // il == 1: swa
  163. // il == 2: dense
  164. // il == 3: swa
  165. // il == 4: swa
  166. // il == 5: dense
  167. // il == 6: swa
  168. // etc ...
  169. // example 2: n_pattern = 2, dense_first = true
  170. // il == 0: dense
  171. // il == 1: swa
  172. // il == 2: dense
  173. // il == 3: swa
  174. // etc ...
  175. void set_swa_pattern(uint32_t n_pattern, bool dense_first = false);
  176. // return true if one of the layers is SWA
  177. bool is_swa_any() const;
  178. uint32_t n_head(uint32_t il = 0) const;
  179. uint32_t n_head_kv(uint32_t il = 0) const;
  180. uint32_t n_ff(uint32_t il = 0) const;
  181. uint32_t n_gqa(uint32_t il = 0) const;
  182. // dimension of key embeddings across all k-v heads
  183. uint32_t n_embd_k_gqa(uint32_t il = 0) const;
  184. // dimension of value embeddings across all k-v heads
  185. uint32_t n_embd_v_gqa(uint32_t il = 0) const;
  186. // true if any layer has a different n_embd_k_gqa/n_embd_v_gqa
  187. bool is_n_embd_k_gqa_variable() const;
  188. bool is_n_embd_v_gqa_variable() const;
  189. // return the maximum n_embd_k_gqa/n_embd_v_gqa across all layers
  190. uint32_t n_embd_k_gqa_max() const;
  191. uint32_t n_embd_v_gqa_max() const;
  192. // dimension of the rolling state embeddings
  193. // corresponds to Mamba's conv_states size or RWKV's token_shift states size
  194. uint32_t n_embd_r() const;
  195. // dimension of the recurrent state embeddings
  196. uint32_t n_embd_s() const;
  197. // whether or not the given layer is recurrent (for hybrid models)
  198. bool is_recurrent(uint32_t il) const;
  199. uint32_t n_pos_per_embd() const;
  200. bool is_swa(uint32_t il) const;
  201. bool has_kv(uint32_t il) const;
  202. // number of layers for which has_kv() returns true
  203. uint32_t n_layer_kv() const;
  204. // note that this function uses different SWA parameters from those in the hparams
  205. // TODO: think of a better place for this function
  206. // TODO: pack the SWA params in a struct?
  207. static bool is_masked_swa(uint32_t n_swa, llama_swa_type swa_type, llama_pos p0, llama_pos p1);
  208. };
  209. static_assert(std::is_trivially_copyable<llama_hparams>::value, "llama_hparams must be trivially copyable");