2021-05-11 17:34:40 +00:00
|
|
|
// Copyright (c) Microsoft Corporation. All rights reserved.
|
|
|
|
|
// Licensed under the MIT License.
|
|
|
|
|
|
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
2024-08-14 23:51:22 +00:00
|
|
|
import type { InferenceSession } from 'onnxruntime-common';
|
|
|
|
|
import { NativeModules } from 'react-native';
|
2021-05-11 17:34:40 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* model loading information
|
|
|
|
|
*/
|
|
|
|
|
interface ModelLoadInfo {
|
|
|
|
|
/**
|
|
|
|
|
* Key for an instance of InferenceSession, which is passed to run() function as parameter.
|
|
|
|
|
*/
|
|
|
|
|
readonly key: string;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get input names of the loaded model.
|
|
|
|
|
*/
|
|
|
|
|
readonly inputNames: string[];
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Get output names of the loaded model.
|
|
|
|
|
*/
|
|
|
|
|
readonly outputNames: string[];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2023-06-16 09:37:02 +00:00
|
|
|
* JSIBlob is a blob object that exchange ArrayBuffer by OnnxruntimeJSIHelper.
|
|
|
|
|
*/
|
|
|
|
|
export type JSIBlob = {
|
2024-08-14 23:51:22 +00:00
|
|
|
blobId: string;
|
|
|
|
|
offset: number;
|
|
|
|
|
size: number;
|
2023-06-16 09:37:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Tensor type for react native, which doesn't allow ArrayBuffer in native bridge, so data will be stored as JSIBlob.
|
2021-05-11 17:34:40 +00:00
|
|
|
*/
|
|
|
|
|
interface EncodedTensor {
|
|
|
|
|
/**
|
|
|
|
|
* the dimensions of the tensor.
|
|
|
|
|
*/
|
|
|
|
|
readonly dims: readonly number[];
|
|
|
|
|
/**
|
|
|
|
|
* the data type of the tensor.
|
|
|
|
|
*/
|
|
|
|
|
readonly type: string;
|
|
|
|
|
/**
|
2023-06-16 09:37:02 +00:00
|
|
|
* the JSIBlob object of the buffer data of the tensor.
|
|
|
|
|
* if data is string array, it won't be stored as JSIBlob.
|
2021-05-11 17:34:40 +00:00
|
|
|
*/
|
2024-08-14 23:51:22 +00:00
|
|
|
readonly data: JSIBlob | string[];
|
2021-05-11 17:34:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Binding exports a simple synchronized inference session object wrap.
|
|
|
|
|
*/
|
|
|
|
|
export declare namespace Binding {
|
|
|
|
|
type ModelLoadInfoType = ModelLoadInfo;
|
|
|
|
|
type EncodedTensorType = EncodedTensor;
|
|
|
|
|
|
|
|
|
|
type SessionOptions = InferenceSession.SessionOptions;
|
|
|
|
|
type RunOptions = InferenceSession.RunOptions;
|
|
|
|
|
|
2024-08-14 23:51:22 +00:00
|
|
|
type FeedsType = { [name: string]: EncodedTensor };
|
2021-05-11 17:34:40 +00:00
|
|
|
|
|
|
|
|
// SessionHanlder FetchesType is different from native module's one.
|
|
|
|
|
// It's because Java API doesn't support preallocated output values.
|
|
|
|
|
type FetchesType = string[];
|
|
|
|
|
|
2024-08-14 23:51:22 +00:00
|
|
|
type ReturnType = { [name: string]: EncodedTensor };
|
2021-05-11 17:34:40 +00:00
|
|
|
|
|
|
|
|
interface InferenceSession {
|
|
|
|
|
loadModel(modelPath: string, options: SessionOptions): Promise<ModelLoadInfoType>;
|
2023-06-16 09:37:02 +00:00
|
|
|
loadModelFromBlob?(blob: JSIBlob, options: SessionOptions): Promise<ModelLoadInfoType>;
|
2023-06-08 23:17:33 +00:00
|
|
|
dispose(key: string): Promise<void>;
|
2021-05-11 17:34:40 +00:00
|
|
|
run(key: string, feeds: FeedsType, fetches: FetchesType, options: RunOptions): Promise<ReturnType>;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// export native binding
|
2024-08-14 23:51:22 +00:00
|
|
|
const { Onnxruntime, OnnxruntimeJSIHelper } = NativeModules;
|
2021-05-11 17:34:40 +00:00
|
|
|
export const binding = Onnxruntime as Binding.InferenceSession;
|
2023-06-16 09:37:02 +00:00
|
|
|
|
|
|
|
|
// install JSI helper global functions
|
|
|
|
|
OnnxruntimeJSIHelper.install();
|
|
|
|
|
|
|
|
|
|
declare global {
|
|
|
|
|
// eslint-disable-next-line no-var
|
2024-08-14 23:51:22 +00:00
|
|
|
var jsiOnnxruntimeStoreArrayBuffer: ((buffer: ArrayBuffer) => JSIBlob) | undefined;
|
2023-06-16 09:37:02 +00:00
|
|
|
// eslint-disable-next-line no-var
|
2024-08-14 23:51:22 +00:00
|
|
|
var jsiOnnxruntimeResolveArrayBuffer: ((blob: JSIBlob) => ArrayBuffer) | undefined;
|
2023-06-16 09:37:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
export const jsiHelper = {
|
2024-08-14 23:51:22 +00:00
|
|
|
storeArrayBuffer:
|
|
|
|
|
globalThis.jsiOnnxruntimeStoreArrayBuffer ||
|
|
|
|
|
(() => {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'jsiOnnxruntimeStoreArrayBuffer is not found, ' +
|
|
|
|
|
'please make sure OnnxruntimeJSIHelper installation is successful.',
|
|
|
|
|
);
|
|
|
|
|
}),
|
|
|
|
|
resolveArrayBuffer:
|
|
|
|
|
globalThis.jsiOnnxruntimeResolveArrayBuffer ||
|
|
|
|
|
(() => {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'jsiOnnxruntimeResolveArrayBuffer is not found, ' +
|
|
|
|
|
'please make sure OnnxruntimeJSIHelper installation is successful.',
|
|
|
|
|
);
|
|
|
|
|
}),
|
2023-06-16 09:37:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Remove global functions after installation
|
|
|
|
|
{
|
|
|
|
|
delete globalThis.jsiOnnxruntimeStoreArrayBuffer;
|
|
|
|
|
delete globalThis.jsiOnnxruntimeResolveArrayBuffer;
|
|
|
|
|
}
|