https://mooseframework.inl.gov
Loading...
Searching...
No Matches
LibtorchRLMiniBatchSampler.C
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://mooseframework.inl.gov
3//*
4//* All rights reserved, see COPYRIGHT for full restrictions
5//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
6//*
7//* Licensed under LGPL 2.1, please see LICENSE for details
8//* https://www.gnu.org/licenses/lgpl-2.1.html
9
10#ifdef MOOSE_LIBTORCH_ENABLED
11
13
14#include "MooseError.h"
15
16#include <algorithm>
17#include <string_view>
18
19std::vector<LibtorchRLMiniBatch>
21 const unsigned int batch_size,
22 const bool standardize_advantage,
23 const c10::optional<at::Generator> generator) const
24{
25 std::vector<LibtorchRLMiniBatch> mini_batches;
26
27 if (!batch.size())
28 return mini_batches;
29
30 validateBatch(batch);
31
32 const auto effective_batch_size = std::max<unsigned int>(1, batch_size);
33 auto permutation =
34 at::randperm(batch.size(), generator, torch::TensorOptions().dtype(torch::kLong));
35
36 for (std::int64_t batch_begin = 0; batch_begin < batch.size();
37 batch_begin += effective_batch_size)
38 {
39 const auto batch_end = std::min<std::int64_t>(
40 batch.size(), batch_begin + static_cast<std::int64_t>(effective_batch_size));
41 const auto indices = permutation.narrow(0, batch_begin, batch_end - batch_begin);
42 mini_batches.push_back(makeMiniBatch(batch, indices, standardize_advantage));
43 }
44
45 return mini_batches;
46}
47
48void
50{
51 if (!batch.actions.defined() || !batch.log_probabilities.defined() ||
52 !batch.value_targets.defined() || !batch.advantages.defined())
53 mooseError("RL tensor batches must define observations, actions, log probabilities, value "
54 "targets, and advantages before mini-batch sampling.");
55
56 const auto batch_size = batch.size();
57 const auto validate_rows = [batch_size](const torch::Tensor & tensor, std::string_view name)
58 {
59 if (!tensor.defined() || tensor.size(0) != batch_size)
61 "RL tensor batch field ", name, " must have the same number of rows as observations.");
62 };
63
64 validate_rows(batch.actions, "actions");
65 validate_rows(batch.log_probabilities, "log_probabilities");
66 validate_rows(batch.value_targets, "value_targets");
67 validate_rows(batch.advantages, "advantages");
68}
69
72 const torch::Tensor & indices,
73 const bool standardize_advantage)
74{
75 LibtorchRLMiniBatch mini_batch;
76
77 mini_batch.observations = batch.observations.index({indices});
78 mini_batch.actions = batch.actions.index({indices});
79 mini_batch.old_log_probabilities = batch.log_probabilities.index({indices});
80 mini_batch.value_targets = batch.value_targets.index({indices});
81 mini_batch.advantages = batch.advantages.index({indices});
82
83 if (standardize_advantage)
84 {
85 const auto std = mini_batch.advantages.std(false);
86 mini_batch.advantages = (mini_batch.advantages - mini_batch.advantages.mean()) / (std + 1e-10);
87 }
88
89 return mini_batch;
90}
91
92#endif
void mooseError(Args &&... args)
const std::string name
Definition Setup.h:21
static void validateBatch(const LibtorchRLTrajectoryBuffer::TensorBatch &batch)
Sanity-check that the flattened rollout tensors all line up.
std::vector< LibtorchRLMiniBatch > sample(const LibtorchRLTrajectoryBuffer::TensorBatch &batch, unsigned int batch_size, bool standardize_advantage, c10::optional< at::Generator > generator=c10::nullopt) const
Shuffle a flattened rollout batch into PPO-sized chunks.
static LibtorchRLMiniBatch makeMiniBatch(const LibtorchRLTrajectoryBuffer::TensorBatch &batch, const torch::Tensor &indices, bool standardize_advantage)
Slice one shuffled mini-batch out of the flattened rollout tensors.
Tensor data for one PPO mini-batch.
torch::Tensor actions
Action rows that match the sampled observations.
torch::Tensor value_targets
Critic targets aligned with the sampled observations.
torch::Tensor old_log_probabilities
Behavior-policy log probabilities for the sampled actions.
torch::Tensor advantages
Advantage estimates aligned with the sampled observations.
torch::Tensor observations
Flattened observation rows for the mini-batch.
Flattened tensor data assembled from the stored trajectories.
torch::Tensor actions
Flattened action matrix.
torch::Tensor value_targets
Flattened critic targets.
torch::Tensor observations
Flattened observation matrix.
torch::Tensor advantages
Flattened advantages.
torch::Tensor log_probabilities
Flattened action log-probabilities.
std::int64_t size() const
Return the number of transitions represented by the batch.