The noise \(\epsilon_k\) of pair \(k\) in generation \(g\) is drawn from the seed task_seed(task_seed(seed, g), k), so every rank can draw every direction itself. Once it has the returns of all the pairs, every rank computes the same gradient, adding the pairs in the same order, and takes the same Adam step. That keeps the ranks alike without ever sending a parameter, and the run is the run on one rank, to the last bit.
A rank sends the returns of its own pairs, 16 numbers here, 128 bytes, whatever the size of the network. PPO over several ranks sends every rank's gradients instead, 4 bytes per weight: a network of a million weights would send 4 MB per rank and update, and ES would still send 128 bytes. That is why evolution strategies scale to thousands of cores.
Swinging the pole up: 300 generations of 64 episodes
1. One rank, on one thread
8.67 s; the return of the mean policy 288.9
2. 4 ranks, each on one thread, every rank 8 of the 32 pairs
2.99 s, a speed-up of 2.90; the return of the mean policy 288.9
every rank ended with the parameters of the one rank, to the last bit: true (checksum 91f1b53333f47f40)
every generation each rank sends the returns of its own pairs, at most 16 numbers, 128 bytes,
and receives the others': 64 returns in all
a gradient of this network would be 452 bytes; of a network of a million weights, 4 MB:
the returns stay 128 bytes, however large the network
Four ranks train about 3 times faster than one, not 4 times: every generation waits for the slowest rank, and the ranks share the cores of this laptop with each other. The speed-up varies from run to run, between about 2.5 and 3.4. On separate machines, each rank has its cores to itself.
#include <chrono>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <thread>
#include <vector>
namespace
{
{
CartPoleSwingUp env;
env.make("v1", {{"max_steps", MAX_STEPS}}, {});
auto act = [&p](const std::vector<real_t> &o)
{ return NET.act_continuous(p, {o[0] / 2.4, o[1] / 3.0, o[2], o[3], o[4] / 10.0}, 10.0); };
}
std::vector<real_t> initial_parameters()
{
auto p = NET.initial_parameters(1);
for (std::size_t j = p.size() - 17; j < p.size(); ++j)
{
p[j] *= 0.1;
}
return p;
}
{
return c;
}
double seconds_since(std::chrono::steady_clock::time_point start)
{
return std::chrono::duration<double>(std::chrono::steady_clock::now() - start).count();
}
template <typename F> void on_one_thread(F &&f)
{
ThreadPool one(1);
one.submit([&] { f(one); }).
get();
}
struct Run
{
std::uint64_t checksum{0};
double seconds{0.0};
};
Run
train(Communicator *communicator,
uint_t generations)
{
on_one_thread(
[&](ThreadPool &pool)
{
const auto start = std::chrono::steady_clock::now();
for (
uint_t g = 0; g < generations; ++g)
{
if (communicator)
{
es.step(fitness, *communicator, pool);
}
else
{
}
}
run.seconds = seconds_since(start);
run.checksum =
es.checksum();
run.return_of_mean = fitness(
es.parameters(), 1000000, 0);
});
}
void report_traffic(
uint_t ranks)
{
const uint_t pairs = config().n_pairs;
const uint_t most = 2 * ((pairs + ranks - 1) / ranks);
std::printf(" every generation each rank sends the returns of its own pairs, at most %lu "
"numbers, %lu bytes,\n and receives the others': %lu returns in all\n",
most, most *
sizeof(
real_t), 2 * pairs);
std::printf(" a gradient of this network would be %lu bytes; of a network of a million "
"weights, 4 MB:\n the returns stay %lu bytes, however large the network\n",
NET.n_parameters() *
sizeof(
float), most *
sizeof(
real_t));
}
}
int main(
int argc,
char **argv)
{
uint_t generations = 300, ranks = 4;
long rank = -1;
std::string host = "127.0.0.1";
for (int i = 1; i + 1 < argc; i += 2)
{
const std::string a = argv[i];
const char *v = argv[i + 1];
if (a == "--generations")
generations = std::strtoul(v, nullptr, 10);
else if (a == "--ranks")
ranks = std::strtoul(v, nullptr, 10);
else if (a == "--rank")
rank = std::strtol(v, nullptr, 10);
else if (a == "--host")
host = v;
else if (a == "--port")
port = std::strtoul(v, nullptr, 10);
}
if (ranks == 0 || generations == 0)
{
std::fprintf(stderr, "need --ranks and --generations of at least 1\n");
return 1;
}
if (rank >= 0)
{
TCPStoreCommunicator communicator(host, port,
static_cast<uint_t>(rank), ranks);
const auto run =
train(&communicator, generations);
std::printf("rank %ld of %lu: %lu generations in %.2f s, return of the mean %.1f, "
"checksum %lx\n",
rank, ranks, generations,
run.seconds,
run.return_of_mean,
run.checksum);
return 0;
}
std::printf("Swinging the pole up: %lu generations of %lu episodes\n\n", generations,
2 * config().n_pairs);
std::printf("1. One rank, on one thread\n");
const auto alone =
train(
nullptr, generations);
std::printf(" %.2f s; the return of the mean policy %.1f\n\n", alone.seconds,
alone.return_of_mean);
std::printf("2. %lu ranks, each on one thread, every rank %lu of the %lu pairs\n", ranks,
(config().n_pairs + ranks - 1) / ranks, config().n_pairs);
std::vector<Run> runs(ranks);
std::vector<std::thread> threads;
for (
uint_t r = 0; r < ranks; ++r)
{
threads.emplace_back(
[&, r]
{
TCPStoreCommunicator communicator(host, port, r, ranks);
runs[r] =
train(&communicator, generations);
});
}
for (auto &t : threads)
{
t.join();
}
bool same = true;
for (const auto &run : runs)
{
same = same &&
run.checksum == alone.checksum;
}
std::printf(" %.2f s, a speed-up of %.2f; the return of the mean policy %.1f\n",
runs[0].seconds, alone.seconds / runs[0].seconds, runs[0].return_of_mean);
std::printf(" every rank ended with the parameters of the one rank, to the last bit: %s "
"(checksum %lx)\n",
same ? "true" : "false", alone.checksum);
report_traffic(ranks);
return same ? 0 : 1;
}
Swing the pole of a cart-pole up from hanging down, and keep it up, with a force that can take any va...
Definition cart_pole_swing_up.h:44
The evolution strategy of Salimans et al. (2017): a search for the parameters that maximise a fitness...
Definition evolution_strategy.h:100
A small neural network whose weights are a flat vector of parameters, for EvolutionStrategy.
Definition mlp_policy.h:30
How the ranks of a distributed training exchange numbers.
Definition communicator.h:32
A Communicator through c10d::TCPStore, the key-value store that comes with libtorch.
Definition communicator.h:79
A fixed number of threads that run the tasks given to them, in the order given.
Definition thread_pool.h:33
int main()
Definition discounted_returns.cpp:19
real_t episode_return(EnvType &env, Act &&act, uint_t seed, uint_t max_steps)
The undiscounted return of one episode of env, reset with seed, acting with act, at most max_steps st...
Definition mlp_policy.h:153
uint_t task_seed(uint_t base_seed, uint_t task) noexcept
A seed for task task, made from base_seed.
Definition parallel.h:148
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
CartPoleState start()
Definition cart_pole_lqr_mpc.cpp:73
json run(const Config &c, real_t learning_rate, const fs::path &dir, bitrl::network::TensorboardServer *tensorboard)
Definition log_experiment.cpp:106
Run train(GridObservation observation, uint_t seed, uint_t n_episodes)
Definition multi_agent_grid_world.cpp:57
std::vector< T > get(const std::vector< experience_tuple_type > &experience)
Definition dqn_experience_replay.cpp:132
The settings of EvolutionStrategy.
Definition es_config.h:15
real_t sigma
the standard deviation of the perturbations
Definition es_config.h:17
uint_t n_pairs
antithetic pairs per generation: 2 * n_pairs evaluations
Definition es_config.h:16
real_t learning_rate
Adam's step size.
Definition es_config.h:18