bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
bitrl::rl::algos::pg::DistributedPPOSolver< EnvType, PolicyType, CriticType > Class Template Referencefinal

Proximal policy optimisation over several ranks, threads, processes or machines, that average their gradients. More...

#include <distributed_ppo.h>

Inheritance diagram for bitrl::rl::algos::pg::DistributedPPOSolver< EnvType, PolicyType, CriticType >:
Collaboration diagram for bitrl::rl::algos::pg::DistributedPPOSolver< EnvType, PolicyType, CriticType >:

Public Types

typedef ParallelPPOSolver< EnvType, PolicyType, CriticType > base_type
typedef base_type::env_type env_type
typedef PolicyType policy_type
typedef CriticType critic_type
Public Types inherited from bitrl::rl::algos::pg::ParallelPPOSolver< EnvType, PolicyType, CriticType >
typedef envs::ParallelEnvs< EnvType > env_type
typedef PolicyType policy_type
typedef CriticType critic_type
typedef EnvType::state_type state_type
typedef EnvType::action_type action_type
typedef ACSolverBase< env_type, PolicyType, CriticType, A2CMonitor< action_type, state_type >, PPOConfig > base_type
Public Types inherited from bitrl::rl::algos::pg::ACSolverBase< envs::ParallelEnvs< EnvType >, PolicyType, CriticType, A2CMonitor< EnvType::action_type, EnvType::state_type >, PPOConfig >
typedef EnvType env_type
 The environment type.
typedef PolicyType policy_type
 The policy or action type.
typedef CriticType critic_type
 The critic type.
typedef env_type::state_type state_type
typedef env_type::action_type action_type
typedef A2CMonitor< EnvType::action_type, EnvType::state_type > monitor_type
 The monitor type to use.
typedef monitor_type::experience_buffer_type experience_buffer_type
typedef monitor_type::experience_tuple_type experience_tuple_type
typedef PPOConfig config_type
 The solver configuration type.
Public Types inherited from bitrl::rl::algos::RLSolverBase< EnvType >
typedef EnvType env_type

Public Member Functions

 DistributedPPOSolver (const PPOConfig &config, policy_type &policy, critic_type &critic, std::unique_ptr< torch::optim::Optimizer > &policy_optimizer, std::unique_ptr< torch::optim::Optimizer > &critic_optimizer, rl::distributed::Communicator &communicator)
void actions_before_training_begins (env_type &envs) override
 Take the networks of rank 0, then get ready to train.
EpisodeInfo on_training_episode (env_type &envs, uint_t round) override
const std::vector< real_t > & global_returns () const noexcept
 The mean return of the episodes of all the ranks, per round.
std::uint64_t checksum () const
 A checksum of the networks, the same on every rank.
const rl::distributed::Communicator & communicator () const noexcept
Public Member Functions inherited from bitrl::rl::algos::pg::ParallelPPOSolver< EnvType, PolicyType, CriticType >
 ParallelPPOSolver (const PPOConfig &config, policy_type &policy, critic_type &critic, std::unique_ptr< torch::optim::Optimizer > &policy_optimizer, std::unique_ptr< torch::optim::Optimizer > &critic_optimizer)
EpisodeInfo on_training_episode (env_type &envs, uint_t round) override
 Play an episode in every copy and learn from all of them; the reward of the EpisodeInfo is the mean return of the episodes, its iterations the steps of all of them.
const std::vector< real_t > & last_returns () const noexcept
 The return of every episode of the last round.
Public Member Functions inherited from bitrl::rl::algos::pg::ACSolverBase< envs::ParallelEnvs< EnvType >, PolicyType, CriticType, A2CMonitor< EnvType::action_type, EnvType::state_type >, PPOConfig >
virtual ~ACSolverBase ()=default
 Destructor.
virtual void actions_after_training_ends (env_type &)
 actions_after_training_ends. Actions to execute after the training iterations have finisehd
virtual void actions_before_episode_begins (env_type &, uint_t)
 actions_before_training_episode
virtual void actions_after_episode_ends (env_type &, uint_t, const EpisodeInfo &)
 actions_after_training_episode
virtual void actions_before_training_begins (env_type &)
 actions_before_training_begins. Execute any actions the algorithm needs before starting the iterations
void set_train_mode () noexcept
 set_train_mode for both the Actor and the Critic
void set_evaluation_mode () noexcept
 set_evaluation_mode for both the Actor and the Critic
monitor_type & get_monitor ()
 Read-write access to the monitor object.
Public Member Functions inherited from bitrl::rl::algos::RLSolverBase< EnvType >
virtual ~RLSolverBase ()=default
 Destructor.
virtual EpisodeInfo on_training_episode (env_type &, uint_t)=0
 on_episode Do one on_episode of the algorithm

Protected Member Functions

void on_gradients_ () override
 Called after every backward pass, before the optimisers step, with the gradients of both networks in place; DistributedPPOSolver averages them over its ranks.
Protected Member Functions inherited from bitrl::rl::algos::pg::ACSolverBase< envs::ParallelEnvs< EnvType >, PolicyType, CriticType, A2CMonitor< EnvType::action_type, EnvType::state_type >, PPOConfig >
 ACSolverBase (const config_type &config, policy_type &policy, critic_type &critic, std::unique_ptr< torch::optim::Optimizer > &policy_optimizer, std::unique_ptr< torch::optim::Optimizer > &critic_optimizer)
 A2C.
uint_t create_episode_batch_ (env_type &env, uint_t, experience_buffer_type &buffer)
Protected Member Functions inherited from bitrl::rl::algos::RLSolverBase< EnvType >
 RLSolverBase ()=default
 Constructor.

Additional Inherited Members

Protected Attributes inherited from bitrl::rl::algos::pg::ACSolverBase< envs::ParallelEnvs< EnvType >, PolicyType, CriticType, A2CMonitor< EnvType::action_type, EnvType::state_type >, PPOConfig >
config_type config_
 config_
policy_type & policy_
 policy_
critic_type & critic_
 The action network.
monitor_type monitor_
 Helper class to monitor the algorithm.
std::unique_ptr< torch::optim::Optimizer > policy_optimizer_
 The policy_ optimzer.
std::unique_ptr< torch::optim::Optimizer > critic_optimizer_
 The optimizer for the critic network.

Detailed Description

template<typename EnvType, typename PolicyType, typename CriticType>
class bitrl::rl::algos::pg::DistributedPPOSolver< EnvType, PolicyType, CriticType >

Proximal policy optimisation over several ranks, threads, processes or machines, that average their gradients.

Every rank has its own copies of the environment, a bitrl::envs::ParallelEnvs seeded differently on every rank, and its own copy of the networks and their optimisers. Every training round each rank plays an episode in each of its copies, as ParallelPPOSolver does. Then, at every pass over the batch, each rank computes the gradients of its own episodes, the ranks replace them with their mean over all the ranks, through a bitrl::rl::distributed::Communicator, and every rank's optimisers take the same step.

Before training, every rank takes the networks of rank 0, so the ranks start alike. The mean of the gradients is the same on every rank to the last bit, so the ranks stay alike: the networks of the ranks are one network, trained on the episodes of all of them. This is the data parallelism of PyTorch's DistributedDataParallel.

Each rank normalises the advantages of its own episodes and averages the loss over its own steps, so every rank counts the same in the mean, whatever the length of its episodes.

The EpisodeInfo of a round gives the mean return of the episodes of all the ranks, and its iterations the steps of all of them.

Every rank must train for the same number of rounds. The policy and the critic are as for ParallelPPOSolver; their parameters must be float32.

Member Typedef Documentation

◆ base_type

template<typename EnvType, typename PolicyType, typename CriticType>
typedef ParallelPPOSolver<EnvType, PolicyType, CriticType> bitrl::rl::algos::pg::DistributedPPOSolver< EnvType, PolicyType, CriticType >::base_type

◆ critic_type

template<typename EnvType, typename PolicyType, typename CriticType>
typedef CriticType bitrl::rl::algos::pg::DistributedPPOSolver< EnvType, PolicyType, CriticType >::critic_type

◆ env_type

template<typename EnvType, typename PolicyType, typename CriticType>
typedef base_type::env_type bitrl::rl::algos::pg::DistributedPPOSolver< EnvType, PolicyType, CriticType >::env_type

◆ policy_type

template<typename EnvType, typename PolicyType, typename CriticType>
typedef PolicyType bitrl::rl::algos::pg::DistributedPPOSolver< EnvType, PolicyType, CriticType >::policy_type

Constructor & Destructor Documentation

◆ DistributedPPOSolver()

template<typename EnvType, typename PolicyType, typename CriticType>
bitrl::rl::algos::pg::DistributedPPOSolver< EnvType, PolicyType, CriticType >::DistributedPPOSolver ( const PPOConfig & config,
policy_type & policy,
critic_type & critic,
std::unique_ptr< torch::optim::Optimizer > & policy_optimizer,
std::unique_ptr< torch::optim::Optimizer > & critic_optimizer,
rl::distributed::Communicator & communicator )
inline
Parameters
communicatorhow the ranks meet; it must outlive the solver

Member Function Documentation

◆ actions_before_training_begins()

template<typename EnvType, typename PolicyType, typename CriticType>
void bitrl::rl::algos::pg::DistributedPPOSolver< EnvType, PolicyType, CriticType >::actions_before_training_begins ( env_type & envs)
inlineoverride

Take the networks of rank 0, then get ready to train.

◆ checksum()

template<typename EnvType, typename PolicyType, typename CriticType>
std::uint64_t bitrl::rl::algos::pg::DistributedPPOSolver< EnvType, PolicyType, CriticType >::checksum ( ) const
inline

A checksum of the networks, the same on every rank.

◆ communicator()

template<typename EnvType, typename PolicyType, typename CriticType>
const rl::distributed::Communicator & bitrl::rl::algos::pg::DistributedPPOSolver< EnvType, PolicyType, CriticType >::communicator ( ) const
inlinenoexcept

◆ global_returns()

template<typename EnvType, typename PolicyType, typename CriticType>
const std::vector< real_t > & bitrl::rl::algos::pg::DistributedPPOSolver< EnvType, PolicyType, CriticType >::global_returns ( ) const
inlinenoexcept

The mean return of the episodes of all the ranks, per round.

◆ on_gradients_()

template<typename EnvType, typename PolicyType, typename CriticType>
void bitrl::rl::algos::pg::DistributedPPOSolver< EnvType, PolicyType, CriticType >::on_gradients_ ( )
inlineoverrideprotectedvirtual

Called after every backward pass, before the optimisers step, with the gradients of both networks in place; DistributedPPOSolver averages them over its ranks.

Reimplemented from bitrl::rl::algos::pg::ParallelPPOSolver< EnvType, PolicyType, CriticType >.

◆ on_training_episode()

template<typename EnvType, typename PolicyType, typename CriticType>
EpisodeInfo bitrl::rl::algos::pg::DistributedPPOSolver< EnvType, PolicyType, CriticType >::on_training_episode ( env_type & envs,
uint_t round )
inlineoverride

The documentation for this class was generated from the following file: