llama-model-loader.h 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "llama-impl.h"
  29. #include "llama-arch.h"
  30. #include "llama-mmap.h"
  31. #include "ggml-cpp.h"
  32. #include <cstddef>
  33. #include <map>
  34. #include <stdexcept>
  35. #include <unordered_map>
  36. using llama_buf_map = std::unordered_map<uint32_t, ggml_backend_buffer_t>;
  37. enum llama_fver {
  38. GGUF_FILE_VERSION_V1 = 1,
  39. GGUF_FILE_VERSION_V2 = 2,
  40. GGUF_FILE_VERSION_V3 = 3,
  41. };
  42. const char * llama_file_version_name(llama_fver version);
  43. struct llama_model_loader {
  44. // Holds information on a model weight
  45. struct llama_tensor_weight {
  46. uint16_t idx; // source file index
  47. size_t offs; // tensor data offset in the original file
  48. ggml_tensor * tensor;
  49. llama_tensor_weight(const llama_file * file, uint16_t idx, const struct gguf_context * gguf_ctx, ggml_tensor * tensor) : idx(idx), tensor(tensor) {
  50. const int tensor_idx = gguf_find_tensor(gguf_ctx, ggml_get_name(tensor));
  51. if (tensor_idx < 0) {
  52. throw std::runtime_error(format("tensor '%s' not found in the model", ggml_get_name(tensor)));
  53. }
  54. offs = gguf_get_data_offset(gguf_ctx) + gguf_get_tensor_offset(gguf_ctx, tensor_idx);
  55. if (offs + ggml_nbytes(tensor) < offs || offs + ggml_nbytes(tensor) > file->size()) {
  56. throw std::runtime_error(format("tensor '%s' data is not within the file bounds, model is corrupted or incomplete", ggml_get_name(tensor)));
  57. }
  58. }
  59. };
  60. // custom comparator to sort weights more nicely by layer
  61. struct weight_name_comparer {
  62. bool operator()(const std::string & a, const std::string & b) const {
  63. int a_layer = -1;
  64. int b_layer = -1;
  65. sscanf(a.c_str(), "blk.%d.", &a_layer);
  66. sscanf(b.c_str(), "blk.%d.", &b_layer);
  67. if (a_layer != b_layer) {
  68. return a_layer < b_layer;
  69. }
  70. return a < b;
  71. }
  72. };
  73. static const int TENSOR_NOT_REQUIRED = 1;
  74. static const int TENSOR_DUPLICATED = 2;
  75. int n_kv = 0;
  76. int n_tensors = 0;
  77. int n_created = 0;
  78. uint64_t n_elements = 0;
  79. size_t n_bytes = 0;
  80. bool use_mmap = false;
  81. bool check_tensors;
  82. llama_files files;
  83. llama_ftype ftype;
  84. llama_fver fver;
  85. llama_mmaps mappings;
  86. std::map<std::string, struct llama_tensor_weight, weight_name_comparer> weights_map;
  87. std::unordered_map<std::string, struct llama_model_kv_override> kv_overrides;
  88. gguf_context_ptr meta;
  89. std::vector<ggml_context_ptr> contexts;
  90. std::string arch_name;
  91. LLM_KV llm_kv = LLM_KV(LLM_ARCH_UNKNOWN);
  92. size_t size_done = 0;
  93. size_t size_data = 0;
  94. std::vector<std::pair<size_t, size_t>> mmaps_used;
  95. llama_model_loader(const std::string & fname, bool use_mmap, bool check_tensors, const struct llama_model_kv_override * param_overrides_p);
  96. template<typename T>
  97. typename std::enable_if<std::is_integral<T>::value, bool>::type
  98. get_arr_n(const std::string & key, T & result, bool required = true);
  99. template<typename T>
  100. typename std::enable_if<std::is_integral<T>::value, bool>::type
  101. get_arr_n(enum llm_kv kid, T & result, bool required = true);
  102. template<typename T>
  103. bool get_arr(const std::string & key, std::vector<T> & result, bool required = true);
  104. template<typename T, size_t N_MAX>
  105. bool get_arr(const std::string & key, std::array<T, N_MAX> & result, bool required = true);
  106. template<typename T>
  107. bool get_arr(enum llm_kv kid, T & result, bool required = true);
  108. template<typename T>
  109. bool get_key(const std::string & key, T & result, bool required = true);
  110. template<typename T>
  111. bool get_key(enum llm_kv kid, T & result, bool required = true);
  112. template<typename T, size_t N_MAX>
  113. bool get_key_or_arr(const std::string & key, std::array<T, N_MAX> & result, uint32_t n, bool required = true);
  114. template<typename T>
  115. bool get_key_or_arr(enum llm_kv kid, T & result, uint32_t n, bool required = true);
  116. std::string get_arch_name() const;
  117. enum llm_arch get_arch() const;
  118. const llama_tensor_weight * get_weight(const char * name) const;
  119. const llama_tensor_weight & require_weight(const char * name) const;
  120. struct ggml_tensor * get_tensor_meta(const char * name) const;
  121. struct ggml_tensor * require_tensor_meta(const std::string & name) const;
  122. const struct ggml_tensor * check_tensor_dims(const std::string & name, const std::vector<int64_t> & ne, bool required) const;
  123. struct ggml_tensor * create_tensor(struct ggml_context * ctx, const std::string & name, const std::initializer_list<int64_t> & ne, int flags = 0);
  124. struct ggml_tensor * create_tensor_as_view(struct ggml_context * ctx, struct ggml_tensor * base, const std::string & name, const std::initializer_list<int64_t> & ne, size_t offset, bool required = true);
  125. void done_getting_tensors() const;
  126. void init_mappings(bool prefetch = true, llama_mlocks * mlock_mmaps = nullptr);
  127. void get_mapping_range(size_t * first, size_t * last, void ** addr, int idx, ggml_context * ctx) const;
  128. // for backwards compatibility, does not support ggml-backend
  129. void load_data_for(struct ggml_tensor * cur) const;
  130. // Returns false if cancelled by progress_callback
  131. bool load_all_data(
  132. struct ggml_context * ctx,
  133. llama_buf_map & bufs,
  134. llama_mlocks * lmlocks,
  135. llama_progress_callback progress_callback,
  136. void * progress_callback_user_data);
  137. };