Skip to content

Fix tokenizer special token handling #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 2, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions include/pytorch/tokenizers/bpe_tokenizer_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
#include <pytorch/tokenizers/string_integer_map.h>
#include <pytorch/tokenizers/tokenizer.h>

#include "re2/re2.h"

namespace tokenizers {
namespace detail {

Expand Down Expand Up @@ -104,6 +106,25 @@ static Result<TokenMap> buildTokenMap(
return buildTokenMap(std::move(pairs));
}

static Result<std::unique_ptr<IRegex>> build_special_token_regex(
const TokenMap& special_token_map) {
std::string special_pattern;
const std::size_t count = special_token_map.size();

for (std::size_t i = 0; i < count; ++i) {
const auto& [token, _] = special_token_map.getElement(i);
if (!special_pattern.empty()) {
special_pattern += "|";
}
special_pattern += re2::RE2::QuoteMeta(std::string(token));
}

if (special_pattern.empty()) {
return static_cast<std::unique_ptr<IRegex>>(nullptr);
}
return create_regex(special_pattern);
}

class BPETokenizerBase : public Tokenizer {
public:
Result<std::vector<uint64_t>>
Expand Down
5 changes: 5 additions & 0 deletions src/hf_tokenizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,11 @@ Error HFTokenizer::load(const std::string& path) {
special_tokens,
[](const auto& it) -> std::string { return it.at("content"); },
[](const auto& it) -> std::uint64_t { return it.at("id"); }));

// Create special token regex to help later with encoding.
special_token_regex_ = TK_UNWRAP(detail::build_special_token_regex(special_token_map));

// Store for future use.
special_token_map_.emplace(std::move(special_token_map));
} catch (const json::out_of_range& e) {
fprintf(stderr, "Could not parse special tokens: %s\n", e.what());
Expand Down
17 changes: 1 addition & 16 deletions src/tiktoken.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
#include <fstream>
#include <limits>
#include <unordered_set>
#include "re2/re2.h"

namespace tokenizers {

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

static Result<std::unique_ptr<IRegex>> _build_special_token_regex(
const std::vector<std::pair<std::string, std::uint64_t>>& special_encoder) {
std::string special_pattern;
for (const auto& ele : special_encoder) {
if (!special_pattern.empty()) {
special_pattern += "|";
}
special_pattern += re2::RE2::QuoteMeta(ele.first);
}
if (special_pattern.empty()) {
return static_cast<std::unique_ptr<IRegex>>(nullptr);
}
return _create_regex(special_pattern);
}

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

_regex = TK_UNWRAP(_create_regex(_pattern));
special_token_regex_ =
TK_UNWRAP(_build_special_token_regex(special_token_map));
TK_UNWRAP(detail::build_special_token_regex(TokenMap(special_token_map)));

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