@@ -2164,8 +2164,7 @@ struct llama_vocab {
2164
2164
std::vector<token_data> id_to_token;
2165
2165
2166
2166
std::vector<id> cache_special_tokens;
2167
- std::vector<token> cache_token_to_piece; // llama_token_to_piece(special = false);
2168
- std::vector<token> cache_token_to_piece_special; // llama_token_to_piece(special = true);
2167
+ std::vector<token> cache_token_to_piece; // llama_token_to_piece(special = true);
2169
2168
2170
2169
std::map<std::pair<std::string, std::string>, int> bpe_ranks;
2171
2170
@@ -4845,23 +4844,19 @@ static void llm_load_vocab(
4845
4844
LLAMA_LOG_INFO("%s: special tokens cache size = %u\n", __func__, (uint32_t)vocab.cache_special_tokens.size());
4846
4845
}
4847
4846
4848
- // build token to piece caches
4847
+ // build token to piece cache
4849
4848
{
4850
4849
size_t size_cache = 0;
4851
4850
4852
- std::vector<llama_vocab::token> cache_token_to_piece (n_vocab);
4853
- std::vector<llama_vocab::token> cache_token_to_piece_special(n_vocab);
4851
+ std::vector<llama_vocab::token> cache_token_to_piece(n_vocab);
4854
4852
4855
4853
for (uint32_t id = 0; id < n_vocab; ++id) {
4856
- cache_token_to_piece[id] = llama_token_to_piece(&model, id, false);
4857
- cache_token_to_piece_special[id] = llama_token_to_piece(&model, id, true);
4854
+ cache_token_to_piece[id] = llama_token_to_piece(&model, id, true);
4858
4855
4859
4856
size_cache += cache_token_to_piece[id].size();
4860
- size_cache += cache_token_to_piece_special[id].size();
4861
4857
}
4862
4858
4863
- std::swap(vocab.cache_token_to_piece, cache_token_to_piece);
4864
- std::swap(vocab.cache_token_to_piece_special, cache_token_to_piece_special);
4859
+ std::swap(vocab.cache_token_to_piece, cache_token_to_piece);
4865
4860
4866
4861
LLAMA_LOG_INFO("%s: token to piece cache size = %.4f MB\n", __func__, size_cache / 1024.0 / 1024.0);
4867
4862
}
@@ -18318,9 +18313,14 @@ static std::string llama_decode_text(const std::string & text) {
18318
18313
18319
18314
// does not write null-terminator to buf
18320
18315
int32_t llama_token_to_piece(const struct llama_model * model, llama_token token, char * buf, int32_t length, bool special) {
18316
+ // ref: https://github.com/ggerganov/llama.cpp/pull/7587#discussion_r1620983843
18317
+ if (!special && llama_is_control_token(model->vocab, token)) {
18318
+ return 0;
18319
+ }
18320
+
18321
18321
// if we have a cache - use it
18322
18322
{
18323
- const auto & cache = special ? model->vocab.cache_token_to_piece_special : model->vocab.cache_token_to_piece;
18323
+ const auto & cache = model->vocab.cache_token_to_piece;
18324
18324
18325
18325
if (!cache.empty()) {
18326
18326
const auto & res = cache.at(token);
0 commit comments