Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

grammars: cache decoded token codepoints for faster sampling #6811

Closed
wants to merge 19 commits into from
Closed
Changes from all commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
98f33ba
grammars: early exit when no next_candidates to reject
ochafik Apr 21, 2024
f608415
grammars: cache decoded tokens
ochafik Apr 21, 2024
cbc7580
grammars: faster llama_grammar_copy
ochafik Apr 21, 2024
1fb300c
Merge remote-tracking branch 'origin/master' into grammar-fast
ochafik Apr 21, 2024
24769f9
grammars: fix bad merge
ochafik Apr 21, 2024
05efa34
grammars: keep llama_grammar_copy non-quadratic optim for later
ochafik Apr 24, 2024
9f13623
Merge remote-tracking branch 'origin/master' into grammar-fast
ochafik Apr 27, 2024
d41f314
grammars: move token caches to llama_context
ochafik Apr 28, 2024
49f0faa
grammars: cache codepoints in llama_new_context_with_model
ochafik Apr 28, 2024
d3425f5
grammar: nit (layout)
ochafik Apr 28, 2024
c70037f
grammars: nits (revert const grammar sig, fix comment)
ochafik Apr 28, 2024
0148661
Merge remote-tracking branch 'origin/master' into grammar-fast
ochafik Apr 30, 2024
80736c5
Update llama.cpp
ochafik May 1, 2024
939e143
grammars: mutex-guarded lazy caching of token pieces in llama_sample_…
ochafik May 18, 2024
60745ac
grammars: remove early exit --> https://github.com/ggerganov/llama.cp…
ochafik May 18, 2024
40655e8
Merge remote-tracking branch 'origin/master' into grammar-fast
ochafik May 18, 2024
2477d84
Merge remote-tracking branch 'origin/master' into grammar-fast
ochafik May 21, 2024
c208932
Merge remote-tracking branch 'origin/master' into grammar-fast
ochafik Jun 4, 2024
6a9b626
Merge remote-tracking branch 'origin/master' into grammar-fast
ochafik Jun 10, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 28 additions & 3 deletions llama.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2368,6 +2368,11 @@ struct llama_context {

// control vectors
struct llama_control_vector cvec;

// caching token pieces & their decoded codepoints.
std::mutex token_cache_mutex;
std::vector<std::pair<std::vector<uint32_t>, llama_partial_utf8>>
token_codepoints_without_partial_utf8_prefix;
};

static size_t llama_get_device_count(const llama_model & model) {
Expand Down Expand Up @@ -14496,9 +14501,24 @@ void llama_sample_grammar(struct llama_context * ctx, llama_token_data_array * c
}
}

std::vector<std::pair<std::vector<uint32_t>, llama_partial_utf8>> candidates_decoded;
candidates_decoded.reserve(candidates->size);
{
// cache tokens & their decoded codepoints (for common case where there's no partial utf8 prefix bytes) for grammar-constrained sampling.
std::unique_lock<std::mutex> lock(ctx->token_cache_mutex);
if (ctx->token_codepoints_without_partial_utf8_prefix.empty()) {
auto n_vocab = llama_n_vocab(llama_get_model(ctx));
ctx->token_codepoints_without_partial_utf8_prefix.resize(n_vocab);
for (llama_token id = 0; id < n_vocab; ++id) {
const std::string & piece = ctx->model.vocab.cache_token_to_piece.at(id);
ctx->token_codepoints_without_partial_utf8_prefix[id] = decode_utf8(piece, {0, 0});
}
}
}

// Store decoded codepoints when they are not cached (happens when there's a partial utf8 string prefix).
std::vector<std::pair<std::vector<uint32_t>, llama_partial_utf8>> candidates_decoded;
if (grammar->partial_utf8.n_remain > 0) {
candidates_decoded.reserve(candidates->size);
HanClinto marked this conversation as resolved.
Show resolved Hide resolved
}
std::vector<llama_grammar_candidate> candidates_grammar;
candidates_grammar.reserve(candidates->size);

Expand All @@ -14512,6 +14532,9 @@ void llama_sample_grammar(struct llama_context * ctx, llama_token_data_array * c
}
} else if (piece.empty() || piece[0] == 0) {
candidates->data[i].logit = -INFINITY;
} else if (grammar->partial_utf8.n_remain == 0){
const auto & decoded = ctx->token_codepoints_without_partial_utf8_prefix.at(id);
candidates_grammar.push_back({ i, decoded.first.data(), decoded.second });
} else {
candidates_decoded.push_back(decode_utf8(piece, grammar->partial_utf8));
candidates_grammar.push_back({ i, candidates_decoded.back().first.data(), candidates_decoded.back().second });
Expand Down Expand Up @@ -14707,7 +14730,9 @@ void llama_grammar_accept_token(struct llama_context * ctx, struct llama_grammar
const std::string & piece = ctx->model.vocab.cache_token_to_piece.at(token);

// Note terminating 0 in decoded string
const auto decoded = decode_utf8(piece, grammar->partial_utf8);
const auto decoded = grammar->partial_utf8.n_remain == 0
? ctx->token_codepoints_without_partial_utf8_prefix[token]
: decode_utf8(piece, grammar->partial_utf8);
const auto & code_points = decoded.first;
std::vector<std::vector<const llama_grammar_element *>> tmp_new_stacks;
for (auto it = code_points.begin(), end = code_points.end() - 1; it != end; ++it) {
Expand Down
Loading