25#include "neml2/models/ComposedModel.h"
36 "Compose multiple models together to form a single model. The composed model can then be "
37 "treated as a new model and composed with others. The [system documentation](@ref "
38 "model-composition) provides in-depth explanation on how the models are composed together.";
42 options.set<std::vector<std::string>>(
"models");
43 options.set(
"models").doc() =
"Models being composed together";
45 options.set<std::vector<VariableName>>(
"additional_outputs");
46 options.set(
"additional_outputs").doc() =
47 "Extra output variables to be extracted from the composed model in addition to the ones "
48 "identified through dependency resolution.";
50 options.set<std::vector<std::string>>(
"priority");
52 "Priorities of models in decreasing order. A model with higher priority will be evaluated "
53 "first. This is useful for breaking cyclic dependency.";
85 for (
const auto &
var : _additional_outputs)
86 _dependency.add_additional_outbound_item(
var);
95 _dependency.unique_item_provider() =
true;
96 _dependency.unique_item_consumer() =
false;
97 _dependency.resolve();
103 for (
const auto &
item : _dependency.inbound_items())
106 auto sz =
item.parent->input_axis().storage_size(
var);
110 "Multiple sub-models in a ComposedModel define the same input variable ",
112 ", but with different shape/storage size.");
118 for (
const auto &
item : _dependency.outbound_items())
121 auto sz =
item.parent->output_axis().storage_size(
var);
123 "Multiple sub-models in a ComposedModel define the same output variable ",
133 for (
auto && [
pname, param] :
m.nl_params())
135 neml_assert_dbg(param->name().size() == 1,
"Internal parameter name error");
137 auto submodel = Factory::get_object_ptr<Model>(
"Models", param->name().vec()[0]);
150 "ComposedModel does not use automatic differentiation. _use_AD_first_derivative and "
151 "_use_AD_second_derivative should be set to false.");
168 for (
const auto &
item : _dependency.inbound_items())
172 for (
const auto & [
item,
providers] : _dependency.item_providers())
175 ->setup_views(&
providers.begin()->parent->output_storage());
177 submodel->setup_submodel_input_views();
184 clear_chain_rule_cache();
191 i->value_and_dvalue();
193 i->value_and_dvalue_and_d2value();
195 throw NEMLException(
"Unsupported call signature to set_value");
201 apply_second_order_chain_rule(
i);
204 for (
auto model : _dependency.end_nodes())
216 clear_chain_rule_cache();
220ComposedModel::apply_chain_rule(
Model *
i)
223 dpin_din.fill(_din_din);
225 if (_dependency.node_providers().count(i))
226 for (
auto dep : _dependency.node_providers().at(i))
227 dpin_din.fill(_dpout_din[dep]);
233ComposedModel::apply_second_order_chain_rule(Model * i)
238 dpin_din.fill(_din_din);
240 if (_dependency.node_providers().count(i))
241 for (
auto dep : _dependency.node_providers().at(i))
243 dpin_din.fill(_dpout_din[dep]);
244 d2pin_din2.fill(_d2pout_din2[dep]);
247 _dpout_din[i] = i->derivative_storage().chain(dpin_din);
249 i->second_derivative_storage().chain(d2pin_din2, i->derivative_storage(), dpin_din);
Definition ComposedModel.h:33
virtual void setup_submodel_input_views() override
Definition ComposedModel.cxx:164
virtual void check_AD_limitation() const override
Definition ComposedModel.cxx:146
virtual void allocate_variables(int deriv_order, bool options_changed) override
Call VariableStore::allocate_variables recursively on all submodels.
Definition ComposedModel.cxx:155
void register_nonlinear_params(const Model &m)
Recursively register sub-model's nonlinar parameters.
Definition ComposedModel.cxx:131
static OptionSet expected_options()
Definition ComposedModel.cxx:32
void set_value(bool, bool, bool) override
The map between input -> output, and optionally its derivatives.
Definition ComposedModel.cxx:182
The wrapper (decorator) for cross-referencing unresolved values at parse time.
Definition CrossRef.h:52
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:44
TorchSize storage_size() const
Get the (total) storage size of this axis.
Definition LabeledAxis.h:142
bool has_variable(const LabeledAxisAccessor &var) const
Does the variable of a given primitive type exist?
Definition LabeledAxis.h:126
LabeledMatrix chain(const LabeledMatrix &other) const
Chain rule product of two derivatives.
Definition LabeledMatrix.cxx:61
static LabeledMatrix identity(TorchShapeRef batch_size, const LabeledAxis &axis, const torch::TensorOptions &options=default_tensor_options())
Create a labeled identity tensor.
Definition LabeledMatrix.cxx:34
void fill(const LabeledMatrix &other, bool recursive=true)
Definition LabeledMatrix.cxx:52
void fill(const LabeledTensor3D &other, bool recursive=true)
Definition LabeledTensor3D.cxx:43
static LabeledTensor3D zeros(TorchShapeRef batch_shape, const std::vector< const LabeledAxis * > &axes, const torch::TensorOptions &options=default_tensor_options())
Setup new storage with zeros.
Definition LabeledTensor.cxx:109
static LabeledMatrix empty(TorchShapeRef batch_shape, const std::vector< const LabeledAxis * > &axes, const torch::TensorOptions &options=default_tensor_options())
Setup new empty storage.
Definition LabeledTensor.cxx:87
void fill(const LabeledVector &other, bool recursive=true)
Definition LabeledVector.cxx:45
The base class for all constitutive models.
Definition Model.h:53
friend class ComposedModel
ComposedModel's set_value need to call submodel's set_value.
Definition Model.h:194
TorchShapeRef batch_sizes() const
This model's batch shape.
Definition Model.h:113
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 allocate_variables(int deriv_order, bool options_changed)
Call VariableStore::allocate_variables recursively on all submodels.
Definition Model.cxx:159
const torch::TensorOptions & options() const
This model's tensor options.
Definition Model.h:116
bool _AD_1st_deriv
Whether to use AD to compute 1st derivatives.
Definition Model.h:278
static OptionSet expected_options()
Definition Model.cxx:33
Model * registered_model(const std::string &name) const
Get a registered model by its name.
Definition Model.cxx:423
static void enable_automatic_scaling(OptionSet &options)
Definition NonlinearSystem.cxx:62
A custom map-like data structure. The keys are strings, and the values can be nonhomogeneously typed.
Definition OptionSet.h:59
LabeledMatrix & derivative_storage()
Definition VariableStore.h:127
Variable< T > & declare_output_variable(S &&... name)
Declare an output variable.
Definition VariableStore.h:205
LabeledVector & output_storage()
Definition VariableStore.h:121
const Variable< T > & declare_input_variable(S &&... name)
Declare an input variable.
Definition VariableStore.h:180
LabeledAxis & output_axis()
Definition VariableStore.h:97
VariableBase * input_view(const VariableName &)
Get the view of an input variable.
Definition VariableStore.cxx:57
LabeledTensor3D & second_derivative_storage()
Definition VariableStore.h:133
LabeledAxis & input_axis()
Definition VariableStore.h:91
Definition CrossRef.cxx:32
void neml_assert_dbg(bool assertion, Args &&... args)
Definition error.h:85
void neml_assert(bool assertion, Args &&... args)
Definition error.h:73