From 10ac60b37bfe900af1f4340b425509ac9caaf7a2 Mon Sep 17 00:00:00 2001 From: Clint Herron Date: Mon, 11 Mar 2024 17:46:05 -0400 Subject: [PATCH] Resolves #3878 by enforcing existence of root node before returning valid grammar structure. This is an alternate fix location to put the required root node in sampling.cpp instead of grammar-parser.cpp. --- common/sampling.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/common/sampling.cpp b/common/sampling.cpp index 823031febc7e2..5a54509827cbf 100644 --- a/common/sampling.cpp +++ b/common/sampling.cpp @@ -17,6 +17,13 @@ struct llama_sampling_context * llama_sampling_init(const struct llama_sampling_ return nullptr; } + // Ensure that there is a "root" node. + if (result->parsed_grammar.symbol_ids.find("root") == result->parsed_grammar.symbol_ids.end()) { + fprintf(stderr, "%s: grammar does not contain a 'root' symbol\n", __func__); + delete result; + return nullptr; + } + std::vector grammar_rules(result->parsed_grammar.c_rules()); result->grammar = llama_grammar_init(