Building Tensorflow C++ shared library on Windows

As of February, 2018, TensorFlow’s Windows support is limited. Its pre-built packages only contains Python API. In order to integrate it into your C++ application, you’ll have to build TensorFlow’s C++ library by yourself.

TensorFlow’s source code contains CMake files for building TensorFlow on Windows, and official instructions are given here. This post contains steps to build TensorFlowC++ shared library (tensorflow.dll) with CPU or GPU support on three known-good configurations.

Configurations

A. TensorFlow 1.6.0-rc0 with CPU support

  • 15-inch Mid-2015 MacBook Pro (Core i7)
  • Windows 10 Pro Insider Preview 1709 64-bit
  • Visual Studio Community 2017 15.5.5
  • CMake 3.10.2
  • Git 2.15.0
  • SWIG 3.0.12

B. TensorFlow 1.5.0 with GPU support

  • Dell Precision Tower 5810 (Xeon E5 + GTX 1060)
  • Windows 10 Pro 1709 64-bit
  • Visual Studio Community 2017 15.5.6
  • CMake 3.10.2
  • Git 2.15.0
  • SWIG 3.0.12
  • CUDA 9.0
  • cuDNN 7.0.5

C. TensorFlow 1.4.1 with GPU support

  • Dell Precision Tower 5810 (Xeon E5 + GTX 1060)
  • Windows 10 Pro 1709 64-bit
  • Visual Studio Community 2017 15.5.6
  • CMake 3.10.2
  • Git 2.15.0
  • SWIG 3.0.12
  • CUDA 8.0
  • cuDNN 6.0

Steps

1. Install dependencies

I prefer to using Anaconda to limit the changes I make within a virtual environment. In Anaconda Prompt, install Python 3.5 (Python 3.6 is not supported) and numpy:

conda create -n tensorflow pip python=3.5
activate tensorflow
conda install numpy

I actually installed tensorflow or tensorflow-gpu because I also need to use TensorFlow in Python environment.

2. Get TensorFlow source code

In x64 Native Tools Command Prompt for VS 2017, clone TensorFlow’s GitHub repo:

A. TensorFlow 1.6.0-rc0 with CPU support

git clone https://github.com/tensorflow/tensorflow.git v1.6.0-rc0
cd v1.6.0-rc0
git checkout tags/v1.6.0-rc0

B. TensorFlow 1.5.0 with GPU support

git clone https://github.com/tensorflow/tensorflow.git v1.5.0
cd v1.5.0
git checkout tags/v1.5.0

C. TensorFlow 1.4.1 with GPU support

git clone https://github.com/tensorflow/tensorflow.git v1.4.1
cd v1.4.1
git checkout tags/v1.4.1

3. Prepare project files

Create working directory:

cd tensorflow\contrib\cmake
mkdir build
cd build

Invoke CMake to generate Visual Studio project files:

A. TensorFlow 1.6.0-rc0 with CPU support

cmake .. -A x64 ^
-DCMAKE_BUILD_TYPE=Release ^
-DSWIG_EXECUTABLE=C:\swigwin-3.0.12\swig.exe ^
-DPYTHON_EXECUTABLE=C:\Anaconda3\envs\tensorflow\python.exe ^
-DPYTHON_LIBRARIES=C:\Anaconda3\envs\tensorflow\libs\python35.lib ^
-Dtensorflow_BUILD_PYTHON_BINDINGS=OFF ^
-Dtensorflow_ENABLE_GRPC_SUPPORT=OFF ^
-Dtensorflow_BUILD_SHARED_LIB=ON

B. TensorFlow 1.5.0 with GPU support

cmake .. -A x64 ^
-DCMAKE_BUILD_TYPE=Release ^
-DSWIG_EXECUTABLE=C:\swigwin-3.0.12\swig.exe ^
-DPYTHON_EXECUTABLE=C:\Anaconda3\envs\tensorflow\python.exe ^
-DPYTHON_LIBRARIES=C:\Anaconda3\envs\tensorflow\libs\python35.lib ^
-Dtensorflow_ENABLE_GPU=ON ^
-DCUDNN_HOME="C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0" ^
-Dtensorflow_BUILD_PYTHON_BINDINGS=OFF ^
-Dtensorflow_ENABLE_GRPC_SUPPORT=OFF ^
-Dtensorflow_BUILD_SHARED_LIB=ON ^
-DCUDA_HOST_COMPILER="C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe"

C. TensorFlow 1.4.1 with GPU support

cmake .. -A x64 ^
-DCMAKE_BUILD_TYPE=Release ^
-DSWIG_EXECUTABLE=C:\swigwin-3.0.12\swig.exe ^
-DPYTHON_EXECUTABLE=C:\Anaconda3\envs\tensorflow\python.exe ^
-DPYTHON_LIBRARIES=C:\Anaconda3\envs\tensorflow\libs\python35.lib ^
-Dtensorflow_ENABLE_GPU=ON ^
-DCUDNN_HOME="C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v8.0" ^
-Dtensorflow_BUILD_PYTHON_BINDINGS=OFF ^
-Dtensorflow_ENABLE_GRPC_SUPPORT=OFF ^
-Dtensorflow_BUILD_SHARED_LIB=ON ^
-DCUDA_HOST_COMPILER="C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe"

Make sure the SWIG and Anaconda paths match the ones on your system. A few notes on other parameters:

  • -A x64 -DCMAKE_BUILD_TYPE=Release to build for x64 architecture in Release mode.
  • -Dtensorflow_ENABLE_GPU=ON to build with GPU support.
  • The two GPU-supported configurations differs in CUDA and cuDNN versions, which is represented by -DCUDNN_HOME.
  • -Dtensorflow_BUILD_PYTHON_BINDINGS=OFF since we are only building TensorFlow’s C++ API.
  • -Dtensorflow_ENABLE_GRPC_SUPPORT=OFF since we don’t need distributed deployment. Turning this option on may cause trouble in building.
  • -Dtensorflow_BUILD_SHARED_LIB=ON so that we can get tensorflow.dll. This option is default to OFF, which will leave us a bunch of static libraries (.lib).
  • -DCUDA_HOST_COMPILER=”C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/amd64/cl.exe” CMake’s CUDA_HOST_COMPILER is set to $(VCInstallDir)bin by default, which is not valid if you are using Visual Studio 2017. We need to explicitly set it.

4. Build TensorFlow

Invoke MSBuild to build the ALL_BUILD.vcxproj project.

“C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\MSBuild\15.0\Bin\amd64\MSBuild.exe” ^
/m:1 ^
/p:CL_MPCount=1 ^
/p:Configuration=Release ^
/p:Platform=x64 ^
/p:PreferredToolArchitecture=x64 ALL_BUILD.vcxproj ^
/filelogger

Notes on parameters:

  • /m:1 /p:CL_MPCount=1 to disable parallelism in building. Due to the scale of the TensorFlow project, the compiler can run out of heap space. Only allowing one instance of the compiler may reduce the chance this error occurs.
  • /p:Configuration=Release /p:Platform=x64 /p:PreferredToolArchitecture=x64 ALL_BUILD.vcxproj to match the configurations you’ve used in the CMake command. Also make sure you run CMake and MSBuild in x64 Native Tools Command Prompt for VS 2017 so that the correct toolchain is selected.
  • /filelogger will save the console output to tensorflow\contrib\cmake\msbuild.log. The log will be very handy if building error occurs.

The building process will take 1–2 hours. Once it finishes without error, you’ll find tensorflow.lib and tensorflow.dll within tensorflow\contrib\cmake\build\Release, and it’s time to celebrate! ?

Troubleshooting

For TensorFlow 1.4.1 and 1.5.0, there is one line of code which confuses Visual Studio 2017. Open tensorflow\contrib\boosted_trees\lib\utils\sparse_column_iterable.cc, replace

reference operator*() { return iter_->ix()(row_idx_, 0); }

with

reference operator*() const { return iter_->ix()(row_idx_, 0); }

Reference here.

Source: Deep Learning on Medium