Skip to content

Instantly share code, notes, and snippets.

@meteorcloudy
Last active April 7, 2019 09:28
Show Gist options
  • Save meteorcloudy/8e5f1aab7c7fa87b16ae28e6f8fd3fd2 to your computer and use it in GitHub Desktop.
Save meteorcloudy/8e5f1aab7c7fa87b16ae28e6f8fd3fd2 to your computer and use it in GitHub Desktop.
Build TensorFlow on Windows With Bazel (Preview)

Install TensorFlow from Sources on Windows (Preview)

This guide explains how to build TensorFlow sources into a TensorFlow binary and how to install that TensorFlow binary on Windows.

Determine which TensorFlow to install

You must choose one of the following types of TensorFlow to build and install:

  • TensorFlow with CPU support only. If your system does not have a NVIDIA® GPU, build and install this version. Note that this version of TensorFlow is typically easier to build and install, so even if you have an NVIDIA GPU, we recommend building and installing this version first.

  • TensorFlow with GPU support. TensorFlow programs typically run significantly faster on a GPU than on a CPU. Therefore, if your system has a NVIDIA GPU and you need to run performance-critical applications, you should ultimately build and install this version. Beyond the NVIDIA GPU itself, your system must also fulfill the NVIDIA software requirements described in the following document:

Prepare environment for Windows

Before building TensorFlow on Windows, install the following build tools on your system:

Install MSYS2

Bash bin tools are used in TensorFlow Bazel build, you can install them through MSYS2.

Assume you installed MSYS2 at C:\msys64, add C:\msys64\usr\bin to your %PATH% environment variable.

To install necessary bash bin tools, issue the following command under cmd.exe:

C:\> pacman -S git patch unzip

Install Visual C++ Build Tools 2015

To build TensorFlow, you need to install Visual C++ build tools 2015. It is a part of Visual Studio 2015. But you can install it separately by the following way:

  • Open the official downloand page.
  • Go to Redistributables and Build Tools section.
  • Find Microsoft Build Tools 2015 Update 3 and click download.
  • Run the installer.

It's possible to build TensorFlow with newer version of Visual C++ build tools, but we only test against Visual Studio 2015 Update 3.

Install Bazel

If bazel is not installed on your system, install it now by following these instructions. It is recommended to use a Bazel version >= 0.15.0.

Add the directory where you installed Bazel to your %PATH% environment variable.

Install TensorFlow Python dependencies

If you don't have Python 3.5 or Python 3.6 installed, install it now:

To build and install TensorFlow, you must install the following python packages:

  • six, which provides simple utilities for wrapping over differences between Python 2 and Python 3.
  • numpy, which is a numerical processing package that TensorFlow requires.
  • wheel, which enables you to manage Python compressed packages in the wheel (.whl) format.
  • keras_applications, the applications module of the Keras deep learning library.
  • keras_preprocessing, the data preprocessing and data augmentation module of the Keras deep learning library.

Assume you already have pip3 in %PATH%, issue the following command:

C:\> pip3 install six numpy wheel
C:\> pip3 install keras_applications==1.0.4 --no-deps
C:\> pip3 install keras_preprocessing==1.0.2 --no-deps

Optional: install TensorFlow for GPU prerequisites

If you are building TensorFlow without GPU support, skip this section.

The following NVIDIA® hardware must be installed on your system:

  • GPU card with CUDA Compute Capability 3.5 or higher. See NVIDIA documentation for a list of supported GPU cards.

The following NVIDIA® software must be installed on your system:

  • GPU drivers. CUDA 9.0 requires 384.x or higher.
  • CUDA Toolkit (>= 8.0). We recommend version 9.0.
  • cuDNN SDK (>= 6.0). We recommend version 7.1.x.
  • CUPTI ships with the CUDA Toolkit, but you also need to append its path to %PATH% environment variable.

Assume you have CUDA Toolkit installed at C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0 and cuDNN at C:\tools\cuda, issue the following commands.

C:\> SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin;%PATH%
C:\> SET PATH=C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\extras\CUPTI\libx64;%PATH%
C:\> SET PATH=C:\tools\cuda\bin;%PATH%

Clone the TensorFlow repository

Now you need to clone the latest TensorFlow repository, thanks to MSYS2 we already have git avaiable, issue the following command:

C:\> git clone https://github.com/tensorflow/tensorflow.git 

The preceding git clone command creates a subdirectory named tensorflow. After cloning, you may optionally build a specific branch (such as a release branch) by invoking the following commands:

C:\> cd tensorflow
C:\> git checkout Branch # where Branch is the desired branch

For example, to work with the r1.11 release instead of the master release, issue the following command:

C:\> git checkout r1.11

Next, you must now configure the installation.

Configure the installation

The root of the source tree contains a python script named configure.py. This script asks you to identify the pathname of all relevant TensorFlow dependencies and specify other build configuration options such as compiler flags. You must run this script prior to creating the pip package and installing TensorFlow.

If you wish to build TensorFlow with GPU, configure.py will ask you to specify the version numbers of CUDA and cuDNN. If several versions of CUDA or cuDNN are installed on your system, explicitly select the desired version instead of relying on the default.

One of the questions that configure.py will ask is as follows:

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]:

Here is an example execution of the configure.py script. Note that your own input will likely differ from our sample input:

C:\> cd tensorflow  # cd to the top-level directory created
C:\tensorflow> python ./configure.py
Starting local Bazel server and connecting to it...
................
You have bazel 0.15.0 installed.
Please specify the location of python. [Default is C:\python36\python.exe]: 

Found possible Python library paths:
  C:\python36\lib\site-packages
Please input the desired Python library path to use.  Default is [C:\python36\lib\site-packages]

Do you wish to build TensorFlow with CUDA support? [y/N]: Y
CUDA support will be enabled for TensorFlow.

Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]:

Please specify the location where CUDA 9.0 toolkit is installed. Refer to README.md for more details. [Default is C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0]:

Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]: 7.0

Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0]: C:\tools\cuda

Please specify a list of comma-separated Cuda compute capabilities you want to build with.
You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.
Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 3.5,7.0]: 3.7

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]: 

Would you like to override eigen strong inline for some C++ compilation to reduce the compilation time? [Y/n]:
Eigen strong inline overridden.

Configuration finished

Build the pip package

CPU-only support

To build a pip package for TensorFlow with CPU-only support:

C:\tensorflow> bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

GPU support

To build a pip package for TensorFlow with GPU support:

C:\tensorflow> bazel build --config=opt --config=cuda //tensorflow/tools/pip_package:build_pip_package

NOTE : When building with GPU support, you might want to add --copt=-nvcc_options=disable-warnings to suppress nvcc warning messages.

The bazel build command builds a binary named build_pip_package (an executable binary to launch bash and run a bash script to create the pip package). Running this binary as follows will build a .whl file within the C:/tmp/tensorflow_pkg directory:

C:\tensorflow> bazel-bin\tensorflow\tools\pip_package\build_pip_package C:/tmp/tensorflow_pkg

Install the pip package

Invoke pip3 install to install that pip package. The filename of the .whl file depends on the TensorFlow version and your platform. For example, the following command will install the pip package for TensorFlow 1.11.0rc0:

C:\tensorflow> pip3 install C:/tmp/tensorflow_pkg/tensorflow-1.11.0rc0-cp36-cp36m-win_amd64.whl

Validate your installation

Validate your TensorFlow installation by doing the following:

Start a terminal.

Change directory (cd) to any directory on your system other than the tensorflow subdirectory from which you invoked the configure command.

Invoke python:

$ python

Enter the following short program inside the python interactive shell:

# Python
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

If the system outputs the following, then you are ready to begin writing TensorFlow programs:

Hello, TensorFlow!

To learn more, see the TensorFlow tutorials.

Build under MSYS shell

The above instruction assumes you are building under the Windows native command line (cmd.exe), but you can also build TensorFlow from MSYS shell. There are a few things to notice:

  • Disable the path conversion heuristic in MSYS. MSYS automatically converts arguments that look like a Unix path to Windows path when running a program, this will confuse Bazel. (eg. A Bazel label //foo/bar:bin is considered a Unix absolute path, only because it starts with a slash)
$ export MSYS_NO_PATHCONV=1
$ export MSYS2_ARG_CONV_EXCL="*"
  • Add the directory where you install Bazel in $PATH. Assume you have Bazel installed at C:\tools\bazel.exe, issue the following command:
# `:` is used as path separator, so we have to convert the path to Unix style.
$ export PATH="/c/tools:$PATH"
  • Add the directory where you install Python in $PATH. Assume you have Python installed at C:\Python36\python.exe, issue the following command:
$ export PATH="/c/Python36:$PATH"
  • If you have Python in $PATH, you can run configure script just by ./configure, a shell script will help you invoke python.

  • (For GPU build only) Add Cuda and cuDNN bin directories in $PATH in the following way:

$ export PATH="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0/bin:$PATH"
$ export PATH="/c/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.0/extras/CUPTI/libx64:$PATH"
$ export PATH="/c/tools/cuda/bin:$PATH"

The rest steps should be the same as building under cmd.exe.

@ahmedyarub
Copy link

what about MKL? is there a way to compile Tensorflow with MKL support on Windows?

@meteorcloudy
Copy link
Author

TensorFlow Windows build doesn't work with MKL support yet, but it is possible, and any contribution will be welcome!

@czcbangkai
Copy link

czcbangkai commented Aug 24, 2018

How to generate a dynamic library for Tensorflow tensorflow.dll with this method?

@meteorcloudy
Copy link
Author

@czcbangkai You can just build //tensorflow:libtensorflow.so instead of the pip package. Then rename the output file to tensorflow.dll

@taotaolin
Copy link

taotaolin commented Sep 12, 2018

bazel build --config=opt --config=cuda //tensorflow:libtensorflow.so will be generated tensorflow.dll and tensorflow.lib ?
Can provide a detailed windows tutorial? thx

@dshawul
Copy link

dshawul commented Sep 17, 2018

I am getting an error even though I followed your steps correctly. Something to do with the cuDNN version

c:\dev\tensorflow>bazel build --config=opt --config=monolithic --config=cuda //tensorflow:libtensorflow_cc.so
Starting local Bazel server and connecting to it...
WARNING: The following configs were expanded more than once: [monolithic, cuda]. For repeatable flags, repeats are counted twice and may lead to unexpected behavior.
WARNING: Option 'experimental_shortened_obj_file_path' is deprecated
ERROR: Skipping '//tensorflow:libtensorflow_cc.so': error loading package 'tensorflow': Encountered error while reading extension file 'cuda/build_defs.bzl': no such package '@local_config_cuda//cuda': Traceback (most recent call last):
        File "C:/dev/tensorflow/third_party/gpus/cuda_configure.bzl", line 1445
                _create_local_cuda_repository(repository_ctx)
        File "C:/dev/tensorflow/third_party/gpus/cuda_configure.bzl", line 1185, in _create_local_cuda_repository
                _get_cuda_config(repository_ctx)
        File "C:/dev/tensorflow/third_party/gpus/cuda_configure.bzl", line 909, in _get_cuda_config
                _cudnn_version(repository_ctx, cudnn_install_base..., ...)
        File "C:/dev/tensorflow/third_party/gpus/cuda_configure.bzl", line 584, in _cudnn_version
                find_cuda_define(repository_ctx, cudnn_header_dir, "c...", ...)
        File "C:/dev/tensorflow/third_party/gpus/cuda_configure.bzl", line 534, in find_cuda_define
                auto_configure_fail(("Error reading %s: %s" % (str(h...)))
        File "C:/dev/tensorflow/third_party/gpus/cuda_configure.bzl", line 315, in auto_configure_fail
                fail(("\n%sCuda Configuration Error:%...)))

Cuda Configuration Error: Error reading C:/dev/cuDNN/cuda/include/cudnn.h: java.io.IOException: ERROR: src/main/native/windows/processes-jni.cc(239): CreateProcessW("grep" --color=never -A1 -E "#define CUDNN_MAJOR" C:/dev/cuDNN/cuda/include/cudnn.h): The system cannot find the file specified.

WARNING: Target pattern parsing failed.
ERROR: error loading package 'tensorflow': Encountered error while reading extension file 'cuda/build_defs.bzl': no such package '@local_config_cuda//cuda': Traceback (most recent call last):
        File "C:/dev/tensorflow/third_party/gpus/cuda_configure.bzl", line 1445
                _create_local_cuda_repository(repository_ctx)
        File "C:/dev/tensorflow/third_party/gpus/cuda_configure.bzl", line 1185, in _create_local_cuda_repository
                _get_cuda_config(repository_ctx)
        File "C:/dev/tensorflow/third_party/gpus/cuda_configure.bzl", line 909, in _get_cuda_config
                _cudnn_version(repository_ctx, cudnn_install_base..., ...)
        File "C:/dev/tensorflow/third_party/gpus/cuda_configure.bzl", line 584, in _cudnn_version
                find_cuda_define(repository_ctx, cudnn_header_dir, "c...", ...)
        File "C:/dev/tensorflow/third_party/gpus/cuda_configure.bzl", line 534, in find_cuda_define
                auto_configure_fail(("Error reading %s: %s" % (str(h...)))
        File "C:/dev/tensorflow/third_party/gpus/cuda_configure.bzl", line 315, in auto_configure_fail
                fail(("\n%sCuda Configuration Error:%...)))

Cuda Configuration Error: Error reading C:/dev/cuDNN/cuda/include/cudnn.h: java.io.IOException: ERROR: src/main/native/windows/processes-jni.cc(239): CreateProcessW("grep" --color=never -A1 -E "#define CUDNN_MAJOR" C:/dev/cuDNN/cuda/include/cudnn.h): The system cannot find the file specified.

INFO: Elapsed time: 3.679s
INFO: 0 processes.
FAILED: Build did NOT complete successfully (0 packages loaded)
    currently loading: tensorflow

@dshawul
Copy link

dshawul commented Sep 17, 2018

The solution for my problem is to install grep for windows and add its locaction to Path env. variable. This is pointed out here https://stackoverflow.com/questions/51534685/tensorflow-1-9-bazel-build-error-with-cuda-on-windows/51879261
It is probably a good idea to add this requirement to your documentation.

@LJXLJXLJX
Copy link

I am sorry to bother. When I run configure.py , a window pop up, indicating an win32 exception and ask me to debug it . I ignored and closed it but still finished this step successfully.
But then I entered bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package
An error occurred

ERROR: SymlinkDirectories(C:\Users\Administrator/_bazel_Administrator/install/9b4a3d8963d27679daf2ef8226ad0888, c:\users\administrator\_bazel_administrator\pve46h2d/install): CreateJunction:
FATAL: failed to create installation symlink 'c:\users\administrator\_bazel_administrator\pve46h2d/install': success

I hope you can give me some suggestion, thank you!

@Arritmic
Copy link

Hello!
Does someone feedback about to compile only tensorflow lite as static lib for windows x64?
Could someone give me some steps for it? Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment