Getting Tensorflow and its C++ API working as a third party dependency in your own DLL on Windows and with GPU support can be an adventure. Here are the necessary steps to get it working, which might help. This tutorial is written for Tensorflow V 1.3, some information might be invalid for other versions.
1. Acquire the source code.
This one is easy, tensorflow is available on Github and can be found at https://github.com/tensorflow/tensorflow/tree/r1.3/
2. Have all dependencies installed.
Since we do not use the python bindings, there is no need for SWIG, but you should have
the correct CUDA version and cuDNN installed on your machine. For V1.3 this is CUDA 8 in combination with cuDNN 5.1. Git and cmake are also needed. More info here.
3. Use the 64bit Tools from Visual Studio 2015
VS2015 is necessary to build the DLL, but instead of building through the application,
press your windows key and type in “Native”. You should be able to open the “VS2015 x64 Native Tools Command Prompt”. This is needed so VS uses the 64 bit toolset, otherwise your build will just fail because it is not able to leverage enough memory.
4. Navigate to the source files
Navigate in the commandline to the “tensorflow/contrib/cmake” subfolder of the source code and create a directory with “mkdir build”. Afterwards navigate to the fresh build folder with “cd build”.
5. Create a build solution
Use the following build command:
cmake .. -A x64 -DCMAKE_BUILD_TYPE=RelWithDebInfo --Dtensorflow_BUILD_PYTHON_BINDINGS=OFF -Dtensorflow_BUILD_CC_EXAMPLE=OFF -Dtensorflow_ENABLE_GRPC_SUPPORT=OFF -Dtensorflow_BUILD_CC_TESTS=OFF -Dtensorflow_BUILD_PYTHON_TESTS=OFF -Dtensorflow_ENABLE_GPU=ON -DCUDNN_HOME="[YOUR_CUDA_LOCATION]" -Dtensorflow_WIN_CPU_SIMD_OPTIONS=/arch:AVX -Dtensorflow_BUILD_SHARED_LIB=ON
Make sure to replace “YOUR_CUDA_LOCATION” with the location of your cuda installation.
This setup does not need SWIG since we do not build the python bindings, which makes the build more lightweight and still contains the whole C++ API to load and run graphs at runtime.
The WIN_CPU_SIMD_OPTIONS flag can of course only be used when your machine has this
When everything went right you should be able to build the tensorflow.dll by issuing the following command:
MSBuild /p:Configuration=RelWithDebInfo tensorflow.vcxproj
6. Add the tensorflow DLL as dependency
After the build is done (this can take 3-4 hours on an average desktop computer) adopt your cmake setup to link to Tensorflow correctly. I created two environment variables, for the root of the tensorflow source and for the build directory, you can do this as you feel like. With TENSORFLOW_SRC_DIR as source directory and TENSORFLOW_BUILD_DIR as the build directory, make sure you add the following directories as includes:
Make sure you are adding the folder with the DLL as linker dependency:
And also target the library as additional linking target:
7. Debug in Release
Since tensorflow only builds in release with build symbols, we need to do the same for our own project. Otherwise the STL containers that are handed between our DLL and the C++ API of tensorflow will make it explode. Therefore follow the given instructions here, to adopt your own visual studio solution so that it generates debug symbols in the release build mode. Exercise for advanced users: Set the given settings in the linked webpage directly from cmake.
8. Additional Defines
Also make sure that you add the following three defines to your own code, to be able to link to tensorflow correctly. Tensorflow generates quite some warnings, so you can of course add a #pragma warning( disable : 4049) for every warning that annoys you, but this is up to you.
After this you should be able to compile your code base while linking to Tensorflow and use the C++ API, doing some deep learning magic with CUDA under windows. The C++ API.