In this post, I demonstrate how to build Tensorflow 1.5 from source on MacOS 10.13 High Sierra. To verify that building indeed works, I show how to change some Tensorflow Python code, do an incremental build and observe the change in action via a simple test program.

Installing Pre-Requisites

Tensorflow Source Code

You will most likely want to fork the main Tensorflow git repository on Github and clone your own fork to your local computer. The following clone instruction clones our Knowm fork of Tensorflow, containing a custom build script not present in the main Tensorflow project.

Shell

1

2

git clonehttps://github.com/knowm/tensorflow.git

XCode

Install XCode via AppStore. I unsuccessfully tried to avoid this step, by installing just the command line XCode tools using xcode-select --install, but later found out by trial and error that I indeed needed to install XCode.

Bazel

Bazel is a software dependency and build tool similar to ANT and Maven. Installation Instructions are here.

Shell

1

2

3

4

5

6

brew install bazel

bazel version

brew upgrade bazel

pip3 install six numpy wheel

brew install coreutils

Custom Build Script

We need a custom build_tf.sh because we didn’t install Python the recommended way using VirtualEnv. The inspiration of our build file is here. This retrieves all CPU features and applies some of them to build TF, which makes TF faster as it will utilize specialized CPU instructions if your computer has them.

Run an Example

Now that Tensorflow was built from source, you should run an example in the main Tensorflow project to verify that everything works:

Shell

1

2

python3 tensorflow/examples/tutorials/mnist/mnist_softmax.py

Congratulations!!

Incrementally Building Tensorflow

For developing Tensorflow, we obviously don’t want to have to build the entire source tree from scratch every single time a change is made. Therefore, we want to build TF incrementally each time a change is made.

To accomplish this, we need to first understand what our build file build_tf.sh does, in particular the last few steps:

This takes the wheel file and makes it an executable on the system via calling tensorflow.

For an incremental build we only want to run the last 3 (of 5) lines of the build file and to do so we can just comment out the first 2 lines of code. the incremental build then takes less than 2 minutes.

Hacking Tensorflow at the Python Level

Let’s see if we can change some code in TF itself in a trivial way, recompile, re-run asimple hello world program such as hellotf.py from our HelloTensorflow Project and verify that our incremental build setup is working as expected. We’ll use the following simple TF program:

Shell

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

# https://mubaris.com/2017-10-21/tensorflow-101

# Import TensorFlow

import tensorflow astf

# Define Constant

output=tf.constant("Hello, World")

# To print the value of constant you need to start a session.

sess=tf.Session()

# Print

print(sess.run(output))

# Close the session

sess.close()

, which produces:

Shell

1

2

b'Hello, World'

What if we change the tf.constant code to append the String _hack onto the end of the inputted constant?

In constant_op.py (source here), line 212, we can modify it like this: