https://mooseframework.inl.gov
Loading...
Searching...
No Matches
LibtorchActionDistribution.h
Go to the documentation of this file.
1//* This file is part of the MOOSE framework
2//* https://www.mooseframework.org
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
12#pragma once
13
14#include <torch/torch.h>
15
16#include "MooseTypes.h"
17
18#include <string>
19#include <vector>
20
21namespace Moose
22{
23
27class LibtorchActionDistribution : public torch::nn::Module
28{
29public:
39 LibtorchActionDistribution(const std::string & name,
40 unsigned int num_inputs,
41 unsigned int num_outputs,
42 torch::DeviceType device_type = torch::kCPU,
43 torch::ScalarType scalar_type = torch::kDouble,
44 const std::vector<Real> & output_scaling_factors = {});
45
50 virtual void initialize(c10::optional<at::Generator> generator = c10::nullopt) = 0;
51
56 virtual void reset(const torch::Tensor & input) = 0;
57
62 virtual torch::Tensor sample(c10::optional<at::Generator> generator = c10::nullopt) const = 0;
63
67 virtual torch::Tensor deterministicAction() const = 0;
68
73 virtual torch::Tensor logProbability(const torch::Tensor & action) const = 0;
74
78 virtual torch::Tensor entropy() const = 0;
79
83 virtual bool isBounded() const = 0;
84
89
90protected:
95 torch::Tensor prepareFeatures(const torch::Tensor & input) const;
100 torch::Tensor prepareAction(const torch::Tensor & action) const;
104 const torch::Tensor & actionScaleTensor() const { return _action_scale_tensor; }
105
107 const std::string _name;
109 const unsigned int _num_inputs;
111 const unsigned int _num_outputs;
113 const torch::DeviceType _device_type;
115 const torch::ScalarType _data_type;
117 std::vector<Real> _output_scaling_factors;
118
120 torch::Tensor _action_scale_tensor;
121};
122
127{
128public:
140 LibtorchGaussianActionDistribution(const std::string & name,
141 unsigned int num_inputs,
142 unsigned int num_outputs,
143 torch::DeviceType device_type = torch::kCPU,
144 torch::ScalarType scalar_type = torch::kDouble,
145 bool build_on_construct = true,
146 const std::vector<Real> & output_scaling_factors = {},
147 bool state_independent_std = true);
148
153 virtual void initialize(c10::optional<at::Generator> generator = c10::nullopt) override;
154
159 virtual void reset(const torch::Tensor & input) override;
160
165 virtual torch::Tensor
166 sample(c10::optional<at::Generator> generator = c10::nullopt) const override;
167
171 virtual torch::Tensor deterministicAction() const override;
172
177 virtual torch::Tensor logProbability(const torch::Tensor & action) const override;
178
182 virtual torch::Tensor entropy() const override;
183
187 virtual bool isBounded() const override { return false; }
188
193
197 torch::nn::Linear & meanModule() { return _mean_module; }
198
202 const torch::nn::Linear & meanModule() const { return _mean_module; }
203
207 torch::nn::Linear & stdModule() { return _std_module; }
208
212 const torch::nn::Linear & stdModule() const { return _std_module; }
213
217 torch::Tensor & logStd() { return _log_std_parameter; }
218
222 const torch::Tensor & logStd() const { return _log_std_parameter; }
223
227 const torch::Tensor & stdTensor() const { return _std_tensor; }
228
229private:
234
238 torch::nn::Linear _mean_module{nullptr};
240 torch::nn::Linear _std_module{nullptr};
242 torch::Tensor _log_std_parameter;
244 torch::Tensor _mean;
246 torch::Tensor _std_tensor;
248 torch::Tensor _log_std_tensor;
249};
250
255{
256public:
269 LibtorchBetaActionDistribution(const std::string & name,
270 unsigned int num_inputs,
271 unsigned int num_outputs,
272 const std::vector<Real> & minimum_values,
273 const std::vector<Real> & maximum_values,
274 torch::DeviceType device_type = torch::kCPU,
275 torch::ScalarType scalar_type = torch::kDouble,
276 bool build_on_construct = true,
277 const std::vector<Real> & output_scaling_factors = {});
278
283 virtual void initialize(c10::optional<at::Generator> generator = c10::nullopt) override;
284
289 virtual void reset(const torch::Tensor & input) override;
290
295 virtual torch::Tensor
296 sample(c10::optional<at::Generator> generator = c10::nullopt) const override;
297
301 virtual torch::Tensor deterministicAction() const override;
302
307 virtual torch::Tensor logProbability(const torch::Tensor & action) const override;
308
312 virtual torch::Tensor entropy() const override;
313
317 virtual bool isBounded() const override { return true; }
318
322 torch::nn::Linear & alphaModule() { return _alpha_module; }
323
327 const torch::nn::Linear & alphaModule() const { return _alpha_module; }
328
332 torch::nn::Linear & betaModule() { return _beta_module; }
333
337 const torch::nn::Linear & betaModule() const { return _beta_module; }
338
342 const torch::Tensor & alphaTensor() const { return _alpha_tensor; }
343
347 const torch::Tensor & betaTensor() const { return _beta_tensor; }
348
349private:
354
356 const std::vector<Real> _minimum_values;
358 const std::vector<Real> _maximum_values;
359
361 torch::nn::Linear _alpha_module{nullptr};
363 torch::nn::Linear _beta_module{nullptr};
365 torch::Tensor _min_tensor;
367 torch::Tensor _max_tensor;
369 torch::Tensor _alpha_tensor;
371 torch::Tensor _beta_tensor;
373 torch::Tensor _alpha_beta_tensor;
375 torch::Tensor _log_norm;
377 torch::Tensor _mean;
378};
379
380} // namespace Moose
381
382#endif
const std::string name
Definition Setup.h:21
Reusable continuous-action distribution interface for actor policies.
const torch::DeviceType _device_type
Torch device used by this distribution.
const std::string _name
Module name used for registration and serialization.
torch::Tensor _action_scale_tensor
Registered libtorch buffer holding the per-action scaling factors.
torch::Tensor prepareAction(const torch::Tensor &action) const
Convert actions to the configured device and scalar type.
virtual torch::Tensor sample(c10::optional< at::Generator > generator=c10::nullopt) const =0
Draw a stochastic action sample in physical units.
void synchronizeScalingFactorsFromBuffer()
Sync cached scaling metadata from the registered buffers after loading state.
virtual torch::Tensor logProbability(const torch::Tensor &action) const =0
Evaluate the log-probability of an action under the current distribution.
std::vector< Real > _output_scaling_factors
Cached per-action scaling factors applied in physical units.
torch::Tensor prepareFeatures(const torch::Tensor &input) const
Convert actor features to the configured device and scalar type.
const torch::Tensor & actionScaleTensor() const
Return the registered tensor that stores per-action scaling factors.
virtual torch::Tensor deterministicAction() const =0
Return the deterministic action used for evaluation.
virtual void initialize(c10::optional< at::Generator > generator=c10::nullopt)=0
Initialize the trainable distribution parameters.
const unsigned int _num_outputs
Number of action dimensions produced by this distribution.
virtual void reset(const torch::Tensor &input)=0
Refresh cached distribution parameters from the latest actor features.
virtual torch::Tensor entropy() const =0
Compute the entropy of the current distribution.
const unsigned int _num_inputs
Number of actor features feeding this distribution.
const torch::ScalarType _data_type
Torch scalar type used by this distribution.
virtual bool isBounded() const =0
Tell callers whether the distribution enforces explicit action bounds.
Beta action distribution for bounded action spaces.
virtual void reset(const torch::Tensor &input) override
Refresh the Beta distribution parameters from the latest actor features.
torch::Tensor _min_tensor
Tensor form of the lower action bounds.
torch::nn::Linear _beta_module
Linear head that produces the Beta beta parameters.
torch::nn::Linear _alpha_module
Linear head that produces the Beta alpha parameters.
const torch::nn::Linear & alphaModule() const
Return the Beta alpha head.
const std::vector< Real > _maximum_values
Upper action bounds in physical units.
torch::Tensor _max_tensor
Tensor form of the upper action bounds.
virtual void initialize(c10::optional< at::Generator > generator=c10::nullopt) override
Initialize the trainable Beta distribution parameters.
const std::vector< Real > _minimum_values
Lower action bounds in physical units.
torch::Tensor _alpha_tensor
Cached Beta alpha parameters from the latest reset.
torch::nn::Linear & alphaModule()
Return the Beta alpha head.
const torch::nn::Linear & betaModule() const
Return the Beta beta head.
torch::Tensor _mean
Cached normalized Beta mean from the latest reset.
torch::Tensor _log_norm
Cached log normalization factor for the latest Beta distribution state.
torch::Tensor _beta_tensor
Cached Beta beta parameters from the latest reset.
virtual bool isBounded() const override
Return true because the Beta distribution enforces action bounds.
const torch::Tensor & alphaTensor() const
Return the cached Beta alpha tensor.
torch::Tensor _alpha_beta_tensor
Cached sum of the alpha and beta parameters.
torch::nn::Linear & betaModule()
Return the Beta beta head.
void constructDistribution()
Build and register the Beta distribution heads.
const torch::Tensor & betaTensor() const
Return the cached Beta beta tensor.
virtual torch::Tensor deterministicAction() const override
Return the Beta mean action used for deterministic evaluation.
virtual torch::Tensor entropy() const override
Compute the entropy of the current Beta distribution.
virtual torch::Tensor sample(c10::optional< at::Generator > generator=c10::nullopt) const override
Draw a stochastic Beta action sample in physical units.
virtual torch::Tensor logProbability(const torch::Tensor &action) const override
Evaluate the log-probability of an action under the current Beta distribution.
Gaussian action distribution for unbounded action spaces.
void constructDistribution()
Build and register the Gaussian distribution heads.
torch::Tensor _std_tensor
Cached Gaussian action standard deviation from the latest reset.
torch::nn::Linear _std_module
Linear head that produces state-dependent Gaussian log-std inputs.
torch::nn::Linear & meanModule()
Return the Gaussian mean head.
const torch::nn::Linear & stdModule() const
Return the state-dependent Gaussian standard-deviation head.
torch::nn::Linear & stdModule()
Return the state-dependent Gaussian standard-deviation head.
torch::Tensor _mean
Cached Gaussian action mean from the latest reset.
bool stateIndependentStd() const
Return whether the Gaussian standard deviation ignores the current actor features.
virtual torch::Tensor entropy() const override
Compute the entropy of the current Gaussian distribution.
torch::nn::Linear _mean_module
Linear head that produces the Gaussian action mean.
virtual torch::Tensor deterministicAction() const override
Return the Gaussian mean action used for deterministic evaluation.
virtual torch::Tensor sample(c10::optional< at::Generator > generator=c10::nullopt) const override
Draw a stochastic Gaussian action sample in physical units.
const torch::Tensor & logStd() const
Return the state-independent Gaussian log-standard-deviation parameter.
torch::Tensor _log_std_tensor
Cached Gaussian action log standard deviation from the latest reset.
torch::Tensor _log_std_parameter
Trainable state-independent Gaussian log-std.
virtual void initialize(c10::optional< at::Generator > generator=c10::nullopt) override
Initialize the trainable Gaussian distribution parameters.
const torch::Tensor & stdTensor() const
Return the cached Gaussian standard-deviation tensor.
virtual torch::Tensor logProbability(const torch::Tensor &action) const override
Evaluate the log-probability of an action under the current Gaussian distribution.
virtual void reset(const torch::Tensor &input) override
Refresh the Gaussian distribution parameters from the latest actor features.
const torch::nn::Linear & meanModule() const
Return the Gaussian mean head.
virtual bool isBounded() const override
Return false because the Gaussian distribution is unbounded.
const bool _state_independent_std
Whether the Gaussian std ignores the current actor features.
torch::Tensor & logStd()
Return the state-independent Gaussian log-standard-deviation parameter.