The instructions for Tensorflow Lite cross compilation for ARM are inaccurate (contain errors/omissions, as far as I can see), as follows:
-
In the PREREQUISITES section it states " You need CMake installed and downloaded TensorFlow source code. Please check Build TensorFlow Lite with CMake page for the details."
To be more accurate you need to execute step 1 and 2 shown on that referenced page. Step 3 also needs to be done but not yet. -
In the section " Build for Raspberry Pi Zero (ARMv6)" and then the instructions for “download toolchain” a second line should be added that reads “mkdir -p ${HOME}/toolchains” This line is shown in the instructions for the other builds but not in the instructions for the Raspberry Pi Zero. Without creating this directory, the next statements will fail.
-
After installing the toolchains but before running cmake you need to execute step 3 of the PREREQUISITES, being:
mkdir tflite_build
cd tflite_build
since the cmake statement needs to be run from that directory. -
If you then execute the cmake statement as shown it will fail, throwing errors as follows:
4a) first error “CMake Error: The source directory “/home/tensorflow/lite” does not exist.” this is because the directory should be “/home/tensorflow_src/tensorflow/lite” instead. You should add “/tensorflow_src” to the cmake statement “…/tensorflow/lite”
4b) running it again then throws the error (not the complete text shown):
The C compiler identification is unknown
– The CXX compiler identification is unknown
– Detecting C compiler ABI info
– Detecting C compiler ABI info - failed
– Check for working C compiler: /home/pi/toolchains/arm-rpi-linux-gnueabihf/x64-gcc-6.5.0/arm-rpi-linux-gnueabihf/bin/arm-rpi-linux-gnueabihf-gcc
– Check for working C compiler: /home/pi/toolchains/arm-rpi-linux-gnueabihf/x64-gcc-6.5.0/arm-rpi-linux-gnueabihf/bin/arm-rpi-linux-gnueabihf-gcc - broken
CMake Error at /usr/share/cmake-3.18/Modules/CMakeTestCCompiler.cmake:66 (message):
The C compiler
“/home/pi/toolchains/arm-rpi-linux-gnueabihf/x64-gcc-6.5.0/arm-rpi-linux-gnueabihf/bin/arm-rpi-linux-gnueabihf-gcc”
is not able to compile a simple test program. (etc etc etc rest of text not copied.)
I was not sure how to correct this but I removed the ${ARMCC_PREFIX} for the gcc and g++ compiler in the cmake statement, just using the compilers already installed. At least it finds the compilers and starts the process. (if I change it to use 8.3.0. instead of 6.5.0 it also fails)
4c) Cmake gives a lot of positive messages about progress but then at the end reports the errors:
Looking for a Fortran compiler
– Looking for a Fortran compiler - NOTFOUND
– Could NOT find CLANG_FORMAT: Found unsuitable version “0.0”, but required is exact version “9” (found CLANG_FORMAT_EXECUTABLE-NOTFOUND)
so I installed a fortran compiler with “sudo apt-get install gfortran”. Nowhere this is mentioned as a prerequisite.
It still reports failures on the following points, not sure whether this is due to changing the compiler path or anything else, or how to solve it. All other message are positive.
– Performing Test CMAKE_HAVE_LIBC_PTHREAD - Failed
– Performing Test COMPILER_SUPPORT_Wshorten64to32 - Failed
– Performing Test COMPILER_SUPPORT_Wenumconversion - Failed
– Performing Test COMPILER_SUPPORT_Wcpp11extensions - Failed
– Performing Test COMPILER_SUPPORT_wd981 - Failed
– Performing Test COMPILER_SUPPORT_wd2304 - Failed
4d) Running cmake again now still reports
Could NOT find CLANG_FORMAT: Found unsuitable version “0.0”, but required is exact version “9” (found CLANG_FORMAT_EXECUTABLE-NOTFOUND)
while the other messages all look positive. (except for the tests shown at 4c above)
I don’t know whether this message about clang-format is critical and I don’t know what to do to solve it.
Also I have not tested running tensorflow yet after these steps.
I am running a Raspberry Pi Zero W v1.1 with updated and upgraded Bullseye OS (Dec 17, 2021).
So to summarise, the final cmake statement I used was as follows (running from the tflite_build directory):
cmake -DCMAKE_C_COMPILER=gcc -DCMAKE_CXX_COMPILER=g++ -DCMAKE_C_FLAGS=“${ARMCC_FLAGS}” -DCMAKE_CXX_FLAGS=“${ARMCC_FLAGS}” -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON -DCMAKE_SYSTEM_NAME=Linux -DCMAKE_SYSTEM_PROCESSOR=armv6 -DTFLITE_ENABLE_XNNPACK=OFF …/tensorflow_src/tensorflow/lite/
(note: somehow the forum adds 3 dots in front of “…/tensorflow_src/etc” , should just be 2.)
- For clarity it could be added that step 5 or 6 of the page referenced in the PREREQUISITES should then be executed.