Skip to content

Commit 2e5c8ae

Browse files
committed
reserve space for codepoints
1 parent 207b519 commit 2e5c8ae

File tree

1 file changed

+12
-2
lines changed

1 file changed

+12
-2
lines changed

llama.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6903,10 +6903,13 @@ struct llama_grammar_candidate {
69036903
// pointer. If an invalid sequence is encountered, returns `llama_partial_utf8.n_remain == -1`.
69046904
static std::pair<std::vector<uint32_t>, llama_partial_utf8> decode_utf8(
69056905
const char * src,
6906+
size_t n_src,
69066907
llama_partial_utf8 partial_start) {
69076908
static const int lookup[] = { 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 2, 3, 4 };
69086909
const char * pos = src;
69096910
std::vector<uint32_t> code_points;
6911+
// common english strings have the same number of codepoints and bytes.
6912+
code_points.reserve(n_src);
69106913
uint32_t value = partial_start.value;
69116914
int n_remain = partial_start.n_remain;
69126915

@@ -6957,6 +6960,13 @@ static std::pair<std::vector<uint32_t>, llama_partial_utf8> decode_utf8(
69576960
return std::make_pair(std::move(code_points), llama_partial_utf8{ value, n_remain });
69586961
}
69596962

6963+
static std::pair<std::vector<uint32_t>, llama_partial_utf8> decode_utf8(
6964+
std::string src,
6965+
llama_partial_utf8 partial_start
6966+
) {
6967+
return decode_utf8(src.c_str(), src.size(), partial_start);
6968+
}
6969+
69606970
// returns true iff pos points to the end of one of the definitions of a rule
69616971
static bool llama_grammar_is_end_of_sequence(const llama_grammar_element * pos) {
69626972
switch (pos->type) {
@@ -7580,7 +7590,7 @@ void llama_sample_grammar(struct llama_context * ctx, llama_token_data_array * c
75807590
} else if (piece.empty() || piece[0] == 0) {
75817591
candidates->data[i].logit = -INFINITY;
75827592
} else {
7583-
candidates_decoded.push_back(decode_utf8(piece.c_str(), grammar->partial_utf8));
7593+
candidates_decoded.push_back(decode_utf8(piece, grammar->partial_utf8));
75847594
candidates_grammar.push_back({ i, candidates_decoded.back().first.data(), candidates_decoded.back().second });
75857595
}
75867596
}
@@ -7787,7 +7797,7 @@ void llama_grammar_accept_token(struct llama_context * ctx, struct llama_grammar
77877797
const std::string piece = llama_token_to_piece(ctx, token);
77887798

77897799
// Note terminating 0 in decoded string
7790-
const auto decoded = decode_utf8(piece.c_str(), grammar->partial_utf8);
7800+
const auto decoded = decode_utf8(piece, grammar->partial_utf8);
77917801
const auto & code_points = decoded.first;
77927802
for (auto it = code_points.begin(), end = code_points.end() - 1; it != end; ++it) {
77937803
grammar->stacks = llama_grammar_accept(grammar->rules, grammar->stacks, *it);

0 commit comments

Comments
 (0)