Skip to content
Open
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
16 changes: 13 additions & 3 deletions src/llama-grammar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,16 @@
#include <set>
#include <stdexcept>

#define MAX_REPETITION_THRESHOLD 2000
#define MAX_REPETITION_THRESHOLD_DEFAULT 2000

static uint64_t get_max_repetition_threshold() {
const char * env = getenv("LLAMA_GRAMMAR_MAX_REPETITIONS");
if (env) {
uint64_t val = strtoull(env, nullptr, 10);
return val > 0 ? val : MAX_REPETITION_THRESHOLD_DEFAULT;
}
return MAX_REPETITION_THRESHOLD_DEFAULT;
}
//
// helpers
//
Expand Down Expand Up @@ -491,7 +500,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 >= get_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 +658,8 @@ 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)) {
const uint64_t max_rep = get_max_repetition_threshold();
if (min_times > max_rep || (has_max && max_times > max_rep)) {
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