Tuesday, August 6, 2019

TensorFlow, Docker, and macOS


I wanted to experiment a bit with TensorFlow on a macOS installation but did not want to go through the installation of python3 and all the needed dependencies on the machine itself. So, I decided to give the latest TensorFlow docker image a spin.

There is an instruction page on the TensorFlow flow about how to get a TensorFlow docker image up and running. However I still ran into an error when following the instructions...

I started out by installing Docker Desktop to get a running docker environment on my machine. Once the docker environment is installed docker itself can be started. This took a little while though...

Once the docker environment is running, the docker command becomes available on the command line / terminal.

Next step pulling the latest stable TensorFlow docker image:


$ docker pull tensorflow/tensorflow
Using default tag: latest
latest: Pulling from tensorflow/tensorflow
5b7339215d1d: Pull complete 
14ca88e9f672: Pull complete 
a31c3b1caad4: Pull complete 
b054a26005b7: Pull complete 
ce020afbc108: Pull complete 
ff2859ce8b51: Pull complete 
fcbc4a17b2cb: Pull complete 
2ba8286c8cf8: Pull complete 
c7618033dbfa: Pull complete 
879aa66b376b: Pull complete 
Digest: sha256:87463fd80faa6e7979b78d2f1a26d62262210653eb166b638069ed06ae68dacb
Status: Downloaded newer image for tensorflow/tensorflow:latest
docker.io/tensorflow/tensorflow:latest

So far, so good.

Now when I ran the first example for CPU-only images I got the following output:

$ docker run -it --rm tensorflow/tensorflow \
>    python -c "import tensorflow as tf; tf.enable_eager_execution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))"
2019-08-06 19:19:56.383326: I tensorflow/core/platform/cpu_feature_guard.cc:142] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA
2019-08-06 19:19:56.413668: I tensorflow/core/platform/profile_utils/cpu_utils.cc:94] CPU Frequency: 2904000000 Hz
2019-08-06 19:19:56.414661: I tensorflow/compiler/xla/service/service.cc:168] XLA service 0x5639b0667000 executing computations on platform Host. Devices:
2019-08-06 19:19:56.414929: I tensorflow/compiler/xla/service/service.cc:175]   StreamExecutor device (0): <undefined>, <undefined>
tf.Tensor(345.6819, shape=(), dtype=float32)


All the extra messages make it look like there is something wrong with our environment. But there isn't. The line last line containing "tf.Tensor(345.6819, shape(), dtype=float32)" is an expected output of the example command.
All those extra information log lines are quite confusing, at least for me they were. Especially the line about the StreamExecutor; mentions of undefined are usually indications that something is wrong somewhere.

The CPU instruction warning can be suppressed by setting the environment variable TF_CPP_MIN_LOG_LEVEL to 2. This will cause the warnings to simply no longer get printed on the screen.

After adding the setting to the example program the execution is a lot cleaner:

docker run -it --rm tensorflow/tensorflow    python -c "import os; import tensorflow as tf; os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'; tf.enable_eager_execution(); print(tf.reduce_sum(tf.random_normal([1000, 1000])))"
tf.Tensor(-112.096924, shape=(), dtype=float32)


The parts added to the script were "import os;" and "os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2';". These can be added to the start of any TensorFlow python script to get cleaner output under macOS.