@@ -2303,6 +2303,18 @@ static uint8_t llama_byte_to_char(const llama_vocab & vocab, uint8_t byte) {
2303
2303
return false ;
2304
2304
}
2305
2305
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
+
2306
2318
static std::string llama_escape_whitespace (const std::string& text) {
2307
2319
std::string result;
2308
2320
bool escaping = false ;
@@ -2439,7 +2451,7 @@ struct llama_tokenizer {
2439
2451
if (p == rev_merge.end ()) {
2440
2452
// output any symbols that did not form tokens as bytes.
2441
2453
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]);
2443
2455
output.push_back (token_id);
2444
2456
}
2445
2457
return ;
@@ -4871,8 +4883,8 @@ int llama_token_to_str_with_model(const struct llama_model * model, llama_token
4871
4883
return 0 ;
4872
4884
}
4873
4885
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);
4876
4888
}
4877
4889
4878
4890
std::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
4889
4901
return std::string (result.data (), result.size ());
4890
4902
}
4891
4903
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) {
4893
4905
if (0 <= token && token < llama_n_vocab_from_model (&ctx->model )) {
4894
4906
std::string result = ctx->model .vocab .id_to_token [token].tok ;
4895
4907
if (length < (int ) result.length ()) {
4896
4908
return -result.length ();
4897
4909
}
4898
- memcpy (str , result.c_str (), result.length ());
4910
+ memcpy (buf , result.c_str (), result.length ());
4899
4911
return result.length ();
4900
4912
}
4901
4913
return 0 ;
0 commit comments