tvm
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.
79  // static assignments that may subject to change.
85  };
86 }; // namespace TypeIndex
87 
171 class TVM_DLL Object {
172  public:
177  typedef void (*FDeleter)(Object* self);
179  uint32_t type_index() const { return type_index_; }
184  std::string GetTypeKey() const { return TypeIndex2Key(type_index_); }
188  size_t GetTypeKeyHash() const { return TypeIndex2KeyHash(type_index_); }
194  template <typename TargetType>
195  inline bool IsInstance() const;
200  inline bool unique() const;
206  static std::string TypeIndex2Key(uint32_t tindex);
212  static size_t TypeIndex2KeyHash(uint32_t tindex);
218  static uint32_t TypeKey2Index(const std::string& key);
219 
220 #if TVM_OBJECT_ATOMIC_REF_COUNTER
221  using RefCounterType = std::atomic<int32_t>;
222 #else
223  using RefCounterType = int32_t;
224 #endif
225 
226  static constexpr const char* _type_key = "runtime.Object";
227 
228  static uint32_t _GetOrAllocRuntimeTypeIndex() { return TypeIndex::kRoot; }
229  static uint32_t RuntimeTypeIndex() { return TypeIndex::kRoot; }
230 
231  // Default object type properties for sub-classes
232  static constexpr bool _type_final = false;
233  static constexpr uint32_t _type_child_slots = 0;
234  static constexpr bool _type_child_slots_can_overflow = true;
235  // member information
236  static constexpr bool _type_has_method_visit_attrs = true;
237  static constexpr bool _type_has_method_sequal_reduce = false;
238  static constexpr bool _type_has_method_shash_reduce = false;
239  // NOTE: the following field is not type index of Object
240  // but was intended to be used by sub-classes as default value.
241  // The type index of Object is TypeIndex::kRoot
242  static constexpr uint32_t _type_index = TypeIndex::kDynamic;
243 
244  // Default constructor and copy constructor
245  Object() {}
246  // Override the copy and assign constructors to do nothing.
247  // This is to make sure only contents, but not deleter and ref_counter
248  // are copied when a child class copies itself.
249  // This will enable us to use make_object<ObjectClass>(*obj_ptr)
250  // to copy an existing object.
251  Object(const Object& other) { // NOLINT(*)
252  }
253  Object(Object&& other) { // NOLINT(*)
254  }
255  Object& operator=(const Object& other) { // NOLINT(*)
256  return *this;
257  }
258  Object& operator=(Object&& other) { // NOLINT(*)
259  return *this;
260  }
261 
262  protected:
263  // The fields of the base object cell.
265  uint32_t type_index_{0};
267  RefCounterType ref_counter_{0};
273  FDeleter deleter_ = nullptr;
274  // Invariant checks.
275  static_assert(sizeof(int32_t) == sizeof(RefCounterType) &&
276  alignof(int32_t) == sizeof(RefCounterType),
277  "RefCounter ABI check.");
278 
296  static uint32_t GetOrAllocRuntimeTypeIndex(const std::string& key, uint32_t static_tindex,
297  uint32_t parent_tindex, uint32_t type_child_slots,
298  bool type_child_slots_can_overflow);
299 
300  // reference counter related operations
302  inline void IncRef();
307  inline void DecRef();
308 
309  private:
314  inline int use_count() const;
320  bool DerivedFrom(uint32_t parent_tindex) const;
321  // friend classes
322  template <typename>
323  friend class ObjAllocatorBase;
324  template <typename>
325  friend class ObjectPtr;
326  friend class TVMRetValue;
327  friend class ObjectInternal;
328 };
329 
342 template <typename RelayRefType, typename ObjectType>
343 inline RelayRefType GetRef(const ObjectType* ptr);
344 
353 template <typename SubRef, typename BaseRef>
354 inline SubRef Downcast(BaseRef ref);
355 
361 template <typename T>
362 class ObjectPtr {
363  public:
367  ObjectPtr(std::nullptr_t) {} // NOLINT(*)
372  ObjectPtr(const ObjectPtr<T>& other) // NOLINT(*)
373  : ObjectPtr(other.data_) {}
378  template <typename U>
379  ObjectPtr(const ObjectPtr<U>& other) // NOLINT(*)
380  : ObjectPtr(other.data_) {
381  static_assert(std::is_base_of<T, U>::value,
382  "can only assign of child class ObjectPtr to parent");
383  }
388  ObjectPtr(ObjectPtr<T>&& other) // NOLINT(*)
389  : data_(other.data_) {
390  other.data_ = nullptr;
391  }
396  template <typename Y>
397  ObjectPtr(ObjectPtr<Y>&& other) // NOLINT(*)
398  : data_(other.data_) {
399  static_assert(std::is_base_of<T, Y>::value,
400  "can only assign of child class ObjectPtr to parent");
401  other.data_ = nullptr;
402  }
404  ~ObjectPtr() { this->reset(); }
409  void swap(ObjectPtr<T>& other) { // NOLINT(*)
410  std::swap(data_, other.data_);
411  }
415  T* get() const { return static_cast<T*>(data_); }
419  T* operator->() const { return get(); }
423  T& operator*() const { // NOLINT(*)
424  return *get();
425  }
431  ObjectPtr<T>& operator=(const ObjectPtr<T>& other) { // NOLINT(*)
432  // takes in plane operator to enable copy elison.
433  // copy-and-swap idiom
434  ObjectPtr(other).swap(*this); // NOLINT(*)
435  return *this;
436  }
442  ObjectPtr<T>& operator=(ObjectPtr<T>&& other) { // NOLINT(*)
443  // copy-and-swap idiom
444  ObjectPtr(std::move(other)).swap(*this); // NOLINT(*)
445  return *this;
446  }
451  explicit operator bool() const { return get() != nullptr; }
453  void reset() {
454  if (data_ != nullptr) {
455  data_->DecRef();
456  data_ = nullptr;
457  }
458  }
460  int use_count() const { return data_ != nullptr ? data_->use_count() : 0; }
462  bool unique() const { return data_ != nullptr && data_->use_count() == 1; }
464  bool operator==(const ObjectPtr<T>& other) const { return data_ == other.data_; }
466  bool operator!=(const ObjectPtr<T>& other) const { return data_ != other.data_; }
468  bool operator==(std::nullptr_t null) const { return data_ == nullptr; }
470  bool operator!=(std::nullptr_t null) const { return data_ != nullptr; }
471 
472  private:
474  Object* data_{nullptr};
479  explicit ObjectPtr(Object* data) : data_(data) {
480  if (data != nullptr) {
481  data_->IncRef();
482  }
483  }
489  static ObjectPtr<T> MoveFromRValueRefArg(Object** ref) {
490  ObjectPtr<T> ptr;
491  ptr.data_ = *ref;
492  *ref = nullptr;
493  return ptr;
494  }
495  // friend classes
496  friend class Object;
497  friend class ObjectRef;
498  friend struct ObjectPtrHash;
499  template <typename>
500  friend class ObjectPtr;
501  template <typename>
502  friend class ObjAllocatorBase;
503  friend class TVMPODValue_;
504  friend class TVMArgsSetter;
505  friend class TVMRetValue;
506  friend class TVMArgValue;
507  friend class TVMMovableArgValue_;
508  template <typename RelayRefType, typename ObjType>
509  friend RelayRefType GetRef(const ObjType* ptr);
510  template <typename BaseType, typename ObjType>
511  friend ObjectPtr<BaseType> GetObjectPtr(ObjType* ptr);
512 };
513 
514 // Forward declaration, to prevent circular includes.
515 template <typename T>
516 class Optional;
517 
519 class ObjectRef {
520  public:
522  ObjectRef() = default;
524  explicit ObjectRef(ObjectPtr<Object> data) : data_(data) {}
530  bool same_as(const ObjectRef& other) const { return data_ == other.data_; }
536  bool operator==(const ObjectRef& other) const { return data_ == other.data_; }
542  bool operator!=(const ObjectRef& other) const { return data_ != other.data_; }
548  bool operator<(const ObjectRef& other) const { return data_.get() < other.data_.get(); }
552  bool defined() const { return data_ != nullptr; }
554  const Object* get() const { return data_.get(); }
556  const Object* operator->() const { return get(); }
558  bool unique() const { return data_.unique(); }
560  int use_count() const { return data_.use_count(); }
561 
574  template <typename ObjectType, typename = std::enable_if_t<std::is_base_of_v<Object, ObjectType>>>
575  inline const ObjectType* as() const;
576 
594  template <typename ObjectRefType,
595  typename = std::enable_if_t<std::is_base_of_v<ObjectRef, ObjectRefType>>>
596  inline Optional<ObjectRefType> as() const;
597 
600  // Default type properties for the reference class.
601  static constexpr bool _type_is_nullable = true;
602 
603  protected:
607  Object* get_mutable() const { return data_.get(); }
614  template <typename T>
615  static T DowncastNoCheck(ObjectRef ref) {
616  return T(std::move(ref.data_));
617  }
623  static void FFIClearAfterMove(ObjectRef* ref) { ref->data_.data_ = nullptr; }
630  template <typename ObjectType>
632  return ObjectPtr<ObjectType>(ref.data_.data_);
633  }
634  // friend classes.
635  friend struct ObjectPtrHash;
636  friend class TVMRetValue;
637  friend class TVMArgsSetter;
638  friend class ObjectInternal;
639  template <typename SubRef, typename BaseRef>
640  friend SubRef Downcast(BaseRef ref);
641 };
642 
651 template <typename BaseType, typename ObjectType>
652 inline ObjectPtr<BaseType> GetObjectPtr(ObjectType* ptr);
653 
656  size_t operator()(const ObjectRef& a) const { return operator()(a.data_); }
657 
658  template <typename T>
659  size_t operator()(const ObjectPtr<T>& a) const {
660  return std::hash<Object*>()(a.get());
661  }
662 };
663 
666  bool operator()(const ObjectRef& a, const ObjectRef& b) const { return a.same_as(b); }
667 
668  template <typename T>
669  size_t operator()(const ObjectPtr<T>& a, const ObjectPtr<T>& b) const {
670  return a == b;
671  }
672 };
673 
679 #define TVM_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType) \
680  static_assert(!ParentType::_type_final, "ParentObj marked as final"); \
681  static uint32_t RuntimeTypeIndex() { \
682  static_assert(TypeName::_type_child_slots == 0 || ParentType::_type_child_slots == 0 || \
683  TypeName::_type_child_slots < ParentType::_type_child_slots, \
684  "Need to set _type_child_slots when parent specifies it."); \
685  if (TypeName::_type_index != ::tvm::runtime::TypeIndex::kDynamic) { \
686  return TypeName::_type_index; \
687  } \
688  return _GetOrAllocRuntimeTypeIndex(); \
689  } \
690  static uint32_t _GetOrAllocRuntimeTypeIndex() { \
691  static uint32_t tindex = Object::GetOrAllocRuntimeTypeIndex( \
692  TypeName::_type_key, TypeName::_type_index, ParentType::_GetOrAllocRuntimeTypeIndex(), \
693  TypeName::_type_child_slots, TypeName::_type_child_slots_can_overflow); \
694  return tindex; \
695  }
696 
702 #define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType) \
703  static const constexpr bool _type_final = true; \
704  static const constexpr int _type_child_slots = 0; \
705  TVM_DECLARE_BASE_OBJECT_INFO(TypeName, ParentType)
706 
708 #if defined(__GNUC__)
709 #define TVM_ATTRIBUTE_UNUSED __attribute__((unused))
710 #else
711 #define TVM_ATTRIBUTE_UNUSED
712 #endif
713 
714 #define TVM_STR_CONCAT_(__x, __y) __x##__y
715 #define TVM_STR_CONCAT(__x, __y) TVM_STR_CONCAT_(__x, __y)
716 
717 #define TVM_OBJECT_REG_VAR_DEF static TVM_ATTRIBUTE_UNUSED uint32_t __make_Object_tid
718 
725 #define TVM_REGISTER_OBJECT_TYPE(TypeName) \
726  TVM_STR_CONCAT(TVM_OBJECT_REG_VAR_DEF, __COUNTER__) = TypeName::_GetOrAllocRuntimeTypeIndex()
727 
728 /*
729  * \brief Define the default copy/move constructor and assign operator
730  * \param TypeName The class typename.
731  */
732 #define TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName) \
733  TypeName(const TypeName& other) = default; \
734  TypeName(TypeName&& other) = default; \
735  TypeName& operator=(const TypeName& other) = default; \
736  TypeName& operator=(TypeName&& other) = default;
737 
738 /*
739  * \brief Define object reference methods.
740  * \param TypeName The object type name
741  * \param ParentType The parent type of the objectref
742  * \param ObjectName The type name of the object.
743  */
744 #define TVM_DEFINE_OBJECT_REF_METHODS_WITHOUT_DEFAULT_CONSTRUCTOR(TypeName, ParentType, \
745  ObjectName) \
746  explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \
747  TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \
748  const ObjectName* operator->() const { return static_cast<const ObjectName*>(data_.get()); } \
749  const ObjectName* get() const { return operator->(); } \
750  using ContainerType = ObjectName;
751 
752 /*
753  * \brief Define object reference methods.
754  * \param TypeName The object type name
755  * \param ParentType The parent type of the objectref
756  * \param ObjectName The type name of the object.
757  */
758 #define TVM_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \
759  TypeName() = default; \
760  TVM_DEFINE_OBJECT_REF_METHODS_WITHOUT_DEFAULT_CONSTRUCTOR(TypeName, ParentType, ObjectName)
761 
762 /*
763  * \brief Define object reference methods that is not nullable.
764  *
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  */
769 #define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \
770  explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \
771  TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \
772  const ObjectName* operator->() const { return static_cast<const ObjectName*>(data_.get()); } \
773  const ObjectName* get() const { return operator->(); } \
774  static constexpr bool _type_is_nullable = false; \
775  using ContainerType = ObjectName;
776 
777 /*
778  * \brief Define object reference methods of whose content is mutable.
779  * \param TypeName The object type name
780  * \param ParentType The parent type of the objectref
781  * \param ObjectName The type name of the object.
782  * \note We recommend making objects immutable when possible.
783  * This macro is only reserved for objects that stores runtime states.
784  */
785 #define TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \
786  TypeName() = default; \
787  TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \
788  explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \
789  ObjectName* operator->() const { return static_cast<ObjectName*>(data_.get()); } \
790  using ContainerType = ObjectName;
791 
792 /*
793  * \brief Define object reference methods that is both not nullable and mutable.
794  *
795  * \param TypeName The object type name
796  * \param ParentType The parent type of the objectref
797  * \param ObjectName The type name of the object.
798  */
799 #define TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName) \
800  explicit TypeName(::tvm::runtime::ObjectPtr<::tvm::runtime::Object> n) : ParentType(n) {} \
801  TVM_DEFINE_DEFAULT_COPY_MOVE_AND_ASSIGN(TypeName); \
802  ObjectName* operator->() const { return static_cast<ObjectName*>(data_.get()); } \
803  ObjectName* get() const { return operator->(); } \
804  static constexpr bool _type_is_nullable = false; \
805  using ContainerType = ObjectName;
806 
826 #define TVM_DEFINE_OBJECT_REF_COW_METHOD(ObjectName) \
827  ObjectName* CopyOnWrite() { \
828  ICHECK(data_ != nullptr); \
829  if (!data_.unique()) { \
830  auto n = make_object<ObjectName>(*(operator->())); \
831  ObjectPtr<Object>(std::move(n)).swap(data_); \
832  } \
833  return static_cast<ObjectName*>(data_.get()); \
834  }
835 
836 // Implementations details below
837 // Object reference counting.
838 #if TVM_OBJECT_ATOMIC_REF_COUNTER
839 
840 inline void Object::IncRef() { ref_counter_.fetch_add(1, std::memory_order_relaxed); }
841 
842 inline void Object::DecRef() {
843  if (ref_counter_.fetch_sub(1, std::memory_order_release) == 1) {
844  std::atomic_thread_fence(std::memory_order_acquire);
845  if (this->deleter_ != nullptr) {
846  (*this->deleter_)(this);
847  }
848  }
849 }
850 
851 inline int Object::use_count() const { return ref_counter_.load(std::memory_order_relaxed); }
852 
853 #else
854 
855 inline void Object::IncRef() { ++ref_counter_; }
856 
857 inline void Object::DecRef() {
858  if (--ref_counter_ == 0) {
859  if (this->deleter_ != nullptr) {
860  (*this->deleter_)(this);
861  }
862  }
863 }
864 
865 inline int Object::use_count() const { return ref_counter_; }
866 
867 #endif // TVM_OBJECT_ATOMIC_REF_COUNTER
868 
869 template <typename TargetType>
870 inline bool Object::IsInstance() const {
871  const Object* self = this;
872  // NOTE: the following code can be optimized by
873  // compiler dead-code elimination for already known constants.
874  if (self != nullptr) {
875  // Everything is a subclass of object.
876  if (std::is_same<TargetType, Object>::value) return true;
877  if (TargetType::_type_final) {
878  // if the target type is a final type
879  // then we only need to check the equivalence.
880  return self->type_index_ == TargetType::RuntimeTypeIndex();
881  } else {
882  // if target type is a non-leaf type
883  // Check if type index falls into the range of reserved slots.
884  uint32_t begin = TargetType::RuntimeTypeIndex();
885  // The condition will be optimized by constant-folding.
886  if (TargetType::_type_child_slots != 0) {
887  uint32_t end = begin + TargetType::_type_child_slots;
888  if (self->type_index_ >= begin && self->type_index_ < end) return true;
889  } else {
890  if (self->type_index_ == begin) return true;
891  }
892  if (!TargetType::_type_child_slots_can_overflow) return false;
893  // Invariance: parent index is always smaller than the child.
894  if (self->type_index_ < TargetType::RuntimeTypeIndex()) return false;
895  // The rare slower-path, check type hierarchy.
896  return self->DerivedFrom(TargetType::RuntimeTypeIndex());
897  }
898  } else {
899  return false;
900  }
901 }
902 
903 inline bool Object::unique() const { return use_count() == 1; }
904 
905 template <typename ObjectType, typename>
906 inline const ObjectType* ObjectRef::as() const {
907  if (data_ != nullptr && data_->IsInstance<ObjectType>()) {
908  return static_cast<ObjectType*>(data_.get());
909  } else {
910  return nullptr;
911  }
912 }
913 
914 template <typename RefType, typename ObjType>
915 inline RefType GetRef(const ObjType* ptr) {
916  static_assert(std::is_base_of<typename RefType::ContainerType, ObjType>::value,
917  "Can only cast to the ref of same container type");
918  if (!RefType::_type_is_nullable) {
919  ICHECK(ptr != nullptr);
920  }
921  return RefType(ObjectPtr<Object>(const_cast<Object*>(static_cast<const Object*>(ptr))));
922 }
923 
924 template <typename BaseType, typename ObjType>
925 inline ObjectPtr<BaseType> GetObjectPtr(ObjType* ptr) {
926  static_assert(std::is_base_of<BaseType, ObjType>::value,
927  "Can only cast to the ref of same container type");
928  return ObjectPtr<BaseType>(static_cast<Object*>(ptr));
929 }
930 
931 template <typename SubRef, typename BaseRef>
932 inline SubRef Downcast(BaseRef ref) {
933  if (ref.defined()) {
934  ICHECK(ref->template IsInstance<typename SubRef::ContainerType>())
935  << "Downcast from " << ref->GetTypeKey() << " to " << SubRef::ContainerType::_type_key
936  << " failed.";
937  } else {
938  ICHECK(SubRef::_type_is_nullable) << "Downcast from nullptr to not nullable reference of "
939  << SubRef::ContainerType::_type_key;
940  }
941  return SubRef(std::move(ref.data_));
942 }
943 
944 } // namespace runtime
945 } // namespace tvm
946 
947 #endif // TVM_RUNTIME_OBJECT_H_
Managed reference to RelayRefTypeNode.
Definition: type.h:577
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:362
void swap(ObjectPtr< T > &other)
Swap this array with another Object.
Definition: object.h:409
T * get() const
Definition: object.h:415
friend class Object
Definition: object.h:496
bool operator!=(const ObjectPtr< T > &other) const
Definition: object.h:466
ObjectPtr()
default constructor
Definition: object.h:365
friend ObjectPtr< BaseType > GetObjectPtr(ObjType *ptr)
Definition: object.h:925
friend class ObjectPtr
Definition: object.h:500
T * operator->() const
Definition: object.h:419
friend RelayRefType GetRef(const ObjType *ptr)
Definition: object.h:915
ObjectPtr(std::nullptr_t)
default constructor
Definition: object.h:367
ObjectPtr< T > & operator=(ObjectPtr< T > &&other)
move assignment
Definition: object.h:442
ObjectPtr(const ObjectPtr< T > &other)
copy constructor
Definition: object.h:372
ObjectPtr(ObjectPtr< Y > &&other)
move constructor
Definition: object.h:397
int use_count() const
Definition: object.h:460
bool operator==(const ObjectPtr< T > &other) const
Definition: object.h:464
ObjectPtr(ObjectPtr< T > &&other)
move constructor
Definition: object.h:388
void reset()
reset the content of ptr to be nullptr
Definition: object.h:453
~ObjectPtr()
destructor
Definition: object.h:404
T & operator*() const
Definition: object.h:423
ObjectPtr(const ObjectPtr< U > &other)
copy constructor
Definition: object.h:379
bool operator==(std::nullptr_t null) const
Definition: object.h:468
bool operator!=(std::nullptr_t null) const
Definition: object.h:470
bool unique() const
Definition: object.h:462
ObjectPtr< T > & operator=(const ObjectPtr< T > &other)
copy assignment
Definition: object.h:431
Base class of all object reference.
Definition: object.h:519
int use_count() const
Definition: object.h:560
bool defined() const
Definition: object.h:552
static void FFIClearAfterMove(ObjectRef *ref)
Clear the object ref data field without DecRef after we successfully moved the field.
Definition: object.h:623
const Object * operator->() const
Definition: object.h:556
static constexpr bool _type_is_nullable
Definition: object.h:601
bool operator<(const ObjectRef &other) const
Comparator.
Definition: object.h:548
friend class ObjectInternal
Definition: object.h:638
bool unique() const
Definition: object.h:558
friend SubRef Downcast(BaseRef ref)
Downcast a base reference type to a more specific type.
Definition: object.h:932
ObjectRef(ObjectPtr< Object > data)
Constructor from existing object ptr.
Definition: object.h:524
ObjectRef()=default
default constructor
bool operator!=(const ObjectRef &other) const
Comparator.
Definition: object.h:542
const Object * get() const
Definition: object.h:554
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:605
static T DowncastNoCheck(ObjectRef ref)
Internal helper function downcast a ref without check.
Definition: object.h:615
const ObjectType * as() const
Try to downcast the internal Object to a raw pointer of a corresponding type.
Definition: object.h:906
bool same_as(const ObjectRef &other) const
Comparator.
Definition: object.h:530
Object * get_mutable() const
Definition: object.h:607
static ObjectPtr< ObjectType > GetDataPtr(const ObjectRef &ref)
Internal helper function get data_ as ObjectPtr of ObjectType.
Definition: object.h:631
bool operator==(const ObjectRef &other) const
Comparator.
Definition: object.h:536
base class of all object containers.
Definition: object.h:171
RefCounterType ref_counter_
The internal reference counter.
Definition: object.h:267
Object()
Definition: object.h:245
uint32_t type_index() const
Definition: object.h:179
uint32_t type_index_
Type index(tag) that indicates the type of the object.
Definition: object.h:265
std::string GetTypeKey() const
Definition: object.h:184
std::atomic< int32_t > RefCounterType
Definition: object.h:221
size_t GetTypeKeyHash() const
Definition: object.h:188
static uint32_t _GetOrAllocRuntimeTypeIndex()
Definition: object.h:228
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:255
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:842
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:870
Object(Object &&other)
Definition: object.h:253
Object(const Object &other)
Definition: object.h:251
void IncRef()
developer function, increases reference counter.
Definition: object.h:840
static uint32_t RuntimeTypeIndex()
Definition: object.h:229
Object & operator=(Object &&other)
Definition: object.h:258
FDeleter deleter_
deleter of this object to enable customized allocation. If the deleter is nullptr,...
Definition: object.h:273
bool unique() const
Definition: object.h:903
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:657
Definition: packed_func.h:1680
Internal auxiliary struct for TypedPackedFunc to indicate a movable argument.
Definition: packed_func.h:717
Internal base class to handle conversion to POD values.
Definition: packed_func.h:552
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:807
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:932
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:665
size_t operator()(const ObjectPtr< T > &a, const ObjectPtr< T > &b) const
Definition: object.h:669
bool operator()(const ObjectRef &a, const ObjectRef &b) const
Definition: object.h:666
ObjectRef hash functor.
Definition: object.h:655
size_t operator()(const ObjectPtr< T > &a) const
Definition: object.h:659
size_t operator()(const ObjectRef &a) const
Definition: object.h:656
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
@ kRuntimeRPCObjectRef
runtime::RPCObjectRef
Definition: object.h:78
@ kStaticIndexEnd
Definition: object.h:82
@ kRuntimeNDArray
runtime::NDArray.
Definition: object.h:64
@ kRuntimeMap
runtime::Map.
Definition: object.h:70
@ kRuntimeClosure
Definition: object.h:80
@ kRuntimeADT
Definition: object.h:81
@ kRuntimeString
runtime::String.
Definition: object.h:66
@ kDynamic
Type index is allocated during runtime.
Definition: object.h:84
@ kRuntimeArray
runtime::Array.
Definition: object.h:68
@ kRuntimeModule
runtime::Module.
Definition: object.h:62
@ kRuntimeDiscoDRef
runtime::DRef for disco distributed runtime
Definition: object.h:76
@ kRuntimeShapeTuple
runtime::ShapeTuple.
Definition: object.h:72