llama-hparams.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /**
  2. * llama.cpp - commit 46e3556e01b824e52395fb050b29804b6cff2a7c - do not edit this file
  3. *
  4. * MIT License
  5. *
  6. * Copyright (c) 2023-2024 The ggml authors
  7. *
  8. * Permission is hereby granted, free of charge, to any person obtaining a copy
  9. * of this software and associated documentation files (the "Software"), to deal
  10. * in the Software without restriction, including without limitation the rights
  11. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  12. * copies of the Software, and to permit persons to whom the Software is
  13. * furnished to do so, subject to the following conditions:
  14. *
  15. * The above copyright notice and this permission notice shall be included in all
  16. * copies or substantial portions of the Software.
  17. *
  18. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  19. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  20. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  21. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  22. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  23. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  24. * SOFTWARE.
  25. */
  26. #pragma once
  27. #include "llama.h"
  28. #include <array>
  29. // bump if necessary
  30. #define LLAMA_MAX_LAYERS 512
  31. #define LLAMA_MAX_EXPERTS 256 // DeepSeekV3
  32. enum llama_expert_gating_func_type {
  33. LLAMA_EXPERT_GATING_FUNC_TYPE_NONE = 0,
  34. LLAMA_EXPERT_GATING_FUNC_TYPE_SOFTMAX = 1,
  35. LLAMA_EXPERT_GATING_FUNC_TYPE_SIGMOID = 2,
  36. };
  37. struct llama_hparams_posnet {
  38. uint32_t n_embd;
  39. uint32_t n_layer;
  40. };
  41. struct llama_hparams_convnext {
  42. uint32_t n_embd;
  43. uint32_t n_layer;
  44. };
  45. struct llama_hparams {
  46. bool vocab_only;
  47. bool rope_finetuned;
  48. bool use_par_res;
  49. bool swin_norm;
  50. uint32_t n_vocab = 0;
  51. uint32_t n_ctx_train; // context size the model was trained on
  52. uint32_t n_embd;
  53. uint32_t n_embd_features = 0;
  54. uint32_t n_layer;
  55. uint32_t n_rot;
  56. uint32_t n_swa = 0; // sliding window attention (SWA)
  57. 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
  58. uint32_t n_embd_head_v; // dimension of values (d_v) aka n_embd_head
  59. uint32_t n_expert = 0;
  60. uint32_t n_expert_used = 0;
  61. uint32_t n_vocab_type = 0; // for BERT-style token types
  62. uint32_t n_rel_attn_bkts = 0;
  63. // for WavTokenizer
  64. struct llama_hparams_posnet posnet;
  65. struct llama_hparams_convnext convnext;
  66. std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_arr;
  67. std::array<uint32_t, LLAMA_MAX_LAYERS> n_head_kv_arr;
  68. std::array<uint32_t, LLAMA_MAX_LAYERS> n_ff_arr;
  69. std::array<std::array<uint32_t, LLAMA_MAX_LAYERS>, 4> n_bskcn_arr = {};
  70. std::array<uint32_t, LLAMA_MAX_LAYERS> cross_attn_layers;
  71. uint32_t n_layer_dense_lead = 0;
  72. uint32_t n_lora_q = 0;
  73. uint32_t n_lora_kv = 0;
  74. uint32_t n_ff_exp = 0;
  75. uint32_t n_ff_shexp = 0;
  76. uint32_t n_expert_shared = 0;
  77. uint32_t n_norm_groups = 0;
  78. float expert_weights_scale = 0.0;
  79. bool expert_weights_norm = false;
  80. uint32_t expert_gating_func = LLAMA_EXPERT_GATING_FUNC_TYPE_NONE;
  81. float f_norm_eps;
  82. float f_norm_rms_eps;
  83. float f_norm_group_eps;
  84. float f_attn_logit_softcapping = 50.0f;
  85. float f_final_logit_softcapping = 30.0f;
  86. // for RWKV
  87. uint32_t rescale_every_n_layers = 0;
  88. uint32_t time_mix_extra_dim = 0;
  89. uint32_t time_decay_extra_dim = 0;
  90. uint32_t wkv_head_size = 0;
  91. float rope_attn_factor = 1.0f;
  92. float rope_freq_base_train;
  93. float rope_freq_scale_train;
  94. uint32_t n_ctx_orig_yarn;
  95. float rope_yarn_log_mul;
  96. std::array<int, 4> rope_sections;
  97. // for State Space Models
  98. uint32_t ssm_d_conv = 0;
  99. uint32_t ssm_d_inner = 0;
  100. uint32_t ssm_d_state = 0;
  101. uint32_t ssm_dt_rank = 0;
  102. bool ssm_dt_b_c_rms = false;
  103. float f_clamp_kqv = 0.0f;
  104. float f_max_alibi_bias = 0.0f;
  105. float f_logit_scale = 0.0f;
  106. // Additional scale factors (Granite/Granite MoE)
  107. float f_residual_scale = 0.0f;
  108. float f_embedding_scale = 0.0f;
  109. float f_attention_scale = 0.0f;
  110. bool causal_attn = true;
  111. bool use_alibi = false;
  112. bool attn_soft_cap = false;
  113. // needed by encoder-decoder models (e.g. T5, FLAN-T5)
  114. // ref: https://github.com/ggerganov/llama.cpp/pull/8141
  115. llama_token dec_start_token_id = LLAMA_TOKEN_NULL;
  116. enum llama_pooling_type pooling_type = LLAMA_POOLING_TYPE_NONE;
  117. enum llama_rope_type rope_type = LLAMA_ROPE_TYPE_NONE;
  118. enum llama_rope_scaling_type rope_scaling_type_train = LLAMA_ROPE_SCALING_TYPE_NONE;
  119. uint32_t n_head(uint32_t il = 0) const;
  120. uint32_t n_head_kv(uint32_t il = 0) const;
  121. uint32_t n_ff(uint32_t il = 0) const;
  122. uint32_t n_gqa(uint32_t il = 0) const;
  123. // dimension of key embeddings across all k-v heads
  124. uint32_t n_embd_k_gqa(uint32_t il = 0) const;
  125. // dimension of value embeddings across all k-v heads
  126. uint32_t n_embd_v_gqa(uint32_t il = 0) const;
  127. // dimension of the rolling state embeddings
  128. // corresponds to Mamba's conv_states size or RWKV's token_shift states size
  129. uint32_t n_embd_k_s() const;
  130. // dimension of the recurrent state embeddings
  131. uint32_t n_embd_v_s() const;
  132. // Block skip connection
  133. bool n_bskcn(uint32_t n, uint32_t il) const;
  134. // cross attention layers
  135. bool cross_attention_layers(uint32_t il) const;
  136. };
  137. static_assert(std::is_trivially_copyable<llama_hparams>::value, "llama_hparams must be trivially copyable");