| 855 | // |
| 856 | |
| 857 | static void llama_model_quantize_impl(const std::string & fname_inp, const std::string & fname_out, const llama_model_quantize_params * params) { |
| 858 | llama_ftype ftype = params->ftype; |
| 859 | |
| 860 | int nthread = params->nthread; |
| 861 | |
| 862 | if (nthread <= 0) { |
| 863 | nthread = std::thread::hardware_concurrency(); |
| 864 | } |
| 865 | |
| 866 | ggml_type default_type = llama_ftype_get_default_type(ftype); |
| 867 | if (default_type == GGML_TYPE_COUNT) { |
| 868 | throw std::runtime_error(format("invalid output file type %d\n", ftype)); |
| 869 | } |
| 870 | |
| 871 | // mmap consistently increases speed on Linux, and also increases speed on Windows with |
| 872 | // hot cache. It may cause a slowdown on macOS, possibly related to free memory. |
| 873 | #if defined(__linux__) || defined(_WIN32) |
| 874 | constexpr bool use_mmap = true; |
| 875 | #else |
| 876 | constexpr bool use_mmap = false; |
| 877 | #endif |
| 878 | |
| 879 | const llama_model_kv_override * kv_overrides = params->kv_overrides; |
| 880 | std::vector<std::string> splits = {}; |
| 881 | llama_model_loader ml(/*metadata*/ nullptr, /*set_tensor_data*/ nullptr, /*set_tensor_data_ud*/ nullptr, |
| 882 | fname_inp, splits, /*file*/ nullptr, use_mmap, /*use_direct_io*/ false, /*check_tensors*/ true, /*no_alloc*/ false, kv_overrides, nullptr); |
| 883 | ml.init_mappings(false); // no prefetching |
| 884 | |
| 885 | llama_model model(llama_model_default_params()); |
| 886 | |
| 887 | model.load_arch (ml); |
| 888 | model.load_hparams(ml); |
| 889 | model.load_stats (ml); |
| 890 | |
| 891 | quantize_state_impl qs(model, params); |
| 892 | |
| 893 | if (params->only_copy) { |
| 894 | ftype = ml.ftype; |
| 895 | } |
| 896 | std::unordered_map<std::string, std::vector<float>> i_data; |
| 897 | const std::unordered_map<std::string, std::vector<float>> * imatrix_data = nullptr; |
| 898 | if (params->imatrix) { |
| 899 | for (const llama_model_imatrix_data * p = params->imatrix; p->name != nullptr; p++) { |
| 900 | i_data.emplace(p->name, std::vector<float>(p->data, p->data + p->size)); |
| 901 | } |
| 902 | imatrix_data = & i_data; |
| 903 | if (imatrix_data) { |
| 904 | LLAMA_LOG_INFO("\n%s: have importance matrix data with %d entries\n", |
| 905 | __func__, (int)imatrix_data->size()); |
| 906 | qs.has_imatrix = true; |
| 907 | // check imatrix for nans or infs |
| 908 | for (const auto & kv : *imatrix_data) { |
| 909 | for (float f : kv.second) { |
| 910 | if (!std::isfinite(f)) { |
| 911 | throw std::runtime_error(format("imatrix contains non-finite value %f\n", f)); |
| 912 | } |
| 913 | } |
| 914 | } |
no test coverage detected