导航

How to compile tensorflow on CentOS

Posted on 2018-07-25 14:40  MaHaLo  阅读(186)  评论(0编辑  收藏  举报

Tensorflow is a very effective machine learning library implemented by C++, we can use tensorflow with Python, but, there is a problem if we don't compile the tensorflow, it would cost a lot of time to compute. when we install the tensorflow with pip, we can see a warning message:"The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations." when import tensorflow. so, we need to compile the tensorflow library to speed up computation.

 

What to prepare:

  1. Java 8
  2. Bazel
  3. Tensorflow
  4. Python 3+
  5. CuDNN and CUDA toolkit(if you want to build tensorflow-gpu version)

Install Bazel:

  1. check you JAVA_HOME or test java: $ java -version
  2. get Bazel package: $ git clone  https://github.com/bazelbuild/bazel.git (bazel can't install with yum.)
  3. switch to a proper version: $ git checkout tags/0.3.0
  4. $ cd bazel
  5. $ ./compile.sh
  6. add bazel to PATH for convenient: $ PATH = $PATH:(PATH_TO_BAZEL)/output/

Tensorflow:

  1. get Tensorflow package: $ git clone https://github.com/tensorflow/tensorflow
  2. $ cd tensorflow
  3. configure

    ./configure
    Please specify the location of python. [Default is /usr/bin/python]: /home/xxxx/.pyenv/version/python36/bin/python
    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] Y
    jemalloc enabled
    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
    Found possible Python library paths:
      /usr/local/lib/python2.7/dist-packages
      /usr/lib/python2.7/dist-packages
    Please input the desired Python library path to use.  Default is [/usr/local/lib/python2.7/dist-packages]
    Using python library path: /home/xxxx/.pyenv/version/python36/lib/python3.6/site-packages
    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
     
    Configuration finished
  4. build tensorflow with bazel: $ bazel build -c opt --copt=-msse4.1 --copt=-msse4.2  -k //tensorflow/tools/pip_package:build_pip_package
  5. build whl file: $ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
  6. install tensorflow with whl file: $ pip install --upgrade /tmp/tensorflow_pkg/<your whl file>.whl

#Troubleshotting

After installing tensorflow with whl file, if you get an error with message "illegal instruction", that may caused by you use unsupported sse to build tensorflow, AVX, SSE4.1, SSE4.2, MFA are different kinds of extended instruction sets on X86 CPUs. Many contain optimized instructions for processing matrix or vector operations. before installing, check which instructions your CPU support, and put those optimizing flags in for all.

$ bazel build -c opt --copt=-mavx --copt=-msse4.1 --copt=-msse4.2  -k //tensorflow/tools/pip_package:build_pip_package

This solution refer to: https://github.com/tensorflow/tensorflow/issues/8976