环境
系统环境:
% cat /etc/issue
Ubuntu 18.04.5 LTS \n \l
安装的tensorflow为当前Release版本:tensorflow-2.4.1.tar.gz。可直接到github下载。
在其他版本的Linux上也可以安装。
本文目标:编译出tensorflow的动态链接库以及准备好tensorflow第三方依赖库。
基础包
包括但不限于python3、Numpy、tar、zip、git等。
如果有缺失,安装过程中会报错,根据提示安装即可。
大部分系统已经默认安装,如需安装可参考网上资料。
安装bazel
bazel是谷歌推出的项目构建工具,tensorflow就是用它构建的。
注意,需要安装 3.1.0 版本,否则会报错:
具体安装过程可参考:Bazel 4.0.0在Linux下的安装(附国内安装包下载源)
相应的下载包替换为:bazel-3.1.0-installer-linux-x86_64.sh 即可。
编译TensorFlow
在github下载稳定版本源码:https://github.com/tensorflow/tensorflow/releases。
我使用的是 tensorflow-2.4.1.tar.gz。
执行以下指令:
tar zxf tensorflow-2.4.1.tar.gz
cd tensorflow-2.4.1
./configure
会有许多配置项选择,注意,python指定python3(如果没有,需要提前安装),然后其他根据需要选择,一般都选择默认的就可以。
可以看到,编译前目录如下:
# ls
ACKNOWLEDGMENTS AUTHORS CODE_OF_CONDUCT.md configure configure.py ISSUES.md LICENSE README.md SECURITY.md third_party WORKSPACE
arm_compiler.BUILD BUILD CODEOWNERS configure.cmd CONTRIBUTING.md ISSUE_TEMPLATE.md models.BUILD RELEASE.md tensorflow tools
下面开始编译:
- 不需要CUDA:bazel build --config=opt //tensorflow:libtensorflow_cc.so
- 需要CUDA: bazel build --config=opt --config=cuda //tensorflow:libtensorflow_cc.so
这个过程需要在线下载很多依赖包,时间较长,而且需要较多的CPU资源,不要在生产环境中执行,以免影响正常进程。需要修持网络连接良好,失败了可以重试。
结果如下:
# bazel build --config=opt //tensorflow:libtensorflow_cc.so
Starting local Bazel server and connecting to it...
INFO: Options provided by the client:
Inherited 'common' options: --isatty=1 --terminal_columns=236
INFO: Reading rc options for 'build' from /home/ysj/pkg/tensorflow-2.4.1/.bazelrc:
Inherited 'common' options: --experimental_repo_remote_exec
INFO: Reading rc options for 'build' from /home/ysj/pkg/tensorflow-2.4.1/.bazelrc:
'build' options: --apple_platform_type=macos --define framework_shared_object=true --define open_source_build=true --java_toolchain=//third_party/toolchains/java:tf_java_toolchain --host_java_toolchain=//third_party/toolchains/java:tf_java_toolchain --define=tensorflow_enable_mlir_generated_gpu_kernels=0 --define=use_fast_cpp_protos=true --define=allow_oversize_protos=true --spawn_strategy=standalone -c opt --announce_rc --define=grpc_no_ares=true --noincompatible_remove_legacy_whole_archive --noincompatible_prohibit_aapt1 --enable_platform_specific_config --config=short_logs --config=v2
INFO: Reading rc options for 'build' from /home/ysj/pkg/tensorflow-2.4.1/.tf_configure.bazelrc:
'build' options: --action_env PYTHON_BIN_PATH=/usr/bin/python3 --action_env PYTHON_LIB_PATH=/usr/lib/python3/dist-packages --python_path=/usr/bin/python3 --config=xla --action_env TF_CONFIGURE_IOS=0
INFO: Found applicable config definition build:short_logs in file /home/ysj/pkg/tensorflow-2.4.1/.bazelrc: --output_filter=DONT_MATCH_ANYTHING
INFO: Found applicable config definition build:v2 in file /home/ysj/pkg/tensorflow-2.4.1/.bazelrc: --define=tf_api_version=2 --action_env=TF2_BEHAVIOR=1
INFO: Found applicable config definition build:xla in file /home/ysj/pkg/tensorflow-2.4.1/.bazelrc: --define=with_xla_support=true
INFO: Found applicable config definition build:opt in file /home/ysj/pkg/tensorflow-2.4.1/.tf_configure.bazelrc: --copt=-Wno-sign-compare --host_copt=-Wno-sign-compare --define with_default_optimizations=true
INFO: Found applicable config definition build:linux in file /home/ysj/pkg/tensorflow-2.4.1/.bazelrc: --copt=-w --host_copt=-w --define=PREFIX=/usr --define=LIBDIR=$(PREFIX)/lib --define=INCLUDEDIR=$(PREFIX)/include --define=PROTOBUF_INCLUDE_PATH=$(PREFIX)/include --cxxopt=-std=c++14 --host_cxxopt=-std=c++14 --config=dynamic_kernels
INFO: Found applicable config definition build:dynamic_kernels in file /home/ysj/pkg/tensorflow-2.4.1/.bazelrc: --define=dynamic_loaded_kernels=true --copt=-DAUTOLOAD_DYNAMIC_KERNELS
Analyzing: target //tensorflow:libtensorflow_cc.so (207 packages loaded, 13900 targets configured)
WARNING: Download from https://storage.googleapis.com/mirror.tensorflow.org/github.com/llvm/llvm-project/archive/f402e682d0ef5598eeffc9a21a691b03e602ff58.tar.gz failed: class com.google.devtools.build.lib.bazel.repository.downloader.UnrecoverableHttpException GET returned 404 Not Found
INFO: Analyzed target //tensorflow:libtensorflow_cc.so (211 packages loaded, 19961 targets configured).
INFO: Found 1 target...
Target //tensorflow:libtensorflow_cc.so up-to-date:
bazel-bin/tensorflow/libtensorflow_cc.so
INFO: Elapsed time: 8528.702s, Critical Path: 193.83s
INFO: 8631 processes: 8631 local.
INFO: Build completed successfully, 8992 total actions
成功后,会生成一些个目录:
# ls
ACKNOWLEDGMENTS AUTHORS bazel-out bazel-testlogs CODE_OF_CONDUCT.md configure configure.py ISSUES.md LICENSE README.md SECURITY.md third_party WORKSPACE
arm_compiler.BUILD bazel-bin bazel-tensorflow-2.4.1 BUILD CODEOWNERS configure.cmd CONTRIBUTING.md ISSUE_TEMPLATE.md models.BUILD RELEASE.md tensorflow tools
可以看到,多出来的几个目录分别是:bazel-out bazel-testlogs bazel-bin bazel-tensorflow-2.4.1
。
在tensorflow根目录的bazel-bin/tensorflow
文件夹下出现libtensorflow_cc.so
以及libtensorflow_framework.so
等文件(有些是软链接):
# ls bazel-bin/tensorflow/
c compiler libtensorflow_cc.so libtensorflow_cc.so.2.4.1 libtensorflow_framework.so.2 libtensorflow_framework.so.2.4.1-2.params
cc core libtensorflow_cc.so.2 libtensorflow_cc.so.2.4.1-2.params libtensorflow_framework.so.2.4.1 stream_executor
建议将这些文件拷贝到 /usr/local/lib
。
把根目录的tensorflow文件夹拷贝到include目录使用,如 /usr/local/include
。
至此,tf2.4的库文件和头文件已准备完毕。
外部依赖包
但是在实际开发应用时,可能会遇到各种问题,下面记录一下我遇到的问题。
找不到头文件。遇到这样的问题可以使用find . -name file_name
搜索一下是否存在该文件,如果存在,则把目录添加到include即可。
如果不存在,则查看是否是相关的依赖库未安装。
可在 tensorflow/workspace.bzl
文件查看依赖包的版本。
如:
- absl
- protobuf
- eigen3
根据报错情况,依次解决即可。