tvm
executor.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_EXECUTOR_H_
25 #define TVM_RELAY_EXECUTOR_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>
44 
45 namespace relay {
46 
55 class ExecutorNode : public Object {
56  public:
59  /* \brief Additional attributes storing meta-data about the Executor. */
61 
67  return name == "aot" || GetAttr<Bool>("link-params").value_or(Bool(false));
68  }
69 
89  template <typename TObjectRef>
91  const std::string& attr_key,
92  Optional<TObjectRef> default_value = Optional<TObjectRef>(nullptr)) const {
93  return attrs.GetAttr(attr_key, default_value);
94  }
95  // variant that uses TObjectRef to enable implicit conversion to default value.
96  template <typename TObjectRef>
97  Optional<TObjectRef> GetAttr(const std::string& attr_key, TObjectRef default_value) const {
98  return GetAttr<TObjectRef>(attr_key, Optional<TObjectRef>(default_value));
99  }
100 
102  v->Visit("name", &name);
103  v->Visit("attrs", &attrs);
104  }
105 
106  bool SEqualReduce(const ExecutorNode* other, SEqualReducer equal) const {
107  return name == other->name && equal.DefEqual(attrs, other->attrs);
108  }
109 
110  void SHashReduce(SHashReducer hash_reduce) const {
111  hash_reduce(name);
112  hash_reduce(attrs);
113  }
114 
115  static constexpr const char* _type_key = "Executor";
116  static constexpr const bool _type_has_method_sequal_reduce = true;
117  static constexpr const bool _type_has_method_shash_reduce = true;
119 };
120 
125 class Executor : public ObjectRef {
126  public:
134  TVM_DLL static Executor Create(String name, Map<String, ObjectRef> attrs = {});
135 
140  TVM_DLL static Array<String> ListExecutors();
141 
147  TVM_DLL static Map<String, String> ListExecutorOptions(const String& name);
148 
152 
153  private:
159  TVM_DLL Executor(String name, DictAttrs attrs) {
160  auto n = make_object<ExecutorNode>();
161  n->name = std::move(name);
162  n->attrs = std::move(attrs);
163  data_ = std::move(n);
164  }
165 };
166 
172  public:
178  template <typename ValueType>
179  inline ExecutorRegEntry& add_attr_option(const String& key);
180 
187  template <typename ValueType>
188  inline ExecutorRegEntry& add_attr_option(const String& key, ObjectRef default_value);
189 
195  TVM_DLL static ExecutorRegEntry& RegisterOrGet(const String& name);
196 
197  private:
199  struct ValueTypeInfo {
200  std::string type_key;
201  uint32_t type_index;
202  };
203  std::unordered_map<std::string, ValueTypeInfo> key2vtype_;
205  std::unordered_map<String, ObjectRef> key2default_;
206 
208  uint32_t index_;
209 
210  // the name
211  std::string name;
212 
214  uint32_t AttrRegistryIndex() const { return index_; }
216  String AttrRegistryName() const { return name; }
217 
219  explicit ExecutorRegEntry(uint32_t reg_index) : index_(reg_index) {}
220 
221  // friend class
222  template <typename>
224  template <typename, typename>
225  friend class tvm::AttrRegistry;
226  friend class Executor;
227 };
228 
229 template <typename ValueType>
231  ICHECK(!key2vtype_.count(key)) << "AttributeError: add_attr_option failed because '" << key
232  << "' has been set once";
233 
234  using ValueNodeType = typename ValueType::ContainerType;
235  // NOTE: we could further update the function later.
236  uint32_t value_type_index = ValueNodeType::_GetOrAllocRuntimeTypeIndex();
237 
238  ValueTypeInfo info;
239  info.type_index = value_type_index;
240  info.type_key = runtime::Object::TypeIndex2Key(value_type_index);
241  key2vtype_[key] = info;
242  return *this;
243 }
244 
245 template <typename ValueType>
247  ObjectRef default_value) {
248  add_attr_option<ValueType>(key);
249  key2default_[key] = default_value;
250  return *this;
251 }
252 
253 // internal macros to make executor entries
254 #define TVM_EXECUTOR_REGISTER_VAR_DEF \
255  static DMLC_ATTRIBUTE_UNUSED ::tvm::relay::ExecutorRegEntry& __make_##Executor
256 
271 #define TVM_REGISTER_EXECUTOR(ExecutorName) \
272  TVM_STR_CONCAT(TVM_EXECUTOR_REGISTER_VAR_DEF, __COUNTER__) = \
273  ::tvm::relay::ExecutorRegEntry::RegisterOrGet(ExecutorName)
274 } // namespace relay
275 } // namespace tvm
276 
277 #endif // TVM_RELAY_EXECUTOR_H_
Attribute map used in registry.
Generic attribute map.
Definition: attr_registry_map.h:38
Definition: executor.h:43
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
Boolean constant.
Definition: expr.h:597
Managed reference to DictAttrsNode.
Definition: attrs.h:227
Optional< TObjectRef > GetAttr(const std::string &attr_key, Optional< TObjectRef > default_value=Optional< TObjectRef >(nullptr)) const
Get a function attribute.
Definition: attrs.h:258
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:137
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:121
Executor information.
Definition: executor.h:55
String name
name of the Executor
Definition: executor.h:58
DictAttrs attrs
Definition: executor.h:60
static constexpr const bool _type_has_method_sequal_reduce
Definition: executor.h:116
void VisitAttrs(AttrVisitor *v)
Definition: executor.h:101
bool SEqualReduce(const ExecutorNode *other, SEqualReducer equal) const
Definition: executor.h:106
TVM_DECLARE_FINAL_OBJECT_INFO(ExecutorNode, Object)
Optional< TObjectRef > GetAttr(const std::string &attr_key, Optional< TObjectRef > default_value=Optional< TObjectRef >(nullptr)) const
Get an attribute.
Definition: executor.h:90
Bool ShouldLinkParameters() const
Should Link Parameters into the module.
Definition: executor.h:66
void SHashReduce(SHashReducer hash_reduce) const
Definition: executor.h:110
static constexpr const bool _type_has_method_shash_reduce
Definition: executor.h:117
Optional< TObjectRef > GetAttr(const std::string &attr_key, TObjectRef default_value) const
Definition: executor.h:97
static constexpr const char * _type_key
Definition: executor.h:115
Helper structure to register Executors.
Definition: executor.h:171
static ExecutorRegEntry & RegisterOrGet(const String &name)
Register or get a new entry.
ExecutorRegEntry & add_attr_option(const String &key)
Register a valid configuration option and its ValueType for validation.
Definition: executor.h:230
Managed reference class to ExecutorNode.
Definition: executor.h:125
static Executor Create(String name, Map< String, ObjectRef > attrs={})
Create a new Executor object using the registry.
TVM_DEFINE_OBJECT_REF_METHODS(Executor, ObjectRef, ExecutorNode)
specify container node
static Array< String > ListExecutors()
List all registered Executors.
static Map< String, String > ListExecutorOptions(const String &name)
List all options for a specific Executor.
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
Base class of all object reference.
Definition: object.h:519
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:605
base class of all object containers.
Definition: object.h:171
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
Reference to string objects.
Definition: string.h:98
Helpers for attribute objects.
Base expr nodes in TVM.
IR/AST nodes for the unified type system in TVM.
Box< bool > Bool
Boxed version of C++ bool.
Definition: boxed_primitive.h:121
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
#define TVM_DEFINE_OBJECT_REF_COW_METHOD(ObjectName)
Define CopyOnWrite function in an ObjectRef.
Definition: object.h:826
This file defines the TVM global function registry.
Type relation and function for type inference(checking).