06. MPI/stable_baselinesインストール

★ MPIインストール


本サイト内で『 強化学習用アルゴリズム「PPO(Proximal Policy Optimization:近傍方策最適化)」の内「PPO1」はマルチプロセッシング仕様で「並列処理」を行いますが、MPI仕様です』という記載があります。

「並列」は電池の並びで「直列」「並列」にあるように物理的・位置的に別々の複数が仕事をする状態を指し、その際電池は複数個必要です。一方 「並行」は、ある意味兼業と同じで、実行する実体は必ず1つの処理しか行わない原則の元、高速で複数の仕事を同時に行うことを指します。だから実行している人は1人です。

今回扱うのは「並列処理」です。
「並列処理」は実行する側も複数、実行させる側も分配させたり収集したりを複数に対し行います。ウィンドウズは並列に処理をしますが、複数台同時やそのエミレーションは行えませんので、「Message Passing Interface(MPI)」という並列処理を行うための統一規格に則った「Microsoft MPI」をインストールし使用します。

「Microsoft MPI」は本サイトで言うなら、先ほどから登場している強化学習用外部モジュール「Stable Baselines」を使用する時のみに使用するほか、複数のマシン間での並列処理、同一マシン内での並列処理に使用します。並行処理ではないので注意すると同時に、それ以外には無用の長物です

★ ダウンロード

【注意事項】
初めにインストール前の重要な注意点です。
これから「Message Passing Interface(MPI)」サイトへ移動しダウンロードしてきますが、Microsoft MPIをインストール際、Microsoft MPIがバージョンの新旧に関わらずインストールされている場合、インストールできません

このため、アップデートする際も含め行うためには、事前にアンインストールが必要です(2023/06時点 v10.1.2対応時)。




① URLへ移動
下記URLへ移動してください。

https://learn.microsoft.com/ja-jp/message-passing-interface/microsoft-mpi#ms-mpi-source-code




② Download
該当URL内の「Download」をクリックしますが、該当ファイルは英語版1択で、2023/06時点ではSupported Operating Systemにwindows11記載が見受けられません。

[Supported Operating System]
Windows 7, Windows 8.1, Windows 10, Windows Server 2012 R2, Windows Server 2016, Windows Server 2019


③ インストール開始
「msmpisetup.exe」を実行すると、チェックは変更せず次々実行していきます。







★ 設定確認

コマンドプロンプトから下記のようにパスを確認します。


(py36) d:\VisualStudio2017\Python3.5_GPU\DeZero\DeZero_DQN\ch10>set MSMPI
MSMPI_BENCHMARKS=C:\Program Files\Microsoft MPI\Benchmarks\
MSMPI_BIN=C:\Program Files\Microsoft MPI\Bin\

次に「システム環境変数」内「PATH」に下記のようにパスが追加されていることを確認します。




同時に「システム環境変数」内に下記2つが追加されたことを確認します。



★ 動作確認

コマンドプロンプトから下記コマンドを発行して動作することを確認します。
インストール済で実行しない場合は、パスを再度確認して下さい。


(py36) d:\VisualStudio2017\Python3.5_GPU\DeZero\DeZero_DQN\ch10>mpiexec -help
Microsoft MPI Startup Program [Version 10.0.12498.5]

Launches an application on multiple hosts.

Usage:

    mpiexec [options] executable [args] [ : [options] exe [args] : ... ]
    mpiexec -configfile 

Common options:

-n 
-env  
-wdir 
-hosts n host1 [m1] host2 [m2] ... hostn [mn]
-cores 
-lines
-debug [0-3]
-logfile 

Examples:

    mpiexec -n 4 pi.exe
    mpiexec -hosts 1 server1 master : -n 8 worker

For a complete list of options, run mpiexec -help2
For a list of environment variables, run mpiexec -help3

You can reach the Microsoft MPI team via email at askmpi@microsoft.com

★ baselinesのインストール

「baselines」をインストールします。

外部モジュール「baselines」はtensorflow 1.14のGPU仕様に対応します。このためCUDA経由でGPUを動かす場合は、「tensorflow」をGPU仕様に変更する必要があります。 このためtensorflowをGPU仕様に変更するためには、一旦アンインスールして下記のようにインストールします。(CUDAも事前インストール必須)

pip install tensorflow_gpu==1.14

次に「baselines」のインストールですが、本来Ubuntu / Mac OS X 仕様でありwindowsインストール方法が記載されていないので止むを得ず、正規手段ではない方法でインストールします。

① GitHub openai/baselines をダウンロードします。
https://github.com/openai/baselines

② baselines-masterを解凍
「baselines-master.zip」を解凍するとフォルダー「baselines-master」が出来ます。

③ baselinesコピー 
フォルダー「baselines-master」内に「baselines」というフォルダーが作成されますので、フォルダー毎全部を作業フォルダー「ch10」内にコピーして終了です。

★ stable_baselinesのインストール

これからインストールする外部モジュールstable_baselinesはインストール時Microsoft MPIの有無によりインスール方法が変わります。

「Microsoft MPI」インストールの際に本来無用の長物と書きましたが、これからインストールする
    Stable Baselinesを使用する場合、MPIは必須
ということです。このため、どうしてもMPIインストールが嫌だという方は、いつものように pip でインストールしますが、多くのアルゴリズムは使用できず機能が限定されます

pip install stable-baselines

「Microsoft MPI」をインストールした方はStable Baselinesの全てのアルゴリズムを有効にする下記インストール方法で行います。


pip install stable-baselines[mpi]

★ stable_baselinesの確認

インストール完了かどうかは、内容は不問で下記サンプルプログラムを使用して動作確認します。 最後レンダリングまでできればOKです。


【case00_hello_baselines.py】
# coding: utf-8
# ////////////////////////////////////////////////////////////////////////////
# ///【CartPole-v1】                                                       ///
# ///                                                                      ///
# ///  【改訂履歴】                                                        ///
# /// V0.00 2022/07/05 プロトタイプ                                        ///
# ////////////////////////////////////////////////////////////////////////////
if "__file__" in globals():
    import os, sys
    sys.path.append(os.path.join(os.path.dirname(__file__), "..") )
    sys.path.append(os.path.join(os.path.dirname(__file__), "..") + "//..//")
# from common.nlp_util import *
# tensorflow_warning_cancel()
import gym
from stable_baselines.common.vec_env import DummyVecEnv
from stable_baselines import PPO2

env = gym.make("CartPole-v0")
env = DummyVecEnv([lambda: env])
model = PPO2("MlpPolicy", env, verbose=1)
model.learn(total_timesteps=1000)
state = env.reset()
while True:
    env.render()
    action, _ = model.predict(state, deterministic=True)
    state, rewards, done, info = env.step(action)
    if done:
        break

env.close()

# [result]
# (py36) d:\VisualStudio2017\Python3.5_GPU\DeZero\DeZero_DQN\ch10>python case00_hello_baselines.py
# C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\dtypes.py:516: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)typ# e'.
#   _np_qint8 = np.dtype([("qint8", np.int8, 1)])
# C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\dtypes.py:517: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)typ# e'.
#   _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
# C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\dtypes.py:518: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)typ# e'.
#   _np_qint16 = np.dtype([("qint16", np.int16, 1)])
# C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\dtypes.py:519: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)typ# e'.
#   _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
# C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\dtypes.py:520: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)typ# e'.
#   _np_qint32 = np.dtype([("qint32", np.int32, 1)])
# C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\framework\dtypes.py:525: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '(1,)typ# e'.
#   np_resource = np.dtype([("resource", np.ubyte, 1)])
# C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:541: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '# (1,)type'.
#   _np_qint8 = np.dtype([("qint8", np.int8, 1)])
# C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:542: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '# (1,)type'.
#   _np_quint8 = np.dtype([("quint8", np.uint8, 1)])
# C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:543: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '# (1,)type'.
#   _np_qint16 = np.dtype([("qint16", np.int16, 1)])
# C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:544: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '# (1,)type'.
#   _np_quint16 = np.dtype([("quint16", np.uint16, 1)])
# C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:545: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '# (1,)type'.
#   _np_qint32 = np.dtype([("qint32", np.int32, 1)])
# C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\tensorboard\compat\tensorflow_stub\dtypes.py:550: FutureWarning: Passing (type, 1) or '1type' as a synonym of type is deprecated; in a future version of numpy, it will be understood as (type, (1,)) / '# (1,)type'.
#   np_resource = np.dtype([("resource", np.ubyte, 1)])
# WARNING:tensorflow:From C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\stable_baselines\common\tf_util.py:57: The name tf.ConfigProto is deprecated. Please use tf.compat.v1.ConfigProto instead.
# 
# WARNING:tensorflow:From C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\stable_baselines\common\tf_util.py:66: The name tf.Session is deprecated. Please use tf.compat.v1.Session instead.
# 
# WARNING:tensorflow:From C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\stable_baselines\common\policies.py:115: The name tf.variable_scope is deprecated. Please use tf.compat.v1.variable_scope instead.
# 
# WARNING:tensorflow:From C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\stable_baselines\common\input.py:25: The name tf.placeholder is deprecated. Please use tf.compat.v1.placeholder instead.
# 
# WARNING:tensorflow:From C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\stable_baselines\common\policies.py:562: flatten (from tensorflow.python.layers.core) is deprecated and will be removed in a future version.
# Instructions for updating:
# Use keras.layers.flatten instead.
# WARNING:tensorflow:From C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\stable_baselines\a2c\utils.py:156: The name tf.get_variable is deprecated. Please use tf.compat.v1.get_variable instead.
# 
# WARNING:tensorflow:From C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\stable_baselines\ppo2\ppo2.py:193: The name tf.summary.scalar is deprecated. Please use tf.compat.v1.summary.scalar instead.
# 
# WARNING:tensorflow:From C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\tensorflow\python\ops\math_grad.py:1250: add_dispatch_support..wrapper (from tensorflow.python.ops.array_ops) is deprecated and will be removed in a future version.
# Instructions for updating:
# Use tf.where in 2.0, which has the same broadcast rule as np.where
# WARNING:tensorflow:From C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\stable_baselines\ppo2\ppo2.py:209: The name tf.train.AdamOptimizer is deprecated. Please use tf.compat.v1.train.AdamOptimizer instead.
# 
# WARNING:tensorflow:From C:\ProgramData\Anaconda3\envs\py36\lib\site-packages\stable_baselines\ppo2\ppo2.py:245: The name tf.summary.merge_all is deprecated. Please use tf.compat.v1.summary.merge_all instead.
# 
# --------------------------------------
# | approxkl           | 0.00021873586 |
# | clipfrac           | 0.0           |
# | explained_variance | -0.00127      |
# | fps                | 253           |
# | n_updates          | 1             |
# | policy_entropy     | 0.69292223    |
# | policy_loss        | -0.0035118635 |
# | serial_timesteps   | 128           |
# | time_elapsed       | 0             |
# | total_timesteps    | 128           |
# | value_loss         | 34.327312     |
# --------------------------------------
# --------------------------------------
# | approxkl           | 6.346035e-05  |
# | clipfrac           | 0.0           |
# | explained_variance | 0.0164        |
# | fps                | 787           |
# | n_updates          | 2             |
# | policy_entropy     | 0.6923793     |
# | policy_loss        | -0.0008587773 |
# | serial_timesteps   | 256           |
# | time_elapsed       | 0.505         |
# | total_timesteps    | 256           |
# | value_loss         | 48.824497     |
# --------------------------------------
# -------------------------------------
# | approxkl           | 6.731033e-06 |
# | clipfrac           | 0.0          |
# | explained_variance | 0.0103       |
# | fps                | 787          |
# | n_updates          | 3            |
# | policy_entropy     | 0.69191784   |
# | policy_loss        | 3.005017e-05 |
# | serial_timesteps   | 384          |
# | time_elapsed       | 0.668        |
# | total_timesteps    | 384          |
# | value_loss         | 48.761276    |
# -------------------------------------
# ---------------------------------------
# | approxkl           | 2.8752806e-06  |
# | clipfrac           | 0.0            |
# | explained_variance | -0.00833       |
# | fps                | 773            |
# | n_updates          | 4              |
# | policy_entropy     | 0.6919998      |
# | policy_loss        | -0.00010425784 |
# | serial_timesteps   | 512            |
# | time_elapsed       | 0.83           |
# | total_timesteps    | 512            |
# | value_loss         | 41.801987      |
# ---------------------------------------
# --------------------------------------
# | approxkl           | 1.0969635e-06 |
# | clipfrac           | 0.0           |
# | explained_variance | 0.00405       |
# | fps                | 792           |
# | n_updates          | 5             |
# | policy_entropy     | 0.691302      |
# | policy_loss        | 2.1914486e-05 |
# | serial_timesteps   | 640           |
# | time_elapsed       | 0.997         |
# | total_timesteps    | 640           |
# | value_loss         | 33.806343     |
# --------------------------------------
# ---------------------------------------
# | approxkl           | 2.9547932e-06  |
# | clipfrac           | 0.0            |
# | explained_variance | -0.00746       |
# | fps                | 787            |
# | n_updates          | 6              |
# | policy_entropy     | 0.69167846     |
# | policy_loss        | -0.00022557745 |
# | serial_timesteps   | 768            |
# | time_elapsed       | 1.16           |
# | total_timesteps    | 768            |
# | value_loss         | 40.427795      |
# ---------------------------------------
# --------------------------------------
# | approxkl           | 6.883509e-05  |
# | clipfrac           | 0.0           |
# | explained_variance | -0.0229       |
# | fps                | 731           |
# | n_updates          | 7             |
# | policy_entropy     | 0.6907279     |
# | policy_loss        | -0.0021043923 |
# | serial_timesteps   | 896           |
# | time_elapsed       | 1.32          |
# | total_timesteps    | 896           |
# | value_loss         | 40.58045      |
# --------------------------------------




≪清須電脳倶楽部メインページへ戻る場合はここをクリックしてください≫
Copyright(c)2018 GGE Kiyosu Cyber Club Allrights Reserved
inserted by FC2 system