| 24 | const char * llama_file_version_name(llama_fver version); |
| 25 | |
| 26 | struct llama_model_loader { |
| 27 | // Holds information on a model weight |
| 28 | struct llama_tensor_weight { |
| 29 | uint16_t idx; // source file index |
| 30 | size_t offs; // tensor data offset in the original file |
| 31 | |
| 32 | ggml_tensor * tensor; |
| 33 | |
| 34 | llama_tensor_weight(const llama_file * file, uint16_t idx, const struct gguf_context * gguf_ctx, ggml_tensor * tensor) : idx(idx), tensor(tensor) { |
| 35 | const int tensor_idx = gguf_find_tensor(gguf_ctx, ggml_get_name(tensor)); |
| 36 | if (tensor_idx < 0) { |
| 37 | throw std::runtime_error(format("tensor '%s' not found in the model", ggml_get_name(tensor))); |
| 38 | } |
| 39 | |
| 40 | offs = gguf_get_data_offset(gguf_ctx) + gguf_get_tensor_offset(gguf_ctx, tensor_idx); |
| 41 | if (offs + ggml_nbytes(tensor) < offs || offs + ggml_nbytes(tensor) > file->size()) { |
| 42 | throw std::runtime_error(format("tensor '%s' data is not within the file bounds, model is corrupted or incomplete", ggml_get_name(tensor))); |
| 43 | } |
| 44 | } |
| 45 | }; |
| 46 | |
| 47 | // custom comparator to sort weights more nicely by layer |
| 48 | struct weight_name_comparer { |
| 49 | bool operator()(const std::string & a, const std::string & b) const { |
| 50 | int a_layer = -1; |
| 51 | int b_layer = -1; |
| 52 | sscanf(a.c_str(), "blk.%d.", &a_layer); |
| 53 | sscanf(b.c_str(), "blk.%d.", &b_layer); |
| 54 | if (a_layer != b_layer) { |
| 55 | return a_layer < b_layer; |
| 56 | } |
| 57 | return a < b; |
| 58 | } |
| 59 | }; |
| 60 | |
| 61 | static const int TENSOR_NOT_REQUIRED = 1 << 0; |
| 62 | static const int TENSOR_DUPLICATED = 1 << 1; |
| 63 | static const int TENSOR_SKIP = 1 << 2; |
| 64 | |
| 65 | int n_kv = 0; |
| 66 | int n_tensors = 0; |
| 67 | int n_created = 0; |
| 68 | |
| 69 | uint64_t n_elements = 0; |
| 70 | size_t n_bytes = 0; |
| 71 | |
| 72 | bool use_mmap = false; |
| 73 | bool use_direct_io = false; |
| 74 | bool check_tensors; |
| 75 | bool no_alloc; |
| 76 | |
| 77 | llama_files files; |
| 78 | llama_ftype ftype; |
| 79 | llama_fver fver; |
| 80 | |
| 81 | llama_mmaps mappings; |
| 82 | |
| 83 | std::map<std::string, llama_tensor_weight, weight_name_comparer> weights_map; |