Skip to content
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

Add missing tokens from added_tokens_decoder into added_tokens #890

Merged
merged 5 commits into from
Feb 21, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
26 changes: 26 additions & 0 deletions operators/tokenizer/bpe_kernels.cc
Original file line number Diff line number Diff line change
Expand Up @@ -682,6 +682,32 @@ void JsonFastTokenizer::UpdateTokenizer(const TokenJsonConfig& config, const jso
}
}

const json added_tokens_decoder = config.added_tokens_decoder;

// Add any tokens from the added_tokens_decoder that were missing in added_tokens_
if (!added_tokens_decoder.empty()) {
for (const auto& [id_str, token] : added_tokens_decoder.items()) {
int id = std::stoi(id_str); // Convert key (ID) from string to integer

// Check if this token is already in the added_tokens_
auto existing_token = added_tokens_.find(ustring(token.value("content", "")));
if (existing_token == added_tokens_.end()) { // Token doesn't exist yet
// Prepare a new token (populate id's with the keys from added_tokens_decoder)
AddedToken added_token;
added_token.id_ = id;
added_token.content_ = token.value("content", "");
added_token.lstrip_ = token.value("lstrip", false);
added_token.normalized_ = token.value("normalized", false);
added_token.rstrip_ = token.value("rstrip", false);
added_token.single_word_ = token.value("single_word", false);
added_token.special_ = token.value("special", false);

// Add the new token to added_tokens_
added_tokens_.emplace(ustring(added_token.content_), added_token);
}
}
}

// iterate the added_tokens_ map and set the special tokens
for (const auto& [key, added_token] : added_tokens_) {
if (added_token.content_ == config.bos_token_) {
Expand Down
4 changes: 4 additions & 0 deletions operators/tokenizer/tokenizer_jsconfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ class TokenJsonConfig final {
~TokenJsonConfig() {}
using json = nlohmann::json;
using json_pointer = nlohmann::json_pointer<std::string>;
json added_tokens_decoder;

public:
OrtxStatus AppendModuleJson(json& json_config) {
Expand Down Expand Up @@ -191,6 +192,9 @@ class TokenJsonConfig final {
return OrtxStatus(kOrtxErrorInvalidArgument, "Failed to parse config json.");
}

// Store added_tokens_decoder to add any missed tokens into added_tokens in UpdateTokenizer
added_tokens_decoder = json_config.value("added_tokens_decoder", json::object());

auto module_cfg = tok_dir / "tokenizer_module.json";
if (module_cfg.exists()) {
std::ifstream module_ifs = module_cfg.open();
Expand Down
Loading