Skip to content

Commit 61991ca

Browse files
committed
cont : pre-fetch rules
1 parent 5a35294 commit 61991ca

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

examples/gbnf-validator/gbnf-validator.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,15 @@ static bool llama_sample_grammar_string(struct llama_grammar * grammar, const st
1616
auto decoded = decode_utf8(input_str, {});
1717
const auto & code_points = decoded.first;
1818

19-
llama_grammar_stacks & cur_stacks = llama_grammar_get_stacks(grammar);
19+
const llama_grammar_rules & prev_rules = llama_grammar_get_rules (grammar);
20+
llama_grammar_stacks & cur_stacks = llama_grammar_get_stacks(grammar);
2021

2122
size_t pos = 0;
2223
for (auto it = code_points.begin(), end = code_points.end() - 1; it != end; ++it) {
23-
const llama_grammar_rules & prev_rules = llama_grammar_get_rules (grammar);
24-
const llama_grammar_stacks prev_stacks = llama_grammar_get_stacks(grammar); // copy
24+
const llama_grammar_stacks prev_stacks = llama_grammar_get_stacks(grammar); // copy
25+
2526
llama_grammar_accept(prev_rules, prev_stacks, *it, cur_stacks);
27+
2628
if (cur_stacks.empty()) {
2729
error_pos = pos;
2830
error_msg = "Unexpected character '" + unicode_cpt_to_utf8(*it) + "'";

tests/test-grammar-integration.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,14 @@ static bool match_string(const std::string & input, llama_grammar * grammar) {
4949

5050
const auto & code_points = decoded.first;
5151

52-
llama_grammar_stacks & cur_stacks = llama_grammar_get_stacks(grammar);
52+
const llama_grammar_rules & prev_rules = llama_grammar_get_rules (grammar);
53+
llama_grammar_stacks & cur_stacks = llama_grammar_get_stacks(grammar);
5354

5455
for (auto it = code_points.begin(), end = code_points.end() - 1; it != end; ++it) {
55-
const llama_grammar_rules & prev_rules = llama_grammar_get_rules (grammar);
56-
const llama_grammar_stacks prev_stacks = llama_grammar_get_stacks(grammar); // copy
56+
const llama_grammar_stacks prev_stacks = llama_grammar_get_stacks(grammar); // copy
57+
5758
llama_grammar_accept(prev_rules, prev_stacks, *it, cur_stacks);
59+
5860
if (cur_stacks.empty()) {
5961
// no stacks means that the grammar failed to match at this point
6062
return false;

0 commit comments

Comments
 (0)