tvm
Public Member Functions | Friends | List of all members
tvm::relax::NestedMsg< T > Class Template Reference

Container that stores possibly nested message with leaf message type T. More...

#include <nested_msg.h>

Collaboration diagram for tvm::relax::NestedMsg< T >:

Public Member Functions

 NestedMsg ()=default
 
 NestedMsg (const NestedMsg< T > &)=default
 
 NestedMsg (NestedMsg< T > &&)=default
 
NestedMsg< T > & operator= (const NestedMsg< T > &)=default
 
NestedMsg< T > & operator= (NestedMsg< T > &&)=default
 
 NestedMsg (std::nullopt_t)
 Nullopt handling. More...
 
 NestedMsg (std::nullptr_t)
 
NestedMsg< T > & operator= (std::nullptr_t)
 
 NestedMsg (T other)
 
NestedMsg< T > & operator= (T other)
 
 NestedMsg (Array< NestedMsg< T >, void > other)
 
NestedMsg< T > & operator= (Array< NestedMsg< T >, void > other)
 
 NestedMsg (std::initializer_list< NestedMsg< T >> other)
 
NestedMsg< T > & operator= (std::initializer_list< NestedMsg< T >> other)
 
 NestedMsg (int val)=delete
 
NestedMsg< T > & operator= (int val)=delete
 
bool operator== (std::nullptr_t) const
 
bool operator!= (std::nullptr_t) const
 
bool IsLeaf () const
 
bool IsNull () const
 
bool IsNested () const
 
LeafValue () const
 
Array< NestedMsg< T >, void > NestedArray () const
 

Friends

template<typename , typename >
struct ffi::TypeTraits
 

Detailed Description

template<typename T>
class tvm::relax::NestedMsg< T >

Container that stores possibly nested message with leaf message type T.

NestedMsg is a helper structure to store intermediate message state in pass analysis so we can robustly handle message passing with the presence of nested tuple types.

Under the hood, NestedMsg[T] = Union[T, std::nullopt, Array[NestedMsg[T]]]. Each nested message corresponds to the same nesting structure as the nested tuple types when we encounter them in analysis.

Relax support nested tuple structures in the IR. Nested tuple structure is important to support advanced groupings in cases such as gradient calculation and other scenarios.

The possible presence of nested tuple does mean that we need to to robustly handle analysis that contains nested tuple structures in a dataflow graph.

v1 = relu(v0)
v2 = exp(v0)
t = ((v0, v1), (v2,), v0)
t1 = t[0]
v3 = concat(t1)
v4 = t[2]
v5 = add(v4, v3)
tvm::te::Tensor relu(const tvm::te::Tensor &t, T threshold=static_cast< T >(0), std::string name="T_relu", std::string tag=kElementWise)
Creates an operation that performs a rectified linear unit.
Definition: nn.h:55
PrimExpr exp(PrimExpr x, Span span=Span())
Definition: op.h:724
PrimExpr add(PrimExpr a, PrimExpr b, Span span=Span())
add operator

Consider the above code sequence that contains a mixture of tuple nesting and normal operations. A common message-passing-based analysis will track messages attached to each intermediate variable.

Because the intermediate value can contain nested-tuples, we need to have abilities to nest messages according to tuple structure and propagate them along the way. In python, this simply corresponds to using a tuple to hold nested messages. This class provides a helper wrapper in C++ to present such possibly nested message for a given leaf message.

This design pattern is necessary to handle tuple values regardless of the normal form design of the IR to enable different messages for each tuple component without enforcing all tuple elements to have the same message.

Please consider the following patterns in our pass:

On a forward propagation message passing analysis:

On a backward propagation message passing analysis:

Here leafnode is a node that you would like to propagate messages to such as constant, var and should not include tuple.

We also recommend writing unit-test cases that involve nested tuple composition and decomposition.

See also
MapToNestedMsg, DecomposeNestedMsg, CombineNestedMsg, ForEachLeaf, Equal
Note
If you want to write robust message passing-based analysis for programs that can contain nested tuples, you likely need to use this class or logic of a similar kind.

Constructor & Destructor Documentation

◆ NestedMsg() [1/9]

template<typename T >
tvm::relax::NestedMsg< T >::NestedMsg ( )
default

◆ NestedMsg() [2/9]

template<typename T >
tvm::relax::NestedMsg< T >::NestedMsg ( const NestedMsg< T > &  )
default

◆ NestedMsg() [3/9]

template<typename T >
tvm::relax::NestedMsg< T >::NestedMsg ( NestedMsg< T > &&  )
default

◆ NestedMsg() [4/9]

template<typename T >
tvm::relax::NestedMsg< T >::NestedMsg ( std::nullopt_t  )
inline

Nullopt handling.

◆ NestedMsg() [5/9]

template<typename T >
tvm::relax::NestedMsg< T >::NestedMsg ( std::nullptr_t  )
inlineexplicit

◆ NestedMsg() [6/9]

template<typename T >
tvm::relax::NestedMsg< T >::NestedMsg ( other)
inline

◆ NestedMsg() [7/9]

template<typename T >
tvm::relax::NestedMsg< T >::NestedMsg ( Array< NestedMsg< T >, void >  other)
inline

◆ NestedMsg() [8/9]

template<typename T >
tvm::relax::NestedMsg< T >::NestedMsg ( std::initializer_list< NestedMsg< T >>  other)
inline

◆ NestedMsg() [9/9]

template<typename T >
tvm::relax::NestedMsg< T >::NestedMsg ( int  val)
explicitdelete

Member Function Documentation

◆ IsLeaf()

template<typename T >
bool tvm::relax::NestedMsg< T >::IsLeaf ( ) const
inline
Returns
Whether the nested message is not-null leaf value

◆ IsNested()

template<typename T >
bool tvm::relax::NestedMsg< T >::IsNested ( ) const
inline
Returns
Whether the nested message is nested

◆ IsNull()

template<typename T >
bool tvm::relax::NestedMsg< T >::IsNull ( ) const
inline
Returns
Whether the nested message is null

◆ LeafValue()

template<typename T >
T tvm::relax::NestedMsg< T >::LeafValue ( ) const
inline
Returns
The underlying leaf value.
Note
This function checks if the msg is leaf.

◆ NestedArray()

template<typename T >
Array<NestedMsg<T>, void> tvm::relax::NestedMsg< T >::NestedArray ( ) const
inline
Returns
a corresponding nested array.
Note
This checks if the underlying data type is array.

◆ operator!=()

template<typename T >
bool tvm::relax::NestedMsg< T >::operator!= ( std::nullptr_t  ) const
inline

◆ operator=() [1/7]

template<typename T >
NestedMsg<T>& tvm::relax::NestedMsg< T >::operator= ( Array< NestedMsg< T >, void >  other)
inline

◆ operator=() [2/7]

template<typename T >
NestedMsg<T>& tvm::relax::NestedMsg< T >::operator= ( const NestedMsg< T > &  )
default

◆ operator=() [3/7]

template<typename T >
NestedMsg<T>& tvm::relax::NestedMsg< T >::operator= ( int  val)
delete

◆ operator=() [4/7]

template<typename T >
NestedMsg<T>& tvm::relax::NestedMsg< T >::operator= ( NestedMsg< T > &&  )
default

◆ operator=() [5/7]

template<typename T >
NestedMsg<T>& tvm::relax::NestedMsg< T >::operator= ( std::initializer_list< NestedMsg< T >>  other)
inline

◆ operator=() [6/7]

template<typename T >
NestedMsg<T>& tvm::relax::NestedMsg< T >::operator= ( std::nullptr_t  )
inline

◆ operator=() [7/7]

template<typename T >
NestedMsg<T>& tvm::relax::NestedMsg< T >::operator= ( other)
inline

◆ operator==()

template<typename T >
bool tvm::relax::NestedMsg< T >::operator== ( std::nullptr_t  ) const
inline

Friends And Related Function Documentation

◆ ffi::TypeTraits

template<typename T >
template<typename , typename >
friend struct ffi::TypeTraits
friend

The documentation for this class was generated from the following file: