Mac下解决tensorflow提示未编译使用SSE4.1,SSE4.2等Warning的解决方法

一、报错信息

mac下使用tensorflow提示如下warning

2018-03-10 19:23:23.999284: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.2 instructions, but these are available on your machine and could speed up CPU computations.

2018-03-10 19:23:23.999318: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine and could speed up CPU computations.

2018-03-10 19:23:23.999324: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine and could speed up CPU computations.

2018-03-10 19:23:23.999329: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine and could speed up CPU computations.


二、解决方案

(1)下载tensorflow源码

git clone --recurse-submodules https://github.com/tensorflow/tensorflow

(2)更新bazel

brew upgrade bazel

(3)配置tensorflow   命令如下:

cd tensorflow/    (进入第一步中创建的tensorflow文件夹)

./configure 

此时会弹出一些配置需要你填写,首先弹出的是选择python的路径和版本,在提示信息中会给出defult信息,由于默认是python2,因此需要改成python3

You have bazel 0.10.1-homebrew installed.

Please specify the location of python. [Default is /usr/bin/python]: /Library/Frameworks/Python.framework/Versions/3.6/bin/python3

Found possible Python library paths:

  /Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages

Please input the desired Python library path to use.  Default is [/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages]

完成之后会让你输入Y/N选择tensorflow是否支持google cloud,Hadoop等,建议Hadoop选Y其他选择N即可(如果都选Y又需要额外下载很多东西)


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]: y

Hadoop File System support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Amazon S3 File System support? [Y/n]: n

No Amazon S3 File System support will be enabled for TensorFlow.

Do you wish to build TensorFlow with Apache Kafka Platform support? [y/N]: n

No Apache Kafka Platform support will be enabled for TensorFlow.

Do you wish to build TensorFlow with XLA JIT support? [y/N]: n

No XLA JIT support will be enabled for TensorFlow.

Do you wish to build TensorFlow with GDR support? [y/N]: n

No GDR 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 SYCL support? [y/N]: n

No OpenCL SYCL 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.

Do you wish to build TensorFlow with MPI support? [y/N]: n

No MPI support will be enabled for TensorFlow.

Please specify optimization flags to use during compilation when bazel option "--config=opt" is specified [Default is -march=native]: --copt=-msse3 --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mfma

Would you like to interactively configure ./WORKSPACE for Android builds? [y/N]: n

Not configuring the WORKSPACE for Android builds.

Preconfigured Bazel build configs. You can use any of the below by adding "--config=<>" to your build command. See tools/bazel.rc for more details.

--config=mkl        # Build with MKL support.

--config=monolithic  # Config for mostly static monolithic build.

Configuration finished

(4)生成pip安装包   命令:bazel build -c opt --copt=-msse3 --copt=-msse4.1 --copt=-msse4.2 --copt=-mavx --copt=-mavx2 --copt=-mfma //tensorflow/tools/pip_package:build_pip_package  (时间较长,请耐心等待) 

(其中msse3,msse4.1 mavx等表示cpu支持的指令集,不同机器会有差异,如果你电脑的CPU不支持AVX指令,但在此处却输出了--copt=-mavx 最后还是会报错提示:你的机器不支持此指令,所以在写这条命令时候,建议大家下载CPU-Z看看本机CPU支持的指令,然后对应写此指令)

(5)安装  命令如下:

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

    到/tmp/tensorflow_pkg拷贝出来whl文件,再用pip3安装

     pip3 install tensorflow-1.6.0-cp36-cp36m-macosx_10_6_intel.whl  (最后是生成的软件包名,会有不同,根据自己生成的包名填写)

三、效果

对于同样一段代码,再优化之前需要800s每epoch,而优化之后大概需要310s左右,提升了一半以上,效果还是比较明显。

你可能感兴趣的:(Mac下解决tensorflow提示未编译使用SSE4.1,SSE4.2等Warning的解决方法)