Tensorflow Windows Build with GPU Support

时间:2021-09-22 03:21:18

标签:

Step-by-step Windows build

虽然Research一直在用Caffe,而且用的飞起,但还是很关注tensorflow社区的事情,最近发现TF有windows版本的了,就自己试了试。

步骤:https://github.com/tensorflow/tensorflow/tree/master/tensorflow/contrib/cmake

Pre-requisites:

Microsoft Windows 10

Install the pre-requisites detailed above, and set up your environment.

The following commands assume that you are using the Windows Command Prompt (cmd.exe). You will need to set up your environment to use the appropriate toolchain, i.e. the 64-bit tools. (Some of the binary targets we will build are too large for the 32-bit tools, and they will fail with out-of-memory errors.) The typical command to do set up your environment is:

D:\temp> "C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC\bin\amd64\vcvarsall.bat"

When building with GPU support after installing the CUDNN zip file from NVidia, append its bin directory to your PATH environment variable. In case TensorFlow fails to find the CUDA dll‘s during initialization, check your PATH environment variable. It should contain the directory of the CUDA dlls and the directory of the CUDNN dll. For example:

D:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0\bin D:\local\cuda\bin

We assume that cmake and git are installed and in your %PATH%. If for example cmake is not in your path and it is installed in C:\Program Files (x86)\CMake\bin\cmake.exe, you can add this directory to your %PATH% as follows:

D:\temp> set PATH="%PATH%;C:\Program Files (x86)\CMake\bin\cmake.exe"

Clone the TensorFlow repository and create a working directory for your build:

D:\temp> git clone https://github.com/tensorflow/tensorflow.git D:\temp> cd tensorflow\tensorflow\contrib\cmake D:\temp\tensorflow\tensorflow\contrib\cmake> mkdir build D:\temp\tensorflow\tensorflow\contrib\cmake> cd build D:\temp\tensorflow\tensorflow\contrib\cmake\build>

Invoke CMake to create Visual Studio solution and project files.

N.B. This assumes that cmake.exe is in your %PATH% environment variable. The other paths are for illustrative purposes only, and may be different on your platform. The ^ character is a line continuation and must be the last character on each line.

D:\...\build> cmake .. -A x64 -DCMAKE_BUILD_TYPE=Release ^ More? -DSWIG_EXECUTABLE=C:/tools/swigwin-3.0.10/swig.exe ^ More? -DPYTHON_EXECUTABLE=C:/Users/%USERNAME%/AppData/Local/Continuum/Anaconda3/python.exe ^ More? -DPYTHON_LIBRARIES=C:/Users/%USERNAME%/AppData/Local/Continuum/Anaconda3/libs/python35.lib

To build with GPU support add "^" at the end of the last line above following with:

More? -Dtensorflow_ENABLE_GPU=ON ^ More? -DCUDNN_HOME="D:\...\cudnn"

Note that the -DCMAKE_BUILD_TYPE=Release flag must match the build configuration that you choose when invoking msbuild. The known-good values are Release and RelWithDebInfo. The Debug build type is not currently supported, because it relies on a Debug library for Python (python35d.lib) that is not distributed by default.

There are various options that can be specified when generating the solution and project files:

Invoke MSBuild to build TensorFlow.

To build the C++ example program, which will be created as a .exe executable in the subdirectory .\Release:

D:\...\build> MSBuild /p:Configuration=Release tf_tutorials_example_trainer.vcxproj D:\...\build> Release\tf_tutorials_example_trainer.exe

To build the PIP package, which will be created as a .whl file in the subdirectory .\tf_python\dist:

D:\...\build> MSBuild /p:Configuration=Release tf_python_build_pip_package.vcxproj

在进行第四部的时候出了错,原因是在tensorflow\tensorflow\contrib\cmake\build\CMakeFiles\tf_core_gpu_kernels.dir\__\下面生成的cmake文件有问题,解决方案是:line 81处__VERSION__="MSVC"要改成__VERSION__=\"MSVC\"

改了之后重新进行Step 4生成whl文件,,pip install *.whl开始玩吧。。。

Tensorflow Windows Build with GPU Support