Browse Source

llama : better replace_all (cont) (#8926)

* llama : better replace_all (cont)

ggml-ci

* code : deduplicate replace_all

ggml-ci
Georgi Gerganov 1 year ago
parent
commit
45a55b91aa
7 changed files with 35 additions and 49 deletions
  1. 11 0
      common/common.cpp
  2. 2 0
      common/common.h
  3. 0 14
      examples/export-lora/export-lora.cpp
  4. 7 10
      examples/llava/clip.cpp
  5. 15 0
      src/llama-impl.h
  6. 0 14
      src/llama-vocab.cpp
  7. 0 11
      src/llama.cpp

+ 11 - 0
common/common.cpp

@@ -1777,6 +1777,17 @@ std::string string_get_sortable_timestamp() {
     return std::string(timestamp_no_ns) + "." + std::string(timestamp_ns);
     return std::string(timestamp_no_ns) + "." + std::string(timestamp_ns);
 }
 }
 
 
+void string_replace_all(std::string & s, const std::string & search, const std::string & replace) {
+    if (search.empty()) {
+        return; // Avoid infinite loop if 'search' is an empty string
+    }
+    size_t pos = 0;
+    while ((pos = s.find(search, pos)) != std::string::npos) {
+        s.replace(pos, search.length(), replace);
+        pos += replace.length();
+    }
+}
+
 void string_process_escapes(std::string & input) {
 void string_process_escapes(std::string & input) {
     std::size_t input_len = input.length();
     std::size_t input_len = input.length();
     std::size_t output_idx = 0;
     std::size_t output_idx = 0;

+ 2 - 0
common/common.h

@@ -286,6 +286,8 @@ std::vector<std::string> string_split(std::string input, char separator);
 std::string string_strip(const std::string & str);
 std::string string_strip(const std::string & str);
 std::string string_get_sortable_timestamp();
 std::string string_get_sortable_timestamp();
 
 
+void string_replace_all(std::string & s, const std::string & search, const std::string & replace);
+
 template<class T>
 template<class T>
 static std::vector<T> string_split(const std::string & str, char delim) {
 static std::vector<T> string_split(const std::string & str, char delim) {
     std::vector<T> values;
     std::vector<T> values;

+ 0 - 14
examples/export-lora/export-lora.cpp

@@ -50,20 +50,6 @@ static struct gguf_context * load_gguf(std::string & fname, struct ggml_context
     return ctx_gguf;
     return ctx_gguf;
 }
 }
 
 
-static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
-    std::string result;
-    for (size_t pos = 0; ; pos += search.length()) {
-        auto new_pos = s.find(search, pos);
-        if (new_pos == std::string::npos) {
-            result += s.substr(pos, s.size() - pos);
-            break;
-        }
-        result += s.substr(pos, new_pos - pos) + replace;
-        pos = new_pos;
-    }
-    s = std::move(result);
-}
-
 struct file_input {
 struct file_input {
     struct ggml_context * ctx_meta = nullptr;
     struct ggml_context * ctx_meta = nullptr;
     struct gguf_context * ctx_gguf = nullptr;
     struct gguf_context * ctx_gguf = nullptr;

+ 7 - 10
examples/llava/clip.cpp

@@ -210,17 +210,14 @@ static std::string gguf_data_to_str(enum gguf_type type, const void * data, int
 }
 }
 
 
 static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
 static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
-    std::string result;
-    for (size_t pos = 0; ; pos += search.length()) {
-        auto new_pos = s.find(search, pos);
-        if (new_pos == std::string::npos) {
-            result += s.substr(pos, s.size() - pos);
-            break;
-        }
-        result += s.substr(pos, new_pos - pos) + replace;
-        pos = new_pos;
+    if (search.empty()) {
+        return; // Avoid infinite loop if 'search' is an empty string
+    }
+    size_t pos = 0;
+    while ((pos = s.find(search, pos)) != std::string::npos) {
+        s.replace(pos, search.length(), replace);
+        pos += replace.length();
     }
     }
-    s = std::move(result);
 }
 }
 
 
 static std::string gguf_kv_to_str(const struct gguf_context * ctx_gguf, int i) {
 static std::string gguf_kv_to_str(const struct gguf_context * ctx_gguf, int i) {

+ 15 - 0
src/llama-impl.h

@@ -24,3 +24,18 @@ void llama_log_callback_default(ggml_log_level level, const char * text, void *
 #define LLAMA_LOG_INFO(...)  llama_log_internal(GGML_LOG_LEVEL_INFO , __VA_ARGS__)
 #define LLAMA_LOG_INFO(...)  llama_log_internal(GGML_LOG_LEVEL_INFO , __VA_ARGS__)
 #define LLAMA_LOG_WARN(...)  llama_log_internal(GGML_LOG_LEVEL_WARN , __VA_ARGS__)
 #define LLAMA_LOG_WARN(...)  llama_log_internal(GGML_LOG_LEVEL_WARN , __VA_ARGS__)
 #define LLAMA_LOG_ERROR(...) llama_log_internal(GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
 #define LLAMA_LOG_ERROR(...) llama_log_internal(GGML_LOG_LEVEL_ERROR, __VA_ARGS__)
+
+//
+// helpers
+//
+
+static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
+    if (search.empty()) {
+        return; // Avoid infinite loop if 'search' is an empty string
+    }
+    size_t pos = 0;
+    while ((pos = s.find(search, pos)) != std::string::npos) {
+        s.replace(pos, search.length(), replace);
+        pos += replace.length();
+    }
+}

+ 0 - 14
src/llama-vocab.cpp

@@ -16,20 +16,6 @@
 // helpers
 // helpers
 //
 //
 
 
-static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
-    std::string result;
-    for (size_t pos = 0; ; pos += search.length()) {
-        auto new_pos = s.find(search, pos);
-        if (new_pos == std::string::npos) {
-            result += s.substr(pos, s.size() - pos);
-            break;
-        }
-        result += s.substr(pos, new_pos - pos) + replace;
-        pos = new_pos;
-    }
-    s = std::move(result);
-}
-
 LLAMA_ATTRIBUTE_FORMAT(1, 2)
 LLAMA_ATTRIBUTE_FORMAT(1, 2)
 static std::string format(const char * fmt, ...) {
 static std::string format(const char * fmt, ...) {
     va_list ap;
     va_list ap;

+ 0 - 11
src/llama.cpp

@@ -121,17 +121,6 @@ static std::string trim(const std::string & str) {
     return str.substr(start, end - start);
     return str.substr(start, end - start);
 }
 }
 
 
-static void replace_all(std::string & s, const std::string & search, const std::string & replace) {
-    if (search.empty()) {
-        return; // Avoid infinite loop if 'search' is an empty string
-    }
-    size_t pos = 0;
-    while ((pos = s.find(search, pos)) != std::string::npos) {
-        s.replace(pos, search.length(), replace);
-        pos += replace.length();
-    }
-}
-
 static bool is_float_close(float a, float b, float abs_tol) {
 static bool is_float_close(float a, float b, float abs_tol) {
     // Check for non-negative tolerance
     // Check for non-negative tolerance
     if (abs_tol < 0.0) {
     if (abs_tol < 0.0) {