Skip to content

Commit 733207f

Browse files
committed
tests : fix wstring_convert
1 parent 3e123a3 commit 733207f

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

llama.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ static void llama_log_callback_default(llama_log_level level, const char * text,
6363
#define LLAMA_LOG_WARN(...) llama_log_internal(LLAMA_LOG_LEVEL_WARN , __VA_ARGS__)
6464
#define LLAMA_LOG_ERROR(...) llama_log_internal(LLAMA_LOG_LEVEL_ERROR, __VA_ARGS__)
6565

66+
6667
#if !defined(GGML_USE_CUBLAS) && !defined(GGML_USE_METAL)
6768
#include "ggml-alloc.h"
6869
#define LLAMA_USE_ALLOCATOR
@@ -1988,7 +1989,7 @@ static bool llama_is_eos_token(const llama_vocab& vocab, llama_token token) {
19881989
return false;
19891990
}
19901991

1991-
static bool llama_is_user_defined_token(const llama_vocab & vocab, llama_token token) {
1992+
static bool llama_is_user_defined_token(const llama_vocab& vocab, llama_token token) {
19921993
UNUSED(vocab);
19931994
UNUSED(token);
19941995
// TODO: improve?
@@ -4400,24 +4401,24 @@ int llama_token_to_str_with_model(const struct llama_model * model, llama_token
44004401
if (0 <= token && token < llama_n_vocab_from_model(model)) {
44014402
if (llama_is_normal_token(model->vocab, token)) {
44024403
std::string result = model->vocab.id_to_token[token].tok;
4403-
if (llama_vocab_type(model->vocab) == "spm") {
4404+
if(llama_vocab_type(model->vocab) == "spm") {
44044405
result = llama_unescape_whitespace(result);
44054406
}
44064407
if (length < (int) result.length()) {
44074408
return -result.length();
44084409
}
4409-
strcpy(str, result.c_str());
4410+
strncpy(str, result.c_str(), result.length());
44104411
return result.length();
44114412
} else if (llama_is_unknown_token(model->vocab, token)) {
44124413
if (length < 3) {
44134414
return -3;
44144415
}
4415-
strcpy(str, "\xe2\x96\x85");
4416+
strncpy(str, "\xe2\x96\x85", 3);
44164417
return 3;
44174418
} else if (llama_is_control_token(model->vocab, token)) {
44184419
;
44194420
} else if (llama_is_byte_token(model->vocab, token)) {
4420-
if(1 > length) {
4421+
if (length < 1) {
44214422
return -1;
44224423
}
44234424
str[0] = llama_byte_to_char(model->vocab, token);
@@ -4452,7 +4453,7 @@ int llama_token_to_str_bpe(const struct llama_context * ctx, llama_token token,
44524453
if (length < (int) result.length()) {
44534454
return -result.length();
44544455
}
4455-
strcpy(str, result.c_str());
4456+
strncpy(str, result.c_str(), result.length());
44564457
return result.length();
44574458
}
44584459
return 0;
@@ -4463,9 +4464,8 @@ std::string llama_token_to_str_bpe(const struct llama_context * ctx, llama_token
44634464
const int length = llama_token_to_str_bpe(ctx, token, result.data(), result.size());
44644465
if (length < 0) {
44654466
result.resize(-length);
4466-
const int check = llama_token_to_str_bpe(ctx, token, (char*)result.data(), result.size());
4467+
const int check = llama_token_to_str_bpe(ctx, token, result.data(), result.size());
44674468
GGML_ASSERT(check == -length);
4468-
GGML_UNUSED(check);
44694469
} else {
44704470
result.resize(length);
44714471
}

tests/test-tokenizer-1.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,12 @@ int main(int argc, char **argv) {
106106
std::wstring_convert<typename std::codecvt_utf8<wchar_t>, wchar_t> converter;
107107
for (wchar_t ch = 0x0000; ch < 0xffff; ++ch) {
108108
std::wstring wstr(1, ch);
109-
std::string str = converter.to_bytes(wstr);
109+
std::string str;
110+
try {
111+
str = converter.to_bytes(wstr);
112+
} catch (std::exception & e) {
113+
continue;
114+
}
110115
std::vector<llama_token> tokens = llama_tokenize(ctx, escape_whitespace(str), false);
111116
if (tokens.size() == 1) {
112117
fprintf(stderr, "%s : info: %s tokenized to %d \n",

0 commit comments

Comments
 (0)