From ca5d21c17a6875336cae4a1ae91a2c02d9fc35bf Mon Sep 17 00:00:00 2001 From: Georgi Gerganov Date: Wed, 4 Sep 2024 14:26:23 +0300 Subject: [PATCH] grammar : fix reset call ggml-ci --- examples/batched.swift/Sources/main.swift | 3 --- src/llama-sampling.cpp | 13 +++++++------ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/examples/batched.swift/Sources/main.swift b/examples/batched.swift/Sources/main.swift index 4d73ccd24..380040e57 100644 --- a/examples/batched.swift/Sources/main.swift +++ b/examples/batched.swift/Sources/main.swift @@ -51,9 +51,6 @@ defer { } var sparams = llama_sampler_params() -sparams.top_k = 40 -sparams.top_p = 0.9 -sparams.temp = 0.4 let smpl = llama_sampler_init(model, sparams) guard smpl != nil else { diff --git a/src/llama-sampling.cpp b/src/llama-sampling.cpp index eca2adb2b..a134fda95 100644 --- a/src/llama-sampling.cpp +++ b/src/llama-sampling.cpp @@ -724,17 +724,18 @@ static struct llama_constraint_i llama_constraint_grammar_i = { }, /* .reset = */ [](struct llama_constraint * cnstr) { auto * ctx = (llama_constraint_context_grammar *) cnstr->ctx; - if (ctx->grammar) { - llama_grammar_free_impl(ctx->grammar); - ctx->grammar = nullptr; + if (!ctx->grammar) { + return; } - if (!ctx->grammar_str.empty()) { - ctx->grammar = llama_grammar_init_impl(nullptr, ctx->grammar_str.c_str(), ctx->grammar_root.c_str()); - } + auto * grammar_new = llama_grammar_init_impl(ctx->grammar->vocab, ctx->grammar_str.c_str(), ctx->grammar_root.c_str()); + + llama_grammar_free_impl(ctx->grammar); + ctx->grammar = grammar_new; }, /* .copy = */ [](const struct llama_constraint * cnstr) { const auto * ctx_src = (const llama_constraint_context_grammar *) cnstr->ctx; + auto * result = llama_constraint_init_grammar_impl(*ctx_src->grammar->vocab, nullptr, nullptr); auto * ctx_dst = (llama_constraint_context_grammar *) result->ctx;