Skip to content

Commit 8d177ed

Browse files
authored
llama : improve token type support (#2668)
* Merge tokenizer fixes into the gguf branch. * Add test vocabularies * Adapt convert-new.py (and fix a clang-cl compiler error on windows) * Improved tokenizer test But does it work on MacOS? * Improve token type support - Added @klosax code to convert.py - Improved token type support in vocabulary * Exclude platform dependent tests * More sentencepiece compatibility by eliminating magic numbers * Restored accidentally removed comment
1 parent e06cbce commit 8d177ed

File tree

4 files changed

+94
-98
lines changed

4 files changed

+94
-98
lines changed

convert.py

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -261,12 +261,12 @@ def bpe_tokens(self) -> Iterable[Tuple[bytes, float]]:
261261
for i, item in enumerate(tokenizer):
262262
text: bytes = item.encode("utf-8")
263263
score: float = -i
264-
yield text, score
264+
yield text, score, 4
265265

266266
def added_tokens(self) -> Iterable[Tuple[bytes, float]]:
267267
for text in self.added_tokens_list:
268268
score = -1000.0
269-
yield text.encode("utf-8"), score
269+
yield text.encode("utf-8"), score, 4
270270

271271
def all_tokens(self) -> Iterable[Tuple[bytes, float]]:
272272
yield from self.bpe_tokens()
@@ -303,12 +303,28 @@ def sentencepiece_tokens(self) -> Iterable[Tuple[bytes, float]]:
303303
piece = tokenizer.id_to_piece(i)
304304
text: bytes = piece.encode("utf-8")
305305
score: float = tokenizer.get_score(i)
306-
yield text, score
306+
307+
toktype = 1 # defualt to normal token type
308+
if tokenizer.is_unknown(i):
309+
toktype = 2
310+
if tokenizer.is_control(i):
311+
toktype = 3
312+
313+
# NOTE: I think added_tokens are user defined.
314+
# ref: https://github.com/google/sentencepiece/blob/master/src/sentencepiece_model.proto
315+
# if tokenizer.is_user_defined(i): toktype = 4
316+
317+
if tokenizer.is_unused(i):
318+
toktype = 5
319+
if tokenizer.is_byte(i):
320+
toktype = 6
321+
322+
yield text, score, toktype
307323

308324
def added_tokens(self) -> Iterable[Tuple[bytes, float]]:
309325
for text in self.added_tokens_list:
310326
score = -1000.0
311-
yield text.encode("utf-8"), score
327+
yield text.encode("utf-8"), score, 4
312328

313329
def all_tokens(self) -> Iterable[Tuple[bytes, float]]:
314330
yield from self.sentencepiece_tokens()
@@ -721,14 +737,16 @@ def add_meta_arch(self, params: Params) -> None:
721737
def add_meta_vocab(self, vocab: Vocab) -> None:
722738
tokens = []
723739
scores = []
724-
for text, score in vocab.all_tokens():
740+
toktypes = []
741+
for text, score, toktype in vocab.all_tokens():
725742
tokens.append(text)
726743
scores.append(score)
744+
toktypes.append(toktype)
727745

728746
self.gguf.add_tokenizer_model("llama")
729747
self.gguf.add_token_list(tokens)
730748
self.gguf.add_token_scores(scores)
731-
#self.gguf.add_token_types(toktypes) # TODO: add this
749+
self.gguf.add_token_types(toktypes)
732750

733751
# TODO: added / special tokens
734752

llama.cpp

Lines changed: 51 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -772,15 +772,16 @@ struct llama_vocab {
772772
using id = int32_t;
773773
using token = std::string;
774774

775-
struct token_score {
775+
struct token_data {
776776
token tok;
777777
float score;
778+
int toktype;
778779
};
779780

780781
llama_vocab_type type = LLAMA_VOCAB_TYPE_SPM;
781782

782783
std::unordered_map<token, id> token_to_id;
783-
std::vector<token_score> id_to_token;
784+
std::vector<token_data> id_to_token;
784785

785786
// default LLaMA special tokens
786787
id special_bos_id = 1;
@@ -1507,17 +1508,25 @@ static void llama_model_load_internal(
15071508

15081509
const float * scores = (const float * ) gguf_get_arr_data(ctx, score_idx);
15091510

1511+
const int toktype_idx = gguf_find_key(ctx, "tokenizer.ggml.token_type");
1512+
if (toktype_idx == -1) {
1513+
throw std::runtime_error("cannot find token type list in GGUF file\n");
1514+
}
1515+
1516+
const int * toktypes = (const int * ) gguf_get_arr_data(ctx, toktype_idx);
1517+
15101518
for (uint32_t i = 0; i < hparams.n_vocab; i++) {
15111519
std::string word = gguf_get_arr_str(ctx, token_idx, i);
15121520

15131521
vocab.token_to_id[word] = i;
15141522

1515-
auto & tok_score = vocab.id_to_token[i];
1516-
tok_score.tok = std::move(word);
1517-
tok_score.score = scores[i];
1523+
auto & token_data = vocab.id_to_token[i];
1524+
token_data.tok = std::move(word);
1525+
token_data.score = scores[i];
1526+
token_data.toktype = toktypes[i];
15181527

15191528
// determine the newline token: 0x0A == 10 == '\n'
1520-
if (tok_score.tok == "<0x0A>") {
1529+
if (token_data.tok == "<0x0A>") {
15211530
vocab.linefeed_id = i;
15221531
}
15231532
}
@@ -2345,92 +2354,57 @@ static enum llama_vocab_type llama_vocab_get_type(const llama_vocab & vocab) {
23452354
return vocab.type;
23462355
}
23472356

2348-
static bool llama_is_normal_token(const llama_vocab & vocab, llama_token token) {
2349-
if (llama_vocab_get_type(vocab) == LLAMA_VOCAB_TYPE_SPM) {
2350-
return token >= 259;
2351-
}
2352-
2353-
if (llama_vocab_get_type(vocab) == LLAMA_VOCAB_TYPE_BPE) {
2354-
return token >= 95;
2355-
}
2356-
2357-
return false;
2357+
static bool llama_is_normal_token(const llama_vocab & vocab, llama_token id) {
2358+
return vocab.id_to_token[id].toktype == 1;
23582359
}
23592360

2360-
static bool llama_is_bos_token(const llama_vocab & vocab, llama_token token) {
2361-
return token == vocab.special_bos_id;
2361+
static bool llama_is_unknown_token(const llama_vocab & vocab, llama_token id) {
2362+
return vocab.id_to_token[id].toktype == 2;
23622363
}
23632364

2364-
static bool llama_is_eos_token(const llama_vocab & vocab, llama_token token) {
2365-
return token == vocab.special_eos_id;
2365+
static bool llama_is_control_token(const llama_vocab & vocab, llama_token id) {
2366+
return vocab.id_to_token[id].toktype == 3;
23662367
}
23672368

2368-
static bool llama_is_control_token(const llama_vocab & vocab, llama_token token) {
2369-
if (llama_vocab_get_type(vocab) == LLAMA_VOCAB_TYPE_SPM) {
2370-
return token == llama_is_bos_token(vocab, token) || token == llama_is_eos_token(vocab, token);
2371-
}
2372-
2373-
// TODO: improve?
2374-
return false;
2369+
static bool llama_is_bos_token(const llama_vocab & vocab, llama_token id) {
2370+
GGML_ASSERT(llama_is_control_token(vocab, id));
2371+
return id == vocab.special_bos_id;
23752372
}
23762373

2377-
static bool llama_is_unknown_token(const llama_vocab & vocab, llama_token token) {
2378-
if (llama_vocab_get_type(vocab) == LLAMA_VOCAB_TYPE_SPM) {
2379-
return token == 0;
2380-
}
2381-
2382-
// TODO: improve?
2383-
return false;
2374+
static bool llama_is_eos_token(const llama_vocab & vocab, llama_token id ) {
2375+
GGML_ASSERT(llama_is_control_token(vocab, id));
2376+
return id == vocab.special_eos_id;
23842377
}
23852378

2386-
static bool llama_is_user_defined_token(const llama_vocab & vocab, llama_token token) {
2387-
GGML_UNUSED(vocab);
2388-
GGML_UNUSED(token);
2389-
// TODO: improve?
2390-
return false;
2379+
static bool llama_is_pad_token(const llama_vocab & vocab, llama_token id ) {
2380+
GGML_ASSERT(id < 0 || llama_is_control_token(vocab, id));
2381+
return id == vocab.special_pad_id;
23912382
}
23922383

2393-
static bool llama_is_unused_token(const llama_vocab & vocab, llama_token token) {
2394-
GGML_UNUSED(vocab);
2395-
GGML_UNUSED(token);
2396-
// TODO: improve?
2397-
return false;
2384+
static bool llama_is_user_defined_token(const llama_vocab & vocab, llama_token id) {
2385+
return vocab.id_to_token[id].toktype == 4;
23982386
}
23992387

2400-
static bool llama_is_byte_token(const llama_vocab & vocab, llama_token token) {
2401-
if (llama_vocab_get_type(vocab) == LLAMA_VOCAB_TYPE_SPM) {
2402-
return 3 <= token && token < 259;
2403-
}
2404-
2405-
if (llama_vocab_get_type(vocab) == LLAMA_VOCAB_TYPE_BPE) {
2406-
return 1 <= token && token < 95;
2407-
}
2408-
2409-
return false;
2388+
static bool llama_is_unused_token(const llama_vocab & vocab, llama_token id) {
2389+
return vocab.id_to_token[id].toktype == 5;
24102390
}
24112391

2412-
static uint8_t llama_byte_to_char(const llama_vocab & vocab, uint8_t byte) {
2413-
if (llama_vocab_get_type(vocab) == LLAMA_VOCAB_TYPE_SPM) {
2414-
return byte - 3;
2415-
}
2416-
2417-
if (llama_vocab_get_type(vocab) == LLAMA_VOCAB_TYPE_BPE) {
2418-
return byte + 32;
2419-
}
2420-
2421-
return false;
2392+
static bool llama_is_byte_token(const llama_vocab & vocab, llama_token id) {
2393+
return vocab.id_to_token[id].toktype == 6;
24222394
}
24232395

2424-
static uint8_t llama_char_to_byte(const llama_vocab & vocab, uint8_t ch) {
2425-
if (llama_vocab_get_type(vocab) == LLAMA_VOCAB_TYPE_SPM) {
2426-
return ch + 3;
2427-
}
2428-
2429-
if (llama_vocab_get_type(vocab) == LLAMA_VOCAB_TYPE_BPE) {
2430-
return ch - 32;
2431-
}
2396+
static uint8_t llama_token_to_byte(const llama_vocab & vocab, llama_token id) {
2397+
GGML_ASSERT(llama_is_byte_token(vocab, id));
2398+
const auto& token_data = vocab.id_to_token.at(id);
2399+
auto buf = token_data.tok.substr(3, 2);
2400+
return strtol(buf.c_str(), NULL, 16);
2401+
}
24322402

2433-
return false;
2403+
static llama_token llama_byte_to_token(const llama_vocab & vocab, uint8_t ch) {
2404+
char buf[7];
2405+
int result = snprintf(buf, sizeof(buf), "<0x%02X>", ch);
2406+
GGML_ASSERT(0 <= result && result < 7);
2407+
return vocab.token_to_id.at(buf);
24342408
}
24352409

24362410
static std::string llama_escape_whitespace(const std::string& text) {
@@ -2569,7 +2543,7 @@ struct llama_tokenizer {
25692543
if (p == rev_merge.end()) {
25702544
// output any symbols that did not form tokens as bytes.
25712545
for (int j = 0; j < (int)symbol.n; ++j) {
2572-
llama_vocab::id token_id = llama_char_to_byte(vocab_, symbol.text[j]);
2546+
llama_vocab::id token_id = llama_byte_to_token(vocab_, symbol.text[j]);
25732547
output.push_back(token_id);
25742548
}
25752549
return;
@@ -2595,12 +2569,12 @@ struct llama_tokenizer {
25952569
return;
25962570
}
25972571

2598-
const auto &tok_score = vocab_.id_to_token[(*token).second];
2572+
const auto &tok_data = vocab_.id_to_token[(*token).second];
25992573

26002574
llama_sp_bigram bigram;
26012575
bigram.left = left;
26022576
bigram.right = right;
2603-
bigram.score = tok_score.score;
2577+
bigram.score = tok_data.score;
26042578
bigram.size = text.size();
26052579
work_queue_.push(bigram);
26062580

@@ -5109,7 +5083,7 @@ int llama_token_to_str_with_model(const struct llama_model * model, llama_token
51095083
if (length < 1) {
51105084
return -1;
51115085
}
5112-
buf[0] = llama_byte_to_char(model->vocab, token);
5086+
buf[0] = llama_token_to_byte(model->vocab, token);
51135087
return 1;
51145088
}
51155089
}

models/ggml-vocab-llama.gguf

125 KB
Binary file not shown.

tests/test-tokenizer-1.cpp

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,6 @@
1010
#include <vector>
1111
#include <locale>
1212

13-
static std::string vocab_type(llama_context * ctx) {
14-
return llama_n_vocab(ctx) == 32000 ? "spm": "bpe";
15-
}
16-
1713
static std::string escape_whitespace(const std::string& text) {
1814
std::string result;
1915
bool escaping = false;
@@ -91,8 +87,8 @@ int main(int argc, char **argv) {
9187
return 2;
9288
}
9389
} else {
94-
if ((vocab_type(ctx) == "spm" && i <= 258) ||
95-
(vocab_type(ctx) == "bpe" && (i == 0 || i >= 100000))) {
90+
// TODO: needs access to token types
91+
if (0 <= i && i < 259) {
9692
fprintf(stderr, "%s : info: token %d is string %s and bpe returns tokens %s\n",
9793
__func__, i, llama_token_to_str(ctx, i).c_str(), unescape_whitespace(ctx, tokens).c_str());
9894
} else {
@@ -103,20 +99,28 @@ int main(int argc, char **argv) {
10399
}
104100
}
105101

106-
std::wstring_convert<typename std::codecvt_utf8<wchar_t>, wchar_t> converter;
107-
for (wchar_t ch = 0x0000; ch < 0xffff; ++ch) {
108-
std::wstring wstr(1, ch);
109-
std::string str;
110-
try {
111-
str = converter.to_bytes(wstr);
112-
} catch (std::exception & e) {
113-
continue;
102+
#ifdef _WIN32
103+
std::wstring_convert<typename std::codecvt_utf8<char16_t>, char16_t> u16converter;
104+
for (char16_t ch = 0x0000; ch < 0xffff; ++ch) {
105+
std::u16string u16str(1, ch);
106+
std::string str = u16converter.to_bytes(u16str);
107+
std::vector<llama_token> tokens = llama_tokenize(ctx, escape_whitespace(str).c_str(), false);
108+
if (tokens.size() == 1) {
109+
fprintf(stderr, "%s : info: %s tokenized to %d \n",
110+
__func__, str.c_str(), tokens[0]);
114111
}
115-
std::vector<llama_token> tokens = llama_tokenize(ctx, escape_whitespace(str), false);
112+
}
113+
114+
std::wstring_convert<typename std::codecvt_utf8<char32_t>, char32_t> u32converter;
115+
for (char32_t ch = 0x0000; ch < 0x0010ffff; ++ch) {
116+
std::u32string u32str(1, ch);
117+
std::string str = u32converter.to_bytes(u32str);
118+
std::vector<llama_token> tokens = llama_tokenize(ctx, escape_whitespace(str).c_str(), false);
116119
if (tokens.size() == 1) {
117120
fprintf(stderr, "%s : info: %s tokenized to %d \n", __func__, str.c_str(), tokens[0]);
118121
}
119122
}
123+
#endif
120124

121125
llama_free_model(model);
122126
llama_free(ctx);

0 commit comments

Comments
 (0)