シンギュラリティ実験ノート

購入した1000ドルPCで何がどこまでできるのか実験した記録です。

将棋AI dlshogi(Python版)を1000ドルPCで動かす

DeepLearning系将棋AIのdlshogiをUM790 Proにインストールした。GPUで動作させたかったのでUbuntu環境を使用した。今回紹介するのはPython版である。CPUでも動作させたのでその性能比についても報告したい。

 

インストール手順

dlshogiを知らない方はdlshogi作者の山岡忠夫氏が書かれた「強い将棋ソフトの創り方」を読むことをお勧めする。この中にdlshogiの仕組みはもちろん、インストール手順も詳しく書かれている。

 

動かすのに必要なソース等は公開されており、更にGoogle Colabで動かすためのノートはこちらに公開されている。このノートを参考にしてインストールできるが、ノートはGoogle Colab環境用なので、Ubuntu環境でとりあえずdlshogiを動作させたいのであれば、導入先フォルダを作成して以下のコマンドを実行してほしい。

$ pip install cshogi
$ git clone https://github.com/TadaoYamaoka/python-dlshogi2.git
$ cd python-dlshogi2
$ pip install -e .

次に起動シェルmcts_player.shの内容を修正する。GPUで動作させるため環境変数HSA_OVERRIDE_GFX_VERSIONを設定し、pythonをpython3に変更する。

#!/bin/sh
HSA_OVERRIDE_GFX_VERSION=11.0.0  python3 -m pydlshogi2.player.mcts_player

起動シェルに実行権を付与する。

$ chmod +x mcts_player.sh

ターミナルから起動シェルを実行し、動作確認する。プログラムは入力待ちとなるので、setoption、isreadyなどのコマンドを入力するとプログラムが応答を返す。

$ ./mcts_player.sh 
setoption name debug value true
setoption name modelfile value checkpoints/checkpoint-001.pth
isready
readyok
position startpos
'  9  8  7  6  5  4  3  2  1
P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
P2 * -HI *  *  *  *  * -KA * 
P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
P4 *  *  *  *  *  *  *  *  * 
P5 *  *  *  *  *  *  *  *  * 
P6 *  *  *  *  *  *  *  *  * 
P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
P8 * +KA *  *  *  *  * +HI * 
P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+

go byoyomi 1000
info nps 203 time 506 nodes 103 score cp 52 pv 7g7f 8c8d 7i6h 3c3d 6h7g 3a4b 2g2f
info nps 315 time 912 nodes 288 score cp 54 pv 7g7f 8c8d 7i6h 3c3d 6h7g 3a4b 2g2f 4b3c 3i4h 7a6b 3g3f 4a3b 6i7h
  0:1g1f  move_count:   0 nn_rate:0.02069 win_rate:0.00000
  1:2g2f  move_count: 120 nn_rate:0.41731 win_rate:0.52517
  2:3g3f  move_count:   0 nn_rate:0.00016 win_rate:0.00000
  3:4g4f  move_count:   0 nn_rate:0.00029 win_rate:0.00000
  4:5g5f  move_count:   0 nn_rate:0.00163 win_rate:0.00000
  5:6g6f  move_count:   0 nn_rate:0.00490 win_rate:0.00000
  6:7g7f  move_count: 168 nn_rate:0.50487 win_rate:0.52282
  7:8g8f  move_count:   0 nn_rate:0.00004 win_rate:0.00000
  8:9g9f  move_count:   0 nn_rate:0.00611 win_rate:0.00000
  9:1i1h  move_count:   0 nn_rate:0.00003 win_rate:0.00000
 10:9i9h  move_count:   0 nn_rate:0.00000 win_rate:0.00000
 11:3i3h  move_count:   0 nn_rate:0.00594 win_rate:0.00000
 12:3i4h  move_count:   0 nn_rate:0.00677 win_rate:0.00000
 13:7i6h  move_count:   0 nn_rate:0.00068 win_rate:0.00000
 14:7i7h  move_count:   0 nn_rate:0.00159 win_rate:0.00000
 15:2h1h  move_count:   0 nn_rate:0.00004 win_rate:0.00000
 16:2h3h  move_count:   0 nn_rate:0.00005 win_rate:0.00000
 17:2h4h  move_count:   0 nn_rate:0.00007 win_rate:0.00000
 18:2h5h  move_count:   0 nn_rate:0.00407 win_rate:0.00000
 19:2h6h  move_count:   0 nn_rate:0.00259 win_rate:0.00000
 20:2h7h  move_count:   0 nn_rate:0.00519 win_rate:0.00000
 21:4i3h  move_count:   0 nn_rate:0.00001 win_rate:0.00000
 22:4i4h  move_count:   0 nn_rate:0.00001 win_rate:0.00000
 23:4i5h  move_count:   0 nn_rate:0.00108 win_rate:0.00000
 24:6i5h  move_count:   0 nn_rate:0.00013 win_rate:0.00000
 25:6i6h  move_count:   0 nn_rate:0.00004 win_rate:0.00000
 26:6i7h  move_count:   0 nn_rate:0.01382 win_rate:0.00000
 27:5i4h  move_count:   0 nn_rate:0.00039 win_rate:0.00000
 28:5i5h  move_count:   0 nn_rate:0.00011 win_rate:0.00000
 29:5i6h  move_count:   0 nn_rate:0.00139 win_rate:0.00000
bestmove 7g7f ponder 8c8d

infoコマンドで表示されるNPS値を確認すると、203、315という数値になっている。Mac mini Late2012では私の記録ではNPS=52となっていたので約6倍といったところか。

将棋GUIのインストール

動作確認できたら次は将棋GUIのインストールだ。将棋GUIには将棋所とShogiGUIの2つのソフトがある。どちらもWindows用であるが、Ubuntuでも動作する。インストール方法は将棋所のページに詳しく書いてあるので、まずこちらからインストールするといいだろう。GUIがインストールできたら将棋エンジンとしてdlshogiを追加する。詳細は将棋GUIのページを確認してほしい。Python版dlshogi同士で対戦させた結果が以下である。

将棋所でdlshogi同士を対戦させた結果

文字が小さくて分からないと思うが、将棋所ではNPSが2000近くで表示される。こんなに早いのかと最初は喜んだが、別途導入したShogiGUIではNPSは800程度であった。おそらくShogiGUIの表示を信用した方がよいと思う。ただコマンド起動では200程度となっており、同じエンジンでなぜこんなにバラツキがでるのかはよく分からない。

CPUでdlshogiを動かす

GPUからCPUへ変更する方法は、pythonのソースmcts_player.pyで宣言している変数DEFAULT_GPU_IDの値を0から-1に変更するだけである。

# デフォルトGPU ID
DEFAULT_GPU_ID = -1

CPUで動作確認した結果が以下である。

$ ./mcts_player.sh 
setoption name debug value true
setoption name modelfile value checkpoints/checkpoint-001.pth
isready
readyok
position startpos
'  9  8  7  6  5  4  3  2  1
P1-KY-KE-GI-KI-OU-KI-GI-KE-KY
P2 * -HI *  *  *  *  * -KA * 
P3-FU-FU-FU-FU-FU-FU-FU-FU-FU
P4 *  *  *  *  *  *  *  *  * 
P5 *  *  *  *  *  *  *  *  * 
P6 *  *  *  *  *  *  *  *  * 
P7+FU+FU+FU+FU+FU+FU+FU+FU+FU
P8 * +KA *  *  *  *  * +HI * 
P9+KY+KE+GI+KI+OU+KI+GI+KE+KY
+

go byoyomi 1000
info nps 625 time 511 nodes 320 score cp 51 pv 7g7f 8c8d 7i6h 3c3d 6h7g 3a4b 2g2f 4b3c 3i4h 7a6b 3g3f 4a3b 6i7h 7c7d
info nps 578 time 940 nodes 544 score cp 47 pv 7g7f 8c8d 2g2f 8d8e 8h7g 3c3d 7i6h 2b7g+ 6h7g 3a2b 6i7h 2b3c 3i3h 7a7b
  0:1g1f  move_count:   0 nn_rate:0.02069 win_rate:0.00000
  1:2g2f  move_count: 240 nn_rate:0.41732 win_rate:0.52359
  2:3g3f  move_count:   0 nn_rate:0.00016 win_rate:0.00000
  3:4g4f  move_count:   0 nn_rate:0.00029 win_rate:0.00000
  4:5g5f  move_count:   0 nn_rate:0.00163 win_rate:0.00000
  5:6g6f  move_count:   0 nn_rate:0.00490 win_rate:0.00000
  6:7g7f  move_count: 304 nn_rate:0.50487 win_rate:0.51967
  7:8g8f  move_count:   0 nn_rate:0.00004 win_rate:0.00000
  8:9g9f  move_count:   0 nn_rate:0.00611 win_rate:0.00000
  9:1i1h  move_count:   0 nn_rate:0.00003 win_rate:0.00000
 10:9i9h  move_count:   0 nn_rate:0.00000 win_rate:0.00000
 11:3i3h  move_count:   0 nn_rate:0.00594 win_rate:0.00000
 12:3i4h  move_count:   0 nn_rate:0.00677 win_rate:0.00000
 13:7i6h  move_count:   0 nn_rate:0.00068 win_rate:0.00000
 14:7i7h  move_count:   0 nn_rate:0.00159 win_rate:0.00000
 15:2h1h  move_count:   0 nn_rate:0.00004 win_rate:0.00000
 16:2h3h  move_count:   0 nn_rate:0.00005 win_rate:0.00000
 17:2h4h  move_count:   0 nn_rate:0.00007 win_rate:0.00000
 18:2h5h  move_count:   0 nn_rate:0.00407 win_rate:0.00000
 19:2h6h  move_count:   0 nn_rate:0.00259 win_rate:0.00000
 20:2h7h  move_count:   0 nn_rate:0.00519 win_rate:0.00000
 21:4i3h  move_count:   0 nn_rate:0.00001 win_rate:0.00000
 22:4i4h  move_count:   0 nn_rate:0.00001 win_rate:0.00000
 23:4i5h  move_count:   0 nn_rate:0.00108 win_rate:0.00000
 24:6i5h  move_count:   0 nn_rate:0.00013 win_rate:0.00000
 25:6i6h  move_count:   0 nn_rate:0.00004 win_rate:0.00000
 26:6i7h  move_count:   0 nn_rate:0.01382 win_rate:0.00000
 27:5i4h  move_count:   0 nn_rate:0.00039 win_rate:0.00000
 28:5i5h  move_count:   0 nn_rate:0.00011 win_rate:0.00000
 29:5i6h  move_count:   0 nn_rate:0.00139 win_rate:0.00000
bestmove 7g7f ponder 8c8d

NPSが578、625という値を示している。CPUの方が早いのだ。いろいろ苦労してGPUを動かしたのだが、結果的にはCPUの方が早かったというのが今回のオチである(笑)。CPUが動作している時のシステムモニタを見ると16のCPU(スレッド?)が動作していて壮観である。GPUの動作状況はUbuntuではモニタできない。何か方法があれば確認したいと思っている。

システムモニタ

なお、C++版のdlshogiも本日動作を確認したので別の記事で紹介したい。コンパイルも問題なく通るので、興味のある方は参考にして動かして欲しい。今日も王将戦が行われていたが、自分で将棋AIに棋譜を入力しながら観戦すると、どちらがどれくらい優勢か、どんな手が考えられるのかを将棋AIが示してくれる。それらとプロの解説を聞きながら将棋を観戦すると、将棋を知らない人でも結構楽しめるのだ。是非一度やってみてほしい。