tvm
Classes | Namespaces | Macros | Functions
object.h File Reference

A managed object in the TVM runtime. More...

#include <tvm/runtime/c_runtime_api.h>
#include <tvm/runtime/logging.h>
#include <string>
#include <type_traits>
#include <utility>
#include <atomic>
Include dependency graph for object.h:
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

struct  tvm::runtime::TypeIndex
 Namespace for the list of type index. More...
 
class  tvm::runtime::Object
 base class of all object containers. More...
 
class  tvm::runtime::ObjectPtr< T >
 A custom smart pointer for Object. More...
 
class  tvm::runtime::ObjectRef
 Base class of all object reference. More...
 
struct  tvm::runtime::ObjectPtrHash
 ObjectRef hash functor. More...
 
struct  tvm::runtime::ObjectPtrEqual
 ObjectRef equal functor. More...
 

Namespaces

 tvm
 runtime implementation for LibTorch/TorchScript.
 
 tvm::runtime
 

Macros

#define TVM_OBJECT_ATOMIC_REF_COUNTER   1
 Whether or not use atomic reference counter. If the reference counter is not atomic, an object cannot be owned by multiple threads. We can, however, move an object across threads. More...
 
#define TVM_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType)
 helper macro to declare a base object type that can be inherited. More...
 
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
 helper macro to declare type information in a final class. More...
 
#define TVM_ATTRIBUTE_UNUSED
 helper macro to suppress unused warning More...
 
#define TVM_STR_CONCAT_(__x, __y)   __x##__y
 
#define TVM_STR_CONCAT(__x, __y)   TVM_STR_CONCAT_(__x, __y)
 
#define TVM_OBJECT_REG_VAR_DEF   static TVM_ATTRIBUTE_UNUSED uint32_t __make_Object_tid
 
#define TVM_REGISTER_OBJECT_TYPE(TypeName)    TVM_STR_CONCAT(TVM_OBJECT_REG_VAR_DEF, __COUNTER__) = TypeName::_GetOrAllocRuntimeTypeIndex()
 Helper macro to register the object type to runtime. Makes sure that the runtime type table is correctly populated. More...
 
#define TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName)
 
#define TVM_DEFINE_OBJECT_REF_METHODS_WITHOUT_DEFAULT_CONSTRUCTOR(TypeName, ParentType, ObjectName)
 
#define TVM_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
 
#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
 
#define TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
 
#define TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
 
#define TVM_DEFINE_OBJECT_REF_COW_METHOD(ObjectName)
 Define CopyOnWrite function in an ObjectRef. More...
 

Functions

template<typename RelayRefType , typename ObjectType >
RelayRefType tvm::runtime::GetRef (const ObjectType *ptr)
 Get a reference type from a raw object ptr type. More...
 
template<typename SubRef , typename BaseRef >
SubRef tvm::runtime::Downcast (BaseRef ref)
 Downcast a base reference type to a more specific type. More...
 
template<typename BaseType , typename ObjectType >
ObjectPtr< BaseType > tvm::runtime::GetObjectPtr (ObjectType *ptr)
 Get an object ptr type from a raw object ptr. More...
 
template<typename RefType , typename ObjType >
RefType tvm::runtime::GetRef (const ObjType *ptr)
 
template<typename BaseType , typename ObjType >
ObjectPtr< BaseType > tvm::runtime::GetObjectPtr (ObjType *ptr)
 

Detailed Description

A managed object in the TVM runtime.

Macro Definition Documentation

◆ TVM_ATTRIBUTE_UNUSED

#define TVM_ATTRIBUTE_UNUSED

helper macro to suppress unused warning

◆ TVM_DECLARE_BASE_OBJECT_INFO

#define TVM_DECLARE_BASE_OBJECT_INFO (   TypeName,
  ParentType 
)
Value:
static_assert(!ParentType::_type_final, "ParentObj marked as final"); \
static uint32_t RuntimeTypeIndex() { \
static_assert(TypeName::_type_child_slots == 0 || ParentType::_type_child_slots == 0 || \
TypeName::_type_child_slots < ParentType::_type_child_slots, \
"Need to set _type_child_slots when parent specifies it."); \
if (TypeName::_type_index != ::tvm::runtime::TypeIndex::kDynamic) { \
return TypeName::_type_index; \
} \
return _GetOrAllocRuntimeTypeIndex(); \
} \
static uint32_t _GetOrAllocRuntimeTypeIndex() { \
static uint32_t tindex = Object::GetOrAllocRuntimeTypeIndex( \
TypeName::_type_key, TypeName::_type_index, ParentType::_GetOrAllocRuntimeTypeIndex(), \
TypeName::_type_child_slots, TypeName::_type_child_slots_can_overflow); \
return tindex; \
}
@ kDynamic
Type index is allocated during runtime.
Definition: object.h:84

helper macro to declare a base object type that can be inherited.

Parameters
TypeNameThe name of the current type.
ParentTypeThe name of the ParentType

◆ TVM_DECLARE_FINAL_OBJECT_INFO

#define TVM_DECLARE_FINAL_OBJECT_INFO (   TypeName,
  ParentType 
)
Value:
static const constexpr bool _type_final = true; \
static const constexpr int _type_child_slots = 0; \
TVM_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType)

helper macro to declare type information in a final class.

Parameters
TypeNameThe name of the current type.
ParentTypeThe name of the ParentType

◆ TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN

#define TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN (   TypeName)
Value:
TypeName(const TypeName& other) = default; \
TypeName(TypeName&& other) = default; \
TypeName& operator=(const TypeName& other) = default; \
TypeName& operator=(TypeName&& other) = default;

◆ TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS

#define TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS (   TypeName,
  ParentType,
  ObjectName 
)
Value:
explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \
TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \
ObjectName* operator->() const { return static_cast<ObjectName*>(data_.get()); } \
ObjectName* get() const { return operator->(); } \
static constexpr bool _type_is_nullable = false; \
using ContainerType = ObjectName;
A custom smart pointer for Object.
Definition: object.h:362

◆ TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS

#define TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS (   TypeName,
  ParentType,
  ObjectName 
)
Value:
TypeName() = default; \
TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \
explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \
ObjectName* operator->() const { return static_cast<ObjectName*>(data_.get()); } \
using ContainerType = ObjectName;

◆ TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS

#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS (   TypeName,
  ParentType,
  ObjectName 
)
Value:
explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \
TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \
const ObjectName* operator->() const { return static_cast<const ObjectName*>(data_.get()); } \
const ObjectName* get() const { return operator->(); } \
static constexpr bool _type_is_nullable = false; \
using ContainerType = ObjectName;

◆ TVM_DEFINE_OBJECT_REF_COW_METHOD

#define TVM_DEFINE_OBJECT_REF_COW_METHOD (   ObjectName)
Value:
ObjectName* CopyOnWrite() { \
ICHECK(data_ != nullptr); \
if (!data_.unique()) { \
auto n = make_object<ObjectName>(*(operator->())); \
ObjectPtr<Object>(std::move(n)).swap(data_); \
} \
return static_cast<ObjectName*>(data_.get()); \
}

Define CopyOnWrite function in an ObjectRef.

Parameters
ObjectNameThe Type of the Node.

CopyOnWrite will generate a unique copy of the internal node. The node will be copied if it is referenced by multiple places. The function returns the raw pointer to the node to allow modification of the content.

MyCOWObjectRef ref, ref2;
ref2 = ref;
ref.CopyOnWrite()->value = new_value;
assert(ref2->value == old_value);
assert(ref->value == new_value);

◆ TVM_DEFINE_OBJECT_REF_METHODS

#define TVM_DEFINE_OBJECT_REF_METHODS (   TypeName,
  ParentType,
  ObjectName 
)
Value:
TypeName() = default; \
TVM_DEFINE_OBJECT_REF_METHODS_WITHOUT_DEFAULT_CONSTRUCTOR(TypeName, ParentType, ObjectName)

◆ TVM_DEFINE_OBJECT_REF_METHODS_WITHOUT_DEFAULT_CONSTRUCTOR

#define TVM_DEFINE_OBJECT_REF_METHODS_WITHOUT_DEFAULT_CONSTRUCTOR (   TypeName,
  ParentType,
  ObjectName 
)
Value:
explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \
TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \
const ObjectName* operator->() const { return static_cast<const ObjectName*>(data_.get()); } \
const ObjectName* get() const { return operator->(); } \
using ContainerType = ObjectName;

◆ TVM_OBJECT_ATOMIC_REF_COUNTER

#define TVM_OBJECT_ATOMIC_REF_COUNTER   1

Whether or not use atomic reference counter. If the reference counter is not atomic, an object cannot be owned by multiple threads. We can, however, move an object across threads.

◆ TVM_OBJECT_REG_VAR_DEF

#define TVM_OBJECT_REG_VAR_DEF   static TVM_ATTRIBUTE_UNUSED uint32_t __make_Object_tid

◆ TVM_REGISTER_OBJECT_TYPE

#define TVM_REGISTER_OBJECT_TYPE (   TypeName)     TVM_STR_CONCAT(TVM_OBJECT_REG_VAR_DEF, __COUNTER__) = TypeName::_GetOrAllocRuntimeTypeIndex()

Helper macro to register the object type to runtime. Makes sure that the runtime type table is correctly populated.

Use this macro in the cc file for each terminal class.

◆ TVM_STR_CONCAT

#define TVM_STR_CONCAT (   __x,
  __y 
)    TVM_STR_CONCAT_(__x, __y)

◆ TVM_STR_CONCAT_

#define TVM_STR_CONCAT_ (   __x,
  __y 
)    __x##__y