-
Notifications
You must be signed in to change notification settings - Fork 71
feat: port TokenizerModule to C++ #393
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
Changes from 19 commits
Commits
Show all changes
23 commits
Select commit
Hold shift + click to select a range
3ced9e2
feat: add static libs & include for tokenizers-cpp
chmjkb 78a1e98
fix: add object detection to installer check
chmjkb 7844378
chore: add TokenizerModule to RnExecutorchInstaller
chmjkb f522ba2
wip
chmjkb b676862
wip
chmjkb c5acf97
fix: register TokenizerModule constructor & fix concept usages
chmjkb 93ba4a6
fix: implement getMemoryLowerBound
chmjkb 6db7d24
fix: remove redundant shared pointer usage within et bindings
chmjkb 0ed7ea5
feat: Add remaining Tokenizers method implementations to c++
chmjkb dc57ff0
feat: implement remaining tokenizer methods in TS api
chmjkb b6d51d7
chore: remove consts
chmjkb 73e6058
chore: remove reduntant includes in StyleTransfer.cpp
chmjkb f85f5cd
chore: remove redundant loge, call std::move() when forwarding args t…
chmjkb 3132dbf
wip: tokenizer hook refactor
chmjkb f9baa74
fix: replace old tokenizermodule with the new one, make changes in st…
chmjkb 89d1ee1
chore: make tokenizer private
chmjkb 9ebcfed
chore: fix return types in native code, add const / const noexcept
chmjkb f56b454
refactor: move repeated code to a function
chmjkb e46e795
refactor: make it possible to pass methodname to ensureTokenizerLoaded
chmjkb b55947f
fix: export all the TokenizerModule methods to JS & add a specific co…
chmjkb 75f5ede
chore: use list initialization in TokenizerModule constructor
chmjkb 4e5ba99
refactor: 🧹
chmjkb f5c9001
chore: make TokenizerModule constructor explicit
chmjkb 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 hidden or 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 hidden or 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
53 changes: 53 additions & 0 deletions
53
packages/react-native-executorch/common/rnexecutorch/TokenizerModule.cpp
This file contains hidden or 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,53 @@ | ||
| #include "TokenizerModule.h" | ||
| #include <executorch/extension/module/module.h> | ||
| #include <filesystem> | ||
| #include <rnexecutorch/Log.h> | ||
| #include <rnexecutorch/data_processing/FileUtils.h> | ||
|
|
||
| namespace rnexecutorch { | ||
| using namespace facebook; | ||
|
|
||
| TokenizerModule::TokenizerModule( | ||
| std::string source, std::shared_ptr<react::CallInvoker> callInvoker) { | ||
| auto blob = fileutils::loadBytesFromFile(source); | ||
| memorySizeLowerBound = std::filesystem::file_size(source); | ||
| tokenizer = tokenizers::Tokenizer::FromBlobJSON(blob); | ||
| } | ||
|
|
||
| void TokenizerModule::ensureTokenizerLoaded( | ||
| const std::string &methodName) const { | ||
| if (!tokenizer) { | ||
| throw std::runtime_error( | ||
| methodName + " function was called on an uninitialized tokenizer!"); | ||
| } | ||
| } | ||
|
|
||
| std::vector<int32_t> TokenizerModule::encode(std::string s) const { | ||
| ensureTokenizerLoaded("encode"); | ||
| return tokenizer->Encode(s); | ||
| } | ||
|
|
||
| std::string TokenizerModule::decode(std::vector<int32_t> vec, | ||
| bool skipSpecialTokens) const { | ||
| ensureTokenizerLoaded("decode"); | ||
| return tokenizer->Decode(vec, skipSpecialTokens); | ||
| } | ||
|
|
||
| size_t TokenizerModule::getVocabSize() const { | ||
| ensureTokenizerLoaded("getVocabSize"); | ||
| return tokenizer->GetVocabSize(); | ||
| } | ||
|
|
||
| std::string TokenizerModule::idToToken(int32_t tokenId) const { | ||
| ensureTokenizerLoaded("idToToken"); | ||
| return tokenizer->IdToToken(tokenId); | ||
| } | ||
|
|
||
| int32_t TokenizerModule::tokenToId(std::string token) const { | ||
| ensureTokenizerLoaded("tokenToId"); | ||
| return tokenizer->TokenToId(token); | ||
| } | ||
| std::size_t TokenizerModule::getMemoryLowerBound() const noexcept { | ||
| return memorySizeLowerBound; | ||
| } | ||
| } // namespace rnexecutorch | ||
26 changes: 26 additions & 0 deletions
26
packages/react-native-executorch/common/rnexecutorch/TokenizerModule.h
This file contains hidden or 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,26 @@ | ||
| #pragma once | ||
|
|
||
| #include <ReactCommon/CallInvoker.h> | ||
| #include <string> | ||
| #include <tokenizers-cpp/tokenizers_cpp.h> | ||
|
|
||
| namespace rnexecutorch { | ||
| using namespace facebook; | ||
|
|
||
| class TokenizerModule { | ||
| public: | ||
| TokenizerModule(std::string source, | ||
|
chmjkb marked this conversation as resolved.
Outdated
|
||
| std::shared_ptr<react::CallInvoker> callInvoker); | ||
| std::vector<int32_t> encode(std::string s) const; | ||
| std::string decode(std::vector<int32_t> vec, bool skipSpecialTokens) const; | ||
| std::string idToToken(int32_t tokenId) const; | ||
| int32_t tokenToId(std::string token) const; | ||
| std::size_t getVocabSize() const; | ||
| std::size_t getMemoryLowerBound() const noexcept; | ||
|
|
||
| private: | ||
| void ensureTokenizerLoaded(const std::string &methodName) const; | ||
| std::unique_ptr<tokenizers::Tokenizer> tokenizer; | ||
| std::size_t memorySizeLowerBound{0}; | ||
| }; | ||
| } // namespace rnexecutorch | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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.
Uh oh!
There was an error while loading. Please reload this page.