-
Notifications
You must be signed in to change notification settings - Fork 71
feat: add support for hugging face tokenizers, add executorch and tokenizers as submodules #144
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 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
a1b1ec9
feat: add support for hugging face tokenizers(ios)
NorbertKlockiewicz 50d018d
feat: add lib with support for hf tokenizers on android, added tokeni…
NorbertKlockiewicz e49fe02
chore: update paths
NorbertKlockiewicz 1499671
fix: remove unnecessary gitignore
NorbertKlockiewicz f5e289c
fix: correctly add tokenizers as submodule
NorbertKlockiewicz 0fd1c5e
chore: update lib versions to include latest changes
NorbertKlockiewicz 08b774c
feat: expose tokenizers api to typescript, requested changes
NorbertKlockiewicz dd564e1
refactor: remove Log.d
NorbertKlockiewicz d7af218
refactor: add try catch in tokenizer native modules
NorbertKlockiewicz 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| [submodule "executorch"] | ||
| path = third-party/executorch | ||
| url = https://github.com/software-mansion-labs/executorch | ||
| [submodule "tokenizers-cpp"] | ||
| path = third-party/tokenizers-cpp | ||
| url = https://github.com/software-mansion-labs/tokenizers-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
Binary file not shown.
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
66 changes: 66 additions & 0 deletions
66
android/src/main/java/com/swmansion/rnexecutorch/Tokenizer.kt
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,66 @@ | ||
| package com.swmansion.rnexecutorch | ||
|
|
||
| import com.facebook.react.bridge.Promise | ||
| import com.facebook.react.bridge.ReactApplicationContext | ||
| import com.facebook.react.bridge.ReadableArray | ||
| import com.swmansion.rnexecutorch.utils.ArrayUtils.Companion.createIntArray | ||
| import com.swmansion.rnexecutorch.utils.ArrayUtils.Companion.createReadableArrayFromIntArray | ||
| import com.swmansion.rnexecutorch.utils.ETError | ||
| import org.pytorch.executorch.HuggingFaceTokenizer | ||
| import java.net.URL | ||
|
|
||
| class Tokenizer( | ||
| reactContext: ReactApplicationContext, | ||
| ) : NativeTokenizerSpec(reactContext) { | ||
| private lateinit var tokenizer: HuggingFaceTokenizer | ||
|
|
||
| companion object { | ||
| const val NAME = "Tokenizer" | ||
| } | ||
|
|
||
| override fun load( | ||
| tokenizerSource: String, | ||
| promise: Promise, | ||
| ) { | ||
| try { | ||
| tokenizer = HuggingFaceTokenizer(URL(tokenizerSource).path) | ||
| promise.resolve(0) | ||
| } catch (e: Exception) { | ||
| promise.reject(e.message!!, ETError.InvalidModelSource.toString()) | ||
| } | ||
| } | ||
|
|
||
| override fun decode( | ||
| input: ReadableArray, | ||
| promise: Promise, | ||
| ) { | ||
| promise.resolve(tokenizer.decode(createIntArray(input))) | ||
| } | ||
|
|
||
| override fun encode( | ||
| input: String, | ||
| promise: Promise, | ||
| ) { | ||
| promise.resolve(createReadableArrayFromIntArray(tokenizer.encode(input))) | ||
| } | ||
|
|
||
| override fun getVocabSize(promise: Promise) { | ||
| promise.resolve(tokenizer.vocabSize) | ||
| } | ||
|
|
||
| override fun idToToken( | ||
| id: Double, | ||
| promise: Promise, | ||
| ) { | ||
| promise.resolve(tokenizer.idToToken(id.toInt())) | ||
| } | ||
|
|
||
| override fun tokenToId( | ||
| token: String, | ||
| promise: Promise, | ||
| ) { | ||
| promise.resolve(tokenizer.tokenToId(token)) | ||
| } | ||
|
NorbertKlockiewicz marked this conversation as resolved.
|
||
|
|
||
| override fun getName(): String = NAME | ||
| } | ||
Binary file modified
BIN
+6.43 MB
(150%)
ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/ExecutorchLib
Binary file not shown.
12 changes: 12 additions & 0 deletions
12
...ib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/Headers/HuggingFaceTokenizer.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,12 @@ | ||
| #import <Foundation/Foundation.h> | ||
|
|
||
| @interface HuggingFaceTokenizer : NSObject | ||
|
|
||
| - (instancetype)initWithTokenizerPath:(NSString *)tokenizerPath; | ||
| - (NSArray<NSNumber *> *)encode:(NSString *)text; | ||
| - (NSString *)decode:(NSArray<NSNumber *> *)tokenIds; | ||
| - (NSUInteger)getVocabSize; | ||
| - (NSString *)idToToken:(NSInteger)tokenId; | ||
| - (NSInteger)tokenToId:(NSString *)token; | ||
|
|
||
| @end |
Binary file modified
BIN
-5 Bytes
(99%)
ios/ExecutorchLib.xcframework/ios-arm64-simulator/ExecutorchLib.framework/Info.plist
Binary file not shown.
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
Binary file modified
BIN
+6.54 MB
(150%)
ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/ExecutorchLib
Binary file not shown.
12 changes: 12 additions & 0 deletions
12
...xecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/Headers/HuggingFaceTokenizer.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,12 @@ | ||
| #import <Foundation/Foundation.h> | ||
|
|
||
| @interface HuggingFaceTokenizer : NSObject | ||
|
|
||
| - (instancetype)initWithTokenizerPath:(NSString *)tokenizerPath; | ||
| - (NSArray<NSNumber *> *)encode:(NSString *)text; | ||
| - (NSString *)decode:(NSArray<NSNumber *> *)tokenIds; | ||
| - (NSUInteger)getVocabSize; | ||
| - (NSString *)idToToken:(NSInteger)tokenId; | ||
| - (NSInteger)tokenToId:(NSString *)token; | ||
|
|
||
| @end |
Binary file modified
BIN
-5 Bytes
(99%)
ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/Info.plist
Binary file not shown.
135 changes: 135 additions & 0 deletions
135
ios/ExecutorchLib.xcframework/ios-arm64/ExecutorchLib.framework/_CodeSignature/CodeResources
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,135 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
| <plist version="1.0"> | ||
| <dict> | ||
| <key>files</key> | ||
| <dict> | ||
| <key>Headers/ETModel.h</key> | ||
| <data> | ||
| uR77dUqxDWLwIE0h9dvXHSjMFWo= | ||
| </data> | ||
| <key>Headers/HuggingFaceTokenizer.h</key> | ||
| <data> | ||
| cVZsliuTmV2umgK601d6PamTRSw= | ||
| </data> | ||
| <key>Headers/LLaMARunner.h</key> | ||
| <data> | ||
| 7OcOQPzHkK7faqsbtXBztEr9VO0= | ||
| </data> | ||
| <key>Info.plist</key> | ||
| <data> | ||
| xUwVRDNEWZOSxtl3vg4xATbAoi8= | ||
| </data> | ||
| </dict> | ||
| <key>files2</key> | ||
| <dict> | ||
| <key>Headers/ETModel.h</key> | ||
| <dict> | ||
| <key>hash2</key> | ||
| <data> | ||
| +Ty+KzH7+xNA5B9kfmD44hgTJKIZuk2qN1kauF6thOw= | ||
| </data> | ||
| </dict> | ||
| <key>Headers/HuggingFaceTokenizer.h</key> | ||
| <dict> | ||
| <key>hash2</key> | ||
| <data> | ||
| 0ETM5qw12+W8ULx2zP2UkVomFrBRnwAr8I7po2ACk/k= | ||
| </data> | ||
| </dict> | ||
| <key>Headers/LLaMARunner.h</key> | ||
| <dict> | ||
| <key>hash2</key> | ||
| <data> | ||
| AZECdFqfkT4YWu8Nqga5ALaJUme8IOAnhOaAUM+iOvU= | ||
| </data> | ||
| </dict> | ||
| </dict> | ||
| <key>rules</key> | ||
| <dict> | ||
| <key>^.*</key> | ||
| <true/> | ||
| <key>^.*\.lproj/</key> | ||
| <dict> | ||
| <key>optional</key> | ||
| <true/> | ||
| <key>weight</key> | ||
| <real>1000</real> | ||
| </dict> | ||
| <key>^.*\.lproj/locversion.plist$</key> | ||
| <dict> | ||
| <key>omit</key> | ||
| <true/> | ||
| <key>weight</key> | ||
| <real>1100</real> | ||
| </dict> | ||
| <key>^Base\.lproj/</key> | ||
| <dict> | ||
| <key>weight</key> | ||
| <real>1010</real> | ||
| </dict> | ||
| <key>^version.plist$</key> | ||
| <true/> | ||
| </dict> | ||
| <key>rules2</key> | ||
| <dict> | ||
| <key>.*\.dSYM($|/)</key> | ||
| <dict> | ||
| <key>weight</key> | ||
| <real>11</real> | ||
| </dict> | ||
| <key>^(.*/)?\.DS_Store$</key> | ||
| <dict> | ||
| <key>omit</key> | ||
| <true/> | ||
| <key>weight</key> | ||
| <real>2000</real> | ||
| </dict> | ||
| <key>^.*</key> | ||
| <true/> | ||
| <key>^.*\.lproj/</key> | ||
| <dict> | ||
| <key>optional</key> | ||
| <true/> | ||
| <key>weight</key> | ||
| <real>1000</real> | ||
| </dict> | ||
| <key>^.*\.lproj/locversion.plist$</key> | ||
| <dict> | ||
| <key>omit</key> | ||
| <true/> | ||
| <key>weight</key> | ||
| <real>1100</real> | ||
| </dict> | ||
| <key>^Base\.lproj/</key> | ||
| <dict> | ||
| <key>weight</key> | ||
| <real>1010</real> | ||
| </dict> | ||
| <key>^Info\.plist$</key> | ||
| <dict> | ||
| <key>omit</key> | ||
| <true/> | ||
| <key>weight</key> | ||
| <real>20</real> | ||
| </dict> | ||
| <key>^PkgInfo$</key> | ||
| <dict> | ||
| <key>omit</key> | ||
| <true/> | ||
| <key>weight</key> | ||
| <real>20</real> | ||
| </dict> | ||
| <key>^embedded\.provisionprofile$</key> | ||
| <dict> | ||
| <key>weight</key> | ||
| <real>20</real> | ||
| </dict> | ||
| <key>^version\.plist$</key> | ||
| <dict> | ||
| <key>weight</key> | ||
| <real>20</real> | ||
| </dict> | ||
| </dict> | ||
| </dict> | ||
| </plist> |
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,5 @@ | ||
| #import <RnExecutorchSpec/RnExecutorchSpec.h> | ||
|
|
||
| @interface Tokenizer : NSObject <NativeTokenizerSpec> | ||
|
|
||
| @end |
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,58 @@ | ||
| #import "Tokenizer.h" | ||
| #import <ExecutorchLib/HuggingFaceTokenizer.h> | ||
| #import <React/RCTBridgeModule.h> | ||
|
|
||
| @implementation Tokenizer { | ||
| HuggingFaceTokenizer *tokenizer; | ||
| } | ||
|
|
||
| RCT_EXPORT_MODULE() | ||
|
|
||
| - (void)load:(NSString *)tokenizerSource | ||
| resolve:(RCTPromiseResolveBlock)resolve | ||
| reject:(RCTPromiseRejectBlock)reject { | ||
| @try { | ||
| tokenizer = [[HuggingFaceTokenizer alloc] | ||
| initWithTokenizerPath:[NSURL URLWithString:tokenizerSource].path]; | ||
| resolve(@(0)); | ||
| } @catch (NSException *exception) { | ||
| reject(@"Tokenizer_Error", @"Failed to load tokenizer", nil); | ||
| } | ||
| } | ||
|
|
||
| - (void)encode:(NSString *)input | ||
| resolve:(RCTPromiseResolveBlock)resolve | ||
| reject:(RCTPromiseRejectBlock)reject { | ||
| resolve([tokenizer encode:input]); | ||
| } | ||
|
|
||
| - (void)decode:(NSArray *)input | ||
| resolve:(RCTPromiseResolveBlock)resolve | ||
| reject:(RCTPromiseRejectBlock)reject { | ||
| resolve([tokenizer decode:input]); | ||
| } | ||
|
|
||
| - (void)getVocabSize:(RCTPromiseResolveBlock)resolve | ||
| reject:(RCTPromiseRejectBlock)reject { | ||
| resolve([NSNumber numberWithUnsignedInteger:[tokenizer getVocabSize]]); | ||
| } | ||
|
|
||
| - (void)idToToken:(double)input | ||
| resolve:(RCTPromiseResolveBlock)resolve | ||
| reject:(RCTPromiseRejectBlock)reject { | ||
| NSInteger tokenID = (NSInteger)input; | ||
| resolve([tokenizer idToToken:tokenID]); | ||
| } | ||
|
|
||
| - (void)tokenToId:(NSString *)input | ||
| resolve:(RCTPromiseResolveBlock)resolve | ||
| reject:(RCTPromiseRejectBlock)reject { | ||
| resolve([NSNumber numberWithInteger:[tokenizer tokenToId:input]]); | ||
| } | ||
|
|
||
| - (std::shared_ptr<facebook::react::TurboModule>)getTurboModule: | ||
| (const facebook::react::ObjCTurboModule::InitParams &)params { | ||
| return std::make_shared<facebook::react::NativeTokenizerSpecJSI>(params); | ||
|
NorbertKlockiewicz marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| @end | ||
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.