How to compile Tensorflow with SSE4.2 and AVX instructions?

前端 未结 12 721
南笙
南笙 2020-11-22 04:14

This is the message received from running a script to check if Tensorflow is working:

I tensorflow/stream_executor/dso_loader.cc:125] successfully opened CUD         


        
相关标签:
12条回答
  • 2020-11-22 04:46

    Thanks to all this replies + some trial and errors, I managed to install it on a Mac with clang. So just sharing my solution in case it is useful to someone.

    1. Follow the instructions on Documentation - Installing TensorFlow from Sources

    2. When prompted for

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

    then copy-paste this string:

    -mavx -mavx2 -mfma -msse4.2
    

    (The default option caused errors, so did some of the other flags. I got no errors with the above flags. BTW I replied n to all the other questions)

    After installing, I verify a ~2x to 2.5x speedup when training deep models with respect to another installation based on the default wheels - Installing TensorFlow on macOS

    Hope it helps

    0 讨论(0)
  • 2020-11-22 04:46

    To compile TensorFlow with SSE4.2 and AVX, you can use directly

    bazel build --config=mkl --config="opt" --copt="-march=broadwell" --copt="-O3" //tensorflow/tools/pip_package:build_pip_package

    Source: https://github.com/tensorflow/tensorflow/blob/master/tensorflow/tools/docker/Dockerfile.devel-cpu-mkl

    0 讨论(0)
  • 2020-11-22 04:50

    Let's start with the explanation of why do you see these warnings in the first place.


    Most probably you have not installed TF from source and instead of it used something like pip install tensorflow. That means that you installed pre-built (by someone else) binaries which were not optimized for your architecture. And these warnings tell you exactly this: something is available on your architecture, but it will not be used because the binary was not compiled with it. Here is the part from documentation.

    TensorFlow checks on startup whether it has been compiled with the optimizations available on the CPU. If the optimizations are not included, TensorFlow will emit warnings, e.g. AVX, AVX2, and FMA instructions not included.

    Good thing is that most probably you just want to learn/experiment with TF so everything will work properly and you should not worry about it


    What are SSE4.2 and AVX?

    Wikipedia has a good explanation about SSE4.2 and AVX. This knowledge is not required to be good at machine-learning. You may think about them as a set of some additional instructions for a computer to use multiple data points against a single instruction to perform operations which may be naturally parallelized (for example adding two arrays).

    Both SSE and AVX are implementation of an abstract idea of SIMD (Single instruction, multiple data), which is

    a class of parallel computers in Flynn's taxonomy. It describes computers with multiple processing elements that perform the same operation on multiple data points simultaneously. Thus, such machines exploit data level parallelism, but not concurrency: there are simultaneous (parallel) computations, but only a single process (instruction) at a given moment

    This is enough to answer your next question.


    How do these SSE4.2 and AVX improve CPU computations for TF tasks

    They allow a more efficient computation of various vector (matrix/tensor) operations. You can read more in these slides


    How to make Tensorflow compile using the two libraries?

    You need to have a binary which was compiled to take advantage of these instructions. The easiest way is to compile it yourself. As Mike and Yaroslav suggested, you can use the following bazel command

    bazel build -c opt --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-mfpmath=both --copt=-msse4.2 --config=cuda -k //tensorflow/tools/pip_package:build_pip_package

    0 讨论(0)
  • 2020-11-22 04:50

    I have recently installed it from source and bellow are all the steps needed to install it from source with the mentioned instructions available.

    Other answers already describe why those messages are shown. My answer gives a step-by-step on how to isnstall, which may help people struglling on the actual installation as I did.

    1. Install Bazel

    Download it from one of their available releases, for example 0.5.2. Extract it, go into the directory and configure it: bash ./compile.sh. Copy the executable to /usr/local/bin: sudo cp ./output/bazel /usr/local/bin

    1. Install Tensorflow

    Clone tensorflow: git clone https://github.com/tensorflow/tensorflow.git Go to the cloned directory to configure it: ./configure

    It will prompt you with several questions, bellow I have suggested the response to each of the questions, you can, of course, choose your own responses upon as you prefer:

    Using python library path: /usr/local/lib/python2.7/dist-packages
    Do you wish to build TensorFlow with MKL support? [y/N] y
    MKL support will be enabled for TensorFlow
    Do you wish to download MKL LIB from the web? [Y/n] Y
    Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: 
    Do you wish to use jemalloc as the malloc implementation? [Y/n] n
    jemalloc disabled
    Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] N
    No Google Cloud Platform support will be enabled for TensorFlow
    Do you wish to build TensorFlow with Hadoop File System support? [y/N] N
    No Hadoop File System support will be enabled for TensorFlow
    Do you wish to build TensorFlow with the XLA just-in-time compiler (experimental)? [y/N] N
    No XLA JIT support will be enabled for TensorFlow
    Do you wish to build TensorFlow with VERBS support? [y/N] N
    No VERBS support will be enabled for TensorFlow
    Do you wish to build TensorFlow with OpenCL support? [y/N] N
    No OpenCL support will be enabled for TensorFlow
    Do you wish to build TensorFlow with CUDA support? [y/N] N
    No CUDA support will be enabled for TensorFlow
    
    1. The pip package. To build it you have to describe which instructions you want (you know, those Tensorflow informed you are missing).

    Build pip script: bazel build -c opt --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-mfpmath=both --copt=-msse4.1 --copt=-msse4.2 -k //tensorflow/tools/pip_package:build_pip_package

    Build pip package: bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg

    Install Tensorflow pip package you just built: sudo pip install /tmp/tensorflow_pkg/tensorflow-1.2.1-cp27-cp27mu-linux_x86_64.whl

    Now next time you start up Tensorflow it will not complain anymore about missing instructions.

    0 讨论(0)
  • 2020-11-22 04:50

    I compiled a small Bash script for Mac (easily can be ported to Linux) to retrieve all CPU features and apply some of them to build TF. Im on TF master and use kinda often (couple times in a month).

    https://gist.github.com/venik/9ba962c8b301b0e21f99884cbd35082f

    0 讨论(0)
  • 2020-11-22 04:52

    2.0 COMPATIBLE SOLUTION:

    Execute the below commands in Terminal (Linux/MacOS) or in Command Prompt (Windows) to install Tensorflow 2.0 using Bazel:

    git clone https://github.com/tensorflow/tensorflow.git
    cd tensorflow
    
    #The repo defaults to the master development branch. You can also checkout a release branch to build:
    git checkout r2.0
    
    #Configure the Build => Use the Below line for Windows Machine
    python ./configure.py 
    
    #Configure the Build => Use the Below line for Linux/MacOS Machine
    ./configure
    #This script prompts you for the location of TensorFlow dependencies and asks for additional build configuration options. 
    
    #Build Tensorflow package
    
    #CPU support
    bazel build --config=opt //tensorflow/tools/pip_package:build_pip_package 
    
    #GPU support
    bazel build --config=opt --config=cuda --define=no_tensorflow_py_deps=true //tensorflow/tools/pip_package:build_pip_package
    
    0 讨论(0)
提交回复
热议问题