Skip to content

Commit

Permalink
Optimization: eliminate addition of redundant stacks when advancing g…
Browse files Browse the repository at this point in the history
…rammar. (#6616)
  • Loading branch information
HanClinto authored Apr 12, 2024
1 parent f7001cc commit 04a5ac2
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions llama.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11861,7 +11861,9 @@ static void llama_grammar_advance_stack(
std::vector<std::vector<const llama_grammar_element *>> & new_stacks) {

if (stack.empty()) {
new_stacks.emplace_back(stack);
if (std::find(new_stacks.begin(), new_stacks.end(), stack) == new_stacks.end()) {
new_stacks.emplace_back(stack);
}
return;
}

Expand Down Expand Up @@ -11898,7 +11900,10 @@ static void llama_grammar_advance_stack(
}
case LLAMA_GRETYPE_CHAR:
case LLAMA_GRETYPE_CHAR_NOT:
new_stacks.emplace_back(stack);
if (std::find(new_stacks.begin(), new_stacks.end(), stack) == new_stacks.end()) {
// only add the stack if it's not a duplicate of one we already have
new_stacks.emplace_back(stack);
}
break;
default:
// end of alternate (LLAMA_GRETYPE_END, LLAMA_GRETYPE_ALT) or middle of char range
Expand Down

0 comments on commit 04a5ac2

Please sign in to comment.