Skip to main content
Version: 0.4.x

ExecuTorchModule

TypeScript API implementation of the useExecutorchModule hook.

Reference

import {
ExecutorchModule,
STYLE_TRANSFER_CANDY,
} from 'react-native-executorch';

// Creating the input array
const shape = [1, 3, 640, 640];
const input = new Float32Array(1 * 3 * 640 * 640);

// Loading the model
await ExecutorchModule.load(STYLE_TRANSFER_CANDY);

// Running the model
const output = await ExecutorchModule.forward(input, shape);

Methods

MethodTypeDescription
load(modelSource: ResourceSource): Promise<void>Loads the model, where modelSource is a string that specifies the location of the model binary.
forward(input: ETInput[] | ETInput, shape: number[][]): Promise<unknown>Executes the model's forward pass, where input is a JavaScript typed array or an array of them and shape is an array of arrays of integers representing input Tensors' shapes. The output is a Tensor - raw result of inference.
loadMethod(methodName: string): Promise<void>Loads resources specific to methodName into memory before execution.
loadForward(): Promise<void>Loads resources specific to forward method into memory before execution. Uses loadMethod under the hood.
onDownloadProgress(callback: (downloadProgress: number) => void): voidSubscribe to the download progress event.

Type definitions

type ResourceSource = string | number | object;

export type ETInput =
| Int8Array
| Int32Array
| BigInt64Array
| Float32Array
| Float64Array;

Loading the model

To load the model, use the load method. It accepts the modelSource which is a string that specifies the location of the model binary. For more information, take a look at loading models page. This method returns a promise, which can resolve to an error or void.

Running the model

To run the model use the forward method. It accepts two arguments: input and shape. The input can be a single JavaScript typed array or an array of them. The shape is an array of arrays of integers, where each inner array represents the shape of a corresponding input tensor. There's no need to explicitly define the input type, as it will automatically be inferred from the typed array you pass to forward method. Outputs from the model, such as classification probabilities, are returned in raw format.

Loading methods

Loads resources specific to methodName into memory before execution.

Loading forward

Loads resources specific to forward method into memory before execution. Uses loadMethod under the hood.

info

This code assumes that you have handled preprocessing of the input image (scaling, normalization) and postprocessing of the output (interpreting the raw output data) according to the model's requirements. Make sure to adjust these parts depending on your specific data and model outputs.