bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
distributed_ppo.h
Go to the documentation of this file.
1#ifndef DISTRIBUTED_PPO_H
2#define DISTRIBUTED_PPO_H
3
4#include "bitrl/base/bitrl_config.h"
5
6#ifdef USE_PYTORCH
7
14
15#include <torch/torch.h>
16
17#include <memory>
18#include <vector>
19
20namespace bitrl
21{
22namespace rl::algos::pg
23{
24
50template <typename EnvType, typename PolicyType, typename CriticType>
51class DistributedPPOSolver final : public ParallelPPOSolver<EnvType, PolicyType, CriticType>
52{
53 public:
55 typedef typename base_type::env_type env_type;
56 typedef PolicyType policy_type;
57 typedef CriticType critic_type;
58
62 DistributedPPOSolver(const PPOConfig &config, policy_type &policy, critic_type &critic,
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),
67 communicator_(communicator)
68 {
69 }
70
79
81 {
82 auto info = base_type::on_training_episode(envs, round);
83 // the returns and the steps of all the ranks
84 std::vector<double> totals{0.0, static_cast<double>(this->last_returns().size()),
85 static_cast<double>(info.episode_iterations)};
86 for (const real_t r : this->last_returns())
87 {
88 totals[0] += r;
89 }
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);
94 return info;
95 }
96
100 const std::vector<real_t> &global_returns() const noexcept { return global_returns_; }
101
105 std::uint64_t checksum() const
106 {
108 const_cast<DistributedPPOSolver *>(this)->parameters_());
109 }
110
111 const rl::distributed::Communicator &communicator() const noexcept { return communicator_; }
112
113 protected:
114 void on_gradients_() override
115 {
116 rl::distributed::average_gradients(communicator_, parameters_());
117 }
118
119 private:
120 std::vector<torch::Tensor> parameters_()
121 {
122 auto p = this->policy_->parameters();
123 const auto c = this->critic_->parameters();
124 p.insert(p.end(), c.begin(), c.end());
125 return p;
126 }
127
128 rl::distributed::Communicator &communicator_;
129 std::vector<real_t> global_returns_;
130};
131
132} // namespace rl::algos::pg
133} // namespace bitrl
134
135#endif // USE_PYTORCH
136#endif // DISTRIBUTED_PPO_H
virtual void actions_before_training_begins(env_type &)=0
actions_before_training_begins. Execute any actions the algorithm needs before starting the iteration...
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
Definition a2c.h:37
void average_gradients(Communicator &communicator, const std::vector< torch::Tensor > &parameters)
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 > &parameters)
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 > &parameters, 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