6
6
#include < map>
7
7
#include < vector>
8
8
9
- static std::string unescape_whitespace (llama_context* ctx, const std::vector<llama_token>& tokens) {
9
+ static std::string llama_detokenize (llama_context * ctx, const std::vector<llama_token> & tokens) {
10
10
std::string result;
11
11
for (size_t i = 0 ; i < tokens.size (); ++i) {
12
12
result += llama_token_to_str (ctx, tokens[i]);
@@ -16,38 +16,40 @@ static std::string unescape_whitespace(llama_context* ctx, const std::vector<lla
16
16
17
17
static const std::map<std::string, std::vector<llama_token>> & k_tests () {
18
18
static std::map<std::string, std::vector<llama_token>> _k_tests = {
19
- { " " , { 1 , 259 , }, },
20
- { " " , { 1 , 1678 , }, },
21
- { " " , { 1 , 268 , }, },
22
- { " \t " , { 1 , 29871 , 12 , }, },
23
- { " \n " , { 1 , 29871 , 13 , }, },
24
- { " \t\n " , { 1 , 29871 , 12 , 13 , }, },
25
- { " Hello world" , { 1 , 15043 , 3186 , }, },
26
- { " Hello world" , { 1 , 29871 , 15043 , 3186 , }, },
27
- { " Hello World" , { 1 , 15043 , 2787 , }, },
28
- { " Hello World" , { 1 , 29871 , 15043 , 2787 , }, },
29
- { " Hello World!" , { 1 , 29871 , 15043 , 2787 , 29991 , }, },
30
- { " Hello, world!" , { 1 , 15043 , 29892 , 3186 , 29991 , }, },
31
- { " Hello, world!" , { 1 , 29871 , 15043 , 29892 , 3186 , 29991 , }, },
32
- { " this is 🦙.cpp" , { 1 , 29871 , 445 , 338 , 29871 , 243 , 162 , 169 , 156 , 29889 , 8223 , }, },
33
- { " w048 7tuijk dsdfhu" , { 1 , 281 , 29900 , 29946 , 29947 , 29871 , 29955 , 9161 , 13535 , 18031 , 2176 , 6905 , }, },
34
- { " нещо на Български" , { 1 , 1538 , 4851 , 665 , 1386 , 29713 , 1305 , }, },
35
- { " កាន់តែពិសេសអាចខលចេញ" , { 1 , 29871 , 31849 , 31324 , 31934 , 228 , 162 , 142 , 228 , 161 ,
36
- 146 , 228 , 162 , 133 , 228 , 161 , 153 , 228 , 161 , 186 ,
37
- 31708 , 228 , 162 , 132 , 31708 , 228 , 161 , 165 , 31324 , 228 ,
38
- 161 , 136 , 228 , 161 , 132 , 228 , 161 , 158 , 228 , 161 ,
39
- 136 , 228 , 162 , 132 , 228 , 161 , 140 , }, },
19
+ { " " , { }, },
20
+ { " " , { 259 , }, },
21
+ { " " , { 1678 , }, },
22
+ { " " , { 268 , }, },
23
+ { " \t " , { 29871 , 12 , }, },
24
+ { " \n " , { 29871 , 13 , }, },
25
+ { " \t\n " , { 29871 , 12 , 13 , }, },
26
+ { " Hello world" , { 15043 , 3186 , }, },
27
+ { " Hello world" , { 29871 , 15043 , 3186 , }, },
28
+ { " Hello World" , { 15043 , 2787 , }, },
29
+ { " Hello World" , { 29871 , 15043 , 2787 , }, },
30
+ { " Hello World!" , { 29871 , 15043 , 2787 , 29991 , }, },
31
+ { " Hello, world!" , { 15043 , 29892 , 3186 , 29991 , }, },
32
+ { " Hello, world!" , { 29871 , 15043 , 29892 , 3186 , 29991 , }, },
33
+ { " this is 🦙.cpp" , { 29871 , 445 , 338 , 29871 , 243 , 162 , 169 , 156 , 29889 , 8223 , }, },
34
+ { " w048 7tuijk dsdfhu" , { 281 , 29900 , 29946 , 29947 , 29871 , 29955 , 9161 , 13535 , 18031 , 2176 , 6905 , }, },
35
+ { " нещо на Български" , { 1538 , 4851 , 665 , 1386 , 29713 , 1305 , }, },
36
+ { " កាន់តែពិសេសអាចខលចេញ" ,
37
+ { 29871 , 31849 , 31324 , 31934 , 228 , 162 , 142 , 228 , 161 ,
38
+ 146 , 228 , 162 , 133 , 228 , 161 , 153 , 228 , 161 , 186 ,
39
+ 31708 , 228 , 162 , 132 , 31708 , 228 , 161 , 165 , 31324 , 228 ,
40
+ 161 , 136 , 228 , 161 , 132 , 228 , 161 , 158 , 228 , 161 ,
41
+ 136 , 228 , 162 , 132 , 228 , 161 , 140 , }, },
40
42
{ " 🚀 (normal) 😶🌫️ (multiple emojis concatenated) ✅ (only emoji that has its own token)" ,
41
- { 1 , 29871 , 243 , 162 , 157 , 131 , 313 , 8945 , 29897 , 29871 ,
42
- 243 , 162 , 155 , 185 , 30722 , 243 , 162 , 143 , 174 , 30598 ,
43
- 313 , 20787 , 953 , 3848 , 275 , 16125 , 630 , 29897 , 29871 , 31681 ,
44
- 313 , 6194 , 953 , 29877 , 2397 , 393 , 756 , 967 , 1914 , 5993 , 29897 , }, },
45
- { " Hello" , { 1 , 15043 , }, },
46
- { " Hello" , { 1 , 29871 , 15043 , }, },
47
- { " Hello" , { 1 , 259 , 15043 , }, },
48
- { " Hello" , { 1 , 1678 , 15043 , }, },
49
- { " Hello" , { 1 , 268 , 15043 , }, },
50
- { " Hello\n Hello" , { 1 , 268 , 15043 , 13 , 1678 , 15043 , }, },
43
+ { 29871 , 243 , 162 , 157 , 131 , 313 , 8945 , 29897 , 29871 ,
44
+ 243 , 162 , 155 , 185 , 30722 , 243 , 162 , 143 , 174 , 30598 ,
45
+ 313 , 20787 , 953 , 3848 , 275 , 16125 , 630 , 29897 , 29871 , 31681 ,
46
+ 313 , 6194 , 953 , 29877 , 2397 , 393 , 756 , 967 , 1914 , 5993 , 29897 , }, },
47
+ { " Hello" , { 15043 , }, },
48
+ { " Hello" , { 29871 , 15043 , }, },
49
+ { " Hello" , { 259 , 15043 , }, },
50
+ { " Hello" , { 1678 , 15043 , }, },
51
+ { " Hello" , { 268 , 15043 , }, },
52
+ { " Hello\n Hello" , { 268 , 15043 , 13 , 1678 , 15043 , }, },
51
53
};
52
54
53
55
return _k_tests;
@@ -102,30 +104,34 @@ int main(int argc, char **argv) {
102
104
bool success = true ;
103
105
104
106
for (const auto & test_kv : k_tests ()) {
105
- // Add a space in front of the first character to match OG llama tokenizer behavior
106
- std::vector<llama_token> res = llama_tokenize (ctx, " " + test_kv.first , true );
107
- fprintf (stderr, " %s : '%s' tokenized to '%s'\n " ,
108
- __func__, test_kv.first .c_str (), unescape_whitespace (ctx, res).c_str ());
107
+ const std::vector<llama_token> res_bos = llama_tokenize (ctx, test_kv.first , true );
108
+ const std::vector<llama_token> res_nobos = llama_tokenize (ctx, test_kv.first , false );
109
109
110
- bool correct = res. size () == test_kv. second . size ( );
110
+ fprintf (stderr, " %s : '%s' tokenized to '%s' \n " , __func__, test_kv. first . c_str (), llama_detokenize (ctx, res_bos). c_str () );
111
111
112
- for (int i = 0 ; i < (int ) res.size () && correct; ++i) {
113
- if (res[i] != test_kv.second [i]) {
112
+ bool correct = res_nobos.size () == test_kv.second .size () && res_bos.size () == res_nobos.size () + 1 && res_bos[0 ] == 1 ;
113
+
114
+ for (int i = 0 ; i < (int ) res_nobos.size () && correct; ++i) {
115
+ if (test_kv.second [i] != res_bos[i + 1 ]) {
116
+ correct = false ;
117
+ }
118
+ if (test_kv.second [i] != res_nobos[i]) {
114
119
correct = false ;
115
120
}
116
121
}
117
122
118
123
if (!correct) {
119
124
fprintf (stderr, " %s : failed test: '%s'\n " , __func__, test_kv.first .c_str ());
120
125
fprintf (stderr, " %s : detokenized to: '%s' instead of '%s'\n " , __func__,
121
- unescape_whitespace (ctx, res).c_str (), unescape_whitespace (ctx, test_kv.second ).c_str ());
126
+ llama_detokenize (ctx, res_nobos).c_str (),
127
+ llama_detokenize (ctx, test_kv.second ).c_str ());
122
128
fprintf (stderr, " %s : expected tokens: " , __func__);
123
129
for (const auto & t : test_kv.second ) {
124
130
fprintf (stderr, " %6d, " , t);
125
131
}
126
132
fprintf (stderr, " \n " );
127
133
fprintf (stderr, " %s : got tokens: " , __func__);
128
- for (const auto & t : res ) {
134
+ for (const auto & t : res_nobos ) {
129
135
fprintf (stderr, " %6d, " , t);
130
136
}
131
137
fprintf (stderr, " \n " );
0 commit comments