Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions MIGRATION_V2_TO_V3.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 3 additions & 1 deletion cpp/TfliteHelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
1 change: 0 additions & 1 deletion example/ios/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ if linkage != nil
end

$EnableCoreMLDelegate=true
$EnableMetalDelegate=true

target 'TfliteExample' do
config = use_native_modules!
Expand Down
2 changes: 1 addition & 1 deletion src/loadTensorflowModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 9 additions & 0 deletions src/specs/Tflite.nitro.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down
4 changes: 2 additions & 2 deletions src/useTensorflowModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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)])

Expand Down