react-native-executorch
Version:
An easy way to run AI models in react native with ExecuTorch
40 lines (39 loc) • 1.25 kB
JavaScript
import { BaseModule } from '../BaseModule';
import { ETError, getError } from '../../Error';
import { _ETModule } from '../../native/RnExecutorchModules';
import { getTypeIdentifier } from '../../types/common';
export class ExecutorchModule extends BaseModule {
static module = new _ETModule();
static async forward(input, shape) {
if (!Array.isArray(input)) {
input = [input];
}
let inputTypeIdentifiers = [];
let modelInputs = [];
for (let idx = 0; idx < input.length; idx++) {
let currentInputTypeIdentifier = getTypeIdentifier(input[idx]);
if (currentInputTypeIdentifier === -1) {
throw new Error(getError(ETError.InvalidArgument));
}
inputTypeIdentifiers.push(currentInputTypeIdentifier);
modelInputs.push([...input[idx]]);
}
try {
return await this.module.forward(modelInputs, shape, inputTypeIdentifiers);
} catch (e) {
throw new Error(getError(e));
}
}
static async loadMethod(methodName) {
try {
await this.module.loadMethod(methodName);
} catch (e) {
throw new Error(getError(e));
}
}
static async loadForward() {
await this.loadMethod('forward');
}
}
//# sourceMappingURL=ExecutorchModule.js.map
;