September 15

How to use GPU of MX150 with Tensorflow 1.8 CUDA 9.2 (Phase 3: Compilation of Tensorflow 1.8)

Reading Time: 4 minutes

In the serie, “How to use GPU with Tensorflow 1.8 and CUDA 9.2”, we are now in the third phase. This step is related to the compilation of Tensorflow 1.8 by including the library CUDA. This compilation will enable tensorflow to use the GPU core of the graphical card (384 cores). 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. In the previous sections, we already installed the last driver of the graphical card (MX150), the  library CUDNN, and CUPTI to prepare the laptop for the compilation of Tensorflow 1.8. We also check if the library is correctly installed by using the samples available in the package of CUDA.

The compilation of Tensorflow is not simple. So, I recommend to reserve around 2 hours to make this task. The compilation of the software is structured into 4 steps:

  • Step 1: Download the required packages in the Ubuntu repository
  • Step 2: Installation of Blazel. Blazel is an open-source build and test tool similar to Make, Maven, and Gradle.
  • Step 3: Compilation of Tensorflow from the source.
  • Step 4: Creation of the package whl. This package will be deployed in the phase 4.

Step 1: Download  the required packages in the Ubuntu Repository

If you remember the introduction post, the objective is to use the Keras library to make a deep neural network in python. Keras is an open source neural network library written in Python. It is capable of running on top of TensorFlow, Microsoft Cognitive Toolkit or Theano. Designed to enable fast experimentation with deep neural networks, it focuses on being user-friendly, modular, and extensible. We have decided to use Tensorflow on GPU as backend of Keras.

So, the first step for this phase is the installation of the python environment which will be required for the execution of the DNN program that we will develop based on the Keras library. We will install the version 2.7 and 3 of python, numpy, python-wheel and pip on the system:

  • Python is an interpreted high-level programming language for general-purpose programming. Python features a dynamic type system and automatic memory management. It supports multiple programming paradigms, including object-oriented, imperative, functional and procedural, and has a large and comprehensive standard library
  • Python-wheel is Wheels are the new standard of Python distribution and are intended to replace eggs. Support is offered in pip >= 1.4 and setuptools >= 0.8. A wheel is a ZIP-format archive with a specially formatted filename and the .whl extension. It is designed to contain all the files for a PEP 376 compatible install in a way that is very close to the on-disk format.
  • Python_numpy is the fundamental package for scientific computing with Python. It contains a powerful N-dimensional array object, sophisticated (broadcasting) functions, tools for integrating C/C++ and Fortran code and useful linear algebra, Fourier transform, and random number capabilities
  • Python-dev is a Ubuntu package which regroups Header files, a static library and development tools for building Python modules, extending the Python interpreter or embedding Python in applications. This will be required by blazel to build the tensorflow package.
  • Pip is a package management system used to install and manage software packages written in Python. Many packages can be found in the default source for packages and their dependencies. pip is a recursive acronym for “Pip Installs Packages”.
apt-get install python3-pip
apt-get install python-numpy python-dev python-pip python-wheel
apt-get install python3-numpy python3-dev python3-pip python3-wheel
apt-get install git

Git is installed to clone the Blazel and the tensorflow source codes available on github.

Step 2: Installation of Blazel

For the compilation of Tensorflow, you need the software Blazel. Blazel is an open-source build and test tool similar to Make, Maven, and Gradle. It uses a human-readable, high-level build language. Bazel supports projects in multiple languages and builds outputs for multiple platforms. .All the documentation for this software can be found on the site : https://bazel.build/).

Blazel is not available in the Ubuntu repository, so you need to download the package from github and install it.

cd ~/
wget"https://github.com/bazelbuild/bazel/releases/download/0.14.0/bazel-0.14.0-installer-linux-x86_64.sh" 
chmod +x bazel-0.14.0-installer-linux-x86_64.sh
./bazel-0.14.0-installer-linux-x86_64.sh --user
echo 'export PATH="$PATH:$HOME/bin"' >> ~/.bashrc
ldconfig

 💡 attention point : It will be better to install bazel in other directory (in the standard one or a specific directory by configuring the environment variable via the .bashrc file)

Step 3: Compilation of Tensorflow

To compile Tensorflow from the source, you firstly need  to clone the directory of Tensorflow on the github thanks to the git command.

cd ~/ 
git clone https://github.com/tensorflow/tensorflow.git 
cd tensorflow 
git pull 
git checkout r1.8

Secondly, you need to prepare the compilation by configuring the different options by running the configure program.

./configure
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3
Press enter two times
Do you wish to build TensorFlow with jemalloc as malloc support? [Y/n]: Y
Do you wish to build TensorFlow with Google Cloud Platform support? [Y/n]: Y
Do you wish to build TensorFlow with Hadoop File System support? [Y/n]: Y
Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]: Y
Do you wish to build TensorFlow with Apache Kafka Platform support? [Y/n]: Y
Do you wish to build TensorFlow with XLA JIT support? [y/N]: N
Do you wish to build TensorFlow with GDR support? [y/N]: N
Do you wish to build TensorFlow with VERBS support? [y/N]: N
Do you wish to build TensorFlow with OpenCL SYCL support? [y/N]: N
Do you wish to build TensorFlow with CUDA support? [y/N]: Y
Please specify the CUDA SDK version you want to use, e.g. 7.0. [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 /usr/local/cuda]: /usr/local/cuda-9.2
Please specify the cuDNN version you want to use. [Leave empty to default to cuDNN 7.0]: 7.1.4
Please specify the location where cuDNN 7 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda-9.2]: /usr/local/cuda-9.2
Do you wish to build TensorFlow with TensorRT support? [y/N]: N
Please specify the NCCL version you want to use. [Leave empty to default to NCCL 1.3]: 2.2
Please specify the location where NCCL 2 library is installed. Refer to README.md for more details. [Default is /usr/local/cuda-9.2]: /usr/local/cuda-9.2/targets/x86_64-linux
Now we need compute capability which we have noted at step 1 eg. 5.0
Please note that each additional compute capability significantly increases your build time and binary size. [Default is: 5.0] 5.0 Do you want to use clang as CUDA compiler? [y/N]: N
Please specify which gcc should be used by nvcc as the host compiler. [Default is /usr/bin/gcc]: /usr/bin/gcc
Do you wish to build TensorFlow with MPI support? [y/N]: N
Please specify optimization flags to use during compilation when bazel option “--config=opt” is specified [Default is -march=native]: -march=native
Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]:N
Configuration finished

This is at this place that we link tensorflow with Cuda 9.2 that we installed in the previous post.

Thirdly, you can make the compilation and create the wheel package that you will install in the section 4 with pip.The next step in the process to install tensorflow GPU version will be to build tensorflow using bazel. This process takes a long time (around 1h30 on the T580).

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

Finally, the creation of the wheel package of tensorflow using GPU

bazel-bin/tensorflow/tools/pip_package/build_pip_package

You will find in the directory the file with the name: tensorflow-1.8.0-cp36-cp36m-linux_x86_64.whl

Conclusion:

Good ! Our initiative is almost close to the end. We have now the wheel package of Tensorflow using the  GPU of the graphical card.

So, let’s  start now with the Phase 4! The final post where we will learn how to run a DNN software using Keras, Tensorflow and the GPU of the card. For this, I will adapt an additional step to use a IDE “visual code” for this exercise.

Feel free to give comments or ask questions and I will try to answer you. It’s possible that I forgot a step in the explanation seeing that I made the compilation of this package in August. If you have any problem, just give me a comment and I will answer you and adapt the post.

If you find this post valuable, Rate it ! This helps me to improve it. For comments, you need to register with your LinkedIn account.

1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)
Loading...

References:

When I write an article, I always give the references I used because I believe in intellectual property and I know how many times you need to write a valuable article:


Tags: , , , , , , ,
Copyright 2018. All rights reserved.

Posted September 15, 2018 by DisruptIT in category "Artificial Intelligence

1 COMMENTS :

  1. By TRIXz on

    Im Using windows platform with MX150 graphics card, can I use this steps to install tensorflow gpud on my laptop?

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *