Skip to content

[js/rn] support load model from buffer on Android#12676

Merged
YUNQIUGUO merged 13 commits intomainfrom
fs-eire/js-rn-support-android-load-model-from-buffer
Nov 30, 2022
Merged

[js/rn] support load model from buffer on Android#12676
YUNQIUGUO merged 13 commits intomainfrom
fs-eire/js-rn-support-android-load-model-from-buffer

Conversation

@fs-eire
Copy link
Copy Markdown
Contributor

@fs-eire fs-eire commented Aug 23, 2022

Description: [js/React Native] Add android implementation for creating session from buffer. #12500

public WritableMap loadModel(String uri, InputStream modelStream, ReadableMap options) throws Exception {
OrtSession ortSession = null;

if (!sessionMap.containsKey(uri)) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we have tests at the react native level that can be updated to validate this path?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no, but we can add the test case.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

https://www.callstack.com/blog/testing-react-native-with-the-new-jest-part-2-redux-snapshots-for-your-actions-and-reducers

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>;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

* @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 {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@@ -46,6 +47,13 @@ public class OnnxruntimeModule extends ReactContextBaseJavaModule {
private static OrtEnvironment ortEnvironment = OrtEnvironment.getEnvironment();
private static Map<String, OrtSession> sessionMap = new HashMap<>();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we ever clean up entries in sessionMap?

@YUNQIUGUO YUNQIUGUO merged commit 77c97b6 into main Nov 30, 2022
@YUNQIUGUO YUNQIUGUO deleted the fs-eire/js-rn-support-android-load-model-from-buffer branch November 30, 2022 18:55
YUNQIUGUO added a commit that referenced this pull request Dec 8, 2022
YUNQIUGUO added a commit that referenced this pull request Dec 9, 2022
…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.
baijumeswani pushed a commit that referenced this pull request Dec 13, 2022
…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.
simon-moo pushed a commit to simon-moo/onnxruntime that referenced this pull request Dec 26, 2022
**Description**: [js/React Native] Add android implementation for
creating session from buffer. microsoft#12500

Co-authored-by: Rachel Guo <guorachel@microsoft.com>
simon-moo pushed a commit to simon-moo/onnxruntime that referenced this pull request Dec 26, 2022
…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.
YUNQIUGUO added a commit that referenced this pull request Apr 29, 2023
### 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>
ShukantPal pushed a commit to ShukantPal/onnxruntime that referenced this pull request May 7, 2023
### 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>
siweic0 pushed a commit to siweic0/onnxruntime-web that referenced this pull request May 9, 2024
**Description**: [js/React Native] Add android implementation for
creating session from buffer. microsoft#12500

Co-authored-by: Rachel Guo <guorachel@microsoft.com>
siweic0 pushed a commit to siweic0/onnxruntime-web that referenced this pull request May 9, 2024
…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.
siweic0 pushed a commit to siweic0/onnxruntime-web that referenced this pull request May 9, 2024
### 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>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants