diff --git a/common/common.cpp b/common/common.cpp index e4e71ad13f..66462f5d93 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -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) { 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) { if (!str.empty() && !stop.empty()) { const char text_last_char = str.back(); diff --git a/common/common.h b/common/common.h index e08a59eae7..d6e5411e10 100644 --- a/common/common.h +++ b/common/common.h @@ -518,6 +518,7 @@ static bool string_starts_with(const std::string & str, // 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_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); bool string_parse_kv_override(const char * data, std::vector & overrides); diff --git a/tools/imatrix/imatrix.cpp b/tools/imatrix/imatrix.cpp index 37152070d8..1a16ae2c08 100644 --- a/tools/imatrix/imatrix.cpp +++ b/tools/imatrix/imatrix.cpp @@ -31,18 +31,6 @@ static void print_usage(int, char ** argv) { 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_CHUNK_COUNT = "imatrix.chunk_count"; 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; // 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); return; } @@ -584,10 +572,10 @@ bool IMatrixCollector::load_imatrix(const char * file_name) { if (name.empty()) { continue; } - if (str_remove_suffix(name, in_sum2_suffix)) { + if (string_remove_suffix(name, in_sum2_suffix)) { // in_sum2 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 sums_counts_for[std::move(name)].second = cur; } else { diff --git a/tools/quantize/quantize.cpp b/tools/quantize/quantize.cpp index 69b03f504a..45c59ecb6f 100644 --- a/tools/quantize/quantize.cpp +++ b/tools/quantize/quantize.cpp @@ -147,15 +147,6 @@ static void usage(const char * executable) { 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 & imatrix_datasets, std::unordered_map> & imatrix_data) { std::ifstream in(imatrix_file.c_str(), std::ios::binary); if (!in) { @@ -265,10 +256,10 @@ static int load_imatrix(const std::string & imatrix_file, std::vector