tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
object_path.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  */
19 
26 #ifndef TVM_NODE_OBJECT_PATH_H_
27 #define TVM_NODE_OBJECT_PATH_H_
28 
31 #include <tvm/runtime/object.h>
32 
33 #include <string>
34 
35 namespace tvm {
36 
37 using runtime::Object;
38 using runtime::ObjectPtr;
39 using runtime::ObjectRef;
40 
41 class ObjectPath;
42 
55 class ObjectPathNode : public Object {
56  public:
64  int32_t Length() const;
65 
71  ObjectPath GetPrefix(int32_t length) const;
72 
78  bool IsPrefixOf(const ObjectPath& other) const;
79 
81  bool PathsEqual(const ObjectPath& other) const;
82 
84  ObjectPath Attr(const char* attr_key) const;
85 
87  ObjectPath Attr(Optional<String> attr_key) const;
88 
90  ObjectPath ArrayIndex(int32_t index) const;
91 
93  ObjectPath MissingArrayElement(int32_t index) const;
94 
96  ObjectPath MapValue(ObjectRef key) const;
97 
100 
101  static constexpr const char* _type_key = "ObjectPath";
103 
104  protected:
105  explicit ObjectPathNode(const ObjectPathNode* parent);
106 
107  friend class ObjectPath;
108  friend std::string GetObjectPathRepr(const ObjectPathNode* node);
109 
110  const ObjectPathNode* ParentNode() const;
111 
113  virtual bool LastNodeEqual(const ObjectPathNode* other) const = 0;
114 
115  virtual std::string LastNodeString() const = 0;
116 
117  private:
118  Optional<ObjectRef> parent_;
119  int32_t length_;
120 };
121 
122 class ObjectPath : public ObjectRef {
123  public:
125  static ObjectPath Root();
126 
128 };
129 
130 //-------------------------------------------------------------------------
131 //----- Concrete object path nodes ------------------------------------
132 //-------------------------------------------------------------------------
133 
134 // ----- Root -----
135 
136 class RootPathNode final : public ObjectPathNode {
137  public:
138  explicit RootPathNode();
139 
140  static constexpr const char* _type_key = "RootPath";
142 
143  protected:
144  bool LastNodeEqual(const ObjectPathNode* other) const final;
145  std::string LastNodeString() const final;
146 };
147 
148 class RootPath : public ObjectPath {
149  public:
151 };
152 
153 // ----- Attribute access -----
154 
156  public:
159 
160  explicit AttributeAccessPathNode(const ObjectPathNode* parent, String attr_key);
161 
162  static constexpr const char* _type_key = "AttributeAccessPath";
164 
165  protected:
166  bool LastNodeEqual(const ObjectPathNode* other) const final;
167  std::string LastNodeString() const final;
168 };
169 
171  public:
174 };
175 
176 // ----- Unknown attribute access -----
177 
179  public:
180  explicit UnknownAttributeAccessPathNode(const ObjectPathNode* parent);
181 
182  static constexpr const char* _type_key = "UnknownAttributeAccessPath";
184 
185  protected:
186  bool LastNodeEqual(const ObjectPathNode* other) const final;
187  std::string LastNodeString() const final;
188 };
189 
191  public:
194 };
195 
196 // ----- Array element access by index -----
197 
199  public:
201  int32_t index;
202 
203  explicit ArrayIndexPathNode(const ObjectPathNode* parent, int32_t index);
204 
205  static constexpr const char* _type_key = "ArrayIndexPath";
207 
208  protected:
209  bool LastNodeEqual(const ObjectPathNode* other) const final;
210  std::string LastNodeString() const final;
211 };
212 
213 class ArrayIndexPath : public ObjectPath {
214  public:
216 };
217 
218 // ----- Missing array element -----
219 
221  public:
223  int32_t index;
224 
225  explicit MissingArrayElementPathNode(const ObjectPathNode* parent, int32_t index);
226 
227  static constexpr const char* _type_key = "MissingArrayElementPath";
229 
230  protected:
231  bool LastNodeEqual(const ObjectPathNode* other) const final;
232  std::string LastNodeString() const final;
233 };
234 
236  public:
239 };
240 
241 // ----- Map value -----
242 
244  public:
247 
248  explicit MapValuePathNode(const ObjectPathNode* parent, ObjectRef key);
249 
250  static constexpr const char* _type_key = "MapValuePath";
252 
253  protected:
254  bool LastNodeEqual(const ObjectPathNode* other) const final;
255  std::string LastNodeString() const final;
256 };
257 
258 class MapValuePath : public ObjectPath {
259  public:
261 };
262 
263 // ----- Missing map entry -----
264 
266  public:
267  explicit MissingMapEntryPathNode(const ObjectPathNode* parent);
268 
269  static constexpr const char* _type_key = "MissingMapEntryPath";
271 
272  protected:
273  bool LastNodeEqual(const ObjectPathNode* other) const final;
274  std::string LastNodeString() const final;
275 };
276 
278  public:
281 };
282 
283 } // namespace tvm
284 
285 #endif // TVM_NODE_OBJECT_PATH_H_
String attr_key
Name of the attribute being accessed. Must be a static string.
Definition: object_path.h:158
int32_t index
Index of the array element that is missing.
Definition: object_path.h:223
Optional< ObjectPath > GetParent() const
Get the parent path.
Runtime Optional container types.
ObjectPath MissingArrayElement(int32_t index) const
Extend this path with access to a missing array element.
Runtime String container types.
Definition: object_path.h:148
Definition: object_path.h:243
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
bool PathsEqual(const ObjectPath &other) const
Check if two paths are equal.
ObjectRef key
Key of the map entry that is being accessed.
Definition: object_path.h:246
Definition: object_path.h:170
Definition: object_path.h:265
Definition: object_path.h:213
ObjectPath ArrayIndex(int32_t index) const
Extend this path with access to an array element.
Definition: object_path.h:258
base class of all object containers.
Definition: object.h:167
virtual bool LastNodeEqual(const ObjectPathNode *other) const =0
Definition: object_path.h:136
Definition: object_path.h:155
Definition: object_path.h:220
ObjectPath MissingMapEntry() const
Extend this path with access to a missing map entry.
friend std::string GetObjectPathRepr(const ObjectPathNode *node)
int32_t Length() const
Get the length of the path.
Path to an object from some root object.
Definition: object_path.h:55
ObjectPathNode(const ObjectPathNode *parent)
Definition: object_path.h:190
ObjectPath Attr(const char *attr_key) const
Extend this path with access to an object attribute.
Definition: object_path.h:235
Reference to string objects.
Definition: string.h:98
Definition: object_path.h:178
virtual std::string LastNodeString() const =0
Base class of all object reference.
Definition: object.h:511
Definition: object_path.h:277
int32_t index
Index of the array element that is being accessed.
Definition: object_path.h:201
static constexpr const char * _type_key
Definition: object_path.h:101
A managed object in the TVM runtime.
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:671
Definition: object_path.h:122
ObjectPath MapValue(ObjectRef key) const
Extend this path with access to a map value.
TVM_DECLARE_BASE_OBJECT_INFO(ObjectPathNode, Object)
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Definition: object_path.h:198
bool IsPrefixOf(const ObjectPath &other) const
Check if this path is a prefix of another path.
#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:728
ObjectPath GetPrefix(int32_t length) const
Get a path prefix of the given length.
const ObjectPathNode * ParentNode() const