-
Notifications
You must be signed in to change notification settings - Fork 3k
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
[Feature Request] Migrate to JSI for React Native #16031
Comments
We have looked at adding this support but haven't had the resources available to do so. Do you have a production scenario you require this for? Whilst the base64 encode/decode overhead isn't great, typically model execution is the largest cost for a production model. I'm struggling to understand how base64 encode/decode could add up to multiple seconds in the comment you linked though. The buffer size can be pre-calculated, and it's a single iteration of the data to encode/decode. Reasonably trivial stuff. We don't do much when decoding either - convert from base64 using buffer and put in a typed array. onnxruntime/js/react_native/lib/backend.ts Lines 149 to 151 in 4dc4470
|
In the case the model run just take ~500ms for each decode session run, but every decode run in JS (decodeReturnType) take ~3.7s with ~5250000 size buffer. I've plan to use onnxruntime for production in the second half of the year, this may be the current challenge. Will be great if we have resources/maintenance here. |
We'll see if we can get it done in the next release. |
For #16094 I've been testing for a while, and there are a few things I see that aren't perfect:
For a task that requires multiple session runs (like whisper), such a delay may very large, it may take a few seconds. The best way still a full JSI migration, but currently the performance of PR #16094 is acceptable for me. |
Can you expand on what 'full JSI migration' would equate to? I have a very limited knowledge of JSI so I'm not sure what the cost/benefit of that would be. My understanding was the main reason for using JSI with ORT would be to avoid the base64 encode/decode when running the model (given that's typically the performance sensitive operation), and your much appreciated PR seems to cover that. |
The full JSI migration will need some refactor in my imagination, to rewrite
It may basically solve the delay of native bridge, especially the Android platform. The base64 encode/decode is my main concern, the full implementation of JSI may not be urgent. |
Thanks for the info. Is the native bridge delay on every |
This is result (use ASR task of this example, and patched #16094) to running whisper-tiny.en for a demo audio file
Platform: Android
Not sure if it might caused by some scheduling issue, so it could have a delay up to 60 ms. The difference may be less noticeable by use MNIST in the e2e project. |
### Description <!-- Describe your changes. --> - Create `OnnxruntimeJSIHelper` native module to provide two JSI functions - `jsiOnnxruntimeStoreArrayBuffer`: Store buffer in Blob Manager & return blob object (iOS: RCTBlobManager, Android: BlobModule) - `jsiOnnxruntimeResolveArrayBuffer`: Use blob object to get buffer - The part of implementation is reference to [react-native-blob-jsi-helper](https://github.com/mrousavy/react-native-blob-jsi-helper) - Replace base64 encode/decode - `loadModelFromBlob`: Rename from `loadModelFromBase64EncodedBuffer` - `run`: Use blob object to replace input.data & results[].data For [this context](#16031 (comment)), it saved a lot of time and avoid JS thread blocking in decode return type, it is 3700ms -> 5~20ms for the case. (resolve function only takes 0.x ms) ### Motivation and Context <!-- - Why is this change required? What problem does it solve? - If it fixes an open issue, please link to the issue here. --> It’s related to #16031, but not a full implementation for migrate to JSI. It just uses JSI through BlobManager to replace the slow part (base64 encode / decode). Rewriting it entirely in JSI could be complicated, like type convertion and threading. This PR might be considered a minor change. /cc @skottmckay
Describe the feature request
react_native binding
Migrate to JSI can help to improve performance issues with the current implementation of React Native bridge and base64 encode/decode.
For return type of tensor input and output, we can construct ArrayBuffers from existing memory buffers, but it may be only supported by newer React Native version (>= 0.71?). Another way is implement type like blob-jsi-helper does with memcpy but it may causes some memory issues.
I might try to send a PR later for iOS first.
For Android, since it's using C++, it may require some additional work to access onnxruntime directly. (Calling java class through JNI may become complicated)
Describe scenario use case
Transformers.js is trying to support React Native (huggingface/transformers.js#118 by @hans00), I found that the performance in base64 encode/decode of buffer is not so good. See also this comment.
I also try to use react-native-blob-jsi-helper to replace base64 by a patch for test purpose, see this draft PR for more details.
The text was updated successfully, but these errors were encountered: