ARM-software / armnn

Arm NN ML Software. The code here is a read-only mirror of https://review.mlplatform.org/admin/repos/ml/armnn
https://developer.arm.com/products/processors/machine-learning/arm-nn
MIT License
1.15k stars 307 forks source link

Compile error for TFlite Delegate #593

Closed xiaotongnii closed 2 years ago

xiaotongnii commented 2 years ago

aim at: Build the TfLite Delegate (Stand-Alone) ref from:https://github.com/ARM-software/armnn/blob/branches%2Farmnn_21_02/delegate/BuildGuideNative.md env: 1) ubuntu18.04(x86) 2) tensorflow-2.3.1 -build success 3) flatbuffers-1.12.0-build arm64 success 4) compute library_21.02 -build success scons arch=arm64-v8a neon=1 opencl=1 embed_kernels=1 extra_cxx_flags="-fPIC" benchmark_tests=0 validation_tests=0 -j8 internal_only=0 5)build armnn 21.02 success

CXX=arrch64-linux-gnu-g++ CC=aarch64-linux-gnu-gcc  cmake .. -DARMCOMPUTE_ROOT=$BASEDIR/ComputeLibrary -DARMCOMPUTENEON=1 -DBUILD_UNIT_TESTS=0 
make

6) build tflte delegate

CXX=arrch64-linux-gnu-g++ CC=aarch64-linux-gnu-gcc cmake .. -DTENSORFLOW_LIB_DIR=$BASEDIR/tensorflow/bazel-bin -DTENSORFLOW_ROOT=$BASEDIR/tensorflow -DTFLITE_LIB_ROOT=$BASEDIR/tensorflow/bazel-bin -DFLATBUFFERS_ROOT=$BASEDIR/flatbuffers/install -DArmnn_DIR=$BASEDIR/armnn/build -DARMNN_SOURCE_DIR=$BASEDIR/armnn
make

/usr/lib/gcc-cross/aarch64-linux-gnu/7/../../../aarch64-linux-gnu/bin/ld:skipping incompatible /home/***/delegate/tensorflow/bazel-bin/libtensorflow_lite_all.so when searching for -ltensorflow_lite_all /usr/lib/gcc-cross/aarch64-linux-gnu/7/../../../aarch64-linux-gnu/bin/ld: cannot find -ltensorflow_lite_all look at dirctory,libtensorflow_lite_all.so is exist:

ls ../../../tensorflow-bazel-bin
libtensorflow_lite_all.so

please give me some advice to slove it,thanks。

xiaotongnii commented 2 years ago

please give me some advice. thanks.

MikeJKelly commented 2 years ago

Hi @Niuxiaotong

The skipping incompatible warning indicates that the libtensorflow_lite_all.so you built is for an incompatible architecture. The guide you mention is for building on a native Aarch64 machine but you're compiling on x86. What kind of device are you planning on using the delegate on?

It would be better to follow the cross compilation guide at: https://github.com/ARM-software/armnn/blob/branches/armnn_21_02/BuildGuideCrossCompilation.md which will give you a way of building the dependencies on x86 for an Aarch64 machine. The step to build the Delegate should work then.

Best regards, MIke

MikeJKelly commented 2 years ago

You may find it better to download the pre-built binaries for your target platform from https://github.com/ARM-software/armnn/releases/tag/v21.08

xiaotongnii commented 2 years ago

Hi @MikeJKelly sure, I build x86 machine。And I build them in cross-compile way ,except tensorflow。 in the Documentation, it use bazel to build tensorflow, as follow: wget -O bazel-3.1.0-dist.zip https://github.com/bazelbuild/bazel/releases/download/3.1.0/bazel-3.1.0-dist.zip unzip -d bazel bazel-3.1.0-dist.zip cd bazel env EXTRA_BAZEL_ARGS="--host_javabase=@local_jdk//:jdk" bash ./compile.sh

PATH="$BASEDIR/bazel/output:$PATH" ./configure $BASEDIR/bazel/output/bazel build --define=grpc_no_ares=true --config=opt --config=monolithic --strip=always --config=noaws libtensorflow_all.so $BASEDIR/bazel/output/bazel build --config=opt --config=monolithic --strip=always libtensorflow_lite_all.so

Fristly,use this bazel to build tensorflow and have something about architecture? and tensorflow_lite.so is x86 or aarch64?

xiaotongnii commented 2 years ago

Hi @MikeJKelly Secondly,According to https://github.com/ARM-software/armnn/blob/branches/armnn_21_02/BuildGuideCrossCompilation.md build tensorflow cd $HOME/armnn-devenv git clone https://github.com/tensorflow/tensorflow.git cd tensorflow/ git checkout fcc4b966f1265f466e82617020af93670141b009 ../armnn/scripts/generate_tensorflow_protobuf.sh ../tensorflow-protobuf ../google/x86_64_pb_install build tflite cd $HOME/armnn-devenv mkdir tflite cd tflite cp ../tensorflow/tensorflow/lite/schema/schema.fbs . ../flatbuffers-1.12.0/build/flatc -c --gen-object-api --reflect-types --reflect-names schema.fbs

It seems that it can not build tensorflow to a 'tensorflow_lite.so'. Generanly, there is a different building method different between 'bazel'and other. Looking for reply,thanks.

MikeJKelly commented 2 years ago

Hi @Niuxiaotong

if you follow the native guide then the libtensorflow_lite_all.so file will be compiled for x86 and not aarch64 (because that guide has the details for native-compilation, not cross-compilaiton).

You can see the architecture that the library is compiled for by using:

cd $HOME/armnn-devenv/tensorflow/bazel-bin
file libtensorflow_lite_all.so

If the file is compiled for x86 you will see something like the following:

libtensorflow_lite_all.so: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=216897f06692279b906ee7868debc1a9ea71b802, not stripped

You can cross-compile TensorFlow for aarch64 using the following command:

cd $HOME/armnn-devenv/tensorflow
PATH="$HOME/armnn-devenv/bazel/output:$PATH" ./configure
$HOME/armnn-devenv/bazel/output/bazel build --config=elinux_aarch64 -c opt //tensorflow/lite/c:libtensorflowlite_c.so --strip=always libtensorflow_lite_all.so

Then when you call file libtensorflow_lite_all.so you should see something like this:

libtensorflow_lite_all.so: ELF 64-bit LSB shared object, ARM aarch64, version 1 (GNU/Linux), dynamically linked, BuildID[md5/uuid]=82df50ea1ac2dde2830ae1f4236eb893, not stripped

Best regards, Mike.

xiaotongnii commented 2 years ago

Hi, @MikeJKelly Yes, exactly, file libtensorflow_lite_all.so libtensorflow_lite_all.so: ELF 64-bit LSB shared object, x86-64, version 1 (GNU/Linux), dynamically linked, BuildID[sha1]=216897f06692279b906ee7868debc1a9ea71b802, not stripped cross-compile tensorflow for aarch64 , libtensorflow_lite_all.so cd $HOME/armnn-devenv/tensorflow PATH="$HOME/armnn-devenv/bazel/output:$PATH" ./configure $HOME/armnn-devenv/bazel/output/bazel build --config=elinux_aarch64 -c opt //tensorflow/lite/c:libtensorflowlite_c.so --strip=always libtensorflow_lite_all.so I also get arrch64 libtensorflow_lite,all.so. thanks, xiaotong.

xiaotongnii commented 2 years ago

Hi, @MikeJKelly I have built Tflite delegate stand-alone successfully, mkdir delegate && cd delegate LD_LIBRARY_PATH=./$LD_LIBRARY_PATH ./DelegateUnitTests --test-suite=*CpuAcc* DelegateUnitTests also test ssucessfully. Due to corss-compile on x86 machine,need to copy DelegateUnitTests \libarmmnn.so.24\libArmnnDelegate.so.VERSION\libtensorflow_lite_all.so to arm64 mechine,and run . Fianlly,thanks very much.

xiaotongnii commented 2 years ago

Hi @MikeJKelly I find a question, building a C library other than C++ library,according to the following code cd $HOME/armnn-devenv/tensorflow PATH="$HOME/armnn-devenv/bazel/output:$PATH" ./configure $HOME/armnn-devenv/bazel/output/bazel build --config=elinux_aarch64 -c opt //tensorflow/lite/c:libtensorflowlite_c.so --strip=always libtensorflow_lite_all.so build C++ library ,following cd $HOME/armnn-devenv/tensorflow PATH="$HOME/armnn-devenv/bazel/output:$PATH" ./configure $HOME/armnn-devenv/bazel/output/bazel build --config=elinux_aarch64 -c opt //tensorflow/lite:libtensorflowlite.so --strip=always libtensorflow_lite_all.so is there diffieret between both?

MikeJKelly commented 2 years ago

Hi @Niuxiaotong

The first command //tensorflow/lite/c:libtensorflowlite_c.so will build the TensorFlow Lite C API the second command //tensorflow/lite:libtensorflowlite.so will build the TensorFlow Lite C++ API. You should probably use the second command as it matches the build guides. Sorry for any confusion!

Best regards, Mike

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/c/README.md

xiaotongnii commented 2 years ago

Hi @MikeJKelly It's OK , I have deat it, Anyway thank you!