Android tflite flex delegate error

Hi everyone! I have a problem with Tensorflow flex delegate on Android.

I’m trying to run simple model with some functions that require Flex Delegate to be imported.

I built and from tensorflow-r2.14 for Android.

I’m using CMake to build my own .so lib with included tf’s lib. So the first CMakeLists.txt looks like this

cmake_minimum_required(VERSION 3.10)
project(ModelTrainerLib VERSION 1.0)

# Specify where to find the header files for TF Lite C++

add_library(tflite SHARED IMPORTED)
add_library(tflite-flex SHARED IMPORTED)

# Source files

# Build the main target `ModelTrainer` that will use TF Lite
# Create shared library from sources
add_library(ModelTrainer SHARED ${SOURCES})

# Link the main target with required libs ``
    -Wl,--no-as-needed # Need --no-as-needed to link tensorflowlite_flex 

Than I link my own .so lib with main.cpp like this:

cmake_minimum_required(VERSION 3.13)

# Add the executable
add_executable(UseModelTrainerApp main.cpp)

# Create an imported target for
add_library(libtensorflowlite SHARED IMPORTED)
set_target_properties(libtensorflowlite PROPERTIES
    IMPORTED_LOCATION "/Users/andrey/Desktop/mnist_tf/lib/arm64-v8a/"
    INTERFACE_INCLUDE_DIRECTORIES "/Users/andrey/Desktop/mnist_tf/lib/include"

# Create an imported target for
add_library(libtensorflowlite_flex SHARED IMPORTED)
set_target_properties(libtensorflowlite_flex PROPERTIES
    IMPORTED_LOCATION "/Users/andrey/Desktop/mnist_tf/lib/arm64-v8a/"

# Do the same for, and specify its dependency on libtensorflowlite
add_library(libModelTrainer SHARED IMPORTED)
set_target_properties(libModelTrainer PROPERTIES
    IMPORTED_LOCATION "/Users/andrey/Desktop/mnist_tf/lib/build_android_arm64/"
    INTERFACE_INCLUDE_DIRECTORIES "/Users/andrey/Desktop/mnist_tf/lib"
    -Wl,--no-as-needed # Need --no-as-needed to link tensorflowlite_flex 

# Link UseModelTrainerApp with libModelTrainer only, libtensorflowlite will be included automatically

# If the library uses TensorFlow Lite or other Android NDK libraries, link them as well
find_library(log-lib log)
target_link_libraries(UseModelTrainerApp ${log-lib})

Than I get executable file successfully. I push these files (my own lib, tf’s libs, executable file and model) to my device with adb. Run program and still get error:

ERROR: Select TensorFlow op(s), included in the given model, is(are) not supported by this interpreter. Make sure you apply/link the Flex delegate before inference. For the Android, it can be resolved by adding “org.tensorflow:tensorflow-lite-select-tf-ops” dependency. See instructions: Selecione operadores do TensorFlow  |  TensorFlow Lite

ERROR: Node number 5 (FlexSave) failed to prepare.

I have no idea how to fix it, so I will be very grateful for any help.

[Best guess]

To fix the TensorFlow Lite Flex delegate error on Android, ensure that the Flex delegate is correctly linked and initialized in your C++ code. Double-check library paths in your CMake files, and explicitly apply the Flex delegate to the TensorFlow Lite interpreter in your application. If issues persist, consider building TensorFlow Lite and the Flex delegate from source to ensure compatibility.

How to initialise delegate in c++?

To initialize a TensorFlow Lite Flex Delegate in C++, you need to include the Flex Delegate header, create the delegate using TfLiteFlexDelegateCreate(), and apply it to your TensorFlow Lite Interpreter instance with ModifyGraphWithDelegate(). After using the interpreter, remember to delete the delegate with TfLiteFlexDelegateDelete() to free resources.

Are you sure that tf lite has these methods? I can’t find such methods in source code