From dddb199d2a03ca48adfd5aa1d0fc37ec555d8eb4 Mon Sep 17 00:00:00 2001 From: Patrick Kabwe Date: Thu, 10 Sep 2026 20:48:57 +0200 Subject: [PATCH] docs: document android xnnpack delegate --- README.md | 14 ++++++++++++++ src/loadTensorflowModel.ts | 2 +- src/useTensorflowModel.ts | 2 +- 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index bece50df..02b6aad7 100644 --- a/README.md +++ b/README.md @@ -151,6 +151,20 @@ return [!NOTE] > Unlike v4, VisionCamera v5 no longer requires boxing the model with `NitroModules.box()`. Since v5 is built on Nitro Modules and uses [react-native-worklets](https://docs.swmansion.com/react-native-worklets/), worklets can access HybridObjects like the TFLite model directly. +### Using Android XNNPACK CPU Delegate + +Pass `['xnnpack']` to opt into the Android XNNPACK CPU delegate: + +```ts +const model = await loadTensorflowModel(require('assets/my-model.tflite'), [ + 'xnnpack', +]) +``` + +Passing `[]` keeps the standard TFLite CPU path. XNNPACK is Android-only, uses an internally selected thread count capped for mobile devices, and cannot currently be combined with `android-gpu` or `nnapi`. + +Use XNNPACK when CPU inference is the right fit for your model or when GPU/NNAPI is unavailable or slower on your target devices. Benchmark your model on real devices, especially if the app also runs camera, image preprocessing, or other CPU-heavy work. + ### Using GPU Delegates GPU Delegates offer faster, GPU-accelerated computation. There are multiple delegates available: diff --git a/src/loadTensorflowModel.ts b/src/loadTensorflowModel.ts index 04cf0b3e..f67593c2 100644 --- a/src/loadTensorflowModel.ts +++ b/src/loadTensorflowModel.ts @@ -22,7 +22,7 @@ const tfliteModule = * * If you are passing in a `{ url: ... }`, make sure the URL points directly to a `.tflite` model. This can either be a web URL (`http://..`/`https://..`), or a local file (`file://..`). * * @param source The `.tflite` model in form of either a `require(..)` statement or a `{ url: string }`. - * @param delegates The delegates to use for computations. Uses the standard CPU delegate per default. The `core-ml` or `metal` delegates are GPU-accelerated, but don't work on every model. + * @param delegates The delegates to use for computations. Uses the standard CPU delegate per default. On Android, use `xnnpack` to opt into XNNPACK CPU acceleration. The `core-ml`, `metal`, `android-gpu`, and `nnapi` delegates are hardware-accelerated, but don't work on every model. * @returns The loaded Model. */ export async function loadTensorflowModel( diff --git a/src/useTensorflowModel.ts b/src/useTensorflowModel.ts index d833142c..c97f8f15 100644 --- a/src/useTensorflowModel.ts +++ b/src/useTensorflowModel.ts @@ -24,7 +24,7 @@ export type TensorflowPlugin = * * If you are passing in a `{ url: ... }`, make sure the URL points directly to a `.tflite` model. This can either be a web URL (`http://..`/`https://..`), or a local file (`file://..`). * * @param source The `.tflite` model in form of either a `require(..)` statement or a `{ url: string }`. - * @param delegates The delegates to use for computations. Uses the standard CPU delegate per default. The `core-ml` or `metal` delegates are GPU-accelerated, but don't work on every model. + * @param delegates The delegates to use for computations. Uses the standard CPU delegate per default. On Android, use `xnnpack` to opt into XNNPACK CPU acceleration. The `core-ml`, `metal`, `android-gpu`, and `nnapi` delegates are hardware-accelerated, but don't work on every model. * @returns The state of the Model. */ export function useTensorflowModel(