@@ -1279,6 +1279,7 @@ struct llama_vocab::impl {
12791279
12801280 std::string type_name () const ;
12811281
1282+ bool is_valid (llama_token id) const ;
12821283 bool is_normal (llama_token id) const ;
12831284 bool is_unknown (llama_token id) const ;
12841285 bool is_control (llama_token id) const ;
@@ -2068,6 +2069,11 @@ std::string llama_vocab::impl::type_name() const{
20682069 }
20692070}
20702071
2072+ bool llama_vocab::impl::is_valid (llama_token id) const {
2073+ GGML_ASSERT (type != LLAMA_VOCAB_TYPE_NONE);
2074+ return 0 <= id && id < (int32_t ) id_to_token.size ();
2075+ }
2076+
20712077bool llama_vocab::impl::is_normal (llama_token id) const {
20722078 GGML_ASSERT (type != LLAMA_VOCAB_TYPE_NONE);
20732079 return id_to_token[id].attr & LLAMA_TOKEN_ATTR_NORMAL;
@@ -2760,6 +2766,10 @@ std::string llama_vocab::type_name() const{
27602766 return pimpl->type_name ();
27612767}
27622768
2769+ bool llama_vocab::is_valid (llama_token id) const {
2770+ return pimpl->is_valid (id);
2771+ }
2772+
27632773bool llama_vocab::is_normal (llama_token id) const {
27642774 return pimpl->is_normal (id);
27652775}
@@ -3262,3 +3272,11 @@ int32_t llama_detokenize(
32623272 return vocab->detokenize (tokens, n_tokens, text, text_len_max, remove_special, unparse_special);
32633273}
32643274
3275+ bool can_detokenize (const struct llama_vocab * vocab, const llama_token * tokens, int32_t n_tokens) {
3276+ for (int32_t i = 0 ; i < n_tokens; ++i) {
3277+ if (!vocab->is_valid (tokens[i])) {
3278+ return false ;
3279+ }
3280+ }
3281+ return true ;
3282+ }
0 commit comments