@@ -208,7 +208,7 @@ struct llm_tokenizer_spm_session {
208
208
return ;
209
209
}
210
210
211
- if (static_cast <uint32_t >(token) >= vocab.n_vocab ) {
211
+ if (static_cast <uint32_t >(token) >= vocab.n_vocab () ) {
212
212
return ;
213
213
}
214
214
@@ -734,7 +734,7 @@ struct llm_tokenizer_ugm : llm_tokenizer {
734
734
prefix_replacements_size = precompiled_charsmap.size () - charsmap_offset;
735
735
}
736
736
737
- for (uint32_t id = 0 ; id < vocab.n_vocab ; ++id) {
737
+ for (uint32_t id = 0 ; id < vocab.n_vocab () ; ++id) {
738
738
const auto & token_data = vocab.get_token_data (id);
739
739
740
740
if (vocab.is_normal (id)) {
@@ -1119,7 +1119,7 @@ struct llm_tokenizer_rwkv : llm_tokenizer {
1119
1119
// For now, we decode the vocab here into the lookup we'll use for tokenization.
1120
1120
1121
1121
// build trie
1122
- for (uint32_t id = 0 ; id < vocab.n_vocab ; ++id) {
1122
+ for (uint32_t id = 0 ; id < vocab.n_vocab () ; ++id) {
1123
1123
const auto & data = vocab.get_token_data (id);
1124
1124
const auto text = llama_unescape_rwkv_token (data.text );
1125
1125
token_matcher.insert ((const char *) text.data (), text.size (), id);
@@ -1204,6 +1204,8 @@ struct fragment_buffer_variant {
1204
1204
};
1205
1205
1206
1206
struct llama_vocab ::impl {
1207
+ uint32_t n_vocab = 0 ;
1208
+
1207
1209
std::unordered_map<std::string, llama_token> token_to_id;
1208
1210
std::vector<token_data> id_to_token;
1209
1211
@@ -1283,6 +1285,13 @@ llama_vocab::~llama_vocab() {
1283
1285
void llama_vocab::load (llama_model_loader & ml, const LLM_KV & kv) {
1284
1286
struct gguf_context * ctx = ml.meta .get ();
1285
1287
1288
+ auto & n_vocab = pimpl->n_vocab ;
1289
+ auto & id_to_token = pimpl->id_to_token ;
1290
+ auto & token_to_id = pimpl->token_to_id ;
1291
+ auto & special_eog_ids = pimpl->special_eog_ids ;
1292
+ auto & cache_special_tokens = pimpl->cache_special_tokens ;
1293
+ auto & cache_token_to_piece = pimpl->cache_token_to_piece ;
1294
+
1286
1295
// determine vocab type
1287
1296
{
1288
1297
std::string tokenizer_model;
@@ -1589,12 +1598,6 @@ void llama_vocab::load(llama_model_loader & ml, const LLM_KV & kv) {
1589
1598
toktypes = (const int * ) gguf_get_arr_data (ctx, toktype_idx);
1590
1599
}
1591
1600
1592
- auto & id_to_token = pimpl->id_to_token ;
1593
- auto & token_to_id = pimpl->token_to_id ;
1594
- auto & special_eog_ids = pimpl->special_eog_ids ;
1595
- auto & cache_special_tokens = pimpl->cache_special_tokens ;
1596
- auto & cache_token_to_piece = pimpl->cache_token_to_piece ;
1597
-
1598
1601
n_vocab = gguf_get_arr_n (ctx, token_idx);
1599
1602
id_to_token.resize (n_vocab);
1600
1603
@@ -1908,7 +1911,7 @@ void llama_vocab::load(llama_model_loader & ml, const LLM_KV & kv) {
1908
1911
1909
1912
// build special tokens cache
1910
1913
{
1911
- for (llama_token id = 0 ; id < (llama_token)n_vocab; ++id) {
1914
+ for (llama_token id = 0 ; id < (llama_token) n_vocab; ++id) {
1912
1915
if (id_to_token[id].attr & (LLAMA_TOKEN_ATTR_CONTROL | LLAMA_TOKEN_ATTR_USER_DEFINED | LLAMA_TOKEN_ATTR_UNKNOWN)) {
1913
1916
cache_special_tokens.push_back (id);
1914
1917
}
@@ -2002,6 +2005,10 @@ enum llama_vocab_pre_type llama_vocab::get_pre_type() const {
2002
2005
return pre_type;
2003
2006
}
2004
2007
2008
+ uint32_t llama_vocab::n_vocab () const {
2009
+ return (uint32_t ) pimpl->id_to_token .size ();
2010
+ }
2011
+
2005
2012
std::string llama_vocab::type_name () const {
2006
2013
switch (type) {
2007
2014
case LLAMA_VOCAB_TYPE_NONE: return " no vocab" ;
@@ -2366,8 +2373,8 @@ int llama_vocab::max_token_text_len() const {
2366
2373
2367
2374
void llama_vocab::print_info () const {
2368
2375
LLAMA_LOG_INFO (" %s: vocab type = %s\n " , __func__, type_name ().c_str ());
2369
- LLAMA_LOG_INFO (" %s: n_vocab = %u\n " , __func__, n_vocab);
2370
- LLAMA_LOG_INFO (" %s: n_merges = %u\n " , __func__, (int ) pimpl->bpe_ranks .size ());
2376
+ LLAMA_LOG_INFO (" %s: n_vocab = %u\n " , __func__, pimpl-> n_vocab );
2377
+ LLAMA_LOG_INFO (" %s: n_merges = %u\n " , __func__, (uint32_t ) pimpl->bpe_ranks .size ());
2371
2378
2372
2379
auto & id_to_token = pimpl->id_to_token ;
2373
2380
auto & special_eog_ids = pimpl->special_eog_ids ;
0 commit comments