@@ -917,6 +917,10 @@ llama_grammar_stacks & llama_grammar_get_stacks(struct llama_grammar * grammar)
917917 return grammar->stacks ;
918918}
919919
920+ llama_grammar_stacks_cache & llama_grammar_get_stacks_cache (struct llama_grammar * grammar) {
921+ return grammar->stacks_cache ;
922+ }
923+
920924void llama_grammar_accept (
921925 const llama_grammar_rules & rules,
922926 const llama_grammar_stacks & stacks,
@@ -1058,7 +1062,7 @@ struct llama_grammar * llama_grammar_init_impl(
10581062 // Important: vec_rules has to be moved here, not copied, because stacks contains
10591063 // pointers to elements of vec_rules. If vec_rules were copied into llama_grammar
10601064 // then the pointers would be invalidated when the local vec_rules goes out of scope.
1061- return new llama_grammar { vocab, std::move (vec_rules), std::move (stacks), {}, };
1065+ return new llama_grammar { vocab, std::move (vec_rules), std::move (stacks), {}, std::move (stacks_cache), };
10621066}
10631067
10641068struct llama_grammar * llama_grammar_init_impl (const struct llama_vocab * vocab, const char * grammar_str, const char * grammar_root) {
@@ -1137,7 +1141,7 @@ struct llama_grammar * llama_grammar_init_impl(const struct llama_vocab * vocab,
11371141 // Important: vec_rules has to be moved here, not copied, because stacks contains
11381142 // pointers to elements of vec_rules. If vec_rules were copied into llama_grammar
11391143 // then the pointers would be invalidated when the local vec_rules goes out of scope.
1140- return new llama_grammar { vocab, std::move (vec_rules), std::move (stacks), {}, };
1144+ return new llama_grammar { vocab, std::move (vec_rules), std::move (stacks), {}, std::move (stacks_cache), };
11411145}
11421146
11431147void llama_grammar_free_impl (struct llama_grammar * grammar) {
@@ -1225,10 +1229,9 @@ void llama_grammar_accept_impl(struct llama_grammar & grammar, llama_token token
12251229 const auto & code_points = decoded.first ;
12261230
12271231 llama_grammar_stacks stacks_new;
1228- llama_grammar_stacks_cache stacks_cache;
12291232
12301233 for (auto it = code_points.begin (), end = code_points.end () - 1 ; it != end; ++it) {
1231- llama_grammar_accept (grammar.rules , grammar.stacks , *it, stacks_new, stacks_cache);
1234+ llama_grammar_accept (grammar.rules , grammar.stacks , *it, stacks_new, grammar. stacks_cache );
12321235 grammar.stacks = std::move (stacks_new);
12331236 }
12341237
0 commit comments