tvm
runtime.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_RELAY_RUNTIME_H_
25 #define TVM_RELAY_RUNTIME_H_
26 
27 #include <dmlc/registry.h>
28 #include <tvm/ir/attrs.h>
29 #include <tvm/ir/expr.h>
30 #include <tvm/ir/type.h>
31 #include <tvm/ir/type_relation.h>
33 #include <tvm/runtime/registry.h>
34 
35 #include <string>
36 #include <unordered_map>
37 #include <utility>
38 #include <vector>
39 
40 namespace tvm {
41 
42 template <typename, typename>
43 class AttrRegistry;
44 
45 namespace relay {
46 
55 class RuntimeNode : public Object {
56  public:
59  /* \brief Additional attributes storing meta-data about the Runtime. */
61 
81  template <typename TObjectRef>
83  const std::string& attr_key,
84  Optional<TObjectRef> default_value = Optional<TObjectRef>(nullptr)) const {
85  return attrs.GetAttr(attr_key, default_value);
86  }
87  // variant that uses TObjectRef to enable implicit conversion to default value.
88  template <typename TObjectRef>
89  Optional<TObjectRef> GetAttr(const std::string& attr_key, TObjectRef default_value) const {
90  return GetAttr<TObjectRef>(attr_key, Optional<TObjectRef>(default_value));
91  }
92 
94  v->Visit("name", &name);
95  v->Visit("attrs", &attrs);
96  }
97 
98  bool SEqualReduce(const RuntimeNode* other, SEqualReducer equal) const {
99  return name == other->name && equal.DefEqual(attrs, other->attrs);
100  }
101 
102  void SHashReduce(SHashReducer hash_reduce) const {
103  hash_reduce(name);
104  hash_reduce(attrs);
105  }
106 
107  static constexpr const char* _type_key = "Runtime";
109 };
110 
115 class Runtime : public ObjectRef {
116  public:
124  TVM_DLL static Runtime Create(String name, Map<String, ObjectRef> attrs);
125 
130  TVM_DLL static Array<String> ListRuntimes();
131 
137  TVM_DLL static Map<String, String> ListRuntimeOptions(const String& name);
138 
141 
142  private:
148  TVM_DLL Runtime(String name, DictAttrs attrs) {
149  auto n = make_object<RuntimeNode>();
150  n->name = std::move(name);
151  n->attrs = std::move(attrs);
152  data_ = std::move(n);
153  }
154 };
155 
161  public:
163  inline RuntimeRegEntry& set_name();
164 
170  template <typename ValueType>
171  inline RuntimeRegEntry& add_attr_option(const String& key);
172 
179  template <typename ValueType>
180  inline RuntimeRegEntry& add_attr_option(const String& key, ObjectRef default_value);
181 
187  TVM_DLL static RuntimeRegEntry& RegisterOrGet(const String& name);
188 
189  private:
191  struct ValueTypeInfo {
192  std::string type_key;
193  uint32_t type_index;
194  };
195  std::unordered_map<std::string, ValueTypeInfo> key2vtype_;
197  std::unordered_map<String, ObjectRef> key2default_;
198 
200  uint32_t index_;
201 
202  // the name
203  std::string name;
204 
206  uint32_t AttrRegistryIndex() const { return index_; }
208  String AttrRegistryName() const { return name; }
209 
211  explicit RuntimeRegEntry(uint32_t reg_index) : index_(reg_index) {}
212 
213  // friend class
214  template <typename>
216  template <typename, typename>
217  friend class tvm::AttrRegistry;
218  friend class Runtime;
219 };
220 
222  if (name.empty()) {
223  name = name;
224  }
225  return *this;
226 }
227 
228 template <typename ValueType>
230  ICHECK(!key2vtype_.count(key)) << "AttributeError: add_attr_option failed because '" << key
231  << "' has been set once";
232 
233  using ValueNodeType = typename ValueType::ContainerType;
234  // NOTE: we could further update the function later.
235  uint32_t value_type_index = ValueNodeType::_GetOrAllocRuntimeTypeIndex();
236 
237  ValueTypeInfo info;
238  info.type_index = value_type_index;
239  info.type_key = runtime::Object::TypeIndex2Key(value_type_index);
240  key2vtype_[key] = info;
241  return *this;
242 }
243 
244 template <typename ValueType>
246  ObjectRef default_value) {
247  add_attr_option<ValueType>(key);
248  key2default_[key] = default_value;
249  return *this;
250 }
251 
252 // internal macros to make Runtime entries
253 #define TVM_RUNTIME_REGISTER_VAR_DEF \
254  static DMLC_ATTRIBUTE_UNUSED ::tvm::relay::RuntimeRegEntry& __make_##Runtime
255 
270 #define TVM_REGISTER_RUNTIME(RuntimeName) \
271  TVM_STR_CONCAT(TVM_RUNTIME_REGISTER_VAR_DEF, __COUNTER__) = \
272  ::tvm::relay::RuntimeRegEntry::RegisterOrGet(RuntimeName).set_name()
273 } // namespace relay
274 } // namespace tvm
275 
276 #endif // TVM_RELAY_RUNTIME_H_
static constexpr const char * _type_key
Definition: runtime.h:107
bool DefEqual(const ObjectRef &lhs, const ObjectRef &rhs)
Reduce condition to comparison of two definitions, where free vars can be mapped. ...
Definition: structural_equal.h:165
void VisitAttrs(AttrVisitor *v)
Definition: runtime.h:93
RuntimeRegEntry & set_name()
Set name of the Runtime to be the same as registry if it is empty.
Definition: runtime.h:221
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:102
Base expr nodes in TVM.
DictAttrs attrs
Definition: runtime.h:60
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:101
Type relation and function for type inference(checking).
RuntimeRegEntry & add_attr_option(const String &key)
Register a valid configuration option and its ValueType for validation.
Definition: runtime.h:229
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
Managed reference to DictAttrsNode.
Definition: attrs.h:227
base class of all object containers.
Definition: object.h:165
Definition: executor.h:43
Helpers for attribute objects.
bool SEqualReduce(const RuntimeNode *other, SEqualReducer equal) const
Definition: runtime.h:98
Generic attribute map.
Definition: attr_registry_map.h:38
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
String name
name of the Runtime
Definition: runtime.h:58
IR/AST nodes for the unified type system in TVM.
Helper structure to register Runtimes.
Definition: runtime.h:160
Optional< TObjectRef > GetAttr(const std::string &attr_key, Optional< TObjectRef > default_value=Optional< TObjectRef >(nullptr)) const
Get a function attribute.
Definition: attrs.h:259
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:270
Optional< TObjectRef > GetAttr(const std::string &attr_key, TObjectRef default_value) const
Definition: runtime.h:89
Reference to string objects.
Definition: string.h:129
Runtime information.
Definition: runtime.h:55
Managed reference class to RuntimeNode.
Definition: runtime.h:115
Base class of all object reference.
Definition: object.h:504
Attribute map used in registry.
uint32_t type_index() const
Definition: object.h:173
TVM_DECLARE_FINAL_OBJECT_INFO(RuntimeNode, Object)
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:1235
static std::string TypeIndex2Key(uint32_t tindex)
Get the type key of the corresponding index from runtime.
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
bool empty() const
Retun if the string is empty.
Definition: string.h:239
Optional< TObjectRef > GetAttr(const std::string &attr_key, Optional< TObjectRef > default_value=Optional< TObjectRef >(nullptr)) const
Get an attribute.
Definition: runtime.h:82
#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:721
void SHashReduce(SHashReducer hash_reduce) const
Definition: runtime.h:102
This file defines the TVM global function registry.