Windows 10 环境下编译 Tensorflow 1.12 GPU 版本指南

编译环境


Thinkapd X1 Carbon 2018
CPU:Intel i7-8550U @ 1.8GHz
Memory:16GB DDR @ 2133MHz
Storage:Samsung 512GB SSD
Graphics:AORUS RTX 2070 Gaming Box(eGPU)
OS:Windows 10 Build 1809
CUDA:10.1
cuDNN:7.5.0 for CUDA 10.1
Compiler:Visual Studio 2015 with Update 3

Step 1:安装、配置 Anaconda


安装 Anaconda x64 最新版。

添加清华开源镜像为 conda 的源(可选)

conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/free/
conda config --add channels https://mirrors.tuna.tsinghua.edu.cn/anaconda/pkgs/main/
conda config --set show_channel_urls yes

Tips: 清华开源镜像下载速度会快很多。

创建一个编译工作环境并激活

conda create --name tf-compile python=3.6
conda activate tf-compile

Tips: Tensorflow 1.12 兼容的 Python 版本可以是 3.5 或者 3.6。

Step 2:安装编译工具及依赖项


安装 Bazel

Bazel 是用于编译 Tensorflow 的构建工具(类似于 Make、Ant、Gradle、Buck、Pants和Maven)。

conda install Bazel=0.18.0 --name tf-compile

Tips:
新版的 Bazel(0.22.0) 和 Tensorflow 1.12 源码构建配置文件不兼容,会导致 “ERROR: Config value cuda is not defined in any .rc file” 错误,务必使用 Bazel 0.18.0。

安装 Tensorflow 软件包依赖

conda activate tf-compile
conda install six numpy wheel
pip install keras_applications==1.0.5 --no-deps
pip install keras_preprocessing==1.0.3 --no-deps

Tips:

  • 如果 pip 安装速度很慢或不可(被)用(墙),可以选择 “清华开源镜像” 来加速。
    pip install keras_applications==1.0.5 --no-deps -i https://pypi.tuna.tsinghua.edu.cn/simple
    pip install keras_preprocessing==1.0.3 --no-deps -i https://pypi.tuna.tsinghua.edu.cn/simple

安装 MSYS2 x64

MSYS2 x64 提供了 Windows 环境下 Linux 命令运行环境。如果 MSYS2 已安装到 C:\msys64 下,请将 C:\msys64\usr\bin 添加到 %PATH% 环境变量中。

安装 unzip

MSYS2 没有内置 unzip,需手动安装。使用 cmd.exe 运行以下命令:

pacman -S git patch unzip

Tips:

  • 如果 pacman 安装速度很慢或不可(被)用(墙),可以选择 “清华开源镜像” 来加速(加速方法)。

安装 Visual C++ 生成工具 2015

安装 Visual C++ 生成工具 2015。此软件包随附在 Visual Studio 2015 中,但可以单独安装:

  1. 转到 Visual Studio 下载页面
  2. 选择“可再发行组件和生成工具”,
  3. 下载并安装:
    Microsoft Visual C++ 2015 Redistributable 更新 3
    Microsoft 生成工具 2015 更新 3

注意:TensorFlow 1.12 针对 Visual Studio 2015 更新 3 进行了测试。

安装 CUDA Toolkit

既然决定了自行编译,当然选择最新版本的 CUDA Toolkit,当前最新版为:CUDA Toolkit v10.1。

Tips:

  • 很多指南指出(包括 CUDA 官方安装指南):安装完成后,通过编译、运行 deviceQuery 来验证是否正确安装了 CUDA。事实上,deviceQuery.exe 在 extra\demo_suite 目录下已经有编译好的版本,可以直接运行验证。
  • CUDA Toolkit 中包含了 Graphics Driver(图形卡驱动),这个驱动往往不是最新的,但是 “精简” 安装模式下会覆盖你现有的驱动,因此安装完毕后,更新一下驱动到最新版。
  • 最坑的是 CUDA Toolkit v10.1 中包含的 418.96 版驱动程序,居然不兼容我的 RTX 2070 显卡,更新到 419.17 版才解决问题。因此,安装完 CUDA 后,更新图形卡驱动非常重要。

安装 cuDNN

从官方网站下载 cuDNN 需要注册 NVIDIA developer 账号,注册过程还算简单。

安装 cuDNN 只需要将压缩包中所有文件和文件夹,解压到 CUDA Toolkit 的安装文件夹中。CUDA v10.1 的安装文件夹为:C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.1,将 cuDNN 中的文件,解压到对应的 bin、include、lib\x64 文件夹中。

Tips: cuDNN 下载时要注意版本对应关系,cuDNN 7.5.0 分别有 for CUDA 10.1、10.0、9.2、9.0 四个版本。

Step 3:下载 Tensorflow 源码,并打补丁


下载 Tensorflow 源码

可以用 git clone 指令来下载 Tensorflow 源码,但是由于不可描述的网络原因,直接在 Github 上下载更快、更便捷。

下载后解压到任意目录。

Eigen 补丁(强制性)

由于 Eigen 的一个 issue,导致:如果打开 Eigen 编译选项,在编译中会导致 “error : more than one instance of overloaded function “__hadd” matches the argument list:” 错误,因此,需要对源码打补丁。

关于这个补丁的官方解释:

打补丁的方法:

  • 下载 patch,并保存为文件:eigen_half.patch
  • eigen_half.patch 拷贝到 third_party 目录下。
  • 编辑 tensorflow/workspace.bzl 文件,找到 eigen_archive 节点,在这个节点下增加一行:
    patch_file = clean_dep(“//third_party:eigen_half.patch”),

最终 tensorflow/workspace.bzl 修改的结果大致如下:

...
    tf_http_archive(
        name = "eigen_archive",
        patch_file = clean_dep("//third_party:eigen_half.patch"),
        build_file = clean_dep("//third_party:eigen.BUILD"),
        sha256 = "d956415d784fa4e42b6a2a45c32556d6aec9d0a3d8ef48baee2522ab762556a9",
        strip_prefix = "eigen-eigen-fd6845384b86",
        urls = [
            "https://mirror.bazel.build/bitbucket.org/eigen/eigen/get/fd6845384b86.tar.gz",
            "https://bitbucket.org/eigen/eigen/get/fd6845384b86.tar.gz",
        ],
    )
...

Step4:配置编译参数


启动参数配置

在 Tensorflow 源码的根目录下,执行以下指令,开始配置过程:

python ./configure.py

配置选项、回答及解释

Please specify the location of python. [Default is C:\Users\rlee\Anaconda3\envs\tf-c\python.exe]:


Found possible Python library paths:
  C:\Users\rlee\Anaconda3\envs\tf-c\lib\site-packages
Please input the desired Python library path to use.  Default is [C:\Users\rlee\Anaconda3\envs\tf-c\lib\site-packages]

解释:选项一、二,是确定 Python 以及 Python library 的安装位置。
回答:直接 “Return”,Default 目录就 OK。

Do you wish to build TensorFlow with Apache Ignite support? [Y/n]: n
No Apache Ignite support will be enabled for TensorFlow.

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

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

解释:选项三、四、五,是否需要支持 Apache Ignite、XLA JIT、ROCm。
回答:n,不需要。

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]: 10.1

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

Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7]: 7.5.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/v10.1]:

解释:选项六 ~ 十,是否需要支持 CUDA,以及 CUDA toolkit、cuDNN 的版本、安装位置。
回答:y,需要支持 CUDA;10.1,CUDA 版本号;“Return”,CUDA 安装的 Default 目录;7.5.0,cuDNN 版本号;“Return”,cuDNN 安装的 Default 目录。

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]: 7.5

解释:确定使用的 NVIDIA GPU 算力值,这个值根据显卡型号,在 https://developer.nvidia.com/cuda-gpus 网址下查询。
回答:7.5,RTX 2070 的算力值是 7.5。

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]: /arch:AVX2

解释:CPU 是否支持 AVX、AVX2 指令集。
回答:/arch:AVX2,酷睿2代之后的 CPU 都支持 AVX2 指令集,选择它可以稍微加快运行速度。

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

解释:对 eigen 的编译选项,可加快编译速度。
回答:y

Step 5:编译


启动编译

在 Tensorflow 源码的根目录下,执行以下指令,开始编译过程:

bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

如果编译成功,最终会得到大致如下的输出:

INFO: Elapsed time: 4759.367s, Critical Path: 811.76s
INFO: 4490 processes: 4490 local.
INFO: Build completed successfully, 5975 total actions

编译时间

在我的编译环境下,编译一次大约:1 小时 21 分钟。

构建软件包

编译成功后,在 Tensorflow 源码的根目录下,执行以下指令,构建软件包:

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

以上命令会在 C:/tmp/tensorflow_pkg 目录中构建 .whl 软件包。

安装软件包

在 Anaconda 环境中,执行以下指令,安装软件包:

conda create --name tf python=3.6
conda activate tf
pip install c:\tmp\tensorflow_pkg\tensorflow-version-cp36-cp36m-win_amd64.whl

测试软件包

conda activate tf
python verify_tensorflow_gpu.py

如果编译、安装成功,将看到以下输出:

TensorFlow successfully installed.
The installed version of TensorFlow includes GPU support.
  • 简单测试也可以用以下 python 脚本:
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
session = tf.Session()
print(session.run(hello))

如果编译、安装成功,将看到大致如下的输出,说明找到了 GPU:

2019-03-06 21:14:15.963957: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties:
name: GeForce RTX 2070 major: 7 minor: 5 memoryClockRate(GHz): 1.62
pciBusID: 0000:3e:00.0
totalMemory: 8.00GiB freeMemory: 6.59GiB
2019-03-06 21:14:15.971147: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0
2019-03-06 21:14:17.382103: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix:
2019-03-06 21:14:17.388240: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988]      0
2019-03-06 21:14:17.391058: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0:   N
2019-03-06 21:14:17.395849: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 6329 MB memory) -> physical GPU (device: 0, name: GeForce RTX 2070, pci bus id: 0000:3e:00.0, compute capability: 7.5)

汇总:遇到的问题及解决方案


1. 使用 conda、pip、pcsman 安装时,下载速度很慢,怎么解决?

使用清华开源镜像可以解决速度慢,或(被)(墙)的问题。使用指南参见:

2. 需要设置哪些环境变量,怎么设置?

本指南使用了 Anaconda 来构建编译环境,大部分软件包是通过 condapip 进行安装的;同时,CUDA Toolkit 和 vs2015 在安装过程中,安装程序自动添加了 %PATH% 环境变量,因此,几乎无需手动设置任何环境变量!只有一个 例外:MSYS2 x64 无法通过 Anaconda 进行安装,需要将 C:\mysys64\usr\bin 添加到 %PATH% 环境变量中。

3. 如何解决 “ERROR: Config value cuda is not defined in any .rc file” 错误?

编译的时候,可能会遇到 “ERROR: Config value cuda is not defined in any .rc file” 错误,这是由于 Bazel 新版与 Tensorflow 源码构建配置文件不兼容导致的。解决方法有两种:

  • 修改构建配置文件。在 “.bazel” 文件的第一行,增加一行 import ./tools/bazel.rc
  • 最简单的方案:安装 Bazel 0.18.0 版。

4. 如何解决下载第三方软件包错误的问题?

编译的开始阶段,编译脚本将自动下载一些需要的第三方软件包(有大约几十个左右)。由于网速慢、以及一些不可描述的网络原因,下载过程可能超时,导致编译失败。

我的解决方案是:

  • 在编译输出中,找出哪个软件包下载失败。
  • 下载对应的软件包。
  • 启动一个本地的 Web Server(推荐 miniweb,一个很小的 portable web server),在本地发布这些软件包。
  • 修改对应第三方软件包的 “workspace.bzl” 文件。

我编译过程中:icu 软件包总是下载失败。修改:thrid_party/icu/workspace.bzl 文件,增加一行:"http://localhost:8000/release-62-1.tar.gz",

修改完成的文件内容,大致如下:

"""Loads a lightweight subset of the ICU library for Unicode processing."""

load("//third_party:repo.bzl", "third_party_http_archive")

def repo():
    third_party_http_archive(
        name = "icu",
        strip_prefix = "icu-release-62-1",
        sha256 = "e15ffd84606323cbad5515bf9ecdf8061cc3bf80fb883b9e6aa162e485aa9761",
        urls = [
            "http://localhost:8000/release-62-1.tar.gz",
            "https://mirror.bazel.build/github.com/unicode-org/icu/archive/release-62-1.tar.gz",
            "https://github.com/unicode-org/icu/archive/release-62-1.tar.gz",
        ],
        build_file = "//third_party/icu:BUILD.bazel",
    )

5. 如何解决 “error : more than one instance of overloaded function “__hadd” matches the argument list:” 错误?

给源码打上 eigen_half 补丁。详见:Eigen 补丁(强制性)

  • 4
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
编译tensorflow: 1.python3.5,tensorflow1.12; 2.支持cuda10.0,cudnn7.3.1,TensorRT-5.0.2.6-cuda10.0-cudnn7.3; 3.支持mkl,无MPI; 软硬件硬件环境:Ubuntu16.04,GeForce GTX 1080 配置信息: hp@dla:~/work/ts_compile/tensorflow$ ./configure WARNING: --batch mode is deprecated. Please instead explicitly shut down your Bazel server using the command "bazel shutdown". You have bazel 0.19.1 installed. Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3 Found possible Python library paths: /usr/local/lib/python3.5/dist-packages /usr/lib/python3/dist-packages Please input the desired Python library path to use. Default is [/usr/local/lib/python3.5/dist-packages] Do you wish to build TensorFlow with XLA JIT support? [Y/n]: XLA JIT support will be enabled for TensorFlow. Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: No OpenCL SYCL support will be enabled for TensorFlow. 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]: y CUDA support will be enabled for TensorFlow. Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 10.0]: Please specify the location where CUDA 10.0 toolkit is installed. Refer to README.md for more details. [Default is /usr/local/cuda]: /usr/local/cuda-10.0 Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7]: 7.3.1 Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda-10.0]: Do you wish to build TensorFlow with TensorRT support? [y/N]: y TensorRT support will be enabled for TensorFlow. Please specify the location where TensorRT is installed. [Default is /usr/lib/x86_64-linux-gnu]:/home/hp/bin/TensorRT-5.0.2.6-cuda10.0-cudnn7.3/targets/x86_64-linux-gnu Please specify the locally installed NCCL version you want to use. [Default is to use https://github.com/nvidia/nccl]: 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: 6.1,6.1,6.1]: Do you want to use clang as CUDA compiler? [y/N]: nvcc will be used as CUDA compiler. Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: Do you wish to build TensorFlow with MPI support? [y/N]: No MPI support will be enabled for TensorFlow. Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native -Wno-sign-compare]: Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: Not configuring the WORKSPACE for Android builds. 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=gdr # Build with GDR support. --config=verbs # Build with libverbs support. --config=ngraph # Build with Intel nGraph support. --config=dynamic_kernels # (Experimental) Build kernels into separate shared objects. 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=noignite # Disable Apacha Ignite support. --config=nokafka # Disable Apache Kafka support. --config=nonccl # Disable NVIDIA NCCL support. Configuration finished 编译: hp@dla:~/work/ts_compile/tensorflow$ bazel build --config=opt --config=mkl --verbose_failures //tensorflow/tools/pip_package:build_pip_package 卸载已有tensorflow: hp@dla:~/temp$ sudo pip3 uninstall tensorflow 安装自己编译的成果: hp@dla:~/temp$ sudo pip3 install tensorflow-1.12.0-cp35-cp35m-linux_x86_64.whl
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值