Skip to content

Commit e33de12

Browse files
compiladeCISC
andcommitted
common : move string_remove_suffix from quantize and imatrix
Co-authored-by: Sigbjørn Skjæret <[email protected]>
1 parent 118d52f commit e33de12

File tree

4 files changed

+15
-26
lines changed

4 files changed

+15
-26
lines changed

common/common.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,15 @@ void string_replace_all(std::string & s, const std::string & search, const std::
448448
bool string_ends_with(const std::string_view & str, const std::string_view & suffix) {
449449
return str.size() >= suffix.size() && str.compare(str.size()-suffix.size(), suffix.size(), suffix) == 0;
450450
}
451+
452+
bool string_remove_suffix(std::string & str, const std::string_view & suffix) {
453+
bool has_suffix = string_ends_with(str, suffix);
454+
if (has_suffix) {
455+
str = str.substr(0, str.size() - suffix.size());
456+
}
457+
return has_suffix;
458+
}
459+
451460
size_t string_find_partial_stop(const std::string_view & str, const std::string_view & stop) {
452461
if (!str.empty() && !stop.empty()) {
453462
const char text_last_char = str.back();

common/common.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,7 @@ static bool string_starts_with(const std::string & str,
518518

519519
// While we wait for C++20's std::string::ends_with...
520520
bool string_ends_with(const std::string_view & str, const std::string_view & suffix);
521+
bool string_remove_suffix(std::string & str, const std::string_view & suffix);
521522
size_t string_find_partial_stop(const std::string_view & str, const std::string_view & stop);
522523

523524
bool string_parse_kv_override(const char * data, std::vector<llama_model_kv_override> & overrides);

tools/imatrix/imatrix.cpp

Lines changed: 3 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -31,18 +31,6 @@ static void print_usage(int, char ** argv) {
3131
LOG("\n");
3232
}
3333

34-
static bool str_has_suffix(const std::string & str, const std::string & suffix) {
35-
return str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), str.size(), suffix) == 0;
36-
}
37-
38-
static bool str_remove_suffix(std::string & str, const std::string & suffix) {
39-
bool has_suffix = str_has_suffix(str, suffix);
40-
if (has_suffix) {
41-
str = str.substr(0, str.size() - suffix.size());
42-
}
43-
return has_suffix;
44-
}
45-
4634
static const char * const LLM_KV_IMATRIX_DATASETS = "imatrix.datasets";
4735
static const char * const LLM_KV_IMATRIX_CHUNK_COUNT = "imatrix.chunk_count";
4836
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 {
362350
auto fname = m_params.out_file;
363351

364352
// TODO: use the new format by default also for .imatrix
365-
if (!str_has_suffix(fname, ".gguf")) {
353+
if (!string_ends_with(fname, ".gguf")) {
366354
this->save_imatrix_legacy(n_chunk);
367355
return;
368356
}
@@ -584,10 +572,10 @@ bool IMatrixCollector::load_imatrix(const char * file_name) {
584572

585573
if (name.empty()) { continue; }
586574

587-
if (str_remove_suffix(name, in_sum2_suffix)) {
575+
if (string_remove_suffix(name, in_sum2_suffix)) {
588576
// in_sum2
589577
sums_counts_for[std::move(name)].first = cur;
590-
} else if (str_remove_suffix(name, counts_suffix)) {
578+
} else if (string_remove_suffix(name, counts_suffix)) {
591579
// counts
592580
sums_counts_for[std::move(name)].second = cur;
593581
} else {

tools/quantize/quantize.cpp

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -147,15 +147,6 @@ static void usage(const char * executable) {
147147
exit(1);
148148
}
149149

150-
// TODO: share with implementation in imatrix.cpp
151-
static bool str_remove_suffix(std::string & str, const std::string & suffix) {
152-
bool has_suffix = str.size() >= suffix.size() && str.compare(str.size() - suffix.size(), str.size(), suffix) == 0;
153-
if (has_suffix) {
154-
str = str.substr(0, str.size() - suffix.size());
155-
}
156-
return has_suffix;
157-
}
158-
159150
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) {
160151
std::ifstream in(imatrix_file.c_str(), std::ios::binary);
161152
if (!in) {
@@ -265,10 +256,10 @@ static int load_imatrix(const std::string & imatrix_file, std::vector<std::strin
265256

266257
if (name.empty()) { continue; }
267258

268-
if (str_remove_suffix(name, sums_suffix)) {
259+
if (string_remove_suffix(name, sums_suffix)) {
269260
// in_sum2
270261
sums_counts_for[std::move(name)].first = cur;
271-
} else if (str_remove_suffix(name, counts_suffix)) {
262+
} else if (string_remove_suffix(name, counts_suffix)) {
272263
// counts
273264
sums_counts_for[std::move(name)].second = cur;
274265
} else {

0 commit comments

Comments
 (0)