mirror of
				https://github.com/ggml-org/llama.cpp.git
				synced 2025-10-31 08:51:55 +00:00 
			
		
		
		
	llama : add llama_sampling API + move grammar in libllama
ggml-ci
This commit is contained in:
		| @@ -1,9 +1,5 @@ | ||||
| #define LLAMA_API_INTERNAL | ||||
|  | ||||
| #include "grammar-parser.h" | ||||
| #include "ggml.h" | ||||
| #include "llama.h" | ||||
| #include "unicode.h" | ||||
| #include "llama-grammar.h" | ||||
|  | ||||
| #include <cstdio> | ||||
| #include <cstdlib> | ||||
| @@ -12,22 +8,21 @@ | ||||
| #include <string> | ||||
| #include <vector> | ||||
|  | ||||
| static bool llama_sample_grammar_string(struct llama_grammar * grammar, const std::string & input_str, size_t & error_pos, std::string & error_msg) { | ||||
|     auto decoded = decode_utf8(input_str, {}); | ||||
|     const auto & code_points = decoded.first; | ||||
| static bool llama_grammar_validate(struct llama_grammar * grammar, const std::string & input_str, size_t & error_pos, std::string & error_msg) { | ||||
|     const auto cpts = unicode_cpts_from_utf8(input_str); | ||||
|  | ||||
|     const llama_grammar_rules  & rules      = llama_grammar_get_rules (grammar); | ||||
|           llama_grammar_stacks & cur_stacks = llama_grammar_get_stacks(grammar); | ||||
|  | ||||
|     size_t pos = 0; | ||||
|     for (auto it = code_points.begin(), end = code_points.end() - 1; it != end; ++it) { | ||||
|     for (const auto & cpt : cpts) { | ||||
|         const llama_grammar_stacks prev_stacks = llama_grammar_get_stacks(grammar); // copy | ||||
|  | ||||
|         llama_grammar_accept(rules, prev_stacks, *it, cur_stacks); | ||||
|         cur_stacks = llama_grammar_accept(rules, prev_stacks, cpt); | ||||
|  | ||||
|         if (cur_stacks.empty()) { | ||||
|             error_pos = pos; | ||||
|             error_msg = "Unexpected character '" + unicode_cpt_to_utf8(*it) + "'"; | ||||
|             error_msg = "Unexpected character '" + unicode_cpt_to_utf8(cpt) + "'"; | ||||
|             cur_stacks = prev_stacks; | ||||
|             return false; | ||||
|         } | ||||
| @@ -85,27 +80,7 @@ int main(int argc, char** argv) { | ||||
|         grammar_str = buffer.str(); | ||||
|     } | ||||
|  | ||||
|     // Parse the GBNF grammar | ||||
|     auto parsed_grammar = grammar_parser::parse(grammar_str.c_str()); | ||||
|  | ||||
|     // will be empty (default) if there are parse errors | ||||
|     if (parsed_grammar.rules.empty()) { | ||||
|         fprintf(stdout, "%s: failed to parse grammar\n", __func__); | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
|     // Ensure that there is a "root" node. | ||||
|     if (parsed_grammar.symbol_ids.find("root") == parsed_grammar.symbol_ids.end()) { | ||||
|         fprintf(stdout, "%s: grammar does not contain a 'root' symbol\n", __func__); | ||||
|         return 1; | ||||
|     } | ||||
|  | ||||
|     std::vector<const llama_grammar_element *> grammar_rules(parsed_grammar.c_rules()); | ||||
|  | ||||
|     // Create the LLAMA grammar | ||||
|     auto grammar = llama_grammar_init( | ||||
|             grammar_rules.data(), | ||||
|             grammar_rules.size(), parsed_grammar.symbol_ids.at("root")); | ||||
|     llama_grammar * grammar = llama_grammar_init_impl(nullptr, grammar_str.c_str(), "root"); | ||||
|     if (grammar == nullptr) { | ||||
|         throw std::runtime_error("Failed to initialize llama_grammar"); | ||||
|     } | ||||
| @@ -122,7 +97,7 @@ int main(int argc, char** argv) { | ||||
|     // Validate the input string against the grammar | ||||
|     size_t error_pos; | ||||
|     std::string error_msg; | ||||
|     bool is_valid = llama_sample_grammar_string(grammar, input_str, error_pos, error_msg); | ||||
|     bool is_valid = llama_grammar_validate(grammar, input_str, error_pos, error_msg); | ||||
|  | ||||
|     if (is_valid) { | ||||
|         fprintf(stdout, "Input string is valid according to the grammar.\n"); | ||||
| @@ -131,7 +106,7 @@ int main(int argc, char** argv) { | ||||
|     } | ||||
|  | ||||
|     // Clean up | ||||
|     llama_grammar_free(grammar); | ||||
|     llama_grammar_free_impl(grammar); | ||||
|  | ||||
|     return 0; | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 Georgi Gerganov
					Georgi Gerganov