最終更新日: 2019-03-03 日 21:10
ホーム | 文書トップ | 目次

CentOS7 TensorFlowビルド

Table of Contents

概要

要約

PC環境に合わせて TensorFlow をビルドして動作させることができた

環境

下記の環境で TensorFlow を導入しようとした。

  • Pentium 3805U 搭載の小型PC (Pentium 3805U PC 参照)
  • CentOS 7.6 64bit
  • Python 3.4
  • Jupyter
  • JupyterHub

不具合の現象

pip3 で tensorflow をインストールしたが、自動で取得された tensorflow ではうまく動作せず。

Jupyter Notebook上に下記のようなダイアログが出てPython3カーネルが再起動してしまう。

Kernel Restarting
The kernel appears to have died. It will restart automatically.

JupyterHub の標準出力には下記のメッセージが出ていた。

2019-03-02 21:36:03.586565: F tensorflow/core/platform/cpu_feature_guard.cc:37] The TensorFlow library was compiled to use AVX instructions, but these aren't available on your machine.
[I 2019-03-02 21:36:05.925 wurly restarter:103] KernelRestarter: restarting kernel (1/5)
WARNING:root:kernel 336e07a6-5964-424e-b6d6-41773f678549 restarted

AVXに対応したパッケージなので動作できないようだ。

Pentium 3805U の "Instruction Set Extensions" は SSE4.1、SSE4.2 だけでAVX には非対応である。

対処方法

調査結果

tensorflowを環境に合わせてビルドすれば使えそうであることがわかった。

現在のバージョン確認

自動で取得された tensorflow のバージョンを確認すると 1.13.1 であった。よって同バージョンをビルドする。

$ pip3 list | grep tensorflow
DEPRECATION: Python 3.4 support has been deprecated. pip 19.1 will be the last one supporting it. Please upgrade your Python as Python 3.4 won't be maintained after March 2019 (cf PEP 429).
tensorflow           1.13.1
tensorflow-estimator 1.13.0

TensorFlow/Python/Compiler/GCC/Bazel の対応バージョン

下記ページにテストされたビルド構成の記載があり。

ここには記載が無いが、TensorFlow 1.13.1 では Bazel 0.19 以上が必要とのこと。(Bazel 0.15でビルドしようとしたところその旨が表示された)

  • TensorFlow
  • Build from source
    • Tested build configurations
      • Linux
        • Version Python version Compiler Build tools
        • tensorflow-1.12.0 2.7, 3.3-3.6 GCC 4.8 Bazel 0.15.0

ビルド環境構築

bazelのインストール

下記の通り Bazel をインストールする。

ビルド手順

tensorflow ソースのクローン

$ git clone https://github.com/tensorflow/tensorflow
$ cd tensorflow
$ git tag
$ git checkout v1.13.1

configure

configure では、最初の "Please specify the location of python. [Default is /usr/bin/python]:" に対し、"/usr/bin/python3" と入力し、それ以降は全てデフォルトとした。

$ ./configure
You have bazel 0.19.2 installed.
Please specify the location of python. [Default is /usr/bin/python]: /usr/bin/python3
  (中略)
Configuration finished

ビルド

ビルドオプションは参考ページを見て、local_resources を指定した。

$ /usr/local/bin/bazel build --local_resources=2048,8,1.0 --verbose_failures --config=opt //tensorflow/tools/pip_package:build_pip_package

非力なCPUの為、ビルドは数時間かかった。

パッケージ作成

ビルドが終わったら、インストールするためのパッケージを作成する。

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

パッケージは /tmp/tensorflow_pkg に生成される。

$ ls -la /tmp/tensorflow_pkg/
合計 88632
-rw-rw-r--.  1 wurly wurly 90751626  3月  3 11:13 tensorflow-1.13.1-cp34-cp34m-linux_x86_64.whl

インストール済のものをアンインストール

まず、インストール済のものをアンインストールする。

$ sudo /usr/local/bin/pip3 uninstall tensorflow
  (中略)
  Successfully uninstalled tensorflow-1.13.1

ビルドしたパッケージをインストール

pip3で ファイルを指定してインストールする。

$ sudo /usr/local/bin/pip3 install /tmp/tensorflow_pkg/tensorflow-1.13.1-cp34-cp34m-linux_x86_64.whl
  (中略)
Successfully installed tensorflow-1.13.1

結果

非常に簡単なプログラムで tensorflow が動作することが確認できた。

import tensorflow as tf

hello = tf.constant('Hello, TensorFlow!')
sess = tf.Session()
print(sess.run(hello))

これはあくまでお試し(勉強)用で、実際のところ、AVX無効で TensorFlowを本格的に使うようなケースはあまり無いと思われるが、 せっかくなのでビルドしたものを tensorflow-1.13.1-cp34-cp34m-linux_x86_64.whl に置いておく。

躓いたところ

ビルドエラー 'cc1plus'

gcc: error trying to exec 'cc1plus': execvp: No such file or directory

gcc-c++ をインストールすることで解決

ビルドエラー this rule is missing dependency

this rule is missing dependency declarations for the following files included by 'external/nasm/regvals.c':
  '/usr/include/stdc-predef.h'
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/stddef.h'
  '/usr/lib/gcc/x86_64-redhat-linux/4.8.5/include/stdarg.h'
  '/usr/include/stdio.h'
   (中略)
Target //tensorflow/tools/pip_package:build_pip_package failed to build

上記を参考に、下記のcleanを行うことで解決

$ /usr/local/bin/bazel clean --expunge

作業の詳細履歴

ホーム | 文書トップ | 目次
Created by Emacs 27.1 (Org mode 9.3)