23 #ifndef TVM_RUNTIME_OBJECT_H_ 24 #define TVM_RUNTIME_OBJECT_H_ 27 #include <tvm/runtime/logging.h> 30 #include <type_traits> 39 #ifndef TVM_OBJECT_ATOMIC_REF_COUNTER 40 #define TVM_OBJECT_ATOMIC_REF_COUNTER 1 43 #if TVM_OBJECT_ATOMIC_REF_COUNTER 45 #endif // TVM_OBJECT_ATOMIC_REF_COUNTER 180 std::string
GetTypeKey()
const {
return TypeIndex2Key(type_index_); }
190 template <
typename TargetType>
191 inline bool IsInstance()
const;
196 inline bool unique()
const;
202 static std::string TypeIndex2Key(uint32_t tindex);
208 static size_t TypeIndex2KeyHash(uint32_t tindex);
214 static uint32_t TypeKey2Index(
const std::string& key);
216 #if TVM_OBJECT_ATOMIC_REF_COUNTER 222 static constexpr
const char* _type_key =
"runtime.Object";
228 static constexpr
bool _type_final =
false;
229 static constexpr uint32_t _type_child_slots = 0;
230 static constexpr
bool _type_child_slots_can_overflow =
true;
232 static constexpr
bool _type_has_method_visit_attrs =
true;
233 static constexpr
bool _type_has_method_sequal_reduce =
false;
234 static constexpr
bool _type_has_method_shash_reduce =
false;
261 uint32_t type_index_{0};
269 FDeleter deleter_ =
nullptr;
273 "RefCounter ABI check.");
292 static uint32_t GetOrAllocRuntimeTypeIndex(
const std::string& key, uint32_t static_tindex,
293 uint32_t parent_tindex, uint32_t type_child_slots,
294 bool type_child_slots_can_overflow);
298 inline void IncRef();
303 inline void DecRef();
310 inline int use_count()
const;
316 bool DerivedFrom(uint32_t parent_tindex)
const;
323 friend class ObjectInternal;
338 template <
typename RelayRefType,
typename ObjectType>
349 template <
typename SubRef,
typename BaseRef>
350 inline SubRef
Downcast(BaseRef ref);
357 template <
typename T>
374 template <
typename U>
377 static_assert(std::is_base_of<T, U>::value,
378 "can only assign of child class ObjectPtr to parent");
385 : data_(other.data_) {
386 other.data_ =
nullptr;
392 template <
typename Y>
394 : data_(other.data_) {
395 static_assert(std::is_base_of<T, Y>::value,
396 "can only assign of child class ObjectPtr to parent");
397 other.data_ =
nullptr;
406 std::swap(data_, other.data_);
411 T*
get()
const {
return static_cast<T*
>(data_); }
447 explicit operator bool()
const {
return get() !=
nullptr; }
450 if (data_ !=
nullptr) {
456 int use_count()
const {
return data_ !=
nullptr ? data_->use_count() : 0; }
458 bool unique()
const {
return data_ !=
nullptr && data_->use_count() == 1; }
464 bool operator==(std::nullptr_t null)
const {
return data_ ==
nullptr; }
466 bool operator!=(std::nullptr_t null)
const {
return data_ !=
nullptr; }
476 if (data !=
nullptr) {
504 template <
typename RelayRefType,
typename ObjType>
506 template <
typename BaseType,
typename ObjType>
544 bool defined()
const {
return data_ !=
nullptr; }
546 const Object*
get()
const {
return data_.get(); }
550 bool unique()
const {
return data_.unique(); }
564 template <
typename ObjectType>
565 inline const ObjectType* as()
const;
570 static constexpr
bool _type_is_nullable =
true;
583 template <
typename T>
585 return T(std::move(ref.
data_));
599 template <
typename ObjectType>
607 friend class ObjectInternal;
608 template <
typename SubRef,
typename BaseRef>
609 friend SubRef
Downcast(BaseRef ref);
620 template <
typename BaseType,
typename ObjectType>
627 template <
typename T>
629 return std::hash<Object*>()(a.
get());
637 template <
typename T>
648 #define TVM_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType) \ 649 static_assert(!ParentType::_type_final, "ParentObj marked as final"); \ 650 static uint32_t RuntimeTypeIndex() { \ 651 static_assert(TypeName::_type_child_slots == 0 || ParentType::_type_child_slots == 0 || \ 652 TypeName::_type_child_slots < ParentType::_type_child_slots, \ 653 "Need to set _type_child_slots when parent specifies it."); \ 654 if (TypeName::_type_index != ::tvm::runtime::TypeIndex::kDynamic) { \ 655 return TypeName::_type_index; \ 657 return _GetOrAllocRuntimeTypeIndex(); \ 659 static uint32_t _GetOrAllocRuntimeTypeIndex() { \ 660 static uint32_t tindex = Object::GetOrAllocRuntimeTypeIndex( \ 661 TypeName::_type_key, TypeName::_type_index, ParentType::_GetOrAllocRuntimeTypeIndex(), \ 662 TypeName::_type_child_slots, TypeName::_type_child_slots_can_overflow); \ 671 #define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType) \ 672 static const constexpr bool _type_final = true; \ 673 static const constexpr int _type_child_slots = 0; \ 674 TVM_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType) 677 #if defined(__GNUC__) 678 #define TVM_ATTRIBUTE_UNUSED __attribute__((unused)) 680 #define TVM_ATTRIBUTE_UNUSED 683 #define TVM_STR_CONCAT_(__x, __y) __x##__y 684 #define TVM_STR_CONCAT(__x, __y) TVM_STR_CONCAT_(__x, __y) 686 #define TVM_OBJECT_REG_VAR_DEF static TVM_ATTRIBUTE_UNUSED uint32_t __make_Object_tid 694 #define TVM_REGISTER_OBJECT_TYPE(TypeName) \ 695 TVM_STR_CONCAT(TVM_OBJECT_REG_VAR_DEF, __COUNTER__) = TypeName::_GetOrAllocRuntimeTypeIndex() 701 #define TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName) \ 702 TypeName(const TypeName& other) = default; \ 703 TypeName(TypeName&& other) = default; \ 704 TypeName& operator=(const TypeName& other) = default; \ 705 TypeName& operator=(TypeName&& other) = default; 713 #define TVM_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \ 714 TypeName() = default; \ 715 explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \ 716 TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \ 717 const ObjectName* operator->() const { return static_cast<const ObjectName*>(data_.get()); } \ 718 const ObjectName* get() const { return operator->(); } \ 719 using ContainerType = ObjectName; 728 #define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \ 729 explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \ 730 TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \ 731 const ObjectName* operator->() const { return static_cast<const ObjectName*>(data_.get()); } \ 732 const ObjectName* get() const { return operator->(); } \ 733 static constexpr bool _type_is_nullable = false; \ 734 using ContainerType = ObjectName; 744 #define TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \ 745 TypeName() = default; \ 746 TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \ 747 explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \ 748 ObjectName* operator->() const { return static_cast<ObjectName*>(data_.get()); } \ 749 using ContainerType = ObjectName; 758 #define TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \ 759 explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \ 760 TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \ 761 ObjectName* operator->() const { return static_cast<ObjectName*>(data_.get()); } \ 762 ObjectName* get() const { return operator->(); } \ 763 static constexpr bool _type_is_nullable = false; \ 764 using ContainerType = ObjectName; 785 #define TVM_DEFINE_OBJECT_REF_COW_METHOD(ObjectName) \ 786 ObjectName* CopyOnWrite() { \ 787 ICHECK(data_ != nullptr); \ 788 if (!data_.unique()) { \ 789 auto n = make_object<ObjectName>(*(operator->())); \ 790 ObjectPtr<Object>(std::move(n)).swap(data_); \ 792 return static_cast<ObjectName*>(data_.get()); \ 797 #if TVM_OBJECT_ATOMIC_REF_COUNTER 799 inline void Object::IncRef() { ref_counter_.fetch_add(1, std::memory_order_relaxed); }
802 if (ref_counter_.fetch_sub(1, std::memory_order_release) == 1) {
803 std::atomic_thread_fence(std::memory_order_acquire);
804 if (this->deleter_ !=
nullptr) {
805 (*this->deleter_)(
this);
810 inline int Object::use_count()
const {
return ref_counter_.load(std::memory_order_relaxed); }
817 if (--ref_counter_ == 0) {
818 if (this->deleter_ !=
nullptr) {
819 (*this->deleter_)(
this);
824 inline int Object::use_count()
const {
return ref_counter_; }
826 #endif // TVM_OBJECT_ATOMIC_REF_COUNTER 828 template <
typename TargetType>
830 const Object*
self =
this;
833 if (
self !=
nullptr) {
835 if (std::is_same<TargetType, Object>::value)
return true;
836 if (TargetType::_type_final) {
839 return self->
type_index_ == TargetType::RuntimeTypeIndex();
843 uint32_t begin = TargetType::RuntimeTypeIndex();
845 if (TargetType::_type_child_slots != 0) {
846 uint32_t end = begin + TargetType::_type_child_slots;
847 if (self->type_index_ >= begin && self->type_index_ < end)
return true;
849 if (self->type_index_ == begin)
return true;
851 if (!TargetType::_type_child_slots_can_overflow)
return false;
853 if (self->type_index_ < TargetType::RuntimeTypeIndex())
return false;
855 return self->DerivedFrom(TargetType::RuntimeTypeIndex());
864 template <
typename ObjectType>
866 if (data_ !=
nullptr && data_->IsInstance<ObjectType>()) {
867 return static_cast<ObjectType*
>(data_.get());
873 template <
typename RefType,
typename ObjType>
874 inline RefType
GetRef(
const ObjType* ptr) {
875 static_assert(std::is_base_of<typename RefType::ContainerType, ObjType>::value,
876 "Can only cast to the ref of same container type");
877 if (!RefType::_type_is_nullable) {
878 ICHECK(ptr !=
nullptr);
880 return RefType(
ObjectPtr<Object>(const_cast<Object*>(static_cast<const Object*>(ptr))));
883 template <
typename BaseType,
typename ObjType>
885 static_assert(std::is_base_of<BaseType, ObjType>::value,
886 "Can only cast to the ref of same container type");
890 template <
typename SubRef,
typename BaseRef>
893 ICHECK(ref->template IsInstance<typename SubRef::ContainerType>())
894 <<
"Downcast from " << ref->GetTypeKey() <<
" to " << SubRef::ContainerType::_type_key
897 ICHECK(SubRef::_type_is_nullable) <<
"Downcast from nullptr to not nullable reference of " 898 << SubRef::ContainerType::_type_key;
900 return SubRef(std::move(ref.data_));
906 #endif // TVM_RUNTIME_OBJECT_H_ runtime::Map.
Definition: object.h:70
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:799
Object(const Object &other)
Definition: object.h:247
A custom smart pointer for Object.
Definition: object.h:358
runtime::Module.
Definition: object.h:62
ObjectRef equal functor.
Definition: object.h:634
Internal base class to handle conversion to POD values.
Definition: packed_func.h:541
SubRef Downcast(BaseRef ref)
Downcast a base reference type to a more specific type.
Definition: object.h:891
bool operator!=(std::nullptr_t null) const
Definition: object.h:466
void reset()
reset the content of ptr to be nullptr
Definition: object.h:449
size_t operator()(const ObjectRef &a) const
Definition: object.h:625
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
runtime::String.
Definition: object.h:66
Object()
Definition: object.h:241
bool operator==(const ObjectRef &other) const
Comparator.
Definition: object.h:528
void IncRef()
developer function, increases reference counter.
Definition: object.h:799
size_t GetTypeKeyHash() const
Definition: object.h:184
bool operator!=(const ObjectRef &other) const
Comparator.
Definition: object.h:534
static ObjectPtr< ObjectType > GetDataPtr(const ObjectRef &ref)
Internal helper function get data_ as ObjectPtr of ObjectType.
Definition: object.h:600
base class of all object containers.
Definition: object.h:167
RelayRefType GetRef(const ObjectType *ptr)
Get a reference type from a raw object ptr type.
static void FFIClearAfterMove(ObjectRef *ref)
Clear the object ref data field without DecRef after we successfully moved the field.
Definition: object.h:592
Namespace for the list of type index.
Definition: object.h:55
void DecRef()
developer function, decrease reference counter.
Definition: object.h:801
bool IsInstance() const
Definition: object.h:829
static uint32_t RuntimeTypeIndex()
Definition: object.h:225
bool operator<(const ObjectRef &other) const
Comparator.
Definition: object.h:540
Internal auxiliary struct for TypedPackedFunc to indicate a movable argument.
Definition: packed_func.h:709
ObjectPtr< BaseType > GetObjectPtr(ObjectType *ptr)
Get an object ptr type from a raw object ptr.
bool defined() const
Definition: object.h:544
Object(Object &&other)
Definition: object.h:249
bool operator()(const ObjectRef &a, const ObjectRef &b) const
Definition: object.h:635
ObjectRef hash functor.
Definition: object.h:624
runtime::PackedFunc.
Definition: object.h:74
std::atomic< int32_t > RefCounterType
Definition: object.h:217
bool unique() const
Definition: object.h:862
T & operator*() const
Definition: object.h:419
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:574
const Object * operator->() const
Definition: object.h:548
T * operator->() const
Definition: object.h:415
bool same_as(const ObjectRef &other) const
Comparator.
Definition: object.h:522
static T DowncastNoCheck(ObjectRef ref)
Internal helper function downcast a ref without check.
Definition: object.h:584
ObjectPtr< T > & operator=(ObjectPtr< T > &&other)
move assignment
Definition: object.h:438
bool unique() const
Definition: object.h:550
ObjectPtr(std::nullptr_t)
default constructor
Definition: object.h:363
Object & operator=(const Object &other)
Definition: object.h:251
runtime::ShapeTuple.
Definition: object.h:72
static uint32_t _GetOrAllocRuntimeTypeIndex()
Definition: object.h:224
bool operator==(std::nullptr_t null) const
Definition: object.h:464
ObjectRef(ObjectPtr< Object > data)
Constructor from existing object ptr.
Definition: object.h:516
Base class of all object reference.
Definition: object.h:511
Base class of object allocators that implements make. Use curiously recurring template pattern...
Definition: memory.h:60
std::string GetTypeKey() const
Definition: object.h:180
Root object type.
Definition: object.h:58
T * get() const
Definition: object.h:411
ObjectPtr(const ObjectPtr< T > &other)
copy constructor
Definition: object.h:368
size_t operator()(const ObjectPtr< T > &a) const
Definition: object.h:628
uint32_t type_index() const
Definition: object.h:175
bool unique() const
Definition: object.h:458
runtime::Array.
Definition: object.h:68
void swap(ObjectPtr< T > &other)
Swap this array with another Object.
Definition: object.h:405
size_t operator()(const ObjectPtr< T > &a, const ObjectPtr< T > &b) const
Definition: object.h:638
A single argument value to PackedFunc. Containing both type_code and TVMValue.
Definition: packed_func.h:646
int use_count() const
Definition: object.h:456
ObjectPtr()
default constructor
Definition: object.h:361
ObjectPtr(ObjectPtr< Y > &&other)
move constructor
Definition: object.h:393
~ObjectPtr()
destructor
Definition: object.h:400
Definition: packed_func.h:1517
uint32_t type_index_
Type index(tag) that indicates the type of the object.
Definition: object.h:261
int use_count() const
Definition: object.h:552
Object & operator=(Object &&other)
Definition: object.h:254
bool operator!=(const ObjectPtr< T > &other) const
Definition: object.h:462
const ObjectType * as() const
Try to downcast the internal Object to a raw pointer of a corresponding type.
Definition: object.h:865
bool operator==(const ObjectPtr< T > &other) const
Definition: object.h:460
Object * get_mutable() const
Definition: object.h:576
Managed reference to RelayRefTypeNode.
Definition: type.h:576
ObjectPtr(ObjectPtr< T > &&other)
move constructor
Definition: object.h:384
ObjectPtr(const ObjectPtr< U > &other)
copy constructor
Definition: object.h:375
Type index is allocated during runtime.
Definition: object.h:80
ObjectPtr< T > & operator=(const ObjectPtr< T > &other)
copy assignment
Definition: object.h:427
runtime::NDArray.
Definition: object.h:64