Tensorflow CPU版を高速化する。
tensorflowを起動すると以下のようなワーニングが出る。
これまで無視していたが気になったので対応した。
Using TensorFlow backend. 2019-03-06 08:02:48.776153: W tensorflow/core/platform/cpu_feature_guard.cc:45] The TensorFlow library wasn't compiled to use SSE4.1 instructions, but these are available on your machine and could speed up CPU computations. 2019-03-06 08:02:48.776206: 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. 2019-03-06 08:02:48.776224: 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.
SSE4.1、 SSE4.2、AVXはCPU内蔵の拡張命令セットで一つの命令を複数のデータに適用し高速化する
tesorflowに適用するにはソースからコンパイルする必要があるようです。
目次
作業環境
- CPU: corei7-3610QM
- OS: Ubuntu16.04
- python: 3.5
Tensorflowをソースからコンパイル
tensorflowをソースからコンパイルするにはbazelが必要です。
また、tensorflowのバージョンに対応するバージョンのbazelをインストールする必要がある。
今回はtensorflowは1.2.0なのでbazelは0.4.5をインストールしました。
- tensorflowとBazelの対応
bazelのインストール
- 参考 bazelのインストール方法
- Bazelのインストールスクリプト
Release 0.4.5 · bazelbuild/bazel · GitHub
インストールスクリプトの実行
※ JDKが必要な場合は sudo apt install openjdk-8-jre openjdk-8-jdk
# 関連パッケージの取得 sudo apt update sudo apt install python3-dev, python3-numpy, python3-pip, python3-wheel sudo apt install pkg-config zip g++ zlib1g-dev unzip # bazelのインストールスクリプトの実行 chmod +x bazel-0.4.5-installer-linux-x86_64.sh ./ bazel-0.4.5-installer-linux-x86_64.sh --user export PATH="$PATH:$HOME/bin"
Tesnsorflowのビルド
tensorflowのソースを入手する。
直接サイトからzipを入手した。
- tensorflow 1.2.0のソース
「Tag」を「v1.2.0」に変更し、「Clone or download」の 「Dawnload ZIP」をクリック。
ソースのビルド
# 解凍 unzip tensorflow-1.2.0.zip cd tensorflow-1.2.0 # 設定 ./configure # 設定内容 python3に設定、あとはデフォルト Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3 Found possible Python library paths: /usr/lib/python3/dist-packages /usr/local/lib/python3.6/dist-packages Please input the desired Python library path to use. Default is [/usr/lib/python3/dist-packages] /usr/lib/python3/dist-packages Do you wish to build TensorFlow with MKL support? [y/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]: Do you wish to use jemalloc as the malloc implementation? [Y/n] jemalloc enabled Do you wish to build TensorFlow with Google Cloud Platform support? [y/N] No Google Cloud Platform support will be enabled for TensorFlow Do you wish to build TensorFlow with Hadoop File System support? [y/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] No XLA support will be enabled for TensorFlow Do you wish to build TensorFlow with VERBS support? [y/N] No VERBS support will be enabled for TensorFlow Do you wish to build TensorFlow with OpenCL support? [y/N] No OpenCL support will be enabled for TensorFlow Do you wish to build TensorFlow with CUDA support? [y/N] No CUDA support will be enabled for TensorFlow WARNING: The following rc files are no longer being read, please transfer their contents or import their path into one of the standard rc files: /home/user/tensorflow-1.2.0/tools/bazel.rc Extracting Bazel installation... Starting local Bazel server and connecting to it... INFO: Starting clean (this may take a while). Consider using --async if the clean takes more than several minutes. Configuration finished # ビルド bazel build -c opt --copt=-mavx --copt=-msse4.1 --copt=-msse4.2 //tensorflow/tools/pip_package:build_pip_package # whlを生成 ./tensorflow/tools/pip_package/build_pip_package.sh /tmp/tensorflow_pkg
※AVX2やFMAの場合
--copt=-mavx2 --copt=-mfmaを追加する
tensorflowのインストール
作成したWHLをpipでインストールする。
pip3 install /tmp/tensorflow_pkg/tensorflow-1.2.0-cp35-cp35m-linux_x86_64.whl
以上