Skip to content

Commit 5a64af6

Browse files
author
Olivier Chafik
committed
add llama_sampler_init_grammar_lazy instead of renaming the non-lazy
1 parent 7d59bf4 commit 5a64af6

File tree

3 files changed

+42
-19
lines changed

3 files changed

+42
-19
lines changed

common/sampling.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,11 @@ struct common_sampler * common_sampler_init(const struct llama_model * model, co
158158
}
159159
auto * result = new common_sampler {
160160
/* .params = */ params,
161-
/* .grmr = */ llama_sampler_grammar_init(vocab, params.grammar.c_str(), "root",
162-
params.grammar_lazy,
163-
trigger_words.data(), trigger_words.size(),
164-
params.grammar_trigger_tokens.data(), params.grammar_trigger_tokens.size()),
161+
/* .grmr = */ params.grammar_lazy
162+
? llama_sampler_init_grammar_lazy(vocab, params.grammar.c_str(), "root",
163+
trigger_words.data(), trigger_words.size(),
164+
params.grammar_trigger_tokens.data(), params.grammar_trigger_tokens.size())
165+
: llama_sampler_init_grammar(vocab, params.grammar.c_str(), "root"),
165166
/* .chain = */ llama_sampler_chain_init(lparams),
166167
/* .prev = */ ring_buffer<llama_token>(std::max(32, params.n_prev)),
167168
/* .cur = */ {},

include/llama.h

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,17 +1194,18 @@ extern "C" {
11941194
float tau,
11951195
float eta);
11961196

1197-
DEPRECATED(LLAMA_API struct llama_sampler * llama_sampler_init_grammar(
1197+
LLAMA_API struct llama_sampler * llama_sampler_init_grammar(
11981198
const struct llama_vocab * vocab,
11991199
const char * grammar_str,
1200-
const char * grammar_root),
1201-
"use llama_sampler_grammar_init instead");
1200+
const char * grammar_root);
12021201

1203-
LLAMA_API struct llama_sampler * llama_sampler_grammar_init(
1202+
/// @details Lazy grammar sampler, introduced in https://github.com/ggerganov/llama.cpp/pull/9639
1203+
/// @param trigger_words A list of words that will trigger the grammar sampler. This may be updated to a loose regex syntax (w/ ^) in a near future.
1204+
/// @param trigger_tokens A list of tokens that will trigger the grammar sampler.
1205+
LLAMA_API struct llama_sampler * llama_sampler_init_grammar_lazy(
12041206
const struct llama_vocab * vocab,
12051207
const char * grammar_str,
12061208
const char * grammar_root,
1207-
bool lazy,
12081209
const char ** trigger_words,
12091210
size_t num_trigger_words,
12101211
const llama_token * trigger_tokens,

src/llama-sampling.cpp

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1433,6 +1433,17 @@ static void llama_sampler_grammar_apply(struct llama_sampler * smpl, llama_token
14331433
}
14341434
}
14351435

1436+
// Fwd declare to break reset --> init_impl --> llama_sampler_grammar_i --> reset cycle.
1437+
static struct llama_sampler * llama_sampler_init_grammar_impl(
1438+
const struct llama_vocab * vocab,
1439+
const char * grammar_str,
1440+
const char * grammar_root,
1441+
bool lazy,
1442+
const char ** trigger_words,
1443+
size_t num_trigger_words,
1444+
const llama_token * trigger_tokens,
1445+
size_t num_trigger_tokens);
1446+
14361447
static void llama_sampler_grammar_reset(struct llama_sampler * smpl) {
14371448
auto * ctx = (llama_sampler_grammar *) smpl->ctx;
14381449
if (!ctx->grammar) {
@@ -1454,7 +1465,7 @@ static void llama_sampler_grammar_reset(struct llama_sampler * smpl) {
14541465
static struct llama_sampler * llama_sampler_grammar_clone(const struct llama_sampler * smpl) {
14551466
const auto * ctx = (const llama_sampler_grammar *) smpl->ctx;
14561467

1457-
auto * result = llama_sampler_grammar_init(ctx->vocab, nullptr, nullptr, false, nullptr, 0, nullptr, 0);
1468+
auto * result = llama_sampler_init_grammar_impl(ctx->vocab, nullptr, nullptr, false, nullptr, 0, nullptr, 0);
14581469

14591470
// copy the state
14601471
{
@@ -1490,15 +1501,7 @@ static struct llama_sampler_i llama_sampler_grammar_i = {
14901501
/* .free = */ llama_sampler_grammar_free,
14911502
};
14921503

1493-
1494-
struct llama_sampler * llama_sampler_init_grammar(
1495-
const struct llama_vocab * vocab,
1496-
const char * grammar_str,
1497-
const char * grammar_root) {
1498-
return llama_sampler_grammar_init(vocab, grammar_str, grammar_root, false, nullptr, 0, nullptr, 0);
1499-
}
1500-
1501-
struct llama_sampler * llama_sampler_grammar_init(
1504+
static struct llama_sampler * llama_sampler_init_grammar_impl(
15021505
const struct llama_vocab * vocab,
15031506
const char * grammar_str,
15041507
const char * grammar_root,
@@ -1531,6 +1534,24 @@ struct llama_sampler * llama_sampler_grammar_init(
15311534
};
15321535
}
15331536

1537+
struct llama_sampler * llama_sampler_init_grammar(
1538+
const struct llama_vocab * vocab,
1539+
const char * grammar_str,
1540+
const char * grammar_root) {
1541+
return llama_sampler_init_grammar_impl(vocab, grammar_str, grammar_root, /* lazy= */ false, nullptr, 0, nullptr, 0);
1542+
}
1543+
1544+
struct llama_sampler * llama_sampler_init_grammar_lazy(
1545+
const struct llama_vocab * vocab,
1546+
const char * grammar_str,
1547+
const char * grammar_root,
1548+
const char ** trigger_words,
1549+
size_t num_trigger_words,
1550+
const llama_token * trigger_tokens,
1551+
size_t num_trigger_tokens) {
1552+
return llama_sampler_init_grammar_impl(vocab, grammar_str, grammar_root, /* lazy= */ true, trigger_words, num_trigger_words, trigger_tokens, num_trigger_tokens);
1553+
}
1554+
15341555
// penalties
15351556

15361557
struct llama_sampler_penalties {

0 commit comments

Comments
 (0)