From 80553e53c075a2d4e1936735eb06368eba5f599d Mon Sep 17 00:00:00 2001 From: Clint Herron Date: Thu, 11 Apr 2024 13:57:10 -0400 Subject: [PATCH] Optimization: eliminate addition of redundant stacks when advancing grammar. --- llama.cpp | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/llama.cpp b/llama.cpp index ad07059c4533a..73ff607062884 100644 --- a/llama.cpp +++ b/llama.cpp @@ -11861,7 +11861,9 @@ static void llama_grammar_advance_stack( std::vector> & 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; } @@ -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