系统:
win10 64bit
vs2017
硬件:
cpu:i7-6700hq
显卡:gtx970m
软件:
python3.7(64bit) https://www.python.org/downloads/release/python-370/
注意安装debugging symbols 和debug binraries
cuda10.10 cuda_10.0.130_411.31_win10.exe https://developer.nvidia.com/cuda-downloads?target_os=Windows&target_arch=x86_64&target_version=10&target_type=exelocal
cudnn7.3 cudnn-10.0-windows10-x64-v7.3.1.20.zip https://developer.nvidia.com/cudnn
tensorflow1.11 https://github.com/tensorflow/tensorflow.git c19e29306ce1777456b2dbb3a14f511edf7883a8
cmake cmake-3.12.3-win64-x64.msi https://cmake.org/download/
swig swigwin-3.0.12 http://www.swig.org/download.html
numpy numpy-1.15.2-cp37-none-win_amd64.whl
环境变量:
PreferredToolArchitecture=x64 https://msdn.microsoft.com/zh-cn/library/ee662426.aspx
PreferredToolArchitecture 属性确定在生产过程中,是否使用了 32 位或 64 位编译器和工具。 此属性不影响输出平台体系结构或配置。 默认情况下,如果该属性未设置或设置的值不为 x64,MSBuild 就会使用编译器和工具的 x86 版本。
将 PreferredToolArchitecture 的属性设置为 x64 以使用 64 位编译器和工具生成应用程序。
msbuild myProject.vcxproj /p:PreferredToolArchitecture=x64
务必保证编译期间 Microsoft® C/C++ Compiler Driver 是64位,否则可能会爆出 fatal error C1002: 在第2遍中编译器的堆空间不足
Cmake配置
提前fix一下 tensorflow\tensorflow\contrib\cmake\CMakeLists.txt
https://stackoverflow.com/questions/42603407/how-to-compile-tensor-flow-with-sse-and-and-avx-instructions-on-windows
Tensorflow make a mistake on flag "tensorflow_WIN_CPU_SIMD_OPTIONS".
It is a Flag, not a Boolean.
"Tensorflow-github/tensorflow/contrib/cmake/CMakeLists.txt"
option(tensorflow_WIN_CPU_SIMD_OPTIONS "Enables CPU SIMD instructions")
Replace it wtih
set(tensorflow_WIN_CPU_SIMD_OPTIONS "/arch:AVX" CACHE STRING "Enables CPU SIMD instructions" )
这里我们使用 /arch:AVX2 ,根据机器cpu选择
由于
- Starting from TensorFlow 1.11, Windows builds will use Bazel. Therefore, we will drop official support for cmake.
https://github.com/tensorflow/tensorflow/blob/master/RELEASE.md#breaking-changes
使用这里 https://github.com/tensorflow/tensorflow/issues/22047
FWIW, I've included a fix for CMake and Windows in this patch here:
https://github.com/bytedeco/javacpp-presets/blob/beb0a4a/tensorflow/tensorflow-windows.patch
对cmake相关文件进行修改
根据 https://medium.com/@amsokol.com/update-1-how-to-build-and-install-tensorflow-gpu-cpu-for-windows-from-source-code-using-bazel-and-c2e86fec9ef2
给eigen手动打上patch https://github.com/amsokol/tensorflow-windows-build-tutorial/blob/master/eigen_half.patch
cmake 应该是不支持bazel这种patch patch_file = clean_dep("//third_party:eigen_half.patch")
cmake GUI :
source code : tensorflow/tensorflow/contrib/cmake
configure 后
勾选 tensorflow_ENABLE_GPU
tensorflow_CUDA_VERSION 10.0
tensorflow_CUDNN_VERSION 7.3
新增 CUDNN_HOME 指向cudnn解压后目录
tensorflow_WIN_CPU_SIMD_OPTIONS /arch:AVX2
修改完后重新Configure,直到Configuring done
然后Generating done
开始build 参照 https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/cmake
命令行执行 set PreferredToolArchitecture=x64 设置环境变量(重要)
载入 VS 2017的开发人员命令提示符
执行 MSBuild /p:Configuration=Release /p:PreferredToolArchitecture=x64 tf_python_build_pip_package.vcxproj
可选参数 /p:CL_MPCount=4 同时运行的cl.exe数量,减少可有效降低内存占用,同时会降低编译速度