NEML2 1.4.0
Loading...
Searching...
No Matches
Model.cxx
1// Copyright 2023, UChicago Argonne, LLC
2// All Rights Reserved
3// Software Name: NEML2 -- the New Engineering material Model Library, version 2
4// By: Argonne National Laboratory
5// OPEN SOURCE LICENSE (MIT)
6//
7// Permission is hereby granted, free of charge, to any person obtaining a copy
8// of this software and associated documentation files (the "Software"), to deal
9// in the Software without restriction, including without limitation the rights
10// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11// copies of the Software, and to permit persons to whom the Software is
12// furnished to do so, subject to the following conditions:
13//
14// The above copyright notice and this permission notice shall be included in
15// all copies or substantial portions of the Software.
16//
17// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23// THE SOFTWARE.
24
25#include "neml2/models/Model.h"
26#include <torch/autograd.h>
27
28namespace neml2
29{
31
34{
38
39 options.section() = "Models";
40
41 options.set<bool>("_use_AD_first_derivative") = false;
42 options.set<bool>("_use_AD_second_derivative") = false;
43 options.set<int>("_extra_derivative_order") = 0;
44 options.set<bool>("_nonlinear_system") = false;
45
46 options.set("_extra_derivative_order").suppressed() = true;
47 options.set("_nonlinear_system").suppressed() = true;
48 options.set("_use_AD_first_derivative").suppressed() = true;
49 options.set("_use_AD_second_derivative").suppressed() = true;
50
51 return options;
52}
53
54Model::Model(const OptionSet & options)
55 : Data(options),
56 ParameterStore(options, this),
57 VariableStore(options, this),
58 NonlinearSystem(options),
59 _AD_1st_deriv(options.get<bool>("_use_AD_first_derivative")),
60 _AD_2nd_deriv(options.get<bool>("_use_AD_second_derivative")),
61 _options(default_tensor_options()),
62 _deriv_order(-1),
63 _extra_deriv_order(options.get<int>("_extra_derivative_order")),
64 _nonlinear_system(options.get<bool>("_nonlinear_system"))
65{
67}
68
69std::vector<Diagnosis>
71{
72 neml_assert(host() == this, "This method should only be called on the host model.");
73
74 std::vector<Diagnosis> errors;
75
76 // Check for statefulness
77 if (input_axis().has_subaxis("old_state"))
78 {
79 if (!output_axis().has_subaxis("state"))
80 errors.push_back(
82 ": input axis has sub-axis 'old_state', but output axis does not "
83 "have sub-axis 'state'."));
84 else
85 {
86 auto s_vars = output_axis().subaxis("state").variable_accessors(/*recursive=*/true);
87 for (auto var : input_axis().subaxis("old_state").variable_accessors(/*recursive=*/true))
88 if (!s_vars.count(var))
89 errors.push_back(make_diagnosis(name(),
90 ": input axis has old state named ",
91 var,
92 ", but it doesn't exist on the output axis."));
93 }
94 }
95
96 return errors;
97}
98
99void
101{
102 // Declare nonlinear parameters as additional input variables
103 for (const auto & [name, param] : nl_params())
104 declare_input_variable(param->base_storage(), VariableName(param->name()));
105
106 // Setup input and output axes
107 setup_layout();
108
109 // Setup functional dependence for each output variable
110 for (auto && [y_name, y_var] : output_views())
111 {
112 y_var.clear_args();
113 for (auto && [x_name, x_var] : input_views())
114 y_var.add_arg(x_var);
115 }
116
117 // Setup variable views
120}
121
122void
124 int deriv_order,
125 const torch::Device & device,
126 const torch::Dtype & dtype)
127{
128 neml_assert(host() == this, "This method should only be called on the host model.");
129
130 // Re-cache batch shape if it has changed
131 const auto batch_shape_promoted = batch_shape.empty() ? TorchShape{1} : batch_shape.vec();
135
136 // Re-cache tensor options if they have changed
137 const bool device_changed = device != options().device();
138 const bool dtype_changed = dtype != options().dtype();
140 if (options_changed)
141 {
142 const auto options = default_tensor_options().device(device).dtype(dtype);
143 cache(options);
146 }
147
148 // Reallocate variable storage when necessary
150}
151
152void
154{
155 reinit(tensor.batch_sizes(), deriv_order, tensor.device(), tensor.scalar_type());
156}
157
158void
160{
161 const auto deriv_order_old = _deriv_order;
162 const auto deriv_order_new = deriv_order + _extra_deriv_order;
163
164 bool in = false;
165 bool out = false;
166 bool dout_din = false;
167 bool d2out_din2 = false;
168
169 if (options_changed)
170 {
171 in = true;
172 out = deriv_order_new >= 0;
175 }
176 else
177 {
178 out = deriv_order_new >= 0 && deriv_order_old < 0;
181 }
182
183 _deriv_order = std::max(deriv_order_new, deriv_order_old);
185
186 for (auto submodel : registered_models())
187 submodel->allocate_variables(_deriv_order, options_changed);
188}
189
190void
196
197void
199{
200 for (auto submodel : registered_models())
201 {
202 for (auto && [name, var] : submodel->input_views())
203 var.setup_views(input_view(var.name()));
204
205 submodel->setup_submodel_input_views();
206 }
207}
208
209void
215
216void
218{
219 for (auto submodel : registered_models())
220 {
221 for (auto && [name, var] : submodel->output_views())
222 var.setup_views(&submodel->output_storage(),
223 &submodel->derivative_storage(),
224 &submodel->second_derivative_storage());
225
226 submodel->setup_submodel_output_views();
227 }
228}
229
230void
241
242void
244{
247
249 {
250 if (out)
251 _residual = output_storage()("residual");
252 if (dout_din && requires_grad())
253 _Jacobian = derivative_storage()("residual", "state");
254 }
255}
256
257void
266
267void
269{
271
272 // Also update the model input variables
273 LabeledVector sol(x, {&output_axis().subaxis("residual")});
274 host<VariableStore>()->input_storage().slice("state").fill(sol);
275}
276
277void
279{
280 _batch_sizes = batch_shape.vec();
282 for (auto submodel : registered_models())
283 submodel->cache(batch_shape);
284}
285
286void
287Model::cache(const torch::TensorOptions & options)
288{
289 _options = options;
290 for (auto submodel : registered_models())
291 submodel->cache(options);
292}
293
294void
296{
298 throw NEMLException("AD derivative is requested, but AD second derivative is not requested.");
299}
300
301void
303{
304 for (auto && [name, var] : input_views())
305 var.requires_grad_(req);
306}
307
308void
315
316void
318{
320 neml_assert_dbg(in.axis(0) == input_axis(),
321 "Incompatible input axis. The model has input axis: \n",
322 input_axis(),
323 "The input vector has axis: \n",
324 in.axis(0));
325
326 input_storage().copy_(in.tensor().batch_expand(batch_sizes()));
327}
328
331{
332 return output_storage().clone();
333}
334
340
346
349{
350 set_input(in);
351 detach_and_zero(true, false, false);
352 value();
353 return get_output();
354}
355
356std::tuple<LabeledVector, LabeledMatrix>
358{
359 set_input(in);
360 detach_and_zero(true, true, false);
362 return {get_output(), get_doutput_dinput()};
363}
364
365std::tuple<LabeledVector, LabeledMatrix, LabeledTensor3D>
373
374void
376{
377 set_value(true, false, false);
378}
379
380void
382{
384 "value_and_dvalue() is called but derivative storage hasn't been allocated.");
385
386 if (!_AD_1st_deriv)
387 set_value(true, true, false);
388 else
389 {
391 set_value(true, false, false);
392 extract_derivatives(/*retain_graph=*/true, /*create_graph=*/false, /*allow_unused=*/true);
393 }
394}
395
396void
398{
400 "value_and_dvalue_and_d2value() is called but second derivative storage hasn't "
401 "been allocated.");
402
403 if (!_AD_2nd_deriv)
404 set_value(true, true, true);
405 else
406 {
408
409 if (!_AD_1st_deriv)
410 set_value(true, true, false);
411 else
412 {
413 set_value(true, false, false);
414 extract_derivatives(/*retain_graph=*/true, /*create_graph=*/true, /*allow_unused=*/true);
415 }
416
417 extract_second_derivatives(
418 /*retain_graph=*/true, /*create_graph=*/false, /*allow_unused=*/true);
419 }
420}
421
422Model *
423Model::registered_model(const std::string & name) const
424{
425 for (auto submodel : _registered_models)
426 if (submodel->name() == name)
427 return submodel;
428
429 throw NEMLException("There is no registered model named '" + name + "' in '" + this->name() +
430 "'");
431}
432
433const std::set<VariableName>
435{
436 return input_axis().variable_accessors(true);
437}
438
439const std::set<VariableName>
441{
442 return output_axis().variable_accessors(true);
443}
444
445void
446Model::assemble(bool residual, bool Jacobian)
447{
448 if (residual && !Jacobian)
449 {
450 detach_and_zero(true, false, false);
451 value();
452 }
453 else if (Jacobian)
454 {
455 detach_and_zero(true, true, false);
457 }
458}
459
460void
461Model::extract_derivatives(bool retain_graph, bool create_graph, bool allow_unused)
462{
463 // Loop over rows to retrieve the derivatives
464 if (output_storage().tensor().requires_grad())
465 for (TorchSize i = 0; i < output_storage().base_sizes()[0]; i++)
466 {
468 grad_outputs.index_put_({torch::indexing::Ellipsis, i}, 1.0);
469 for (auto && [name, var] : input_views())
470 {
471 auto dyi_dvar = torch::autograd::grad({output_storage()},
472 {var.tensor()},
473 {grad_outputs},
474 retain_graph,
475 create_graph,
476 allow_unused)[0];
477
478 if (dyi_dvar.defined())
479 {
481 {i, input_axis().indices(name)},
482 dyi_dvar.reshape(utils::add_shapes(batch_sizes(), var.base_storage())));
483 }
484 }
485 }
486}
487
488void
489Model::extract_second_derivatives(bool retain_graph, bool create_graph, bool allow_unused)
490{
491 // Loop over rows to retrieve the second derivatives
492 if (derivative_storage().tensor().requires_grad())
493 for (TorchSize i = 0; i < derivative_storage().base_sizes()[0]; i++)
494 for (TorchSize j = 0; j < derivative_storage().base_sizes()[1]; j++)
495 {
496 auto grad_outputs = torch::zeros_like(derivative_storage());
497 grad_outputs.index_put_({torch::indexing::Ellipsis, i, j}, 1.0);
498 for (auto && [name, var] : input_views())
499 {
500 auto dydxij_dvar = torch::autograd::grad({derivative_storage()},
501 {var.tensor()},
502 {grad_outputs},
503 retain_graph,
504 create_graph,
505 allow_unused)[0];
506 if (dydxij_dvar.defined())
508 {i, j, input_axis().indices(name)},
509 dydxij_dvar.reshape(utils::add_shapes(batch_sizes(), var.base_storage())));
510 }
511 }
512}
513} // namespace neml2
TorchShapeRef batch_sizes() const
Return the batch size.
Definition BatchTensorBase.cxx:149
static BatchTensor zeros_like(const BatchTensor &other)
Zero tensor like another, i.e. same batch and base shapes, same tensor options, etc.
Definition BatchTensorBase.cxx:59
Definition BatchTensor.h:32
static BatchTensor empty(const TorchShapeRef &base_shape, const torch::TensorOptions &options=default_tensor_options())
Unbatched empty tensor given base shape.
Definition BatchTensor.cxx:30
virtual void send_buffers_to(const torch::TensorOptions &options)
Send all buffers to options.
Definition BufferStore.cxx:44
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
Definition Data.h:36
static OptionSet expected_options()
Definition Data.cxx:30
std::set< LabeledAxisAccessor > variable_accessors(bool recursive=false, const LabeledAxisAccessor &subaxis={}) const
Get the variable accessors.
Definition LabeledAxis.cxx:333
const LabeledAxis & subaxis(const std::string &name) const
Get a sub-axis.
Definition LabeledAxis.cxx:365
TorchIndex indices(const LabeledAxisAccessor &accessor) const
Get the indices of a specific item by a LabeledAxisAccessor
Definition LabeledAxis.cxx:240
TorchSize storage_size() const
Get the (total) storage size of this axis.
Definition LabeledAxis.h:142
A single-batched, logically 2D LabeledTensor.
Definition LabeledMatrix.h:38
A single-batched, logically 3D LabeledTensor.
Definition LabeledTensor3D.h:38
Derived clone(torch::MemoryFormat memory_format=torch::MemoryFormat::Contiguous) const
Clone this LabeledTensor.
Definition LabeledTensor.cxx:131
TorchShapeRef base_sizes() const
Return the base size.
Definition LabeledTensor.cxx:180
void base_index_put(TorchSlice indices, const torch::Tensor &other)
Set a index sliced on the batch dimensions to a value.
Definition LabeledTensor.cxx:228
const BatchTensor & tensor() const
Definition LabeledTensor.h:107
void copy_(const T &other)
Copy the value from another tensor.
Definition LabeledTensor.h:235
A single-batched, logically 1D LabeledTensor.
Definition LabeledVector.h:38
The base class for all constitutive models.
Definition Model.h:53
virtual void detach_and_zero(bool out, bool dout_din=true, bool d2out_din2=true) override
Call VariableStore::detach_and_zero recursively on all submodels.
Definition Model.cxx:258
virtual LabeledMatrix get_doutput_dinput()
Definition Model.cxx:336
virtual void setup_submodel_output_views()
Definition Model.cxx:217
virtual void assemble(bool residual, bool Jacobian) override
Compute the residual and Jacobian.
Definition Model.cxx:446
void input_requires_grad_(bool req=true)
Set requires_grad for the input variables.
Definition Model.cxx:302
void use_AD_derivatives(bool first=true, bool second=true)
Tell this model to use AD to get derivatives.
Definition Model.cxx:309
virtual LabeledVector get_output()
Definition Model.cxx:330
bool requires_grad() const
Whether derivative has been requested for this model.
Definition Model.h:104
virtual LabeledTensor3D get_d2output_dinput2()
Definition Model.cxx:342
TorchShapeRef batch_sizes() const
This model's batch shape.
Definition Model.h:113
virtual void value_and_dvalue_and_d2value()
Definition Model.cxx:397
const std::vector< Model * > & registered_models() const
The models that may be used during the evaluation of this model.
Definition Model.h:119
std::vector< Model * > _registered_models
Models this model may use during its evaluation.
Definition Model.h:275
bool _AD_2nd_deriv
Whether to use AD to compute 2nd derivatives.
Definition Model.h:281
virtual void cache(TorchShapeRef batch_shape) override
Cache the variable's batch shape.
Definition Model.cxx:278
virtual void setup_submodel_input_views()
Definition Model.cxx:198
virtual void allocate_variables(int deriv_order, bool options_changed)
Call VariableStore::allocate_variables recursively on all submodels.
Definition Model.cxx:159
bool requires_2nd_grad() const
Whether 2nd derivative has been requested for this model.
Definition Model.h:107
virtual void set_value(bool out, bool dout_din, bool d2out_din2)=0
The map between input -> output, and optionally its derivatives.
virtual void set_solution(const BatchTensor &x) override
Set x as the current solution of the nonlinear system.
Definition Model.cxx:268
virtual void reinit_output_views(bool out, bool dout_din=true, bool d2out_din2=true) override
Call VariableStore::reinit_output_views recursively on all submodels.
Definition Model.cxx:243
const torch::TensorOptions & options() const
This model's tensor options.
Definition Model.h:116
virtual void setup_input_views() override
Call VariableStore::setup_input_views recursively on all submodels.
Definition Model.cxx:191
virtual void reinit(TorchShapeRef batch_shape, int deriv_order=0, const torch::Device &device=torch::kCPU, const torch::Dtype &dtype=NEML2_DTYPE)
Allocate storage and setup views for all the variables of this model and recursively all of the sub-m...
Definition Model.cxx:123
bool _AD_1st_deriv
Whether to use AD to compute 1st derivatives.
Definition Model.h:278
virtual bool is_nonlinear_system() const
Whether this model defines one or more nonlinear equations to be solved.
Definition Model.h:75
virtual void check_AD_limitation() const
Definition Model.cxx:295
virtual void set_input(const LabeledVector &in)
Set in to be the input of this model.
Definition Model.cxx:317
virtual void setup() override
Setup this model.
Definition Model.cxx:100
virtual void value()
Definition Model.cxx:375
virtual void reinit_input_views() override
Call VariableStore::reinit_input_views recursively on all submodels.
Definition Model.cxx:231
Stage
Definition Model.h:188
static OptionSet expected_options()
Definition Model.cxx:33
virtual void value_and_dvalue()
Definition Model.cxx:381
virtual void setup_output_views() override
Call VariableStore::setup_output_views recursively on all submodels.
Definition Model.cxx:210
virtual std::vector< Diagnosis > preflight() const
Check for common problems.
Definition Model.cxx:70
virtual const std::set< VariableName > consumed_items() const override
The variables that this model depends on.
Definition Model.cxx:434
virtual const std::set< VariableName > provided_items() const override
The variables that this model defines as part of its output.
Definition Model.cxx:440
static enum neml2::Model::Stage stage
Definition Model.cxx:30
Model(const OptionSet &options)
Construct a new Model object.
Definition Model.cxx:54
Model * registered_model(const std::string &name) const
Get a registered model by its name.
Definition Model.cxx:423
const std::string & name() const
A readonly reference to the object's name.
Definition NEML2Object.h:65
const T * host() const
Get a readonly pointer to the host.
Definition NEML2Object.h:90
Definition error.h:33
Definition of a nonlinear system of equations.
Definition NonlinearSystem.h:37
static void disable_automatic_scaling(OptionSet &options)
Definition NonlinearSystem.cxx:54
BatchTensor _Jacobian
View for the Jacobian of this nonlinear system.
Definition NonlinearSystem.h:108
virtual void set_solution(const BatchTensor &x)
Set the solution vector.
Definition NonlinearSystem.cxx:161
void residual()
Convenient shortcut to assemble and return the system residual.
Definition NonlinearSystem.cxx:175
BatchTensor _solution
View for the solution of this nonlinear system.
Definition NonlinearSystem.h:102
TorchSize _ndof
Number of degrees of freedom.
Definition NonlinearSystem.h:99
BatchTensor _residual
View for the residual of this nonlinear system.
Definition NonlinearSystem.h:105
void Jacobian()
Convenient shortcut to assemble and return the system Jacobian.
Definition NonlinearSystem.cxx:192
static OptionSet expected_options()
Definition NonlinearSystem.cxx:31
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:59
Interface for object which can store parameters.
Definition ParameterStore.h:38
virtual void send_parameters_to(const torch::TensorOptions &options)
Send parameters to options.
Definition ParameterStore.cxx:47
const std::map< std::string, const VariableBase * > & nl_params() const
Get all nonlinear parameters.
Definition ParameterStore.h:60
Definition VariableStore.h:37
virtual void setup_input_views()
Tell each input variable view which tensor storage(s) to view into.
Definition VariableStore.cxx:107
LabeledMatrix & derivative_storage()
Definition VariableStore.h:127
Storage< VariableName, VariableBase > & output_views()
Definition VariableStore.h:109
LabeledVector & output_storage()
Definition VariableStore.h:121
const Variable< T > & declare_input_variable(S &&... name)
Declare an input variable.
Definition VariableStore.h:180
virtual void reinit_output_views(bool out, bool dout_din=true, bool d2out_din2=true)
Create the views for output variables, and optionally for the derivative and second derivatives.
Definition VariableStore.cxx:128
LabeledAxis & output_axis()
Definition VariableStore.h:97
VariableBase * input_view(const VariableName &)
Get the view of an input variable.
Definition VariableStore.cxx:57
virtual void setup_output_views()
Tell each output variable view which tensor storage(s) to view into.
Definition VariableStore.cxx:114
virtual void allocate_variables(TorchShapeRef batch_shape, const torch::TensorOptions &options, bool in, bool out, bool dout_din, bool d2out_din2)
Allocate variable storages given the batch shape and tensor options.
Definition VariableStore.cxx:78
virtual void detach_and_zero(bool out, bool dout_din=true, bool d2out_din2=true)
Detach the tensor storages and set each element in the tensor to 0.
Definition VariableStore.cxx:135
Storage< VariableName, VariableBase > & input_views()
Definition VariableStore.h:103
virtual void setup_layout()
Setup the layouts of all the registered axes.
Definition VariableStore.cxx:50
LabeledVector & input_storage()
Definition VariableStore.h:115
virtual void cache(TorchShapeRef batch_shape)
Cache the variable's batch shape.
Definition VariableStore.cxx:69
LabeledTensor3D & second_derivative_storage()
Definition VariableStore.h:133
LabeledAxis & input_axis()
Definition VariableStore.h:91
virtual void reinit_input_views()
Create the views for input variables.
Definition VariableStore.cxx:121
TorchShape add_shapes(S &&... shape)
Definition utils.h:294
Definition CrossRef.cxx:32
const torch::TensorOptions default_tensor_options()
Definition types.cxx:30
void neml_assert_dbg(bool assertion, Args &&... args)
Definition error.h:85
int64_t TorchSize
Definition types.h:35
std::vector< TorchSize > TorchShape
Definition types.h:36
Diagnosis make_diagnosis(Args &&... args)
Definition error.h:94
void neml_assert_batch_broadcastable(T &&...)
A helper function to assert that all tensors are batch-broadcastable.
torch::IntArrayRef TorchShapeRef
Definition types.h:37
LabeledAxisAccessor VariableName
Definition Variable.h:35
void neml_assert(bool assertion, Args &&... args)
Definition error.h:73