react-native-fast-tflite v3 is a major rewrite that migrates from TurboModules to Nitro Modules. The public API remains similar, but there are several breaking changes.
v3 requires react-native-nitro-modules as a peer dependency:
# npm
npm install react-native-fast-tflite react-native-nitro-modules
# yarn
yarn add react-native-fast-tflite react-native-nitro-modulesThen rebuild native projects:
cd ios && pod install && cd ..- const model = await loadTensorflowModel(source)
+ const model = await loadTensorflowModel(source, [])The delegate parameter has been renamed to delegates and now accepts an array instead of a single string.
- const model = await loadTensorflowModel(source, 'core-ml')
+ const model = await loadTensorflowModel(source, ['core-ml'])- const model = useTensorflowModel(source, 'metal')
+ const model = useTensorflowModel(source, ['metal'])To use CPU-only inference (previously 'default'), pass an empty array:
- const model = await loadTensorflowModel(source, 'default')
+ const model = await loadTensorflowModel(source, [])- console.log(model.delegate) // 'core-ml'
+ console.log(model.delegates) // ['core-ml']run() and runSync() now accept and return ArrayBuffer[] instead of TypedArray[].
- const output = model.runSync([float32Array])
+ const output = model.runSync([float32Array.buffer])If your TypedArray is a view into a larger buffer, slice it first:
const inputBuffer = typedArray.buffer.slice(
typedArray.byteOffset,
typedArray.byteOffset + typedArray.byteLength
)
const output = model.runSync([inputBuffer]) const outputs = model.runSync([input])
- const scores = outputs[0] // was already a Float32Array
+ const scores = new Float32Array(outputs[0]!) // wrap ArrayBuffer manuallyChoose the appropriate TypedArray constructor based on your model's output tensor data type (e.g., Uint8Array, Int32Array, Float32Array).
Because TfliteModel is now a Nitro HybridObject, VisionCamera v4's worklet runtime cannot access it directly. You need to box/unbox it:
import { NitroModules } from 'react-native-nitro-modules'
const model = await loadTensorflowModel(source, ['core-ml'])
const boxedModel = NitroModules.box(model)
// Inside a VisionCamera V4 frame processor worklet:
const unboxedModel = boxedModel.unbox()
const output = unboxedModel.runSync([inputBuffer])This workaround will not be needed with VisionCamera v5.
If you had custom property overrides using the Tflite_ prefix, rename them to NitroTflite_:
- Tflite_enableGpuDelegate=true
+ NitroTflite_enableGpuDelegate=trueTensor.dataType now includes additional types: string, bfloat16, int4, uint16, uint32, uint64, complex64, complex128, resource, variant, none.
The 'invalid' type has been removed. If you had exhaustive switch/case handling over dataType, update it accordingly.