@@ -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+
14361447static 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) {
14541465static 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
15361557struct llama_sampler_penalties {
0 commit comments