llama-model-loader.cpp 42 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039
  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-model-loader.h"
  27. #include "ggml.h"
  28. #include <array>
  29. #include <cinttypes>
  30. #include <cstring>
  31. #include <future>
  32. const char * llama_file_version_name(llama_fver version) {
  33. switch (version) {
  34. case GGUF_FILE_VERSION_V1: return "GGUF V1 (support until nov 2023)";
  35. case GGUF_FILE_VERSION_V2: return "GGUF V2";
  36. case GGUF_FILE_VERSION_V3: return "GGUF V3 (latest)";
  37. }
  38. return "unknown";
  39. }
  40. namespace GGUFMeta {
  41. template <typename T, gguf_type gt_, T (*gfun)(const gguf_context *, const int)>
  42. struct GKV_Base_Type {
  43. static constexpr gguf_type gt = gt_;
  44. static T getter(const gguf_context * ctx, const int kid) {
  45. return gfun(ctx, kid);
  46. }
  47. };
  48. template<typename T> struct GKV_Base;
  49. template<> struct GKV_Base<bool >: GKV_Base_Type<bool, GGUF_TYPE_BOOL, gguf_get_val_bool> {};
  50. template<> struct GKV_Base<uint8_t >: GKV_Base_Type<uint8_t, GGUF_TYPE_UINT8, gguf_get_val_u8 > {};
  51. template<> struct GKV_Base<uint16_t >: GKV_Base_Type<uint16_t, GGUF_TYPE_UINT16, gguf_get_val_u16 > {};
  52. template<> struct GKV_Base<uint32_t >: GKV_Base_Type<uint32_t, GGUF_TYPE_UINT32, gguf_get_val_u32 > {};
  53. template<> struct GKV_Base<uint64_t >: GKV_Base_Type<uint64_t, GGUF_TYPE_UINT64, gguf_get_val_u64 > {};
  54. template<> struct GKV_Base<int8_t >: GKV_Base_Type<int8_t, GGUF_TYPE_INT8, gguf_get_val_i8 > {};
  55. template<> struct GKV_Base<int16_t >: GKV_Base_Type<int16_t, GGUF_TYPE_INT16, gguf_get_val_i16 > {};
  56. template<> struct GKV_Base<int32_t >: GKV_Base_Type<int32_t, GGUF_TYPE_INT32, gguf_get_val_i32 > {};
  57. template<> struct GKV_Base<int64_t >: GKV_Base_Type<int64_t, GGUF_TYPE_INT64, gguf_get_val_i64 > {};
  58. template<> struct GKV_Base<float >: GKV_Base_Type<float, GGUF_TYPE_FLOAT32, gguf_get_val_f32 > {};
  59. template<> struct GKV_Base<double >: GKV_Base_Type<double, GGUF_TYPE_FLOAT64, gguf_get_val_f64 > {};
  60. template<> struct GKV_Base<const char *>: GKV_Base_Type<const char *, GGUF_TYPE_STRING, gguf_get_val_str > {};
  61. template<> struct GKV_Base<std::string> {
  62. static constexpr gguf_type gt = GGUF_TYPE_STRING;
  63. static std::string getter(const gguf_context * ctx, const int kid) {
  64. return gguf_get_val_str(ctx, kid);
  65. }
  66. };
  67. struct ArrayInfo {
  68. const gguf_type gt;
  69. const size_t length;
  70. const void * data;
  71. };
  72. template<> struct GKV_Base<ArrayInfo> {
  73. public:
  74. static constexpr gguf_type gt = GGUF_TYPE_ARRAY;
  75. static ArrayInfo getter(const gguf_context *ctx, const int k) {
  76. return ArrayInfo {
  77. gguf_get_arr_type(ctx, k),
  78. size_t(gguf_get_arr_n(ctx, k)),
  79. gguf_get_arr_data(ctx, k),
  80. };
  81. }
  82. };
  83. template<typename T>
  84. class GKV : public GKV_Base<T> {
  85. GKV() = delete;
  86. public:
  87. static T get_kv(const gguf_context * ctx, const int k) {
  88. const enum gguf_type kt = gguf_get_kv_type(ctx, k);
  89. if (kt != GKV::gt) {
  90. throw std::runtime_error(format("key %s has wrong type %s but expected type %s",
  91. gguf_get_key(ctx, k), gguf_type_name(kt), gguf_type_name(GKV::gt)));
  92. }
  93. return GKV::getter(ctx, k);
  94. }
  95. static const char * override_type_to_str(const llama_model_kv_override_type ty) {
  96. switch (ty) {
  97. case LLAMA_KV_OVERRIDE_TYPE_BOOL: return "bool";
  98. case LLAMA_KV_OVERRIDE_TYPE_INT: return "int";
  99. case LLAMA_KV_OVERRIDE_TYPE_FLOAT: return "float";
  100. case LLAMA_KV_OVERRIDE_TYPE_STR: return "str";
  101. }
  102. return "unknown";
  103. }
  104. static bool validate_override(const llama_model_kv_override_type expected_type, const struct llama_model_kv_override * ovrd) {
  105. if (!ovrd) { return false; }
  106. if (ovrd->tag == expected_type) {
  107. LLAMA_LOG_INFO("%s: Using metadata override (%5s) '%s' = ",
  108. __func__, override_type_to_str(ovrd->tag), ovrd->key);
  109. switch (ovrd->tag) {
  110. case LLAMA_KV_OVERRIDE_TYPE_BOOL: {
  111. LLAMA_LOG_INFO("%s\n", ovrd->val_bool ? "true" : "false");
  112. } break;
  113. case LLAMA_KV_OVERRIDE_TYPE_INT: {
  114. LLAMA_LOG_INFO("%" PRId64 "\n", ovrd->val_i64);
  115. } break;
  116. case LLAMA_KV_OVERRIDE_TYPE_FLOAT: {
  117. LLAMA_LOG_INFO("%.6f\n", ovrd->val_f64);
  118. } break;
  119. case LLAMA_KV_OVERRIDE_TYPE_STR: {
  120. LLAMA_LOG_INFO("%s\n", ovrd->val_str);
  121. } break;
  122. default:
  123. // Shouldn't be possible to end up here, but just in case...
  124. throw std::runtime_error(
  125. format("Unsupported attempt to override %s type for metadata key %s\n",
  126. override_type_to_str(ovrd->tag), ovrd->key));
  127. }
  128. return true;
  129. }
  130. LLAMA_LOG_WARN("%s: Warning: Bad metadata override type for key '%s', expected %s but got %s\n",
  131. __func__, ovrd->key, override_type_to_str(expected_type), override_type_to_str(ovrd->tag));
  132. return false;
  133. }
  134. template<typename OT>
  135. static typename std::enable_if<std::is_same<OT, bool>::value, bool>::type
  136. try_override(OT & target, const struct llama_model_kv_override * ovrd) {
  137. if (validate_override(LLAMA_KV_OVERRIDE_TYPE_BOOL, ovrd)) {
  138. target = ovrd->val_bool;
  139. return true;
  140. }
  141. return false;
  142. }
  143. template<typename OT>
  144. static typename std::enable_if<!std::is_same<OT, bool>::value && std::is_integral<OT>::value, bool>::type
  145. try_override(OT & target, const struct llama_model_kv_override * ovrd) {
  146. if (validate_override(LLAMA_KV_OVERRIDE_TYPE_INT, ovrd)) {
  147. target = ovrd->val_i64;
  148. return true;
  149. }
  150. return false;
  151. }
  152. template<typename OT>
  153. static typename std::enable_if<std::is_floating_point<OT>::value, bool>::type
  154. try_override(T & target, const struct llama_model_kv_override * ovrd) {
  155. if (validate_override(LLAMA_KV_OVERRIDE_TYPE_FLOAT, ovrd)) {
  156. target = ovrd->val_f64;
  157. return true;
  158. }
  159. return false;
  160. }
  161. template<typename OT>
  162. static typename std::enable_if<std::is_same<OT, std::string>::value, bool>::type
  163. try_override(T & target, const struct llama_model_kv_override * ovrd) {
  164. if (validate_override(LLAMA_KV_OVERRIDE_TYPE_STR, ovrd)) {
  165. target = ovrd->val_str;
  166. return true;
  167. }
  168. return false;
  169. }
  170. static bool set(const gguf_context * ctx, const int k, T & target, const struct llama_model_kv_override * ovrd = nullptr) {
  171. if (try_override<T>(target, ovrd)) {
  172. return true;
  173. }
  174. if (k < 0) { return false; }
  175. target = get_kv(ctx, k);
  176. return true;
  177. }
  178. static bool set(const gguf_context * ctx, const char * key, T & target, const struct llama_model_kv_override * ovrd = nullptr) {
  179. return set(ctx, gguf_find_key(ctx, key), target, ovrd);
  180. }
  181. static bool set(const gguf_context * ctx, const std::string & key, T & target, const struct llama_model_kv_override * ovrd = nullptr) {
  182. return set(ctx, key.c_str(), target, ovrd);
  183. }
  184. };
  185. }
  186. template<typename T>
  187. typename std::enable_if<std::is_integral<T>::value, bool>::type
  188. llama_model_loader::get_arr_n(const std::string & key, T & result, bool required) {
  189. const int kid = gguf_find_key(meta.get(), key.c_str());
  190. if (kid < 0) {
  191. if (required) {
  192. throw std::runtime_error(format("key not found in model: %s", key.c_str()));
  193. }
  194. return false;
  195. }
  196. struct GGUFMeta::ArrayInfo arr_info =
  197. GGUFMeta::GKV<GGUFMeta::ArrayInfo>::get_kv(meta.get(), kid);
  198. result = arr_info.length;
  199. return true;
  200. }
  201. template<typename T>
  202. typename std::enable_if<std::is_integral<T>::value, bool>::type
  203. llama_model_loader::get_arr_n(enum llm_kv kid, T & result, bool required) {
  204. return get_arr_n(llm_kv(kid), result, required);
  205. }
  206. template bool llama_model_loader::get_arr_n(enum llm_kv kid, uint32_t & result, bool required);
  207. template<typename T>
  208. bool llama_model_loader::get_arr(const std::string & key, std::vector<T> & result, bool required) {
  209. const int kid = gguf_find_key(meta.get(), key.c_str());
  210. if (kid < 0 || gguf_get_kv_type(meta.get(), kid) != GGUF_TYPE_ARRAY) {
  211. if (required) {
  212. throw std::runtime_error(format("array key not found in model: %s", key.c_str()));
  213. }
  214. return false;
  215. }
  216. struct GGUFMeta::ArrayInfo arr_info =
  217. GGUFMeta::GKV<GGUFMeta::ArrayInfo>::get_kv(meta.get(), kid);
  218. switch (arr_info.gt) {
  219. case GGUF_TYPE_FLOAT32: GGML_ASSERT((std::is_same<T, float>::value)); break;
  220. case GGUF_TYPE_INT32: GGML_ASSERT(
  221. (std::is_same<T, int32_t>::value) ||
  222. (std::is_same<T, uint32_t>::value)); break;
  223. default:
  224. throw std::runtime_error(format("%s is not a float32, int32 array", key.c_str()));
  225. }
  226. result.resize(arr_info.length);
  227. result.assign((const T*)arr_info.data, (const T *)arr_info.data + arr_info.length);
  228. return true;
  229. }
  230. template bool llama_model_loader::get_arr<std::array<unsigned int, 512>>(enum llm_kv kid, std::array<unsigned int, 512>& result, bool required);
  231. template<typename T, size_t N_MAX>
  232. bool llama_model_loader::get_arr(const std::string & key, std::array<T, N_MAX> & result, bool required) {
  233. const int kid = gguf_find_key(meta.get(), key.c_str());
  234. if (kid < 0 || gguf_get_kv_type(meta.get(), kid) != GGUF_TYPE_ARRAY) {
  235. if (required) {
  236. throw std::runtime_error(format("array key not found in model: %s", key.c_str()));
  237. }
  238. return false;
  239. }
  240. struct GGUFMeta::ArrayInfo arr_info =
  241. GGUFMeta::GKV<GGUFMeta::ArrayInfo>::get_kv(meta.get(), kid);
  242. switch (arr_info.gt) {
  243. case GGUF_TYPE_FLOAT32: GGML_ASSERT((std::is_same<T, float>::value)); break;
  244. case GGUF_TYPE_INT32: GGML_ASSERT(
  245. (std::is_same<T, int32_t>::value) ||
  246. (std::is_same<T, uint32_t>::value)); break;
  247. default:
  248. throw std::runtime_error(format("%s is not a float32, int32 array", key.c_str()));
  249. }
  250. if (arr_info.length > N_MAX) {
  251. throw std::runtime_error(format("array length %u for key %s exceeds max %u", (uint32_t) arr_info.length, key.c_str(), (uint32_t) N_MAX));
  252. }
  253. std::copy((const T*)arr_info.data, (const T *)arr_info.data + arr_info.length, result.begin());
  254. return true;
  255. }
  256. template<typename T>
  257. bool llama_model_loader::get_arr(enum llm_kv kid, T & result, bool required) {
  258. return get_arr(llm_kv(kid), result, required);
  259. }
  260. template<typename T>
  261. bool llama_model_loader::get_key(const std::string & key, T & result, bool required) {
  262. auto it = kv_overrides.find(key);
  263. const struct llama_model_kv_override * override =
  264. it != kv_overrides.end() ? &it->second : nullptr;
  265. const bool found = GGUFMeta::GKV<T>::set(meta.get(), key, result, override);
  266. if (required && !found) {
  267. throw std::runtime_error(format("key not found in model: %s", key.c_str()));
  268. }
  269. return found;
  270. }
  271. template<typename T>
  272. bool llama_model_loader::get_key(enum llm_kv kid, T & result, bool required) {
  273. return get_key(llm_kv(kid), result, required);
  274. }
  275. template bool llama_model_loader::get_key<bool> (enum llm_kv kid, bool & result, bool required);
  276. template bool llama_model_loader::get_key<float> (enum llm_kv kid, float & result, bool required);
  277. template bool llama_model_loader::get_key<uint32_t> (enum llm_kv kid, uint32_t & result, bool required);
  278. template bool llama_model_loader::get_key<std::string>(enum llm_kv kid, std::string & result, bool required);
  279. template<>
  280. bool llama_model_loader::get_key(enum llm_kv kid, enum llama_pooling_type & result, bool required) {
  281. uint32_t tmp;
  282. const bool found = get_key(kid, tmp, required);
  283. if (found) {
  284. result = (enum llama_pooling_type) tmp;
  285. } else {
  286. result = LLAMA_POOLING_TYPE_UNSPECIFIED;
  287. }
  288. return found;
  289. }
  290. // get array of n <= N_MAX elements, or a single element repeated n times
  291. template<typename T, size_t N_MAX>
  292. bool llama_model_loader::get_key_or_arr(const std::string & key, std::array<T, N_MAX> & result, uint32_t n, bool required) {
  293. const int kid = gguf_find_key(meta.get(), key.c_str());
  294. if (kid < 0) {
  295. if (required) {
  296. throw std::runtime_error(format("key not found in model: %s", key.c_str()));
  297. }
  298. return false;
  299. }
  300. if (n > N_MAX) {
  301. throw std::runtime_error(format("n > N_MAX: %u > %u for key %s", (uint32_t) n, (uint32_t) N_MAX, key.c_str()));
  302. }
  303. if (gguf_get_kv_type(meta.get(), kid) == GGUF_TYPE_ARRAY) {
  304. struct GGUFMeta::ArrayInfo arr_info =
  305. GGUFMeta::GKV<GGUFMeta::ArrayInfo>::get_kv(meta.get(), kid);
  306. if (n != arr_info.length) {
  307. throw std::runtime_error(format("key %s has wrong array length; expected %u, got %u", key.c_str(), n, (uint32_t) arr_info.length));
  308. }
  309. return get_arr(key, result, required);
  310. }
  311. T value;
  312. bool ok = get_key(key, value, required);
  313. if (!ok) {
  314. return false;
  315. }
  316. for (uint32_t i = 0; i < n; i++) {
  317. result[i] = value;
  318. }
  319. return true;
  320. }
  321. template<typename T>
  322. bool llama_model_loader::get_key_or_arr(enum llm_kv kid, T & result, uint32_t n, bool required) {
  323. return get_key_or_arr(llm_kv(kid), result, n, required);
  324. }
  325. // TODO: this is not very clever - figure out something better
  326. template bool llama_model_loader::get_key_or_arr<std::array<int, 4>>(enum llm_kv kid, std::array<int, 4> & result, uint32_t n, bool required);
  327. template bool llama_model_loader::get_key_or_arr<std::array<uint32_t, 512>>(enum llm_kv kid, std::array<uint32_t, 512> & result, uint32_t n, bool required);
  328. template bool llama_model_loader::get_key_or_arr<uint32_t>(const std::string & key, std::array<uint32_t, 512> & result, uint32_t n, bool required);
  329. llama_model_loader::llama_model_loader(const std::string & fname, bool use_mmap, bool check_tensors, const struct llama_model_kv_override * param_overrides_p) {
  330. int trace = 0;
  331. if (getenv("LLAMA_TRACE")) {
  332. trace = atoi(getenv("LLAMA_TRACE"));
  333. }
  334. if (param_overrides_p != nullptr) {
  335. for (const struct llama_model_kv_override * p = param_overrides_p; p->key[0] != 0; p++) {
  336. kv_overrides.insert({std::string(p->key), *p});
  337. }
  338. }
  339. struct ggml_context * ctx = NULL;
  340. struct gguf_init_params params = {
  341. /*.no_alloc = */ true,
  342. /*.ctx = */ &ctx,
  343. };
  344. meta.reset(gguf_init_from_file(fname.c_str(), params));
  345. if (!meta) {
  346. throw std::runtime_error(format("%s: failed to load model from %s\n", __func__, fname.c_str()));
  347. }
  348. get_key(llm_kv(LLM_KV_GENERAL_ARCHITECTURE), arch_name, false);
  349. llm_kv = LLM_KV(llm_arch_from_string(arch_name));
  350. files.emplace_back(new llama_file(fname.c_str(), "rb"));
  351. contexts.emplace_back(ctx);
  352. // Save tensors data offset of the main file.
  353. // For subsidiary files, `meta` tensor data offset must not be used,
  354. // so we build a unified tensors index for weights.
  355. for (ggml_tensor * cur = ggml_get_first_tensor(ctx); cur; cur = ggml_get_next_tensor(ctx, cur)) {
  356. std::string tensor_name = std::string(cur->name);
  357. // make sure there is no duplicated tensor names
  358. if (weights_map.find(tensor_name) != weights_map.end()) {
  359. throw std::runtime_error(format("invalid model: tensor '%s' is duplicated", ggml_get_name(cur)));
  360. }
  361. n_elements += ggml_nelements(cur);
  362. n_bytes += ggml_nbytes(cur);
  363. weights_map.emplace(tensor_name, llama_tensor_weight(files.back().get(), 0, meta.get(), cur));
  364. }
  365. uint16_t n_split = 0;
  366. get_key(llm_kv(LLM_KV_SPLIT_COUNT), n_split, false);
  367. // Load additional GGML contexts
  368. if (n_split > 1) {
  369. uint16_t idx = 0;
  370. get_key(llm_kv(LLM_KV_SPLIT_NO), idx);
  371. if (idx != 0) {
  372. throw std::runtime_error(format("illegal split file: %d, model must be loaded with the first split", idx));
  373. }
  374. std::vector<char> split_prefix(llama_path_max(), 0);
  375. if (!llama_split_prefix(split_prefix.data(), split_prefix.size(), fname.c_str(), idx, n_split)) {
  376. throw std::runtime_error(format("invalid split file: %s", fname.c_str()));
  377. }
  378. if (trace > 0) {
  379. LLAMA_LOG_INFO("%s: loading additional %d GGUFs\n", __func__, n_split);
  380. }
  381. std::vector<char> split_path(llama_path_max(), 0);
  382. for (idx = 1; idx < n_split; idx++) {
  383. llama_split_path(split_path.data(), split_path.size(), split_prefix.data(), idx, n_split);
  384. struct gguf_init_params split_params = {
  385. /*.no_alloc = */ true,
  386. /*.ctx = */ &ctx,
  387. };
  388. gguf_context_ptr ctx_gguf { gguf_init_from_file(split_path.data(), split_params) };
  389. if (!ctx_gguf) {
  390. throw std::runtime_error(format("%s: failed to load GGUF split from %s\n", __func__, split_path.data()));
  391. }
  392. files.emplace_back(new llama_file(split_path.data(), "rb"));
  393. contexts.emplace_back(ctx);
  394. // Save tensors data offset info of the shard.
  395. for (ggml_tensor * cur = ggml_get_first_tensor(ctx); cur; cur = ggml_get_next_tensor(ctx, cur)) {
  396. std::string tensor_name = std::string(cur->name);
  397. // make sure there is no duplicated tensor names
  398. if (weights_map.find(tensor_name) != weights_map.end()) {
  399. throw std::runtime_error(format("invalid model: tensor '%s' is duplicated", ggml_get_name(cur)));
  400. }
  401. n_elements += ggml_nelements(cur);
  402. n_bytes += ggml_nbytes(cur);
  403. weights_map.emplace(tensor_name, llama_tensor_weight(files.back().get(), idx, ctx_gguf.get(), cur));
  404. }
  405. }
  406. get_key(llm_kv(LLM_KV_SPLIT_TENSORS_COUNT), n_tensors);
  407. // sanity check
  408. {
  409. const int n_tensors_loaded = (int) weights_map.size();
  410. if (n_tensors != n_tensors_loaded) {
  411. throw std::runtime_error(format("corrupted model: %d tensors expected but %d found", n_tensors, n_tensors_loaded));
  412. }
  413. }
  414. LLAMA_LOG_INFO("%s: additional %d GGUFs metadata loaded.\n", __func__, n_split - 1);
  415. }
  416. n_kv = gguf_get_n_kv(meta.get());
  417. n_tensors = weights_map.size();
  418. fver = (enum llama_fver) gguf_get_version(meta.get());
  419. LLAMA_LOG_INFO("%s: loaded meta data with %d key-value pairs and %d tensors from %s (version %s)\n",
  420. __func__, n_kv, n_tensors, fname.c_str(), llama_file_version_name(fver));
  421. // determine file type based on the number of tensors for each quantization and print meta data
  422. // TODO: make optional
  423. {
  424. std::map<enum ggml_type, uint32_t> n_type;
  425. uint32_t n_type_max = 0;
  426. enum ggml_type type_max = GGML_TYPE_F32;
  427. for (const auto & it : weights_map) {
  428. const llama_tensor_weight & w = it.second;
  429. const ggml_tensor * tensor = w.tensor;
  430. enum ggml_type type = tensor->type;
  431. n_type[type]++;
  432. if (n_type_max < n_type[type]) {
  433. n_type_max = n_type[type];
  434. type_max = type;
  435. }
  436. if (trace > 0) {
  437. const uint16_t sid = w.idx;
  438. LLAMA_LOG_INFO("%s: - tensor split %2d: %32s %-8s [ %s ]\n", __func__, sid, ggml_get_name(tensor), ggml_type_name(type), llama_format_tensor_shape(tensor).c_str());
  439. }
  440. }
  441. switch (type_max) {
  442. case GGML_TYPE_F32: ftype = LLAMA_FTYPE_ALL_F32; break;
  443. case GGML_TYPE_F16: ftype = LLAMA_FTYPE_MOSTLY_F16; break;
  444. case GGML_TYPE_BF16: ftype = LLAMA_FTYPE_MOSTLY_BF16; break;
  445. case GGML_TYPE_Q4_0: ftype = LLAMA_FTYPE_MOSTLY_Q4_0; break;
  446. case GGML_TYPE_Q4_1: ftype = LLAMA_FTYPE_MOSTLY_Q4_1; break;
  447. case GGML_TYPE_Q5_0: ftype = LLAMA_FTYPE_MOSTLY_Q5_0; break;
  448. case GGML_TYPE_Q5_1: ftype = LLAMA_FTYPE_MOSTLY_Q5_1; break;
  449. case GGML_TYPE_Q8_0: ftype = LLAMA_FTYPE_MOSTLY_Q8_0; break;
  450. case GGML_TYPE_Q2_K: ftype = LLAMA_FTYPE_MOSTLY_Q2_K; break;
  451. case GGML_TYPE_Q3_K: ftype = LLAMA_FTYPE_MOSTLY_Q3_K_M; break;
  452. case GGML_TYPE_Q4_K: ftype = LLAMA_FTYPE_MOSTLY_Q4_K_M; break;
  453. case GGML_TYPE_Q5_K: ftype = LLAMA_FTYPE_MOSTLY_Q5_K_M; break;
  454. case GGML_TYPE_Q6_K: ftype = LLAMA_FTYPE_MOSTLY_Q6_K; break;
  455. case GGML_TYPE_TQ1_0: ftype = LLAMA_FTYPE_MOSTLY_TQ1_0; break;
  456. case GGML_TYPE_TQ2_0: ftype = LLAMA_FTYPE_MOSTLY_TQ2_0; break;
  457. case GGML_TYPE_IQ2_XXS: ftype = LLAMA_FTYPE_MOSTLY_IQ2_XXS; break;
  458. case GGML_TYPE_IQ2_XS: ftype = LLAMA_FTYPE_MOSTLY_IQ2_XS; break;
  459. case GGML_TYPE_IQ2_S: ftype = LLAMA_FTYPE_MOSTLY_IQ2_S; break;
  460. case GGML_TYPE_IQ3_XXS: ftype = LLAMA_FTYPE_MOSTLY_IQ3_XXS; break;
  461. case GGML_TYPE_IQ1_S: ftype = LLAMA_FTYPE_MOSTLY_IQ1_S; break;
  462. case GGML_TYPE_IQ1_M: ftype = LLAMA_FTYPE_MOSTLY_IQ1_M; break;
  463. case GGML_TYPE_IQ4_NL: ftype = LLAMA_FTYPE_MOSTLY_IQ4_NL; break;
  464. case GGML_TYPE_IQ4_XS: ftype = LLAMA_FTYPE_MOSTLY_IQ4_XS; break;
  465. case GGML_TYPE_IQ3_S: ftype = LLAMA_FTYPE_MOSTLY_IQ3_S; break;
  466. default:
  467. {
  468. LLAMA_LOG_WARN("%s: unknown type %s\n", __func__, ggml_type_name(type_max));
  469. ftype = LLAMA_FTYPE_ALL_F32;
  470. } break;
  471. }
  472. // this is a way to mark that we have "guessed" the file type
  473. ftype = (llama_ftype) (ftype | LLAMA_FTYPE_GUESSED);
  474. {
  475. const int kid = gguf_find_key(meta.get(), "general.file_type"); // TODO: use LLM_KV
  476. if (kid >= 0) {
  477. ftype = (llama_ftype) gguf_get_val_u32(meta.get(), kid);
  478. }
  479. }
  480. LLAMA_LOG_INFO("%s: Dumping metadata keys/values. Note: KV overrides do not apply in this output.\n", __func__);
  481. for (int i = 0; i < n_kv; i++) {
  482. const char * name = gguf_get_key(meta.get(), i);
  483. const enum gguf_type type = gguf_get_kv_type(meta.get(), i);
  484. const std::string type_name =
  485. type == GGUF_TYPE_ARRAY
  486. ? format("%s[%s,%d]", gguf_type_name(type), gguf_type_name(gguf_get_arr_type(meta.get(), i)), gguf_get_arr_n(meta.get(), i))
  487. : gguf_type_name(type);
  488. std::string value = gguf_kv_to_str(meta.get(), i);
  489. const size_t MAX_VALUE_LEN = 40;
  490. if (value.size() > MAX_VALUE_LEN) {
  491. value = format("%s...", value.substr(0, MAX_VALUE_LEN - 3).c_str());
  492. }
  493. replace_all(value, "\n", "\\n");
  494. LLAMA_LOG_INFO("%s: - kv %3d: %42s %-16s = %s\n", __func__, i, name, type_name.c_str(), value.c_str());
  495. }
  496. // print type counts
  497. for (auto & kv : n_type) {
  498. if (kv.second == 0) {
  499. continue;
  500. }
  501. LLAMA_LOG_INFO("%s: - type %4s: %4d tensors\n", __func__, ggml_type_name(kv.first), kv.second);
  502. }
  503. }
  504. if (!llama_mmap::SUPPORTED) {
  505. LLAMA_LOG_WARN("%s: mmap is not supported on this platform\n", __func__);
  506. use_mmap = false;
  507. }
  508. this->use_mmap = use_mmap;
  509. this->check_tensors = check_tensors;
  510. }
  511. std::string llama_model_loader::get_arch_name() const {
  512. return arch_name;
  513. }
  514. enum llm_arch llama_model_loader::get_arch() const {
  515. return llm_kv.arch;
  516. }
  517. const llama_model_loader::llama_tensor_weight * llama_model_loader::get_weight(const char * name) const {
  518. auto pos = weights_map.find(name);
  519. if (pos != weights_map.end()) {
  520. return &pos->second;
  521. }
  522. return nullptr;
  523. }
  524. const llama_model_loader::llama_tensor_weight & llama_model_loader::require_weight(const char * name) const {
  525. const llama_tensor_weight * weight = get_weight(name);
  526. if (!weight) {
  527. throw std::runtime_error(format("%s: tensor '%s' not found", __func__, name));
  528. }
  529. return *weight;
  530. }
  531. struct ggml_tensor * llama_model_loader::get_tensor_meta(const char * name) const {
  532. const auto * weight = get_weight(name);
  533. if (!weight) {
  534. return nullptr;
  535. }
  536. return weight->tensor;
  537. }
  538. struct ggml_tensor * llama_model_loader::require_tensor_meta(const std::string & name) const {
  539. struct ggml_tensor * tensor = get_tensor_meta(name.c_str());
  540. if (!tensor) {
  541. throw std::runtime_error(format("%s: tensor '%s' not found", __func__, name.c_str()));
  542. }
  543. return tensor;
  544. }
  545. const struct ggml_tensor * llama_model_loader::check_tensor_dims(const std::string & name, const std::vector<int64_t> & ne, bool required) const {
  546. const struct ggml_tensor * cur = get_tensor_meta(name.c_str());
  547. if (cur == NULL) {
  548. if (!required) {
  549. return NULL;
  550. }
  551. throw std::runtime_error(format("%s: tensor '%s' not found", __func__, name.c_str()));
  552. }
  553. {
  554. bool is_ok = true;
  555. for (size_t i = 0; i < GGML_MAX_DIMS; ++i) {
  556. if ((i < ne.size() && ne[i] != cur->ne[i]) || (i >= ne.size() && cur->ne[i] != 1)) {
  557. is_ok = false;
  558. break;
  559. }
  560. }
  561. if (!is_ok) {
  562. throw std::runtime_error(
  563. format("%s: tensor '%s' has wrong shape; expected %s, got %s",
  564. __func__, name.c_str(),
  565. llama_format_tensor_shape(ne).c_str(),
  566. llama_format_tensor_shape(cur).c_str()));
  567. }
  568. }
  569. return cur;
  570. }
  571. struct ggml_tensor * llama_model_loader::create_tensor(struct ggml_context * ctx, const std::string & name, const std::initializer_list<int64_t> & ne, int flags) {
  572. const struct ggml_tensor * cur = check_tensor_dims(name, ne, !(flags & TENSOR_NOT_REQUIRED));
  573. if (cur == NULL) {
  574. return NULL;
  575. }
  576. bool duplicated = flags & TENSOR_DUPLICATED;
  577. struct ggml_tensor * tensor = ggml_dup_tensor(ctx, cur);
  578. ggml_set_name(tensor, ggml_get_name(cur));
  579. if (duplicated) {
  580. size_data += ggml_nbytes(cur);
  581. } else {
  582. n_created++;
  583. }
  584. return tensor;
  585. }
  586. struct ggml_tensor * llama_model_loader::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) {
  587. const struct ggml_tensor * cur = check_tensor_dims(name, ne, required);
  588. if (cur == NULL) {
  589. return NULL;
  590. }
  591. if (cur->type != base->type) {
  592. throw std::runtime_error(format("%s: tensor '%s' has wrong type; expected %s, got %s", __func__, name.c_str(), ggml_type_name(base->type), ggml_type_name(cur->type)));
  593. }
  594. std::array<int64_t, GGML_MAX_DIMS> dims;
  595. for (size_t i = 0; i < GGML_MAX_DIMS; ++i) {
  596. dims[i] = i < ne.size() ? ne.begin()[i] : 1;
  597. }
  598. struct ggml_tensor * tensor = ggml_view_4d(ctx, base,
  599. dims[0], dims[1], dims[2], dims[3],
  600. cur->nb[1], cur->nb[2], cur->nb[3],
  601. offset);
  602. ggml_set_name(tensor, name.c_str());
  603. n_created++;
  604. return tensor;
  605. }
  606. void llama_model_loader::done_getting_tensors() const {
  607. if (n_created != n_tensors) {
  608. throw std::runtime_error(format("%s: wrong number of tensors; expected %d, got %d", __func__, n_tensors, n_created));
  609. }
  610. }
  611. void llama_model_loader::init_mappings(bool prefetch, llama_mlocks * mlock_mmaps) {
  612. if (use_mmap) {
  613. mappings.reserve(files.size());
  614. mmaps_used.reserve(files.size());
  615. for (const auto & file : files) {
  616. auto * reg = ggml_backend_dev_backend_reg(ggml_backend_dev_by_type(GGML_BACKEND_DEVICE_TYPE_CPU));
  617. auto * is_numa_fn = (decltype(ggml_is_numa) *) ggml_backend_reg_get_proc_address(reg, "ggml_backend_cpu_is_numa");
  618. std::unique_ptr<llama_mmap> mapping(new llama_mmap(file.get(), prefetch ? -1 : 0, is_numa_fn()));
  619. mmaps_used.emplace_back(mapping->size(), 0);
  620. if (mlock_mmaps) {
  621. std::unique_ptr<llama_mlock> mlock_mmap(new llama_mlock());
  622. mlock_mmap->init(mapping->addr());
  623. mlock_mmaps->emplace_back(std::move(mlock_mmap));
  624. }
  625. mappings.emplace_back(std::move(mapping));
  626. }
  627. }
  628. // compute the total size of all tensors for progress reporting
  629. for (const auto & it : weights_map) {
  630. size_data += ggml_nbytes(it.second.tensor);
  631. }
  632. }
  633. void llama_model_loader::get_mapping_range(size_t * first, size_t * last, void ** addr, int idx, ggml_context * ctx) const {
  634. GGML_ASSERT(!mappings.empty());
  635. const auto & mapping = mappings.at(idx);
  636. *first = mapping->size();
  637. *last = 0;
  638. *addr = mapping->addr();
  639. for (ggml_tensor * tensor = ggml_get_first_tensor(ctx); tensor; tensor = ggml_get_next_tensor(ctx, tensor)) {
  640. const auto * weight = get_weight(ggml_get_name(tensor));
  641. if (!weight || weight->idx != idx) {
  642. continue;
  643. }
  644. *first = std::min(*first, weight->offs);
  645. *last = std::max(*last, weight->offs + ggml_nbytes(tensor));
  646. }
  647. }
  648. void llama_model_loader::load_data_for(struct ggml_tensor * cur) const {
  649. const auto & w = require_weight(ggml_get_name(cur));
  650. if (use_mmap) {
  651. const auto & mapping = mappings.at(w.idx);
  652. if (cur->data == nullptr) {
  653. cur->data = (uint8_t *)mapping->addr() + w.offs;
  654. } else {
  655. memcpy(cur->data, (uint8_t *)mapping->addr() + w.offs, ggml_nbytes(cur));
  656. }
  657. } else {
  658. GGML_ASSERT(cur->data != nullptr);
  659. GGML_ASSERT(w.idx < files.size());
  660. const auto & file = files.at(w.idx);
  661. file->seek(w.offs, SEEK_SET);
  662. file->read_raw(cur->data, ggml_nbytes(cur));
  663. }
  664. if (check_tensors && !ggml_validate_row_data(cur->type, cur->data, ggml_nbytes(cur))) {
  665. throw std::runtime_error(format("tensor '%s' has invalid data", ggml_get_name(cur)));
  666. }
  667. }
  668. bool llama_model_loader::load_all_data(
  669. struct ggml_context * ctx,
  670. llama_buf_map & bufs,
  671. llama_mlocks * lmlocks,
  672. llama_progress_callback progress_callback,
  673. void * progress_callback_user_data) {
  674. GGML_ASSERT(size_data != 0 && "call init_mappings() first");
  675. std::vector<no_init<uint8_t>> read_buf;
  676. std::vector<std::future<std::pair<ggml_tensor *, bool>>> validation_result;
  677. // 4 staging buffers for async uploads, each sized 1MB seems to be a good default for single NVMe drives.
  678. // NVMe raid configurations might require more / larger buffers.
  679. constexpr size_t n_buffers = 4;
  680. constexpr size_t buffer_size = 1 * 1024 * 1024; // 1MB
  681. std::vector<ggml_backend_buffer_t> host_buffers;
  682. std::vector<ggml_backend_event_t> events;
  683. std::vector<void *> host_ptrs;
  684. size_t buffer_idx = 0; // buffer to use for async loads
  685. ggml_backend_t upload_backend = [&](const char * func) -> ggml_backend_t {
  686. if (use_mmap || check_tensors) {
  687. return nullptr;
  688. }
  689. // When not using mmaped io use async uploads from pinned memory to GPU memory.
  690. // First determine if the backend supports the necessary features for async uploads.
  691. auto * buf = bufs.count(0) ? bufs.at(0) : nullptr;
  692. if (!buf) {
  693. LLAMA_LOG_DEBUG("%s: no buffer found for async uploads\n", func);
  694. return nullptr;
  695. }
  696. auto * buft = ggml_backend_buffer_get_type(buf);
  697. auto * dev = ggml_backend_buft_get_device(buft);
  698. if (!dev) {
  699. LLAMA_LOG_DEBUG("%s: no device found for buffer type %s for async uploads\n", func,
  700. ggml_backend_buft_name(buft));
  701. return nullptr;
  702. }
  703. if (buft != ggml_backend_dev_buffer_type(dev)) {
  704. LLAMA_LOG_DEBUG("%s: buffer type %s is not the default buffer type for device %s for async uploads\n", func,
  705. ggml_backend_buft_name(buft), ggml_backend_dev_name(dev));
  706. return nullptr;
  707. }
  708. ggml_backend_dev_props props;
  709. ggml_backend_dev_get_props(dev, &props);
  710. if (!props.caps.async || !props.caps.host_buffer || !props.caps.events) {
  711. LLAMA_LOG_DEBUG("%s: device %s does not support async, host buffers or events\n", func,
  712. ggml_backend_dev_name(dev));
  713. return nullptr;
  714. }
  715. auto * host_buft = ggml_backend_dev_host_buffer_type(dev);
  716. if (!host_buft) {
  717. LLAMA_LOG_DEBUG("%s: no host buffer type found for device %s\n", func,
  718. ggml_backend_dev_name(dev));
  719. return nullptr;
  720. }
  721. // If the backend is supported, create pinned memory buffers and events for synchronisation.
  722. for (size_t idx = 0; idx < n_buffers; ++idx) {
  723. auto * buf = ggml_backend_buft_alloc_buffer(host_buft, buffer_size);
  724. if (!buf) {
  725. LLAMA_LOG_DEBUG("%s: failed to allocate host buffer for async uploads for device %s\n", func,
  726. ggml_backend_dev_name(dev));
  727. return nullptr;
  728. }
  729. host_buffers.emplace_back(buf);
  730. host_ptrs.emplace_back(ggml_backend_buffer_get_base(buf));
  731. auto * event = ggml_backend_event_new(dev);
  732. if (!event) {
  733. LLAMA_LOG_DEBUG("%s: failed to create event for async uploads for device %s\n", func,
  734. ggml_backend_dev_name(dev));
  735. return nullptr;
  736. }
  737. events.emplace_back(event);
  738. }
  739. ggml_backend_t backend = ggml_backend_dev_init(dev, nullptr);
  740. if (!backend) {
  741. LLAMA_LOG_DEBUG("%s: failed to initialize backend for device %s for async uploads\n", func,
  742. ggml_backend_dev_name(dev));
  743. return nullptr;
  744. }
  745. return backend;
  746. }(__func__);
  747. if (upload_backend) {
  748. LLAMA_LOG_DEBUG("%s: using async uploads for device %s, buffer type %s, backend %s\n", __func__,
  749. ggml_backend_dev_name(ggml_backend_get_device(upload_backend)),
  750. ggml_backend_buft_name(ggml_backend_buffer_get_type(bufs.at(0))),
  751. ggml_backend_name(upload_backend));
  752. }
  753. for (struct ggml_tensor * cur = ggml_get_first_tensor(ctx); cur != NULL; cur = ggml_get_next_tensor(ctx, cur)) {
  754. const auto * weight = get_weight(ggml_get_name(cur));
  755. if (weight == nullptr) {
  756. // this can happen with split experts models
  757. continue;
  758. }
  759. if (progress_callback) {
  760. if (!progress_callback((float) size_done / size_data, progress_callback_user_data)) {
  761. return false;
  762. }
  763. }
  764. size_t n_size = ggml_nbytes(cur);
  765. if (use_mmap) {
  766. const auto & mapping = mappings.at(weight->idx);
  767. ggml_backend_buffer_t buf_mmap = nullptr;
  768. if (bufs.count(weight->idx)) {
  769. buf_mmap = bufs.at(weight->idx);
  770. }
  771. uint8_t * data = (uint8_t *) mapping->addr() + weight->offs;
  772. if (check_tensors) {
  773. validation_result.emplace_back(std::async(std::launch::async, [cur, data, n_size] {
  774. return std::make_pair(cur, ggml_validate_row_data(cur->type, data, n_size));
  775. }));
  776. }
  777. GGML_ASSERT(buf_mmap || cur->data); // either we have a buffer to allocate the tensor in, or it is already allocated
  778. if (buf_mmap && cur->data == nullptr) {
  779. ggml_backend_tensor_alloc(buf_mmap, cur, data);
  780. if (lmlocks) {
  781. const auto & lmlock = lmlocks->at(weight->idx);
  782. lmlock->grow_to(weight->offs + n_size);
  783. }
  784. auto & mmap_used = mmaps_used[weight->idx];
  785. mmap_used.first = std::min(mmap_used.first, weight->offs);
  786. mmap_used.second = std::max(mmap_used.second, weight->offs + n_size);
  787. } else {
  788. ggml_backend_tensor_set(cur, data, 0, n_size);
  789. }
  790. } else {
  791. const auto & file = files.at(weight->idx);
  792. if (ggml_backend_buffer_is_host(cur->buffer)) {
  793. file->seek(weight->offs, SEEK_SET);
  794. file->read_raw(cur->data, n_size);
  795. if (check_tensors) {
  796. validation_result.emplace_back(std::async(std::launch::async, [cur, n_size] {
  797. return std::make_pair(cur, ggml_validate_row_data(cur->type, cur->data, n_size));
  798. }));
  799. }
  800. } else {
  801. // If upload_backend is valid load the tensor in chunks to pinned memory and upload the buffers asynchronously to the GPU.
  802. if (upload_backend) {
  803. file->seek(weight->offs, SEEK_SET);
  804. size_t bytes_read = 0;
  805. while (bytes_read < n_size) {
  806. size_t read_iteration = std::min<size_t>(buffer_size, n_size - bytes_read);
  807. ggml_backend_event_synchronize(events[buffer_idx]);
  808. file->read_raw(host_ptrs[buffer_idx], read_iteration);
  809. ggml_backend_tensor_set_async(upload_backend, cur, host_ptrs[buffer_idx], bytes_read, read_iteration);
  810. ggml_backend_event_record(events[buffer_idx], upload_backend);
  811. bytes_read += read_iteration;
  812. ++buffer_idx;
  813. buffer_idx %= n_buffers;
  814. }
  815. } else {
  816. read_buf.resize(n_size);
  817. file->seek(weight->offs, SEEK_SET);
  818. file->read_raw(read_buf.data(), n_size);
  819. ggml_backend_tensor_set(cur, read_buf.data(), 0, n_size);
  820. if (check_tensors && !ggml_validate_row_data(cur->type, read_buf.data(), n_size)) {
  821. throw std::runtime_error(format("tensor '%s' has invalid data", ggml_get_name(cur)));
  822. }
  823. }
  824. }
  825. }
  826. size_done += n_size;
  827. }
  828. // free temporary resources used for async uploads
  829. for (auto * event : events) {
  830. ggml_backend_event_synchronize(event);
  831. ggml_backend_event_free(event);
  832. }
  833. for (auto * buf : host_buffers) {
  834. ggml_backend_buffer_free(buf);
  835. }
  836. ggml_backend_free(upload_backend);
  837. // check validation results
  838. bool validation_failed = false;
  839. for (auto & future : validation_result) {
  840. auto result = future.get();
  841. if (!result.second) {
  842. LLAMA_LOG_ERROR("%s: tensor '%s' has invalid data\n", __func__, ggml_get_name(result.first));
  843. validation_failed = true;
  844. }
  845. }
  846. if (validation_failed) {
  847. throw std::runtime_error("found tensors with invalid data");
  848. }
  849. // check if this is the last call and do final cleanup
  850. if (size_done >= size_data) {
  851. // unmap offloaded tensors and metadata
  852. if (use_mmap) {
  853. for (uint32_t idx = 0; idx < mappings.size(); idx++) {
  854. const auto & mmap_used = mmaps_used.at(idx);
  855. auto & mapping = mappings.at(idx);
  856. mapping->unmap_fragment(0, mmap_used.first);
  857. if (mmap_used.second != 0) {
  858. mapping->unmap_fragment(mmap_used.second, mapping->size());
  859. }
  860. }
  861. }
  862. if (progress_callback) {
  863. // Even though the model is done loading, we still honor
  864. // cancellation since we need to free allocations.
  865. return progress_callback(1.0f, progress_callback_user_data);
  866. }
  867. }
  868. return true;
  869. }