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 
29 #include <tvm/runtime/data_type.h>
30 #include <tvm/runtime/ndarray.h>
31 #include <tvm/runtime/object.h>
32 
33 #include <memory>
34 #include <string>
35 #include <utility>
36 #include <vector>
37 
38 namespace tvm {
39 namespace runtime {
40 namespace metadata {
41 
49  public:
50  virtual const char* get_c_struct_name() const = 0;
51 
52  static constexpr const char* _type_key = "metadata.MetadataBaseNode";
54 };
55 
58  public:
60 };
61 
62 template <typename C, class Ref>
63 class ArrayAccessor;
64 
66 template <typename C, class Ref>
68  public:
69  ArrayIterator(size_t index, const ArrayAccessor<C, Ref>* parent)
70  : index_{index}, parent_{parent} {}
71 
72  inline Ref operator*() { return (*parent_)[index_]; }
73 
75  if (index_ < parent_->size()) {
76  index_++;
77  }
78 
79  return *this;
80  }
81 
82  inline bool operator==(const ArrayIterator<C, Ref>& other) const {
83  return parent_ == other.parent_ && index_ == other.index_;
84  }
85 
86  inline bool operator!=(const ArrayIterator<C, Ref>& other) const { return !operator==(other); }
87 
88  private:
89  size_t index_;
90  const ArrayAccessor<C, Ref>* parent_;
91 };
92 
97 template <typename C, class Ref>
99  public:
100  using value_type = Ref;
103 
104  template <typename T = typename std::enable_if<std::is_base_of<ObjectRef, Ref>::value>::type>
105  ArrayAccessor(const C* data, size_t num_data) : data_{data}, num_data_{num_data} {}
106 
107  inline size_t size() const { return num_data_; }
108 
109  inline Ref operator[](size_t index) const {
110  if (index >= num_data_) {
111  throw std::runtime_error("Index out of range");
112  }
113 
114  return Ref(&data_[index]);
115  }
116 
117  inline ArrayIterator<C, Ref> begin() const { return ArrayIterator<C, Ref>{0, this}; }
118 
119  inline ArrayIterator<C, Ref> end() const { return ArrayIterator<C, Ref>{num_data_, this}; }
120 
121  private:
122  const C* data_;
123  size_t num_data_;
124 };
125 
130 template <>
132  public:
136 
137  ArrayAccessor(const char** data, size_t num_data) : data_{data}, num_data_{num_data} {}
138 
139  inline size_t size() const { return num_data_; }
140 
141  inline ::tvm::runtime::String operator[](size_t index) const {
142  if (index >= num_data_) {
143  throw std::runtime_error("Index out of range");
144  }
145  return ::tvm::runtime::String(data_[index]);
146  }
147 
150  }
151 
154  }
155 
156  private:
157  const char** data_;
158  size_t num_data_;
159 };
160 
165 enum MetadataKind : uint8_t {
166  kUint64 = 0,
167  kInt64 = 1,
168  kBool = 2,
169  kString = 3,
170  kHandle = 4,
172 };
173 
180  public:
182  : array(::std::move(array)), kind{kind}, type_key{type_key} {}
183 
184  const char* get_c_struct_name() const final;
185 
186  std::string get_element_c_struct_name() const {
187  CHECK(kind == MetadataKind::kMetadata)
188  << "cannot get struct name for MetadataArray with kind=" << kind;
189  constexpr int prefix_size = sizeof("metadata.") - 1;
190  constexpr int suffix_size = sizeof("Node") - 1;
191  std::string type_key_str(type_key);
192  return std::string("TVM") +
193  type_key_str.substr(prefix_size, type_key_str.size() - prefix_size - suffix_size);
194  }
195 
197 
200 
202  const char* type_key;
203 
204  static constexpr const char* _type_key = "metadata.MetadataArrayNode";
206 };
207 
209 class MetadataArray : public MetadataBase {
210  public:
211  MetadataArray(Array<ObjectRef> array, MetadataKind kind, const char* struct_name);
212 
214 };
215 
216 } // namespace metadata
217 } // namespace runtime
218 } // namespace tvm
219 
220 #endif // TVM_RUNTIME_METADATA_BASE_H_
Runtime Array container types.
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Base class of all object reference.
Definition: object.h:519
base class of all object containers.
Definition: object.h:171
Reference to string objects.
Definition: string.h:98
ArrayAccessor(const char **data, size_t num_data)
Definition: metadata_base.h:137
inline ::tvm::runtime::String operator[](size_t index) const
Definition: metadata_base.h:141
ArrayIterator< const char *, ::tvm::runtime::String > begin() const
Definition: metadata_base.h:148
ArrayIterator< const char *, ::tvm::runtime::String > end() const
Definition: metadata_base.h:152
A span-like class which permits access to Array fields with complex elements. These array fields shou...
Definition: metadata_base.h:98
Ref value_type
Definition: metadata_base.h:100
ArrayIterator< C, Ref > end() const
Definition: metadata_base.h:119
ArrayIterator< C, Ref > begin() const
Definition: metadata_base.h:117
Ref operator[](size_t index) const
Definition: metadata_base.h:109
ArrayIterator< C, Ref > iterator
Definition: metadata_base.h:101
ArrayAccessor(const C *data, size_t num_data)
Definition: metadata_base.h:105
size_t size() const
Definition: metadata_base.h:107
An iterator implementation that lazily instantiates the C++ wrapping Metadata class.
Definition: metadata_base.h:67
bool operator!=(const ArrayIterator< C, Ref > &other) const
Definition: metadata_base.h:86
bool operator==(const ArrayIterator< C, Ref > &other) const
Definition: metadata_base.h:82
Ref operator*()
Definition: metadata_base.h:72
ArrayIterator< C, Ref > & operator++()
Definition: metadata_base.h:74
ArrayIterator(size_t index, const ArrayAccessor< C, Ref > *parent)
Definition: metadata_base.h:69
Container for arrays in the metadata.
Definition: metadata_base.h:179
const char * get_c_struct_name() const final
const char * type_key
When kind is Metadata, type_key of the MetadataBaseNode used with this array.
Definition: metadata_base.h:202
MetadataKind kind
Describes the storage class of the emitted struct member.
Definition: metadata_base.h:199
MetadataArrayNode(Array< ObjectRef > array, MetadataKind kind, const char *type_key)
Definition: metadata_base.h:181
TVM_DECLARE_BASE_OBJECT_INFO(MetadataArrayNode, MetadataBaseNode)
static constexpr const char * _type_key
Definition: metadata_base.h:204
Array< ObjectRef > array
Definition: metadata_base.h:196
std::string get_element_c_struct_name() const
Definition: metadata_base.h:186
Reference class for MetadataArray.
Definition: metadata_base.h:209
MetadataArray(Array< ObjectRef > array, MetadataKind kind, const char *struct_name)
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(MetadataArray, MetadataBase, MetadataArrayNode)
Common base class for all Metadata.
Definition: metadata_base.h:48
virtual const char * get_c_struct_name() const =0
TVM_DECLARE_BASE_OBJECT_INFO(MetadataBaseNode, ::tvm::runtime::Object)
static constexpr const char * _type_key
Definition: metadata_base.h:52
Reference class for the common MetadataBaseNode class.
Definition: metadata_base.h:57
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(MetadataBase, ::tvm::runtime::ObjectRef, MetadataBaseNode)
MetadataKind
Enumerates the primitive types which can be part of a Metadata instance.
Definition: metadata_base.h:165
@ kHandle
Definition: metadata_base.h:170
@ kMetadata
Definition: metadata_base.h:171
@ kInt64
Definition: metadata_base.h:167
@ kString
Definition: metadata_base.h:169
@ kBool
Definition: metadata_base.h:168
@ kUint64
Definition: metadata_base.h:166
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
A device-independent managed NDArray abstraction.
A managed object in the TVM runtime.
Runtime String container types.