@@ -917,6 +917,10 @@ llama_grammar_stacks & llama_grammar_get_stacks(struct llama_grammar * grammar)
917
917
return grammar->stacks ;
918
918
}
919
919
920
+ llama_grammar_stacks_cache & llama_grammar_get_stacks_cache (struct llama_grammar * grammar) {
921
+ return grammar->stacks_cache ;
922
+ }
923
+
920
924
void llama_grammar_accept (
921
925
const llama_grammar_rules & rules,
922
926
const llama_grammar_stacks & stacks,
@@ -1058,7 +1062,7 @@ struct llama_grammar * llama_grammar_init_impl(
1058
1062
// Important: vec_rules has to be moved here, not copied, because stacks contains
1059
1063
// pointers to elements of vec_rules. If vec_rules were copied into llama_grammar
1060
1064
// 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), };
1062
1066
}
1063
1067
1064
1068
struct 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,
1137
1141
// Important: vec_rules has to be moved here, not copied, because stacks contains
1138
1142
// pointers to elements of vec_rules. If vec_rules were copied into llama_grammar
1139
1143
// 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), };
1141
1145
}
1142
1146
1143
1147
void llama_grammar_free_impl (struct llama_grammar * grammar) {
@@ -1225,10 +1229,9 @@ void llama_grammar_accept_impl(struct llama_grammar & grammar, llama_token token
1225
1229
const auto & code_points = decoded.first ;
1226
1230
1227
1231
llama_grammar_stacks stacks_new;
1228
- llama_grammar_stacks_cache stacks_cache;
1229
1232
1230
1233
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 );
1232
1235
grammar.stacks = std::move (stacks_new);
1233
1236
}
1234
1237
0 commit comments