29#include "neml2/models/map_types_fwd.h"
30#include "neml2/base/LabeledAxisAccessor.h"
31#include "neml2/misc/types.h"
39template <
typename,
typename>
152 virtual void set(
const ATensor & val,
bool force =
false) = 0;
178 void request_AD(
const std::vector<const VariableBase *> & us);
184 void request_AD(
const std::vector<const VariableBase *> & u1s,
185 const std::vector<const VariableBase *> & u2s);
197 virtual void clear();
238class Variable :
public VariableBase
241 template <
typename T2 = T,
typename =
typename std::enable_if_t<!std::is_same_v<Tensor, T2>>>
250 template <
typename T2 = T,
typename =
typename std::enable_if_t<std::is_same_v<Tensor, T2>>>
267 Model *
owner =
nullptr)
const override;
269 void ref(
const VariableBase & var,
bool ref_is_mutable =
false)
override;
279 void set(
const ATensor & val,
bool force =
false)
override;
296 operator T()
const {
return value(); }
298 void clear()
override;
324 : _base_sizes(base_sizes),
331 template <
typename T>
357#define FWD_VARIABLE_BINARY_OP(op) \
358 template <typename T1, \
360 typename = typename std::enable_if_t<std::is_base_of_v<VariableBase, T1> || \
361 std::is_base_of_v<VariableBase, T2>>> \
362 auto op(const T1 & a, const T2 & b) \
364 if constexpr (std::is_base_of_v<VariableBase, T1> && std::is_base_of_v<VariableBase, T2>) \
365 return op(a.value(), b.value()); \
367 if constexpr (std::is_base_of_v<VariableBase, T1> && !std::is_base_of_v<VariableBase, T2>) \
368 return op(a.value(), b); \
370 if constexpr (!std::is_base_of_v<VariableBase, T1> && std::is_base_of_v<VariableBase, T2>) \
371 return op(a, b.value()); \
374FWD_VARIABLE_BINARY_OP(
operator+);
375FWD_VARIABLE_BINARY_OP(
operator-);
376FWD_VARIABLE_BINARY_OP(
operator*);
377FWD_VARIABLE_BINARY_OP(
operator/);
The DependencyResolver identifies and resolves the dependencies among a set of objects derived from D...
Definition Variable.h:40
Definition Variable.h:315
Derivative()
Definition Variable.h:317
Derivative & operator=(const Tensor &val)
Definition Variable.cxx:602
Derivative & operator=(const Variable< T > &var)
Definition Variable.h:332
Derivative(TensorShapeRef base_sizes, Tensor *deriv)
Definition Variable.h:323
The accessor containing all the information needed to access an item in a LabeledAxis.
Definition LabeledAxisAccessor.h:56
The base class for all constitutive models.
Definition Model.h:68
Base class of variable.
Definition Variable.h:52
virtual void set(const ATensor &val, bool force=false)=0
VariableBase(VariableBase &&)=delete
Device device() const
Device.
Definition Variable.cxx:118
TraceableTensorShape batch_sizes() const
Return the batch shape.
Definition Variable.cxx:166
virtual ~VariableBase()=default
bool is_old_force() const
Definition Variable.cxx:76
Model *const _owner
The model which declared this variable.
Definition Variable.h:212
bool requires_grad() const
Check if this variable is part of the AD function graph.
Definition Variable.cxx:208
bool is_parameter() const
Definition Variable.cxx:88
bool is_state() const
Definition Variable.cxx:58
const Model & owner() const
Definition Variable.cxx:44
void apply_second_order_chain_rule(const DependencyResolver< Model, VariableName > &)
Apply second order chain rule.
Definition Variable.cxx:301
Derivative d(const VariableBase &var)
Wrapper for assigning partial derivative.
Definition Variable.cxx:214
virtual const VariableBase * ref() const =0
Get the referencing variable (returns this if this is a storing variable)
Dtype scalar_type() const
Scalar type.
Definition Variable.cxx:112
Size base_storage() const
Base storage of the variable.
Definition Variable.cxx:196
void clear_derivatives()
Clear only the derivatives.
Definition Variable.cxx:283
bool batched() const
Whether the tensor is batched.
Definition Variable.cxx:142
Size batch_dim() const
Return the number of batch dimensions.
Definition Variable.cxx:148
void request_AD(const VariableBase &u)
Definition Variable.cxx:241
Size dim() const
Number of tensor dimensions.
Definition Variable.cxx:124
TensorShapeRef sizes() const
Tensor shape.
Definition Variable.cxx:130
Size base_size(Size dim) const
Return the size of a base axis.
Definition Variable.cxx:184
TensorShapeRef list_sizes() const
Return the list shape.
Definition Variable.cxx:172
DerivMap & second_derivatives()
Definition Variable.h:194
Size list_dim() const
Return the number of list dimensions.
Definition Variable.cxx:154
virtual Tensor get() const =0
Get the variable value (with flattened base dimensions, i.e., for assembly purposes)
virtual void ref(const VariableBase &other, bool ref_is_mutable=false)=0
Reference another variable.
virtual void set(const Tensor &val)=0
Set the variable value.
virtual std::unique_ptr< VariableBase > clone(const VariableName &name={}, Model *owner=nullptr) const =0
Clone this variable.
bool is_solve_dependent() const
Definition Variable.cxx:94
virtual void zero(const TensorOptions &options)=0
Set the variable value to zero.
VariableBase(const VariableBase &)=delete
bool is_residual() const
Definition Variable.cxx:82
const ValueMap & derivatives() const
Partial derivatives.
Definition Variable.h:189
ValueMap & derivatives()
Definition Variable.h:190
TraceableSize batch_size(Size dim) const
Return the size of a batch axis.
Definition Variable.cxx:178
VariableBase & operator=(VariableBase &&)=delete
const VariableName & name() const
Name of this variable.
Definition Variable.h:65
virtual void operator=(const Tensor &val)=0
Assignment operator.
bool is_dependent() const
Check if the derivative with respect to this variable should be evaluated.
Definition Variable.cxx:100
VariableBase & operator=(const VariableBase &)=delete
const VariableName _name
Name of the variable.
Definition Variable.h:209
virtual TensorShapeRef base_sizes() const =0
Return the base shape.
const DerivMap & second_derivatives() const
Partial second derivatives.
Definition Variable.h:193
Size size(Size dim) const
Size of a dimension.
Definition Variable.cxx:136
Size list_size(Size dim) const
Return the size of a list axis.
Definition Variable.cxx:190
virtual bool owning() const =0
Check if this is an owning variable.
virtual void clear()
Clear the variable value and derivatives.
Definition Variable.cxx:276
Size base_dim() const
Return the number of base dimensions.
Definition Variable.cxx:160
bool is_force() const
Definition Variable.cxx:70
virtual TensorType type() const =0
Variable tensor type.
void apply_chain_rule(const DependencyResolver< Model, VariableName > &)
Apply first order chain rule.
Definition Variable.cxx:290
virtual Tensor tensor() const =0
Get the variable value.
Size assembly_storage() const
Assembly storage of the variable.
Definition Variable.cxx:202
TensorOptions options() const
Definition Variable.cxx:106
bool is_old_state() const
Definition Variable.cxx:64
virtual void requires_grad_(bool req=true)=0
Mark this variable as a leaf variable in tracing function graph for AD.
Concrete definition of a variable.
Definition VariableStore.h:41
Tensor get() const override
Get the variable value (with flattened base dimensions, i.e., for assembly purposes)
Definition Variable.cxx:523
const Variable< T > * _ref
The variable referenced by this (nullptr if this is a storing variable)
Definition Variable.h:305
Tensor tensor() const override
Get the variable value.
Definition Variable.cxx:530
void requires_grad_(bool req=true) override
Mark this variable as a leaf variable in tracing function graph for AD.
Definition Variable.cxx:544
Variable(VariableName name_in, Model *owner, TensorShapeRef list_shape, TensorShapeRef base_shape)
Definition Variable.h:251
bool _ref_is_mutable
Whether mutating the referenced variable is allowed.
Definition Variable.h:308
void operator=(const Tensor &val) override
Assignment operator.
Definition Variable.cxx:555
const VariableBase * ref() const override
Get the referencing variable (returns this if this is a storing variable)
Definition Variable.h:271
void zero(const TensorOptions &options) override
Set the variable value to zero.
Definition Variable.cxx:449
T operator-() const
Negation.
Definition Variable.h:293
const TensorShape _base_sizes
Base shape of the variable.
Definition Variable.h:302
void set(const Tensor &val) override
Set the variable value.
Definition Variable.cxx:475
const T & value() const
Variable value.
Definition Variable.h:290
TensorType type() const override
Variable tensor type.
Definition Variable.cxx:389
bool owning() const override
Check if this is an owning variable.
Definition Variable.h:273
std::unique_ptr< VariableBase > clone(const VariableName &name={}, Model *owner=nullptr) const override
Clone this variable.
Definition Variable.cxx:396
T _value
Variable value (undefined if this is a referencing variable)
Definition Variable.h:311
Variable(VariableName name_in, Model *owner, TensorShapeRef list_shape)
Definition Variable.h:242
TensorShapeRef base_sizes() const override
Return the base shape.
Definition Variable.h:264
void clear() override
Clear the variable value and derivatives.
Definition Variable.cxx:576
Definition DiagnosticsInterface.cxx:30
c10::Device Device
Definition types.h:63
c10::SmallVector< Size, 8 > TensorShape
Definition types.h:66
std::map< LabeledAxisAccessor, Tensor > ValueMap
Definition map_types_fwd.h:33
std::map< LabeledAxisAccessor, ValueMap > DerivMap
Definition map_types_fwd.h:34
int64_t Size
Definition types.h:65
TensorType
Definition tensors.h:61
at::Tensor ATensor
Definition types.h:38
c10::TensorOptions TensorOptions
Definition types.h:60
c10::ArrayRef< Size > TensorShapeRef
Definition types.h:67
c10::ScalarType Dtype
Definition types.h:61
Traceable size.
Definition TraceableSize.h:40
Traceable tensor shape.
Definition TraceableTensorShape.h:38