1. Chainerx開発環境作成メモ
chainerxにcontributeしたいと思うとまず開発環境を作成しないといけない。
chainerの場合pythonだったので環境作成は簡単であったが
chainerxになってC++言語になったので環境作成に時間がかかった。
環境作成した時の手順をメモにして残す。
2. 手順
今回はGCPのComputer EngineにあるDeep Learning VMをベースに環境構築した。
2.1. git cloneする。
$ git clone https://github.com/chainer/chainer.git
2.2. ローカルのソース環境にpythonのパスを通す。
ソースコードのrootで以下を実行する。
$ python setup.py develop
2.3. コンパイル
https://docs.chainer.org/en/latest/chainerx/contribution.html
を参考に作成
2.3.1. ソースコードのrootで以下を実行する。
$ cd chainerx_cc
$ mkdir -p build
$ cd build
$ cmake-gui ..
2.3.2. cmakeのパラメータ変更
2.3.2.1. デフォルト値から以下のフラグを変更してONにする。
CHAINERX_BUILD_PYTHON
CHAINERX_BIULD_TEST
PYBIND11_INSTALL
USE_PYTHON_INCLUDE_DIR
CHAINERX_BUILD_CUDA(GPU使用時のみ)
2.3.2.2. 以下にインストールパスを設定(default /usr/local)
CMAKE_INSTALL_PREFIX インストールパス
※CMakeCache.txtのPYTHONのパスが正しいか確認する。
2.3.3. make
$ make
$ make install
2.3.4. _core.soをcopyする。ソースコードのrootで以下を実行する。
$ cp chainerx_cc/build/chainerx/python/_core.so chainerx/_core.so
3. 動作確認
Chainer(CPU),ChainerX(CPU),ChainerX(GPU)で動作確認
$ python
Python 3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34)
[GCC 7.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import chainerx as chx
>>> import numpy as np
>>> import chainer
>>> import chainer.functions as F
>>> from chainer import Variable
>>> a_np = np.asarray([[0, 1, -1], [3, 4, 5],[1,1,1]],dtype = np.float32)
>>> a = Variable(a_np)
>>> b = F.softmax(a)
>>> b.grad = np.asarray([[1,0,0],[1,0,0],[1,0,0]], dtype=np.float32)
>>> b.backward()
>>> b
variable([[0.24472848, 0.66524094, 0.09003057],
[0.09003057, 0.24472848, 0.66524094],
[0.33333334, 0.33333334, 0.33333334]])
>>> a.grad
array([[ 0.18483645, -0.1628034 , -0.02203305],
[ 0.08192507, -0.02203305, -0.05989202],
[ 0.22222222, -0.11111112, -0.11111112]], dtype=float32)
>>> x = chx.array([[0, 1, -1], [3, 4, 5],[1,1,1]], dtype=chx.float32).require_grad()
>>> y = chx.softmax(x)
>>> y.grad = chx.array([[1,0,0],[1,0,0],[1,0,0]], dtype=chx.float32)
>>> y.backward()
>>> y
array([[0.24472848, 0.66524094, 0.09003057],
[0.09003057, 0.24472848, 0.66524094],
[0.33333334, 0.33333334, 0.33333334]], shape=(3, 3), dtype=float32, device='native:0', backprop_ids=['<default>'])
>>> x.grad
array([[0.18483645, -0.16280340, -0.02203305],
[0.08192507, -0.02203305, -0.05989203],
[0.22222222, -0.11111113, -0.11111113]], shape=(3, 3), dtype=float32, device='native:0')
>>> cuda_x = chx.array([[0, 1, -1], [3, 4, 5],[1,1,1]], dtype=chx.float32,device='cuda:0').require_grad()
>>> cuda_y = chx.softmax(cuda_x)
>>> cuda_y.grad = chx.array([[1,0,0],[1,0,0],[1,0,0]], dtype=chx.float32, device='cuda:0')
>>> cuda_y.backward()
>>> cuda_y
array([[0.24472848, 0.66524094, 0.09003057],
[0.09003057, 0.24472845, 0.66524088],
[0.33333334, 0.33333334, 0.33333334]], shape=(3, 3), dtype=float32, device='cuda:0', backprop_ids=['<default>'])
>>> cuda_x.grad
array([[0.18483645, -0.16280340, -0.02203305],
[0.08192506, -0.02203304, -0.05989202],
[0.22222222, -0.11111113, -0.11111113]], shape=(3, 3), dtype=float32, device='cuda:0')
>>>
これでchainerxにcontributeできる(た)ぞ。
[Add chainerx::Softmax
and chainerx.softmax
#6814]