2

I'm trying to build tflite in my android project using cmake. I've managed to work this approach on the linux (debian) version of the same project.When compiling the arm64 version I followed the official docs When I open android studio and press build/run on top right corner, I get this error:

In file included from /home/user/Desktop/official_stuff/tensorflow_src/tensorflow/lite/python/interpreter_wrapper/numpy.cc:17:
In file included from /home/user/Desktop/official_stuff/tensorflow_src/tensorflow/lite/python/interpreter_wrapper/numpy.h:49:
In file included from /usr/include/python3.9/Python.h:8:
/usr/include/python3.9/pyconfig.h:9:12: fatal error: 'aarch64-linux-gnu/python3.9/pyconfig.h' file not found

When I open the file that throws the error I see the this line indicating that it's searching for it in the system:

#include <aarch64-linux-gnu/python3.9/pyconfig.h>

I ran sudo find / -name "aarch64-linux-gnu" to see whether I have that file or not and indeed I have this file installed:

user@debian:~$ sudo find / -name "aarch64-linux-gnu"
...
/home/user/toolchains/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu/aarch64-linux-gnu/include/c++/8.3.0/aarch64-linux-gnu
find: ‘/run/user/1000/doc’: Permission denied
find: ‘/run/user/1000/gvfs’: Permission denied
/usr/lib/mesa-diverted/aarch64-linux-gnu
/usr/lib/aarch64-linux-gnu
/usr/share/gdb/auto-load/lib/aarch64-linux-gnu
/usr/share/gdb/auto-load/usr/lib/aarch64-linux-gnu
/usr/include/finclude/aarch64-linux-gnu
/usr/include/aarch64-linux-gnu

I located inside /usr/include/aarch64-linux-gnu and indeed saw /python3.9/pyconfig.h.

The way I did everything is this:

  1. sudo git clone https://github.com/tensorflow/tensorflow.git /home/user/Desktop/official_stuff/tensorflow_src
  2. curl -LO https://storage.googleapis.com/mirror.tensorflow.org/developer.arm.com/media/Files/downloads/gnu-a/8.3-2019.03/binrel/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu.tar.xz
  3. mkdir -p ${HOME}/toolchains
  4. tar xvf gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu.tar.xz -C ${HOME}/toolchains
ARMCC_PREFIX=${HOME}/toolchains/gcc-arm-8.3-2019.03-x86_64-aarch64-linux-gnu/bin/aarch64-linux-gnu-
ARMCC_FLAGS="-funsafe-math-optimizations"
cmake -DCMAKE_C_COMPILER=${ARMCC_PREFIX}gcc \
  -DCMAKE_CXX_COMPILER=${ARMCC_PREFIX}g++ \
  -DCMAKE_C_FLAGS="${ARMCC_FLAGS}" \
  -DCMAKE_CXX_FLAGS="${ARMCC_FLAGS}" \
  -DCMAKE_VERBOSE_MAKEFILE:BOOL=ON \
  -DCMAKE_SYSTEM_NAME=Linux \
  -DCMAKE_SYSTEM_PROCESSOR=aarch64 \
  ../tensorflow/lite/
  1. And then I needed to ran dpkg --add-architecture arm64, apt-get update, sudo apt install libpython3-dev:arm64
  2. I simply clicked build ran after connecting my android device. It compiled for a while and then throw the error.

Here is the cmake snippet I ran that contains my tflite inclusion:

set(TENSORFLOW_SOURCE_DIR "" CACHE PATH
  "Directory that contains the TensorFlow project" )
if(NOT TENSORFLOW_SOURCE_DIR)
  get_filename_component(TENSORFLOW_SOURCE_DIR
    "/home/user/Desktop/official_stuff/tensorflow_src" ABSOLUTE)
endif()
add_library(tensorflowlite SHARED IMPORTED)
add_subdirectory(
  "/home/user/Desktop/official_stuff/tensorflow_src/tensorflow/lite"
  "${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite" EXCLUDE_FROM_ALL
)
...
target_link_libraries(
        my_proj

        tensorflow-lite
)

How can I fix this issue?

Note: I got to this point following a series of questions which all resolved:

  1. An undefined error.
  2. Question regarding android build.

More details about how I built everything can be found here. I'm on debian 11.

Turgut
  • 711
  • 3
  • 25
  • Because in the last questions you didn't follow my suggestions carefully and did extra (uncalled for) steps, I highly recommend that you post both the **entire** output log (or at least the last 200 lines or so) and ideally the entire **CMakeLists.txt** that you use to build this. It will help anyone who wishes to tackle this issue. – Milan Š. Feb 03 '23 at 09:25
  • @MilanŠ. If you are talking about the android build output, this is the entire log. The entire cmake is a bit long due to my project having a lot of dependencies, this is the only part I added. – Turgut Feb 03 '23 at 09:29
  • Also did you modify the `Python.h` file? Because the one I opened has `#include "pyconfig.h"` did you by any chance edit the file? EDIT: You can even [check it here](https://github.com/python/cpython/blob/main/Include/Python.h) – Milan Š. Feb 03 '23 at 09:32
  • @MilanŠ. No, I did not touch any file that does not belong to me – Turgut Feb 03 '23 at 09:34
  • did you try and remove this `-I/usr/include/python3.9 -I/usr/include/python3.9`? And `sudo apt install python3:arm64` To see what happens? EDIT: This is btw what I meant by the unwanted steps, it's really hard to debug something when you copy paste random solutions and pray that they work. Take a look at this path `/usr/include/aarch64-linux-gnu` vs `/usr/include/python3.9` you are probably (again) mixing architectures. – Milan Š. Feb 03 '23 at 09:37
  • I've removed those flags from cmake, ran `sudo apt install python3:arm64` (which weirdly deleted gnome-desktop, turning my pc into a terminal until I re-installed it), ran android studio build again, got the previous error: `In file included from /home/user/Desktop/official_stuff/tensorflow_src/tensorflow/lite/python/interpreter_wrapper/numpy.cc:17: /home/user/Desktop/official_stuff/tensorflow_src/tensorflow/lite/python/interpreter_wrapper/numpy.h:49:10: fatal error: 'Python.h' file not found`. Both /usr/include/aarch64-linux-gnu & /usr/include/python3.9 are present. – Turgut Feb 03 '23 at 11:01
  • Perhaps a side question but, did you or did you not follow the official cross-compilation guide [from "Android developer"](https://developer.android.com/ndk/guides/cmake#command-line)? They specify a lot more defines than you do and use "android-ndk". To me it seems you will keep running into issues. However most of these issues here could be fixed by changing the tensorflow `CMakeLists.txt`. And target including the correct header locations for individual targets. But this is too time heavy for me. So I will let other people (or preferably you) tackle it. – Milan Š. Feb 03 '23 at 11:12
  • My only advise is - use CMake instructions and not compile flags. Because you can very easily **bork up** different things in the build itself. – Milan Š. Feb 03 '23 at 11:16
  • @MilanŠ. I did not follow that link, I've followed https://www.tensorflow.org/lite/guide/build_cmake_arm . Alright, I'll try not to set flags as much as possible. – Turgut Feb 03 '23 at 11:27
  • 1
    I'm talking about what you did with `CXX_COMPILER_FLAGS` the `-D` defines are fine. – Milan Š. Feb 03 '23 at 11:28
  • @MilanŠ. yes, on the previous question I added those flags, now I've removed them. Unfortunately I had to re install debian because trying to install `sudo apt install python3:arm64` caused some really weird behaviours (My system was acting weird beforehand anyways). I'll try the steps you said, clean and keep you updated. Thanks in advance. – Turgut Feb 03 '23 at 12:58

0 Answers0