mirror of
https://github.com/ggerganov/llama.cpp.git
synced 2025-01-14 04:24:30 +00:00
antiprompts
: fix gcc8 build (avoid recursive struct)
This commit is contained in:
parent
ef2a020276
commit
e6be59c2a0
@ -557,12 +557,19 @@ private:
|
|||||||
// The Aho–Corasick algorithm allows efficient string matching with multiple patterns.
|
// The Aho–Corasick algorithm allows efficient string matching with multiple patterns.
|
||||||
// See https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm
|
// See https://en.wikipedia.org/wiki/Aho%E2%80%93Corasick_algorithm
|
||||||
struct TrieNode {
|
struct TrieNode {
|
||||||
std::unordered_map<char, struct TrieNode> children;
|
std::unordered_map<char, TrieNode*> children;
|
||||||
struct TrieNode* fail = nullptr;
|
TrieNode* fail = nullptr;
|
||||||
int output = -1;
|
int output = -1;
|
||||||
size_t depth = 0;
|
size_t depth = 0;
|
||||||
|
|
||||||
|
~TrieNode() {
|
||||||
|
clear();
|
||||||
|
}
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
|
for (auto & pair : children) {
|
||||||
|
delete pair.second;
|
||||||
|
}
|
||||||
children.clear();
|
children.clear();
|
||||||
fail = nullptr;
|
fail = nullptr;
|
||||||
output = -1;
|
output = -1;
|
||||||
@ -581,11 +588,15 @@ private:
|
|||||||
const auto & pattern = antiprompts[i].value;
|
const auto & pattern = antiprompts[i].value;
|
||||||
for (size_t j = 0; j < pattern.length(); ++j) {
|
for (size_t j = 0; j < pattern.length(); ++j) {
|
||||||
char c = pattern[j];
|
char c = pattern[j];
|
||||||
auto & child = node->children[c];
|
auto it = node->children.find(c);
|
||||||
if (child.depth == 0) {
|
if (it != node->children.end()) {
|
||||||
child.depth = j + 1;
|
node = it->second;
|
||||||
|
} else {
|
||||||
|
node = node->children[c] = new TrieNode();
|
||||||
|
}
|
||||||
|
if (node->depth == 0) {
|
||||||
|
node->depth = j + 1;
|
||||||
}
|
}
|
||||||
node = &child;
|
|
||||||
}
|
}
|
||||||
node->output = i;
|
node->output = i;
|
||||||
}
|
}
|
||||||
@ -594,8 +605,8 @@ private:
|
|||||||
void build_failure_and_dict_links() {
|
void build_failure_and_dict_links() {
|
||||||
std::queue<TrieNode*> q;
|
std::queue<TrieNode*> q;
|
||||||
for (auto& child : root.children) {
|
for (auto& child : root.children) {
|
||||||
child.second.fail = &root;
|
child.second->fail = &root;
|
||||||
q.push(&child.second);
|
q.push(child.second);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!q.empty()) {
|
while (!q.empty()) {
|
||||||
@ -611,14 +622,14 @@ private:
|
|||||||
f = f->fail;
|
f = f->fail;
|
||||||
}
|
}
|
||||||
|
|
||||||
child.fail = (f == &root && f->children.find(c) == f->children.end())
|
child->fail = (f == &root && f->children.find(c) == f->children.end())
|
||||||
? &root : &f->children[c];
|
? &root : f->children[c];
|
||||||
|
|
||||||
if (child.fail->output != -1) {
|
if (child->fail->output != -1) {
|
||||||
child.output = child.fail->output;
|
child->output = child->fail->output;
|
||||||
}
|
}
|
||||||
|
|
||||||
q.push(&child);
|
q.push(child);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -703,7 +714,7 @@ private:
|
|||||||
}
|
}
|
||||||
auto it = current->children.find(c);
|
auto it = current->children.find(c);
|
||||||
if (it != current->children.end()) {
|
if (it != current->children.end()) {
|
||||||
current = &it->second;
|
current = it->second;
|
||||||
}
|
}
|
||||||
if (current->output != -1) {
|
if (current->output != -1) {
|
||||||
const auto & antiprompt = antiprompts[current->output];
|
const auto & antiprompt = antiprompts[current->output];
|
||||||
|
Loading…
Reference in New Issue
Block a user