1#ifndef DISTRIBUTED_PPO_H
2#define DISTRIBUTED_PPO_H
4#include "bitrl/base/bitrl_config.h"
15#include <torch/torch.h>
50template <
typename EnvType,
typename PolicyType,
typename CriticType>
63 std::unique_ptr<torch::optim::Optimizer> &policy_optimizer,
64 std::unique_ptr<torch::optim::Optimizer> &critic_optimizer,
66 :
base_type(config, policy, critic, policy_optimizer, critic_optimizer),
84 std::vector<double> totals{0.0,
static_cast<double>(this->
last_returns().size()),
85 static_cast<double>(info.episode_iterations)};
90 communicator_.all_reduce_sum(totals);
91 info.episode_reward = totals[0] / totals[1];
92 info.episode_iterations =
static_cast<uint_t>(totals[2]);
93 global_returns_.push_back(info.episode_reward);
100 const std::vector<real_t> &
global_returns() const noexcept {
return global_returns_; }
120 std::vector<torch::Tensor> parameters_()
122 auto p = this->
policy_->parameters();
123 const auto c = this->
critic_->parameters();
124 p.insert(p.end(), c.begin(), c.end());
129 std::vector<real_t> global_returns_;
virtual void actions_before_training_begins(env_type &)=0
actions_before_training_begins. Execute any actions the algorithm needs before starting the iteration...
policy_type & policy_
Definition actor_critic_solver_base.h:161
critic_type & critic_
Definition actor_critic_solver_base.h:166
base_type::env_type env_type
Definition distributed_ppo.h:55
CriticType critic_type
Definition distributed_ppo.h:57
void actions_before_training_begins(env_type &envs) override
Take the networks of rank 0, then get ready to train.
Definition distributed_ppo.h:74
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)
Definition distributed_ppo.h:62
std::uint64_t checksum() const
A checksum of the networks, the same on every rank.
Definition distributed_ppo.h:105
PolicyType policy_type
Definition distributed_ppo.h:56
EpisodeInfo on_training_episode(env_type &envs, uint_t round) override
Definition distributed_ppo.h:80
ParallelPPOSolver< EnvType, PolicyType, CriticType > base_type
Definition distributed_ppo.h:54
const rl::distributed::Communicator & communicator() const noexcept
Definition distributed_ppo.h:111
void on_gradients_() override
Called after every backward pass, before the optimisers step, with the gradients of both networks in ...
Definition distributed_ppo.h:114
const std::vector< real_t > & global_returns() const noexcept
The mean return of the episodes of all the ranks, per round.
Definition distributed_ppo.h:100
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)
Definition parallel_ppo.h:70
envs::ParallelEnvs< EnvType > env_type
Definition parallel_ppo.h:61
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 r...
Definition parallel_ppo.h:152
const std::vector< real_t > & last_returns() const noexcept
The return of every episode of the last round.
Definition parallel_ppo.h:86
How the ranks of a distributed training exchange numbers.
Definition communicator.h:32
void average_gradients(Communicator &communicator, const std::vector< torch::Tensor > ¶meters)
Replace the gradients of every rank with their mean over the ranks.
Definition torch_parameters.h:132
std::uint64_t parameters_checksum(const std::vector< torch::Tensor > ¶meters)
A checksum of the parameters, to tell that the ranks have the same.
Definition torch_parameters.h:159
void broadcast_parameters(Communicator &communicator, const std::vector< torch::Tensor > ¶meters, uint_t root=0)
Give every rank the parameters of root.
Definition torch_parameters.h:148
Definition with_dynamics_mixin.h:14
Various utilities used when working with RL problems.
Definition bitrl_log.h:25
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
The EpisodeInfo struct.
Definition episode_info.h:21
The PPOConfig struct. Configuration for PPOSolver class.
Definition ppo_config.h:26