diff --git a/MIGRATION_V2_TO_V3.md b/MIGRATION_V2_TO_V3.md index d165670e..0e107daa 100644 --- a/MIGRATION_V2_TO_V3.md +++ b/MIGRATION_V2_TO_V3.md @@ -36,8 +36,22 @@ The `delegate` parameter has been renamed to `delegates` and now accepts an **ar ``` ```diff -- const model = useTensorflowModel(source, 'metal') -+ const model = useTensorflowModel(source, ['metal']) +- const model = useTensorflowModel(source, 'core-ml') ++ const model = useTensorflowModel(source, ['core-ml']) +``` + +### The `'metal'` delegate is not implemented + +`'metal'` is still part of the `TensorflowModelDelegate` union, but it has no +implementation - creating a model with it throws +`The Metal Delegate ("metal") is not implemented!` +(see [`cpp/TfliteHelpers.cpp`](./cpp/TfliteHelpers.cpp), `getMetalDelegate()`). +It is documented as deprecated on the `TensorflowModelDelegate` type. On Apple +platforms, use `'core-ml'` instead: + +```diff +- const model = useTensorflowModel(source, ['metal']) ++ const model = useTensorflowModel(source, ['core-ml']) ``` ### The `'default'` delegate is removed diff --git a/README.md b/README.md index bece50df..24091956 100644 --- a/README.md +++ b/README.md @@ -12,7 +12,7 @@ A high-performance [TensorFlow Lite](https://www.tensorflow.org/lite) library fo - 💨 Zero-copy ArrayBuffers - 🔧 Uses the low-level C/C++ TensorFlow Lite core API for direct memory access - 🔄 Supports swapping out TensorFlow Models at runtime -- 🖥️ Supports GPU-accelerated delegates (CoreML/Metal/OpenGL) +- 🖥️ Supports hardware-accelerated delegates (CoreML on iOS, GPU/NNAPI on Android) - 📸 Easy [VisionCamera](https://github.com/mrousavy/react-native-vision-camera) integration ## Migrating from v2 diff --git a/cpp/TfliteHelpers.cpp b/cpp/TfliteHelpers.cpp index 9f931f93..23ff5163 100644 --- a/cpp/TfliteHelpers.cpp +++ b/cpp/TfliteHelpers.cpp @@ -147,7 +147,9 @@ TfLiteDelegate* getCoreMLDelegate() { } TfLiteDelegate* getMetalDelegate() { - throw std::runtime_error("Metal Delegate is not yet supported!"); + throw std::runtime_error("The Metal Delegate (\"metal\") is not implemented! " + "Use \"core-ml\" on Apple platforms, or \"android-gpu\"/\"nnapi\" on " + "Android instead."); } TfLiteDelegate* getNNAPIDelegate() { diff --git a/example/ios/Podfile b/example/ios/Podfile index 6d181c3b..d9a802ca 100644 --- a/example/ios/Podfile +++ b/example/ios/Podfile @@ -15,7 +15,6 @@ if linkage != nil end $EnableCoreMLDelegate=true -$EnableMetalDelegate=true target 'TfliteExample' do config = use_native_modules! diff --git a/src/loadTensorflowModel.ts b/src/loadTensorflowModel.ts index 04cf0b3e..f72dea68 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. Pass an empty array (`[]`) to use the standard CPU delegate. The `core-ml` (iOS), `android-gpu` and `nnapi` (Android) delegates are hardware-accelerated, but don't work on every model. * @returns The loaded Model. */ export async function loadTensorflowModel( diff --git a/src/specs/Tflite.nitro.ts b/src/specs/Tflite.nitro.ts index 8eca06ca..3ab3d7c7 100644 --- a/src/specs/Tflite.nitro.ts +++ b/src/specs/Tflite.nitro.ts @@ -1,5 +1,14 @@ import type { HybridObject } from 'react-native-nitro-modules' +/** + * A hardware-accelerating delegate to run the TFLite model with. + * + * - `'core-ml'`: CoreML (Apple platforms). Requires `$EnableCoreMLDelegate`. + * - `'nnapi'`: NNAPI (Android) + * - `'android-gpu'`: GPU (Android) + * - `'metal'`: **deprecated, not implemented** - creating a model with this + * delegate always throws. Use `'core-ml'` on Apple platforms instead. + */ export type TensorflowModelDelegate = | 'metal' | 'core-ml' diff --git a/src/useTensorflowModel.ts b/src/useTensorflowModel.ts index d833142c..61e58b63 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. Pass an empty array (`[]`) to use the standard CPU delegate. The `core-ml` (iOS), `android-gpu` and `nnapi` (Android) delegates are hardware-accelerated, but don't work on every model. * @returns The state of the Model. */ export function useTensorflowModel( @@ -50,7 +50,7 @@ export function useTensorflowModel( } load() // JSON.stringify compares delegates by value so inline array literals - // (e.g. ['core-ml', 'default']) don't cause the effect to re-run every render + // (e.g. ['core-ml']) don't cause the effect to re-run every render // eslint-disable-next-line react-hooks/exhaustive-deps }, [source, JSON.stringify(delegates)])