NEML2 2.0.0
Loading...
Searching...
No Matches
VariableBase Class Referenceabstract

Base class of variable. More...

Detailed Description

Base class of variable.

Specific implementations are defined by the derived class Variable<T> where we rely on polymorphism so that we can store different types of variables in the same container.

#include <VariableBase.h>

Inheritance diagram for VariableBase:

Public Member Functions

 VariableBase ()=default
 
 VariableBase (const VariableBase &)=delete
 
 VariableBase (VariableBase &&)=delete
 
VariableBaseoperator= (const VariableBase &)=delete
 
VariableBaseoperator= (VariableBase &&)=delete
 
virtual ~VariableBase ()=default
 
 VariableBase (VariableName name_in, Model *owner, TensorShapeRef base_shape, TensorShapeRef dep_intmd_dims)
 
const VariableNamename () const
 Name of this variable.
 
virtual TensorType type () const =0
 Variable tensor type.
 
virtual std::unique_ptr< VariableBaseclone (const VariableName &name={}, Model *owner=nullptr) const =0
 Clone this variable.
 
virtual void ref (const VariableBase &other, bool ref_is_mutable=false)=0
 Reference another variable.
 
virtual const VariableBaseref () const =0
 Get the referencing variable (returns this if this is a storing variable)
 
void set_intmd_sizes (TensorShapeRef shape)
 Set the intermediate shape.
 
ArrayRef< Sizedep_intmd_dims () const
 Get dependent intermediate dimensions for derivative calculation.
 
virtual bool owning () const =0
 Check if this is an owning variable.
 
Tensor zeros (const TensorOptions &options) const
 Make zeros tensor with the shape of this variable.
 
virtual void zero (const TensorOptions &options)=0
 Set the variable value to zero.
 
virtual void set (const Tensor &val, std::optional< TracerPrivilege > key=std::nullopt)=0
 Set the variable value from a Tensor in assembly format.
 
virtual Tensor get () const =0
 Get the variable value in assembly format.
 
virtual Tensor tensor () const =0
 Get the variable value cast to Tensor.
 
bool requires_grad () const
 Check if this variable is part of the AD function graph.
 
virtual void requires_grad_ (bool req=true)=0
 Mark this variable as a leaf variable in tracing function graph for AD.
 
virtual void operator= (const Tensor &val)=0
 Assignment operator.
 
bool has_derivative (const VariableName &vname) const
 Whether the variable has non-zero derivative with respect to another variable.
 
bool has_derivative (const VariableName &v1name, const VariableName &v2name) const
 Whether the variable has non-zero second derivative with respect to another variable.
 
Derivative< 1 > & d (const VariableBase &var, ArrayRef< Size > dep_dims={})
 Wrapper for assigning partial derivative.
 
const Derivative< 1 > & d (const VariableBase &var) const
 
Derivative< 2 > & d2 (const VariableBase &var1, const VariableBase &var2, ArrayRef< Size > dep_dims={})
 Wrapper for assigning second partial derivative.
 
const Derivative< 2 > & d2 (const VariableBase &var1, const VariableBase &var2) const
 
const std::vector< Derivative< 1 > > & derivatives () const
 Partial derivatives.
 
std::vector< Derivative< 1 > > & derivatives ()
 
const std::vector< Derivative< 2 > > & second_derivatives () const
 Partial second derivatives.
 
std::vector< Derivative< 2 > > & second_derivatives ()
 
virtual void clear ()
 Clear the variable value and derivatives.
 
void clear_derivatives ()
 Clear only the derivatives.
 
void apply_chain_rule (const DependencyResolver< Model, VariableName > &)
 Apply first order chain rule.
 
void apply_second_order_chain_rule (const DependencyResolver< Model, VariableName > &)
 Apply second order chain rule.
 
const Modelowner () const
 
Modelowner ()
 
Subaxis
bool is_state () const
 
bool is_old_state () const
 
bool is_force () const
 
bool is_old_force () const
 
bool is_residual () const
 
bool is_parameter () const
 
bool is_solve_dependent () const
 
bool is_dependent () const
 Check if the derivative with respect to this variable should be evaluated.
 
Tensor information
virtual bool defined () const =0
 
virtual TensorOptions options () const =0
 Tensor options.
 
virtual Dtype scalar_type () const =0
 Scalar type.
 
virtual Device device () const =0
 Device.
 
Size dim () const
 
Size batch_dim () const
 
Size base_dim () const
 
Size dynamic_dim () const
 
Size static_dim () const
 
Size intmd_dim () const
 
TensorShapeRef sizes () const
 
TraceableTensorShape batch_sizes () const
 
TensorShapeRef base_sizes () const
 
virtual const TraceableTensorShapedynamic_sizes () const =0
 
TensorShapeRef static_sizes () const
 
TensorShapeRef intmd_sizes () const
 
Size size (Size i) const
 
TraceableSize batch_size (Size i) const
 
Size base_size (Size i) const
 
const TraceableSizedynamic_size (Size i) const
 
Size static_size (Size i) const
 
Size intmd_size (Size i) const
 
void request_AD (const VariableBase &u)
 
void request_AD (const std::vector< const VariableBase * > &us)
 
void request_AD (const VariableBase &u1, const VariableBase &u2)
 
void request_AD (const std::vector< const VariableBase * > &u1s, const std::vector< const VariableBase * > &u2s)
 

Public Attributes

const VariableName _name = {}
 Name of the variable.
 
Model *const _owner = nullptr
 The model which declared this variable.
 
TensorShape _cached_intmd_sizes = {}
 
const TensorShape _base_sizes = {}
 Base shape of the variable.
 
const TensorShape _dep_intmd_dims = {}
 Dependent intermediate dimensions for derivative calculation.
 

Constructor & Destructor Documentation

◆ VariableBase() [1/4]

VariableBase ( )
default

◆ VariableBase() [2/4]

VariableBase ( const VariableBase & )
delete

◆ VariableBase() [3/4]

VariableBase ( VariableBase && )
delete

◆ ~VariableBase()

virtual ~VariableBase ( )
virtualdefault

◆ VariableBase() [4/4]

VariableBase ( VariableName name_in,
Model * owner,
TensorShapeRef base_shape,
TensorShapeRef dep_intmd_dims )

Member Function Documentation

◆ apply_chain_rule()

void apply_chain_rule ( const DependencyResolver< Model, VariableName > & dep)

Apply first order chain rule.

◆ apply_second_order_chain_rule()

void apply_second_order_chain_rule ( const DependencyResolver< Model, VariableName > & dep)

Apply second order chain rule.

◆ base_dim()

Size base_dim ( ) const

◆ base_size()

Size base_size ( Size i) const

◆ base_sizes()

TensorShapeRef base_sizes ( ) const

◆ batch_dim()

Size batch_dim ( ) const

◆ batch_size()

TraceableSize batch_size ( Size i) const

◆ batch_sizes()

TraceableTensorShape batch_sizes ( ) const

◆ clear()

◆ clear_derivatives()

void clear_derivatives ( )

Clear only the derivatives.

◆ clone()

virtual std::unique_ptr< VariableBase > clone ( const VariableName & name = {},
Model * owner = nullptr ) const
pure virtual

◆ d() [1/2]

const Derivative< 1 > & d ( const VariableBase & var) const

◆ d() [2/2]

Derivative< 1 > & d ( const VariableBase & var,
ArrayRef< Size > dep_dims = {} )

Wrapper for assigning partial derivative.

◆ d2() [1/2]

const Derivative< 2 > & d2 ( const VariableBase & var1,
const VariableBase & var2 ) const

◆ d2() [2/2]

Derivative< 2 > & d2 ( const VariableBase & var1,
const VariableBase & var2,
ArrayRef< Size > dep_dims = {} )

Wrapper for assigning second partial derivative.

◆ defined()

◆ dep_intmd_dims()

TensorShapeRef dep_intmd_dims ( ) const

Get dependent intermediate dimensions for derivative calculation.

◆ derivatives() [1/2]

std::vector< Derivative< 1 > > & derivatives ( )
inline

◆ derivatives() [2/2]

const std::vector< Derivative< 1 > > & derivatives ( ) const
inline

Partial derivatives.

◆ device()

◆ dim()

Size dim ( ) const
Returns
the number of dimensions

◆ dynamic_dim()

Size dynamic_dim ( ) const

◆ dynamic_size()

const TraceableSize & dynamic_size ( Size i) const

◆ dynamic_sizes()

◆ get()

◆ has_derivative() [1/2]

bool has_derivative ( const VariableName & v1name,
const VariableName & v2name ) const

Whether the variable has non-zero second derivative with respect to another variable.

◆ has_derivative() [2/2]

bool has_derivative ( const VariableName & vname) const

Whether the variable has non-zero derivative with respect to another variable.

◆ intmd_dim()

Size intmd_dim ( ) const

◆ intmd_size()

Size intmd_size ( Size i) const

◆ intmd_sizes()

TensorShapeRef intmd_sizes ( ) const

◆ is_dependent()

bool is_dependent ( ) const

Check if the derivative with respect to this variable should be evaluated.

◆ is_force()

bool is_force ( ) const

◆ is_old_force()

bool is_old_force ( ) const

◆ is_old_state()

bool is_old_state ( ) const

◆ is_parameter()

bool is_parameter ( ) const

◆ is_residual()

bool is_residual ( ) const

◆ is_solve_dependent()

bool is_solve_dependent ( ) const

◆ is_state()

bool is_state ( ) const

◆ name()

const VariableName & name ( ) const
inline

Name of this variable.

◆ operator=() [1/3]

◆ operator=() [2/3]

VariableBase & operator= ( const VariableBase & )
delete

◆ operator=() [3/3]

VariableBase & operator= ( VariableBase && )
delete

◆ options()

◆ owner() [1/2]

Model & owner ( )

◆ owner() [2/2]

const Model & owner ( ) const

The Model who declared this variable

◆ owning()

virtual bool owning ( ) const
pure virtual

◆ ref() [1/2]

virtual const VariableBase * ref ( ) const
pure virtual

◆ ref() [2/2]

virtual void ref ( const VariableBase & other,
bool ref_is_mutable = false )
pure virtual

◆ request_AD() [1/4]

void request_AD ( const std::vector< const VariableBase * > & u1s,
const std::vector< const VariableBase * > & u2s )

◆ request_AD() [2/4]

void request_AD ( const std::vector< const VariableBase * > & us)

◆ request_AD() [3/4]

void request_AD ( const VariableBase & u)

Request to use AD to calculate the derivative of this variable with respect to another variable

◆ request_AD() [4/4]

void request_AD ( const VariableBase & u1,
const VariableBase & u2 )

Request to use AD to calculate the second derivative of this variable with respect to two other variables

◆ requires_grad()

bool requires_grad ( ) const

Check if this variable is part of the AD function graph.

◆ requires_grad_()

virtual void requires_grad_ ( bool req = true)
pure virtual

◆ scalar_type()

◆ second_derivatives() [1/2]

std::vector< Derivative< 2 > > & second_derivatives ( )
inline

◆ second_derivatives() [2/2]

const std::vector< Derivative< 2 > > & second_derivatives ( ) const
inline

Partial second derivatives.

◆ set()

virtual void set ( const Tensor & val,
std::optional< TracerPrivilege > key = std::nullopt )
pure virtual

◆ set_intmd_sizes()

void set_intmd_sizes ( TensorShapeRef shape)

Set the intermediate shape.

See also
neml2::VariableStore::set_input_intmd_sizes

◆ size()

Size size ( Size i) const
Returns
the size of dimension i

◆ sizes()

TensorShapeRef sizes ( ) const
Returns
the tensor shape

◆ static_dim()

Size static_dim ( ) const

◆ static_size()

Size static_size ( Size i) const

◆ static_sizes()

TensorShapeRef static_sizes ( ) const

◆ tensor()

◆ type()

◆ zero()

◆ zeros()

Tensor zeros ( const TensorOptions & options) const

Make zeros tensor with the shape of this variable.

Member Data Documentation

◆ _base_sizes

const TensorShape _base_sizes = {}

Base shape of the variable.

◆ _cached_intmd_sizes

TensorShape _cached_intmd_sizes = {}

Cached intermediate shape that this variable last saw

Note
: set() and operator=() are the only methods that cache this. clear() does not invalidate the cache.

◆ _dep_intmd_dims

const TensorShape _dep_intmd_dims = {}

Dependent intermediate dimensions for derivative calculation.

◆ _name

const VariableName _name = {}

Name of the variable.

◆ _owner

Model* const _owner = nullptr

The model which declared this variable.