tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
object.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
23 #ifndef TVM_RUNTIME_OBJECT_H_
24 #define TVM_RUNTIME_OBJECT_H_
25 
27 #include <tvm/runtime/logging.h>
28 
29 #include <string>
30 #include <type_traits>
31 #include <utility>
32 
39 #ifndef TVM_OBJECT_ATOMIC_REF_COUNTER
40 #define TVM_OBJECT_ATOMIC_REF_COUNTER 1
41 #endif
42 
43 #if TVM_OBJECT_ATOMIC_REF_COUNTER
44 #include <atomic>
45 #endif // TVM_OBJECT_ATOMIC_REF_COUNTER
46 
47 namespace tvm {
48 namespace runtime {
49 
55 struct TypeIndex {
56  enum {
58  kRoot = 0,
59  // Standard static index assignments,
60  // Frontends can take benefit of these constants.
75  // static assignments that may subject to change.
81  };
82 }; // namespace TypeIndex
83 
167 class TVM_DLL Object {
168  public:
173  typedef void (*FDeleter)(Object* self);
175  uint32_t type_index() const { return type_index_; }
180  std::string GetTypeKey() const { return TypeIndex2Key(type_index_); }
184  size_t GetTypeKeyHash() const { return TypeIndex2KeyHash(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);
215 
216 #if TVM_OBJECT_ATOMIC_REF_COUNTER
217  using RefCounterType = std::atomic<int32_t>;
218 #else
219  using RefCounterType = int32_t;
220 #endif
221 
222  static constexpr const char* _type_key = "runtime.Object";
223 
224  static uint32_t _GetOrAllocRuntimeTypeIndex() { return TypeIndex::kRoot; }
225  static uint32_t RuntimeTypeIndex() { return TypeIndex::kRoot; }
226 
227  // Default object type properties for sub-classes
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;
231  // member information
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;
235  // NOTE: the following field is not type index of Object
236  // but was intended to be used by sub-classes as default value.
237  // The type index of Object is TypeIndex::kRoot
238  static constexpr uint32_t _type_index = TypeIndex::kDynamic;
239 
240  // Default constructor and copy constructor
241  Object() {}
242  // Override the copy and assign constructors to do nothing.
243  // This is to make sure only contents, but not deleter and ref_counter
244  // are copied when a child class copies itself.
245  // This will enable us to use make_object<ObjectClass>(*obj_ptr)
246  // to copy an existing object.
247  Object(const Object& other) { // NOLINT(*)
248  }
249  Object(Object&& other) { // NOLINT(*)
250  }
251  Object& operator=(const Object& other) { // NOLINT(*)
252  return *this;
253  }
254  Object& operator=(Object&& other) { // NOLINT(*)
255  return *this;
256  }
257 
258  protected:
259  // The fields of the base object cell.
261  uint32_t type_index_{0};
263  RefCounterType ref_counter_{0};
269  FDeleter deleter_ = nullptr;
270  // Invariant checks.
271  static_assert(sizeof(int32_t) == sizeof(RefCounterType) &&
272  alignof(int32_t) == sizeof(RefCounterType),
273  "RefCounter ABI check.");
274 
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);
295 
296  // reference counter related operations
298  inline void IncRef();
303  inline void DecRef();
304 
305  private:
310  inline int use_count() const;
316  bool DerivedFrom(uint32_t parent_tindex) const;
317  // friend classes
318  template <typename>
319  friend class ObjAllocatorBase;
320  template <typename>
321  friend class ObjectPtr;
322  friend class TVMRetValue;
323  friend class ObjectInternal;
324 };
325 
338 template <typename RelayRefType, typename ObjectType>
339 inline RelayRefType GetRef(const ObjectType* ptr);
340 
349 template <typename SubRef, typename BaseRef>
350 inline SubRef Downcast(BaseRef ref);
351 
357 template <typename T>
358 class ObjectPtr {
359  public:
363  ObjectPtr(std::nullptr_t) {} // NOLINT(*)
368  ObjectPtr(const ObjectPtr<T>& other) // NOLINT(*)
369  : ObjectPtr(other.data_) {}
374  template <typename U>
375  ObjectPtr(const ObjectPtr<U>& other) // NOLINT(*)
376  : ObjectPtr(other.data_) {
377  static_assert(std::is_base_of<T, U>::value,
378  "can only assign of child class ObjectPtr to parent");
379  }
384  ObjectPtr(ObjectPtr<T>&& other) // NOLINT(*)
385  : data_(other.data_) {
386  other.data_ = nullptr;
387  }
392  template <typename Y>
393  ObjectPtr(ObjectPtr<Y>&& other) // NOLINT(*)
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;
398  }
400  ~ObjectPtr() { this->reset(); }
405  void swap(ObjectPtr<T>& other) { // NOLINT(*)
406  std::swap(data_, other.data_);
407  }
411  T* get() const { return static_cast<T*>(data_); }
415  T* operator->() const { return get(); }
419  T& operator*() const { // NOLINT(*)
420  return *get();
421  }
427  ObjectPtr<T>& operator=(const ObjectPtr<T>& other) { // NOLINT(*)
428  // takes in plane operator to enable copy elison.
429  // copy-and-swap idiom
430  ObjectPtr(other).swap(*this); // NOLINT(*)
431  return *this;
432  }
438  ObjectPtr<T>& operator=(ObjectPtr<T>&& other) { // NOLINT(*)
439  // copy-and-swap idiom
440  ObjectPtr(std::move(other)).swap(*this); // NOLINT(*)
441  return *this;
442  }
447  explicit operator bool() const { return get() != nullptr; }
449  void reset() {
450  if (data_ != nullptr) {
451  data_->DecRef();
452  data_ = nullptr;
453  }
454  }
456  int use_count() const { return data_ != nullptr ? data_->use_count() : 0; }
458  bool unique() const { return data_ != nullptr && data_->use_count() == 1; }
460  bool operator==(const ObjectPtr<T>& other) const { return data_ == other.data_; }
462  bool operator!=(const ObjectPtr<T>& other) const { return data_ != other.data_; }
464  bool operator==(std::nullptr_t null) const { return data_ == nullptr; }
466  bool operator!=(std::nullptr_t null) const { return data_ != nullptr; }
467 
468  private:
470  Object* data_{nullptr};
475  explicit ObjectPtr(Object* data) : data_(data) {
476  if (data != nullptr) {
477  data_->IncRef();
478  }
479  }
485  static ObjectPtr<T> MoveFromRValueRefArg(Object** ref) {
486  ObjectPtr<T> ptr;
487  ptr.data_ = *ref;
488  *ref = nullptr;
489  return ptr;
490  }
491  // friend classes
492  friend class Object;
493  friend class ObjectRef;
494  friend struct ObjectPtrHash;
495  template <typename>
496  friend class ObjectPtr;
497  template <typename>
498  friend class ObjAllocatorBase;
499  friend class TVMPODValue_;
500  friend class TVMArgsSetter;
501  friend class TVMRetValue;
502  friend class TVMArgValue;
503  friend class TVMMovableArgValue_;
504  template <typename RelayRefType, typename ObjType>
505  friend RelayRefType GetRef(const ObjType* ptr);
506  template <typename BaseType, typename ObjType>
507  friend ObjectPtr<BaseType> GetObjectPtr(ObjType* ptr);
508 };
509 
510 // Forward declaration, to prevent circular includes.
511 template <typename T>
512 class Optional;
513 
515 class ObjectRef {
516  public:
518  ObjectRef() = default;
520  explicit ObjectRef(ObjectPtr<Object> data) : data_(data) {}
526  bool same_as(const ObjectRef& other) const { return data_ == other.data_; }
532  bool operator==(const ObjectRef& other) const { return data_ == other.data_; }
538  bool operator!=(const ObjectRef& other) const { return data_ != other.data_; }
544  bool operator<(const ObjectRef& other) const { return data_.get() < other.data_.get(); }
548  bool defined() const { return data_ != nullptr; }
550  const Object* get() const { return data_.get(); }
552  const Object* operator->() const { return get(); }
554  bool unique() const { return data_.unique(); }
556  int use_count() const { return data_.use_count(); }
557 
570  template <typename ObjectType, typename = std::enable_if_t<std::is_base_of_v<Object, ObjectType>>>
571  inline const ObjectType* as() const;
572 
590  template <typename ObjectRefType,
591  typename = std::enable_if_t<std::is_base_of_v<ObjectRef, ObjectRefType>>>
592  inline Optional<ObjectRefType> as() const;
593 
596  // Default type properties for the reference class.
597  static constexpr bool _type_is_nullable = true;
598 
599  protected:
603  Object* get_mutable() const { return data_.get(); }
610  template <typename T>
611  static T DowncastNoCheck(ObjectRef ref) {
612  return T(std::move(ref.data_));
613  }
619  static void FFIClearAfterMove(ObjectRef* ref) { ref->data_.data_ = nullptr; }
626  template <typename ObjectType>
628  return ObjectPtr<ObjectType>(ref.data_.data_);
629  }
630  // friend classes.
631  friend struct ObjectPtrHash;
632  friend class TVMRetValue;
633  friend class TVMArgsSetter;
634  friend class ObjectInternal;
635  template <typename SubRef, typename BaseRef>
636  friend SubRef Downcast(BaseRef ref);
637 };
638 
647 template <typename BaseType, typename ObjectType>
648 inline ObjectPtr<BaseType> GetObjectPtr(ObjectType* ptr);
649 
652  size_t operator()(const ObjectRef& a) const { return operator()(a.data_); }
653 
654  template <typename T>
655  size_t operator()(const ObjectPtr<T>& a) const {
656  return std::hash<Object*>()(a.get());
657  }
658 };
659 
662  bool operator()(const ObjectRef& a, const ObjectRef& b) const { return a.same_as(b); }
663 
664  template <typename T>
665  size_t operator()(const ObjectPtr<T>& a, const ObjectPtr<T>& b) const {
666  return a == b;
667  }
668 };
669 
675 #define TVM_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType) \
676  static_assert(!ParentType::_type_final, "ParentObj marked as final"); \
677  static uint32_t RuntimeTypeIndex() { \
678  static_assert(TypeName::_type_child_slots == 0 || ParentType::_type_child_slots == 0 || \
679  TypeName::_type_child_slots < ParentType::_type_child_slots, \
680  "Need to set _type_child_slots when parent specifies it."); \
681  if (TypeName::_type_index != ::tvm::runtime::TypeIndex::kDynamic) { \
682  return TypeName::_type_index; \
683  } \
684  return _GetOrAllocRuntimeTypeIndex(); \
685  } \
686  static uint32_t _GetOrAllocRuntimeTypeIndex() { \
687  static uint32_t tindex = Object::GetOrAllocRuntimeTypeIndex( \
688  TypeName::_type_key, TypeName::_type_index, ParentType::_GetOrAllocRuntimeTypeIndex(), \
689  TypeName::_type_child_slots, TypeName::_type_child_slots_can_overflow); \
690  return tindex; \
691  }
692 
698 #define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType) \
699  static const constexpr bool _type_final = true; \
700  static const constexpr int _type_child_slots = 0; \
701  TVM_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType)
702 
704 #if defined(__GNUC__)
705 #define TVM_ATTRIBUTE_UNUSED __attribute__((unused))
706 #else
707 #define TVM_ATTRIBUTE_UNUSED
708 #endif
709 
710 #define TVM_STR_CONCAT_(__x, __y) __x##__y
711 #define TVM_STR_CONCAT(__x, __y) TVM_STR_CONCAT_(__x, __y)
712 
713 #define TVM_OBJECT_REG_VAR_DEF static TVM_ATTRIBUTE_UNUSED uint32_t __make_Object_tid
714 
721 #define TVM_REGISTER_OBJECT_TYPE(TypeName) \
722  TVM_STR_CONCAT(TVM_OBJECT_REG_VAR_DEF, __COUNTER__) = TypeName::_GetOrAllocRuntimeTypeIndex()
723 
724 /*
725  * \brief Define the default copy/move constructor and assign operator
726  * \param TypeName The class typename.
727  */
728 #define TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName) \
729  TypeName(const TypeName& other) = default; \
730  TypeName(TypeName&& other) = default; \
731  TypeName& operator=(const TypeName& other) = default; \
732  TypeName& operator=(TypeName&& other) = default;
733 
734 /*
735  * \brief Define object reference methods.
736  * \param TypeName The object type name
737  * \param ParentType The parent type of the objectref
738  * \param ObjectName The type name of the object.
739  */
740 #define TVM_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \
741  TypeName() = default; \
742  explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \
743  TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \
744  const ObjectName* operator->() const { return static_cast<const ObjectName*>(data_.get()); } \
745  const ObjectName* get() const { return operator->(); } \
746  using ContainerType = ObjectName;
747 
748 /*
749  * \brief Define object reference methods that is not nullable.
750  *
751  * \param TypeName The object type name
752  * \param ParentType The parent type of the objectref
753  * \param ObjectName The type name of the object.
754  */
755 #define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \
756  explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \
757  TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \
758  const ObjectName* operator->() const { return static_cast<const ObjectName*>(data_.get()); } \
759  const ObjectName* get() const { return operator->(); } \
760  static constexpr bool _type_is_nullable = false; \
761  using ContainerType = ObjectName;
762 
763 /*
764  * \brief Define object reference methods of whose content is mutable.
765  * \param TypeName The object type name
766  * \param ParentType The parent type of the objectref
767  * \param ObjectName The type name of the object.
768  * \note We recommend making objects immutable when possible.
769  * This macro is only reserved for objects that stores runtime states.
770  */
771 #define TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \
772  TypeName() = default; \
773  TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \
774  explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \
775  ObjectName* operator->() const { return static_cast<ObjectName*>(data_.get()); } \
776  using ContainerType = ObjectName;
777 
778 /*
779  * \brief Define object reference methods that is both not nullable and mutable.
780  *
781  * \param TypeName The object type name
782  * \param ParentType The parent type of the objectref
783  * \param ObjectName The type name of the object.
784  */
785 #define TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \
786  explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \
787  TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \
788  ObjectName* operator->() const { return static_cast<ObjectName*>(data_.get()); } \
789  ObjectName* get() const { return operator->(); } \
790  static constexpr bool _type_is_nullable = false; \
791  using ContainerType = ObjectName;
792 
812 #define TVM_DEFINE_OBJECT_REF_COW_METHOD(ObjectName) \
813  ObjectName* CopyOnWrite() { \
814  ICHECK(data_ != nullptr); \
815  if (!data_.unique()) { \
816  auto n = make_object<ObjectName>(*(operator->())); \
817  ObjectPtr<Object>(std::move(n)).swap(data_); \
818  } \
819  return static_cast<ObjectName*>(data_.get()); \
820  }
821 
822 // Implementations details below
823 // Object reference counting.
824 #if TVM_OBJECT_ATOMIC_REF_COUNTER
825 
826 inline void Object::IncRef() { ref_counter_.fetch_add(1, std::memory_order_relaxed); }
827 
828 inline void Object::DecRef() {
829  if (ref_counter_.fetch_sub(1, std::memory_order_release) == 1) {
830  std::atomic_thread_fence(std::memory_order_acquire);
831  if (this->deleter_ != nullptr) {
832  (*this->deleter_)(this);
833  }
834  }
835 }
836 
837 inline int Object::use_count() const { return ref_counter_.load(std::memory_order_relaxed); }
838 
839 #else
840 
841 inline void Object::IncRef() { ++ref_counter_; }
842 
843 inline void Object::DecRef() {
844  if (--ref_counter_ == 0) {
845  if (this->deleter_ != nullptr) {
846  (*this->deleter_)(this);
847  }
848  }
849 }
850 
851 inline int Object::use_count() const { return ref_counter_; }
852 
853 #endif // TVM_OBJECT_ATOMIC_REF_COUNTER
854 
855 template <typename TargetType>
856 inline bool Object::IsInstance() const {
857  const Object* self = this;
858  // NOTE: the following code can be optimized by
859  // compiler dead-code elimination for already known constants.
860  if (self != nullptr) {
861  // Everything is a subclass of object.
862  if (std::is_same<TargetType, Object>::value) return true;
863  if (TargetType::_type_final) {
864  // if the target type is a final type
865  // then we only need to check the equivalence.
866  return self->type_index_ == TargetType::RuntimeTypeIndex();
867  } else {
868  // if target type is a non-leaf type
869  // Check if type index falls into the range of reserved slots.
870  uint32_t begin = TargetType::RuntimeTypeIndex();
871  // The condition will be optimized by constant-folding.
872  if (TargetType::_type_child_slots != 0) {
873  uint32_t end = begin + TargetType::_type_child_slots;
874  if (self->type_index_ >= begin && self->type_index_ < end) return true;
875  } else {
876  if (self->type_index_ == begin) return true;
877  }
878  if (!TargetType::_type_child_slots_can_overflow) return false;
879  // Invariance: parent index is always smaller than the child.
880  if (self->type_index_ < TargetType::RuntimeTypeIndex()) return false;
881  // The rare slower-path, check type hierarchy.
882  return self->DerivedFrom(TargetType::RuntimeTypeIndex());
883  }
884  } else {
885  return false;
886  }
887 }
888 
889 inline bool Object::unique() const { return use_count() == 1; }
890 
891 template <typename ObjectType, typename>
892 inline const ObjectType* ObjectRef::as() const {
893  if (data_ != nullptr && data_->IsInstance<ObjectType>()) {
894  return static_cast<ObjectType*>(data_.get());
895  } else {
896  return nullptr;
897  }
898 }
899 
900 template <typename RefType, typename ObjType>
901 inline RefType GetRef(const ObjType* ptr) {
902  static_assert(std::is_base_of<typename RefType::ContainerType, ObjType>::value,
903  "Can only cast to the ref of same container type");
904  if (!RefType::_type_is_nullable) {
905  ICHECK(ptr != nullptr);
906  }
907  return RefType(ObjectPtr<Object>(const_cast<Object*>(static_cast<const Object*>(ptr))));
908 }
909 
910 template <typename BaseType, typename ObjType>
911 inline ObjectPtr<BaseType> GetObjectPtr(ObjType* ptr) {
912  static_assert(std::is_base_of<BaseType, ObjType>::value,
913  "Can only cast to the ref of same container type");
914  return ObjectPtr<BaseType>(static_cast<Object*>(ptr));
915 }
916 
917 template <typename SubRef, typename BaseRef>
918 inline SubRef Downcast(BaseRef ref) {
919  if (ref.defined()) {
920  ICHECK(ref->template IsInstance<typename SubRef::ContainerType>())
921  << "Downcast from " << ref->GetTypeKey() << " to " << SubRef::ContainerType::_type_key
922  << " failed.";
923  } else {
924  ICHECK(SubRef::_type_is_nullable) << "Downcast from nullptr to not nullable reference of "
925  << SubRef::ContainerType::_type_key;
926  }
927  return SubRef(std::move(ref.data_));
928 }
929 
930 } // namespace runtime
931 } // namespace tvm
932 
933 #endif // TVM_RUNTIME_OBJECT_H_
Managed reference to RelayRefTypeNode.
Definition: type.h:576
Base class of object allocators that implements make. Use curiously recurring template pattern.
Definition: memory.h:60
A custom smart pointer for Object.
Definition: object.h:358
void swap(ObjectPtr< T > &other)
Swap this array with another Object.
Definition: object.h:405
T * get() const
Definition: object.h:411
friend class Object
Definition: object.h:492
bool operator!=(const ObjectPtr< T > &other) const
Definition: object.h:462
ObjectPtr()
default constructor
Definition: object.h:361
friend ObjectPtr< BaseType > GetObjectPtr(ObjType *ptr)
Definition: object.h:911
friend class ObjectPtr
Definition: object.h:496
T * operator->() const
Definition: object.h:415
friend RelayRefType GetRef(const ObjType *ptr)
Definition: object.h:901
ObjectPtr(std::nullptr_t)
default constructor
Definition: object.h:363
ObjectPtr< T > & operator=(ObjectPtr< T > &&other)
move assignment
Definition: object.h:438
ObjectPtr(const ObjectPtr< T > &other)
copy constructor
Definition: object.h:368
ObjectPtr(ObjectPtr< Y > &&other)
move constructor
Definition: object.h:393
int use_count() const
Definition: object.h:456
bool operator==(const ObjectPtr< T > &other) const
Definition: object.h:460
ObjectPtr(ObjectPtr< T > &&other)
move constructor
Definition: object.h:384
void reset()
reset the content of ptr to be nullptr
Definition: object.h:449
~ObjectPtr()
destructor
Definition: object.h:400
T & operator*() const
Definition: object.h:419
ObjectPtr(const ObjectPtr< U > &other)
copy constructor
Definition: object.h:375
bool operator==(std::nullptr_t null) const
Definition: object.h:464
bool operator!=(std::nullptr_t null) const
Definition: object.h:466
bool unique() const
Definition: object.h:458
ObjectPtr< T > & operator=(const ObjectPtr< T > &other)
copy assignment
Definition: object.h:427
Base class of all object reference.
Definition: object.h:515
int use_count() const
Definition: object.h:556
bool defined() const
Definition: object.h:548
static void FFIClearAfterMove(ObjectRef *ref)
Clear the object ref data field without DecRef after we successfully moved the field.
Definition: object.h:619
const Object * operator->() const
Definition: object.h:552
static constexpr bool _type_is_nullable
Definition: object.h:597
bool operator<(const ObjectRef &other) const
Comparator.
Definition: object.h:544
friend class ObjectInternal
Definition: object.h:634
bool unique() const
Definition: object.h:554
friend SubRef Downcast(BaseRef ref)
Downcast a base reference type to a more specific type.
Definition: object.h:918
ObjectRef(ObjectPtr< Object > data)
Constructor from existing object ptr.
Definition: object.h:520
ObjectRef()=default
default constructor
bool operator!=(const ObjectRef &other) const
Comparator.
Definition: object.h:538
const Object * get() const
Definition: object.h:550
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:601
static T DowncastNoCheck(ObjectRef ref)
Internal helper function downcast a ref without check.
Definition: object.h:611
const ObjectType * as() const
Try to downcast the internal Object to a raw pointer of a corresponding type.
Definition: object.h:892
bool same_as(const ObjectRef &other) const
Comparator.
Definition: object.h:526
Object * get_mutable() const
Definition: object.h:603
static ObjectPtr< ObjectType > GetDataPtr(const ObjectRef &ref)
Internal helper function get data_ as ObjectPtr of ObjectType.
Definition: object.h:627
bool operator==(const ObjectRef &other) const
Comparator.
Definition: object.h:532
base class of all object containers.
Definition: object.h:167
RefCounterType ref_counter_
The internal reference counter.
Definition: object.h:263
Object()
Definition: object.h:241
uint32_t type_index() const
Definition: object.h:175
uint32_t type_index_
Type index(tag) that indicates the type of the object.
Definition: object.h:261
std::string GetTypeKey() const
Definition: object.h:180
std::atomic< int32_t > RefCounterType
Definition: object.h:217
size_t GetTypeKeyHash() const
Definition: object.h:184
static uint32_t _GetOrAllocRuntimeTypeIndex()
Definition: object.h:224
static uint32_t TypeKey2Index(const std::string &key)
Get the type index of the corresponding key from runtime.
Object & operator=(const Object &other)
Definition: object.h:251
static size_t TypeIndex2KeyHash(uint32_t tindex)
Get the type key hash of the corresponding index from runtime.
void DecRef()
developer function, decrease reference counter.
Definition: object.h:828
static uint32_t GetOrAllocRuntimeTypeIndex(const std::string &key, uint32_t static_tindex, uint32_t parent_tindex, uint32_t type_child_slots, bool type_child_slots_can_overflow)
Get the type index using type key.
static std::string TypeIndex2Key(uint32_t tindex)
Get the type key of the corresponding index from runtime.
bool IsInstance() const
Definition: object.h:856
Object(Object &&other)
Definition: object.h:249
Object(const Object &other)
Definition: object.h:247
void IncRef()
developer function, increases reference counter.
Definition: object.h:826
static uint32_t RuntimeTypeIndex()
Definition: object.h:225
Object & operator=(Object &&other)
Definition: object.h:254
FDeleter deleter_
deleter of this object to enable customized allocation. If the deleter is nullptr,...
Definition: object.h:269
bool unique() const
Definition: object.h:889
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
A single argument value to PackedFunc. Containing both type_code and TVMValue.
Definition: packed_func.h:646
Definition: packed_func.h:1517
Internal auxiliary struct for TypedPackedFunc to indicate a movable argument.
Definition: packed_func.h:709
Internal base class to handle conversion to POD values.
Definition: packed_func.h:541
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:799
ObjectPtr< BaseType > GetObjectPtr(ObjectType *ptr)
Get an object ptr type from a raw object ptr.
SubRef Downcast(BaseRef ref)
Downcast a base reference type to a more specific type.
Definition: object.h:918
RelayRefType GetRef(const ObjectType *ptr)
Get a reference type from a raw object ptr type.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
ObjectRef equal functor.
Definition: object.h:661
size_t operator()(const ObjectPtr< T > &a, const ObjectPtr< T > &b) const
Definition: object.h:665
bool operator()(const ObjectRef &a, const ObjectRef &b) const
Definition: object.h:662
ObjectRef hash functor.
Definition: object.h:651
size_t operator()(const ObjectPtr< T > &a) const
Definition: object.h:655
size_t operator()(const ObjectRef &a) const
Definition: object.h:652
Namespace for the list of type index.
Definition: object.h:55
@ kRoot
Root object type.
Definition: object.h:58
@ kRuntimePackedFunc
runtime::PackedFunc.
Definition: object.h:74
@ kStaticIndexEnd
Definition: object.h:78
@ kRuntimeNDArray
runtime::NDArray.
Definition: object.h:64
@ kRuntimeMap
runtime::Map.
Definition: object.h:70
@ kRuntimeClosure
Definition: object.h:76
@ kRuntimeADT
Definition: object.h:77
@ kRuntimeString
runtime::String.
Definition: object.h:66
@ kDynamic
Type index is allocated during runtime.
Definition: object.h:80
@ kRuntimeArray
runtime::Array.
Definition: object.h:68
@ kRuntimeModule
runtime::Module.
Definition: object.h:62
@ kRuntimeShapeTuple
runtime::ShapeTuple.
Definition: object.h:72