Mac OS X 10.12.6 16G29
Python 3.6.2 (Anaconda 4.3.23)
Intel Core i7-4770HQ @ 2.20GHz
前言
如果直接通過pip install tensorflow
安裝Tensorflow的話嚷堡,在運行時可能會有這種警告
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.
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.
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.
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.
主要是因為你的CPU支持AVX2揭朝,SSE等指令集六荒,而pip安裝的庫沒有使用它們恩袱。啟用這些指令集會提升運算速度欣范,而且每次打印一大坨看著有點煩人鬼吵,所以這里我們從源碼編譯安裝Tensorflow雅潭。
編譯&安裝
- 先安裝一些依賴
$ brew install bazel swig
- 克隆代碼倉庫
$ git clone https://github.com/tensorflow/tensorflow.git
$ cd tensorflow
- master分支代碼一般很新担平,鬼知道有什么問題,這里用
v1.2.1
版本
$ git checkout v1.2.1
- 這里先configure芒率,過程中會問一些編譯過程中要用到的參數(shù)囤耳,比如Python的目錄,是否開啟OpenCL偶芍,CUDA支持充择,我這里直接全否了。
$ ./configure
Please specify the location of python. [Default is /usr/local/anaconda3/bin/python]:
Found possible Python library paths:
/usr/local/anaconda3/lib/python3.6/site-packages
Please input the desired Python library path to use. Default is [/usr/local/anaconda3/lib/python3.6/site-packages]
Using python library path: /usr/local/anaconda3/lib/python3.6/site-packages
Do you wish to build TensorFlow with MKL support? [y/N] n
No MKL 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]: n
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
INFO: Starting clean (this may take a while). Consider using --async if 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.1 --copt=-msse4.2 //tensorflow/tools/pip_package:build_pip_package
- 打包成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-1.2.1-cp36-cp36m-macosx_10_7_x86_64.whl
完事嘁捷,這樣再調用Session就不會有提示了
結尾
最后丟一個我編譯好的版本,1.2.1版本显熏,Python 3.6雄嚣,開啟avx avx2 fma sse4.1 sse4.2指令集,度盤喘蟆,GD缓升。
參考文章
Installing TensorFlow from Sources
Python: Keras/TensorFlow の學習を CPU の拡張命令で高速化する (Mac OS X) - CUBE SUGAR CONTAINER