https://mooseframework.inl.gov
Loading...
Searching...
No Matches
LibtorchModel.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#if 0 // NEML2 v2->v3 migration: DEFERRED (FEM/discretization/typed-tensor path has no v3 C++
10 // equivalent yet)
11
12#ifdef NEML2_ENABLED
13
14// libtorch headers
15#include <ATen/ops/ones_like.h>
16
17// neml2 headers
18#include "neml2/misc/assertions.h"
19
20// moose headers
21#include "LibtorchModel.h"
22
23namespace neml2
24{
26
27OptionSet
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}
40
41LibtorchModel::LibtorchModel(const OptionSet & options)
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}
52
53void
54LibtorchModel::to(const torch::TensorOptions & options)
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}
64
65void
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}
75
76void
77LibtorchModel::set_value(bool out, bool /*dout_din*/, bool /*d2out_din2*/)
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}
105
106}
107
108#endif
109
110#endif // NEML2 v2->v3 migration: DEFERRED
std::array< Real, 2 > values
Definition MortarUtils.C:52
Evaluate a pretrained libtorch model in .pt format, such as a neural network.
LibtorchModel(const OptionSet &options)
virtual void request_AD() override
virtual void to(const torch::TensorOptions &options) override
Override the base implementation to additionally send the model loaded from torch script to different...
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.
virtual void set_value(bool out, bool dout_din, bool d2out_din2) override
static OptionSet expected_options()
std::vector< Variable< Scalar > * > _outputs
MOOSE now contains C++17 code, so give a reasonable error message stating what the user can do to add...
register_NEML2_object(LibtorchModel)