tvm
metadata_base.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 
24 #ifndef TVM_RUNTIME_METADATA_BASE_H_
25 #define TVM_RUNTIME_METADATA_BASE_H_
26 
27 #include <tvm/ir/expr.h>
28 #include <tvm/runtime/object.h>
29 
30 #include <memory>
31 #include <string>
32 #include <utility>
33 #include <vector>
34 
35 namespace tvm {
36 namespace runtime {
37 namespace metadata {
38 
46  public:
47  virtual const char* get_c_struct_name() const = 0;
48 
49  static constexpr const char* _type_key = "metadata.MetadataBaseNode";
51 };
52 
55  public:
57 };
58 
59 template <typename C, class Ref>
61 
63 template <typename C, class Ref>
65  public:
66  ArrayIterator(size_t index, const ArrayAccessor<C, Ref>* parent)
67  : index_{index}, parent_{parent} {}
68 
69  inline Ref operator*() { return (*parent_)[index_]; }
70 
72  if (index_ < parent_->size()) {
73  index_++;
74  }
75 
76  return *this;
77  }
78 
79  inline bool operator==(const ArrayIterator<C, Ref>& other) const {
80  return parent_ == other.parent_ && index_ == other.index_;
81  }
82 
83  inline bool operator!=(const ArrayIterator<C, Ref>& other) const { return !operator==(other); }
84 
85  private:
86  size_t index_;
87  const ArrayAccessor<C, Ref>* parent_;
88 };
89 
94 template <typename C, class Ref>
95 class ArrayAccessor {
96  public:
97  using value_type = Ref;
100 
101  template <typename T = typename std::enable_if<std::is_base_of<ObjectRef, Ref>::value>::type>
102  ArrayAccessor(const C* data, size_t num_data) : data_{data}, num_data_{num_data} {}
103 
104  inline size_t size() const { return num_data_; }
105 
106  inline Ref operator[](size_t index) const {
107  if (index >= num_data_) {
108  throw std::runtime_error("Index out of range");
109  }
110 
111  return Ref(&data_[index]);
112  }
113 
114  inline ArrayIterator<C, Ref> begin() const { return ArrayIterator<C, Ref>{0, this}; }
115 
116  inline ArrayIterator<C, Ref> end() const { return ArrayIterator<C, Ref>{num_data_, this}; }
117 
118  private:
119  const C* data_;
120  size_t num_data_;
121 };
122 
127 template <>
129  public:
133 
134  ArrayAccessor(const char** data, size_t num_data) : data_{data}, num_data_{num_data} {}
135 
136  inline size_t size() const { return num_data_; }
137 
138  inline ::tvm::runtime::String operator[](size_t index) const {
139  if (index >= num_data_) {
140  throw std::runtime_error("Index out of range");
141  }
142  return ::tvm::runtime::String(data_[index]);
143  }
144 
147  }
148 
151  }
152 
153  private:
154  const char** data_;
155  size_t num_data_;
156 };
157 
162 enum MetadataKind : uint8_t {
163  kUint64 = 0,
164  kInt64 = 1,
165  kBool = 2,
166  kString = 3,
167  kHandle = 4,
169 };
170 
177  public:
178  MetadataArrayNode(Array<ObjectRef> array, MetadataKind kind, const char* type_key)
179  : array(::std::move(array)), kind{kind}, type_key{type_key} {}
180 
181  const char* get_c_struct_name() const final;
182 
183  std::string get_element_c_struct_name() const {
184  CHECK(kind == MetadataKind::kMetadata)
185  << "cannot get struct name for MetadataArray with kind=" << kind;
186  constexpr int prefix_size = sizeof("metadata.") - 1;
187  constexpr int suffix_size = sizeof("Node") - 1;
188  std::string type_key_str(type_key);
189  return std::string("TVM") +
190  type_key_str.substr(prefix_size, type_key_str.size() - prefix_size - suffix_size);
191  }
192 
194 
197 
199  const char* type_key;
200 
201  static constexpr const char* _type_key = "metadata.MetadataArrayNode";
203 };
204 
206 class MetadataArray : public MetadataBase {
207  public:
208  MetadataArray(Array<ObjectRef> array, MetadataKind kind, const char* struct_name);
209 
211 };
212 
213 } // namespace metadata
214 } // namespace runtime
215 } // namespace tvm
216 
217 #endif // TVM_RUNTIME_METADATA_BASE_H_
MetadataKind kind
Describes the storage class of the emitted struct member.
Definition: metadata_base.h:196
Definition: metadata_base.h:164
An iterator implementation that lazily instantiates the C++ wrapping Metadata class.
Definition: metadata_base.h:64
Common base class for all Metadata.
Definition: metadata_base.h:45
Base expr nodes in TVM.
Reference class for MetadataArray.
Definition: metadata_base.h:206
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
ArrayIterator< const char *, ::tvm::runtime::String > begin() const
Definition: metadata_base.h:145
Definition: metadata_base.h:168
Definition: loop_state.h:456
static constexpr const char * _type_key
Definition: metadata_base.h:49
ArrayIterator< C, Ref > begin() const
Definition: metadata_base.h:114
base class of all object containers.
Definition: object.h:167
std::string get_element_c_struct_name() const
Definition: metadata_base.h:183
#define TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:744
const char * type_key
When kind is Metadata, type_key of the MetadataBaseNode used with this array.
Definition: metadata_base.h:199
Ref operator*()
Definition: metadata_base.h:69
ArrayAccessor(const char **data, size_t num_data)
Definition: metadata_base.h:134
ArrayIterator< C, Ref > end() const
Definition: metadata_base.h:116
TVM_DECLARE_BASE_OBJECT_INFO(MetadataBaseNode, ::tvm::runtime::Object)
Definition: metadata_base.h:166
Ref value_type
Definition: metadata_base.h:97
bool operator==(const ArrayIterator< C, Ref > &other) const
Definition: metadata_base.h:79
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Container for arrays in the metadata.
Definition: metadata_base.h:176
MetadataKind
Enumerates the primitive types which can be part of a Metadata instance.
Definition: metadata_base.h:162
ArrayIterator(size_t index, const ArrayAccessor< C, Ref > *parent)
Definition: metadata_base.h:66
Reference to string objects.
Definition: string.h:97
ArrayAccessor(const C *data, size_t num_data)
Definition: metadata_base.h:102
A span-like class which permits access to Array fields with complex elements. These array fields shou...
Definition: metadata_base.h:60
Definition: metadata_base.h:163
size_t size() const
Definition: metadata_base.h:104
Base class of all object reference.
Definition: object.h:511
virtual const char * get_c_struct_name() const =0
Definition: metadata_base.h:165
MetadataArrayNode(Array< ObjectRef > array, MetadataKind kind, const char *type_key)
Definition: metadata_base.h:178
Ref operator[](size_t index) const
Definition: metadata_base.h:106
bool operator!=(const ArrayIterator< C, Ref > &other) const
Definition: metadata_base.h:83
A managed object in the TVM runtime.
inline ::tvm::runtime::String operator[](size_t index) const
Definition: metadata_base.h:138
ArrayIterator< const char *, ::tvm::runtime::String > end() const
Definition: metadata_base.h:149
ArrayIterator< C, Ref > & operator++()
Definition: metadata_base.h:71
bool operator==(const String &lhs, const std::string &rhs)
Definition: string.h:407
Definition: metadata_base.h:167
Reference class for the common MetadataBaseNode class.
Definition: metadata_base.h:54
Array< ObjectRef > array
Definition: metadata_base.h:193