Mianzhi Wang

Ph.D. in Electrical Engineering

Build Caffe2 on Windows 10 with GPU Support

Update 8/24/2017: I managed to get the Window build working again using the source code after this commit. I have updated this tutorial, but I cannot guarantee that it will work on any computer. If you really wish to build Caffe2 on Windows using the latest source code, you can try this tutorial. If not, you can wait for another stable release.

Update 8/23/2017: this tutorial no longer works for the latest source code (post 0.8.1). Various tweaks are required to get the Windows build working using the lastest source code (as of 8/23/2017). I am working on an updated tutorial. This tutorial should work on older releases of Caffe2, which can be found here.

Caffe2 is an open source deep learning framework developed by Facebook. While its windows binaries are not yet ready at this moment on its website, it is possible to compile it with GPU support on Windows 10. Actually, in the official repository, a build script named build_windows.bat is included to help users build Caffe2 on Windows. This tutorial summarizes my experience when building Caffe2 with Python binding and GPU support on Windows 10.


To successfully compile Caffe2 on Windows 10 with GPU support, the following prerequisites are mandatory:

  1. Windows 10: according to the official document, Windows 10 or greater is required to run Caffe2. For reference, I am using Windows 10 Pro with Creator Update installed.
  2. Visual Studio 2015 with Visual C++ and appropriate Windows SDK installed: compiling Caffe2 requires a compatible C++ compiler. Currently, CUDA 8.0 only supports up to Visual Studio 2015 with Update 3. If you use Visual Studio 2017 instead, you will not be able to enable GPU support.
  3. CUDA 8.0 with cuDNN installed: you can download and install them from NVIDIA's website. For more details you can refer to my previous post on configuring Keras on windows.
  4. Python 2.7: currently only Python 2.7 is supported. It is recommended to use Anaconda Python as it bundles necessary packages such as numpy, matplotlib, jupyter, etc., by default.
  5. CMake: required to configure and generate Visual Studio solutions. Its binaries should be added to your %PATH% environment variable.
  6. git: use git unless you want to manually download all the source files.

The following Python packages are also required: numpy, protobuf, and future. The Anaconda installation should already include numpy. protobuf can be installed via pip. future can be install via either conda or pip. You can also check and install optional dependencies as listed in the official document.

Obtain the Source Code and Apply Patches

You need to first download the source files of Caffe2, which is pretty easy with git (assuming the git binary is in your %PATH%):

git clone --recursive https://github.com/caffe2/caffe2.git

Now all the necessary files will be downloaded into a folder named caffe2, which will be referred as {BUILD_ROOT} in the following steps.

Since v0.8.1, python binding will not be compiled by default. To enable python binding, you need to change -DBUILD_PYTHON=OFF^ to -DBUILD_PYTHON=ON ^ in {BUILD_ROOT}\scripts\build_windows.bat.

Because Caffe2 is under active development, the underlying source code have changed a lot since May 2017. Without tweaks, the latest version of the source code will fail to compile on Windows when GPU support is enabled (because of MSVC). To get the Windows build working, you will need to edit the source code. You will need to apply all the patches here to your downloaded source code. This should eliminate almost all the build errors.

It is possible that you get C2397 errors when compiling operators\layer_norm_op.cu, a temporary workaround is to add explicit casting to line 264 and line 294 (there must be better ways of fixing this, but this will at least make MSVC happy):

// line 264
gscratch_.Resize(std::vector<size_t>{static_cast<size_t>(left), static_cast<size_t>(right)});
// line 294
stdev_.Resize(vector<size_t>{static_cast<size_t>(left), 1});

Important: Caffe2 is being actively developed. The build errors on Windows is likely to be fixed in the next release. The above patches may no longer work for future versions. It is recommended to follow this pull request.


To build Caffe2, you need to use VS2015 x64 Native Tools Command Prompt (you should have this shortcut if you have Visual Studio 2015 installed correctly) instead of opening a normal command prompt. This ensures that build related environment variables are correctly configured.

Before invoking {BUILD_ROOT}\scripts\build_windows.bat to start the build process, you need to specify CMAKE_GENERATOR and enable CUDA support, which can be done by entering the following commands:

set CMAKE_GENERATOR="Visual Studio 14 2015 Win64"

You can now invoke {BUILD_ROOT}\scripts\build_windows.bat to start the build process. It will first compile native Protocol Buffers binaries (which is required to build Caffe2), and then build Caffe2.

Important: do not turn on BUILD_TEST. Currently it will result in compilation errors.

Note: Prior to v0.8.1, if you have OpenCV installed and its binaries and libaries are included in your %PATH% environment variable, CMake will detect and enable OpenCV support for you. Since v0.8.1, the default build script does not enable OpenCV support by default. You need to manually change -DUSE_OPENCV=OFF to -DUSE_OPENCV=ON in build_windows.bat.

Note: Compiling GPU support for Caffe2 will take a long time. You will also need 4GB free space for the build process.

If all builds complete without errors, you should find the build process terminates with the messages similar to the following:

    2086 Warning(s)
    0 Error(s)

Time Elapsed 00:56:25.74
"Caffe2 built successfully"

You can find all the binaries under {BUILD_ROOT}\build\caffe2\binaries\Release. You should also be able to find two files named caffe2_pybind11_state.pyd and caffe2_pybind11_state_gpu.pyd under {BUILD_ROOT}\build\caffe2\python.

Note: many optional features are not enabled by the default script. You will need to manually enable them. However, not all optional features are available on Windows.

Before you can use Caffe2 in Python, you need to add {BUILD_ROOT}\build to %PYTHONPATH%. If your %PYTHONPATH% is empty, simply enter


Note that you will need to replace {BUILD_ROOT} with the actual path. Otherwise use the following command


You can test your configuration by running the following test

python -m caffe2.python.operator_test.relu_op_test

You can also verify that GPU support is enabled by running the CharCNN example under {BUILD_ROOT}\caffe2\python\examples with the --gpu flag.

Note: Instead of using the provided script, you can always use CMake to generate solution files and build everything with Visual Studio. In this case, you can tweak the options to fit your need. For instance, you can manually set CMAKE_INSTALL_PREFIX so you can install Caffe2 to your desire destination by building the INSTALL project in Visual Studio.

Pydot and GraphViz

In order for net_drawer to function properly, you will need to install pydot, which requires GraphViz. After downloading and installing GraphViz from the official website, add its binaries to %PATH% so pydot can find them.

Bonus: Build Caffe2 under WSL

Surprisingly, it is possible to build Caffe2 under Window Subsystem for Linux. You can just follow the official installation guide for Ubuntu 14.04/16.04 and it just works. Actually the whole installation process is easier. Unfortunately there is no GPU support in WSL. Therefore you cannot do some serious training in WSL. Interestingly, OpenCL and CUDA support get lots of upvotes on User Voice. However, it is not currently planned.