Building Tensorflow on POWER - CPU only

TensorFlow is a widespread software library for numerical computation using data flow graphs. It is very common on machine learning and deep neural networks projects. Therefore, today we are going to see how to install it on POWER with CPU only configuration.

tf logo

Update 03/2021

If you only want to install TensorFlow on POWER (and not build it), there is an easier way which is taught in the following tutorial: https://openpower.ic.unicamp.br/post/installing-tensorflow-on-power

OUTDATED :

OUTDATED 09/2020 The following tutorial is an outdated way of building TensorFlow on Power. If you still want to build TensorFlow from source by following this tutorial, proceed with caution.

Before installing TensorFlow, there are a couple of details we have to pay attention to:

  1. Due to Bazel, one of TF dependencies, the operating system must be Ubuntu 14.04 or Ubuntu 16.04.
  2. We are going to use Python 2.7, since TF doesn’t seem to be supported by Python 3.5 on POWER.

Tensorflow Dependencies

You can use the commands below to solve most of the dependencies:

apt-get update
apt-get install python-numpy python-dev python-pip python-wheel

Bazel installation

Bazel is one of the TF dependencies, but its installation is less intuitive than the others due to its community not officially supporting POWER architecture. That said, we must compile it from the Source. First of all, we need to install its own dependencies by the following commands:

apt-get update
apt-get install unzip build-essential python openjdk-8-jdk protobuf-compiler zip g++ zlib1g-dev

It is also important to add enviroment variables on .bashrc for JDK.

vi .bashrc
	export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-ppc64el
	export JRE_HOME=${JAVA_HOME}/jre
	export CLASSPATH=.:${JAVA_HOME}/lib:${JRE_HOME}/lib
	export PATH=${JAVA_HOME}/bin:$PATH

For compiling Bazel, we are going to download and unpack its distribution archive (the zip file from the release page https://github.com/bazelbuild/bazel/releases. The .sh is not compatible with ppc64le) and compile it.

mkdir bazel
cd bazel
wget -c https://github.com/bazelbuild/bazel/releases/download/0.11.1/bazel-0.11.1-dist.zip
unzip bazel-0.11.1-dist.zip
./compile.sh

if you want to download other version of bazel, this link must be switched by the one you are intenting to use.

Update 09/2020: It is also possible to perform the installation by following this tutorial.

As we can see, this tutorial was tested with bazel 0.11.1, but feel free to try other version and see if it works properly.

Also, if you are having any trouble about lack of resources, you can take a look on ‘Build issues and Support Websites’ to see if there’s any link that could help you. Anticipating: if you don’t have memory enough and your Bazel can’t complete the compile step, you might have a problem with the garbage collector of JAVA (and there’s a link which explains how to deal with it).

Installing Tensorflow

Since we are going to use the current version of TF, we need to clone it from the official GitHub and execute the configuration script.

git clone https://github.com/tensorflow/tensorflow
cd ~/tensorflow
./configure

On this step, we have to specify the pathname of all relevant TF dependencies and other build configuration options. On most of them we can use the answers suggested on each question. Here, I will show how it was done for this tutorial. (Yours might be a little different, depending on the pathnames)

Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python2.7
Found possible Python library paths:
  /usr/local/lib/python2.7/dist-packages
  /usr/lib/python2.7/dist-packages
Please input the desired Python library path to use.  Default is [/usr/lib/python2.7/dist-packages]: /usr/lib/python2.7/dist-packages

Using python library path: /usr/local/lib/python2.7/dist-packages

#Y/N Answers given: All of them as suggested in each question.

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: -mcpu=native
Configuration finished

To build and install TF, we use:

bazel build --copt="-mcpu=native" --jobs 1 --local_resources 2048,0.5,1.0 //tensorflow/tools/pip_package:build_pip_package
bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg #creates the pip package
pip install /tmp/tensorflow_pkg/tensorflow-1.5.0rc0-cp27-cp27mu-linux_ppc64le.whl #installs the pip package.

This name depends on your operating system, Python version and CPU only vs. GPU support. Therefore, check it out its name before this step.

By this moment, your TF must be working. Remember not to import it into its own directory: you have to chance directory before executing Python.

Build Issues and Support Websites:

While testing this tutorial, I could separate some useful issues reports and links to help some of the troubles you might have on the way.

  1. https://github.com/tensorflow/tensorflow/issues/14540 It solves a protobuf problem I had. It seems pretty common on PPC TF installation.
  2. https://github.com/tensorflow/tensorflow/issues/349 This one is about local resources. If you are running out of memory (your build fails on C++ compilation rules), you have to specify your resources on the command line when you build TF. On the tutorial, it is already done.
  3. https://www.tensorflow.org/install/install_sources An official tutorial about how to install TF from Sources
  4. https://docs.bazel.build/versions/master/install-compile-source.html An official tutorial about how to install Bazel from Sources.
  5. https://www.ibm.com/developerworks/community/blogs/fe313521-2e95-46f2-817d-44a4f27eba32/entry/Building_TensorFlow_on_OpenPOWER_Linux_Systems?lang=en IBM source about Tensorflow installation. Provides interesting information about bazel installation on PPC and how to install TF with GPU support. It also points to an IBM Bazel modified to PPC (which we are not using in this tutorial, but you can take a look on it).
  6. https://github.com/tensorflow/tensorflow/issues/7979#issuecomment-283559640 An issue about enviroment variables: on the configuration step, if it does not recognize some of the TF variables, this might help you to solve the problem.
  7. https://github.com/bazelbuild/bazel/issues/1308 An issue about Bazel: “The system is out of resources”. You might need to add a command line on compile file to change the garbage collector size. On the issue on git, it’s suggested to change it to 384, but, at least on one of the computers I tried to compile, I needed to change it to 512 (in other words, change -J-Xmx384m on the solution line to -J-Xmx512m). It’s important to see that ideally we should not have to change the source code, but it solves the problem. Another option is to increase the memory of your system if it’s possible (recommended).