26 #ifndef TVM_SCRIPT_PRINTER_TRACED_OBJECT_H_ 27 #define TVM_SCRIPT_PRINTER_TRACED_OBJECT_H_ 38 template <
typename RefT>
40 template <
typename K,
typename V>
51 template <typename T, bool IsObject = std::is_base_of<ObjectRef, T>::value>
64 template <
typename K,
typename V>
84 template <
typename RefT>
86 using ObjectType =
typename RefT::ContainerType;
93 : ref_(object_ref), path_(
std::move(path)) {}
96 template <
typename DerivedRef>
98 : ref_(derived.Get()), path_(derived.GetPath()) {}
103 template <
typename T,
typename BaseType>
106 const ObjectType* node =
static_cast<const ObjectType*
>(ref_.get());
107 const T& attr = node->*member_ptr;
109 return WrapperType(attr, path_->Attr(attr_key));
115 const RefT&
Get()
const {
return ref_; }
120 template <
typename RefU>
122 return ref_->template IsInstance<typename RefU::ContainerType>();
128 bool defined()
const {
return ref_.defined(); }
135 template <
typename RefU>
145 template <
typename RefU>
147 if (ref_->template IsInstance<typename RefU::ContainerType>()) {
148 return Downcast<RefU>();
167 template <
typename K,
typename V>
180 : iter_(iter), map_path_(
std::move(map_path)) {}
186 pointer operator->()
const =
delete;
190 return std::make_pair(kv.first,
WrappedV(kv.second, map_path_->MapValue(kv.first)));
212 template <
typename K,
typename V>
221 : map_(
std::move(map)), path_(
std::move(path)) {}
227 auto it = map_.find(key);
228 ICHECK(it != map_.end()) <<
"No such key in Map";
230 return WrappedV(kv.second, path_->MapValue(kv.first));
256 bool empty()
const {
return map_.empty(); }
266 template <
typename T>
278 : array_(array), index_(index), array_path_(array_path) {}
310 return array_.get() == other.array_.
get() && index_ == other.index_;
324 template <
typename T>
333 : array_(
std::move(array)), path_(
std::move(path)) {}
349 return WrappedT(array_[index], path_->ArrayIndex(index));
369 bool empty()
const {
return array_.empty(); }
374 size_t size()
const {
return array_.size(); }
384 template <
typename T>
393 : optional_(value.Get().defined() ? value.Get() :
Optional<T>(
NullOpt)),
394 path_(value.GetPath()) {}
398 : optional_(
std::move(optional)), path_(
std::move(path)) {}
413 bool defined()
const {
return optional_.defined(); }
423 explicit operator bool()
const {
return optional_.defined(); }
433 template <
typename T>
437 : value_(value), path_(
std::move(path)) {}
442 const T&
Get()
const {
return value_; }
452 template <
typename F>
466 template <
typename RefT>
475 template <
typename RefT>
479 return WrappedT(
object, std::move(path));
484 #endif // TVM_SCRIPT_PRINTER_TRACED_OBJECT_H_ TracedOptional< RefU > TryDowncast() const
Convert the wrapped reference type to a subtype.
Definition: traced_object.h:146
typename detail::TracedObjectWrapperSelector< T >::Type WrappedT
Definition: traced_object.h:387
Traced wrapper for regular (non-container) TVM objects.
Definition: traced_object.h:39
bool empty() const
Returns true iff the wrapped array is empty.
Definition: traced_object.h:369
bool IsInstance() const
Check if the reference to the wrapped object can be converted to RefU.
Definition: traced_object.h:121
TracedMapIterator & operator++()
Definition: traced_object.h:193
const T & Get() const
Access the wrapped value.
Definition: traced_object.h:442
bool defined() const
Returns true iff the object is present.
Definition: traced_object.h:413
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
static ObjectPath Root()
Create a path that represents the root object itself.
TracedArrayIterator operator+(difference_type offset) const
Definition: traced_object.h:299
bool defined() const
Same as Get().defined().
Definition: traced_object.h:128
bool operator!=(const TracedMapIterator &other) const
Definition: traced_object.h:184
value_type reference
Definition: traced_object.h:177
Definition: loop_state.h:456
TracedArrayIterator & operator--()
Definition: traced_object.h:284
WrappedV at(const K &key) const
Get a value by its key, wrapped in a traced wrapper.
Definition: traced_object.h:226
typename detail::TracedObjectWrapperSelector< T >::Type WrappedT
Definition: traced_object.h:327
const RefT & Get() const
Access the wrapped object.
Definition: traced_object.h:115
TracedArrayIterator(Array< T > array, size_t index, ObjectPath array_path)
Definition: traced_object.h:277
iterator end() const
Get an iterator to the end of the map.
Definition: traced_object.h:251
bool empty() const
Returns true iff the wrapped map is empty.
Definition: traced_object.h:256
WrappedT value_type
Definition: traced_object.h:272
ptrdiff_t difference_type
Definition: traced_object.h:271
const std::pair< K, WrappedV > value_type
Definition: traced_object.h:175
const ObjectPath & GetPath() const
Get the path of the wrapped object.
Definition: traced_object.h:241
Iterator class for TracedMap<K, V>
Definition: traced_object.h:168
typename detail::TracedObjectWrapperSelector< V >::Type WrappedV
Definition: traced_object.h:170
const ObjectPath & GetPath() const
Get the path of the wrapped array object.
Definition: traced_object.h:343
const Map< K, V > & Get() const
Access the wrapped map object.
Definition: traced_object.h:236
ptrdiff_t difference_type
Definition: traced_object.h:174
bool operator==(const TracedMapIterator &other) const
Definition: traced_object.h:182
WrappedT * pointer
Definition: traced_object.h:273
WrappedT value() const
Returns a non-optional traced wrapper, throws if defined() is false.
Definition: traced_object.h:418
Traced wrapper for Map objects.
Definition: traced_object.h:41
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
detail::TracedObjectWrapperSelector< T >::Type GetAttr(T BaseType::*member_ptr) const
Get a traced wrapper for an attribute of the wrapped object.
Definition: traced_object.h:104
TracedObject(const RefT &object_ref, ObjectPath path)
Definition: traced_object.h:92
iterator end() const
Get an iterator to the end of the array.
Definition: traced_object.h:364
std::bidirectional_iterator_tag iterator_category
Definition: traced_object.h:173
WrappedT & reference
Definition: traced_object.h:274
TracedArrayIterator & operator++()
Definition: traced_object.h:280
const Object * get() const
Definition: object.h:546
TracedObject< RefU > Downcast() const
Convert the wrapped reference type to a subtype.
Definition: traced_object.h:136
const ObjectPath & GetPath() const
Get the path of the wrapped object.
Definition: traced_object.h:157
TracedArrayIterator operator--(int)
Definition: traced_object.h:293
detail::TracedObjectWrapperSelector< typename std::invoke_result< F, const T & >::type >::Type ApplyFunc(F &&f) const
Transform the wrapped value without changing its path.
Definition: traced_object.h:454
difference_type operator-(const TracedArrayIterator &rhs) const
Definition: traced_object.h:307
detail::TracedObjectWrapperSelector< RefT >::Type MakeTraced(const RefT &object)
Wrap the given root object in an appropriate traced wrapper class.
Definition: traced_object.h:467
Traced wrapper for Array objects.
Definition: traced_object.h:43
Definition: traced_object.h:52
typename detail::TracedObjectWrapperSelector< T >::Type WrappedT
Definition: traced_object.h:269
typename Map< K, V >::iterator MapIter
Definition: traced_object.h:171
std::random_access_iterator_tag iterator_category
Definition: traced_object.h:275
TracedArrayIterator operator-(difference_type offset) const
Definition: traced_object.h:303
RefT ObjectRefType
Definition: traced_object.h:89
value_type operator*() const
Definition: traced_object.h:313
typename detail::TracedObjectWrapperSelector< V >::Type WrappedV
Definition: traced_object.h:215
A managed object in the TVM runtime.
WrappedT operator[](size_t index) const
Get an element by index, wrapped in a traced wrapper.
Definition: traced_object.h:348
Iterator class for TracedArray<T>
Definition: traced_object.h:267
TracedBasicValue(const T &value, ObjectPath path)
Definition: traced_object.h:436
Definition: object_path.h:122
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics, which means map is mutable but copy will happen when array is referenced in more than two places.
Definition: map.h:1271
TracedMapIterator(MapIter iter, ObjectPath map_path)
Definition: traced_object.h:179
iterator begin() const
Get an iterator to the first array element.
Definition: traced_object.h:357
const ObjectPath & GetPath() const
Get the path of the wrapped value.
Definition: traced_object.h:447
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Optional< String > GetAttrKeyByAddress(const Object *object, const void *attr_address)
Given an object and an address of its attribute, return the key of the attribute. ...
Managed reference to TypeNode.
Definition: type.h:93
const Optional< T > & Get() const
Access the wrapped optional object.
Definition: traced_object.h:403
TracedMapIterator operator++(int)
Definition: traced_object.h:198
Traced wrapper for basic values (i.e. non-TVM objects)
Definition: traced_object.h:47
TracedMap(Map< K, V > map, ObjectPath path)
Definition: traced_object.h:220
iterator begin() const
Get an iterator to the first item of the map.
Definition: traced_object.h:246
const ObjectPath & GetPath() const
Get the path of the wrapped optional object.
Definition: traced_object.h:408
TracedOptional(Optional< T > optional, ObjectPath path)
Definition: traced_object.h:397
constexpr runtime::NullOptType NullOpt
Definition: optional.h:160
Reflection and serialization of compiler IR/AST nodes.
TracedArrayIterator operator++(int)
Definition: traced_object.h:288
bool operator!=(TracedArrayIterator other) const
Definition: traced_object.h:312
Traced wrapper for Optional objects.
Definition: traced_object.h:45
TracedArray(Array< T > array, ObjectPath path)
Definition: traced_object.h:332
bool operator==(TracedArrayIterator other) const
Definition: traced_object.h:309
const Array< T > & Get() const
Access the wrapped array object.
Definition: traced_object.h:338
value_type * pointer
Definition: traced_object.h:176
size_t size() const
Get the size of the wrapped array.
Definition: traced_object.h:374
TracedObject(const TracedObject< DerivedRef > &derived)
Definition: traced_object.h:97
reference operator*() const
Definition: traced_object.h:188
TracedOptional(const WrappedT &value)
Implicit conversion from the corresponding non-optional traced wrapper.
Definition: traced_object.h:392