llama-hparams.h 10 KB

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