On macOS compiled Tensorflow to open AVX2 and FMA

When using Tensorflow,There has been a strange warning: Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA ,Although not affect the use,But looking annoying,You can use this command to turn it off: the.about['TF_CPP_MIN_LOG_LEVEL'] = '2'

but,Have you noticed it? " could speed up CPU computations"……Ok?!

background

In short,according toThe official,Tensorflow default does not support these advanced features to enhance the framework of compatibility Tensorflow,It can be used as much as possible more platforms,Thereby avoiding unnecessary compilation operation。But this is only the cost of using each platform has a CPU instruction set,The high-level commands can not add,After all, different CPU platforms they have different instruction sets of advanced technology。

Since I use macOS,GPU is an AMD,So I could not use Tensorflow accelerated with the GPU,With a very slow CPU,Can only run the test data (small),But I still hope it faster。

So I Tensorflow Tips,For my CPU,,There are more advanced features can be turned on in order to accelerate the training - but how to open it? The answer is definitely a source code recompilation Tensorflow。

AVX2 and FMA

FMA is a modern CPU SupportAn advanced instruction set,Chinese called "fused multiply-add operation";

AVX is supported by modern CPU "Advanced Vector Extensions instruction set”,Obviously,This means that AVX2 AVX advanced version of the meaning,It introduces the FMA arithmetic above,And floating point performance 2 Time。

In short,If Tensorflow able to directly use these advanced features,That training will be able to speed a lot faster,Then,We try to start from scratch to compile a Tensorflow。

Ready to work

Python

Obviously,We use Python to use Tensorflow,So you need to have Python environment:

Here we use Brew installation Python3.

Next is to install the necessary dependencies:

If you have a problem using pip,I wish to refer to this article:Proper use PIP to install Python package to avoid TypeError: ‘module’ object is not callable

Source

Compiled from the source code Tensorflow,It would certainly have to download the source code,Do you like to find a directory location,Columns command execution source projects get Tensorflow: git clone https://github.com/tensorflow/tensorflow.git ,Enter the directory cd tensorflow ,We want to switch to the stable version of M.,First tohttps://github.com/tensorflow/tensorflow/releasesFind the latest stable version of the label,It is the time of writing this article,Stable tag is v2.1.0 ,Here we switch to Stable: git checkout v2.1.0 .

Basel

Tensorflow is to be used to compile the Bazel,Overall compilation process easy and simple,But it takes a little bit of Bazel installation tips。

First of all,Check your source code files Tensorflow tensorflow/configure.py ,Where about 53 Row position,There is a line _TF_MAX_BAZEL_VERSION = '0.29.1' ,This is the version we use in Bazel。

Please note:The latest version is Bazel 2.0,Directly brew installation bazel is not compile the Tensorflow。

First make sure the latest version of Xcode installed on your macOS,Then execute the command: sudo xcodebuild -license accept ,Then tohttps://github.com/bazelbuild/bazel/releasesDownload the installation package Bazel,Note that the file name should look like this: Basel-0.29.1-installer-darwin-x86_64.sh

Click the file name link to download directly above the specified version of the installation package bazel,Tensorflow future requirements may become the source code compiler,By the time the reader is the version number of the corresponding self-bazel。

Since macOS 10.15 Enhanced system security measures,So if you just execute the script, bazel is unable to complete the installation,Will prompt a similar "bazel is unknown developer release,Potentially harmful "kind of warning,Thus refuse to run。

Use the command: sudo spctl --master-disable This warning is temporarily closed,Allow the program to run arbitrary developer。

After closing the warning,You can install and use the Bazel: sh Basel-0.29.1-installer-darwin-x86_64.sh --user ,After installation is complete use Basel --version View,The result should be: Basel 0.29.1

Compile Tensorflow

Final,We can compile a,Back tensorflow directory,carried out ./configure Configure,It will ask you the location and Python library,If you do not use brew installed,You can use the following command to find the location of your Python:

If there are multiple results,The first election to。

The next parameter configuration is almost no need to set,All default to,We do not need GPU support (add, it's useless),But in the end there is a support iOS,Here I chose y,After all by,Configuration,You can compile the。

Use the command Basel build --config=opt //tensorflow/tools/pip_package:build_pip_package Compile Tensorflow installation package,The default compile command is compiling for a local CPU,That is, to enable all current CPU instruction set support,Such is the translation of the Tensorflow accelerated it!

Compilation process is very slow,In my memory 16G 2.5 GHz Quad-Core Intel Core i7 CPU 下want 36301 Seconds to complete,full 10 hour(Given the wrong parameter,10Hours completely wasted) to 54,219.95 seconds to complete,A full 15 hours (15 years, 15-inch models rmbp)!

Another,I saw this warning at compile time:

I thought it could not support the AVX want to do it again ...... but it does not seem to affect the final result。

Generating installation package

Use the command ./Basel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg To generate .whl Installation package:

Then we install it with pip: pip install /tmp/tensorflow_pkg/tensorflow-2.1.0-cp37-cp37m-macosx_10_15_x86_64.whl

At last,Do not forget to close before the open of the security warning: sudo spctl --master-enable

Test results of the installation

Now no longer prompt Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMAThe,Try to run a model,Finally reached a long time can endure。

:)

References

 

Original article written by Gerber drop-off:R0uter's Blog » On macOS compiled Tensorflow to open AVX2 and FMA

Reproduced Please keep the source and description link:https://www.logcg.com/archives/3283.html

About the Author

R0uter

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Leave a Reply

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