Build TensorFlow From source code on Windows 在Windows系統上編譯原始碼並安裝TensorFlow

Source: Deep Learning on Medium


Go to the profile of Leo Lin

This article is going to show the steps that how to build tensorflow from source code.

Environment & target 環境及目標

  • Windows 10
  • Python 3.7.2
  • Git
  • CUDA v10.0
  • cuDNN v7.4.2
  • bazel 0.19.2
  • MSYS2
  • TensorFlow r1.12 with Apache Ignite support

Prepare 前置作業

Please ensure that you have already installed Git and Python. Also, make sure your git and python folder are in %PATH% environment variable.

Summary

  1. Install MSYS2
  2. Install Visual C++ Build Tools 2015
  3. Install Bazel
  4. Install GPU support(optional)
  5. Clone tensorflow
  6. Configure build parameter
  7. Build from source
  8. Create tensorflow wheel file
  9. Install tensorflow wheel file

Steps

Step 1 : Install MSYS2

  1. Go here and download the executable file.
  2. Double Click on the executable and finish the installation.
  3. Add directory with tools to %PATH% environment variable. Example: C:\msys32\usr\bin.
  4. Next, follow the command below to update the package
>> pacman -Syu

you need to press Ctrl + C or restart the terminal, because it won’t return to shell automatically after finishing the installation.

Then run,

>> pacman -Su

Final Step,

>> pacman -S patch unzip

Step 2 : Install Visual C++ Build Tools 2015

  1. Go to the Visual Studio downloads
  2. Select Redistributables and Build Tools(可轉散發套件及建置工具),
  3. Download and install:
  • Microsoft Visual C++ 2015 Redistributable Update 3 ( Microsoft Visual C++ 2015 可轉散發套件)
  • Microsoft Build Tools 2015 Update 3 ( Microsoft Build Tools 2015)

Note: TensorFlow is tested against the Visual Studio 2015 Update 3.

Step 3 : Install Bazel

  1. Go to Bazel release on Github and download the executable file.(Example: bazel-0.19.2-windows-x86_64.exe) . Rename the executable file to bazel.exe.
  2. Add directory of bazel.exe to %PATH% environment variable. Example: C:\bazel.
  3. Add BAZEL_SH environment variable. Its value is the path to MSYS2 Bash. Example: C:\msys64\usr\bin\bash.exe
  4. Add BAZEL_VC environment variable. Its value is the path for Visual Studio Build tools. Example: C:\Program Files (x86)\Microsoft Visual Stuio 14.0\VC

Step 4 : Install GPU Support TK

  1. Go to NVIDIA Drivers and download the driver for your NVIDIA product, also finish the installation.
  2. Go to CUDA Toolkit and download the Installer, also finish the installation.TK
  3. Go to cuDNN SDK and download. After unzip the zipfile, move the file which in bin, include, lib to the correspond directory in CUDA. Example: include\cudnn.h move to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v10.0\include

Step 5 : Clone tensorflow

Clone source code:

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

Checkout the branch(r.1.12, etc):

>> cd tensorflow
>> git checkout <target banch>

you can check the release branch

Step 6 : Configure build parameter

make sure that you have already added python to the %PATH% environment variable or activate the virtual environment

Install tensorflow package dependencies:

>> pip3 install six numpy wheel
>> pip3 install keras_applications==1.0.6 --no-deps
>> pip3 install keras_preprocessing==1.0.5 --no-deps

Configure your system build by running the following at the root of your TensorFlow source tree:

>> python ./configure.py

Ask location of Python. Press Enter to leave default value:

Please specify the location of python. [Default is C:\develop\venv\Scripts\python.exe]:

Ask location of Python library paths. Press Enter to leave default value:

Please input the desired Python library path to use.  Default is [C:\develop\venv\Lib\site-packages]

Ask about Apache Ignite support. Press “y” if you need it:

Do you wish to build TensorFlow with Apache Ignite support? [Y/n]: y

Ask about XLA JIT support. Press “n”:

Do you wish to build TensorFlow with XLA JIT support? [y/N]: n

Ask about ROCm support. We choose CUDA support, so here we press “n”:

Do you wish to build TensorFlow with ROCm support? [y/N]: n

Ask about CUDA support, Answer “y” if you are going to use NVIDIA GPU support. Otherwise press “n”.:

Do you wish to build TensorFlow with CUDA support? [y/N]:

Ask about CUDA version, here we enter 10.0:

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

Ask about CUDA toolkit lacation, Press Enter to leave default value:

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

Ask about cuDNN version, Press Enter to leave default to cuDNN 7:

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

Ask about cuDNN library location, Press Enter to leave default value:

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/v10.0]:

Ask about CUDA compute capabilities:

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]: 5.0

You can find the compute capability of your device at: https://developer.nvidia.com/cuda-gpus.

Ask about optimization flags:

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

I have 6th Gen Intel CPU, and it supports /arch:AVX2, that’s why I answer /arch:AVX2:

Ask about eigen, answer “y” to reduces compile time dramatically.

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

Step 7 : Build from source

Before Building from source code, there is bug we need to fix:

  1. Open and edit the .bazelre file which is at the root of your TensorFlow source tree
  2. Add string “import <your_path>/tensorflow/tools/bazel.rc“ on the top line.

Start to build from the source code

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

And find a place to take a rest.

Step 8 : Create tensorflow wheel file

>> bazel-bin\tensorflow\tools\pip_package\build_pip_package <target directory>

target directory example: C:/tmp/tensorflow_pkg

Step 9 : Install tensorflow wheel file

>> pip3 install <target directory>/tensorflow-<version>-cp36-cp36m-win_amd64.whl

Example: pip3 install C:/tmp/tensorflow_pkg/tensorflow-1.12-cp36-cp36m-win_amd64.whl

Finish!