Skip to content

Commit 267f138

Browse files
committed
grammar : hide decode_utf8 overload
ggml-ci
1 parent 05ce8ce commit 267f138

File tree

6 files changed

+179
-191
lines changed

6 files changed

+179
-191
lines changed

common/sampling.cpp

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -157,25 +157,21 @@ std::vector<llama_sampler_type> llama_sampling_types_from_names(const std::vecto
157157

158158
std::vector<llama_sampler_type> sampler_types;
159159
sampler_types.reserve(names.size());
160-
for (const auto & name : names)
161-
{
160+
161+
for (const auto & name : names) {
162162
auto sampler_item = sampler_canonical_name_map.find(name);
163-
if (sampler_item != sampler_canonical_name_map.end())
164-
{
163+
if (sampler_item != sampler_canonical_name_map.end()) {
165164
sampler_types.push_back(sampler_item->second);
166-
}
167-
else
168-
{
169-
if (allow_alt_names)
170-
{
165+
} else {
166+
if (allow_alt_names) {
171167
sampler_item = sampler_alt_name_map.find(name);
172-
if (sampler_item != sampler_alt_name_map.end())
173-
{
168+
if (sampler_item != sampler_alt_name_map.end()) {
174169
sampler_types.push_back(sampler_item->second);
175170
}
176171
}
177172
}
178173
}
174+
179175
return sampler_types;
180176
}
181177

examples/gbnf-validator/gbnf-validator.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
#include "ggml.h"
2-
#include "llama.h"
3-
#include "llama-grammar.h"
41
#include "unicode.h"
2+
#include "llama-grammar.h"
53

64
#include <cstdio>
75
#include <cstdlib>
@@ -11,21 +9,20 @@
119
#include <vector>
1210

1311
static bool llama_grammar_validate(struct llama_grammar * grammar, const std::string & input_str, size_t & error_pos, std::string & error_msg) {
14-
auto decoded = decode_utf8(input_str, {});
15-
const auto & code_points = decoded.first;
12+
const auto cpts = unicode_cpts_from_utf8(input_str);
1613

1714
const llama_grammar_rules & rules = llama_grammar_get_rules (grammar);
1815
llama_grammar_stacks & cur_stacks = llama_grammar_get_stacks(grammar);
1916

2017
size_t pos = 0;
21-
for (auto it = code_points.begin(), end = code_points.end() - 1; it != end; ++it) {
18+
for (const auto & cpt : cpts) {
2219
const llama_grammar_stacks prev_stacks = llama_grammar_get_stacks(grammar); // copy
2320

24-
cur_stacks = llama_grammar_accept(rules, prev_stacks, *it);
21+
cur_stacks = llama_grammar_accept(rules, prev_stacks, cpt);
2522

2623
if (cur_stacks.empty()) {
2724
error_pos = pos;
28-
error_msg = "Unexpected character '" + unicode_cpt_to_utf8(*it) + "'";
25+
error_msg = "Unexpected character '" + unicode_cpt_to_utf8(cpt) + "'";
2926
cur_stacks = prev_stacks;
3027
return false;
3128
}

0 commit comments

Comments
 (0)