llama-hparams.h 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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 256 // DeepSeekV3
  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. };
  12. enum llama_swa_type {
  13. LLAMA_SWA_TYPE_NONE = 0,
  14. LLAMA_SWA_TYPE_STANDARD = 1,
  15. LLAMA_SWA_TYPE_CHUNKED = 2,
  16. };
  17. struct llama_hparams_posnet {
  18. uint32_t n_embd;
  19. uint32_t n_layer;
  20. };
  21. struct llama_hparams_convnext {
  22. uint32_t n_embd;
  23. uint32_t n_layer;
  24. };
  25. struct llama_hparams {
  26. bool vocab_only;
  27. bool rope_finetuned;
  28. bool use_par_res;
  29. bool swin_norm;
  30. uint32_t n_ctx_train; // context size the model was trained on
  31. uint32_t n_embd;
  32. uint32_t n_embd_features = 0;
  33. uint32_t n_layer;
  34. uint32_t n_rot;
  35. 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
  36. uint32_t n_embd_head_v; // dimension of values (d_v) aka n_embd_head
  37. uint32_t n_expert = 0;
  38. uint32_t n_expert_used = 0;
  39. uint32_t n_rel_attn_bkts = 0;
  40. // note: deepseek2 using MLA converts into MQA with larger heads, then decompresses to MHA
  41. uint32_t n_embd_head_k_mla = 0;
  42. uint32_t n_embd_head_v_mla = 0;
  43. // for WavTokenizer
  44. struct llama_hparams_posnet posnet;
  45. struct llama_hparams_convnext convnext;
  46. std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_arr;
  47. std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_kv_arr;
  48. std::array<uint32_t, LLAMA_MAX_LAYERS> n_ff_arr;
  49. uint32_t n_layer_dense_lead = 0;
  50. uint32_t n_lora_q = 0;
  51. uint32_t n_lora_kv = 0;
  52. uint32_t n_ff_exp = 0;
  53. uint32_t n_ff_shexp = 0;
  54. uint32_t n_expert_shared = 0;
  55. uint32_t n_norm_groups = 0;
  56. float expert_weights_scale = 0.0;
  57. bool expert_weights_norm = false;
  58. uint32_t expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_NONE;
  59. uint32_t moe_every_n_layers = 0;
  60. float f_norm_eps;
  61. float f_norm_rms_eps;
  62. float f_norm_group_eps;
  63. float f_attn_logit_softcapping = 50.0f;
  64. float f_final_logit_softcapping = 30.0f;
  65. // for RWKV
  66. uint32_t rescale_every_n_layers = 0;
  67. uint32_t time_mix_extra_dim = 0;
  68. uint32_t time_decay_extra_dim = 0;
  69. uint32_t wkv_head_size = 0;
  70. uint32_t token_shift_count = 2;
  71. uint32_t n_lora_decay = 0;
  72. uint32_t n_lora_iclr = 0;
  73. uint32_t n_lora_value_res_mix = 0;
  74. uint32_t n_lora_gate = 0;
  75. float rope_attn_factor = 1.0f;
  76. float rope_freq_base_train;
  77. float rope_freq_base_train_swa;
  78. float rope_freq_scale_train;
  79. float rope_freq_scale_train_swa;
  80. uint32_t n_ctx_orig_yarn;
  81. float rope_yarn_log_mul;
  82. std::array<int, 4> rope_sections;
  83. // Sliding Window Attention (SWA)
  84. llama_swa_type swa_type = LLAMA_SWA_TYPE_NONE;
  85. // the size of the sliding window (0 - no SWA)
  86. uint32_t n_swa = 0;
  87. // if swa_layers[il] == true, then layer il is SWA
  88. // if swa_layers[il] == false, then layer il is dense (i.e. non-SWA)
  89. // by default, all layers are dense
  90. std::array<bool, LLAMA_MAX_LAYERS> swa_layers;
  91. // for State Space Models
  92. uint32_t ssm_d_conv = 0;
  93. uint32_t ssm_d_inner = 0;
  94. uint32_t ssm_d_state = 0;
  95. uint32_t ssm_dt_rank = 0;
  96. bool ssm_dt_b_c_rms = false;
  97. float f_clamp_kqv = 0.0f;
  98. float f_max_alibi_bias = 0.0f;
  99. float f_logit_scale = 0.0f;
  100. // Additional scale factors (Granite/Granite MoE)
  101. float f_residual_scale = 0.0f;
  102. float f_embedding_scale = 0.0f;
  103. float f_attention_scale = 0.0f;
  104. bool causal_attn = true;
  105. bool use_alibi = false;
  106. bool attn_soft_cap = false;
  107. bool use_kq_norm = true;
  108. // for Classifiers
  109. uint32_t n_cls_out = 1;
  110. // llama4
  111. uint32_t n_moe_layer_step = 0;
  112. uint32_t n_no_rope_layer_step = 4;
  113. uint32_t n_attn_temp_floor_scale = 8192;
  114. float f_attn_temp_scale = 0.1;
  115. // needed by encoder-decoder models (e.g. T5, FLAN-T5)
  116. // ref: https://github.com/ggerganov/llama.cpp/pull/8141
  117. llama_token dec_start_token_id = LLAMA_TOKEN_NULL;
  118. enum llama_pooling_type pooling_type = LLAMA_POOLING_TYPE_NONE;
  119. enum llama_rope_type rope_type = LLAMA_ROPE_TYPE_NONE;
  120. enum llama_rope_scaling_type rope_scaling_type_train = LLAMA_ROPE_SCALING_TYPE_NONE;
  121. // this value n_pattern means that every nth layer is dense (i.e. non-SWA)
  122. // note that if n_pattern == 0, all layers are SWA
  123. // if n_pattern == 1, all layers are dense
  124. // example: n_pattern = 3
  125. // il == 0: swa
  126. // il == 1: swa
  127. // il == 2: dense
  128. // il == 3: swa
  129. // il == 4: swa
  130. // il == 5: dense
  131. // il == 6: swa
  132. // etc ...
  133. void set_swa_pattern(uint32_t n_pattern);
  134. // return true if one of the layers is SWA
  135. bool is_swa_any() const;
  136. uint32_t n_head(uint32_t il = 0) const;
  137. uint32_t n_head_kv(uint32_t il = 0) const;
  138. uint32_t n_ff(uint32_t il = 0) const;
  139. uint32_t n_gqa(uint32_t il = 0) const;
  140. // dimension of key embeddings across all k-v heads
  141. uint32_t n_embd_k_gqa(uint32_t il = 0) const;
  142. // dimension of value embeddings across all k-v heads
  143. uint32_t n_embd_v_gqa(uint32_t il = 0) const;
  144. // dimension of the rolling state embeddings
  145. // corresponds to Mamba's conv_states size or RWKV's token_shift states size
  146. uint32_t n_embd_k_s() const;
  147. // dimension of the recurrent state embeddings
  148. uint32_t n_embd_v_s() const;
  149. bool is_swa(uint32_t il) const;
  150. };
  151. static_assert(std::is_trivially_copyable<llama_hparams>::value, "llama_hparams must be trivially copyable");