Tensorflow from sources (Windows 10 version)

Source: Deep Learning on Medium

Go to the profile of Jamie Arjona

Have you ever wanted to build tensorflow from source files in order to take better profit of your hardware? Then, this is your guide.

What it is needed

First, it is necessary to download the following programs:

  1. Python with pip (https://www.python.org/downloads/).
  2. MSYS2 (https://www.msys2.org/).
  3. Visual C++ Build Tools 2015 (https://visualstudio.microsoft.com/es/downloads we will download Visual Studio community edition where we can find the C++ build tools).
  4. Bazel (https://www.bazel.build/).
  5. CUDA Toolkit and cuDNN libraries in case of using a CUDA compatible GPU (https://developer.nvidia.com/cuda-gpus).

After the download of the required programs, the instructions below must to be followed.

Previous configurations

First, we will add the Python and pip directories into the environment system variable Path once they are installed. The default Python directory is C:\Program Files\Python and for pip is C:\Program Files\Python\Scripts.

After the installation of MSYS2, it is needed to add into the variable Path its root path ( C:\msys64 by default) and its bin folder ( C:\msys64\usr\bin). Then, execute pacman -S git patch unzip on a console (cmd, powershell, bash, etc.) in order to install the necessary dependencies.

For the Visual C++ build tools, on the installation menu of the Visual Studio, check the option to install the Visual C++ build tools and then, check the Windows 10 SDK and the VC++ 2015 toolset for desktop (only those are needed) in the right part of the menu.

Visual Studio installation menu.

In the case of Bazel, it is needed to rename the downloaded file from the git repository (https://github.com/bazelbuild/bazel/releases where we download bazel-<version>-windows-x86_64.exe) to bazel.exe. After that, also it is needed to add the variable BAZEL_SH in our environment system variables with value c:\msys64\usr\bin\bash.exe. Also, it is needed the variable BAZEL_VS with value Microsoft Visual Studio home (C:\Program Files (x86)\Microsoft Visual Studio 14.0 by default).

While the installation of CUDA is easy, the cuDNN installation could be a little tricky. First install CUDA, then, download cuDNN from https://developer.nvidia.com/cudnn and unzip it. Then, copy the following files:

  1. \cuda\bin\cudnn64_7.dll to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\bin
  2. \cuda\ include\cudnn.h to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\include
  3. \cuda\lib\x64\cudnn.lib to C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0\lib\x64

Next, it is needed to create a CUDA_PATH environment variable with value the directory where the CUDA is installed (usually C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v9.0).

Lastly, it is needed to add a variable called JAVA_HOME with value the java folder (usually C:\Program Files\Java\jre1.8.0_13).

Python packages needed

In addition, the following python packages are needed to be installed before the installation of tensorflow:

  1. The python libraries six, numpy and wheel. (pip3 install six numpy wheel)
  2. The following parts of the keras library: keras_applications and keras_preprocessing.
    (pip3 install keras_applications==1.0.5 — no-deps
    pip3 install keras_preprocessing==1.0.3 — no-deps)

Tensorflow building

The next step is to download tensorflow from git (git clone https://github.com/tensorflow/tensorflow.git), enter into the cloned folder and execute python ./configure.py in a command console (again any of the following: bash, cmd, powershell, MSYS2, etc.).

Let’s go with Bazel and tensorflow source files (and its warnings).

Bazel orders step by step

  1. Enter Python location. If it is in the system environment variables then, it will detect the one by default.
  2. Next, the configurator will ask for the Python library path. If the Python location is configured in the system environment variables, it will be detected by default.
  3. ROCm support. These instructions are used by AMD GPUs. If you have one, active it. If not, skip it.
  4. CUDA support. This one is for Nvidia GPUs. Skip it if not owning an Nvidia GPU with CUDA.
  5. If we have chosen to install the CUDA support, then, the prompt will ask for the folder where the cuDNN libraries are installed.
  6. When asked by the prompt about the AVX support (AVX and AVX2 are a set of instructions used by modern CPUs since Haswell microarchitecture), use the instruction /arch:AVX or /arch:AVX2 or n (depending on your CPU microarchitecture).
  7. The last question is about override eigen strong inline for some C++ compilation in order to reduce the compilation time. I asked yes. (Information about Eigen C++ library: https://bitbucket.org/eigen/eigen/src).

With this, the Blaze part is over. Now, it is the time to compile the tensorflow libraries.

Compiling the package

This is the command to build CPU tensorflow:

bazel build — config=opt //tensorflow/tools/pip_package:build_pip_package

For GPU version:

bazel build — config=opt — config=cuda //tensorflow/tools/pip_package:build_pip_package

Take into account that the process takes several hours.

Once finished, we can proceed to install the package in our Python environment.

Finally, installing tensorflow

First, it is needed to generate the .whl file and then, use it via pip:

  1. bazel-bin/tensorflow/tools/pip_package/build_pip_package C:/tmp/tensorflow_pkg
  2. pip3 install C:/tmp/tensorflow_pkg/tensorflow-version-cp36-cp36m-win_amd64.whl
Finishing to install tensorflow.

And that’s all, for sure we will notice an speed up in the creation of our models.

If you found this post interesting, we are always hiring and interested in meeting all types of engineers, regardless of your skills or what tools you use day-to-day. Your intelligence, creativity, energy and enthusiasm are much more important to us than your experience with our stack.

Check out our careers page in here — https://worldsensing.wpengine.com/engineering/