Windows 环境TensorFlow源码C++编译———实战与避坑记(二)

(续上篇)

下载 TensorFlow 源代码

下载TensorFlow master源码

使用 Git 克隆 TensorFlow 代码库(git 随 MSYS2 一起安装):

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

cd tensorflow

该代码库默认为 master 开发分支。

代码路径
https://github.com/tensorflow/tensorflow/

git clone –recurse-submodules https://github.com/tensorflow/tensorflow

其中–recurse-submodules 参数是必须的, 用于获取 TesorFlow 依赖的 protobuf 库.

检出待编译版本分支源码

您也可以检出想要构建的版本分支

git checkout branch_name # r1.9, r1.10, etc.

git checkout r2.5

要点:如果您在使用最新的开发分支时遇到构建问题,请尝试已知可用的版本分支。

配置

配置命令

通过在 TensorFlow 源代码树的根目录下运行以下命令来配置系统构建:

python ./configure.py

在同一个源代码树中的不同配置之间进行切换时运行 bazel clean

为了支持 GPU,请指定 CUDA 和 cuDNN 的版本。如果您的系统安装了多个版本的 CUDA 或 cuDNN,请明确设置版本,而不是依赖于默认版本。./configure.py 会创建指向系统 CUDA 库的符号链接,因此,如果您更新 CUDA 库路径,就必须在构建之前再次运行此配置步骤。

注意从 TensorFlow 1.6 开始,二进制文件使用 AVX 指令,这些指令可能无法在旧版 CPU 上运行。

此脚本会提示您指定 TensorFlow 依赖项的位置,并要求指定其他构建配置选项(例如,编译器标记)。以下代码展示了 python ./configure.py 的示例运行会话(您的会话可能会有所不同):

配置参数解释

重要参数解释

Please specify the location of python. [Default is /usr/bin/python]: /usr/local/bin/python3

上面的提示是Bazel让我们选择Python的安装路径,这里输入了python3的路径。直接按回车键(Enter)表示使用默认值。

Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]:

jemalloc as malloc support will be enabled for TensorFlow.

上面的选项表示是否使用jemalloc代替传统的malloc来管理内存?Jemalloc是杰森·埃文斯(Jason Evans)于2006年开发的用以取代传统低性能的malloc内存管理模块而开发的一款内存管理模块[4]。埃文斯并非等闲之辈,他是FreeBSD项目(一种类UNIX操作系统)的重要维护者之一。

Jemalloc先被Firefox浏览器采用,后来又被Facebook在其自己的各类应用上广泛使用,一战成名。好技术当然要用!直接按回车,确认默认值Y。

Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]: n

No Google Cloud Platform support will be enabled for TensorFlow.

这个选项是询问是否采用Google云平台来支持TensorFlow。这个云平台国内通常无法访问,建议输入“n”。有条件的读者,可直接按回车确认使用。

Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: n

No Hadoop File System support will be enabled for TensorFlow.

这个选项是询问是否使用Hadoop 文件系统(HDFS)来支持TensorFlow。如果搭建了Hadoop集群,有读取HDFS数据需求的用户,可以回车确认。如果没有需求,手动输入“n”。

Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]: n

No Amazon S3 File System support will be enabled for TensorFlow.

类似的,这个选项是询问TensorFlow是否支持亚马逊的S3文件系统。读者根据自己的需要来确定“Y”或“n”。如果用不着,建议选择“n”。

Do you wish to build TensorFlow with Apache Kafka Platform support? [y/N]:

No Apache Kafka Platform support will be enabled for TensorFlow.

Kafka是由Apache软件基金会开发的一个开源流处理平台,是一种高吞吐量的分布式发布订阅消息系统。如果没有这个需要,建议选择默认值“N”。

Do you wish to build TensorFlow with XLA JIT support? [y/N]: n

No XLA JIT support will be enabled for TensorFlow.

这个选项是询问是否开启XLA JIT编译支持。XLA(Accelerated Linear Algebra/加速线性代数)目前还是TensorFlow的实验项目,XLA 使用 JIT(Just in Time,即时编译)技术来分析用户在运行时(runtime)创建的 TensorFlow 图,专门用于实际运行时的维度和类型。作为新技术,这项编译技术还不成熟,爱折腾的“极客”读者可以选“y”,否则选择默认值“N”。

Do you wish to build TensorFlow with CUDA support? [y/N]:

No CUDA support will be enabled for TensorFlow.

这个选项是询问是否使用CUDA。CUDA是一种由NVIDIA推出的通用并行计算架构,该架构使GPU能够解决复杂的计算问题。如果用户配备有NVIDIA的GPU,可以选择“y”,如果仅使用TensorFlow的CPU版本,回车确认“N”。

nvcc –version 可查询CUDA的版本

Do you wish to build TensorFlow with MPI support? [y/N]:

No MPI support will be enabled for TensorFlow.

这个选项是询问是否使用MPI。MPI(Message-Passing-Interface 消息传递接口)是实现进程级别的并行程序的通信协议,它通过在进程之间进行消息传递。如果不是基于TensorFlow做并行程序开发,建议回车确认选择默认值“N”。

Please specify optimization flags to use during compilation when bazel option “–config=opt” is specified [Default is -march=native]:

这个选项是指定CPU编译优化选项。默认值就是“-march=native”。这里“m”表示“machine(机器)”,“arch”就是“architecture”简写。“march”合在一起表示机器的结构,如果选择“-march=native”,则表示选择本地(native)CPU,如果本地CPU比较高级,就可以支持SSE4.2、AVX等选项。这里建议选择默认值。

Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]:

这个选项是问是否进入Android的工作空间配置,如果不用手机版的TensorFlow开发,则选择默认值“N”。

之后,当显示“Configuration finished”(配置完成)字样,则表示配置顺利完成。

查看示例配置会话

python ./configure.py

Starting local Bazel server and connecting to it…

…………….

You have bazel 0.15.0 installed.

Please specify the location of python. [Default is C:python36python.exe]:

Found possible Python library paths:

C:python36libsite-packages

Please input the desired Python library path to use. Default is [C:python36libsite-packages]

Do you wish to build TensorFlow with CUDA support? [y/N]: Y

CUDA support will be enabled for TensorFlow.

Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]:

Please specify the location where CUDA 9.0 toolkit is installed. Refer to README.md for more details. [Default is C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0]:

Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]: 7.0

Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0]: C:toolscuda

Please specify a list of comma-separated Cuda compute capabilities you want to build with.

You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus

Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 3.5,7.0]: 3.7

Please specify optimization flags to use during compilation when bazel option “–config=opt” is specified [Default is /arch:AVX]:

Would you like to override eigen strong inline for some C++ compilation to reduce the compilation time? [Y/n]:

Eigen strong inline overridden.

Configuration finished

构建

构建Pip软件包

bazel build 命令会创建一个名为 build_pip_package 的可执行文件,此文件是用于构建 pip 软件包的程序。例如,以下命令会在 C:/tmp/tensorflow_pkg 目录中构建 .whl 软件包:

bazel-bintensorflowtoolspip_packagebuild_pip_package C:/tmp/tensorflow_pkg

尽管可以在同一个源代码树下构建 CUDA 和非 CUDA 配置,但建议在同一个源代码树中的这两种配置之间切换时运行 bazel clean。

TensorFlow 2.x

tensorflow:master 代码库已经默认更新为 build 2.x。请安装 Bazel 并使用 bazel build 创建 TensorFlow 软件包。

bazel build //tensorflow/tools/pip_package:build_pip_package

TensorFlow 1.x

如需从 master 分支构建 TensorFlow 1.x,请使用 bazel build –config=v1 创建 TensorFlow 1.x 软件包。

bazel build –config=v1 //tensorflow/tools/pip_package:build_pip_package

仅支持 CPU

使用 bazel 构建仅支持 CPU 的 TensorFlow 软件包构建器:

bazel build –config=opt //tensorflow/tools/pip_package:build_pip_package

GPU 支持

要构建支持 GPU 的 TensorFlow 软件包编译器,请运行以下命令:

bazel build –config=opt –config=cuda –define=no_tensorflow_py_deps=true //tensorflow/tools/pip_package:build_pip_package

实例

bazel build –config=opt –config=cuda –define=no_tensorflow_py_deps=true –copt=-DTHRUST_IGNORE_CUB_VERSION_CHECK –copt=-nvcc_options=disable-warnings –local_ram_resources=10240 –local_cpu_resources=3

Bazel 构建选项

在构建时使用以下选项,避免在创建软件包时出现问题:tensorflow:issue#22390

–define=no_tensorflow_py_deps=true

请参阅 Bazel 命令行参考文档,详细了解构建选选项

从源代码构建 TensorFlow 会消耗大量 RAM。如果您的系统受内存限制,请将 Bazel 的 RAM 使用量限制为:

–local_ram_resources=2048

编译时很耗费CPU资源,如果还想在编译时使用电脑干其他事情,就需要对CPU资源进行限制(下面选项限制只使用CPU的三个独立核):

–local_cpu_resources=3

Windows系统中,在cmd中输入命令“wmic”。

在wmic命令提示符后输入:cpu get *

NumberOfCores表示CPU内核数

NumberOfLogicalProcessors表示CPU线程数

如果构建支持 GPU 的 TensorFlow,请添加

–copt=-nvcc_options=disable-warnings

以禁止显示 nvcc 警告消息。

补充:

编译过程会下载第三方库,放到output_user_root

***此部分的库不要删掉***

编译tensorflow_cc.dll :

bazel –output_user_root=”…/output_user_root” build –noincompatible_do_not_split_linking_cmdline –verbose_failures –config=opt //tensorflow:tensorflow_cc.dll

编译tensorflowlite.dll :

bazel –output_user_root=”…/output_user_root” build –noincompatible_do_not_split_linking_cmdline –verbose_failures –config=opt //tensorflow/lite:tensorflowlite.dll

编译android tflite:

python configure.py

You have bazel 3.2.0 installed.
Please specify the location of python. [Default is D:SoftWareAnaconda3envspythonVersion3python.exe]:

Found possible Python library paths:
D:SoftWareAnaconda3envspythonVersion3libsite-packages
Please input the desired Python library path to use. Default is [D:SoftWareAnaconda3envspythonVersion3libsite-packages]

Do you wish to build TensorFlow with ROCm support? [y/N]:
No ROCm support will be enabled for TensorFlow.

Do you wish to build TensorFlow with CUDA support? [y/N]:
No CUDA support will be enabled for TensorFlow.

Please specify optimization flags to use during compilation when bazel option “–config=opt” is specified [Default is /arch:AVX]:

Would you like to override eigen strong inline for some C++ compilation to reduce the compilation time? [Y/n]:
Eigen strong inline overridden.

Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: y
Searching for NDK and SDK installations.

Please specify the home path of the Android NDK to use. [Default is C:/Users/Admin/AppData/Roaming/Android/Sdk/ndk-bundle]: D:/SoftWare/Android/Sdk/ndk/21.3.6528147

WARNING: The NDK version in D:/SoftWare/Android/Sdk/ndk/21.3.6528147 is 21, which is not supported by Bazel (officially supported versions: [10, 11, 12, 13, 14, 15, 16, 17, 18]). Please use another version. Compiling Android targets may result in confusing errors.

Please specify the (min) Android NDK API level to use. [Available levels: [‘16’, ‘17’, ‘18’, ‘19’, ‘21’, ‘22’, ‘23’, ‘24’, ‘26’, ‘27’, ‘28’, ‘29’, ‘30’]] [Default is 21]: 16

Please specify the home path of the Android SDK to use. [Default is C:/Users/Admin/AppData/Roaming/Android/Sdk]: D:/SoftWare/Android/Sdk

Please specify the Android SDK API level to use. [Available levels: [‘30’, ‘30.backup’]] [Default is 30.backup]: 30

Please specify an Android build tools version to use. [Available versions: [‘30.0.0’]] [Default is 30.0.0]:

Preconfigured Bazel build configs. You can use any of the below by adding “–config=<>” to your build command. See .bazelrc for more details.

–config=mkl # Build with MKL support.

–config=monolithic # Config for mostly static monolithic build.

–config=ngraph # Build with Intel nGraph support.

–config=numa # Build with NUMA support.

–config=dynamic_kernels # (Experimental) Build kernels into separate shared objects.

–config=v2 # Build TensorFlow 2.x instead of 1.x.

Preconfigured Bazel build configs to DISABLE default on features:

–config=noaws # Disable AWS S3 filesystem support.

–config=nogcp # Disable GCP support.

–config=nohdfs # Disable HDFS support.

–config=nonccl # Disable NVIDIA NCCL support.

bazel build -c opt –cxxopt=–std=c++11 –config=android_arm64 //tensorflow/lite/c:tensorflowlite_c OK

bazel –output_user_root=”…/output_user_root” build -c opt –cxxopt=”–std=c++11″ –host_crosstool_top=@bazel_tools//tools/cpp:toolchain –cpu=armeabi-v7a –config=android_arm //tensorflow/lite/java:tensorflowlite

bazel –output_user_root=”…/output_user_root” build –cxxopt=”–std=c++11″ -c opt –host_crosstool_top=@bazel_tools//tools/cpp:toolchain –fat_apk_cpu=armeabi-v7a –config=android_arm //tensorflow/lite/java:tensorflow-lite

D:/SoftWare/Android/Sdk/ndk/21.3.6528147

D:/SoftWare/Android/Sdk

D:/SoftWare/Android/Sdk/ndk/android-ndk-r18b

D:/SoftWare/Android/Sdk

D:/SoftWare/Android/Sdk/ndk/android-ndk-r12b

external/androidndk/ndk/toolchains/arm-linux-androideabi-4.9/prebuilt/windows-x86_64/lib/gcc/arm-linux-androideabi/4.9.x/…/…/…/…/arm-linux-androideabi/lib/…/lib/armv7-a/libatomic.a

bazel build -c opt –copt=-msse4.1 –copt=-msse4.2 –copt=-mavx –copt=-mavx2 –copt=-mfma –copt=-O3 //tensorflow/tools/pip_package:build_pip_package

生成.whl软件包

声明:本站部分文章内容及图片转载于互联 、内容不代表本站观点,如有内容涉及侵权,请您立即联系本站处理,非常感谢!

(0)
上一篇 2022年4月14日
下一篇 2022年4月14日

相关推荐