Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Grammar optimization: eliminate redundant grammar trees (~4x faster grammar sampling) #6616

Merged
merged 1 commit into from
Apr 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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()) {
HanClinto marked this conversation as resolved.
Show resolved Hide resolved
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
Loading