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 
27 #include <tvm/ir/transform.h>
29 #include <tvm/node/node.h>
30 
31 #include <memory>
32 #include <unordered_map>
33 #include <utility>
34 #include <vector>
35 
36 namespace tvm {
37 
38 class Target;
39 
44 
54 
66 
78 
79 namespace detail {
80 template <typename, typename, typename>
82 }
83 
84 class TargetInternal;
85 
86 template <typename>
88 
90 class TargetKindNode : public Object {
91  public:
102 
104  v->Visit("name", &name);
105  v->Visit("default_device_type", &default_device_type);
106  v->Visit("default_keys", &default_keys);
107  }
108 
109  static constexpr const char* _type_key = "TargetKind";
111 
112  private:
114  uint32_t AttrRegistryIndex() const { return index_; }
116  String AttrRegistryName() const { return name; }
118  struct ValueTypeInfo {
119  String type_key;
120  uint32_t type_index;
121  std::unique_ptr<ValueTypeInfo> key;
122  std::unique_ptr<ValueTypeInfo> val;
123  };
125  std::unordered_map<String, ValueTypeInfo> key2vtype_;
127  std::unordered_map<String, ObjectRef> key2default_;
129  uint32_t index_;
130 
131  template <typename, typename, typename>
133  template <typename, typename>
134  friend class AttrRegistry;
135  template <typename>
137  friend class TargetKindRegEntry;
138  friend class TargetInternal;
139 };
140 
145 class TargetKind : public ObjectRef {
146  public:
147  TargetKind() = default;
149  template <typename ValueType>
150  static inline TargetKindAttrMap<ValueType> GetAttrMap(const String& attr_name);
156  TVM_DLL static Optional<TargetKind> Get(const String& target_kind_name);
158 
159  private:
161  TargetKindNode* operator->() { return static_cast<TargetKindNode*>(data_.get()); }
162  TVM_DLL static const AttrRegistryMapContainerMap<TargetKind>& GetAttrMapContainer(
163  const String& attr_name);
164  friend class TargetKindRegEntry;
165  friend class TargetInternal;
166 };
167 
172 template <typename ValueType>
173 class TargetKindAttrMap : public AttrRegistryMap<TargetKind, ValueType> {
174  public:
176  using TParent::count;
177  using TParent::get;
178  using TParent::operator[];
180 };
181 
183 static constexpr const char* kTvmRuntimeCpp = "c++";
184 
186 static constexpr const char* kTvmRuntimeCrt = "c";
187 
193  public:
207  template <typename ValueType>
208  inline TargetKindRegEntry& set_attr(const String& attr_name, const ValueType& value,
209  int plevel = 10);
214  inline TargetKindRegEntry& set_default_device_type(int device_type);
219  inline TargetKindRegEntry& set_default_keys(std::vector<String> keys);
225  template <typename FLambda>
226  inline TargetKindRegEntry& set_attrs_preprocessor(FLambda f);
231  inline TargetKindRegEntry& set_target_parser(FTVMTargetParser parser);
237  template <typename ValueType>
238  inline TargetKindRegEntry& add_attr_option(const String& key);
245  template <typename ValueType>
246  inline TargetKindRegEntry& add_attr_option(const String& key, ObjectRef default_value);
248  inline TargetKindRegEntry& set_name();
253  TVM_DLL static Array<String> ListTargetKinds();
258  TVM_DLL static Map<String, String> ListTargetKindOptions(const TargetKind& kind);
259 
265  TVM_DLL static TargetKindRegEntry& RegisterOrGet(const String& target_kind_name);
266 
267  private:
268  TargetKind kind_;
269  String name;
270 
272  explicit TargetKindRegEntry(uint32_t reg_index) : kind_(make_object<TargetKindNode>()) {
273  kind_->index_ = reg_index;
274  }
281  TVM_DLL void UpdateAttr(const String& key, TVMRetValue value, int plevel);
282  template <typename, typename>
283  friend class AttrRegistry;
284  friend class TargetKind;
285 };
286 
287 namespace detail {
288 template <typename Type, template <typename...> class Container>
289 struct is_specialized : std::false_type {
290  using type = std::false_type;
291 };
292 
293 template <template <typename...> class Container, typename... Args>
294 struct is_specialized<Container<Args...>, Container> : std::true_type {
295  using type = std::true_type;
296 };
297 
298 template <typename ValueType, typename IsArray = typename is_specialized<ValueType, Array>::type,
299  typename IsMap = typename is_specialized<ValueType, Map>::type>
300 struct ValueTypeInfoMaker {};
301 
302 template <typename ValueType>
303 struct ValueTypeInfoMaker<ValueType, std::false_type, std::false_type> {
304  using ValueTypeInfo = TargetKindNode::ValueTypeInfo;
305 
306  ValueTypeInfo operator()() const {
307  uint32_t tindex = ValueType::ContainerType::_GetOrAllocRuntimeTypeIndex();
308  ValueTypeInfo info;
309  info.type_index = tindex;
310  info.type_key = runtime::Object::TypeIndex2Key(tindex);
311  info.key = nullptr;
312  info.val = nullptr;
313  return info;
314  }
315 };
316 
317 template <typename ValueType>
318 struct ValueTypeInfoMaker<ValueType, std::true_type, std::false_type> {
319  using ValueTypeInfo = TargetKindNode::ValueTypeInfo;
320 
321  ValueTypeInfo operator()() const {
323  uint32_t tindex = ValueType::ContainerType::_GetOrAllocRuntimeTypeIndex();
324  ValueTypeInfo info;
325  info.type_index = tindex;
326  info.type_key = runtime::Object::TypeIndex2Key(tindex);
327  info.key = std::make_unique<ValueTypeInfo>(key_type()());
328  info.val = nullptr;
329  return info;
330  }
331 };
332 
333 template <typename ValueType>
334 struct ValueTypeInfoMaker<ValueType, std::false_type, std::true_type> {
335  using ValueTypeInfo = TargetKindNode::ValueTypeInfo;
336  ValueTypeInfo operator()() const {
339  uint32_t tindex = ValueType::ContainerType::_GetOrAllocRuntimeTypeIndex();
340  ValueTypeInfo info;
341  info.type_index = tindex;
342  info.type_key = runtime::Object::TypeIndex2Key(tindex);
343  info.key = std::make_unique<ValueTypeInfo>(key_type()());
344  info.val = std::make_unique<ValueTypeInfo>(val_type()());
345  return info;
346  }
347 };
348 
349 } // namespace detail
350 
351 template <typename ValueType>
353  return TargetKindAttrMap<ValueType>(GetAttrMapContainer(attr_name));
354 }
355 
356 template <typename ValueType>
358  const ValueType& value, int plevel) {
359  ICHECK_GT(plevel, 0) << "plevel in set_attr must be greater than 0";
361  rv = value;
362  UpdateAttr(attr_name, rv, plevel);
363  return *this;
364 }
365 
367  kind_->default_device_type = device_type;
368  return *this;
369 }
370 
371 inline TargetKindRegEntry& TargetKindRegEntry::set_default_keys(std::vector<String> keys) {
372  kind_->default_keys = keys;
373  return *this;
374 }
375 
376 template <typename FLambda>
378  LOG(WARNING) << "set_attrs_preprocessor is deprecated please use set_target_parser instead";
379  using FType = typename tvm::runtime::detail::function_signature<FLambda>::FType;
380  kind_->preprocessor = tvm::runtime::TypedPackedFunc<FType>(std::move(f)).packed();
381  return *this;
382 }
383 
385  kind_->target_parser = parser;
386  return *this;
387 }
388 
389 template <typename ValueType>
391  ICHECK(!kind_->key2vtype_.count(key))
392  << "AttributeError: add_attr_option failed because '" << key << "' has been set once";
393  kind_->key2vtype_[key] = detail::ValueTypeInfoMaker<ValueType>()();
394  return *this;
395 }
396 
397 template <typename ValueType>
399  ObjectRef default_value) {
400  add_attr_option<ValueType>(key);
401  kind_->key2default_[key] = default_value;
402  return *this;
403 }
404 
406  if (kind_->name.empty()) {
407  kind_->name = name;
408  }
409  return *this;
410 }
411 
412 #define TVM_TARGET_KIND_REGISTER_VAR_DEF \
413  static DMLC_ATTRIBUTE_UNUSED ::tvm::TargetKindRegEntry& __make_##TargetKind
414 
415 namespace attr {
416 //
417 // Distinguished TargetKind attribute names.
418 //
419 
432 constexpr const char* kIsExternalCodegen = "is_external_codegen";
433 
441 constexpr const char* kRelayToTIR = "RelayToTIR";
442 
443 } // namespace attr
444 
462 #define TVM_REGISTER_TARGET_KIND(TargetKindName, DeviceType) \
463  TVM_STR_CONCAT(TVM_TARGET_KIND_REGISTER_VAR_DEF, __COUNTER__) = \
464  ::tvm::TargetKindRegEntry::RegisterOrGet(TargetKindName) \
465  .set_name() \
466  .set_default_device_type(DeviceType) \
467  .add_attr_option<Array<String>>("keys") \
468  .add_attr_option<String>("tag") \
469  .add_attr_option<String>("device") \
470  .add_attr_option<String>("model") \
471  .add_attr_option<Array<String>>("libs") \
472  .add_attr_option<Target>("host") \
473  .add_attr_option<Integer>("from_device") \
474  .add_attr_option<Integer>("target_device_type")
475 
476 } // namespace tvm
477 
478 #endif // TVM_TARGET_TARGET_KIND_H_
int default_device_type
Device type of target kind.
Definition: target_kind.h:95
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:799
constexpr const char * device_type
The device type.
Definition: stmt.h:1355
Definition: target_kind.h:289
constexpr const char * kRelayToTIR
A TargetKind attribute of type FTVMRelayToTIR. If set, then the target kind name also corresponds to ...
Definition: target_kind.h:441
TargetKindRegEntry & set_default_keys(std::vector< String > keys)
Set DLPack&#39;s device_type the target.
Definition: target_kind.h:371
Definitions and helper macros for IR/AST nodes.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
void VisitAttrs(AttrVisitor *v)
Definition: target_kind.h:103
TargetKindRegEntry & set_name()
Set name of the TargetKind to be the same as registry if it is empty.
Definition: target_kind.h:405
std::true_type type
Definition: target_kind.h:295
base class of all object containers.
Definition: object.h:167
Definition: executor.h:43
tvm::transform::Pass Pass
Definition: transform.h:43
Managed reference class to TargetKindNode.
Definition: target_kind.h:145
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
TargetKindRegEntry & add_attr_option(const String &key)
Register a valid configuration option and its ValueType for validation.
Definition: target_kind.h:390
TargetKindAttrMap(const AttrRegistryMapContainerMap< TargetKind > &map)
Definition: target_kind.h:179
Helper structure to register TargetKind.
Definition: target_kind.h:192
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
FTVMTargetParser target_parser
Function used to parse a JSON target during creation.
Definition: target_kind.h:101
Definition: transform.h:362
Reference to string objects.
Definition: string.h:98
Please refer to TypedPackedFunc<R(Args..)>.
Definition: packed_func.h:60
Definition: target_kind.h:81
tvm::Type Type
Definition: type.h:47
Array< String > default_keys
Default keys of the target.
Definition: target_kind.h:97
Base class of all object reference.
Definition: object.h:511
Map<Key, ValueType> used to store meta-data.
Definition: attr_registry_map.h:101
Target kind, specifies the kind of the target.
Definition: target_kind.h:90
Attribute map used in registry.
constexpr const char * kIsExternalCodegen
A TargetKind attribute of type Bool. If true, then the target kind name also corresponds to an extern...
Definition: target_kind.h:432
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:671
TargetKindRegEntry & set_attr(const String &attr_name, const ValueType &value, int plevel=10)
Register additional attributes to target_kind.
Definition: target_kind.h:357
std::false_type type
Definition: target_kind.h:290
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:1271
static std::string TypeIndex2Key(uint32_t tindex)
Get the type key of the corresponding index from runtime.
PackedFunc preprocessor
Function used to preprocess on target creation.
Definition: target_kind.h:99
Packed function is a type-erased function. The arguments are passed by packed format.
Definition: packed_func.h:138
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Map<TargetKind, ValueType> used to store meta-information about TargetKind.
Definition: target_kind.h:87
TargetKindRegEntry & set_target_parser(FTVMTargetParser parser)
Set the parsing function applied upon target creation.
Definition: target_kind.h:384
TargetKindRegEntry & set_default_device_type(int device_type)
Set DLPack&#39;s device_type the target.
Definition: target_kind.h:366
static TargetKindAttrMap< ValueType > GetAttrMap(const String &attr_name)
Get the attribute map given the attribute name.
Definition: target_kind.h:352
String name
Name of the target kind.
Definition: target_kind.h:93
#define TVM_DEFINE_NOTNULLABLE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:728
TargetKindRegEntry & set_attrs_preprocessor(FLambda f)
Set the pre-processing function applied upon target creation.
Definition: target_kind.h:377