Skip to content

Commit

Permalink
llama : better replace_all (cont)
Browse files Browse the repository at this point in the history
ggml-ci
  • Loading branch information
ggerganov committed Aug 8, 2024
1 parent ebd541a commit 59952cb
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 30 deletions.
17 changes: 7 additions & 10 deletions examples/export-lora/export-lora.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,14 @@ static struct gguf_context * load_gguf(std::string & fname, struct ggml_context
}

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);
}

struct file_input {
Expand Down
17 changes: 7 additions & 10 deletions examples/llava/clip.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,17 +200,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) {
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) {
Expand Down
17 changes: 7 additions & 10 deletions src/llama-vocab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,14 @@
//

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);
}

LLAMA_ATTRIBUTE_FORMAT(1, 2)
Expand Down

0 comments on commit 59952cb

Please sign in to comment.