mac下编译安装TensorFlow

2,471次阅读
没有评论

mac下编译安装TensorFlow

前言

如果直接通过pip install tensorflow安装Tensorflow的话,在运行时可能会有这种警告

W tensorflow/core/platform/cpu_feature_guard.cc:<span class="hljs-number">45</span>] The TensorFlow library wasn't compiled to use SSE4<span class="hljs-number">.2</span> instructions, but these are available on your machine <span class="hljs-keyword">and</span> could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:<span class="hljs-number">45</span>] The TensorFlow library wasn't compiled to use AVX instructions, but these are available on your machine <span class="hljs-keyword">and</span> could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:<span class="hljs-number">45</span>] The TensorFlow library wasn't compiled to use AVX2 instructions, but these are available on your machine <span class="hljs-keyword">and</span> could speed up CPU computations.
W tensorflow/core/platform/cpu_feature_guard.cc:<span class="hljs-number">45</span>] The TensorFlow library wasn't compiled to use FMA instructions, but these are available on your machine <span class="hljs-keyword">and</span> could speed up CPU computations.

主要是因为你的CPU支持AVX2,SSE等指令集,而pip安装的库没有使用它们。启用这些指令集会提升运算速度,而且每次打印一大坨看着有点烦人,所以这里我们从源码编译安装Tensorflow。

编译&安装

  • 先安装一些依赖
$ brew install bazel swig
  • 克隆代码仓库
$ git <span class="hljs-built_in">clone</span> https://github.com/tensorflow/tensorflow.git
$ <span class="hljs-built_in">cd</span> tensorflow
  • 这里先configure,过程中会问一些编译过程中要用到的参数,比如Python的目录,是否开启OpenCL,CUDA支持,我这里直接全否了。
$ ./configure
Please specify the location <span class="hljs-keyword">of</span> python. [Default is /usr/local/anaconda3/bin/python]:
Found possible Python library paths:
  <span class="hljs-regexp">/usr/</span>local/anaconda3/lib/python3<span class="hljs-number">.6</span>/site-packages
Please input the desired Python library path to use.  Default is [<span class="hljs-regexp">/usr/</span>local/anaconda3/lib/python3<span class="hljs-number">.6</span>/site-packages]

Using python library path: <span class="hljs-regexp">/usr/</span>local/anaconda3/lib/python3<span class="hljs-number">.6</span>/site-packages
Do you wish to build TensorFlow <span class="hljs-keyword">with</span> MKL support? [y/N] n
No MKL support will be enabled <span class="hljs-keyword">for</span> TensorFlow
Please specify optimization flags to use during compilation when bazel option <span class="hljs-string">"--config=opt"</span> is specified [Default is -march=native]: n
Do you wish to build TensorFlow <span class="hljs-keyword">with</span> Google Cloud Platform support? [y/N] n
No Google Cloud Platform support will be enabled <span class="hljs-keyword">for</span> TensorFlow
Do you wish to build TensorFlow <span class="hljs-keyword">with</span> Hadoop File System support? [y/N] n
No Hadoop File System support will be enabled <span class="hljs-keyword">for</span> TensorFlow
Do you wish to build TensorFlow <span class="hljs-keyword">with</span> the XLA just-<span class="hljs-keyword">in</span>-time compiler (experimental)? [y/N] n
No XLA JIT support will be enabled <span class="hljs-keyword">for</span> TensorFlow
Do you wish to build TensorFlow <span class="hljs-keyword">with</span> VERBS support? [y/N] n
No VERBS support will be enabled <span class="hljs-keyword">for</span> TensorFlow
Do you wish to build TensorFlow <span class="hljs-keyword">with</span> OpenCL support? [y/N] n
No OpenCL support will be enabled <span class="hljs-keyword">for</span> TensorFlow
Do you wish to build TensorFlow <span class="hljs-keyword">with</span> CUDA support? [y/N] n
No CUDA support will be enabled <span class="hljs-keyword">for</span> TensorFlow
INFO: Starting clean (<span class="hljs-keyword">this</span> may take a <span class="hljs-keyword">while</span>). Consider using --<span class="hljs-keyword">async</span> <span class="hljs-keyword">if</span> the clean takes more than several minutes.
Configuration finished
  • 编译步骤,在--copt=-m后面添加你需要的指令集,之前警告有什么你就在这加什么就可以,我这里添加了avx avx2 fma sse4.1 sse4.2。编译过程时间很长,我的4770hq跑了快25min
$ bazel build -c opt --copt=-mavx --copt=-mavx2 --copt=-mfma --copt=-msse4<span class="hljs-number">.1</span> --copt=-msse4<span class="hljs-number">.2</span> <span class="hljs-comment">//tensorflow/tools/pip_package:build_pip_package</span>
  • 打包成whl
$ bazel-bin/tensorflow/tools/pip_package/build_pip_package /tmp/tensorflow_pkg
  • 安装这个whl,如果之前安装过Tensorflow的话先卸载,再安装
$ pip uninstall -y tensorflow
$ pip install /tmp/tensorflow_pkg/tensorflow-<span class="hljs-number">1.2</span>.<span class="hljs-number">1</span>-cp36-cp36m-macosx_10_7_x86_64.whl

完事,这样再调用Session就不会有提示了

运行效果如下

mac下编译安装TensorFlow

转载链接:https://www.jianshu.com/p/9d5806e7c248

admin
版权声明:本站原创文章,由admin2018-03-17发表,共计2804字。
转载提示:除特殊说明外本站文章皆由CC-4.0协议发布,转载请注明出处。
评论(没有评论)