-
Notifications
You must be signed in to change notification settings - Fork 94
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add a generic tokenizer operator to support Hugging Face Tokenizer wi…
…th JSON data files. (#859) * add the hf json file embedded tokenizer * add a unit test * fix the build break * update the ort version * build/test break fixes
- Loading branch information
Showing
18 changed files
with
2,227 additions
and
2,119 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
#include <variant> | ||
|
||
#include "bpe_kernels.h" | ||
#include "ugm_kernels.hpp" | ||
|
||
#include "ext_status.h" | ||
#include "op_def_struct.h" | ||
#include "ort_c_to_cpp.h" | ||
|
||
namespace ort_extensions { | ||
|
||
class JsonTokenizerOpKernel { | ||
public: | ||
OrtStatusPtr OnModelAttach(const OrtApi& api, const OrtKernelInfo& info) { | ||
std::string config_json; | ||
ORTW_RETURN_IF_ERROR(OrtW::API::GetOpAttributeString(api, info, "tokenizer_config", config_json)); | ||
|
||
std::string vocab_json; | ||
ORTW_RETURN_IF_ERROR(OrtW::API::GetOpAttributeString(api, info, "tokenizer_vocab", vocab_json)); | ||
|
||
TokenJsonConfig cfg; | ||
OrtxTokenizerBlob blob({config_json.c_str(), config_json.length()}, | ||
{vocab_json.c_str(), vocab_json.length()}); | ||
|
||
ORTX_RETURN_IF_ERROR(cfg.LoadFromBlob(blob)); | ||
|
||
auto type = TokenJsonConfig::GetTokenType(cfg.tokenizer_class_); | ||
if (type == TokenType::kUnigram) { | ||
tokenizer_ = std::make_unique<SpmUgmTokenizer>(); | ||
} else if (type == TokenType::kBPE) { | ||
tokenizer_ = std::make_unique<JsonFastTokenizer>(); | ||
} else { | ||
return OrtxStatus(kOrtxErrorCorruptData, "Unknown tokenizer type"); | ||
} | ||
|
||
return std::visit([&](auto& ptr) { return ptr->Load(cfg); }, tokenizer_); | ||
} | ||
|
||
OrtxStatus Compute(const ortc::Tensor<std::string>& input, | ||
ortc::Tensor<int64_t>& tokenize_output, | ||
std::optional<ortc::Tensor<int64_t>*> attention_mask = std::nullopt, | ||
std::optional<ortc::Tensor<int64_t>*> offset_mapping = std::nullopt) const { | ||
|
||
return std::visit([&](auto& ptr) { | ||
return ptr->Compute(input, tokenize_output, attention_mask, offset_mapping); | ||
}, tokenizer_); | ||
} | ||
|
||
private: | ||
std::variant<std::unique_ptr<JsonFastTokenizer>, std::unique_ptr<SpmUgmTokenizer>> tokenizer_; | ||
}; | ||
|
||
} // namespace ort_extensions |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.