Skip to content

Commit

Permalink
cont : pre-fetch rules
Browse files Browse the repository at this point in the history
  • Loading branch information
ggerganov committed Jul 16, 2024
1 parent 3ec1ab8 commit 72fd8fb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
8 changes: 5 additions & 3 deletions examples/gbnf-validator/gbnf-validator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ static bool llama_sample_grammar_string(struct llama_grammar * grammar, const st
auto decoded = decode_utf8(input_str, {});
const auto & code_points = decoded.first;

llama_grammar_stacks & cur_stacks = llama_grammar_get_stacks(grammar);
const llama_grammar_rules & prev_rules = llama_grammar_get_rules (grammar);
llama_grammar_stacks & cur_stacks = llama_grammar_get_stacks(grammar);

size_t pos = 0;
for (auto it = code_points.begin(), end = code_points.end() - 1; it != end; ++it) {
const llama_grammar_rules & prev_rules = llama_grammar_get_rules (grammar);
const llama_grammar_stacks prev_stacks = llama_grammar_get_stacks(grammar); // copy
const llama_grammar_stacks prev_stacks = llama_grammar_get_stacks(grammar); // copy

llama_grammar_accept(prev_rules, prev_stacks, *it, cur_stacks);

if (cur_stacks.empty()) {
error_pos = pos;
error_msg = "Unexpected character '" + unicode_cpt_to_utf8(*it) + "'";
Expand Down
8 changes: 5 additions & 3 deletions tests/test-grammar-integration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,14 @@ static bool match_string(const std::string & input, llama_grammar * grammar) {

const auto & code_points = decoded.first;

llama_grammar_stacks & cur_stacks = llama_grammar_get_stacks(grammar);
const llama_grammar_rules & prev_rules = llama_grammar_get_rules (grammar);
llama_grammar_stacks & cur_stacks = llama_grammar_get_stacks(grammar);

for (auto it = code_points.begin(), end = code_points.end() - 1; it != end; ++it) {
const llama_grammar_rules & prev_rules = llama_grammar_get_rules (grammar);
const llama_grammar_stacks prev_stacks = llama_grammar_get_stacks(grammar); // copy
const llama_grammar_stacks prev_stacks = llama_grammar_get_stacks(grammar); // copy

llama_grammar_accept(prev_rules, prev_stacks, *it, cur_stacks);

if (cur_stacks.empty()) {
// no stacks means that the grammar failed to match at this point
return false;
Expand Down

0 comments on commit 72fd8fb

Please sign in to comment.