tvm
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 
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:
126 
128 };
129 
130 //-------------------------------------------------------------------------
131 //----- Concrete object path nodes ------------------------------------
132 //-------------------------------------------------------------------------
133 
134 // ----- Root -----
135 
136 class RootPathNode final : public ObjectPathNode {
137  public:
139 
141 
142  static constexpr const char* _type_key = "RootPath";
144 
145  protected:
146  bool LastNodeEqual(const ObjectPathNode* other) const final;
147  std::string LastNodeString() const final;
148 };
149 
150 class RootPath : public ObjectPath {
151  public:
153 };
154 
155 // ----- Attribute access -----
156 
158  public:
161 
162  explicit AttributeAccessPathNode(const ObjectPathNode* parent, String attr_key);
163 
164  static constexpr const char* _type_key = "AttributeAccessPath";
166 
167  protected:
168  bool LastNodeEqual(const ObjectPathNode* other) const final;
169  std::string LastNodeString() const final;
170 };
171 
173  public:
176 };
177 
178 // ----- Unknown attribute access -----
179 
181  public:
183 
184  static constexpr const char* _type_key = "UnknownAttributeAccessPath";
186 
187  protected:
188  bool LastNodeEqual(const ObjectPathNode* other) const final;
189  std::string LastNodeString() const final;
190 };
191 
193  public:
196 };
197 
198 // ----- Array element access by index -----
199 
201  public:
203  int32_t index;
204 
205  explicit ArrayIndexPathNode(const ObjectPathNode* parent, int32_t index);
206 
207  static constexpr const char* _type_key = "ArrayIndexPath";
209 
210  protected:
211  bool LastNodeEqual(const ObjectPathNode* other) const final;
212  std::string LastNodeString() const final;
213 };
214 
215 class ArrayIndexPath : public ObjectPath {
216  public:
218 };
219 
220 // ----- Missing array element -----
221 
223  public:
225  int32_t index;
226 
227  explicit MissingArrayElementPathNode(const ObjectPathNode* parent, int32_t index);
228 
229  static constexpr const char* _type_key = "MissingArrayElementPath";
231 
232  protected:
233  bool LastNodeEqual(const ObjectPathNode* other) const final;
234  std::string LastNodeString() const final;
235 };
236 
238  public:
241 };
242 
243 // ----- Map value -----
244 
246  public:
249 
250  explicit MapValuePathNode(const ObjectPathNode* parent, ObjectRef key);
251 
252  static constexpr const char* _type_key = "MapValuePath";
254 
255  protected:
256  bool LastNodeEqual(const ObjectPathNode* other) const final;
257  std::string LastNodeString() const final;
258 };
259 
260 class MapValuePath : public ObjectPath {
261  public:
263 };
264 
265 // ----- Missing map entry -----
266 
268  public:
269  explicit MissingMapEntryPathNode(const ObjectPathNode* parent);
270 
271  static constexpr const char* _type_key = "MissingMapEntryPath";
273 
274  protected:
275  bool LastNodeEqual(const ObjectPathNode* other) const final;
276  std::string LastNodeString() const final;
277 };
278 
280  public:
283 };
284 
285 } // namespace tvm
286 
287 #endif // TVM_NODE_OBJECT_PATH_H_
Definition: object_path.h:200
bool LastNodeEqual(const ObjectPathNode *other) const final
TVM_DECLARE_FINAL_OBJECT_INFO(ArrayIndexPathNode, ObjectPathNode)
int32_t index
Index of the array element that is being accessed.
Definition: object_path.h:203
ArrayIndexPathNode(const ObjectPathNode *parent, int32_t index)
std::string LastNodeString() const final
Definition: object_path.h:215
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(ArrayIndexPath, ObjectPath, ArrayIndexPathNode)
Definition: object_path.h:157
bool LastNodeEqual(const ObjectPathNode *other) const final
String attr_key
Name of the attribute being accessed. Must be a static string.
Definition: object_path.h:160
AttributeAccessPathNode(const ObjectPathNode *parent, String attr_key)
TVM_DECLARE_FINAL_OBJECT_INFO(AttributeAccessPathNode, ObjectPathNode)
std::string LastNodeString() const final
Definition: object_path.h:172
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(AttributeAccessPath, ObjectPath, AttributeAccessPathNode)
Definition: object_path.h:245
TVM_DECLARE_FINAL_OBJECT_INFO(MapValuePathNode, ObjectPathNode)
std::string LastNodeString() const final
ObjectRef key
Key of the map entry that is being accessed.
Definition: object_path.h:248
bool LastNodeEqual(const ObjectPathNode *other) const final
MapValuePathNode(const ObjectPathNode *parent, ObjectRef key)
Definition: object_path.h:260
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(MapValuePath, ObjectPath, MapValuePathNode)
Definition: object_path.h:222
TVM_DECLARE_FINAL_OBJECT_INFO(MissingArrayElementPathNode, ObjectPathNode)
int32_t index
Index of the array element that is missing.
Definition: object_path.h:225
MissingArrayElementPathNode(const ObjectPathNode *parent, int32_t index)
bool LastNodeEqual(const ObjectPathNode *other) const final
std::string LastNodeString() const final
Definition: object_path.h:237
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(MissingArrayElementPath, ObjectPath, MissingArrayElementPathNode)
Definition: object_path.h:267
bool LastNodeEqual(const ObjectPathNode *other) const final
TVM_DECLARE_FINAL_OBJECT_INFO(MissingMapEntryPathNode, ObjectPathNode)
std::string LastNodeString() const final
MissingMapEntryPathNode(const ObjectPathNode *parent)
Definition: object_path.h:279
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(MissingMapEntryPath, ObjectPath, MissingMapEntryPathNode)
Path to an object from some root object.
Definition: object_path.h:55
friend std::string GetObjectPathRepr(const ObjectPathNode *node)
ObjectPathNode(const ObjectPathNode *parent)
bool IsPrefixOf(const ObjectPath &other) const
Check if this path is a prefix of another path.
Optional< ObjectPath > GetParent() const
Get the parent path.
const ObjectPathNode * ParentNode() const
ObjectPath MissingMapEntry() const
Extend this path with access to a missing map entry.
TVM_DECLARE_BASE_OBJECT_INFO(ObjectPathNode, Object)
ObjectPath MapValue(ObjectRef key) const
Extend this path with access to a map value.
int32_t Length() const
Get the length of the path.
ObjectPath Attr(const char *attr_key) const
Extend this path with access to an object attribute.
virtual std::string LastNodeString() const =0
ObjectPath GetPrefix(int32_t length) const
Get a path prefix of the given length.
virtual bool LastNodeEqual(const ObjectPathNode *other) const =0
ObjectPath Attr(Optional< String > attr_key) const
Extend this path with access to an object attribute.
ObjectPath MissingArrayElement(int32_t index) const
Extend this path with access to a missing array element.
static constexpr const char * _type_key
Definition: object_path.h:101
ObjectPath ArrayIndex(int32_t index) const
Extend this path with access to an array element.
bool PathsEqual(const ObjectPath &other) const
Check if two paths are equal.
Definition: object_path.h:122
static ObjectPath Root(Optional< String > name=NullOpt)
Create a path that represents the root object itself.
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(ObjectPath, ObjectRef, ObjectPathNode)
Definition: object_path.h:136
Optional< String > name
Definition: object_path.h:138
RootPathNode(Optional< String > name=NullOpt)
TVM_DECLARE_FINAL_OBJECT_INFO(RootPathNode, ObjectPathNode)
std::string LastNodeString() const final
static constexpr const char * _type_key
Definition: object_path.h:142
bool LastNodeEqual(const ObjectPathNode *other) const final
Definition: object_path.h:150
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(RootPath, ObjectPath, RootPathNode)
Definition: object_path.h:180
bool LastNodeEqual(const ObjectPathNode *other) const final
UnknownAttributeAccessPathNode(const ObjectPathNode *parent)
TVM_DECLARE_FINAL_OBJECT_INFO(UnknownAttributeAccessPathNode, ObjectPathNode)
std::string LastNodeString() const final
Definition: object_path.h:192
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(UnknownAttributeAccessPath, ObjectPath, UnknownAttributeAccessPathNode)
Base class of all object reference.
Definition: object.h:519
base class of all object containers.
Definition: object.h:171
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Reference to string objects.
Definition: string.h:98
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
constexpr runtime::NullOptType NullOpt
Definition: optional.h:169
A managed object in the TVM runtime.
Runtime Optional container types.
Runtime String container types.