ディープラーニングやろうぜ! MacBookPro-MacOSX10.10にGPUを使えるCaffeをビルドする

テスト自動化サービスのShouldBeeでは、テスト作業のさらなる省力化のために人工知能を用いた機能を開発しています。今回は、ディープラーニングフレームワークであるCaffeをインストールして使える状態にしていきます。
MacBookPro(Late 2013)は、NVIDIAのGeForce GT 750Mが搭載されているので、CUDAが利用できます。
本記事は2015/7/31時点の段階で一番確実なビルド方法です。
これまでは、libstdc++を使うためのFormulaの変更が必要でしたが、libstdc++問題は現在は発生しないので、Formulaの書き換えは不要になりました。
> pyenvを使ってanaconda 2.1.0をインストールするpyenvを使ってanaconda 2.1.0をインストールする
AnacondaはPythonの数値計算環境を構築するために様々なパッケージをまとめた無料のディストリビューションです。 インストールを行うことで、NumPy,SciPy,matplotlibはもちろん、機械学習ライブラリscikit-learn等のパッケージをインストールできます。
とのことなので、今回はpython環境にanacondaを使っていきます。
brew install pyenv
export PYENV_ROOT=/usr/local/var/pyenv
# もしくは、export $HOME/.pyenv
# 記事中では、$HOME/.pyenvとなっていますので適切に読み変えてください。
pyenv install anaconda-2.1.0 # とても時間がかかります
> anaconda-2.1.0を使うように選択するanaconda-2.1.0を使うように選択する
pyenv local anaconda-2.1.0
pyenv global anaconda-2.1.0
> pyenvを使うようにshellを設定pyenvを使うようにshellを設定
.zshrcや、.bashrcに下記を記述して、sourceコマンドで読み込ませる
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
最後に、 pyenv rehash
する。
> 各種依存のインストール各種依存のインストール
brew install --fresh -vd snappy leveldb gflags glog szip lmdb
brew tap homebrew/science
brew install hdf5 opencv
> BLASのインストールBLASのインストール
Basic Linear Algebra Subprograms(BLAS)は、ベクトルと行列に関する基本線型代数操作を実行するライブラリAPIのデファクトスタンダードである
とのことなので、今回はopenblasを使う
brew tap homebrew/science
brew install homebrew/science/openblas
> boostと、boost-pythonboostと、boost-python
boostは、1.55.0を使用しないとcaffeがビルドできない。
cd /usr/local
git checkout a252214 /usr/local/Library/Formula/boost.rb
boost-pythonのformulaを編集して、boost1.55を使用するよう変更する。
brew edit boost-python
class BoostPython < Formula
homepage "http://www.boost.org"
--> url 'https://downloads.sourceforge.net/project/boost/boost/1.55.0/boost_1_55_0.tar.bz2'
--> sha1 'cef9a0cc7084b1d639e06cd3bc34e4251524c840'
最後にインストール
brew install --build-from-source --fresh -vd boost boost-python
> CUDAのインストールCUDAのインストール
> 設定→省電力の設定を変更して常にGPUを有効にする設定→省電力の設定を変更して常にGPUを有効にする

brew cask install cuda
> カーネルエクステンションのロードカーネルエクステンションのロード
sudo kextload /System/Library/Extensions/CUDA.kext
> devieQueryでCUDAデバイスが認識しているか確認devieQueryでCUDAデバイスが認識しているか確認
cd /Developer/NVIDIA/CUDA-7.0/samples/1_Utilities/deviceQuery
make
./deviceQuery
下記のように認識していればOK
./deviceQuery Starting...
CUDA Device Query (Runtime API) version (CUDART static linking)
Detected 1 CUDA Capable device(s)
Device 0: "GeForce GT 750M"
CUDA Driver Version / Runtime Version 7.0 / 7.0
CUDA Capability Major/Minor version number: 3.0
Total amount of global memory: 2048 MBytes (2147024896 bytes)
( 2) Multiprocessors, (192) CUDA Cores/MP: 384 CUDA Cores
GPU Max Clock rate: 926 MHz (0.93 GHz)
Memory Clock rate: 2508 Mhz
Memory Bus Width: 128-bit
L2 Cache Size: 262144 bytes
Maximum Texture Dimension Size (x,y,z) 1D=(65536), 2D=(65536, 65536), 3D=(4096, 4096, 4096)
Maximum Layered 1D Texture Size, (num) layers 1D=(16384), 2048 layers
Maximum Layered 2D Texture Size, (num) layers 2D=(16384, 16384), 2048 layers
Total amount of constant memory: 65536 bytes
Total amount of shared memory per block: 49152 bytes
Total number of registers available per block: 65536
Warp size: 32
Maximum number of threads per multiprocessor: 2048
Maximum number of threads per block: 1024
Max dimension size of a thread block (x,y,z): (1024, 1024, 64)
Max dimension size of a grid size (x,y,z): (2147483647, 65535, 65535)
Maximum memory pitch: 2147483647 bytes
Texture alignment: 512 bytes
Concurrent copy and kernel execution: Yes with 1 copy engine(s)
Run time limit on kernels: Yes
Integrated GPU sharing Host Memory: No
Support host page-locked memory mapping: Yes
Alignment requirement for Surfaces: Yes
Device has ECC support: Disabled
Device supports Unified Addressing (UVA): Yes
Device PCI Domain ID / Bus ID / location ID: 0 / 1 / 0
Compute Mode:
< Default (multiple host threads can use ::cudaSetDevice() with device simultaneously) >
deviceQuery, CUDA Driver = CUDART, CUDA Driver Version = 7.0, CUDA Runtime Version = 7.0, NumDevs = 1, Device0 = GeForce GT 750M
Result = PASS
> dylibライブラリの指定dylibライブラリの指定
export DYLD_FALLBACK_LIBRARY_PATH=/usr/local/cuda/lib:/Users/reoring/.pyenv/versions/anaconda-2.1.0/lib:/usr/local/lib
> CaffeのビルドCaffeのビルド
> ソースコード取得ソースコード取得
git clone https://github.com/BVLC/caffe.git
cd caffe
> ビルドビルド
mkdir build
cd build
cmakeでMakefileを生成する。こときに、-DPYTHON_LIBRARYでanacondaのlibpython2.7.dylibを指定しないと、実行時にSegfaultで落ちる。
cmake -DPYTHON_LIBRARY=/Users/reoring/.pyenv/versions/anaconda-2.1.0/lib/libpython2.7.dylib ..
cmakeの出力が下記のようになっていればOK
-- Boost version: 1.55.0
-- Found the following Boost libraries:
-- system
-- thread
-- Found gflags (include: /usr/local/include, library: /usr/local/lib/libgflags.dylib)
-- Found glog (include: /usr/local/include, library: /usr/local/lib/libglog.dylib)
-- Found PROTOBUF Compiler: /usr/local/bin/protoc
-- Found lmdb (include: /usr/local/include, library: /usr/local/lib/liblmdb.dylib)
-- Found LevelDB (include: /usr/local/include, library: /usr/local/lib/libleveldb.dylib)
-- Found Snappy (include: /usr/local/include, library: /usr/local/lib/libsnappy.dylib)
-- CUDA detected: 7.0
-- Added CUDA NVCC flags for: sm_30
-- Cuda + Boost 1.55: Applying noinline work around
-- OpenCV found (/usr/local/share/OpenCV)
-- Found vecLib as part of Accelerate.framework
-- NumPy ver. 1.9.0 found (include: /Users/reoring/.pyenv/versions/anaconda-2.1.0/lib/python2.7/site-packages/numpy/core/include)
-- Boost version: 1.55.0
-- Found the following Boost libraries:
-- python
-- Could NOT find Doxygen (missing: DOXYGEN_EXECUTABLE)
--
-- ******************* Caffe Configuration Summary *******************
-- General:
-- Version : <TODO> (Caffe doesn't declare its version in headers)
-- Git : rc2-362-g7f70854
-- System : Darwin
-- C++ compiler : /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/c++
-- Release CXX flags : -O3 -DNDEBUG -fPIC -Wall -Wno-sign-compare -Wno-uninitialized
-- Debug CXX flags : -g -fPIC -Wall -Wno-sign-compare -Wno-uninitialized
-- Build type : Release
--
-- BUILD_SHARED_LIBS : ON
-- BUILD_python : ON
-- BUILD_matlab : OFF
-- BUILD_docs : ON
-- CPU_ONLY : OFF
--
-- Dependencies:
-- BLAS : Yes (vecLib)
-- Boost : Yes (ver. 1.55)
-- glog : Yes
-- gflags : Yes
-- protobuf : Yes (ver. 2.6.1)
-- lmdb : Yes (ver. 0.9.14)
-- Snappy : Yes (ver. 1.1.3)
-- LevelDB : Yes (ver. 1.18)
-- OpenCV : Yes (ver. 2.4.11)
-- CUDA : Yes (ver. 7.0)
--
-- NVIDIA CUDA:
-- Target GPU(s) : Auto
-- GPU arch(s) : sm_30
-- cuDNN : Not found
--
-- Python:
-- Interpreter : /Users/reoring/.pyenv/shims/python2.7 (ver. 2.7.10)
-- Libraries : /Users/reoring/.pyenv/versions/anaconda-2.1.0/lib/libpython2.7.dylib (ver 2.7.6)
-- NumPy : /Users/reoring/.pyenv/versions/anaconda-2.1.0/lib/python2.7/site-packages/numpy/core/include (ver 1.9.0)
--
-- Documentaion:
-- Doxygen : No
-- config_file :
--
-- Install:
-- Install path : /Users/reoring/git/github/caffe/build/install
--
-- Configuring done
-- Generating done
-- Build files have been written to: /Users/reoring/git/github/caffe/build
> MNISTを使って学習のテストをしてみるMNISTを使って学習のテストをしてみる
cd /path/to/caffe
./data/mnist/get_mnist.sh
./examples/mnist/create_mnist.sh
./examples/mnist/train_lenet.sh
GPUが使われていれば先頭に下記のようなメッセージがでるはずです。
I0731 19:32:39.669621 2005259008 caffe.cpp:113] Use GPU with device ID 0
> エラーエラー
> Python error__colon__ PyThreadState_Get__colon__ no current threadPython error: PyThreadState_Get: no current thread
boost-pythonの1.55.0を、1.58.0にして再インストールすると治りますが、caffeのビルドには失敗するかもしれません。
brew uninstall boost-python
brew install --build-from-source --fresh -vd boost-python