@@ -2303,6 +2303,18 @@ static uint8_t llama_byte_to_char(const llama_vocab & vocab, uint8_t byte) {
23032303 return false ;
23042304}
23052305
2306+ static uint8_t llama_char_to_byte (const llama_vocab & vocab, uint8_t ch) {
2307+ if (llama_vocab_type (vocab) == " spm" ) {
2308+ return ch + 3 ;
2309+ }
2310+
2311+ if (llama_vocab_type (vocab) == " bpe" ) {
2312+ return ch - 32 ;
2313+ }
2314+
2315+ return false ;
2316+ }
2317+
23062318static std::string llama_escape_whitespace (const std::string& text) {
23072319 std::string result;
23082320 bool escaping = false ;
@@ -2439,7 +2451,7 @@ struct llama_tokenizer {
24392451 if (p == rev_merge.end ()) {
24402452 // output any symbols that did not form tokens as bytes.
24412453 for (int j = 0 ; j < (int )symbol.n ; ++j) {
2442- llama_vocab::id token_id = llama_byte_to_char (vocab_, symbol.text [j]);
2454+ llama_vocab::id token_id = llama_char_to_byte (vocab_, symbol.text [j]);
24432455 output.push_back (token_id);
24442456 }
24452457 return ;
@@ -4871,8 +4883,8 @@ int llama_token_to_str_with_model(const struct llama_model * model, llama_token
48714883 return 0 ;
48724884}
48734885
4874- int llama_token_to_str (const struct llama_context * ctx, llama_token token, char * str , int length) {
4875- return llama_token_to_str_with_model (&ctx->model , token, str , length);
4886+ int llama_token_to_str (const struct llama_context * ctx, llama_token token, char * buf , int length) {
4887+ return llama_token_to_str_with_model (&ctx->model , token, buf , length);
48764888}
48774889
48784890std::string llama_token_to_str (const struct llama_context * ctx, llama_token token) {
@@ -4889,13 +4901,13 @@ std::string llama_token_to_str(const struct llama_context * ctx, llama_token tok
48894901 return std::string (result.data (), result.size ());
48904902}
48914903
4892- int llama_token_to_str_bpe (const struct llama_context * ctx, llama_token token, char * str , int length) {
4904+ int llama_token_to_str_bpe (const struct llama_context * ctx, llama_token token, char * buf , int length) {
48934905 if (0 <= token && token < llama_n_vocab_from_model (&ctx->model )) {
48944906 std::string result = ctx->model .vocab .id_to_token [token].tok ;
48954907 if (length < (int ) result.length ()) {
48964908 return -result.length ();
48974909 }
4898- memcpy (str , result.c_str (), result.length ());
4910+ memcpy (buf , result.c_str (), result.length ());
48994911 return result.length ();
49004912 }
49014913 return 0 ;
0 commit comments