-
Notifications
You must be signed in to change notification settings - Fork 90
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 regex loading from tokenizer.json and code refinement #863
Draft
wenbingl
wants to merge
2
commits into
main
Choose a base branch
from
toki
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+128
−76
Draft
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -44,6 +44,58 @@ class BpeModel { | |
} | ||
} | ||
|
||
OrtxStatus LoadPreTokenizer(const json& bpe_model) { | ||
auto node_pre_tokenizer = bpe_model.find("pre_tokenizer"); | ||
if (node_pre_tokenizer == bpe_model.end() || node_pre_tokenizer->is_null()) { | ||
return {}; | ||
} | ||
|
||
auto iter_type = node_pre_tokenizer->find("type"); | ||
if (iter_type == node_pre_tokenizer->end() || iter_type->is_null()) { | ||
return {}; | ||
} | ||
|
||
if (iter_type->get<std::string>() != "Sequence") { | ||
return {kOrtxErrorNotImplemented, "Unsupported pretokenizer type!"}; | ||
} | ||
|
||
auto iter_node_list = node_pre_tokenizer->find("pretokenizers"); | ||
|
||
if (iter_node_list == node_pre_tokenizer->end() || iter_node_list->is_null()) { | ||
return {}; | ||
} | ||
|
||
for (const auto& node : *iter_node_list) { | ||
auto iter_type = node.find("type"); | ||
if (iter_type == node.end() || iter_type->is_null()) { | ||
continue; // ignore unknown pre-tokenizer type | ||
} | ||
|
||
|
||
auto pre_type = iter_type->get<std::string>(); | ||
if (pre_type == "Split") { | ||
auto iter_pattern = node.find("pattern"); | ||
if (iter_pattern == node.end() || iter_pattern->is_null()) { | ||
continue; | ||
} | ||
|
||
auto regex_str = iter_pattern->find("Regex"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i am seeing some examples of lowercase "regex" in tokenizer.json as well - perhaps we make the case insensitive here? |
||
if (regex_str == iter_pattern->end() || regex_str->is_null()) { | ||
continue; | ||
} | ||
|
||
pre_tokenizer_regex_ = regex_str->get<std::string>(); | ||
} else if (pre_type == "ByteLevel") { | ||
; // need to add more flag support here in the future | ||
} | ||
else { | ||
return {kOrtxErrorNotImplemented, "Unsupported pretokenizer type!"}; | ||
} | ||
} | ||
|
||
return {}; | ||
} | ||
|
||
OrtxStatus Load(std::istream& vocab_stream, std::istream& merges_stream, const char* unk_token, | ||
const char* special_tokens, bool spm_converted) { | ||
nlohmann::json tok_json; | ||
|
@@ -121,6 +173,8 @@ class BpeModel { | |
} | ||
|
||
OrtxStatus Load(const json& bpe_model, const char* /* special_tokens */, bool spm_converted) { | ||
ORTX_RETURN_IF_ERROR(LoadPreTokenizer(bpe_model)); | ||
|
||
const json& vocab_json = bpe_model["vocab"]; | ||
const json& merges_json = bpe_model["merges"]; | ||
vocab_json.get_to(vocab_map_); | ||
|
@@ -358,6 +412,19 @@ class BpeModel { | |
|
||
const std::string& GetEndOfWordSuffix() const { return end_of_word_suffix_; } | ||
|
||
std::string GetPreTokenizerRegex(const std::string& model_name) const { | ||
if (!pre_tokenizer_regex_.empty()) { | ||
return pre_tokenizer_regex_; | ||
} | ||
|
||
if (model_name == "Llama") { | ||
return bpe::PreTokenizerWithRegEx::LLAMA_REGEX_PATTERN; | ||
} | ||
|
||
// by default, use the GPT2 pretokenizer regex | ||
return bpe::PreTokenizerWithRegEx::GPT2_REGEX_PATTERN; | ||
} | ||
|
||
private: | ||
struct BpeNode { | ||
uint32_t id; | ||
|
@@ -379,6 +446,7 @@ class BpeModel { | |
uint32_t unk_id_ = (std::numeric_limits<uint32_t>::max)(); | ||
bpe::SpecialTokenMap special_tokens_; | ||
TrieTree<char32_t> added_tokens_; | ||
std::string pre_tokenizer_regex_; | ||
}; | ||
|
||
} // 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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something in the test seems off, or maybe my understanding is wrong - i saw in
test_pp_api.py
you use the"amd/AMD-OLMo-1B-SFT-DPO"
model, and if you look in thetokenizer.json
for it,pre_tokenizer
type is "ByteLevel", not "Sequence" (Note: you have support for "ByteLevel" for "pretokenizers" below, (which I guess you are expecting within the "pre_tokenizer") but not for "pre_tokenizer" itself); so it would fail here right? (but in the CI it is passing)So - maybe we should add "ByteLevel" to the supported types for "pre_tokenizer" as well here, but also first identify why it is not failing the test currently, perhaps the type is not being extracted right or it is conflating "pretokenizers" and "pre_tokenizer".