在Windows*上极速飞艇平台定制开发编译Tensorflow教程

最简单极速飞艇平台定制开发【大神源码论坛】dsluntan.com 【布丁源码论坛】budingbbs.com 企娥3393756370的 Tensorflow 的安装方法是在 pip 一键式安装官方预编译好的包

pip install tensorflow

通常这种预编译的包的编译参数选择是为了最大兼容性而不是为了最优性能,导致在使用过程中,每次运行代码都会输出一大堆的 warning 信息。例如在安装了谷歌官方的 Tensorflow 1.3.0 包后,运行以下测试代码时

import tensorflow as tf
 
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

Console 会输出

C:\Users\jgu\Anaconda3\python.exe C:/Users/sandman/PycharmProjects/untitled/tf_helloworld.py
2017-10-27 13:42:20.005261: W C:\work\tensorflow-1.3.1\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.
2017-10-27 13:42:20.005475: W C:\work\tensorflow-1.3.1\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
b'Hello, TensorFlow!'
 
Process finished with exit code 0

表示此 Tensorflow 版本只用到了 CPU SSE 指令集优化,可以运行在很多老架构的 CPU 指令集上。
为了充分利用 AVX/AVX2 来加速 Tensorflow 的 CPU 版本的计算速度,需要自己下载 Tensorflow 的源码,在编译时使用这些指令集。
以下教程基于最新的Tensorflow 1.3.1源码,利用用Visual Studio 2015/Visual Studio 2017来编译一个基于 AVX/AVX2 的 CPU 版本的 Tensorflow Python 安装包。

编译过程

准备工作

VS2015编译过程

  • 打开 VS2015 64 位命令行编译环境
  • 在命令行环境中进入 Tensorflow 的源码路径 tensorflow-1.3.1\tensorflow\contrib\cmake
    新建一个文件夹 build2015,进入 build2015 文件夹
  • 用 CMake 生成 VS2015 的编译项目
    C:\...\build2015>cmake .. -A x64 -DCMAKE_BUILD_TYPE=Release
    -DSWIG_EXECUTABLE=C:\work\swigwin-3.0.12\swig.exe
    -DPYTHON_EXECUTABLE="C:\Users\jgu23\AppData\Local\Continuum\Anaconda3\python.exe"
    -DPYTHON_LIBRARIES="C:\Users\jgu23\AppData\Local\Continuum\Anaconda3\libs\python36.lib"
    -Dtensorflow_WIN_CPU_SIMD_OPTIONS=/arch:AVX
  • 用 MSBuild 编译生成 Tensorflow 的 pip 安装包
    C:\...\build2015>MSBuild /p:Configuration=Release tf_python_build_pip_package.vcxproj
    编译时需要联网,git 会从网上下载必要的项目包
    经过漫长的编译过程(我的编译环境是 i5 7440hq + 12G 内存)
  • pip 安装 Tensorflow pip 安装包
    如果编译没错误的话,进入到 C:\...\build2015\tf_python\dist 目录下,可以看到一个文件
    tensorflow-1.3.1-cp36-cp36m-win_amd64.whl
    这个就是编译出来的 Tensorflow 安装包
    在进入 Anaconda Prompt 界面

    进入到 C:\...\build2017\tf_python\dist 目录下,输入“pip install tensorflow-1.3.1-cp36-cp36m-win_amd64.whl”
    到这里,Tensorflow 就已经编译安装成功了
  • 最后验证一下
    运行代码
    import tensorflow as tf
     
    hello = tf.constant('Hello, TensorFlow!')
    sess = tf.Session()
    print(sess.run(hello))     
    Console 输出
    C:\Users\jgu\Anaconda3\python.exe C:/Users/sandman/PycharmProjects/untitled/tf_helloworld.py
    2017-10-27 13:48:20.005261: W C:\work\tensorflow-1.3.1\tensorflow\core\platform\cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.
    b'Hello, TensorFlow!'
    说明这个 Tensorflow 包支持 AVX 指令集
  • 注意
    VS2015 编译 Tensorflow 最高只支持到 AVX, 如果 CMake 生成编译项目时指定 CPU 指令集支持到 AVX2,编译时将出现 error C3861: 'xxx': identifier not found 错误。要支持 AVX2 指令集,必须用 VS2017 编译

    查看大图

VS2017编译过程

  • 打开 VS2017 64 位命令行编译环境
  • 在命令行环境中进入 Tensorflow 的源码路径 tensorflow-1.3.1\tensorflow\contrib\cmake
    新建一个文件夹 build2017,进入 build2017 文件夹
  • 用 CMake 生成 VS2017 的编译项目
    C:\...\build2017>cmake .. -A x64 -DCMAKE_BUILD_TYPE=Release
    -DSWIG_EXECUTABLE=C:\work\swigwin-3.0.12\swig.exe
    -DPYTHON_EXECUTABLE="C:\Users\jgu23\AppData\Local\Continuum\Anaconda3\python.exe"
    -DPYTHON_LIBRARIES="C:\Users\jgu23\AppData\Local\Continuum\Anaconda3\libs\python36.lib"
    -Dtensorflow_WIN_CPU_SIMD_OPTIONS=/arch:AVX2
  • 这里比 VS2015 的编译多了一步
    用 VS2017 打开 tf_core_kernels.vcxproj


    进入 Properties->Configuration Properties->VC++ Directories->Executable Directories



    将 Executable Directories下的 $(VC_ExecutablePath_x64) 改为 $(VC_ExecutablePath_x64_x64)


    保存项目,退出
  • 用 MSBuild 编译生成 Tensorflow 的 pip 安装包
    C:\...\build2017>MSBuild /p:Configuration=Release tf_python_build_pip_package.vcxproj
    编译时需要联网,git 会从网上下载必要的项目包
    经过漫长的编译过程(我的编译环境是 i5 7440hq + 12G 内存)
  • pip安装 Tensorflow 安装包
    如果编译没错误的话,进入到 C:\...\build2017\tf_python\dist目录下,可以看到一个文件
    tensorflow-1.3.1-cp36-cp36m-win_amd64.whl
    这个就是 Tensorflow 的安装包
    在进入 Anaconda Prompt 界面

    进入到 C:\...\build2017\tf_python\dist 目录下,输入 “pip install tensorflow-1.3.1-cp36-cp36m-win_amd64.whl”
    到这里,Tensorflow 就已经编译安装成功了
  • 最后验证一下
    运行代码
    import tensorflow as tf
     
    hello = tf.constant('Hello, TensorFlow!')
    sess = tf.Session()
    print(sess.run(hello))
    Console输出
    C:\Users\jgu\Anaconda3\python.exe C:/Users/sandman/PycharmProjects/untitled/tf_helloworld.py
    b'Hello, TensorFlow!'
     
    Process finished with exit code 0
    没有任何 warning 输出,说明这个 Tensorflow 包支持 AVX2 指令集

常见错误及解决方法

编译 re2.vcxproj 时出错

warning C4819: The file contains a character that cannot be represented in the current code page (936). Save the file in Unicode format to prevent data loss
以及
Error C2001: Newline in constant

错误原因:此错误和编译平台的 windows 操作系统的locale设为中文有关,英文的 windows 没有这个错误
解决办法: 修改 CMakeCache.txt,让 MSBuild 编译这个项目时,强制按照 utf-8 编码文件的格式来解析文件
进入 C:\...\tensorflow-1.3.1\tensorflow\contrib\cmake\build2017\re2\src\re2 目录
用文本编辑器打开 CMakeCache.txt,找到以下2行



在 CMAKE_CXX_FLAGS_RELEASE 这里添加蓝色部分,修改为
CMAKE_CXX_FLAGS_RELEASE:STRING=/MD /O2 /Ob2 /DNDEBUG /source-charset:utf-8

编译 tf_core_kernels.vcxproj 时出错

fatal error c1060: the compiler is out of heap space
以及
fatal error C1002: compiler is out of heap space in pass 2

错误原因:VS 编译环境默认的编译工具链为32位,Tensorflow 编译时会消耗大量的内存,导致编译器消耗的内存超出了32位编译器的寻址范围。
解决方法:

  1. VS2015
    需要运行 64 位命令行编译环境,在“开始”菜单中选择运行 “VS2015 x64 Native Tools Command Prompt”,如本文 2.2 章中“打开 VS2015 64 位命令行编译环境”部分所示
    或者在命令行里手工切换
    首先进入 VS2015 的安装目录 “cd C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC”
    输入“vcvarsall amd64”
    参考 MSDN “How to: Enable a 64-Bit Visual C++ Toolset on the Command Line”
    https://msdn.microsoft.com/en-us/library/x4d2c09s.aspx
  2. VS2017
    可能因为 Tensorflow 项目中 CMake 脚本对 VS2017 支持不好,所以无法按照 VS2015 的修改办法来通过指定64位编译环境的方法来解决这个问题。我们需要用 VS2017 打开 tf_core_kernels.vcxproj,手工将 Properties->Configuration Properties->VC++ Directories->Executable Directories 下的$(VC_ExecutablePath_x64)改为$(VC_ExecutablePath_x64_x64)
    如本文 2.3 章中“VS2017 打开 tf_core_kernels.vcxproj”部分所示

结论

通过手工编译 Tensorflow 源码来支持 AVX/AVX2 指令,可以消除运行 Tensorflow 程序时烦人的 warning 提示,并且可以获得比官方预编译版本更快的 Tensorflow 学习/推理速度,节省了 Tensorflow 开发者的时间。

猜你喜欢

转载自www.cnblogs.com/DwightDuncan/p/9225322.html