tvm
Public Types | Public Member Functions | Static Public Attributes | 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>

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

Public Types

using ContainerType = Object
 
using LeafContainerType = typename T::ContainerType
 
- Public Types inherited from tvm::runtime::ObjectRef
using ContainerType = Object
 type indicate the container type. More...
 

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 (ObjectPtr< Object > ptr)
 Construct from an ObjectPtr whose type already satisfies the constraint. More...
 
 NestedMsg (runtime::NullOptType)
 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
 
- Public Member Functions inherited from tvm::runtime::ObjectRef
 ObjectRef ()=default
 default constructor More...
 
 ObjectRef (ObjectPtr< Object > data)
 Constructor from existing object ptr. More...
 
bool same_as (const ObjectRef &other) const
 Comparator. More...
 
bool operator== (const ObjectRef &other) const
 Comparator. More...
 
bool operator!= (const ObjectRef &other) const
 Comparator. More...
 
bool operator< (const ObjectRef &other) const
 Comparator. More...
 
bool defined () const
 
const Objectget () const
 
const Objectoperator-> () const
 
bool unique () const
 
int use_count () const
 
template<typename ObjectType , typename = std::enable_if_t<std::is_base_of_v<Object, ObjectType>>>
const ObjectType * as () const
 Try to downcast the internal Object to a raw pointer of a corresponding type. More...
 
template<typename ObjectRefType , typename = std::enable_if_t<std::is_base_of_v<ObjectRef, ObjectRefType>>>
Optional< ObjectRefType > as () const
 Try to downcast the ObjectRef to a Optional<T> of the requested type. More...
 

Static Public Attributes

static constexpr bool _type_is_nullable = true
 
- Static Public Attributes inherited from tvm::runtime::ObjectRef
static constexpr bool _type_is_nullable = true
 

Additional Inherited Members

- Protected Member Functions inherited from tvm::runtime::ObjectRef
Objectget_mutable () const
 
- Static Protected Member Functions inherited from tvm::runtime::ObjectRef
template<typename T >
static T DowncastNoCheck (ObjectRef ref)
 Internal helper function downcast a ref without check. More...
 
static void FFIClearAfterMove (ObjectRef *ref)
 Clear the object ref data field without DecRef after we successfully moved the field. More...
 
template<typename ObjectType >
static ObjectPtr< ObjectType > GetDataPtr (const ObjectRef &ref)
 Internal helper function get data_ as ObjectPtr of ObjectType. More...
 
- Protected Attributes inherited from tvm::runtime::ObjectRef
ObjectPtr< Objectdata_
 Internal pointer that backs the reference. More...
 

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, 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:706
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.

Member Typedef Documentation

◆ ContainerType

template<typename T >
using tvm::relax::NestedMsg< T >::ContainerType = Object

◆ LeafContainerType

template<typename T >
using tvm::relax::NestedMsg< T >::LeafContainerType = typename T::ContainerType

Constructor & Destructor Documentation

◆ NestedMsg() [1/10]

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

◆ NestedMsg() [2/10]

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

◆ NestedMsg() [3/10]

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

◆ NestedMsg() [4/10]

template<typename T >
tvm::relax::NestedMsg< T >::NestedMsg ( ObjectPtr< Object ptr)
inlineexplicit

Construct from an ObjectPtr whose type already satisfies the constraint.

Parameters
ptr

◆ NestedMsg() [5/10]

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

Nullopt handling.

◆ NestedMsg() [6/10]

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

◆ NestedMsg() [7/10]

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

◆ NestedMsg() [8/10]

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

◆ NestedMsg() [9/10]

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

◆ NestedMsg() [10/10]

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

Member Data Documentation

◆ _type_is_nullable

template<typename T >
constexpr bool tvm::relax::NestedMsg< T >::_type_is_nullable = true
staticconstexpr

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