龙空技术网

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

蓝天剑客 115

前言:

而今兄弟们对“查看whl依赖”都比较注重,姐妹们都想要了解一些“查看whl依赖”的相关知识。那么小编同时在网摘上搜集了一些关于“查看whl依赖””的相关资讯,希望兄弟们能喜欢,同学们快快来了解一下吧!

(续上篇)

下载 TensorFlow 源代码下载TensorFlow master源码

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

git clone

cd tensorflow

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

代码路径

git clone --recurse-submodules

其中–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:\python36\python.exe]:

Found possible Python library paths:

C:\python36\lib\site-packages

Please input the desired Python library path to use. Default is [C:\python36\lib\site-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:\tools\cuda

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:

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-bin\tensorflow\tools\pip_package\build_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:\SoftWare\Anaconda3\envs\pythonVersion3\python.exe]:

Found possible Python library paths:

D:\SoftWare\Anaconda3\envs\pythonVersion3\lib\site-packages

Please input the desired Python library path to use. Default is [D:\SoftWare\Anaconda3\envs\pythonVersion3\lib\site-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软件包

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

bazel-bin\tensorflow\tools\pip_package\build_pip_package C:/tmp/tensorflow_pkg

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

实例:

bazel-bin\tensorflow\tools\pip_package\build_pip_package D:/thirdparty/tensorflow/tensorflow_pkg

安装及测试

生成的 .whl 文件的文件名取决于 TensorFlow 版本和您的平台。例如,使用 pip3 install 安装软件包。

pip3 install C:/tmp/tensorflow_pkg/tensorflow-version-cp36-cp36m-win_amd64.whl

联网在线安装依赖包

生成的tensorflow的whl安装包安装时需要其他依赖包的支持,如果在联网的情况下,可以直接利用pip指令安装:

pip3 install C:/tmp/tensorflow_pkg/tensorflow-version-cp36-cp36m-win_amd64.whl

联网下载依赖包,离线安装

如果安装环境不具备联网条件,则可以先联网下载依赖包,然后离线安装tensorflow及其依赖包。

查看tensorflow.whl的依赖项

查看whl安装文件的依赖项需要使用pkginfo工具(需先安装)。

执行命令:

pkginfo -f requires_dist ./tensorflow_pkg/tensorflow-2.5.1-cp38-cp38-win_amd64.whl

将依赖项整理成requirements.txt文件

requirements.txt文件每行一个软件包名称及版本。

利用pip download批量下载依赖包

pip download -d ./tensorflow_pkg/deps -r ./tensorflow_pkg/requirements.txt

pip download下载时会将依赖包的依赖包一起下载。

利用pip install批量安装下载的离线依赖包

pip install --no-index --find-links=./tensorflow_pkg/deps -r ./tensorflow_pkg/requirements.txt

备注:批量卸载命令为

pip uninstall -r ./tensorflow_pkg/requirements.txt -y

利用pip install安装本地的tensorflow.whl

pip install ./tensorflow_pkg/tensorflow-2.5.1-cp38-cp38-win_amd64.whl

测试

(1)首先验证tensorflow的能够正常导入:

python

import tensorflow as tf

(2)查看tensorflow版本:

tf.__version__

(2)验证tensorflow gpu是否支持

tf.test.is_built_with_cuda()

(未完待续)

标签: #查看whl依赖