-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[js/rn] Implement blob exchange by JSI instead of use base64 (#16094)
### 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
- Loading branch information
Showing
23 changed files
with
935 additions
and
141 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
project(OnnxruntimeJSIHelper) | ||
cmake_minimum_required(VERSION 3.9.0) | ||
|
||
set (PACKAGE_NAME "onnxruntime-react-native") | ||
set (BUILD_DIR ${CMAKE_SOURCE_DIR}/build) | ||
set(CMAKE_VERBOSE_MAKEFILE ON) | ||
set(CMAKE_CXX_STANDARD 17) | ||
|
||
file(TO_CMAKE_PATH "${NODE_MODULES_DIR}/react-native/ReactCommon/jsi/jsi/jsi.cpp" libPath) | ||
|
||
include_directories( | ||
"${NODE_MODULES_DIR}/react-native/React" | ||
"${NODE_MODULES_DIR}/react-native/React/Base" | ||
"${NODE_MODULES_DIR}/react-native/ReactCommon/jsi" | ||
) | ||
|
||
add_library(onnxruntimejsihelper | ||
SHARED | ||
${libPath} | ||
src/main/cpp/cpp-adapter.cpp | ||
) | ||
|
||
# Configure C++ 17 | ||
set_target_properties( | ||
onnxruntimejsihelper PROPERTIES | ||
CXX_STANDARD 17 | ||
CXX_EXTENSIONS OFF | ||
POSITION_INDEPENDENT_CODE ON | ||
) | ||
|
||
find_library(log-lib log) | ||
|
||
target_link_libraries( | ||
onnxruntimejsihelper | ||
${log-lib} # <-- Logcat logger | ||
android # <-- Android JNI core | ||
) |
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
36 changes: 36 additions & 0 deletions
36
js/react_native/android/src/androidTest/java/ai/onnxruntime/reactnative/FakeBlobModule.java
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,36 @@ | ||
// Copyright (c) Microsoft Corporation. All rights reserved. | ||
// Licensed under the MIT License. | ||
|
||
package ai.onnxruntime.reactnative; | ||
|
||
import com.facebook.react.bridge.Arguments; | ||
import com.facebook.react.bridge.JavaOnlyMap; | ||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReadableMap; | ||
import com.facebook.react.modules.blob.BlobModule; | ||
|
||
public class FakeBlobModule extends BlobModule { | ||
|
||
public FakeBlobModule(ReactApplicationContext context) { super(null); } | ||
|
||
@Override | ||
public String getName() { | ||
return "BlobModule"; | ||
} | ||
|
||
public JavaOnlyMap testCreateData(byte[] bytes) { | ||
String blobId = store(bytes); | ||
JavaOnlyMap data = new JavaOnlyMap(); | ||
data.putString("blobId", blobId); | ||
data.putInt("offset", 0); | ||
data.putInt("size", bytes.length); | ||
return data; | ||
} | ||
|
||
public byte[] testGetData(ReadableMap data) { | ||
String blobId = data.getString("blobId"); | ||
int offset = data.getInt("offset"); | ||
int size = data.getInt("size"); | ||
return resolve(blobId, offset, size); | ||
} | ||
} |
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.