How to build and install TensorFlow GPU/CPU for Windows from source code using bazel and Python 3.6

Summary

  1. Install Git for Windows
  2. Install Bazel
  3. Install MSYS2 x64 and command line tools
  4. Install Visual Studio 2017 Build Tools including Visual Studio 2015 Build Tools
  5. Install Python 3.6 64-bits
  6. Install NVIDIA CUDA 9.2 and cuDNN 7.2 (for GPU acceleration)
  7. Configure build environment
  8. Clone TensorFlow source code and apply mandatory patch
  9. Configure build parameters
  10. Build TensorFlow from sources
  11. Create TensorFlow wheel file for Python 3.6
  12. Install TensorFlow wheel file for Python 3.6 and check result

Step 1: Install Git for Windows

C:\Bin\Git

Step 2: Install MSYS2 x64 and command line tools

C:\Bin\msys64
pacman -Syu
pacman -Su
pacman -S patch unzip

Step 3: Install Visual Studio 2017 Build Tools including Visual Studio 2015 Build Tools

Step 4: Install Bazel

C:\Bin\msys64\usr\bin\bash.exe
C:\Program Files (x86)\Microsoft Visual Studio 14.0\VC

Step 5: Install Python 3.6 64-bits

C:\Users\amsokol\Miniconda3

Step 6: Install NVIDIA CUDA 9.2 and cuDNN 7.2 (for GPU acceleration)

nvcc --version

Step 7: Configure build environment

%windir%\System32\cmd.exe "/K" C:\Users\amsokol\Miniconda3\Scripts\activate.bat C:\Users\amsokol\Miniconda3
conda create -n tensorflow-v1.10 python=3.6conda activate tensorflow-v1.10
conda install numpy cython

Step 8: Clone TensorFlow source code and apply mandatory patch

cd C:\Users\amsokol\Development\tensorflow-build
git clone https://github.com/tensorflow/tensorflow
cd tensorflowgit checkout v1.10.1
  • Download patch here and save with file name eigen_half.patch to third_party folder
  • Add patch_file = clean_dep(“//third_party:eigen_half.patch”), line to eigen_archive section to tensorflow/workspace.bzl file.
...
tf_http_archive(
name = "eigen_archive",
urls = [
"https://mirror.bazel.build/bitbucket.org/eigen/eigen/get/fd6845384b86.tar.gz",
"https://bitbucket.org/eigen/eigen/get/fd6845384b86.tar.gz",
],
sha256 = "d956415d784fa4e42b6a2a45c32556d6aec9d0a3d8ef48baee2522ab762556a9",
strip_prefix = "eigen-eigen-fd6845384b86",
build_file = clean_dep("//third_party:eigen.BUILD"),
patch_file = clean_dep("//third_party:eigen_half.patch"),
)
...

Step 9: Configure build parameters

cd C:\Users\amsokol\Development\tensorflow-build\tensorflow
python configure.py
...
You have bazel 0.16.1 installed.
Please specify the location of python. [Default is C:\Users\amsokol\miniconda3\envs\tensorflow-v1.10\python.exe]:
Found possible Python library paths:
C:\Users\60051029\miniconda3\envs\tensorflow-v1.10\lib\site-packages
Please input the desired Python library path to use. Default is [C:\Users\60051029\miniconda3\envs\tensorflow-v1.10\lib\site-packages]
Do you wish to build TensorFlow with CUDA support? [y/N]:
Please specify the CUDA SDK version you want to use. [Leave empty to default to CUDA 9.0]: 9.2
Please specify the location where CUDA 9.2 toolkit is installed. Refer to README.md for more details. [Default is C:/Program Files/NVIDIA GPU Computing Toolkit/CUDA/v9.2]:
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]: 7.2.1
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.2]:
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]6.1
Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is /arch:AVX]: /arch:AVX2

Step 10: Build TensorFlow from sources

cd C:\Users\amsokol\Development\tensorflow-build\tensorflow
bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package

Step 11: Create TensorFlow wheel file for Python 3.6

bazel-bin\tensorflow\tools\pip_package\build_pip_package tmp/tensorflow_pkg

Step 12: Install TensorFlow wheel file for Python 3.6 and check result

pip install .\tmp\tensorflow_pkg\tensorflow-1.10.1-cp36-cp36m-win_amd64.whl
cd ..
import tensorflow as tf
hello = tf.constant('Hello, TensorFlow!')
session = tf.Session()
print(session.run(hello))

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store