LLM inference in C/C++
C++ ggml
grammar : coalesce find + insert into a single insert and adjust move/copy mechanics (#26885)
Clint Herron7a16a6ce326f
diff --git a/src/llama-grammar.cpp b/src/llama-grammar.cppindex 6aa03c7666..deffec8c07 100644--- a/src/llama-grammar.cpp+++ b/src/llama-grammar.cpp@@ -871,17 +871,18 @@ static void llama_grammar_advance_stack(std::set<llama_grammar_stack, decltype(stack_cmp)> seen(stack_cmp);while (!todo.empty()) {- llama_grammar_stack curr_stack = std::move(todo.back());+ llama_grammar_stack curr_stack_candidate = std::move(todo.back());todo.pop_back();- if (seen.find( curr_stack) != seen.end()) {+ auto [curr_stack_it, inserted] = seen.insert(std::move(curr_stack_candidate));+ if (!inserted) {continue;}- seen.insert(curr_stack);+ const llama_grammar_stack & curr_stack = *curr_stack_it;if (curr_stack.empty()) {if (std::find(new_stacks.begin(), new_stacks.end(), curr_stack) == new_stacks.end()) {- new_stacks.emplace_back(std::move(curr_stack));+ new_stacks.emplace_back(curr_stack);}continue;}@@ -924,7 +925,7 @@ static void llama_grammar_advance_stack(case LLAMA_GRETYPE_TOKEN_NOT:if (std::find(new_stacks.begin(), new_stacks.end(), curr_stack) == new_stacks.end()) {// only add the stack if it's not a duplicate of one we already have- new_stacks.emplace_back(std::move(curr_stack));+ new_stacks.emplace_back(curr_stack);}break;default: