From e6be59c2a09b173768c28ebed91f8006253c40d2 Mon Sep 17 00:00:00 2001 From: ochafik Date: Sat, 28 Sep 2024 19:39:52 +0100 Subject: [PATCH] `antiprompts`: fix gcc8 build (avoid recursive struct) --- common/common.h | 39 +++++++++++++++++++++++++-------------- 1 file changed, 25 insertions(+), 14 deletions(-) diff --git a/common/common.h b/common/common.h index b7a6c91811ed7..64192a9eb3d8f 100644 --- a/common/common.h +++ b/common/common.h @@ -557,12 +557,19 @@ class llama_antiprompts { // The Aho–Corasick algorithm allows efficient string matching with multiple patterns. // See https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm struct TrieNode { - std::unordered_map children; - struct TrieNode* fail = nullptr; + std::unordered_map children; + TrieNode* fail = nullptr; int output = -1; size_t depth = 0; + ~TrieNode() { + clear(); + } + void clear() { + for (auto & pair : children) { + delete pair.second; + } children.clear(); fail = nullptr; output = -1; @@ -581,11 +588,15 @@ class llama_antiprompts { const auto & pattern = antiprompts[i].value; for (size_t j = 0; j < pattern.length(); ++j) { char c = pattern[j]; - auto & child = node->children[c]; - if (child.depth == 0) { - child.depth = j + 1; + auto it = node->children.find(c); + if (it != node->children.end()) { + node = it->second; + } else { + node = node->children[c] = new TrieNode(); + } + if (node->depth == 0) { + node->depth = j + 1; } - node = &child; } node->output = i; } @@ -594,8 +605,8 @@ class llama_antiprompts { void build_failure_and_dict_links() { std::queue q; for (auto& child : root.children) { - child.second.fail = &root; - q.push(&child.second); + child.second->fail = &root; + q.push(child.second); } while (!q.empty()) { @@ -611,14 +622,14 @@ class llama_antiprompts { f = f->fail; } - child.fail = (f == &root && f->children.find(c) == f->children.end()) - ? &root : &f->children[c]; + child->fail = (f == &root && f->children.find(c) == f->children.end()) + ? &root : f->children[c]; - if (child.fail->output != -1) { - child.output = child.fail->output; + if (child->fail->output != -1) { + child->output = child->fail->output; } - q.push(&child); + q.push(child); } } } @@ -703,7 +714,7 @@ class llama_antiprompts { } auto it = current->children.find(c); if (it != current->children.end()) { - current = &it->second; + current = it->second; } if (current->output != -1) { const auto & antiprompt = antiprompts[current->output];