From bd02713ea568b97ed494df1e3190591efab4fa6f Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Fri, 27 Mar 2026 13:32:21 +0100 Subject: [PATCH 1/6] chore: Tidy up, remove `default` delegate --- cpp/HybridTfliteModule.cpp | 82 +++++++------------ cpp/TfliteHelpers.cpp | 61 ++++++++++++++ cpp/TfliteHelpers.hpp | 9 ++ .../shared/c++/TensorflowModelDelegate.hpp | 12 +-- src/specs/Tflite.nitro.ts | 14 +++- 5 files changed, 117 insertions(+), 61 deletions(-) diff --git a/cpp/HybridTfliteModule.cpp b/cpp/HybridTfliteModule.cpp index d1dfdfca..aefeb1a5 100644 --- a/cpp/HybridTfliteModule.cpp +++ b/cpp/HybridTfliteModule.cpp @@ -5,20 +5,35 @@ #include #include #include -#else -#include +#endif +#ifdef __APPLE__ +#include #if FAST_TFLITE_ENABLE_CORE_ML #include #endif #endif -#define WRONG_PLATFORM_HINT \ - " Make sure you are using the correct delegates for the current platform " \ - "(e.g. CoreML/Metal on iOS, GPU/NNAPI on Android)." - namespace margelo::nitro::tflite { +/** + * Return a Hardware accelerated delegate, or throws + * if the given delegate type is not available. + */ +TfLiteDelegate* getDelegate(TensorflowModelDelegate delegateType) { + switch (delegateType) { + case TensorflowModelDelegate::CORE_ML: + return getCoreMLDelegate(); + case TensorflowModelDelegate::METAL: + return getMetalDelegate(); + case TensorflowModelDelegate::NNAPI: + return getNNAPIDelegate(); + case TensorflowModelDelegate::ANDROID_GPU: + return getAndroidGPUDelegate(); + } + throw std::runtime_error("Unknown Delegate \"" + std::to_string(static_cast(delegateType)) + "\"!"); +} + std::shared_ptr HybridTfliteModule::createModel(const std::shared_ptr& modelData, const std::vector& delegates) { @@ -27,52 +42,14 @@ HybridTfliteModule::createModel(const std::shared_ptr& modelData, throw std::runtime_error("Failed to create TFLite model from data!"); } + // Configure interpreter via options TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate(); - - for (const TensorflowModelDelegate& delegate : delegates) { - switch (delegate) { - case TensorflowModelDelegate::CORE_ML: { -#if FAST_TFLITE_ENABLE_CORE_ML - TfLiteCoreMlDelegateOptions delegateOptions; - TfLiteDelegate* coremlDelegate = TfLiteCoreMlDelegateCreate(&delegateOptions); - TfLiteInterpreterOptionsAddDelegate(options, coremlDelegate); - break; -#else - throw std::runtime_error("CoreML Delegate is not enabled! " - "Set $EnableCoreMLDelegate to true in Podfile and rebuild."); -#endif - } - case TensorflowModelDelegate::METAL: { - throw std::runtime_error("Metal Delegate is not supported!"); - } -#ifdef ANDROID - case TensorflowModelDelegate::NNAPI: { - TfLiteNnapiDelegateOptions delegateOptions = TfLiteNnapiDelegateOptionsDefault(); - TfLiteDelegate* nnapiDelegate = TfLiteNnapiDelegateCreate(&delegateOptions); - TfLiteInterpreterOptionsAddDelegate(options, nnapiDelegate); - break; - } - case TensorflowModelDelegate::ANDROID_GPU: { - TfLiteGpuDelegateOptionsV2 delegateOptions = TfLiteGpuDelegateOptionsV2Default(); - TfLiteDelegate* gpuDelegate = TfLiteGpuDelegateV2Create(&delegateOptions); - TfLiteInterpreterOptionsAddDelegate(options, gpuDelegate); - break; - } -#else - case TensorflowModelDelegate::NNAPI: { - throw std::runtime_error( - "NNAPI Delegate is only supported on Android!" WRONG_PLATFORM_HINT); - } - case TensorflowModelDelegate::ANDROID_GPU: { - throw std::runtime_error( - "Android-GPU Delegate is only supported on Android!" WRONG_PLATFORM_HINT); - } -#endif - default: { - // use default CPU delegate. - break; - } - } + + // Add all hardware accelerated delegates (e.g. GPU, NPU, ...) + // if any. The default CPU delegate will always be available. + for (const TensorflowModelDelegate& delegateType : delegates) { + TfLiteDelegate* delegate = getDelegate(delegateType); + TfLiteInterpreterOptionsAddDelegate(options, delegate); } TfLiteInterpreter* interpreter = TfLiteInterpreterCreate(model, options); @@ -87,8 +64,9 @@ HybridTfliteModule::createModel(const std::shared_ptr& modelData, throw std::runtime_error("Failed to create TFLite interpreter!"); } - // Wrap in HybridTfliteModel — stores shared_ptr to keep bytes alive + // Wrap in HybridTfliteModel — stores shared_ptr to keep model data bytes alive return std::make_shared(interpreter, modelData, delegates); } + } // namespace margelo::nitro::tflite diff --git a/cpp/TfliteHelpers.cpp b/cpp/TfliteHelpers.cpp index 562081be..83d26fca 100644 --- a/cpp/TfliteHelpers.cpp +++ b/cpp/TfliteHelpers.cpp @@ -1,5 +1,21 @@ #include "TfliteHelpers.hpp" +#ifdef ANDROID +#include +#include +#include +#endif + +#ifdef __APPLE__ +#include +#if FAST_TFLITE_ENABLE_CORE_ML +#include +#endif +#endif + +namespace margelo::nitro::tflite { + +// TODO: Remove this, this doesn't seem like a good idea at all. typedef float float32_t; typedef double float64_t; @@ -29,10 +45,14 @@ std::string tfLiteStatusToString(TfLiteStatus status) { std::string dataTypeToString(TfLiteType dataType) { switch (dataType) { + case kTfLiteFloat16: + return "float16"; case kTfLiteFloat32: return "float32"; case kTfLiteFloat64: return "float64"; + case kTfLiteBFloat16: + return "bfloat16"; case kTfLiteInt4: return "int4"; case kTfLiteInt8: @@ -105,3 +125,44 @@ int getTensorTotalLength(const TfLiteTensor* tensor) { } return size; } + + +TfLiteDelegate* getCoreMLDelegate() { +#ifdef __APPLE__ +#if FAST_TFLITE_ENABLE_CORE_ML + TfLiteCoreMlDelegateOptions delegateOptions; + TfLiteDelegate* coreMlDelegate = TfLiteCoreMlDelegateCreate(&delegateOptions); + return coreMlDelegate; +#else // FAST_TFLITE_ENABLE_CORE_ML + throw std::runtime_error("The CoreML Delegate (\"core-ml\") is not enabled! " + "Set `$EnableCoreMLDelegate` to `true` in your Podfile, and rebuild."); +#endif +#else // __APPLE__ + throw std::runtime_error("The CoreML Delegate (\"core-ml\") is only supported on Apple Platforms!"); +#endif +} + +TfLiteDelegate* getMetalDelegate() { + throw std::runtime_error("Metal Delegate is not yet supported!"); +} + +TfLiteDelegate* getNNAPIDelegate() { +#ifdef ANDROID + TfLiteNnapiDelegateOptions delegateOptions = TfLiteNnapiDelegateOptionsDefault(); + TfLiteDelegate* nnapiDelegate = TfLiteNnapiDelegateCreate(&delegateOptions); + return nnapiDelegate; +#else // ANDROID + throw std::runtime_error("The NNAPI Delegate (\"nnapi\") is only supported on Android!"); +#endif +} + +TfLiteDelegate* getAndroidGPUDelegate() { +#ifdef ANDROID + TfLiteGpuDelegateOptionsV2 delegateOptions = TfLiteGpuDelegateOptionsV2Default(); + TfLiteDelegate* gpuDelegate = TfLiteGpuDelegateV2Create(&delegateOptions); +#else // ANDROID + throw std::runtime_error("The Android GPU Delegate (\"android-gpu\") is only supported on Android!"); +#endif +} + +} diff --git a/cpp/TfliteHelpers.hpp b/cpp/TfliteHelpers.hpp index 20cc7f92..a1d06adc 100644 --- a/cpp/TfliteHelpers.hpp +++ b/cpp/TfliteHelpers.hpp @@ -8,7 +8,16 @@ #include #endif +namespace margelo::nitro::tflite { + std::string tfLiteStatusToString(TfLiteStatus status); std::string dataTypeToString(TfLiteType dataType); size_t getTFLTensorDataTypeSize(TfLiteType dataType); int getTensorTotalLength(const TfLiteTensor* tensor); + +TfLiteDelegate* getCoreMLDelegate(); +TfLiteDelegate* getMetalDelegate(); +TfLiteDelegate* getNNAPIDelegate(); +TfLiteDelegate* getAndroidGPUDelegate(); + +} diff --git a/nitrogen/generated/shared/c++/TensorflowModelDelegate.hpp b/nitrogen/generated/shared/c++/TensorflowModelDelegate.hpp index d9d63d93..1696aadd 100644 --- a/nitrogen/generated/shared/c++/TensorflowModelDelegate.hpp +++ b/nitrogen/generated/shared/c++/TensorflowModelDelegate.hpp @@ -29,11 +29,10 @@ namespace margelo::nitro::tflite { * An enum which can be represented as a JavaScript union (TensorflowModelDelegate). */ enum class TensorflowModelDelegate { - DEFAULT SWIFT_NAME(default) = 0, - METAL SWIFT_NAME(metal) = 1, - CORE_ML SWIFT_NAME(coreMl) = 2, - NNAPI SWIFT_NAME(nnapi) = 3, - ANDROID_GPU SWIFT_NAME(androidGpu) = 4, + METAL SWIFT_NAME(metal) = 0, + CORE_ML SWIFT_NAME(coreMl) = 1, + NNAPI SWIFT_NAME(nnapi) = 2, + ANDROID_GPU SWIFT_NAME(androidGpu) = 3, } CLOSED_ENUM; } // namespace margelo::nitro::tflite @@ -46,7 +45,6 @@ namespace margelo::nitro { static inline margelo::nitro::tflite::TensorflowModelDelegate fromJSI(jsi::Runtime& runtime, const jsi::Value& arg) { std::string unionValue = JSIConverter::fromJSI(runtime, arg); switch (hashString(unionValue.c_str(), unionValue.size())) { - case hashString("default"): return margelo::nitro::tflite::TensorflowModelDelegate::DEFAULT; case hashString("metal"): return margelo::nitro::tflite::TensorflowModelDelegate::METAL; case hashString("core-ml"): return margelo::nitro::tflite::TensorflowModelDelegate::CORE_ML; case hashString("nnapi"): return margelo::nitro::tflite::TensorflowModelDelegate::NNAPI; @@ -57,7 +55,6 @@ namespace margelo::nitro { } static inline jsi::Value toJSI(jsi::Runtime& runtime, margelo::nitro::tflite::TensorflowModelDelegate arg) { switch (arg) { - case margelo::nitro::tflite::TensorflowModelDelegate::DEFAULT: return JSIConverter::toJSI(runtime, "default"); case margelo::nitro::tflite::TensorflowModelDelegate::METAL: return JSIConverter::toJSI(runtime, "metal"); case margelo::nitro::tflite::TensorflowModelDelegate::CORE_ML: return JSIConverter::toJSI(runtime, "core-ml"); case margelo::nitro::tflite::TensorflowModelDelegate::NNAPI: return JSIConverter::toJSI(runtime, "nnapi"); @@ -73,7 +70,6 @@ namespace margelo::nitro { } std::string unionValue = JSIConverter::fromJSI(runtime, value); switch (hashString(unionValue.c_str(), unionValue.size())) { - case hashString("default"): case hashString("metal"): case hashString("core-ml"): case hashString("nnapi"): diff --git a/src/specs/Tflite.nitro.ts b/src/specs/Tflite.nitro.ts index de5c2447..0d848384 100644 --- a/src/specs/Tflite.nitro.ts +++ b/src/specs/Tflite.nitro.ts @@ -1,7 +1,6 @@ import type { HybridObject } from 'react-native-nitro-modules' export type TensorflowModelDelegate = - | 'default' | 'metal' | 'core-ml' | 'nnapi' @@ -24,6 +23,15 @@ export interface TfliteModel export interface TfliteModule extends HybridObject<{ ios: 'c++'; android: 'c++' }> { + /** + * Create a new {@linkcode TfliteModel} with the given + * {@linkcode modelData} (a binary representation of the + * TFLite model), and optionally a list of hardware + * accelerating {@linkcode TensorflowModelDelegate}s. + * + * If {@linkcode delegates} is empty (`[]`), the default + * CPU delegate will be used. + */ createModel( modelData: ArrayBuffer, delegates: TensorflowModelDelegate[] @@ -32,5 +40,9 @@ export interface TfliteModule export interface AssetLoader extends HybridObject<{ ios: 'swift'; android: 'kotlin' }> { + /** + * Load an asset from the given {@linkcode path} and + * return its contents as an {@linkcode ArrayBuffer}. + */ loadAsset(path: string): Promise } From 8069490f9ef43481e6bd309ae466952dd2579e65 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Fri, 27 Mar 2026 13:33:17 +0100 Subject: [PATCH 2/6] chore: Lint --- cpp/HybridTfliteModule.cpp | 6 +++--- cpp/TfliteHelpers.cpp | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/cpp/HybridTfliteModule.cpp b/cpp/HybridTfliteModule.cpp index aefeb1a5..48c4f181 100644 --- a/cpp/HybridTfliteModule.cpp +++ b/cpp/HybridTfliteModule.cpp @@ -31,7 +31,8 @@ TfLiteDelegate* getDelegate(TensorflowModelDelegate delegateType) { case TensorflowModelDelegate::ANDROID_GPU: return getAndroidGPUDelegate(); } - throw std::runtime_error("Unknown Delegate \"" + std::to_string(static_cast(delegateType)) + "\"!"); + throw std::runtime_error("Unknown Delegate \"" + std::to_string(static_cast(delegateType)) + + "\"!"); } std::shared_ptr @@ -44,7 +45,7 @@ HybridTfliteModule::createModel(const std::shared_ptr& modelData, // Configure interpreter via options TfLiteInterpreterOptions* options = TfLiteInterpreterOptionsCreate(); - + // Add all hardware accelerated delegates (e.g. GPU, NPU, ...) // if any. The default CPU delegate will always be available. for (const TensorflowModelDelegate& delegateType : delegates) { @@ -68,5 +69,4 @@ HybridTfliteModule::createModel(const std::shared_ptr& modelData, return std::make_shared(interpreter, modelData, delegates); } - } // namespace margelo::nitro::tflite diff --git a/cpp/TfliteHelpers.cpp b/cpp/TfliteHelpers.cpp index 83d26fca..dd7e7016 100644 --- a/cpp/TfliteHelpers.cpp +++ b/cpp/TfliteHelpers.cpp @@ -126,7 +126,6 @@ int getTensorTotalLength(const TfLiteTensor* tensor) { return size; } - TfLiteDelegate* getCoreMLDelegate() { #ifdef __APPLE__ #if FAST_TFLITE_ENABLE_CORE_ML @@ -138,7 +137,8 @@ TfLiteDelegate* getCoreMLDelegate() { "Set `$EnableCoreMLDelegate` to `true` in your Podfile, and rebuild."); #endif #else // __APPLE__ - throw std::runtime_error("The CoreML Delegate (\"core-ml\") is only supported on Apple Platforms!"); + throw std::runtime_error( + "The CoreML Delegate (\"core-ml\") is only supported on Apple Platforms!"); #endif } @@ -161,8 +161,9 @@ TfLiteDelegate* getAndroidGPUDelegate() { TfLiteGpuDelegateOptionsV2 delegateOptions = TfLiteGpuDelegateOptionsV2Default(); TfLiteDelegate* gpuDelegate = TfLiteGpuDelegateV2Create(&delegateOptions); #else // ANDROID - throw std::runtime_error("The Android GPU Delegate (\"android-gpu\") is only supported on Android!"); + throw std::runtime_error( + "The Android GPU Delegate (\"android-gpu\") is only supported on Android!"); #endif } -} +} // namespace margelo::nitro::tflite From 586d349f4283308e3fcd32592104c86d0c625980 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Fri, 27 Mar 2026 13:34:03 +0100 Subject: [PATCH 3/6] fix: Remove `default` --- src/loadTensorflowModel.ts | 2 +- src/useTensorflowModel.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/loadTensorflowModel.ts b/src/loadTensorflowModel.ts index b61460fa..04cf0b3e 100644 --- a/src/loadTensorflowModel.ts +++ b/src/loadTensorflowModel.ts @@ -27,7 +27,7 @@ const tfliteModule = */ export async function loadTensorflowModel( source: ModelSource, - delegates: TensorflowModelDelegate[] = ['default'] + delegates: TensorflowModelDelegate[] ): Promise { let uri: string if (typeof source === 'number') { diff --git a/src/useTensorflowModel.ts b/src/useTensorflowModel.ts index a0609552..d833142c 100644 --- a/src/useTensorflowModel.ts +++ b/src/useTensorflowModel.ts @@ -29,7 +29,7 @@ export type TensorflowPlugin = */ export function useTensorflowModel( source: ModelSource, - delegates: TensorflowModelDelegate[] = ['default'] + delegates: TensorflowModelDelegate[] ): TensorflowPlugin { const [state, setState] = useState({ model: undefined, From 9a9b47576d14dd8e9b08c5a2386a8ceca202e6b4 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Fri, 27 Mar 2026 13:36:21 +0100 Subject: [PATCH 4/6] chore: Clean up --- cpp/HybridTfliteModel.cpp | 18 ++++++++++-------- .../TfliteExample.xcodeproj/project.pbxproj | 12 ++++++++++-- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/cpp/HybridTfliteModel.cpp b/cpp/HybridTfliteModel.cpp index ffdb9a7e..cdb72ed2 100644 --- a/cpp/HybridTfliteModel.cpp +++ b/cpp/HybridTfliteModel.cpp @@ -36,7 +36,7 @@ std::vector HybridTfliteModel::getInputs() { int count = TfLiteInterpreterGetInputTensorCount(_interpreter); std::vector tensors; tensors.reserve(count); - for (size_t i = 0; i < count; i++) { + for (int32_t i = 0; i < count; i++) { TfLiteTensor* tensor = TfLiteInterpreterGetInputTensor(_interpreter, i); if (tensor == nullptr) { throw std::runtime_error("TFLite: Failed to get input tensor " + std::to_string(i) + "!"); @@ -44,8 +44,9 @@ std::vector HybridTfliteModel::getInputs() { int dimensions = TfLiteTensorNumDims(tensor); std::vector shape; shape.reserve(dimensions); - for (size_t d = 0; d < dimensions; d++) { - shape.push_back(static_cast(TfLiteTensorDim(tensor, d))); + for (int32_t d = 0; d < dimensions; d++) { + int32_t size = TfLiteTensorDim(tensor, d); + shape.push_back(static_cast(size)); } tensors.push_back(Tensor(std::string(TfLiteTensorName(tensor)), dataTypeToString(TfLiteTensorType(tensor)), std::move(shape))); @@ -57,7 +58,7 @@ std::vector HybridTfliteModel::getOutputs() { int count = TfLiteInterpreterGetOutputTensorCount(_interpreter); std::vector tensors; tensors.reserve(count); - for (size_t i = 0; i < count; i++) { + for (int32_t i = 0; i < count; i++) { const TfLiteTensor* tensor = TfLiteInterpreterGetOutputTensor(_interpreter, i); if (tensor == nullptr) { throw std::runtime_error("TFLite: Failed to get output tensor " + std::to_string(i) + "!"); @@ -65,8 +66,9 @@ std::vector HybridTfliteModel::getOutputs() { int dimensions = TfLiteTensorNumDims(tensor); std::vector shape; shape.reserve(dimensions); - for (size_t d = 0; d < dimensions; d++) { - shape.push_back(static_cast(TfLiteTensorDim(tensor, d))); + for (int32_t d = 0; d < dimensions; d++) { + int32_t size = TfLiteTensorDim(tensor, d); + shape.push_back(static_cast(size)); } tensors.push_back(Tensor(std::string(TfLiteTensorName(tensor)), dataTypeToString(TfLiteTensorType(tensor)), std::move(shape))); @@ -82,7 +84,7 @@ void HybridTfliteModel::copyInputBuffers(const std::vector& buffer = input[i]; TfLiteTensorCopyFromBuffer(tensor, buffer->data(), buffer->size()); @@ -107,7 +109,7 @@ std::vector> HybridTfliteModel::copyOutputBuffers() std::vector> results; results.reserve(outputCount); - for (size_t i = 0; i < outputCount; i++) { + for (int32_t i = 0; i < outputCount; i++) { const TfLiteTensor* tensor = TfLiteInterpreterGetOutputTensor(_interpreter, i); std::shared_ptr outputBuffer = getOutputBufferForTensor(tensor); diff --git a/example/ios/TfliteExample.xcodeproj/project.pbxproj b/example/ios/TfliteExample.xcodeproj/project.pbxproj index 2eef3c4e..392c4308 100644 --- a/example/ios/TfliteExample.xcodeproj/project.pbxproj +++ b/example/ios/TfliteExample.xcodeproj/project.pbxproj @@ -195,10 +195,14 @@ inputFileListPaths = ( "${PODS_ROOT}/Target Support Files/Pods-TfliteExample/Pods-TfliteExample-frameworks-${CONFIGURATION}-input-files.xcfilelist", ); + inputPaths = ( + ); name = "[CP] Embed Pods Frameworks"; outputFileListPaths = ( "${PODS_ROOT}/Target Support Files/Pods-TfliteExample/Pods-TfliteExample-frameworks-${CONFIGURATION}-output-files.xcfilelist", ); + outputPaths = ( + ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-TfliteExample/Pods-TfliteExample-frameworks.sh\"\n"; @@ -234,10 +238,14 @@ inputFileListPaths = ( "${PODS_ROOT}/Target Support Files/Pods-TfliteExample/Pods-TfliteExample-resources-${CONFIGURATION}-input-files.xcfilelist", ); + inputPaths = ( + ); name = "[CP] Copy Pods Resources"; outputFileListPaths = ( "${PODS_ROOT}/Target Support Files/Pods-TfliteExample/Pods-TfliteExample-resources-${CONFIGURATION}-output-files.xcfilelist", ); + outputPaths = ( + ); runOnlyForDeploymentPostprocessing = 0; shellPath = /bin/sh; shellScript = "\"${PODS_ROOT}/Target Support Files/Pods-TfliteExample/Pods-TfliteExample-resources.sh\"\n"; @@ -264,7 +272,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CURRENT_PROJECT_VERSION = 1; - DEVELOPMENT_TEAM = B3P8HDC2S6; + DEVELOPMENT_TEAM = CJW62Q77E7; ENABLE_BITCODE = NO; INFOPLIST_FILE = TfliteExample/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 15.1; @@ -295,7 +303,7 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CLANG_ENABLE_MODULES = YES; CURRENT_PROJECT_VERSION = 1; - DEVELOPMENT_TEAM = B3P8HDC2S6; + DEVELOPMENT_TEAM = CJW62Q77E7; INFOPLIST_FILE = TfliteExample/Info.plist; IPHONEOS_DEPLOYMENT_TARGET = 15.1; LD_RUNPATH_SEARCH_PATHS = ( From 0f1aca9169d1e14f7f9d210b184fe55969e20e81 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Fri, 27 Mar 2026 13:40:22 +0100 Subject: [PATCH 5/6] chore: Lint --- cpp/TfliteHelpers.cpp | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cpp/TfliteHelpers.cpp b/cpp/TfliteHelpers.cpp index dd7e7016..ff89dcde 100644 --- a/cpp/TfliteHelpers.cpp +++ b/cpp/TfliteHelpers.cpp @@ -91,6 +91,8 @@ std::string dataTypeToString(TfLiteType dataType) { size_t getTFLTensorDataTypeSize(TfLiteType dataType) { switch (dataType) { + case kTfLiteBool: + return sizeof(bool); case kTfLiteFloat32: return sizeof(float32_t); case kTfLiteInt32: @@ -111,8 +113,9 @@ size_t getTFLTensorDataTypeSize(TfLiteType dataType) { return sizeof(uint32_t); case kTfLiteUInt16: return sizeof(uint16_t); + default: + throw std::runtime_error("Tensor DataType \"" + dataTypeToString(dataType) + "\" is not supported!"); } - throw std::runtime_error("TFLite: Unsupported output data type! " + dataTypeToString(dataType)); } int getTensorTotalLength(const TfLiteTensor* tensor) { @@ -120,7 +123,7 @@ int getTensorTotalLength(const TfLiteTensor* tensor) { if (dimensions < 1) return 0; int size = 1; - for (size_t i = 0; i < dimensions; i++) { + for (int32_t i = 0; i < dimensions; i++) { size *= TfLiteTensorDim(tensor, i); } return size; From c1c84f99e3a2c1a3280a06ae1b052de2a0523693 Mon Sep 17 00:00:00 2001 From: Marc Rousavy Date: Fri, 27 Mar 2026 13:40:27 +0100 Subject: [PATCH 6/6] Update TfliteHelpers.cpp --- cpp/TfliteHelpers.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/cpp/TfliteHelpers.cpp b/cpp/TfliteHelpers.cpp index ff89dcde..39c31a17 100644 --- a/cpp/TfliteHelpers.cpp +++ b/cpp/TfliteHelpers.cpp @@ -114,7 +114,8 @@ size_t getTFLTensorDataTypeSize(TfLiteType dataType) { case kTfLiteUInt16: return sizeof(uint16_t); default: - throw std::runtime_error("Tensor DataType \"" + dataTypeToString(dataType) + "\" is not supported!"); + throw std::runtime_error("Tensor DataType \"" + dataTypeToString(dataType) + + "\" is not supported!"); } }