mirror of
				https://github.com/ggml-org/llama.cpp.git
				synced 2025-10-31 08:51:55 +00:00 
			
		
		
		
	common : move string_remove_suffix from quantize and imatrix
Co-authored-by: Sigbjørn Skjæret <sigbjorn.skjaeret@scala.com>
This commit is contained in:
		| @@ -448,6 +448,15 @@ void string_replace_all(std::string & s, const std::string & search, const std:: | |||||||
| bool string_ends_with(const std::string_view & str, const std::string_view & suffix) { | bool string_ends_with(const std::string_view & str, const std::string_view & suffix) { | ||||||
|     return str.size() >= suffix.size() && str.compare(str.size()-suffix.size(), suffix.size(), suffix) == 0; |     return str.size() >= suffix.size() && str.compare(str.size()-suffix.size(), suffix.size(), suffix) == 0; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | bool string_remove_suffix(std::string & str, const std::string_view & suffix) { | ||||||
|  |     bool has_suffix = string_ends_with(str, suffix); | ||||||
|  |     if (has_suffix) { | ||||||
|  |         str = str.substr(0, str.size() - suffix.size()); | ||||||
|  |     } | ||||||
|  |     return has_suffix; | ||||||
|  | } | ||||||
|  |  | ||||||
| size_t string_find_partial_stop(const std::string_view & str, const std::string_view & stop) { | size_t string_find_partial_stop(const std::string_view & str, const std::string_view & stop) { | ||||||
|     if (!str.empty() && !stop.empty()) { |     if (!str.empty() && !stop.empty()) { | ||||||
|         const char text_last_char = str.back(); |         const char text_last_char = str.back(); | ||||||
|   | |||||||
| @@ -518,6 +518,7 @@ static bool string_starts_with(const std::string & str, | |||||||
|  |  | ||||||
| // While we wait for C++20's std::string::ends_with... | // While we wait for C++20's std::string::ends_with... | ||||||
| bool string_ends_with(const std::string_view & str, const std::string_view & suffix); | bool string_ends_with(const std::string_view & str, const std::string_view & suffix); | ||||||
|  | bool string_remove_suffix(std::string & str, const std::string_view & suffix); | ||||||
| size_t string_find_partial_stop(const std::string_view & str, const std::string_view & stop); | size_t string_find_partial_stop(const std::string_view & str, const std::string_view & stop); | ||||||
|  |  | ||||||
| bool string_parse_kv_override(const char * data, std::vector<llama_model_kv_override> & overrides); | bool string_parse_kv_override(const char * data, std::vector<llama_model_kv_override> & overrides); | ||||||
|   | |||||||
| @@ -31,18 +31,6 @@ static void print_usage(int, char ** argv) { | |||||||
|     LOG("\n"); |     LOG("\n"); | ||||||
| } | } | ||||||
|  |  | ||||||
| static bool str_has_suffix(const std::string & str, const std::string & suffix) { |  | ||||||
|     return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), str.size(), suffix) == 0; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| static bool str_remove_suffix(std::string & str, const std::string & suffix) { |  | ||||||
|     bool has_suffix = str_has_suffix(str, suffix); |  | ||||||
|     if (has_suffix) { |  | ||||||
|         str = str.substr(0, str.size() - suffix.size()); |  | ||||||
|     } |  | ||||||
|     return has_suffix; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| static const char * const LLM_KV_IMATRIX_DATASETS    = "imatrix.datasets"; | static const char * const LLM_KV_IMATRIX_DATASETS    = "imatrix.datasets"; | ||||||
| static const char * const LLM_KV_IMATRIX_CHUNK_COUNT = "imatrix.chunk_count"; | static const char * const LLM_KV_IMATRIX_CHUNK_COUNT = "imatrix.chunk_count"; | ||||||
| static const char * const LLM_KV_IMATRIX_CHUNK_SIZE  = "imatrix.chunk_size"; | static const char * const LLM_KV_IMATRIX_CHUNK_SIZE  = "imatrix.chunk_size"; | ||||||
| @@ -362,7 +350,7 @@ void IMatrixCollector::save_imatrix(int32_t n_chunk) const { | |||||||
|     auto fname = m_params.out_file; |     auto fname = m_params.out_file; | ||||||
|  |  | ||||||
|     // TODO: use the new format by default also for .imatrix |     // TODO: use the new format by default also for .imatrix | ||||||
|     if (!str_has_suffix(fname, ".gguf")) { |     if (!string_ends_with(fname, ".gguf")) { | ||||||
|         this->save_imatrix_legacy(n_chunk); |         this->save_imatrix_legacy(n_chunk); | ||||||
|         return; |         return; | ||||||
|     } |     } | ||||||
| @@ -584,10 +572,10 @@ bool IMatrixCollector::load_imatrix(const char * file_name) { | |||||||
|  |  | ||||||
|         if (name.empty()) { continue; } |         if (name.empty()) { continue; } | ||||||
|  |  | ||||||
|         if (str_remove_suffix(name, in_sum2_suffix)) { |         if (string_remove_suffix(name, in_sum2_suffix)) { | ||||||
|             // in_sum2 |             // in_sum2 | ||||||
|             sums_counts_for[std::move(name)].first = cur; |             sums_counts_for[std::move(name)].first = cur; | ||||||
|         } else if (str_remove_suffix(name, counts_suffix)) { |         } else if (string_remove_suffix(name, counts_suffix)) { | ||||||
|             // counts |             // counts | ||||||
|             sums_counts_for[std::move(name)].second = cur; |             sums_counts_for[std::move(name)].second = cur; | ||||||
|         } else { |         } else { | ||||||
|   | |||||||
| @@ -147,15 +147,6 @@ static void usage(const char * executable) { | |||||||
|     exit(1); |     exit(1); | ||||||
| } | } | ||||||
|  |  | ||||||
| // TODO: share with implementation in imatrix.cpp |  | ||||||
| static bool str_remove_suffix(std::string & str, const std::string & suffix) { |  | ||||||
|     bool has_suffix = str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), str.size(), suffix) == 0; |  | ||||||
|     if (has_suffix) { |  | ||||||
|         str = str.substr(0, str.size() - suffix.size()); |  | ||||||
|     } |  | ||||||
|     return has_suffix; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| static int load_legacy_imatrix(const std::string & imatrix_file, std::vector<std::string> & imatrix_datasets, std::unordered_map<std::string, std::vector<float>> & imatrix_data) { | static int load_legacy_imatrix(const std::string & imatrix_file, std::vector<std::string> & imatrix_datasets, std::unordered_map<std::string, std::vector<float>> & imatrix_data) { | ||||||
|     std::ifstream in(imatrix_file.c_str(), std::ios::binary); |     std::ifstream in(imatrix_file.c_str(), std::ios::binary); | ||||||
|     if (!in) { |     if (!in) { | ||||||
| @@ -265,10 +256,10 @@ static int load_imatrix(const std::string & imatrix_file, std::vector<std::strin | |||||||
|  |  | ||||||
|         if (name.empty()) { continue; } |         if (name.empty()) { continue; } | ||||||
|  |  | ||||||
|         if (str_remove_suffix(name, sums_suffix)) { |         if (string_remove_suffix(name, sums_suffix)) { | ||||||
|             // in_sum2 |             // in_sum2 | ||||||
|             sums_counts_for[std::move(name)].first = cur; |             sums_counts_for[std::move(name)].first = cur; | ||||||
|         } else if (str_remove_suffix(name, counts_suffix)) { |         } else if (string_remove_suffix(name, counts_suffix)) { | ||||||
|             // counts |             // counts | ||||||
|             sums_counts_for[std::move(name)].second = cur; |             sums_counts_for[std::move(name)].second = cur; | ||||||
|         } else { |         } else { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Francis Couture-Harpin
					Francis Couture-Harpin