From 6f6cda387f2619b1d64b887f3a307c46290bc222 Mon Sep 17 00:00:00 2001 From: jakmro Date: Tue, 21 Jan 2025 12:42:45 +0100 Subject: [PATCH 1/9] Create file structure --- docs/docs/hookless-api/ClassificationModule.md | 4 ++++ docs/docs/hookless-api/ExecutorchModule.md | 4 ++++ docs/docs/hookless-api/LLMModule.md | 4 ++++ docs/docs/hookless-api/ObjectDetectionModule.md | 4 ++++ docs/docs/hookless-api/StyleTransferModule.md | 4 ++++ docs/docs/hookless-api/_category_.json | 7 +++++++ 6 files changed, 27 insertions(+) create mode 100644 docs/docs/hookless-api/ClassificationModule.md create mode 100644 docs/docs/hookless-api/ExecutorchModule.md create mode 100644 docs/docs/hookless-api/LLMModule.md create mode 100644 docs/docs/hookless-api/ObjectDetectionModule.md create mode 100644 docs/docs/hookless-api/StyleTransferModule.md create mode 100644 docs/docs/hookless-api/_category_.json diff --git a/docs/docs/hookless-api/ClassificationModule.md b/docs/docs/hookless-api/ClassificationModule.md new file mode 100644 index 00000000..4252d10f --- /dev/null +++ b/docs/docs/hookless-api/ClassificationModule.md @@ -0,0 +1,4 @@ +--- +title: ClassificationModule +sidebar_position: 1 +--- diff --git a/docs/docs/hookless-api/ExecutorchModule.md b/docs/docs/hookless-api/ExecutorchModule.md new file mode 100644 index 00000000..4fe5be64 --- /dev/null +++ b/docs/docs/hookless-api/ExecutorchModule.md @@ -0,0 +1,4 @@ +--- +title: ExecuTorchModule +sidebar_position: 5 +--- diff --git a/docs/docs/hookless-api/LLMModule.md b/docs/docs/hookless-api/LLMModule.md new file mode 100644 index 00000000..d969fdb4 --- /dev/null +++ b/docs/docs/hookless-api/LLMModule.md @@ -0,0 +1,4 @@ +--- +title: LLMModule +sidebar_position: 4 +--- diff --git a/docs/docs/hookless-api/ObjectDetectionModule.md b/docs/docs/hookless-api/ObjectDetectionModule.md new file mode 100644 index 00000000..6225e7e9 --- /dev/null +++ b/docs/docs/hookless-api/ObjectDetectionModule.md @@ -0,0 +1,4 @@ +--- +title: ObjectDetectionModule +sidebar_position: 2 +--- diff --git a/docs/docs/hookless-api/StyleTransferModule.md b/docs/docs/hookless-api/StyleTransferModule.md new file mode 100644 index 00000000..1f22424f --- /dev/null +++ b/docs/docs/hookless-api/StyleTransferModule.md @@ -0,0 +1,4 @@ +--- +title: StyleTransferModule +sidebar_position: 3 +--- diff --git a/docs/docs/hookless-api/_category_.json b/docs/docs/hookless-api/_category_.json new file mode 100644 index 00000000..6c0a8908 --- /dev/null +++ b/docs/docs/hookless-api/_category_.json @@ -0,0 +1,7 @@ +{ + "label": "Hookless API", + "position": 5, + "link": { + "type": "generated-index" + } +} From a731d59981e1d1ab28d2acfb8bd9638e9a8c1428 Mon Sep 17 00:00:00 2001 From: jakmro Date: Tue, 21 Jan 2025 13:52:43 +0100 Subject: [PATCH 2/9] Add ClassificationModule docs --- .../docs/hookless-api/ClassificationModule.md | 65 +++++++++++++++++++ 1 file changed, 65 insertions(+) diff --git a/docs/docs/hookless-api/ClassificationModule.md b/docs/docs/hookless-api/ClassificationModule.md index 4252d10f..b777f792 100644 --- a/docs/docs/hookless-api/ClassificationModule.md +++ b/docs/docs/hookless-api/ClassificationModule.md @@ -2,3 +2,68 @@ title: ClassificationModule sidebar_position: 1 --- + +Hookless implementation of the [useClassification](../computer-vision/useClassification.mdx) hook. + +## Reference + +```typescript +import { + ClassificationModule, + EFFICIENTNET_V2_S, +} from 'react-native-executorch'; + +const imageUri = 'path/to/image.png'; + +// Loading the model +await ClassificationModule.load(EFFICIENTNET_V2_S); + +// Running the model +const classesWithProbabilities = await ClassificationModule.forward(imageUri); +``` + +### Methods + +| Method | Parameters | Returns | Description | +| --------- | ---------------------------------------------- | ----------------------------------------- | -------------------------------------------------------------------------------------------------------- | +| `load` | modelSource: string | number | `Promise` | Loads the model, where `modelSource` is a string that specifies the location of the model binary. | +| `forward` | `input: string` | `Promise<{ [category: string]: number }>` | Executes the model's forward pass, where `input` can be a fetchable resource or a Base64-encoded string. | + +## 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](../fundamentals/loading-models.md) page. This function returns a promise, which can resolve to an error or void. + +## Running the model + +To run the model, you can use the `forward` method. It accepts one argument, which is the image. The image can be a remote URL, a local file URI, or a base64-encoded image. The function returns a promise, which can resolve either to an error or an object containing categories with their probabilities. + +## Example + +```typescript +import { + ClassificationModule, + EFFICIENTNET_V2_S, +} from 'react-native-executorch'; + +const imageUri = 'path/to/image.png'; + +try { + // Loading the model + await ClassificationModule.load(EFFICIENTNET_V2_S); +} catch (e) { + console.error(e); +} + +try { + // Running the model + const classesWithProbabilities = await ClassificationModule.forward(imageUri); + + // Getting three most likely classes + const topThreeClasses = Object.entries(classesWithProbabilities) + .sort(([, a], [, b]) => b - a) + .slice(0, 3) + .map(([label, score]) => ({ label, score })); +} catch (e) { + console.error(e); +} +``` From 737187ea14a39ec74be0101e45d73ad9c944cf6b Mon Sep 17 00:00:00 2001 From: jakmro Date: Tue, 21 Jan 2025 14:09:00 +0100 Subject: [PATCH 3/9] Add ObjectDetection docs --- .../hookless-api/ObjectDetectionModule.md | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/docs/docs/hookless-api/ObjectDetectionModule.md b/docs/docs/hookless-api/ObjectDetectionModule.md index 6225e7e9..1a20a4c5 100644 --- a/docs/docs/hookless-api/ObjectDetectionModule.md +++ b/docs/docs/hookless-api/ObjectDetectionModule.md @@ -2,3 +2,62 @@ title: ObjectDetectionModule sidebar_position: 2 --- + +Hookless implementation of the [useObjectDetection](../computer-vision/useObjectDetection.mdx) hook. + +## Reference + +```typescript +import { + ObjectDetectionModule, + SSDLITE_320_MOBILENET_V3_LARGE, +} from 'react-native-executorch'; + +const imageUri = 'path/to/image.png'; + +// Loading the model +await ObjectDetectionModule.load(SSDLITE_320_MOBILENET_V3_LARGE); + +// Running the model +const detections = await ObjectDetectionModule.forward(imageUri); +``` + +### Methods + +| Method | Parameters | Returns | Description | +| --------- | ---------------------------------------------- | ----------------------------------------- | -------------------------------------------------------------------------------------------------------- | +| `load` | modelSource: string | number | `Promise` | Loads the model, where `modelSource` is a string that specifies the location of the model binary. | +| `forward` | `input: string` | `Promise<{ [category: string]: number }>` | Executes the model's forward pass, where `input` can be a fetchable resource or a Base64-encoded string. | + +## 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](../fundamentals/loading-models.md) page. This function returns a promise, which can resolve to an error or void. + +## Running the model + +To run the model, you can use the `forward` method. It accepts one argument, which is the image. The image can be a remote URL, a local file URI, or a base64-encoded image. The function returns an array of `Detection` objects. Each object contains coordinates of the bounding box, the label of the detected object, and the confidence score. For more information, please refer to the reference or type definitions. + +## Example + +```typescript +import { + ObjectDetectionModule, + SSDLITE_320_MOBILENET_V3_LARGE, +} from 'react-native-executorch'; + +const imageUri = 'path/to/image.png'; + +try { + // Loading the model + await ObjectDetectionModule.load(SSDLITE_320_MOBILENET_V3_LARGE); +} catch (e) { + console.error(e); +} + +try { + // Running the model + const detections = await ObjectDetectionModule.forward(imageUri); +} catch (e) { + console.error(e); +} +``` From 607b49a35f95ee9fc13d8e4244d518cd65327c88 Mon Sep 17 00:00:00 2001 From: jakmro Date: Sat, 25 Jan 2025 19:42:12 +0100 Subject: [PATCH 4/9] Add StyleTransferModule, ExecutorchModule, LLMModule docs --- .../computer-vision/useObjectDetection.mdx | 45 +++++----- docs/docs/fundamentals/getting-started.mdx | 8 +- .../docs/hookless-api/ClassificationModule.md | 50 +++-------- docs/docs/hookless-api/ExecutorchModule.md | 66 ++++++++++++++ docs/docs/hookless-api/LLMModule.md | 85 +++++++++++++++++++ .../hookless-api/ObjectDetectionModule.md | 53 ++++++------ docs/docs/hookless-api/StyleTransferModule.md | 43 ++++++++++ docs/docs/llms/exporting-llama.mdx | 27 ++++-- docs/docs/llms/running-llms.md | 18 ++-- docs/docs/module-api/executorch-bindings.md | 1 - 10 files changed, 292 insertions(+), 104 deletions(-) diff --git a/docs/docs/computer-vision/useObjectDetection.mdx b/docs/docs/computer-vision/useObjectDetection.mdx index 0e400764..6fbae5f1 100644 --- a/docs/docs/computer-vision/useObjectDetection.mdx +++ b/docs/docs/computer-vision/useObjectDetection.mdx @@ -11,6 +11,7 @@ It is recommended to use models provided by us, which are available at our [Hugg ::: ## Reference + ```jsx import { useObjectDetection, SSDLITE_320_MOBILENET_V3_LARGE } from 'react-native-executorch'; @@ -53,34 +54,35 @@ interface ObjectDetectionModule { forward: (input: string) => Promise; } ``` + ### Arguments `modelSource` -A string that specifies the path to the model file. You can download the model from our [HuggingFace repository](https://huggingface.co/software-mansion/react-native-executorch-ssdlite320-mobilenet-v3-large/tree/main). +A string that specifies the path to the model file. You can download the model from our [HuggingFace repository](https://huggingface.co/software-mansion/react-native-executorch-ssdlite320-mobilenet-v3-large/tree/main). For more information on that topic, you can check out the [Loading models](https://docs.swmansion.com/react-native-executorch/fundamentals/loading-models) page. ### Returns The hook returns an object with the following properties: - -| **Field** | **Type** | **Description** | -|-----------------------|---------------------------------------|------------------------------------------------------------------------------------------------------------------| -| `forward` | `(input: string) => Promise` | A function that accepts an image (url, b64) and returns an array of `Detection` objects. | -| `error` | string | null | Contains the error message if the model loading failed. | -| `isGenerating` | `boolean` | Indicates whether the model is currently processing an inference. | -| `isReady` | `boolean` | Indicates whether the model has successfully loaded and is ready for inference. | - +| Field | Type | Description | +| -------------- | ----------------------------------------- | ---------------------------------------------------------------------------------------- | +| `forward` | `(input: string) => Promise` | A function that accepts an image (url, b64) and returns an array of `Detection` objects. | +| `error` | string | null | Contains the error message if the model loading failed. | +| `isGenerating` | `boolean` | Indicates whether the model is currently processing an inference. | +| `isReady` | `boolean` | Indicates whether the model has successfully loaded and is ready for inference. | ## Running the model To run the model, you can use the `forward` method. It accepts one argument, which is the image. The image can be a remote URL, a local file URI, or a base64-encoded image. The function returns an array of `Detection` objects. Each object contains coordinates of the bounding box, the label of the detected object, and the confidence score. For more information, please refer to the reference or type definitions. ## Detection object + The detection object is specified as follows: + ```typescript interface Bbox { x1: number; @@ -95,14 +97,17 @@ interface Detection { score: number; } ``` + The `bbox` property contains information about the bounding box of detected objects. It is represented as two points: one at the bottom-left corner of the bounding box (`x1`, `y1`) and the other at the top-right corner (`x2`, `y2`). The `label` property contains the name of the detected object, which corresponds to one of the `CocoLabels`. The `score` represents the confidence score of the detected object. - - ## Example + ```tsx -import { useObjectDetection, SSDLITE_320_MOBILENET_V3_LARGE } from 'react-native-executorch'; +import { + useObjectDetection, + SSDLITE_320_MOBILENET_V3_LARGE, +} from 'react-native-executorch'; function App() { const ssdlite = useObjectDetection({ @@ -110,18 +115,18 @@ function App() { }); const runModel = async () => { - const detections = await ssdlite.forward("https://url-to-image.jpg"); + const detections = await ssdlite.forward('https://url-to-image.jpg'); for (const detection of detections) { - console.log("Bounding box: ", detection.bbox); - console.log("Bounding label: ", detection.label); - console.log("Bounding score: ", detection.score); + console.log('Bounding box: ', detection.bbox); + console.log('Bounding label: ', detection.label); + console.log('Bounding score: ', detection.score); } - } + }; } ``` ## Supported models -| Model | Number of classes | Class list | -| --------------------------------------------------------------------------------------------------------------- | ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| [SSDLite320 MobileNetV3 Large](https://pytorch.org/vision/main/models/generated/torchvision.models.detection.ssdlite320_mobilenet_v3_large.html#torchvision.models.detection.SSDLite320_MobileNet_V3_Large_Weights) | 91 | [COCO](https://github.com/software-mansion/react-native-executorch/blob/69802ee1ca161d9df00def1dabe014d36341cfa9/src/types/object_detection.ts#L14) | \ No newline at end of file +| Model | Number of classes | Class list | +| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | +| [SSDLite320 MobileNetV3 Large](https://pytorch.org/vision/main/models/generated/torchvision.models.detection.ssdlite320_mobilenet_v3_large.html#torchvision.models.detection.SSDLite320_MobileNet_V3_Large_Weights) | 91 | [COCO](https://github.com/software-mansion/react-native-executorch/blob/69802ee1ca161d9df00def1dabe014d36341cfa9/src/types/object_detection.ts#L14) | diff --git a/docs/docs/fundamentals/getting-started.mdx b/docs/docs/fundamentals/getting-started.mdx index a53be6d1..924bdb37 100644 --- a/docs/docs/fundamentals/getting-started.mdx +++ b/docs/docs/fundamentals/getting-started.mdx @@ -7,12 +7,15 @@ import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; ## What is ExecuTorch? + ExecuTorch is a novel AI framework developed by Meta, designed to streamline deploying PyTorch models on a variety of devices, including mobile phones and microcontrollers. This framework enables exporting models into standalone binaries, allowing them to run locally without requiring API calls. ExecuTorch achieves state-of-the-art performance through optimizations and delegates such as CoreML and XNNPack. It provides a seamless export process with robust debugging options, making it easier to resolve issues if they arise. ## React Native ExecuTorch + React Native ExecuTorch is our way of bringing ExecuTorch into the React Native world. Our API is built to be simple, declarative, and efficient. Plus, we’ll provide a set of pre-exported models for common use cases, so you won’t have to worry about handling exports yourself. With just a few lines of JavaScript, you’ll be able to run AI models (even LLMs 👀) right on your device—keeping user data private and saving on cloud costs. ## Installation + Installation is pretty straightforward, just use your favorite package manager. @@ -54,12 +57,15 @@ Because we are using ExecuTorch under the hood, you won't be able to build iOS a ::: Running the app with the library: + ```bash yarn run expo: -d ``` ## Good reads -If you want to dive deeper into ExecuTorch or our previous work with the framework, we highly encourage you to check out the following resources: + +If you want to dive deeper into ExecuTorch or our previous work with the framework, we highly encourage you to check out the following resources: + - [ExecuTorch docs](https://pytorch.org/executorch/stable/index.html) - [Native code for iOS](https://medium.com/swmansion/bringing-native-ai-to-your-mobile-apps-with-executorch-part-i-ios-f1562a4556e8?source=user_profile_page---------0-------------250189c98ccf---------------) - [Native code for Android](https://medium.com/swmansion/bringing-native-ai-to-your-mobile-apps-with-executorch-part-ii-android-29431b6b9f7f?source=user_profile_page---------2-------------b8e3a5cb1c63---------------) diff --git a/docs/docs/hookless-api/ClassificationModule.md b/docs/docs/hookless-api/ClassificationModule.md index b777f792..d9ae07bd 100644 --- a/docs/docs/hookless-api/ClassificationModule.md +++ b/docs/docs/hookless-api/ClassificationModule.md @@ -24,46 +24,24 @@ const classesWithProbabilities = await ClassificationModule.forward(imageUri); ### Methods -| Method | Parameters | Returns | Description | -| --------- | ---------------------------------------------- | ----------------------------------------- | -------------------------------------------------------------------------------------------------------- | -| `load` | modelSource: string | number | `Promise` | Loads the model, where `modelSource` is a string that specifies the location of the model binary. | -| `forward` | `input: string` | `Promise<{ [category: string]: number }>` | Executes the model's forward pass, where `input` can be a fetchable resource or a Base64-encoded string. | +| Method | Type | Description | +| --------- | ---------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- | +| `load` | `(modelSource: ResourceSource): Promise` | Loads the model, where `modelSource` is a string that specifies the location of the model binary. | +| `forward` | `(input: string): Promise<{ [category: string]: number }>` | Executes the model's forward pass, where `input` can be a fetchable resource or a Base64-encoded string. | -## 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](../fundamentals/loading-models.md) page. This function returns a promise, which can resolve to an error or void. - -## Running the model - -To run the model, you can use the `forward` method. It accepts one argument, which is the image. The image can be a remote URL, a local file URI, or a base64-encoded image. The function returns a promise, which can resolve either to an error or an object containing categories with their probabilities. - -## Example +
+Type definitions ```typescript -import { - ClassificationModule, - EFFICIENTNET_V2_S, -} from 'react-native-executorch'; +type ResourceSource = string | number; +``` -const imageUri = 'path/to/image.png'; +
-try { - // Loading the model - await ClassificationModule.load(EFFICIENTNET_V2_S); -} catch (e) { - console.error(e); -} +## Loading the model -try { - // Running the model - const classesWithProbabilities = await ClassificationModule.forward(imageUri); +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](../fundamentals/loading-models.md) page. This method returns a promise, which can resolve to an error or void. - // Getting three most likely classes - const topThreeClasses = Object.entries(classesWithProbabilities) - .sort(([, a], [, b]) => b - a) - .slice(0, 3) - .map(([label, score]) => ({ label, score })); -} catch (e) { - console.error(e); -} -``` +## Running the model + +To run the model, you can use the `forward` method. It accepts one argument, which is the image. The image can be a remote URL, a local file URI, or a base64-encoded image. The method returns a promise, which can resolve either to an error or an object containing categories with their probabilities. diff --git a/docs/docs/hookless-api/ExecutorchModule.md b/docs/docs/hookless-api/ExecutorchModule.md index 4fe5be64..c8a37b82 100644 --- a/docs/docs/hookless-api/ExecutorchModule.md +++ b/docs/docs/hookless-api/ExecutorchModule.md @@ -2,3 +2,69 @@ title: ExecuTorchModule sidebar_position: 5 --- + +Hookless implementation of the [useExecutorchModule](../module-api/executorch-bindings.md) hook. + +## Reference + +```typescript +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 + +| Method | Type | Description | +| ------------- | ------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `load` | `(modelSource: ResourceSource): Promise` | Loads the model, where `modelSource` is a string that specifies the location of the model binary. | +| `forward` | `(input: ETInput, shape: number[]): Promise` | Executes the model's forward pass, where `input` is a JavaScript typed array and `shape` is an array of integers representing input Tensor shape. The output is a Tensor - raw result of inference. | +| `loadMethod` | `(methodName: string): Promise` | Loads resources specific to `methodName` into memory before execution. | +| `loadForward` | `(): Promise` | Loads resources specific to `forward` method into memory before execution. Uses `loadMethod` under the hood. | + +
+Type definitions + +```typescript +type ResourceSource = string | number; + +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](../fundamentals/loading-models.md) 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` is a JavaScript typed array, and `shape` is an array of integers representing the input tensor shape. 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. +::: diff --git a/docs/docs/hookless-api/LLMModule.md b/docs/docs/hookless-api/LLMModule.md index d969fdb4..98de5243 100644 --- a/docs/docs/hookless-api/LLMModule.md +++ b/docs/docs/hookless-api/LLMModule.md @@ -2,3 +2,88 @@ title: LLMModule sidebar_position: 4 --- + +Hookless implementation of the [useLLM](../llms/running-llms.md) hook. + +## Reference + +```typescript +import { + LLMModule, + LLAMA3_2_1B_QLORA, + LLAMA3_2_1B_TOKENIZER, +} from 'react-native-executorch'; + +// Listening for download progress +LLMModule.onDownloadProgress((progress) => { + console.log(progress); +}); + +// Loading the model +await LLMModule.load(LLAMA3_2_1B_QLORA, LLAMA3_2_1B_TOKENIZER); + +// Listening for token +LLMModule.onToken((token) => { + console.log(token); +}); + +// Running the model +LLMModule.generate('Hello, World!'); + +// Interrupting the model +LLMModule.interrupt(); + +// Deleting the model from memory +LLMModule.delete(); +``` + +### Methods + +| Method | Type | Description | +| -------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------ | +| `load` | `(modelSource: ResourceSource, tokenizerSource: ResourceSource, systemPrompt?: string, contextWindowLength?: number): Promise` | Loads the model. Checkout the [loading the model](#loading-the-model) section for details. | +| `onDownloadProgress` | `(callback: (data: number) => void): any` | Subscribe to the download progress event. | +| `generate` | `(input: string): Promise` | Method to start generating a response with the given input string. | +| `onToken` | (callback: (data: string | undefined) => void): any | Subscribe to the token generation event. | +| `interrupt` | `(): void` | Method to interrupt the current inference | +| `delete` | `(): void` | Method to delete the model from memory. | + +
+Type definitions + +```typescript +type ResourceSource = string | number; +``` + +
+ +## Loading the model + +To load the model, use the `load` method. It accepts: + +- `modelSource` - A string that specifies the location of the model binary. For more information, take a look at [loading models](../fundamentals/loading-models.md) page. +- `tokenizerSource` - URL to the binary file which contains the tokenizer +- `systemPrompt` - Often used to tell the model what is its purpose, for example - "Be a helpful translator" +- `contextWindowLength` - The number of messages from the current conversation that the model will use to generate a response. The higher the number, the more context the model will have. Keep in mind that using larger context windows will result in longer inference time and higher memory usage. + +This method returns a promise, which can resolve to an error or void. + +## Listening for download progress + +To subscribe to the download progress event, you can use the `onDownloadProgress` method. It accepts a callback function that will be called whenever the download progress changes. + +## Running the model + +To run the model, you can use the `generate` method. It accepts one argument, which is the input string. The method returns a promise, which can resolve to an error or void. + +## Listening for token + +To subscribe to the token event, you can use the `onToken` method. It accepts a callback function that will be called whenever a token is generated. + +## Interrupting the model + +In order to interrupt the model, you can use the `interrupt` method. + +## Deleting the model from memory + +To delete the model from memory, you can use the `delete` method. diff --git a/docs/docs/hookless-api/ObjectDetectionModule.md b/docs/docs/hookless-api/ObjectDetectionModule.md index 1a20a4c5..8e3d0804 100644 --- a/docs/docs/hookless-api/ObjectDetectionModule.md +++ b/docs/docs/hookless-api/ObjectDetectionModule.md @@ -24,40 +24,37 @@ const detections = await ObjectDetectionModule.forward(imageUri); ### Methods -| Method | Parameters | Returns | Description | -| --------- | ---------------------------------------------- | ----------------------------------------- | -------------------------------------------------------------------------------------------------------- | -| `load` | modelSource: string | number | `Promise` | Loads the model, where `modelSource` is a string that specifies the location of the model binary. | -| `forward` | `input: string` | `Promise<{ [category: string]: number }>` | Executes the model's forward pass, where `input` can be a fetchable resource or a Base64-encoded string. | +| Method | Type | Description | +| --------- | ---------------------------------------------- | -------------------------------------------------------------------------------------------------------- | +| `load` | `(modelSource: ResourceSource): Promise` | Loads the model, where `modelSource` is a string that specifies the location of the model binary. | +| `forward` | `(input: string): Promise` | Executes the model's forward pass, where `input` can be a fetchable resource or a Base64-encoded string. | -## Loading the model +
+Type definitions -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](../fundamentals/loading-models.md) page. This function returns a promise, which can resolve to an error or void. +```typescript +type ResourceSource = string | number; -## Running the model +interface Bbox { + x1: number; + x2: number; + y1: number; + y2: number; +} -To run the model, you can use the `forward` method. It accepts one argument, which is the image. The image can be a remote URL, a local file URI, or a base64-encoded image. The function returns an array of `Detection` objects. Each object contains coordinates of the bounding box, the label of the detected object, and the confidence score. For more information, please refer to the reference or type definitions. +interface Detection { + bbox: Bbox; + label: keyof typeof CocoLabel; + score: number; +} +``` -## Example +
-```typescript -import { - ObjectDetectionModule, - SSDLITE_320_MOBILENET_V3_LARGE, -} from 'react-native-executorch'; +## Loading the model -const imageUri = 'path/to/image.png'; +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](../fundamentals/loading-models.md) page. This method returns a promise, which can resolve to an error or void. -try { - // Loading the model - await ObjectDetectionModule.load(SSDLITE_320_MOBILENET_V3_LARGE); -} catch (e) { - console.error(e); -} +## Running the model -try { - // Running the model - const detections = await ObjectDetectionModule.forward(imageUri); -} catch (e) { - console.error(e); -} -``` +To run the model, you can use the `forward` method. It accepts one argument, which is the image. The image can be a remote URL, a local file URI, or a base64-encoded image. The method returns a promise, which can resolve either to an error or an array of `Detection` objects. Each object contains coordinates of the bounding box, the label of the detected object, and the confidence score. diff --git a/docs/docs/hookless-api/StyleTransferModule.md b/docs/docs/hookless-api/StyleTransferModule.md index 1f22424f..340da1d5 100644 --- a/docs/docs/hookless-api/StyleTransferModule.md +++ b/docs/docs/hookless-api/StyleTransferModule.md @@ -2,3 +2,46 @@ title: StyleTransferModule sidebar_position: 3 --- + +Hookless implementation of the [useStyleTransfer](../computer-vision/useStyleTransfer.mdx) hook. + +## Reference + +```typescript +import { + StyleTransferModule, + STYLE_TRANSFER_CANDY, +} from 'react-native-executorch'; + +const imageUri = 'path/to/image.png'; + +// Loading the model +await StyleTransferModule.load(STYLE_TRANSFER_CANDY); + +// Running the model +const generatedImageUrl = await StyleTransferModule.forward(imageUri); +``` + +### Methods + +| Method | Type | Description | +| --------- | ---------------------------------------------- | -------------------------------------------------------------------------------------------------------- | +| `load` | `(modelSource: ResourceSource): Promise` | Loads the model, where `modelSource` is a string that specifies the location of the model binary. | +| `forward` | `(input: string): Promise` | Executes the model's forward pass, where `input` can be a fetchable resource or a Base64-encoded string. | + +
+Type definitions + +```typescript +type ResourceSource = string | number; +``` + +
+ +## 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](../fundamentals/loading-models.md) page. This method returns a promise, which can resolve to an error or void. + +## Running the model + +To run the model, you can use the `forward` method. It accepts one argument, which is the image. The image can be a remote URL, a local file URI, or a base64-encoded image. The method returns a promise, which can resolve either to an error or a URL to generated image. diff --git a/docs/docs/llms/exporting-llama.mdx b/docs/docs/llms/exporting-llama.mdx index 5acbb5d2..28b3ceb9 100644 --- a/docs/docs/llms/exporting-llama.mdx +++ b/docs/docs/llms/exporting-llama.mdx @@ -3,32 +3,41 @@ title: Exporting Llama sidebar_position: 2 --- -In order to make the process of export as simple as possible for you, we created a script that runs a Docker container and exports the model. +In order to make the process of export as simple as possible for you, we created a script that runs a Docker container and exports the model. ## Steps to export Llama + ### 1. Create an account -Get a [HuggingFace](https://huggingface.co/) account. This will allow you to download needed files. You can also use the [official Llama website](https://www.llama.com/llama-downloads/). + +Get a [HuggingFace](https://huggingface.co/) account. This will allow you to download needed files. You can also use the [official Llama website](https://www.llama.com/llama-downloads/). ### 2. Select a model + Pick the model that suits your needs. Before you download it, you'll need to accept a license. For best performance, we recommend using Spin-Quant or QLoRA versions of the model: - - [Llama 3.2 3B](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct/tree/main/original) - - [Llama 3.2 1B](https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct/tree/main/original) - - [Llama 3.2 3B Spin-Quant](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct-SpinQuant_INT4_EO8/tree/main) - - [Llama 3.2 1B Spin-Quant](https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct-SpinQuant_INT4_EO8/tree/main) - - [Llama 3.2 3B QLoRA](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct-QLORA_INT4_EO8/tree/main) - - [Llama 3.2 1B QLoRA](https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct-QLORA_INT4_EO8/tree/main) + +- [Llama 3.2 3B](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct/tree/main/original) +- [Llama 3.2 1B](https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct/tree/main/original) +- [Llama 3.2 3B Spin-Quant](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct-SpinQuant_INT4_EO8/tree/main) +- [Llama 3.2 1B Spin-Quant](https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct-SpinQuant_INT4_EO8/tree/main) +- [Llama 3.2 3B QLoRA](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct-QLORA_INT4_EO8/tree/main) +- [Llama 3.2 1B QLoRA](https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct-QLORA_INT4_EO8/tree/main) ### 3. Download files + Download the `consolidated.00.pth`, `params.json` and `tokenizer.model` files. If you can't see them, make sure to check the `original` directory. ### 4. Rename the tokenizer file + Rename the `tokenizer.model` file to `tokenizer.bin` as required by the library: + ```bash mv tokenizer.model tokenizer.bin ``` ### 5. Run the export script -Navigate to the `llama_export` directory and run the following command: + +Navigate to the `llama_export` directory and run the following command: + ```bash ./build_llama_binary.sh --model-path /path/to/consolidated.00.pth --params-path /path/to/params.json ``` diff --git a/docs/docs/llms/running-llms.md b/docs/docs/llms/running-llms.md index 93a3e4e7..5c1a2e54 100644 --- a/docs/docs/llms/running-llms.md +++ b/docs/docs/llms/running-llms.md @@ -45,15 +45,15 @@ Given computational constraints, our architecture is designed to support only on ### Returns -| Field | Type | Description | -| ------------------- | ---------------------------------- | --------------------------------------------------------------------------------------------------------------- | -| `generate` | `(input: string) => Promise` | Function to start generating a response with the given input string. | -| `response` | `string` | State of the generated response. This field is updated with each token generated by the model | -| `error` | string | null | Contains the error message if the model failed to load | -| `isGenerating` | `boolean` | Indicates whether the model is currently generating a response | -| `interrupt` | `() => void` | Function to interrupt the current inference | -| `isReady` | `boolean` | Indicates whether the model is ready | -| `downloadProgress` | `number` | Represents the download progress as a value between 0 and 1, indicating the extent of the model file retrieval. | +| Field | Type | Description | +| ------------------ | ---------------------------------- | --------------------------------------------------------------------------------------------------------------- | +| `generate` | `(input: string) => Promise` | Function to start generating a response with the given input string. | +| `response` | `string` | State of the generated response. This field is updated with each token generated by the model | +| `error` | string | null | Contains the error message if the model failed to load | +| `isGenerating` | `boolean` | Indicates whether the model is currently generating a response | +| `interrupt` | `() => void` | Function to interrupt the current inference | +| `isReady` | `boolean` | Indicates whether the model is ready | +| `downloadProgress` | `number` | Represents the download progress as a value between 0 and 1, indicating the extent of the model file retrieval. | ## Sending a message diff --git a/docs/docs/module-api/executorch-bindings.md b/docs/docs/module-api/executorch-bindings.md index c7c6ecdb..41470086 100644 --- a/docs/docs/module-api/executorch-bindings.md +++ b/docs/docs/module-api/executorch-bindings.md @@ -76,7 +76,6 @@ const executorchModule = useExecutorchModule({ }); ``` - ## Setting up input parameters To prepare the input for the model, define the shape of the input tensor. This shape depends on the model's requirements. For the `STYLE_TRANSFER_CANDY` model, we need a tensor of shape `[1, 3, 640, 640]`, corresponding to a batch size of 1, 3 color channels (RGB), and dimensions of 640x640 pixels. From 2a98ffa56cddeda9679c89fd39c8a56eeba393e3 Mon Sep 17 00:00:00 2001 From: Jakub Chmura <92989966+chmjkb@users.noreply.github.com> Date: Thu, 30 Jan 2025 10:53:51 +0100 Subject: [PATCH 5/9] docs: Add a quickstart guide for Llama (#91) ## Description As discussed internally, adding a quickstart with running LLMs to the README - #17 ### Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [x] Documentation update (improves or adds clarity to existing documentation) ### Tested on - [x] iOS - [x] Android ### Testing instructions ### Screenshots ### Related issues ### Checklist - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have updated the documentation accordingly - [x] My changes generate no new warnings ### Additional notes --- README.md | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/README.md b/README.md index 005f0740..7505cfa6 100644 --- a/README.md +++ b/README.md @@ -15,6 +15,52 @@ To run any AI model in ExecuTorch, you need to export it to a `.pte` format. If Take a look at how our library can help build you your React Native AI features in our docs: https://docs.swmansion.com/react-native-executorch + +# 🦙 **Quickstart - Running Llama** + +**Get started with AI-powered text generation in 3 easy steps!** + +### 1️⃣ **Installation** +```bash +# Install the package +yarn add react-native-executorch +cd ios && pod install && cd .. +``` + +--- + +### 2️⃣ **Setup & Initialization** +Add this to your component file: +```tsx +import { + LLAMA3_2_1B_QLORA, + LLAMA3_2_3B_TOKENIZER, + useLLM +} from 'react-native-executorch'; + +function MyComponent() { + // Initialize the model 🚀 + const llama = useLLM({ + modelSource: LLAMA3_2_1B_QLORA, + tokenizerSource: LLAMA3_2_1B_TOKENIZER + }); + // ... rest of your component +} +``` + +--- + +### 3️⃣ **Run the model!** +```tsx +const handleGenerate = async () => { + const prompt = "The meaning of life is"; + + // Generate text based on your desired prompt + const response = await llama.generate(prompt); + console.log("Llama says:", response); +}; +``` + ## Minimal supported versions The minimal supported version is 17.0 for iOS and Android 13. From c2eee13aa19b730f6393d6b339ae8335d1d14c28 Mon Sep 17 00:00:00 2001 From: Jakub Mroz <115979017+jakmro@users.noreply.github.com> Date: Mon, 3 Feb 2025 20:27:26 +0100 Subject: [PATCH 6/9] docs: Benchmarks (#92) ## Description Add models Benchmarks (memory usage, inference time, model size) ### Type of change - [ ] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [x] Documentation update (improves or adds clarity to existing documentation) ### Checklist - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have updated the documentation accordingly - [x] My changes generate no new warnings --- docs/docs/benchmarks/_category_.json | 7 +++ docs/docs/benchmarks/inference-time.md | 42 ++++++++++++++ docs/docs/benchmarks/memory-usage.md | 36 ++++++++++++ docs/docs/benchmarks/model-size.md | 36 ++++++++++++ ...lassification.mdx => useClassification.md} | 24 ++++++++ ...ectDetection.mdx => useObjectDetection.md} | 24 ++++++++ ...eStyleTransfer.mdx => useStyleTransfer.md} | 33 +++++++++++ ...getting-started.mdx => getting-started.md} | 10 +++- ...exporting-llama.mdx => exporting-llama.md} | 27 ++++++--- docs/docs/llms/running-llms.md | 55 ++++++++++++++++--- docs/docs/module-api/executorch-bindings.md | 1 - 11 files changed, 274 insertions(+), 21 deletions(-) create mode 100644 docs/docs/benchmarks/_category_.json create mode 100644 docs/docs/benchmarks/inference-time.md create mode 100644 docs/docs/benchmarks/memory-usage.md create mode 100644 docs/docs/benchmarks/model-size.md rename docs/docs/computer-vision/{useClassification.mdx => useClassification.md} (79%) rename docs/docs/computer-vision/{useObjectDetection.mdx => useObjectDetection.md} (82%) rename docs/docs/computer-vision/{useStyleTransfer.mdx => useStyleTransfer.md} (61%) rename docs/docs/fundamentals/{getting-started.mdx => getting-started.md} (94%) rename docs/docs/llms/{exporting-llama.mdx => exporting-llama.md} (66%) diff --git a/docs/docs/benchmarks/_category_.json b/docs/docs/benchmarks/_category_.json new file mode 100644 index 00000000..8e10f7a3 --- /dev/null +++ b/docs/docs/benchmarks/_category_.json @@ -0,0 +1,7 @@ +{ + "label": "Benchmarks", + "position": 5, + "link": { + "type": "generated-index" + } +} diff --git a/docs/docs/benchmarks/inference-time.md b/docs/docs/benchmarks/inference-time.md new file mode 100644 index 00000000..c1f91a3b --- /dev/null +++ b/docs/docs/benchmarks/inference-time.md @@ -0,0 +1,42 @@ +--- +title: Inference Time +sidebar_position: 3 +--- + +:::warning warning +Times presented in the tables are measured as consecutive runs of the model. Initial run times may be up to 2x longer due to model loading and initialization. +::: + +## Classification + +| Model | iPhone 16 Pro (Core ML) [ms] | iPhone 13 Pro (Core ML) [ms] | iPhone SE 3 (Core ML) [ms] | Samsung Galaxy S24 (XNNPACK) [ms] | OnePlus 12 (XNNPACK) [ms] | +| ----------------- | ---------------------------- | ---------------------------- | -------------------------- | --------------------------------- | ------------------------- | +| EFFICIENTNET_V2_S | 100 | 120 | 130 | 180 | 170 | + +## Object Detection + +| Model | iPhone 16 Pro (XNNPACK) [ms] | iPhone 13 Pro (XNNPACK) [ms] | iPhone SE 3 (XNNPACK) [ms] | Samsung Galaxy S24 (XNNPACK) [ms] | OnePlus 12 (XNNPACK) [ms] | +| ------------------------------ | ---------------------------- | ---------------------------- | -------------------------- | --------------------------------- | ------------------------- | +| SSDLITE_320_MOBILENET_V3_LARGE | 190 | 260 | 280 | 100 | 90 | + +## Style Transfer + +| Model | iPhone 16 Pro (Core ML) [ms] | iPhone 13 Pro (Core ML) [ms] | iPhone SE 3 (Core ML) [ms] | Samsung Galaxy S24 (XNNPACK) [ms] | OnePlus 12 (XNNPACK) [ms] | +| ---------------------------- | ---------------------------- | ---------------------------- | -------------------------- | --------------------------------- | ------------------------- | +| STYLE_TRANSFER_CANDY | 450 | 600 | 750 | 1650 | 1800 | +| STYLE_TRANSFER_MOSAIC | 450 | 600 | 750 | 1650 | 1800 | +| STYLE_TRANSFER_UDNIE | 450 | 600 | 750 | 1650 | 1800 | +| STYLE_TRANSFER_RAIN_PRINCESS | 450 | 600 | 750 | 1650 | 1800 | + +## LLMs + +| Model | iPhone 16 Pro (XNNPACK) [tokens/s] | iPhone 13 Pro (XNNPACK) [tokens/s] | iPhone SE 3 (XNNPACK) [tokens/s] | Samsung Galaxy S24 (XNNPACK) [tokens/s] | OnePlus 12 (XNNPACK) [tokens/s] | +| --------------------- | ---------------------------------- | ---------------------------------- | -------------------------------- | --------------------------------------- | ------------------------------- | +| LLAMA3_2_1B | 16.1 | 11.4 | ❌ | 15.6 | 19.3 | +| LLAMA3_2_1B_SPINQUANT | 40.6 | 16.7 | 16.5 | 40.3 | 48.2 | +| LLAMA3_2_1B_QLORA | 31.8 | 11.4 | 11.2 | 37.3 | 44.4 | +| LLAMA3_2_3B | ❌ | ❌ | ❌ | ❌ | 7.1 | +| LLAMA3_2_3B_SPINQUANT | 17.2 | 8.2 | ❌ | 16.2 | 19.4 | +| LLAMA3_2_3B_QLORA | 14.5 | ❌ | ❌ | 14.8 | 18.1 | + +❌ - Insufficient RAM. diff --git a/docs/docs/benchmarks/memory-usage.md b/docs/docs/benchmarks/memory-usage.md new file mode 100644 index 00000000..868a0884 --- /dev/null +++ b/docs/docs/benchmarks/memory-usage.md @@ -0,0 +1,36 @@ +--- +title: Memory Usage +sidebar_position: 2 +--- + +## Classification + +| Model | Android (XNNPACK) [MB] | iOS (Core ML) [MB] | +| ----------------- | ---------------------- | ------------------ | +| EFFICIENTNET_V2_S | 130 | 85 | + +## Object Detection + +| Model | Android (XNNPACK) [MB] | iOS (XNNPACK) [MB] | +| ------------------------------ | ---------------------- | ------------------ | +| SSDLITE_320_MOBILENET_V3_LARGE | 90 | 90 | + +## Style Transfer + +| Model | Android (XNNPACK) [MB] | iOS (Core ML) [MB] | +| ---------------------------- | ---------------------- | ------------------ | +| STYLE_TRANSFER_CANDY | 950 | 350 | +| STYLE_TRANSFER_MOSAIC | 950 | 350 | +| STYLE_TRANSFER_UDNIE | 950 | 350 | +| STYLE_TRANSFER_RAIN_PRINCESS | 950 | 350 | + +## LLMs + +| Model | Android (XNNPACK) [GB] | iOS (XNNPACK) [GB] | +| --------------------- | ---------------------- | ------------------ | +| LLAMA3_2_1B | 3.2 | 3.1 | +| LLAMA3_2_1B_SPINQUANT | 1.9 | 2 | +| LLAMA3_2_1B_QLORA | 2.2 | 2.5 | +| LLAMA3_2_3B | 7.1 | 7.3 | +| LLAMA3_2_3B_SPINQUANT | 3.7 | 3.8 | +| LLAMA3_2_3B_QLORA | 4 | 4.1 | diff --git a/docs/docs/benchmarks/model-size.md b/docs/docs/benchmarks/model-size.md new file mode 100644 index 00000000..a80f59d4 --- /dev/null +++ b/docs/docs/benchmarks/model-size.md @@ -0,0 +1,36 @@ +--- +title: Model Size +sidebar_position: 1 +--- + +## Classification + +| Model | XNNPACK [MB] | Core ML [MB] | +| ----------------- | ------------ | ------------ | +| EFFICIENTNET_V2_S | 85.6 | 43.9 | + +## Object Detection + +| Model | XNNPACK [MB] | +| ------------------------------ | ------------ | +| SSDLITE_320_MOBILENET_V3_LARGE | 13.9 | + +## Style Transfer + +| Model | XNNPACK [MB] | Core ML [MB] | +| ---------------------------- | ------------ | ------------ | +| STYLE_TRANSFER_CANDY | 6.78 | 5.22 | +| STYLE_TRANSFER_MOSAIC | 6.78 | 5.22 | +| STYLE_TRANSFER_UDNIE | 6.78 | 5.22 | +| STYLE_TRANSFER_RAIN_PRINCESS | 6.78 | 5.22 | + +## LLMs + +| Model | XNNPACK [GB] | +| --------------------- | ------------ | +| LLAMA3_2_1B | 2.47 | +| LLAMA3_2_1B_SPINQUANT | 1.14 | +| LLAMA3_2_1B_QLORA | 1.18 | +| LLAMA3_2_3B | 6.43 | +| LLAMA3_2_3B_SPINQUANT | 2.55 | +| LLAMA3_2_3B_QLORA | 2.65 | diff --git a/docs/docs/computer-vision/useClassification.mdx b/docs/docs/computer-vision/useClassification.md similarity index 79% rename from docs/docs/computer-vision/useClassification.mdx rename to docs/docs/computer-vision/useClassification.md index 1043088b..db33fed1 100644 --- a/docs/docs/computer-vision/useClassification.mdx +++ b/docs/docs/computer-vision/useClassification.md @@ -86,3 +86,27 @@ function App() { | Model | Number of classes | Class list | | --------------------------------------------------------------------------------------------------------------- | ----------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | [efficientnet_v2_s](https://pytorch.org/vision/0.20/models/generated/torchvision.models.efficientnet_v2_s.html) | 1000 | [ImageNet1k_v1](https://github.com/software-mansion/react-native-executorch/blob/main/android/src/main/java/com/swmansion/rnexecutorch/models/classification/Constants.kt) | + +## Benchmarks + +### Model size + +| Model | XNNPACK [MB] | Core ML [MB] | +| ----------------- | ------------ | ------------ | +| EFFICIENTNET_V2_S | 85.6 | 43.9 | + +### Memory usage + +| Model | Android (XNNPACK) [MB] | iOS (Core ML) [MB] | +| ----------------- | ---------------------- | ------------------ | +| EFFICIENTNET_V2_S | 130 | 85 | + +### Inference time + +:::warning warning +Times presented in the tables are measured as consecutive runs of the model. Initial run times may be up to 2x longer due to model loading and initialization. +::: + +| Model | iPhone 16 Pro (Core ML) [ms] | iPhone 13 Pro (Core ML) [ms] | iPhone SE 3 (Core ML) [ms] | Samsung Galaxy S24 (XNNPACK) [ms] | OnePlus 12 (XNNPACK) [ms] | +| ----------------- | ---------------------------- | ---------------------------- | -------------------------- | --------------------------------- | ------------------------- | +| EFFICIENTNET_V2_S | 100 | 120 | 130 | 180 | 170 | diff --git a/docs/docs/computer-vision/useObjectDetection.mdx b/docs/docs/computer-vision/useObjectDetection.md similarity index 82% rename from docs/docs/computer-vision/useObjectDetection.mdx rename to docs/docs/computer-vision/useObjectDetection.md index 5de3da41..a0e30337 100644 --- a/docs/docs/computer-vision/useObjectDetection.mdx +++ b/docs/docs/computer-vision/useObjectDetection.md @@ -124,3 +124,27 @@ function App() { | Model | Number of classes | Class list | | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | | [SSDLite320 MobileNetV3 Large](https://pytorch.org/vision/main/models/generated/torchvision.models.detection.ssdlite320_mobilenet_v3_large.html#torchvision.models.detection.SSDLite320_MobileNet_V3_Large_Weights) | 91 | [COCO](https://github.com/software-mansion/react-native-executorch/blob/69802ee1ca161d9df00def1dabe014d36341cfa9/src/types/object_detection.ts#L14) | + +## Benchmarks + +### Model size + +| Model | XNNPACK [MB] | +| ------------------------------ | ------------ | +| SSDLITE_320_MOBILENET_V3_LARGE | 13.9 | + +### Memory usage + +| Model | Android (XNNPACK) [MB] | iOS (XNNPACK) [MB] | +| ------------------------------ | ---------------------- | ------------------ | +| SSDLITE_320_MOBILENET_V3_LARGE | 90 | 90 | + +### Inference time + +:::warning warning +Times presented in the tables are measured as consecutive runs of the model. Initial run times may be up to 2x longer due to model loading and initialization. +::: + +| Model | iPhone 16 Pro (XNNPACK) [ms] | iPhone 13 Pro (XNNPACK) [ms] | iPhone SE 3 (XNNPACK) [ms] | Samsung Galaxy S24 (XNNPACK) [ms] | OnePlus 12 (XNNPACK) [ms] | +| ------------------------------ | ---------------------------- | ---------------------------- | -------------------------- | --------------------------------- | ------------------------- | +| SSDLITE_320_MOBILENET_V3_LARGE | 190 | 260 | 280 | 100 | 90 | diff --git a/docs/docs/computer-vision/useStyleTransfer.mdx b/docs/docs/computer-vision/useStyleTransfer.md similarity index 61% rename from docs/docs/computer-vision/useStyleTransfer.mdx rename to docs/docs/computer-vision/useStyleTransfer.md index c5a5e3e0..6a8a3461 100644 --- a/docs/docs/computer-vision/useStyleTransfer.mdx +++ b/docs/docs/computer-vision/useStyleTransfer.md @@ -78,3 +78,36 @@ function App(){ - [Mosaic](https://github.com/pytorch/examples/tree/main/fast_neural_style) - [Udnie](https://github.com/pytorch/examples/tree/main/fast_neural_style) - [Rain princess](https://github.com/pytorch/examples/tree/main/fast_neural_style) + +## Benchmarks + +### Model size + +| Model | XNNPACK [MB] | Core ML [MB] | +| ---------------------------- | ------------ | ------------ | +| STYLE_TRANSFER_CANDY | 6.78 | 5.22 | +| STYLE_TRANSFER_MOSAIC | 6.78 | 5.22 | +| STYLE_TRANSFER_UDNIE | 6.78 | 5.22 | +| STYLE_TRANSFER_RAIN_PRINCESS | 6.78 | 5.22 | + +### Memory usage + +| Model | Android (XNNPACK) [MB] | iOS (Core ML) [MB] | +| ---------------------------- | ---------------------- | ------------------ | +| STYLE_TRANSFER_CANDY | 950 | 350 | +| STYLE_TRANSFER_MOSAIC | 950 | 350 | +| STYLE_TRANSFER_UDNIE | 950 | 350 | +| STYLE_TRANSFER_RAIN_PRINCESS | 950 | 350 | + +### Inference time + +:::warning warning +Times presented in the tables are measured as consecutive runs of the model. Initial run times may be up to 2x longer due to model loading and initialization. +::: + +| Model | iPhone 16 Pro (Core ML) [ms] | iPhone 13 Pro (Core ML) [ms] | iPhone SE 3 (Core ML) [ms] | Samsung Galaxy S24 (XNNPACK) [ms] | OnePlus 12 (XNNPACK) [ms] | +| ---------------------------- | ---------------------------- | ---------------------------- | -------------------------- | --------------------------------- | ------------------------- | +| STYLE_TRANSFER_CANDY | 450 | 600 | 750 | 1650 | 1800 | +| STYLE_TRANSFER_MOSAIC | 450 | 600 | 750 | 1650 | 1800 | +| STYLE_TRANSFER_UDNIE | 450 | 600 | 750 | 1650 | 1800 | +| STYLE_TRANSFER_RAIN_PRINCESS | 450 | 600 | 750 | 1650 | 1800 | diff --git a/docs/docs/fundamentals/getting-started.mdx b/docs/docs/fundamentals/getting-started.md similarity index 94% rename from docs/docs/fundamentals/getting-started.mdx rename to docs/docs/fundamentals/getting-started.md index a53be6d1..025b085a 100644 --- a/docs/docs/fundamentals/getting-started.mdx +++ b/docs/docs/fundamentals/getting-started.md @@ -7,12 +7,15 @@ import Tabs from '@theme/Tabs'; import TabItem from '@theme/TabItem'; ## What is ExecuTorch? -ExecuTorch is a novel AI framework developed by Meta, designed to streamline deploying PyTorch models on a variety of devices, including mobile phones and microcontrollers. This framework enables exporting models into standalone binaries, allowing them to run locally without requiring API calls. ExecuTorch achieves state-of-the-art performance through optimizations and delegates such as CoreML and XNNPack. It provides a seamless export process with robust debugging options, making it easier to resolve issues if they arise. + +ExecuTorch is a novel AI framework developed by Meta, designed to streamline deploying PyTorch models on a variety of devices, including mobile phones and microcontrollers. This framework enables exporting models into standalone binaries, allowing them to run locally without requiring API calls. ExecuTorch achieves state-of-the-art performance through optimizations and delegates such as Core ML and XNNPACK. It provides a seamless export process with robust debugging options, making it easier to resolve issues if they arise. ## React Native ExecuTorch + React Native ExecuTorch is our way of bringing ExecuTorch into the React Native world. Our API is built to be simple, declarative, and efficient. Plus, we’ll provide a set of pre-exported models for common use cases, so you won’t have to worry about handling exports yourself. With just a few lines of JavaScript, you’ll be able to run AI models (even LLMs 👀) right on your device—keeping user data private and saving on cloud costs. ## Installation + Installation is pretty straightforward, just use your favorite package manager. @@ -54,12 +57,15 @@ Because we are using ExecuTorch under the hood, you won't be able to build iOS a ::: Running the app with the library: + ```bash yarn run expo: -d ``` ## Good reads -If you want to dive deeper into ExecuTorch or our previous work with the framework, we highly encourage you to check out the following resources: + +If you want to dive deeper into ExecuTorch or our previous work with the framework, we highly encourage you to check out the following resources: + - [ExecuTorch docs](https://pytorch.org/executorch/stable/index.html) - [Native code for iOS](https://medium.com/swmansion/bringing-native-ai-to-your-mobile-apps-with-executorch-part-i-ios-f1562a4556e8?source=user_profile_page---------0-------------250189c98ccf---------------) - [Native code for Android](https://medium.com/swmansion/bringing-native-ai-to-your-mobile-apps-with-executorch-part-ii-android-29431b6b9f7f?source=user_profile_page---------2-------------b8e3a5cb1c63---------------) diff --git a/docs/docs/llms/exporting-llama.mdx b/docs/docs/llms/exporting-llama.md similarity index 66% rename from docs/docs/llms/exporting-llama.mdx rename to docs/docs/llms/exporting-llama.md index 5acbb5d2..28b3ceb9 100644 --- a/docs/docs/llms/exporting-llama.mdx +++ b/docs/docs/llms/exporting-llama.md @@ -3,32 +3,41 @@ title: Exporting Llama sidebar_position: 2 --- -In order to make the process of export as simple as possible for you, we created a script that runs a Docker container and exports the model. +In order to make the process of export as simple as possible for you, we created a script that runs a Docker container and exports the model. ## Steps to export Llama + ### 1. Create an account -Get a [HuggingFace](https://huggingface.co/) account. This will allow you to download needed files. You can also use the [official Llama website](https://www.llama.com/llama-downloads/). + +Get a [HuggingFace](https://huggingface.co/) account. This will allow you to download needed files. You can also use the [official Llama website](https://www.llama.com/llama-downloads/). ### 2. Select a model + Pick the model that suits your needs. Before you download it, you'll need to accept a license. For best performance, we recommend using Spin-Quant or QLoRA versions of the model: - - [Llama 3.2 3B](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct/tree/main/original) - - [Llama 3.2 1B](https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct/tree/main/original) - - [Llama 3.2 3B Spin-Quant](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct-SpinQuant_INT4_EO8/tree/main) - - [Llama 3.2 1B Spin-Quant](https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct-SpinQuant_INT4_EO8/tree/main) - - [Llama 3.2 3B QLoRA](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct-QLORA_INT4_EO8/tree/main) - - [Llama 3.2 1B QLoRA](https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct-QLORA_INT4_EO8/tree/main) + +- [Llama 3.2 3B](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct/tree/main/original) +- [Llama 3.2 1B](https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct/tree/main/original) +- [Llama 3.2 3B Spin-Quant](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct-SpinQuant_INT4_EO8/tree/main) +- [Llama 3.2 1B Spin-Quant](https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct-SpinQuant_INT4_EO8/tree/main) +- [Llama 3.2 3B QLoRA](https://huggingface.co/meta-llama/Llama-3.2-3B-Instruct-QLORA_INT4_EO8/tree/main) +- [Llama 3.2 1B QLoRA](https://huggingface.co/meta-llama/Llama-3.2-1B-Instruct-QLORA_INT4_EO8/tree/main) ### 3. Download files + Download the `consolidated.00.pth`, `params.json` and `tokenizer.model` files. If you can't see them, make sure to check the `original` directory. ### 4. Rename the tokenizer file + Rename the `tokenizer.model` file to `tokenizer.bin` as required by the library: + ```bash mv tokenizer.model tokenizer.bin ``` ### 5. Run the export script -Navigate to the `llama_export` directory and run the following command: + +Navigate to the `llama_export` directory and run the following command: + ```bash ./build_llama_binary.sh --model-path /path/to/consolidated.00.pth --params-path /path/to/params.json ``` diff --git a/docs/docs/llms/running-llms.md b/docs/docs/llms/running-llms.md index a00bad23..36016f2c 100644 --- a/docs/docs/llms/running-llms.md +++ b/docs/docs/llms/running-llms.md @@ -45,15 +45,15 @@ Given computational constraints, our architecture is designed to support only on ### Returns -| Field | Type | Description | -| ------------------- | ---------------------------------- | --------------------------------------------------------------------------------------------------------------- | -| `generate` | `(input: string) => Promise` | Function to start generating a response with the given input string. | -| `response` | `string` | State of the generated response. This field is updated with each token generated by the model | -| `error` | string | null | Contains the error message if the model failed to load | -| `isGenerating` | `boolean` | Indicates whether the model is currently generating a response | -| `interrupt` | `() => void` | Function to interrupt the current inference | -| `isReady` | `boolean` | Indicates whether the model is ready | -| `downloadProgress` | `number` | Represents the download progress as a value between 0 and 1, indicating the extent of the model file retrieval. | +| Field | Type | Description | +| ------------------ | ---------------------------------- | --------------------------------------------------------------------------------------------------------------- | +| `generate` | `(input: string) => Promise` | Function to start generating a response with the given input string. | +| `response` | `string` | State of the generated response. This field is updated with each token generated by the model | +| `error` | string | null | Contains the error message if the model failed to load | +| `isGenerating` | `boolean` | Indicates whether the model is currently generating a response | +| `interrupt` | `() => void` | Function to interrupt the current inference | +| `isReady` | `boolean` | Indicates whether the model is ready | +| `downloadProgress` | `number` | Represents the download progress as a value between 0 and 1, indicating the extent of the model file retrieval. | ## Sending a message @@ -88,3 +88,40 @@ Behind the scenes, tokens are generated one by one, and the response property is Sometimes, you might want to stop the model while it’s generating. To do this, you can use `interrupt()`, which will halt the model and append the current response to its internal conversation state. There are also cases when you need to check if tokens are being generated, such as to conditionally render a stop button. We’ve made this easy with the `isTokenBeingGenerated` property. + +## Benchmarks + +### Model size + +| Model | XNNPACK [GB] | +| --------------------- | ------------ | +| LLAMA3_2_1B | 2.47 | +| LLAMA3_2_1B_SPINQUANT | 1.14 | +| LLAMA3_2_1B_QLORA | 1.18 | +| LLAMA3_2_3B | 6.43 | +| LLAMA3_2_3B_SPINQUANT | 2.55 | +| LLAMA3_2_3B_QLORA | 2.65 | + +### Memory usage + +| Model | Android (XNNPACK) [GB] | iOS (XNNPACK) [GB] | +| --------------------- | ---------------------- | ------------------ | +| LLAMA3_2_1B | 3.2 | 3.1 | +| LLAMA3_2_1B_SPINQUANT | 1.9 | 2 | +| LLAMA3_2_1B_QLORA | 2.2 | 2.5 | +| LLAMA3_2_3B | 7.1 | 7.3 | +| LLAMA3_2_3B_SPINQUANT | 3.7 | 3.8 | +| LLAMA3_2_3B_QLORA | 4 | 4.1 | + +### Inference time + +| Model | iPhone 16 Pro (XNNPACK) [tokens/s] | iPhone 13 Pro (XNNPACK) [tokens/s] | iPhone SE 3 (XNNPACK) [tokens/s] | Samsung Galaxy S24 (XNNPACK) [tokens/s] | OnePlus 12 (XNNPACK) [tokens/s] | +| --------------------- | ---------------------------------- | ---------------------------------- | -------------------------------- | --------------------------------------- | ------------------------------- | +| LLAMA3_2_1B | 16.1 | 11.4 | ❌ | 15.6 | 19.3 | +| LLAMA3_2_1B_SPINQUANT | 40.6 | 16.7 | 16.5 | 40.3 | 48.2 | +| LLAMA3_2_1B_QLORA | 31.8 | 11.4 | 11.2 | 37.3 | 44.4 | +| LLAMA3_2_3B | ❌ | ❌ | ❌ | ❌ | 7.1 | +| LLAMA3_2_3B_SPINQUANT | 17.2 | 8.2 | ❌ | 16.2 | 19.4 | +| LLAMA3_2_3B_QLORA | 14.5 | ❌ | ❌ | 14.8 | 18.1 | + +❌ - Insufficient RAM. diff --git a/docs/docs/module-api/executorch-bindings.md b/docs/docs/module-api/executorch-bindings.md index c7c6ecdb..41470086 100644 --- a/docs/docs/module-api/executorch-bindings.md +++ b/docs/docs/module-api/executorch-bindings.md @@ -76,7 +76,6 @@ const executorchModule = useExecutorchModule({ }); ``` - ## Setting up input parameters To prepare the input for the model, define the shape of the input tensor. This shape depends on the model's requirements. For the `STYLE_TRANSFER_CANDY` model, we need a tensor of shape `[1, 3, 640, 640]`, corresponding to a batch size of 1, 3 color channels (RGB), and dimensions of 640x640 pixels. From 96a6e4086682e27f1eacd2b41222404cfd8ea459 Mon Sep 17 00:00:00 2001 From: Jakub Mroz <115979017+jakmro@users.noreply.github.com> Date: Tue, 4 Feb 2025 10:30:29 +0100 Subject: [PATCH 7/9] fix: Correct link in ExecuTorch bindings documentation (#93) ## Description Correct link in ExecuTorch bindings documentation ### Type of change - [x] Bug fix (non-breaking change which fixes an issue) - [ ] New feature (non-breaking change which adds functionality) - [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected) - [x] Documentation update (improves or adds clarity to existing documentation) ### Checklist - [x] I have performed a self-review of my code - [x] I have commented my code, particularly in hard-to-understand areas - [x] I have updated the documentation accordingly - [x] My changes generate no new warnings --- docs/docs/module-api/executorch-bindings.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/docs/module-api/executorch-bindings.md b/docs/docs/module-api/executorch-bindings.md index 41470086..2cfbfa5c 100644 --- a/docs/docs/module-api/executorch-bindings.md +++ b/docs/docs/module-api/executorch-bindings.md @@ -58,7 +58,7 @@ To run model with ExecuTorch Bindings it's essential to specify the shape of the ## End to end example -This example demonstrates the integration and usage of the ExecuTorch bindings with a [style transfer model](../computer-vision/useStyleTransfer.mdx). Specifically, we'll be using the `STYLE_TRANSFER_CANDY` model, which applies artistic style transfer to an input image. +This example demonstrates the integration and usage of the ExecuTorch bindings with a [style transfer model](../computer-vision/useStyleTransfer.md). Specifically, we'll be using the `STYLE_TRANSFER_CANDY` model, which applies artistic style transfer to an input image. ## Importing the Module and loading the model From c6f141f55c7a0659c833168dc13da70d2b70ea9c Mon Sep 17 00:00:00 2001 From: jakmro Date: Mon, 17 Feb 2025 15:39:03 +0100 Subject: [PATCH 8/9] Add downloadProgress field documentation --- docs/docs/computer-vision/useClassification.md | 13 +++++++------ docs/docs/computer-vision/useObjectDetection.md | 13 +++++++------ docs/docs/computer-vision/useStyleTransfer.md | 13 +++++++------ docs/docs/module-api/executorch-bindings.md | 17 +++++++++-------- 4 files changed, 30 insertions(+), 26 deletions(-) diff --git a/docs/docs/computer-vision/useClassification.md b/docs/docs/computer-vision/useClassification.md index db33fed1..98945be6 100644 --- a/docs/docs/computer-vision/useClassification.md +++ b/docs/docs/computer-vision/useClassification.md @@ -38,12 +38,13 @@ A string that specifies the location of the model binary. For more information, ### Returns -| Field | Type | Description | -| -------------- | ------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------- | -| `forward` | `(input: string) => Promise<{ [category: string]: number }>` | Executes the model's forward pass, where `input` can be a fetchable resource or a Base64-encoded string. | -| `error` | string | null | Contains the error message if the model failed to load. | -| `isGenerating` | `boolean` | Indicates whether the model is currently processing an inference. | -| `isReady` | `boolean` | Indicates whether the model has successfully loaded and is ready for inference. | +| Field | Type | Description | +| ------------------ | ------------------------------------------------------------ | -------------------------------------------------------------------------------------------------------- | +| `forward` | `(input: string) => Promise<{ [category: string]: number }>` | Executes the model's forward pass, where `input` can be a fetchable resource or a Base64-encoded string. | +| `error` | string | null | Contains the error message if the model failed to load. | +| `isGenerating` | `boolean` | Indicates whether the model is currently processing an inference. | +| `isReady` | `boolean` | Indicates whether the model has successfully loaded and is ready for inference. | +| `downloadProgress` | `number` | Represents the download progress as a value between 0 and 1 | ## Running the model diff --git a/docs/docs/computer-vision/useObjectDetection.md b/docs/docs/computer-vision/useObjectDetection.md index a0e30337..dae1aeee 100644 --- a/docs/docs/computer-vision/useObjectDetection.md +++ b/docs/docs/computer-vision/useObjectDetection.md @@ -61,12 +61,13 @@ For more information on that topic, you can check out the [Loading models](https The hook returns an object with the following properties: -| Field | Type | Description | -| -------------- | ----------------------------------------- | ---------------------------------------------------------------------------------------- | -| `forward` | `(input: string) => Promise` | A function that accepts an image (url, b64) and returns an array of `Detection` objects. | -| `error` | string | null | Contains the error message if the model loading failed. | -| `isGenerating` | `boolean` | Indicates whether the model is currently processing an inference. | -| `isReady` | `boolean` | Indicates whether the model has successfully loaded and is ready for inference. | +| Field | Type | Description | +| ------------------ | ----------------------------------------- | ---------------------------------------------------------------------------------------- | +| `forward` | `(input: string) => Promise` | A function that accepts an image (url, b64) and returns an array of `Detection` objects. | +| `error` | string | null | Contains the error message if the model loading failed. | +| `isGenerating` | `boolean` | Indicates whether the model is currently processing an inference. | +| `isReady` | `boolean` | Indicates whether the model has successfully loaded and is ready for inference. | +| `downloadProgress` | `number` | Represents the download progress as a value between 0 and 1 | ## Running the model diff --git a/docs/docs/computer-vision/useStyleTransfer.md b/docs/docs/computer-vision/useStyleTransfer.md index 6a8a3461..5b24df13 100644 --- a/docs/docs/computer-vision/useStyleTransfer.md +++ b/docs/docs/computer-vision/useStyleTransfer.md @@ -37,12 +37,13 @@ A string that specifies the location of the model binary. For more information, ### Returns -| Field | Type | Description | -| -------------- | ------------------------------------ | -------------------------------------------------------------------------------------------------------- | -| `forward` | `(input: string) => Promise` | Executes the model's forward pass, where `input` can be a fetchable resource or a Base64-encoded string. | -| `error` | string | null | Contains the error message if the model failed to load. | -| `isGenerating` | `boolean` | Indicates whether the model is currently processing an inference. | -| `isReady` | `boolean` | Indicates whether the model has successfully loaded and is ready for inference. | +| Field | Type | Description | +| ------------------ | ------------------------------------ | -------------------------------------------------------------------------------------------------------- | +| `forward` | `(input: string) => Promise` | Executes the model's forward pass, where `input` can be a fetchable resource or a Base64-encoded string. | +| `error` | string | null | Contains the error message if the model failed to load. | +| `isGenerating` | `boolean` | Indicates whether the model is currently processing an inference. | +| `isReady` | `boolean` | Indicates whether the model has successfully loaded and is ready for inference. | +| `downloadProgress` | `number` | Represents the download progress as a value between 0 and 1 | ## Running the model diff --git a/docs/docs/module-api/executorch-bindings.md b/docs/docs/module-api/executorch-bindings.md index 2cfbfa5c..7e82208c 100644 --- a/docs/docs/module-api/executorch-bindings.md +++ b/docs/docs/module-api/executorch-bindings.md @@ -29,14 +29,15 @@ The `modelSource` parameter expects a location string pointing to the model bina ### Returns -| Field | Type | Description | -| -------------- | ---------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `error` | string | null | Contains the error message if the model failed to load. | -| `isGenerating` | `boolean` | Indicates whether the model is currently processing an inference. | -| `isReady` | `boolean` | Indicates whether the model has successfully loaded and is ready for inference. | -| `loadMethod` | `(methodName: string) => Promise` | Loads resources specific to `methodName` into memory before execution. | -| `loadForward` | `() => Promise` | Loads resources specific to `forward` method into memory before execution. Uses `loadMethod` under the hood. | -| `forward` | `(input: ETInput, shape: number[]) => Promise` | Executes the model's forward pass, where `input` is a Javascript typed array and `shape` is an array of integers representing input Tensor shape. The output is a Tensor - raw result of inference. | +| Field | Type | Description | +| ------------------ | ---------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `error` | string | null | Contains the error message if the model failed to load. | +| `isGenerating` | `boolean` | Indicates whether the model is currently processing an inference. | +| `isReady` | `boolean` | Indicates whether the model has successfully loaded and is ready for inference. | +| `loadMethod` | `(methodName: string) => Promise` | Loads resources specific to `methodName` into memory before execution. | +| `loadForward` | `() => Promise` | Loads resources specific to `forward` method into memory before execution. Uses `loadMethod` under the hood. | +| `forward` | `(input: ETInput, shape: number[]) => Promise` | Executes the model's forward pass, where `input` is a Javascript typed array and `shape` is an array of integers representing input Tensor shape. The output is a Tensor - raw result of inference. | +| `downloadProgress` | `number` | Represents the download progress as a value between 0 and 1 | ## ETInput From fe106efdfb730c9a96dd7d2ec1636af1b4931ba1 Mon Sep 17 00:00:00 2001 From: jakmro Date: Mon, 17 Feb 2025 16:03:27 +0100 Subject: [PATCH 9/9] Add onDownloadProgress method documentation --- docs/docs/hookless-api/ClassificationModule.md | 9 +++++---- docs/docs/hookless-api/ExecutorchModule.md | 13 +++++++------ docs/docs/hookless-api/LLMModule.md | 2 +- docs/docs/hookless-api/ObjectDetectionModule.md | 9 +++++---- docs/docs/hookless-api/StyleTransferModule.md | 9 +++++---- 5 files changed, 23 insertions(+), 19 deletions(-) diff --git a/docs/docs/hookless-api/ClassificationModule.md b/docs/docs/hookless-api/ClassificationModule.md index d9ae07bd..732971db 100644 --- a/docs/docs/hookless-api/ClassificationModule.md +++ b/docs/docs/hookless-api/ClassificationModule.md @@ -24,10 +24,11 @@ const classesWithProbabilities = await ClassificationModule.forward(imageUri); ### Methods -| Method | Type | Description | -| --------- | ---------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- | -| `load` | `(modelSource: ResourceSource): Promise` | Loads the model, where `modelSource` is a string that specifies the location of the model binary. | -| `forward` | `(input: string): Promise<{ [category: string]: number }>` | Executes the model's forward pass, where `input` can be a fetchable resource or a Base64-encoded string. | +| Method | Type | Description | +| -------------------- | ---------------------------------------------------------- | -------------------------------------------------------------------------------------------------------- | +| `load` | `(modelSource: ResourceSource): Promise` | Loads the model, where `modelSource` is a string that specifies the location of the model binary. | +| `forward` | `(input: string): Promise<{ [category: string]: number }>` | Executes the model's forward pass, where `input` can be a fetchable resource or a Base64-encoded string. | +| `onDownloadProgress` | `(callback: (downloadProgress: number) => void): any` | Subscribe to the download progress event. |
Type definitions diff --git a/docs/docs/hookless-api/ExecutorchModule.md b/docs/docs/hookless-api/ExecutorchModule.md index c8a37b82..db3c3cd7 100644 --- a/docs/docs/hookless-api/ExecutorchModule.md +++ b/docs/docs/hookless-api/ExecutorchModule.md @@ -26,12 +26,13 @@ const output = await ExecutorchModule.forward(input, shape); ### Methods -| Method | Type | Description | -| ------------- | ------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `load` | `(modelSource: ResourceSource): Promise` | Loads the model, where `modelSource` is a string that specifies the location of the model binary. | -| `forward` | `(input: ETInput, shape: number[]): Promise` | Executes the model's forward pass, where `input` is a JavaScript typed array and `shape` is an array of integers representing input Tensor shape. The output is a Tensor - raw result of inference. | -| `loadMethod` | `(methodName: string): Promise` | Loads resources specific to `methodName` into memory before execution. | -| `loadForward` | `(): Promise` | Loads resources specific to `forward` method into memory before execution. Uses `loadMethod` under the hood. | +| Method | Type | Description | +| -------------------- | ------------------------------------------------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `load` | `(modelSource: ResourceSource): Promise` | Loads the model, where `modelSource` is a string that specifies the location of the model binary. | +| `forward` | `(input: ETInput, shape: number[]): Promise` | Executes the model's forward pass, where `input` is a JavaScript typed array and `shape` is an array of integers representing input Tensor shape. The output is a Tensor - raw result of inference. | +| `loadMethod` | `(methodName: string): Promise` | Loads resources specific to `methodName` into memory before execution. | +| `loadForward` | `(): Promise` | Loads resources specific to `forward` method into memory before execution. Uses `loadMethod` under the hood. | +| `onDownloadProgress` | `(callback: (downloadProgress: number) => void): any` | Subscribe to the download progress event. |
Type definitions diff --git a/docs/docs/hookless-api/LLMModule.md b/docs/docs/hookless-api/LLMModule.md index 98de5243..005a2f7c 100644 --- a/docs/docs/hookless-api/LLMModule.md +++ b/docs/docs/hookless-api/LLMModule.md @@ -42,7 +42,7 @@ LLMModule.delete(); | Method | Type | Description | | -------------------- | ------------------------------------------------------------------------------------------------------------------------------------ | ------------------------------------------------------------------------------------------ | | `load` | `(modelSource: ResourceSource, tokenizerSource: ResourceSource, systemPrompt?: string, contextWindowLength?: number): Promise` | Loads the model. Checkout the [loading the model](#loading-the-model) section for details. | -| `onDownloadProgress` | `(callback: (data: number) => void): any` | Subscribe to the download progress event. | +| `onDownloadProgress` | `(callback: (downloadProgress: number) => void): any` | Subscribe to the download progress event. | | `generate` | `(input: string): Promise` | Method to start generating a response with the given input string. | | `onToken` | (callback: (data: string | undefined) => void): any | Subscribe to the token generation event. | | `interrupt` | `(): void` | Method to interrupt the current inference | diff --git a/docs/docs/hookless-api/ObjectDetectionModule.md b/docs/docs/hookless-api/ObjectDetectionModule.md index 8e3d0804..eaaf644d 100644 --- a/docs/docs/hookless-api/ObjectDetectionModule.md +++ b/docs/docs/hookless-api/ObjectDetectionModule.md @@ -24,10 +24,11 @@ const detections = await ObjectDetectionModule.forward(imageUri); ### Methods -| Method | Type | Description | -| --------- | ---------------------------------------------- | -------------------------------------------------------------------------------------------------------- | -| `load` | `(modelSource: ResourceSource): Promise` | Loads the model, where `modelSource` is a string that specifies the location of the model binary. | -| `forward` | `(input: string): Promise` | Executes the model's forward pass, where `input` can be a fetchable resource or a Base64-encoded string. | +| Method | Type | Description | +| -------------------- | ----------------------------------------------------- | -------------------------------------------------------------------------------------------------------- | +| `load` | `(modelSource: ResourceSource): Promise` | Loads the model, where `modelSource` is a string that specifies the location of the model binary. | +| `forward` | `(input: string): Promise` | Executes the model's forward pass, where `input` can be a fetchable resource or a Base64-encoded string. | +| `onDownloadProgress` | `(callback: (downloadProgress: number) => void): any` | Subscribe to the download progress event. |
Type definitions diff --git a/docs/docs/hookless-api/StyleTransferModule.md b/docs/docs/hookless-api/StyleTransferModule.md index 340da1d5..d1d22023 100644 --- a/docs/docs/hookless-api/StyleTransferModule.md +++ b/docs/docs/hookless-api/StyleTransferModule.md @@ -24,10 +24,11 @@ const generatedImageUrl = await StyleTransferModule.forward(imageUri); ### Methods -| Method | Type | Description | -| --------- | ---------------------------------------------- | -------------------------------------------------------------------------------------------------------- | -| `load` | `(modelSource: ResourceSource): Promise` | Loads the model, where `modelSource` is a string that specifies the location of the model binary. | -| `forward` | `(input: string): Promise` | Executes the model's forward pass, where `input` can be a fetchable resource or a Base64-encoded string. | +| Method | Type | Description | +| -------------------- | ----------------------------------------------------- | -------------------------------------------------------------------------------------------------------- | +| `load` | `(modelSource: ResourceSource): Promise` | Loads the model, where `modelSource` is a string that specifies the location of the model binary. | +| `forward` | `(input: string): Promise` | Executes the model's forward pass, where `input` can be a fetchable resource or a Base64-encoded string. | +| `onDownloadProgress` | `(callback: (downloadProgress: number) => void): any` | Subscribe to the download progress event. |
Type definitions