tflite编译

安装bazel

从apt安装

1.添加配置

sudo apt install apt-transport-https curl gnupg -y
curl -fsSL https://bazel.build/bazel-release.pub.gpg | gpg --dearmor >bazel-archive-keyring.gpg
sudo mv bazel-archive-keyring.gpg /usr/share/keyrings
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/bazel-archive-keyring.gpg] https://storage.googleapis.com/bazel-apt stable jdk1.8" | sudo tee /etc/apt/sources.list.d/bazel.list

2.安装

sudo apt update && sudo apt install bazel=4.2.1

从源码安装

1.安装依赖

sudo apt install pkg-config zip g++ zlib1g-dev unzip python

2.下载源码

git clone https://github.com/bazelbuild/bazel.git
cd bazel

3.编译

./compile.sh

4.安装

sudo cp output/bazel /usr/local/bin/bazel

下载预编译二进制安装

下载地址: https://github.com/bazelbuild/bazel/releases
运行方式

chmod +x bazel-version-installer-linux-x86_64.sh
./bazel-version-installer-linux-x86_64.sh --user

配置环境变量

export PATH="$PATH:$HOME/bin"

安装TensorFlow Lite

官方教程: https://www.tensorflow.org/lite/guide/build_arm?hl=zh-cn

1.下载源码

git clone https://github.com/tensorflow/tensorflow.git
cd tensorflow

或者下载指定版本

git clone -b v2.15.0 https://github.com/tensorflow/tensorflow.git

手动下载

wget https://github.com/tensorflow/tensorflow/archive/v2.15.0.zip
unzip v2.15.0.zip
cd tensorflow-2.15.0

通过bazel安装

配置

./configure

3.编译x86
on wsl(18.04)

bazel build --config=monolithic //tensorflow/lite:libtensorflowlite.so

经测试,报GLIB2.7错误,发现编译的Ubuntu(22.04)版本与应用的UBUNTU(20.04)版本不同,
使用相同的WSL版本或者docker版本可解决

4.编译arm64
官方

bazel build --config=elinux_aarch64 -c opt //tensorflow/lite:libtensorflowlite.so

经测试,报GLIB2.7错误,默认使用的是 GCC 8.3 ,构建的二进制文件需要 glibc 2.28 或更高版本。如果您的目标是较低版本的 glibc,则需要使用旧版 GCC 工具链。

bazel方式复制头文件和lib文件

此方式不好挂代理和不好设置交叉编译工具链,可编译出动态库
头文件

cd tensorflow/tensorflow
find ./lite -name "*.h" | tar -cf headers.tar -T -

lib文件

tensorflow/bazel-bin/tensorflow/lite/libtensorflowlite.so

flatbuffer

tensorflow/bazel-bin/external/flatbuffers/libflatbuffers.a

tensorflow/tensorflow/lite/tools/make下执行download_dependencies.sh,
或者直接下载flatbuffers压缩包,然后取出include的内容放置到cpp_tflite/include中即可。

通过cmake方式安装

官网地址: https://www.tensorflow.org/lite/guide/build_cmake_arm?hl=zh-cn

1.本地编译

cmake ../tensorflow_src/tensorflow/lite

实际测试,将所有a文件拷到离线设备上也无法使用

2.交叉编译

下载工具链

wget https://releases.linaro.org/components/toolchain/binaries/latest-7/aarch64-linux-gnu/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu.tar.xz
tar xvJf gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu.tar.xz
mv gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu $DIR/
export PATH="$PATH:$DIR/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu/bin"

配置

ARMCC_PREFIX=${HOME}/gcc-linaro-7.5.0-2019.12-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/

make

make -j8

或者

cmake --build . -j

注意: 此方式复制.a文件无法运行

cmake方式直接集成到项目

在Cmakelists.txt中添加代码

set(TENSORFLOW_SOURCE_DIR "" CACHE PATH
"Directory that contains the TensorFlow project"
)
if(NOT TENSORFLOW_SOURCE_DIR)
get_filename_component(TENSORFLOW_SOURCE_DIR
"${CMAKE_CURRENT_LIST_DIR}/../../../../"
ABSOLUTE
)
endif()

add_subdirectory(
"${TENSORFLOW_SOURCE_DIR}/tensorflow/lite"
"${CMAKE_CURRENT_BINARY_DIR}/tensorflow-lite"
EXCLUDE_FROM_ALL
)

set(CMAKE_CXX_STANDARD 11)

设置 TENSORFLOW_SOURCE_DIR 为自己的tensorflow路径
然后直接编译

mkdir build 
cd build
cmake ..
make -j8

交叉编译

export cross_compile_toolchain=/home/jw/gcc-linaro-7.5.0-2019.12-x86_64_aarch64-linux-gnu

mkdir -p build_arm && cd build_arm

cmake .. \
-DCMAKE_SYSTEM_NAME=Linux \
-DCMAKE_SYSTEM_VERSION=1 \
-DCMAKE_SYSTEM_PROCESSOR=aarch64 \
-DCMAKE_C_COMPILER=$cross_compile_toolchain/bin/aarch64-linux-gnu-gcc \
-DCMAKE_CXX_COMPILER=$cross_compile_toolchain/bin/aarch64-linux-gnu-g++ \
-DCMAKE_BUILD_TYPE=release

make -j4

测试发现如果程序有静态库,会自动生成静态库,将静态库保存到lib文件夹内

find ./build -name "*.so*" -exec cp {} /path/to/save/directory/ \;

cmake 方式报错解决

错误内容: cc1: error: invalid feature modifier in ‘-march=armv8.2-a+i8mm+fp16’
或者cc1: error: invalid feature modifier in ‘-march=armv8.2-a+bf16’
由于目标板子不支持i8mm,fp16,bf16,所以编译时去掉选项

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 \
-DXNNPACK_ENABLE_ARM_BF16=OFF \
-DXNNPACK_ENABLE_ARM_I8MM=OFF \
../tensorflow/lite

测试发现tensorflow2.16.1编译时无法去掉,将版本改为2.15.0,成功编译,也可以运行2.16.1转换的模型

------ 本文结束 🎉🎉 谢谢观看 ------
0%