llama-hparams.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. struct llama_hparams_posnet {
  13. uint32_t n_embd;
  14. uint32_t n_layer;
  15. };
  16. struct llama_hparams_convnext {
  17. uint32_t n_embd;
  18. uint32_t n_layer;
  19. };
  20. struct llama_hparams {
  21. bool vocab_only;
  22. bool rope_finetuned;
  23. bool use_par_res;
  24. bool swin_norm;
  25. uint32_t n_vocab = 0;
  26. uint32_t n_ctx_train; // context size the model was trained on
  27. uint32_t n_embd;
  28. uint32_t n_embd_features = 0;
  29. uint32_t n_layer;
  30. uint32_t n_rot;
  31. uint32_t n_swa = 0; // sliding window attention (SWA)
  32. 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
  33. uint32_t n_embd_head_v; // dimension of values (d_v) aka n_embd_head
  34. uint32_t n_expert = 0;
  35. uint32_t n_expert_used = 0;
  36. uint32_t n_vocab_type = 0; // for BERT-style token types
  37. uint32_t n_rel_attn_bkts = 0;
  38. // for WavTokenizer
  39. struct llama_hparams_posnet posnet;
  40. struct llama_hparams_convnext convnext;
  41. std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_arr;
  42. std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_kv_arr;
  43. std::array<uint32_t, LLAMA_MAX_LAYERS> n_ff_arr;
  44. std::array<std::array<uint32_t, LLAMA_MAX_LAYERS>, 4> n_bskcn_arr = {};
  45. std::array<uint32_t, LLAMA_MAX_LAYERS> cross_attn_layers;
  46. uint32_t n_layer_dense_lead = 0;
  47. uint32_t n_lora_q = 0;
  48. uint32_t n_lora_kv = 0;
  49. uint32_t n_ff_exp = 0;
  50. uint32_t n_ff_shexp = 0;
  51. uint32_t n_expert_shared = 0;
  52. uint32_t n_norm_groups = 0;
  53. float expert_weights_scale = 0.0;
  54. bool expert_weights_norm = false;
  55. uint32_t expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_NONE;
  56. float f_norm_eps;
  57. float f_norm_rms_eps;
  58. float f_norm_group_eps;
  59. float f_attn_logit_softcapping = 50.0f;
  60. float f_final_logit_softcapping = 30.0f;
  61. // for RWKV
  62. uint32_t rescale_every_n_layers = 0;
  63. uint32_t time_mix_extra_dim = 0;
  64. uint32_t time_decay_extra_dim = 0;
  65. uint32_t wkv_head_size = 0;
  66. float rope_attn_factor = 1.0f;
  67. float rope_freq_base_train;
  68. float rope_freq_scale_train;
  69. uint32_t n_ctx_orig_yarn;
  70. float rope_yarn_log_mul;
  71. std::array<int, 4> rope_sections;
  72. // for State Space Models
  73. uint32_t ssm_d_conv = 0;
  74. uint32_t ssm_d_inner = 0;
  75. uint32_t ssm_d_state = 0;
  76. uint32_t ssm_dt_rank = 0;
  77. bool ssm_dt_b_c_rms = false;
  78. float f_clamp_kqv = 0.0f;
  79. float f_max_alibi_bias = 0.0f;
  80. float f_logit_scale = 0.0f;
  81. // Additional scale factors (Granite/Granite MoE)
  82. float f_residual_scale = 0.0f;
  83. float f_embedding_scale = 0.0f;
  84. float f_attention_scale = 0.0f;
  85. bool causal_attn = true;
  86. bool use_alibi = false;
  87. bool attn_soft_cap = false;
  88. // needed by encoder-decoder models (e.g. T5, FLAN-T5)
  89. // ref: https://github.com/ggerganov/llama.cpp/pull/8141
  90. llama_token dec_start_token_id = LLAMA_TOKEN_NULL;
  91. enum llama_pooling_type pooling_type = LLAMA_POOLING_TYPE_NONE;
  92. enum llama_rope_type rope_type = LLAMA_ROPE_TYPE_NONE;
  93. enum llama_rope_scaling_type rope_scaling_type_train = LLAMA_ROPE_SCALING_TYPE_NONE;
  94. uint32_t n_head(uint32_t il = 0) const;
  95. uint32_t n_head_kv(uint32_t il = 0) const;
  96. uint32_t n_ff(uint32_t il = 0) const;
  97. uint32_t n_gqa(uint32_t il = 0) const;
  98. // dimension of key embeddings across all k-v heads
  99. uint32_t n_embd_k_gqa(uint32_t il = 0) const;
  100. // dimension of value embeddings across all k-v heads
  101. uint32_t n_embd_v_gqa(uint32_t il = 0) const;
  102. // dimension of the rolling state embeddings
  103. // corresponds to Mamba's conv_states size or RWKV's token_shift states size
  104. uint32_t n_embd_k_s() const;
  105. // dimension of the recurrent state embeddings
  106. uint32_t n_embd_v_s() const;
  107. // Block skip connection
  108. bool n_bskcn(uint32_t n, uint32_t il) const;
  109. // cross attention layers
  110. bool cross_attention_layers(uint32_t il) const;
  111. };
  112. static_assert(std::is_trivially_copyable<llama_hparams>::value, "llama_hparams must be trivially copyable");