tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
target_kind.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_TARGET_TARGET_KIND_H_
25 #define TVM_TARGET_TARGET_KIND_H_
26 
28 #include <tvm/node/node.h>
29 
30 #include <memory>
31 #include <unordered_map>
32 #include <utility>
33 #include <vector>
34 
35 namespace tvm {
36 
37 class Target;
38 
43 
53 
54 namespace detail {
55 template <typename, typename, typename>
56 struct ValueTypeInfoMaker;
57 }
58 
59 class TargetInternal;
60 
61 template <typename>
62 class TargetKindAttrMap;
63 
65 class TargetKindNode : public Object {
66  public:
77 
79  v->Visit("name", &name);
80  v->Visit("default_device_type", &default_device_type);
81  v->Visit("default_keys", &default_keys);
82  }
83 
84  static constexpr const char* _type_key = "TargetKind";
86 
87  private:
89  uint32_t AttrRegistryIndex() const { return index_; }
91  String AttrRegistryName() const { return name; }
93  struct ValueTypeInfo {
94  String type_key;
95  uint32_t type_index;
96  std::unique_ptr<ValueTypeInfo> key;
97  std::unique_ptr<ValueTypeInfo> val;
98  };
100  std::unordered_map<String, ValueTypeInfo> key2vtype_;
102  std::unordered_map<String, ObjectRef> key2default_;
104  uint32_t index_;
105 
106  template <typename, typename, typename>
108  template <typename, typename>
109  friend class AttrRegistry;
110  template <typename>
112  friend class TargetKindRegEntry;
113  friend class TargetInternal;
114 };
115 
120 class TargetKind : public ObjectRef {
121  public:
122  TargetKind() = default;
124  template <typename ValueType>
125  static inline TargetKindAttrMap<ValueType> GetAttrMap(const String& attr_name);
131  TVM_DLL static Optional<TargetKind> Get(const String& target_kind_name);
134  TargetKindNode* operator->() { return static_cast<TargetKindNode*>(data_.get()); }
135 
136  private:
137  TVM_DLL static const AttrRegistryMapContainerMap<TargetKind>& GetAttrMapContainer(
138  const String& attr_name);
139  friend class TargetKindRegEntry;
140  friend class TargetInternal;
141 };
142 
147 template <typename ValueType>
148 class TargetKindAttrMap : public AttrRegistryMap<TargetKind, ValueType> {
149  public:
151  using TParent::count;
152  using TParent::get;
153  using TParent::operator[];
155 };
156 
158 static constexpr const char* kTvmRuntimeCpp = "c++";
159 
161 static constexpr const char* kTvmRuntimeCrt = "c";
162 
168  public:
182  template <typename ValueType>
183  inline TargetKindRegEntry& set_attr(const String& attr_name, const ValueType& value,
184  int plevel = 10);
194  inline TargetKindRegEntry& set_default_keys(std::vector<String> keys);
200  template <typename FLambda>
201  inline TargetKindRegEntry& set_attrs_preprocessor(FLambda f);
212  template <typename ValueType>
213  inline TargetKindRegEntry& add_attr_option(const String& key);
220  template <typename ValueType>
221  inline TargetKindRegEntry& add_attr_option(const String& key, ObjectRef default_value);
223  inline TargetKindRegEntry& set_name();
228  TVM_DLL static Array<String> ListTargetKinds();
234 
240  TVM_DLL static TargetKindRegEntry& RegisterOrGet(const String& target_kind_name);
241 
242  private:
243  TargetKind kind_;
244  String name;
245 
247  explicit TargetKindRegEntry(uint32_t reg_index) : kind_(make_object<TargetKindNode>()) {
248  kind_->index_ = reg_index;
249  }
256  TVM_DLL void UpdateAttr(const String& key, TVMRetValue value, int plevel);
257  template <typename, typename>
258  friend class AttrRegistry;
259  friend class TargetKind;
260 };
261 
262 namespace detail {
263 template <typename Type, template <typename...> class Container>
264 struct is_specialized : std::false_type {
265  using type = std::false_type;
266 };
267 
268 template <template <typename...> class Container, typename... Args>
269 struct is_specialized<Container<Args...>, Container> : std::true_type {
270  using type = std::true_type;
271 };
272 
273 template <typename ValueType, typename IsArray = typename is_specialized<ValueType, Array>::type,
274  typename IsMap = typename is_specialized<ValueType, Map>::type>
276 
277 template <typename ValueType>
278 struct ValueTypeInfoMaker<ValueType, std::false_type, std::false_type> {
279  using ValueTypeInfo = TargetKindNode::ValueTypeInfo;
280 
281  ValueTypeInfo operator()() const {
282  uint32_t tindex = ValueType::ContainerType::_GetOrAllocRuntimeTypeIndex();
283  ValueTypeInfo info;
284  info.type_index = tindex;
285  info.type_key = runtime::Object::TypeIndex2Key(tindex);
286  info.key = nullptr;
287  info.val = nullptr;
288  return info;
289  }
290 };
291 
292 template <typename ValueType>
293 struct ValueTypeInfoMaker<ValueType, std::true_type, std::false_type> {
294  using ValueTypeInfo = TargetKindNode::ValueTypeInfo;
295 
296  ValueTypeInfo operator()() const {
297  using key_type = ValueTypeInfoMaker<typename ValueType::value_type>;
298  uint32_t tindex = ValueType::ContainerType::_GetOrAllocRuntimeTypeIndex();
299  ValueTypeInfo info;
300  info.type_index = tindex;
301  info.type_key = runtime::Object::TypeIndex2Key(tindex);
302  info.key = std::make_unique<ValueTypeInfo>(key_type()());
303  info.val = nullptr;
304  return info;
305  }
306 };
307 
308 template <typename ValueType>
309 struct ValueTypeInfoMaker<ValueType, std::false_type, std::true_type> {
310  using ValueTypeInfo = TargetKindNode::ValueTypeInfo;
311  ValueTypeInfo operator()() const {
312  using key_type = ValueTypeInfoMaker<typename ValueType::key_type>;
313  using val_type = ValueTypeInfoMaker<typename ValueType::mapped_type>;
314  uint32_t tindex = ValueType::ContainerType::_GetOrAllocRuntimeTypeIndex();
315  ValueTypeInfo info;
316  info.type_index = tindex;
317  info.type_key = runtime::Object::TypeIndex2Key(tindex);
318  info.key = std::make_unique<ValueTypeInfo>(key_type()());
319  info.val = std::make_unique<ValueTypeInfo>(val_type()());
320  return info;
321  }
322 };
323 
324 } // namespace detail
325 
326 template <typename ValueType>
328  return TargetKindAttrMap<ValueType>(GetAttrMapContainer(attr_name));
329 }
330 
331 template <typename ValueType>
333  const ValueType& value, int plevel) {
334  ICHECK_GT(plevel, 0) << "plevel in set_attr must be greater than 0";
336  rv = value;
337  UpdateAttr(attr_name, rv, plevel);
338  return *this;
339 }
340 
343  return *this;
344 }
345 
346 inline TargetKindRegEntry& TargetKindRegEntry::set_default_keys(std::vector<String> keys) {
347  kind_->default_keys = keys;
348  return *this;
349 }
350 
351 template <typename FLambda>
353  LOG(WARNING) << "set_attrs_preprocessor is deprecated please use set_target_parser instead";
354  using FType = typename tvm::runtime::detail::function_signature<FLambda>::FType;
355  kind_->preprocessor = tvm::runtime::TypedPackedFunc<FType>(std::move(f)).packed();
356  return *this;
357 }
358 
360  kind_->target_parser = parser;
361  return *this;
362 }
363 
364 template <typename ValueType>
366  ICHECK(!kind_->key2vtype_.count(key))
367  << "AttributeError: add_attr_option failed because '" << key << "' has been set once";
368  kind_->key2vtype_[key] = detail::ValueTypeInfoMaker<ValueType>()();
369  return *this;
370 }
371 
372 template <typename ValueType>
374  ObjectRef default_value) {
375  add_attr_option<ValueType>(key);
376  kind_->key2default_[key] = default_value;
377  return *this;
378 }
379 
381  if (kind_->name.empty()) {
382  kind_->name = name;
383  }
384  return *this;
385 }
386 
387 #define TVM_TARGET_KIND_REGISTER_VAR_DEF \
388  static DMLC_ATTRIBUTE_UNUSED ::tvm::TargetKindRegEntry& __make_##TargetKind
389 
407 #define TVM_REGISTER_TARGET_KIND(TargetKindName, DeviceType) \
408  TVM_STR_CONCAT(TVM_TARGET_KIND_REGISTER_VAR_DEF, __COUNTER__) = \
409  ::tvm::TargetKindRegEntry::RegisterOrGet(TargetKindName) \
410  .set_name() \
411  .set_default_device_type(DeviceType) \
412  .add_attr_option<Array<String>>("keys") \
413  .add_attr_option<String>("tag") \
414  .add_attr_option<String>("device") \
415  .add_attr_option<String>("model") \
416  .add_attr_option<Array<String>>("libs") \
417  .add_attr_option<Target>("host") \
418  .add_attr_option<runtime::Int>("from_device") \
419  .add_attr_option<runtime::Int>("target_device_type")
420 
421 } // namespace tvm
422 
423 #endif // TVM_TARGET_TARGET_KIND_H_
Attribute map used in registry.
Generic attribute map.
Definition: attr_registry_map.h:38
Map<Key, ValueType> used to store meta-data.
Definition: attr_registry_map.h:101
ValueType get(const TargetKind &key, ValueType def_value) const
get the corresponding value element at key with default value.
Definition: attr_registry_map.h:126
int count(const TargetKind &key) const
Check if the map has op as key.
Definition: attr_registry_map.h:113
Definition: instruction.h:30
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
Map<TargetKind, ValueType> used to store meta-information about TargetKind.
Definition: target_kind.h:148
TargetKindAttrMap(const AttrRegistryMapContainerMap< TargetKind > &map)
Definition: target_kind.h:154
Target kind, specifies the kind of the target.
Definition: target_kind.h:65
int default_device_type
Device type of target kind.
Definition: target_kind.h:70
PackedFunc preprocessor
Function used to preprocess on target creation.
Definition: target_kind.h:74
String name
Name of the target kind.
Definition: target_kind.h:68
TVM_DECLARE_FINAL_OBJECT_INFO(TargetKindNode, Object)
FTVMTargetParser target_parser
Function used to parse a JSON target during creation.
Definition: target_kind.h:76
friend class TargetInternal
Definition: target_kind.h:113
void VisitAttrs(AttrVisitor *v)
Definition: target_kind.h:78
Array< String > default_keys
Default keys of the target.
Definition: target_kind.h:72
static constexpr const char * _type_key
Definition: target_kind.h:84
Helper structure to register TargetKind.
Definition: target_kind.h:167
TargetKindRegEntry & set_attrs_preprocessor(FLambda f)
Set the pre-processing function applied upon target creation.
Definition: target_kind.h:352
TargetKindRegEntry & set_target_parser(FTVMTargetParser parser)
Set the parsing function applied upon target creation.
Definition: target_kind.h:359
TargetKindRegEntry & set_default_keys(std::vector< String > keys)
Set DLPack's device_type the target.
Definition: target_kind.h:346
TargetKindRegEntry & set_name()
Set name of the TargetKind to be the same as registry if it is empty.
Definition: target_kind.h:380
static TargetKindRegEntry & RegisterOrGet(const String &target_kind_name)
Register or get a new entry.
TargetKindRegEntry & set_attr(const String &attr_name, const ValueType &value, int plevel=10)
Register additional attributes to target_kind.
Definition: target_kind.h:332
static Array< String > ListTargetKinds()
List all the entry names in the registry.
TargetKindRegEntry & set_default_device_type(int device_type)
Set DLPack's device_type the target.
Definition: target_kind.h:341
TargetKindRegEntry & add_attr_option(const String &key)
Register a valid configuration option and its ValueType for validation.
Definition: target_kind.h:365
static Map< String, String > ListTargetKindOptions(const TargetKind &kind)
Get all supported option names and types for a given Target kind.
Managed reference class to TargetKindNode.
Definition: target_kind.h:120
static Optional< TargetKind > Get(const String &target_kind_name)
Retrieve the TargetKind given its name.
TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TargetKind, ObjectRef, TargetKindNode)
friend class TargetInternal
Definition: target_kind.h:140
static TargetKindAttrMap< ValueType > GetAttrMap(const String &attr_name)
Get the attribute map given the attribute name.
Definition: target_kind.h:327
TargetKindNode * operator->()
Mutable access to the container class
Definition: target_kind.h:134
TargetKind()=default
Managed reference to TypeNode.
Definition: type.h:93
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:520
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:606
base class of all object containers.
Definition: object.h:172
uint32_t type_index() const
Definition: object.h:180
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
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:141
Reference to string objects.
Definition: string.h:97
bool empty() const
Retun if the string is empty.
Definition: string.h:207
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:946
ObjectPtr< ArrayNode > make_object()
Definition: array.h:908
constexpr const char * device_type
The device type.
Definition: stmt.h:1422
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
Map< String, ObjectRef > TargetJSON
TargetParser to apply on instantiation of a given TargetKind.
Definition: target_kind.h:51
Definitions and helper macros for IR/AST nodes.
Definition: target_kind.h:275
std::true_type type
Definition: target_kind.h:270
Definition: target_kind.h:264
std::false_type type
Definition: target_kind.h:265