Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/llama-grammar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,22 @@
#include <cmath>
#include <algorithm>
#include <cstdint>
#include <cstdlib>
#include <set>
#include <stdexcept>

#define MAX_REPETITION_THRESHOLD 2000
static constexpr uint64_t DEFAULT_MAX_REPETITION_THRESHOLD = 50000;

llama_grammar_parser::llama_grammar_parser(const struct llama_vocab * vocab)
: vocab(vocab) {
const char * env = std::getenv("LLAMA_GRAMMAR_MAX_REPS");
if (env) {
max_repetition_threshold = std::stoull(env);
} else {
max_repetition_threshold = DEFAULT_MAX_REPETITION_THRESHOLD;
}
}

//
// helpers
//
Expand Down Expand Up @@ -491,7 +503,7 @@ const char * llama_grammar_parser::parse_sequence(
total_rules = min_times;
}

if (n_prev_rules * total_rules >= MAX_REPETITION_THRESHOLD) {
if (n_prev_rules * total_rules >= max_repetition_threshold) {
throw std::runtime_error("number of rules that are going to be repeated multiplied by the new repetition exceeds sane defaults, please reduce the number of repetitions or rule complexity");
}

Expand Down Expand Up @@ -649,7 +661,7 @@ const char * llama_grammar_parser::parse_sequence(
throw std::runtime_error(std::string("expecting ',' at ") + pos);
}
bool has_max = max_times != UINT64_MAX;
if (min_times > MAX_REPETITION_THRESHOLD || (has_max && max_times > MAX_REPETITION_THRESHOLD)) {
if (min_times > max_repetition_threshold || (has_max && max_times > max_repetition_threshold)) {
throw std::runtime_error(std::string("number of repetitions exceeds sane defaults, please reduce the number of repetitions"));
}
handle_repetitions(min_times, max_times);
Expand Down
5 changes: 4 additions & 1 deletion src/llama-grammar.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ struct llama_grammar_parser {

llama_grammar_rules rules;

llama_grammar_parser(const struct llama_vocab * vocab = nullptr) : vocab(vocab) {}
uint64_t max_repetition_threshold;

llama_grammar_parser(const struct llama_vocab * vocab = nullptr);


llama_grammar_stack c_rules() const;

Expand Down
2 changes: 1 addition & 1 deletion tests/test-grammar-parser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ int main()
)""");

verify_failure(R"""(
root ::= (((((([^x]*){0,99}){0,99}){0,99}){0,99}){0,99}){0,99}
root ::= (((((([^x]*){0,999}){0,999}){0,999}){0,999}){0,999}){0,999}
)""");

verify_failure(R"""(
Expand Down
Loading