[js/rn] support load model from buffer on Android#12676
Conversation
| public WritableMap loadModel(String uri, InputStream modelStream, ReadableMap options) throws Exception { | ||
| OrtSession ortSession = null; | ||
|
|
||
| if (!sessionMap.containsKey(uri)) { |
There was a problem hiding this comment.
What's the reason for removing the check for re-loading the same model?
Is a documentation change required for this as an app that was sloppy about how many times the model was loaded would now get duplicate inference sessions created. Not sure how likely that is in the real world though.
There was a problem hiding this comment.
As long as we support loading model from buffer, it is no longer possible to use the model's filepath as key. so I have to remove this check.
There is no documentation for this, and the duplication is never guaranteed - we don't do this for any language bindings for ORT. If users create inference session twice on the same path, we create 2 instances, and this is the expectation.
|
|
||
| constructor(path: string) { | ||
| constructor(pathOrBuffer: string|Uint8Array) { | ||
| this.#inferenceSession = binding; |
There was a problem hiding this comment.
Do we have tests at the react native level that can be updated to validate this path?
There was a problem hiding this comment.
no, but we can add the test case.
There was a problem hiding this comment.
still working on getting it tested on the react native level... looks like may require writing a simple react native test app using Jest framework.
| interface InferenceSession { | ||
| loadModel(modelPath: string, options: SessionOptions): Promise<ModelLoadInfoType>; | ||
| loadModelFromBase64EncodedBuffer?(buffer: string, options: SessionOptions): Promise<ModelLoadInfoType>; | ||
| run(key: string, feeds: FeedsType, fetches: FetchesType, options: RunOptions): Promise<ReturnType>; |
There was a problem hiding this comment.
Can we keep the base64 encoding as an implementation detail, and allow the user to provide a Uint8Array instead, maybe renaming this to loadModelFromBytes?
If we replace this binding with a JSI based solution the base64 encoding would be unnecessary.
…fs-eire/js-rn-support-android-load-model-from-buffer
| * @param options onnxruntime session options | ||
| * @return model loading information map, such as key, input names, and output names | ||
| */ | ||
| public WritableMap loadModelImpl(String uri, byte[] modelData, ReadableMap options) throws Exception { |
There was a problem hiding this comment.
this method could be private.
nit: it seems a bit odd to take both uri and modelData parameters in one function. I see that there is some common code after the OrtSession creation, perhaps that can be pulled into a helper function?
There was a problem hiding this comment.
yes. it can be pulled into a helper function.
but put it this way as for ios support, we might need to call ort.createSession with both modelPath and model data array - two paths, can be separated this way. just want to match the same pattern there.
js/react_native/android/src/main/java/ai/onnxruntime/reactnative/OnnxruntimeModule.java
Outdated
Show resolved
Hide resolved
js/react_native/android/src/main/java/ai/onnxruntime/reactnative/OnnxruntimeModule.java
Outdated
Show resolved
Hide resolved
js/react_native/android/src/main/java/ai/onnxruntime/reactnative/OnnxruntimeModule.java
Show resolved
Hide resolved
| @@ -46,6 +47,13 @@ public class OnnxruntimeModule extends ReactContextBaseJavaModule { | |||
| private static OrtEnvironment ortEnvironment = OrtEnvironment.getEnvironment(); | |||
| private static Map<String, OrtSession> sessionMap = new HashMap<>(); | |||
There was a problem hiding this comment.
do we ever clean up entries in sessionMap?
js/react_native/android/src/main/java/ai/onnxruntime/reactnative/OnnxruntimeModule.java
Outdated
Show resolved
Hide resolved
js/react_native/android/src/main/java/ai/onnxruntime/reactnative/OnnxruntimeModule.java
Outdated
Show resolved
Hide resolved
…13903) ### Description <!-- Describe your changes. --> As title. This pr is missing an un-updated index.android.gradle, which causing an unstable e2e unit test run for React Native CI. Revert the changes for now. ### 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. --> To unblock Ort React Native CI pipeline failure.
…13903) ### Description <!-- Describe your changes. --> As title. This pr is missing an un-updated index.android.gradle, which causing an unstable e2e unit test run for React Native CI. Revert the changes for now. ### 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. --> To unblock Ort React Native CI pipeline failure.
**Description**: [js/React Native] Add android implementation for creating session from buffer. microsoft#12500 Co-authored-by: Rachel Guo <guorachel@microsoft.com>
…12676)" (microsoft#13903) ### Description <!-- Describe your changes. --> As title. This pr is missing an un-updated index.android.gradle, which causing an unstable e2e unit test run for React Native CI. Revert the changes for now. ### 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. --> To unblock Ort React Native CI pipeline failure.
### Description <!-- Describe your changes. --> -Add support for loading model from buffer on iOS -Update OnnxruntimeModuleTest to use updated loadModelFromBuffer Based on #12676 ### 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. --> Issue: #12500 --------- Co-authored-by: rachguo <rachguo@rachguos-Mini.attlocal.net> Co-authored-by: rachguo <rachguo@rachguos-Mac-mini.local>
### Description <!-- Describe your changes. --> -Add support for loading model from buffer on iOS -Update OnnxruntimeModuleTest to use updated loadModelFromBuffer Based on microsoft#12676 ### 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. --> Issue: microsoft#12500 --------- Co-authored-by: rachguo <rachguo@rachguos-Mini.attlocal.net> Co-authored-by: rachguo <rachguo@rachguos-Mac-mini.local>
**Description**: [js/React Native] Add android implementation for creating session from buffer. microsoft#12500 Co-authored-by: Rachel Guo <guorachel@microsoft.com>
…12676)" (microsoft#13903) ### Description <!-- Describe your changes. --> As title. This pr is missing an un-updated index.android.gradle, which causing an unstable e2e unit test run for React Native CI. Revert the changes for now. ### 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. --> To unblock Ort React Native CI pipeline failure.
### Description <!-- Describe your changes. --> -Add support for loading model from buffer on iOS -Update OnnxruntimeModuleTest to use updated loadModelFromBuffer Based on microsoft#12676 ### 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. --> Issue: microsoft#12500 --------- Co-authored-by: rachguo <rachguo@rachguos-Mini.attlocal.net> Co-authored-by: rachguo <rachguo@rachguos-Mac-mini.local>
Description: [js/React Native] Add android implementation for creating session from buffer. #12500