Skip to content

Commit b93cd82

Browse files
committed
Fix hf tokenizer handling of special tokens
1 parent 606b1b2 commit b93cd82

File tree

3 files changed

+31
-16
lines changed

3 files changed

+31
-16
lines changed

include/pytorch/tokenizers/bpe_tokenizer_base.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
#include <pytorch/tokenizers/string_integer_map.h>
2626
#include <pytorch/tokenizers/tokenizer.h>
2727

28+
#include "re2/re2.h"
29+
2830
namespace tokenizers {
2931
namespace detail {
3032

@@ -104,6 +106,29 @@ static Result<TokenMap> buildTokenMap(
104106
return buildTokenMap(std::move(pairs));
105107
}
106108

109+
static Result<std::unique_ptr<IRegex>> build_special_token_regex(
110+
const TokenMap& special_token_map) {
111+
std::string special_pattern;
112+
const std::size_t count = special_token_map.size();
113+
114+
std::cout << "iterating" << std::endl;
115+
for (std::size_t i = 0; i < count; ++i) {
116+
std::cout << "i: " << i << "/" << count << std::endl;
117+
const auto& [token, _] = special_token_map.getElement(i);
118+
std::cout << "token: " << token << std::endl;
119+
if (!special_pattern.empty()) {
120+
special_pattern += "|";
121+
}
122+
special_pattern += re2::RE2::QuoteMeta(std::string(token));
123+
}
124+
std::cout << "special pattern: " << special_pattern << std::endl;
125+
126+
if (special_pattern.empty()) {
127+
return static_cast<std::unique_ptr<IRegex>>(nullptr);
128+
}
129+
return create_regex(special_pattern);
130+
}
131+
107132
class BPETokenizerBase : public Tokenizer {
108133
public:
109134
Result<std::vector<uint64_t>>

src/hf_tokenizer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,11 @@ Error HFTokenizer::load(const std::string& path) {
6969
special_tokens,
7070
[](const auto& it) -> std::string { return it.at("content"); },
7171
[](const auto& it) -> std::uint64_t { return it.at("id"); }));
72+
73+
// Create special token regex to help later with encoding.
74+
special_token_regex_ = TK_UNWRAP(detail::build_special_token_regex(special_token_map));
75+
76+
// Store for future use.
7277
special_token_map_.emplace(std::move(special_token_map));
7378
} catch (const json::out_of_range& e) {
7479
fprintf(stderr, "Could not parse special tokens: %s\n", e.what());

src/tiktoken.cpp

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
#include <fstream>
3333
#include <limits>
3434
#include <unordered_set>
35-
#include "re2/re2.h"
3635

3736
namespace tokenizers {
3837

@@ -47,20 +46,6 @@ static Result<std::unique_ptr<IRegex>> _create_regex(
4746
return create_regex(pattern);
4847
}
4948

50-
static Result<std::unique_ptr<IRegex>> _build_special_token_regex(
51-
const std::vector<std::pair<std::string, std::uint64_t>>& special_encoder) {
52-
std::string special_pattern;
53-
for (const auto& ele : special_encoder) {
54-
if (!special_pattern.empty()) {
55-
special_pattern += "|";
56-
}
57-
special_pattern += re2::RE2::QuoteMeta(ele.first);
58-
}
59-
if (special_pattern.empty()) {
60-
return static_cast<std::unique_ptr<IRegex>>(nullptr);
61-
}
62-
return _create_regex(special_pattern);
63-
}
6449

6550
static Result<std::pair<std::string, uint64_t>> _parse(
6651
const std::string& line) {
@@ -153,7 +138,7 @@ Error Tiktoken::load(const std::string& path) {
153138

154139
_regex = TK_UNWRAP(_create_regex(_pattern));
155140
special_token_regex_ =
156-
TK_UNWRAP(_build_special_token_regex(special_token_map));
141+
TK_UNWRAP(detail::build_special_token_regex(TokenMap(special_token_map)));
157142

158143
// initialize vocab_size, bos_tok, eos_tok
159144
vocab_size_ = token_map_->size() + special_token_map_->size();

0 commit comments

Comments
 (0)