1#ifndef BITRL_RL_DISTRIBUTED_TORCH_PARAMETERS_H
2#define BITRL_RL_DISTRIBUTED_TORCH_PARAMETERS_H
4#include "bitrl/base/bitrl_config.h"
11#include <torch/torch.h>
27 if (t.scalar_type() != torch::kFloat32)
29 throw std::invalid_argument(
"bitrl::rl::distributed: the parameters must be float32");
33template <
typename Get>
34std::vector<float>
flatten_(
const std::vector<torch::Tensor> &tensors, Get get)
36 std::vector<float> flat;
37 for (
const auto &p : tensors)
40 const auto t = get(p);
41 const auto n =
static_cast<std::size_t
>(p.numel());
44 flat.insert(flat.end(), n, 0.0f);
47 const auto c = t.detach().contiguous();
48 flat.insert(flat.end(), c.template data_ptr<float>(), c.template data_ptr<float>() + n);
59 return detail::flatten_(parameters, [](
const torch::Tensor &p) {
return p; });
67 return detail::flatten_(parameters, [](
const torch::Tensor &p) {
return p.grad(); });
74 const std::vector<float> &values)
76 torch::NoGradGuard no_grad;
78 for (
auto p : parameters)
80 const auto n =
static_cast<std::size_t
>(p.numel());
81 if (at + n > values.size())
83 throw std::invalid_argument(
"set_parameters: too few values");
85 const auto src = torch::from_blob(
const_cast<float *
>(values.data() + at), p.sizes(),
86 torch::TensorOptions().dtype(torch::kFloat32));
90 if (at != values.size())
92 throw std::invalid_argument(
"set_parameters: too many values");
100 const std::vector<float> &values)
103 for (
auto p : parameters)
105 const auto n =
static_cast<std::size_t
>(p.numel());
106 if (at + n > values.size())
108 throw std::invalid_argument(
"set_gradients: too few values");
110 const auto src = torch::from_blob(
const_cast<float *
>(values.data() + at), p.sizes(),
111 torch::TensorOptions().dtype(torch::kFloat32))
113 if (p.mutable_grad().defined())
115 p.mutable_grad().copy_(src);
119 p.mutable_grad() = src;
123 if (at != values.size())
125 throw std::invalid_argument(
"set_gradients: too many values");
133 const std::vector<torch::Tensor> ¶meters)
137 const float n =
static_cast<float>(communicator.
world_size());
149 const std::vector<torch::Tensor> ¶meters,
uint_t root = 0)
162 std::uint64_t h = 1469598103934665603ULL;
163 for (
const float v : values)
166 std::memcpy(&bits, &v,
sizeof(bits));
167 for (
int b = 0; b < 32; b += 8)
169 h ^= (bits >> b) & 0xFFu;
170 h *= 1099511628211ULL;
How the ranks of a distributed training exchange numbers.
Definition communicator.h:32
virtual std::string broadcast(const std::string &bytes, uint_t root)=0
What root gave; the other ranks' bytes are ignored.
void all_reduce_sum(std::vector< T > &values)
Replace values with the sum of every rank's values.
Definition communicator.h:110
virtual uint_t world_size() const noexcept=0
Definition torch_parameters.h:24
void check_float_(const torch::Tensor &t)
Definition torch_parameters.h:25
std::vector< float > flatten_(const std::vector< torch::Tensor > &tensors, Get get)
Definition torch_parameters.h:34
Definition communicator.h:17
std::vector< float > flatten_gradients(const std::vector< torch::Tensor > ¶meters)
The gradients of the parameters, one after the other; 0 where a parameter has none.
Definition torch_parameters.h:65
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
void set_parameters(const std::vector< torch::Tensor > ¶meters, const std::vector< float > &values)
Copy values, as flatten_parameters() gives them, into the parameters.
Definition torch_parameters.h:73
void set_gradients(const std::vector< torch::Tensor > ¶meters, const std::vector< float > &values)
Copy values, as flatten_gradients() gives them, into the gradients.
Definition torch_parameters.h:99
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
std::vector< float > flatten_parameters(const std::vector< torch::Tensor > ¶meters)
The values of the parameters, one after the other.
Definition torch_parameters.h:57
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
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