25#include "neml2/models/Model.h"
26#include <torch/autograd.h>
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;
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;
59 _AD_1st_deriv(options.get<
bool>(
"_use_AD_first_derivative")),
60 _AD_2nd_deriv(options.get<
bool>(
"_use_AD_second_derivative")),
63 _extra_deriv_order(options.get<
int>(
"_extra_derivative_order")),
64 _nonlinear_system(options.get<
bool>(
"_nonlinear_system"))
72 neml_assert(
host() ==
this,
"This method should only be called on the host model.");
74 std::vector<Diagnosis>
errors;
82 ": input axis has sub-axis 'old_state', but output axis does not "
83 "have sub-axis 'state'."));
90 ": input axis has old state named ",
92 ", but it doesn't exist on the output axis."));
125 const torch::Device &
device,
126 const torch::Dtype &
dtype)
128 neml_assert(
host() ==
this,
"This method should only be called on the host model.");
205 submodel->setup_submodel_input_views();
224 &
submodel->second_derivative_storage());
226 submodel->setup_submodel_output_views();
298 throw NEMLException(
"AD derivative is requested, but AD second derivative is not requested.");
321 "Incompatible input axis. The model has input axis: \n",
323 "The input vector has axis: \n",
356std::tuple<LabeledVector, LabeledMatrix>
365std::tuple<LabeledVector, LabeledMatrix, LabeledTensor3D>
384 "value_and_dvalue() is called but derivative storage hasn't been allocated.");
392 extract_derivatives(
true,
false,
true);
400 "value_and_dvalue_and_d2value() is called but second derivative storage hasn't "
414 extract_derivatives(
true,
true,
true);
417 extract_second_derivatives(
433const std::set<VariableName>
439const std::set<VariableName>
468 grad_outputs.index_put_({torch::indexing::Ellipsis,
i}, 1.0);
478 if (dyi_dvar.defined())
489Model::extract_second_derivatives(
bool retain_graph,
bool create_graph,
bool allow_unused)
497 grad_outputs.index_put_({torch::indexing::Ellipsis, i, j}, 1.0);
506 if (dydxij_dvar.defined())
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
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 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