Building custom tensorflow without SSE 4.2 support

Karina
3 min readDec 7, 2020

Recently one colleague of mine faced up with a problem of launching stock tensorflow code on rather old CPU without SSE 4.2 support. So he got an error:

tensorflow/core/platform/cpu_feature_guard.cc:37] The TensorFlow library was compiled to use SSE4.2 instructions, but these aren't available on your machine.

He asked me to build a custom lib without SSE support. The one difficulty was that he needed rather old version of tensorflow — 1.12. And the problem of old versions is broken compatibility with new versions of bazel build system, python version and other libs. I want to share all this stuff with anyone who’ll face this task and to underline several moments important for building other tensorflow versions.

First of all, you need to define bazel version. As I said before tensorflow 1.12 is incompatible with fresh bazel 3.* version, the working solution is bazel 0.15.0. Oh yeah, you even can’t install this version with apt-get. Moreover bazel is available for Ubuntu 16.04 and 18.04. Old bazel version requires JDK8, which is available in Ubuntu 16.04. I tried to install JDK 8 to Ubuntu 18.04 but default is 11, and I had several problems, so for sparing your nerves, please use 16.04 distributive for building linux package.

The second moment is python version. As I checked out, everything is fine with python 3.5 and 3.6 but there may be some issues with 3.7.

The third moment are versions of required python dependencies. For example you won’t build up with numpy 1.19.0. You’ll need at least 1.18.5 or even older.

So grabbing all together prerequisites for tensorflow 1.12 are the following:

Ubuntu 16.04 with JDK8
bazel = 0.15.0
Python3.6
Python packages:
six==1.15.0
numpy<1.19.0
wheel==0.33.1
mock==3.0.5
keras_applications==1.0.8
keras_preprocessing==1.1.2

I think that better solution for building up your own tensorflow with all these dependencies is using docker container. With Dockerfile you can commit all versions together and save them for future use. So here it is:

Building docker images please keep in mind that if you have proxy server, define it via --build-arg and with --env while running, because bazel build downloads several packages.

sudo docker build --build-arg http_proxy=http://ip:port --build-arg https_proxy=http://ip:port .sudo docker run --env http_proxy=http://ip:port --env https_proxy=http://ip:port --rm -it --name tenzorflow_build 0ec4c4a47693 bash

Docker image build ends up with cloning tensorflow repository to your local host. In tensorflow 1.12 I faced up with old hash sum for icu library, so during build you may get an error something like this:

no such package '@icu//': java.io.IOException: Error downloading [https://mirror.bazel.build/github.com/unicode-org/icu/archive/release-62-1.tar.gz, https://github.com/unicode-org/icu/archive/release-62-1.tar.gz] to C:/users/dsavaliya/_bazel_dsavaliya/xv6zejqw/external/icu/release-62-1.tar.gz: Checksum was 86b85fbf1b251d7a658de86ce5a0c8f34151027cc60b01e1b76f167379acf181 but wanted e15ffd84606323cbad5515bf9ecdf8061cc3bf80fb883b9e6aa162e485aa9761 and referenced by

Please, go to third_party/icu/workspace.bzl and change sha256 to:

sha256 = “86b85fbf1b251d7a658de86ce5a0c8f34151027cc60b01e1b76f167379acf181”

After that launch configure script from tensorflow repo folder:

./configure

And when you will be asked about build options, if you want to switch off all CPU extensions, please define:

-mtune=generic

Also I added -march=x86–64. Or define them directly in bazel build as shown below.

After configuring build system, please, execute:

bazel build -c opt --copt="-mtune=generic" --copt="-march=x86-64" //tensorflow/tools/pip_package:build_pip_package

If you get some errors for example for other tensorflow versions because of non supported python versions lib, reinstall them and only after bazel clean launch the process again. Otherwise the problem may stay. Build process speed depends on your system. For Intel NUC I got:

INFO: Elapsed time: 10116.901s, Critical Path: 199.60s
INFO: 7339 processes: 7339 local.
INFO: Build completed successfully, 8021 total actions

For building wheel package than execute:

./bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

You’ll get a package named tensorflow-1.12.3-cp36-cp36m-linux_x86_64.whl where cp36 means your python version.

Using that package on CPU with SSE 4.2 I got the message:

tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: SSE4.1 SSE4.2 AVX AVX2 FMA

It’s confirmed that now we have some generic version.

Thanks for your attention.

--

--