https://mooseframework.inl.gov
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
neml2::LibtorchModel Class Reference

Evaluate a pretrained libtorch model in .pt format, such as a neural network. More...

#include <LibtorchModel.h>

Inheritance diagram for neml2::LibtorchModel:
[legend]

Public Member Functions

 LibtorchModel (const OptionSet &options)
 
virtual void to (const torch::TensorOptions &options) override
 Override the base implementation to additionally send the model loaded from torch script to different device and dtype.
 
virtual void request_AD () override
 

Static Public Member Functions

static OptionSet expected_options ()
 

Protected Member Functions

virtual void set_value (bool out, bool dout_din, bool d2out_din2) override
 

Protected Attributes

std::vector< const Variable< Scalar > * > _inputs
 
std::vector< Variable< Scalar > * > _outputs
 
Moose::DataFileUtils::Path _file_path
 
std::unique_ptr< torch::jit::script::Module > _surrogate
 We need to use a pointer here because forward is not const qualified.
 

Detailed Description

Evaluate a pretrained libtorch model in .pt format, such as a neural network.

Evaluates models with an arbitrary number of inputs and maps them to an arbitrary number of outputs.

Definition at line 33 of file LibtorchModel.h.

Constructor & Destructor Documentation

◆ LibtorchModel()

neml2::LibtorchModel::LibtorchModel ( const OptionSet &  options)

Definition at line 41 of file LibtorchModel.C.

42 : Model(options),
43 _file_path(Moose::DataFileUtils::getPath(options.get<std::string>("file_path"))),
44 _surrogate(std::make_unique<torch::jit::script::Module>(torch::jit::load(_file_path.path)))
45{
46 // inputs
47 for (const auto & fv : options.get<std::vector<VariableName>>("inputs"))
48 _inputs.push_back(&declare_input_variable<Scalar>(fv));
49 for (const auto & fv : options.get<std::vector<VariableName>>("outputs"))
50 _outputs.push_back(&declare_output_variable<Scalar>(fv));
51}
std::vector< const Variable< Scalar > * > _inputs
std::unique_ptr< torch::jit::script::Module > _surrogate
We need to use a pointer here because forward is not const qualified.
Moose::DataFileUtils::Path _file_path
std::vector< Variable< Scalar > * > _outputs
Path getPath(std::string path, const GetPathOptions &options={})
Get the data path for a given path, searching the registered data.
const Elem & get(const ElemType type_in)

Member Function Documentation

◆ expected_options()

OptionSet neml2::LibtorchModel::expected_options ( )
static

Definition at line 28 of file LibtorchModel.C.

29{
30 auto options = Model::expected_options();
31 options.add<std::vector<VariableName>>("inputs", "The input variables for the neural network");
32 options.add<std::vector<VariableName>>("outputs",
33 "The (scaled) output variables for the neural network");
34 options.add<std::string>("file_path", "The path to the neural network file");
35 // No jitting :/
36 options.set<bool>("jit", false);
37 options.suppress("jit");
38 return options;
39}

◆ request_AD()

void neml2::LibtorchModel::request_AD ( )
overridevirtual

Definition at line 66 of file LibtorchModel.C.

67{
68 std::vector<const VariableBase *> inputs;
69 for (size_t i = 0; i < _inputs.size(); ++i)
70 inputs.push_back(_inputs[i]);
71
72 for (size_t i = 0; i < _outputs.size(); ++i)
73 _outputs[i]->request_AD(inputs);
74}
virtual void request_AD() override

Referenced by request_AD().

◆ set_value()

void neml2::LibtorchModel::set_value ( bool  out,
bool  dout_din,
bool  d2out_din2 
)
overrideprotectedvirtual

Definition at line 77 of file LibtorchModel.C.

78{
79 if (out)
80 {
81 std::vector<at::Tensor> values;
82 auto first_batch_dim = _inputs[0]->batch_dim();
83 for (size_t i = 0; i < _inputs.size(); ++i)
84 {
85 // assert that all inputs have the same batch dimension
86 neml_assert(_inputs[i]->batch_dim() == first_batch_dim);
87 values.push_back(_inputs[i]->tensor());
88 }
89
90 auto x = Tensor(torch::transpose(torch::vstack(at::ArrayRef<at::Tensor>(
91 values.data(), static_cast<int64_t>(values.size()))),
92 0,
93 1),
94 _inputs[0]->batch_dim());
95
96 // Feed forward the neural network and process the output
97 auto temp = _surrogate->forward({x}).toTensor().squeeze();
98 auto y0 =
99 (temp.dim() == 1) ? temp.view({temp.size(0), 1}).transpose(0, 1) : temp.transpose(0, 1);
100
101 for (size_t i = 0; i < _outputs.size(); ++i)
102 *_outputs[i] = Scalar(y0[i], 0);
103 }
104}
std::array< Real, 2 > values
Definition MortarUtils.C:52
LIBMESH_DEVICE_INLINE auto transpose(const TensorLike &T_in) -> std::enable_if_t< is_tensor_like_v< TensorLike >, std::conditional_t< std::is_void< ResultTensor >::value, tensor_semantic_type_t< TensorLike >, ResultTensor > >
NumberTensorValue Tensor

◆ to()

void neml2::LibtorchModel::to ( const torch::TensorOptions &  options)
overridevirtual

Override the base implementation to additionally send the model loaded from torch script to different device and dtype.

Definition at line 54 of file LibtorchModel.C.

55{
56 Model::to(options);
57
58 if (options.has_device())
59 _surrogate->to(options.device());
60
61 if (options.has_dtype())
62 _surrogate->to(torch::Dtype(caffe2::typeMetaToScalarType(options.dtype())));
63}

Member Data Documentation

◆ _file_path

Moose::DataFileUtils::Path neml2::LibtorchModel::_file_path
protected

Definition at line 55 of file LibtorchModel.h.

◆ _inputs

std::vector<const Variable<Scalar> *> neml2::LibtorchModel::_inputs
protected

Definition at line 52 of file LibtorchModel.h.

Referenced by LibtorchModel(), request_AD(), and set_value().

◆ _outputs

std::vector<Variable<Scalar> *> neml2::LibtorchModel::_outputs
protected

Definition at line 54 of file LibtorchModel.h.

Referenced by LibtorchModel(), request_AD(), and set_value().

◆ _surrogate

std::unique_ptr<torch::jit::script::Module> neml2::LibtorchModel::_surrogate
protected

We need to use a pointer here because forward is not const qualified.

Definition at line 57 of file LibtorchModel.h.

Referenced by set_value(), and to().


The documentation for this class was generated from the following files: