llama-hparams.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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. #include "llama-hparams.h"
  27. #include "ggml.h"
  28. #include <algorithm>
  29. uint32_t llama_hparams::n_head(uint32_t il) const {
  30. if (il < n_layer) {
  31. return n_head_arr[il];
  32. }
  33. GGML_ABORT("fatal error");
  34. }
  35. uint32_t llama_hparams::n_head_kv(uint32_t il) const {
  36. if (il < n_layer) {
  37. return n_head_kv_arr[il];
  38. }
  39. GGML_ABORT("fatal error");
  40. }
  41. uint32_t llama_hparams::n_ff(uint32_t il) const {
  42. if (il < n_layer) {
  43. return n_ff_arr[il];
  44. }
  45. GGML_ABORT("fatal error");
  46. }
  47. uint32_t llama_hparams::n_gqa(uint32_t il) const {
  48. const uint32_t n_head = this->n_head(il);
  49. const uint32_t n_head_kv = this->n_head_kv(il);
  50. if (n_head_kv == 0) {
  51. return 0;
  52. }
  53. return n_head/n_head_kv;
  54. }
  55. uint32_t llama_hparams::n_embd_k_gqa(uint32_t il) const {
  56. const uint32_t n_head_kv = this->n_head_kv(il);
  57. return n_embd_head_k * n_head_kv;
  58. }
  59. uint32_t llama_hparams::n_embd_v_gqa(uint32_t il) const {
  60. const uint32_t n_head_kv = this->n_head_kv(il);
  61. return n_embd_head_v * n_head_kv;
  62. }
  63. uint32_t llama_hparams::n_embd_k_s() const {
  64. if (wkv_head_size != 0) {
  65. // for RWKV models
  66. return 2 * n_embd;
  67. }
  68. // TODO: maybe support other convolution strides than 1
  69. // NOTE: since the first column of the conv_state is shifted out each time, it's not actually needed
  70. return (ssm_d_conv > 0 ? ssm_d_conv - 1 : 0) * ssm_d_inner;
  71. }
  72. uint32_t llama_hparams::n_embd_v_s() const {
  73. if (wkv_head_size != 0) {
  74. // corresponds to RWKV's wkv_states size
  75. return n_embd * wkv_head_size;
  76. }
  77. // corresponds to Mamba's ssm_states size
  78. return ssm_d_state * ssm_d_inner;
  79. }
  80. bool llama_hparams::n_bskcn(uint32_t n, uint32_t il) const {
  81. if (il < n_layer) {
  82. return n_bskcn_arr[n][il] > 0;
  83. }
  84. GGML_ABORT("fatal error");
  85. }
  86. bool llama_hparams::cross_attention_layers(uint32_t il) const {
  87. return std::find(cross_attn_layers.begin(), cross_attn_layers.end(), il) != cross_attn_layers.end();
  88. }