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.
77  // static assignments that may subject to change.
83  };
84 }; // namespace TypeIndex
85 
169 class TVM_DLL Object {
170  public:
175  typedef void (*FDeleter)(Object* self);
177  uint32_t type_index() const { return type_index_; }
182  std::string GetTypeKey() const { return TypeIndex2Key(type_index_); }
186  size_t GetTypeKeyHash() const { return TypeIndex2KeyHash(type_index_); }
192  template <typename TargetType>
193  inline bool IsInstance() const;
198  inline bool unique() const;
204  static std::string TypeIndex2Key(uint32_t tindex);
210  static size_t TypeIndex2KeyHash(uint32_t tindex);
216  static uint32_t TypeKey2Index(const std::string& key);
217 
218 #if TVM_OBJECT_ATOMIC_REF_COUNTER
219  using RefCounterType = std::atomic<int32_t>;
220 #else
221  using RefCounterType = int32_t;
222 #endif
223 
224  static constexpr const char* _type_key = "runtime.Object";
225 
226  static uint32_t _GetOrAllocRuntimeTypeIndex() { return TypeIndex::kRoot; }
227  static uint32_t RuntimeTypeIndex() { return TypeIndex::kRoot; }
228 
229  // Default object type properties for sub-classes
230  static constexpr bool _type_final = false;
231  static constexpr uint32_t _type_child_slots = 0;
232  static constexpr bool _type_child_slots_can_overflow = true;
233  // member information
234  static constexpr bool _type_has_method_visit_attrs = true;
235  static constexpr bool _type_has_method_sequal_reduce = false;
236  static constexpr bool _type_has_method_shash_reduce = false;
237  // NOTE: the following field is not type index of Object
238  // but was intended to be used by sub-classes as default value.
239  // The type index of Object is TypeIndex::kRoot
240  static constexpr uint32_t _type_index = TypeIndex::kDynamic;
241 
242  // Default constructor and copy constructor
243  Object() {}
244  // Override the copy and assign constructors to do nothing.
245  // This is to make sure only contents, but not deleter and ref_counter
246  // are copied when a child class copies itself.
247  // This will enable us to use make_object<ObjectClass>(*obj_ptr)
248  // to copy an existing object.
249  Object(const Object& other) { // NOLINT(*)
250  }
251  Object(Object&& other) { // NOLINT(*)
252  }
253  Object& operator=(const Object& other) { // NOLINT(*)
254  return *this;
255  }
256  Object& operator=(Object&& other) { // NOLINT(*)
257  return *this;
258  }
259 
260  protected:
261  // The fields of the base object cell.
263  uint32_t type_index_{0};
265  RefCounterType ref_counter_{0};
271  FDeleter deleter_ = nullptr;
272  // Invariant checks.
273  static_assert(sizeof(int32_t) == sizeof(RefCounterType) &&
274  alignof(int32_t) == sizeof(RefCounterType),
275  "RefCounter ABI check.");
276 
294  static uint32_t GetOrAllocRuntimeTypeIndex(const std::string& key, uint32_t static_tindex,
295  uint32_t parent_tindex, uint32_t type_child_slots,
296  bool type_child_slots_can_overflow);
297 
298  // reference counter related operations
300  inline void IncRef();
305  inline void DecRef();
306 
307  private:
312  inline int use_count() const;
318  bool DerivedFrom(uint32_t parent_tindex) const;
319  // friend classes
320  template <typename>
321  friend class ObjAllocatorBase;
322  template <typename>
323  friend class ObjectPtr;
324  friend class TVMRetValue;
325  friend class ObjectInternal;
326 };
327 
340 template <typename RelayRefType, typename ObjectType>
341 inline RelayRefType GetRef(const ObjectType* ptr);
342 
351 template <typename SubRef, typename BaseRef>
352 inline SubRef Downcast(BaseRef ref);
353 
359 template <typename T>
360 class ObjectPtr {
361  public:
365  ObjectPtr(std::nullptr_t) {} // NOLINT(*)
370  ObjectPtr(const ObjectPtr<T>& other) // NOLINT(*)
371  : ObjectPtr(other.data_) {}
376  template <typename U>
377  ObjectPtr(const ObjectPtr<U>& other) // NOLINT(*)
378  : ObjectPtr(other.data_) {
379  static_assert(std::is_base_of<T, U>::value,
380  "can only assign of child class ObjectPtr to parent");
381  }
386  ObjectPtr(ObjectPtr<T>&& other) // NOLINT(*)
387  : data_(other.data_) {
388  other.data_ = nullptr;
389  }
394  template <typename Y>
395  ObjectPtr(ObjectPtr<Y>&& other) // NOLINT(*)
396  : data_(other.data_) {
397  static_assert(std::is_base_of<T, Y>::value,
398  "can only assign of child class ObjectPtr to parent");
399  other.data_ = nullptr;
400  }
402  ~ObjectPtr() { this->reset(); }
407  void swap(ObjectPtr<T>& other) { // NOLINT(*)
408  std::swap(data_, other.data_);
409  }
413  T* get() const { return static_cast<T*>(data_); }
417  T* operator->() const { return get(); }
421  T& operator*() const { // NOLINT(*)
422  return *get();
423  }
429  ObjectPtr<T>& operator=(const ObjectPtr<T>& other) { // NOLINT(*)
430  // takes in plane operator to enable copy elison.
431  // copy-and-swap idiom
432  ObjectPtr(other).swap(*this); // NOLINT(*)
433  return *this;
434  }
440  ObjectPtr<T>& operator=(ObjectPtr<T>&& other) { // NOLINT(*)
441  // copy-and-swap idiom
442  ObjectPtr(std::move(other)).swap(*this); // NOLINT(*)
443  return *this;
444  }
449  explicit operator bool() const { return get() != nullptr; }
451  void reset() {
452  if (data_ != nullptr) {
453  data_->DecRef();
454  data_ = nullptr;
455  }
456  }
458  int use_count() const { return data_ != nullptr ? data_->use_count() : 0; }
460  bool unique() const { return data_ != nullptr && data_->use_count() == 1; }
462  bool operator==(const ObjectPtr<T>& other) const { return data_ == other.data_; }
464  bool operator!=(const ObjectPtr<T>& other) const { return data_ != other.data_; }
466  bool operator==(std::nullptr_t null) const { return data_ == nullptr; }
468  bool operator!=(std::nullptr_t null) const { return data_ != nullptr; }
469 
470  private:
472  Object* data_{nullptr};
477  explicit ObjectPtr(Object* data) : data_(data) {
478  if (data != nullptr) {
479  data_->IncRef();
480  }
481  }
487  static ObjectPtr<T> MoveFromRValueRefArg(Object** ref) {
488  ObjectPtr<T> ptr;
489  ptr.data_ = *ref;
490  *ref = nullptr;
491  return ptr;
492  }
493  // friend classes
494  friend class Object;
495  friend class ObjectRef;
496  friend struct ObjectPtrHash;
497  template <typename>
498  friend class ObjectPtr;
499  template <typename>
500  friend class ObjAllocatorBase;
501  friend class TVMPODValue_;
502  friend class TVMArgsSetter;
503  friend class TVMRetValue;
504  friend class TVMArgValue;
505  friend class TVMMovableArgValue_;
506  template <typename RelayRefType, typename ObjType>
507  friend RelayRefType GetRef(const ObjType* ptr);
508  template <typename BaseType, typename ObjType>
509  friend ObjectPtr<BaseType> GetObjectPtr(ObjType* ptr);
510 };
511 
512 // Forward declaration, to prevent circular includes.
513 template <typename T>
514 class Optional;
515 
517 class ObjectRef {
518  public:
520  ObjectRef() = default;
522  explicit ObjectRef(ObjectPtr<Object> data) : data_(data) {}
528  bool same_as(const ObjectRef& other) const { return data_ == other.data_; }
534  bool operator==(const ObjectRef& other) const { return data_ == other.data_; }
540  bool operator!=(const ObjectRef& other) const { return data_ != other.data_; }
546  bool operator<(const ObjectRef& other) const { return data_.get() < other.data_.get(); }
550  bool defined() const { return data_ != nullptr; }
552  const Object* get() const { return data_.get(); }
554  const Object* operator->() const { return get(); }
556  bool unique() const { return data_.unique(); }
558  int use_count() const { return data_.use_count(); }
559 
572  template <typename ObjectType, typename = std::enable_if_t<std::is_base_of_v<Object, ObjectType>>>
573  inline const ObjectType* as() const;
574 
592  template <typename ObjectRefType,
593  typename = std::enable_if_t<std::is_base_of_v<ObjectRef, ObjectRefType>>>
594  inline Optional<ObjectRefType> as() const;
595 
598  // Default type properties for the reference class.
599  static constexpr bool _type_is_nullable = true;
600 
601  protected:
605  Object* get_mutable() const { return data_.get(); }
612  template <typename T>
613  static T DowncastNoCheck(ObjectRef ref) {
614  return T(std::move(ref.data_));
615  }
621  static void FFIClearAfterMove(ObjectRef* ref) { ref->data_.data_ = nullptr; }
628  template <typename ObjectType>
630  return ObjectPtr<ObjectType>(ref.data_.data_);
631  }
632  // friend classes.
633  friend struct ObjectPtrHash;
634  friend class TVMRetValue;
635  friend class TVMArgsSetter;
636  friend class ObjectInternal;
637  template <typename SubRef, typename BaseRef>
638  friend SubRef Downcast(BaseRef ref);
639 };
640 
649 template <typename BaseType, typename ObjectType>
650 inline ObjectPtr<BaseType> GetObjectPtr(ObjectType* ptr);
651 
654  size_t operator()(const ObjectRef& a) const { return operator()(a.data_); }
655 
656  template <typename T>
657  size_t operator()(const ObjectPtr<T>& a) const {
658  return std::hash<Object*>()(a.get());
659  }
660 };
661 
664  bool operator()(const ObjectRef& a, const ObjectRef& b) const { return a.same_as(b); }
665 
666  template <typename T>
667  size_t operator()(const ObjectPtr<T>& a, const ObjectPtr<T>& b) const {
668  return a == b;
669  }
670 };
671 
677 #define TVM_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType) \
678  static_assert(!ParentType::_type_final, "ParentObj marked as final"); \
679  static uint32_t RuntimeTypeIndex() { \
680  static_assert(TypeName::_type_child_slots == 0 || ParentType::_type_child_slots == 0 || \
681  TypeName::_type_child_slots < ParentType::_type_child_slots, \
682  "Need to set _type_child_slots when parent specifies it."); \
683  if (TypeName::_type_index != ::tvm::runtime::TypeIndex::kDynamic) { \
684  return TypeName::_type_index; \
685  } \
686  return _GetOrAllocRuntimeTypeIndex(); \
687  } \
688  static uint32_t _GetOrAllocRuntimeTypeIndex() { \
689  static uint32_t tindex = Object::GetOrAllocRuntimeTypeIndex( \
690  TypeName::_type_key, TypeName::_type_index, ParentType::_GetOrAllocRuntimeTypeIndex(), \
691  TypeName::_type_child_slots, TypeName::_type_child_slots_can_overflow); \
692  return tindex; \
693  }
694 
700 #define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType) \
701  static const constexpr bool _type_final = true; \
702  static const constexpr int _type_child_slots = 0; \
703  TVM_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType)
704 
706 #if defined(__GNUC__)
707 #define TVM_ATTRIBUTE_UNUSED __attribute__((unused))
708 #else
709 #define TVM_ATTRIBUTE_UNUSED
710 #endif
711 
712 #define TVM_STR_CONCAT_(__x, __y) __x##__y
713 #define TVM_STR_CONCAT(__x, __y) TVM_STR_CONCAT_(__x, __y)
714 
715 #define TVM_OBJECT_REG_VAR_DEF static TVM_ATTRIBUTE_UNUSED uint32_t __make_Object_tid
716 
723 #define TVM_REGISTER_OBJECT_TYPE(TypeName) \
724  TVM_STR_CONCAT(TVM_OBJECT_REG_VAR_DEF, __COUNTER__) = TypeName::_GetOrAllocRuntimeTypeIndex()
725 
726 /*
727  * \brief Define the default copy/move constructor and assign operator
728  * \param TypeName The class typename.
729  */
730 #define TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName) \
731  TypeName(const TypeName& other) = default; \
732  TypeName(TypeName&& other) = default; \
733  TypeName& operator=(const TypeName& other) = default; \
734  TypeName& operator=(TypeName&& other) = default;
735 
736 /*
737  * \brief Define object reference methods.
738  * \param TypeName The object type name
739  * \param ParentType The parent type of the objectref
740  * \param ObjectName The type name of the object.
741  */
742 #define TVM_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \
743  TypeName() = default; \
744  explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \
745  TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \
746  const ObjectName* operator->() const { return static_cast<const ObjectName*>(data_.get()); } \
747  const ObjectName* get() const { return operator->(); } \
748  using ContainerType = ObjectName;
749 
750 /*
751  * \brief Define object reference methods that is not nullable.
752  *
753  * \param TypeName The object type name
754  * \param ParentType The parent type of the objectref
755  * \param ObjectName The type name of the object.
756  */
757 #define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \
758  explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \
759  TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \
760  const ObjectName* operator->() const { return static_cast<const ObjectName*>(data_.get()); } \
761  const ObjectName* get() const { return operator->(); } \
762  static constexpr bool _type_is_nullable = false; \
763  using ContainerType = ObjectName;
764 
765 /*
766  * \brief Define object reference methods of whose content is mutable.
767  * \param TypeName The object type name
768  * \param ParentType The parent type of the objectref
769  * \param ObjectName The type name of the object.
770  * \note We recommend making objects immutable when possible.
771  * This macro is only reserved for objects that stores runtime states.
772  */
773 #define TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \
774  TypeName() = default; \
775  TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \
776  explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \
777  ObjectName* operator->() const { return static_cast<ObjectName*>(data_.get()); } \
778  using ContainerType = ObjectName;
779 
780 /*
781  * \brief Define object reference methods that is both not nullable and mutable.
782  *
783  * \param TypeName The object type name
784  * \param ParentType The parent type of the objectref
785  * \param ObjectName The type name of the object.
786  */
787 #define TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \
788  explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \
789  TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \
790  ObjectName* operator->() const { return static_cast<ObjectName*>(data_.get()); } \
791  ObjectName* get() const { return operator->(); } \
792  static constexpr bool _type_is_nullable = false; \
793  using ContainerType = ObjectName;
794 
814 #define TVM_DEFINE_OBJECT_REF_COW_METHOD(ObjectName) \
815  ObjectName* CopyOnWrite() { \
816  ICHECK(data_ != nullptr); \
817  if (!data_.unique()) { \
818  auto n = make_object<ObjectName>(*(operator->())); \
819  ObjectPtr<Object>(std::move(n)).swap(data_); \
820  } \
821  return static_cast<ObjectName*>(data_.get()); \
822  }
823 
824 // Implementations details below
825 // Object reference counting.
826 #if TVM_OBJECT_ATOMIC_REF_COUNTER
827 
828 inline void Object::IncRef() { ref_counter_.fetch_add(1, std::memory_order_relaxed); }
829 
830 inline void Object::DecRef() {
831  if (ref_counter_.fetch_sub(1, std::memory_order_release) == 1) {
832  std::atomic_thread_fence(std::memory_order_acquire);
833  if (this->deleter_ != nullptr) {
834  (*this->deleter_)(this);
835  }
836  }
837 }
838 
839 inline int Object::use_count() const { return ref_counter_.load(std::memory_order_relaxed); }
840 
841 #else
842 
843 inline void Object::IncRef() { ++ref_counter_; }
844 
845 inline void Object::DecRef() {
846  if (--ref_counter_ == 0) {
847  if (this->deleter_ != nullptr) {
848  (*this->deleter_)(this);
849  }
850  }
851 }
852 
853 inline int Object::use_count() const { return ref_counter_; }
854 
855 #endif // TVM_OBJECT_ATOMIC_REF_COUNTER
856 
857 template <typename TargetType>
858 inline bool Object::IsInstance() const {
859  const Object* self = this;
860  // NOTE: the following code can be optimized by
861  // compiler dead-code elimination for already known constants.
862  if (self != nullptr) {
863  // Everything is a subclass of object.
864  if (std::is_same<TargetType, Object>::value) return true;
865  if (TargetType::_type_final) {
866  // if the target type is a final type
867  // then we only need to check the equivalence.
868  return self->type_index_ == TargetType::RuntimeTypeIndex();
869  } else {
870  // if target type is a non-leaf type
871  // Check if type index falls into the range of reserved slots.
872  uint32_t begin = TargetType::RuntimeTypeIndex();
873  // The condition will be optimized by constant-folding.
874  if (TargetType::_type_child_slots != 0) {
875  uint32_t end = begin + TargetType::_type_child_slots;
876  if (self->type_index_ >= begin && self->type_index_ < end) return true;
877  } else {
878  if (self->type_index_ == begin) return true;
879  }
880  if (!TargetType::_type_child_slots_can_overflow) return false;
881  // Invariance: parent index is always smaller than the child.
882  if (self->type_index_ < TargetType::RuntimeTypeIndex()) return false;
883  // The rare slower-path, check type hierarchy.
884  return self->DerivedFrom(TargetType::RuntimeTypeIndex());
885  }
886  } else {
887  return false;
888  }
889 }
890 
891 inline bool Object::unique() const { return use_count() == 1; }
892 
893 template <typename ObjectType, typename>
894 inline const ObjectType* ObjectRef::as() const {
895  if (data_ != nullptr && data_->IsInstance<ObjectType>()) {
896  return static_cast<ObjectType*>(data_.get());
897  } else {
898  return nullptr;
899  }
900 }
901 
902 template <typename RefType, typename ObjType>
903 inline RefType GetRef(const ObjType* ptr) {
904  static_assert(std::is_base_of<typename RefType::ContainerType, ObjType>::value,
905  "Can only cast to the ref of same container type");
906  if (!RefType::_type_is_nullable) {
907  ICHECK(ptr != nullptr);
908  }
909  return RefType(ObjectPtr<Object>(const_cast<Object*>(static_cast<const Object*>(ptr))));
910 }
911 
912 template <typename BaseType, typename ObjType>
913 inline ObjectPtr<BaseType> GetObjectPtr(ObjType* ptr) {
914  static_assert(std::is_base_of<BaseType, ObjType>::value,
915  "Can only cast to the ref of same container type");
916  return ObjectPtr<BaseType>(static_cast<Object*>(ptr));
917 }
918 
919 template <typename SubRef, typename BaseRef>
920 inline SubRef Downcast(BaseRef ref) {
921  if (ref.defined()) {
922  ICHECK(ref->template IsInstance<typename SubRef::ContainerType>())
923  << "Downcast from " << ref->GetTypeKey() << " to " << SubRef::ContainerType::_type_key
924  << " failed.";
925  } else {
926  ICHECK(SubRef::_type_is_nullable) << "Downcast from nullptr to not nullable reference of "
927  << SubRef::ContainerType::_type_key;
928  }
929  return SubRef(std::move(ref.data_));
930 }
931 
932 } // namespace runtime
933 } // namespace tvm
934 
935 #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:360
void swap(ObjectPtr< T > &other)
Swap this array with another Object.
Definition: object.h:407
T * get() const
Definition: object.h:413
friend class Object
Definition: object.h:494
bool operator!=(const ObjectPtr< T > &other) const
Definition: object.h:464
ObjectPtr()
default constructor
Definition: object.h:363
friend ObjectPtr< BaseType > GetObjectPtr(ObjType *ptr)
Definition: object.h:913
friend class ObjectPtr
Definition: object.h:498
T * operator->() const
Definition: object.h:417
friend RelayRefType GetRef(const ObjType *ptr)
Definition: object.h:903
ObjectPtr(std::nullptr_t)
default constructor
Definition: object.h:365
ObjectPtr< T > & operator=(ObjectPtr< T > &&other)
move assignment
Definition: object.h:440
ObjectPtr(const ObjectPtr< T > &other)
copy constructor
Definition: object.h:370
ObjectPtr(ObjectPtr< Y > &&other)
move constructor
Definition: object.h:395
int use_count() const
Definition: object.h:458
bool operator==(const ObjectPtr< T > &other) const
Definition: object.h:462
ObjectPtr(ObjectPtr< T > &&other)
move constructor
Definition: object.h:386
void reset()
reset the content of ptr to be nullptr
Definition: object.h:451
~ObjectPtr()
destructor
Definition: object.h:402
T & operator*() const
Definition: object.h:421
ObjectPtr(const ObjectPtr< U > &other)
copy constructor
Definition: object.h:377
bool operator==(std::nullptr_t null) const
Definition: object.h:466
bool operator!=(std::nullptr_t null) const
Definition: object.h:468
bool unique() const
Definition: object.h:460
ObjectPtr< T > & operator=(const ObjectPtr< T > &other)
copy assignment
Definition: object.h:429
Base class of all object reference.
Definition: object.h:517
int use_count() const
Definition: object.h:558
bool defined() const
Definition: object.h:550
static void FFIClearAfterMove(ObjectRef *ref)
Clear the object ref data field without DecRef after we successfully moved the field.
Definition: object.h:621
const Object * operator->() const
Definition: object.h:554
static constexpr bool _type_is_nullable
Definition: object.h:599
bool operator<(const ObjectRef &other) const
Comparator.
Definition: object.h:546
friend class ObjectInternal
Definition: object.h:636
bool unique() const
Definition: object.h:556
friend SubRef Downcast(BaseRef ref)
Downcast a base reference type to a more specific type.
Definition: object.h:920
ObjectRef(ObjectPtr< Object > data)
Constructor from existing object ptr.
Definition: object.h:522
ObjectRef()=default
default constructor
bool operator!=(const ObjectRef &other) const
Comparator.
Definition: object.h:540
const Object * get() const
Definition: object.h:552
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:603
static T DowncastNoCheck(ObjectRef ref)
Internal helper function downcast a ref without check.
Definition: object.h:613
const ObjectType * as() const
Try to downcast the internal Object to a raw pointer of a corresponding type.
Definition: object.h:894
bool same_as(const ObjectRef &other) const
Comparator.
Definition: object.h:528
Object * get_mutable() const
Definition: object.h:605
static ObjectPtr< ObjectType > GetDataPtr(const ObjectRef &ref)
Internal helper function get data_ as ObjectPtr of ObjectType.
Definition: object.h:629
bool operator==(const ObjectRef &other) const
Comparator.
Definition: object.h:534
base class of all object containers.
Definition: object.h:169
RefCounterType ref_counter_
The internal reference counter.
Definition: object.h:265
Object()
Definition: object.h:243
uint32_t type_index() const
Definition: object.h:177
uint32_t type_index_
Type index(tag) that indicates the type of the object.
Definition: object.h:263
std::string GetTypeKey() const
Definition: object.h:182
std::atomic< int32_t > RefCounterType
Definition: object.h:219
size_t GetTypeKeyHash() const
Definition: object.h:186
static uint32_t _GetOrAllocRuntimeTypeIndex()
Definition: object.h:226
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:253
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:830
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:858
Object(Object &&other)
Definition: object.h:251
Object(const Object &other)
Definition: object.h:249
void IncRef()
developer function, increases reference counter.
Definition: object.h:828
static uint32_t RuntimeTypeIndex()
Definition: object.h:227
Object & operator=(Object &&other)
Definition: object.h:256
FDeleter deleter_
deleter of this object to enable customized allocation. If the deleter is nullptr,...
Definition: object.h:271
bool unique() const
Definition: object.h:891
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:649
Definition: packed_func.h:1666
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:544
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:920
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:663
size_t operator()(const ObjectPtr< T > &a, const ObjectPtr< T > &b) const
Definition: object.h:667
bool operator()(const ObjectRef &a, const ObjectRef &b) const
Definition: object.h:664
ObjectRef hash functor.
Definition: object.h:653
size_t operator()(const ObjectPtr< T > &a) const
Definition: object.h:657
size_t operator()(const ObjectRef &a) const
Definition: object.h:654
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:80
@ kRuntimeNDArray
runtime::NDArray.
Definition: object.h:64
@ kRuntimeMap
runtime::Map.
Definition: object.h:70
@ kRuntimeClosure
Definition: object.h:78
@ kRuntimeADT
Definition: object.h:79
@ kRuntimeString
runtime::String.
Definition: object.h:66
@ kDynamic
Type index is allocated during runtime.
Definition: object.h:82
@ kRuntimeArray
runtime::Array.
Definition: object.h:68
@ kRuntimeModule
runtime::Module.
Definition: object.h:62
@ kRuntimeDiscoDRef
runtime::DRef
Definition: object.h:76
@ kRuntimeShapeTuple
runtime::ShapeTuple.
Definition: object.h:72