tvm
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 
51 
63 
64 namespace detail {
65 template <typename, typename, typename>
67 }
68 
69 class TargetInternal;
70 
71 template <typename>
73 
75 class TargetKindNode : public Object {
76  public:
85 
87  v->Visit("name", &name);
88  v->Visit("device_type", &device_type);
89  v->Visit("default_keys", &default_keys);
90  }
91 
92  static constexpr const char* _type_key = "TargetKind";
94 
95  private:
97  uint32_t AttrRegistryIndex() const { return index_; }
99  String AttrRegistryName() const { return name; }
101  struct ValueTypeInfo {
102  String type_key;
103  uint32_t type_index;
104  std::unique_ptr<ValueTypeInfo> key;
105  std::unique_ptr<ValueTypeInfo> val;
106  };
108  std::unordered_map<String, ValueTypeInfo> key2vtype_;
110  std::unordered_map<String, ObjectRef> key2default_;
112  uint32_t index_;
113 
114  template <typename, typename, typename>
116  template <typename, typename>
117  friend class AttrRegistry;
118  template <typename>
120  friend class TargetKindRegEntry;
121  friend class TargetInternal;
122 };
123 
128 class TargetKind : public ObjectRef {
129  public:
130  TargetKind() = default;
132  template <typename ValueType>
133  static inline TargetKindAttrMap<ValueType> GetAttrMap(const String& attr_name);
139  TVM_DLL static Optional<TargetKind> Get(const String& target_kind_name);
141 
142  private:
144  TargetKindNode* operator->() { return static_cast<TargetKindNode*>(data_.get()); }
145  TVM_DLL static const AttrRegistryMapContainerMap<TargetKind>& GetAttrMapContainer(
146  const String& attr_name);
147  friend class TargetKindRegEntry;
148  friend class TargetInternal;
149 };
150 
155 template <typename ValueType>
156 class TargetKindAttrMap : public AttrRegistryMap<TargetKind, ValueType> {
157  public:
159  using TParent::count;
160  using TParent::get;
161  using TParent::operator[];
163 };
164 
166 static constexpr const char* kTvmRuntimeCpp = "c++";
167 
169 static constexpr const char* kTvmRuntimeCrt = "c";
170 
176  public:
190  template <typename ValueType>
191  inline TargetKindRegEntry& set_attr(const String& attr_name, const ValueType& value,
192  int plevel = 10);
197  inline TargetKindRegEntry& set_device_type(int device_type);
202  inline TargetKindRegEntry& set_default_keys(std::vector<String> keys);
208  template <typename FLambda>
209  inline TargetKindRegEntry& set_attrs_preprocessor(FLambda f);
215  template <typename ValueType>
216  inline TargetKindRegEntry& add_attr_option(const String& key);
223  template <typename ValueType>
224  inline TargetKindRegEntry& add_attr_option(const String& key, ObjectRef default_value);
226  inline TargetKindRegEntry& set_name();
231  TVM_DLL static Array<String> ListTargetKinds();
236  TVM_DLL static Map<String, String> ListTargetKindOptions(const TargetKind& kind);
237 
243  TVM_DLL static TargetKindRegEntry& RegisterOrGet(const String& target_kind_name);
244 
245  private:
246  TargetKind kind_;
247  String name;
248 
250  explicit TargetKindRegEntry(uint32_t reg_index) : kind_(make_object<TargetKindNode>()) {
251  kind_->index_ = reg_index;
252  }
259  TVM_DLL void UpdateAttr(const String& key, TVMRetValue value, int plevel);
260  template <typename, typename>
261  friend class AttrRegistry;
262  friend class TargetKind;
263 };
264 
265 namespace detail {
266 template <typename Type, template <typename...> class Container>
267 struct is_specialized : std::false_type {
268  using type = std::false_type;
269 };
270 
271 template <template <typename...> class Container, typename... Args>
272 struct is_specialized<Container<Args...>, Container> : std::true_type {
273  using type = std::true_type;
274 };
275 
276 template <typename ValueType, typename IsArray = typename is_specialized<ValueType, Array>::type,
277  typename IsMap = typename is_specialized<ValueType, Map>::type>
278 struct ValueTypeInfoMaker {};
279 
280 template <typename ValueType>
281 struct ValueTypeInfoMaker<ValueType, std::false_type, std::false_type> {
282  using ValueTypeInfo = TargetKindNode::ValueTypeInfo;
283 
284  ValueTypeInfo operator()() const {
285  uint32_t tindex = ValueType::ContainerType::_GetOrAllocRuntimeTypeIndex();
286  ValueTypeInfo info;
287  info.type_index = tindex;
288  info.type_key = runtime::Object::TypeIndex2Key(tindex);
289  info.key = nullptr;
290  info.val = nullptr;
291  return info;
292  }
293 };
294 
295 template <typename ValueType>
296 struct ValueTypeInfoMaker<ValueType, std::true_type, std::false_type> {
297  using ValueTypeInfo = TargetKindNode::ValueTypeInfo;
298 
299  ValueTypeInfo operator()() const {
301  uint32_t tindex = ValueType::ContainerType::_GetOrAllocRuntimeTypeIndex();
302  ValueTypeInfo info;
303  info.type_index = tindex;
304  info.type_key = runtime::Object::TypeIndex2Key(tindex);
305  info.key = std::unique_ptr<ValueTypeInfo>(new ValueTypeInfo(key_type()()));
306  info.val = nullptr;
307  return info;
308  }
309 };
310 
311 template <typename ValueType>
312 struct ValueTypeInfoMaker<ValueType, std::false_type, std::true_type> {
313  using ValueTypeInfo = TargetKindNode::ValueTypeInfo;
314  ValueTypeInfo operator()() const {
317  uint32_t tindex = ValueType::ContainerType::_GetOrAllocRuntimeTypeIndex();
318  ValueTypeInfo info;
319  info.type_index = tindex;
320  info.type_key = runtime::Object::TypeIndex2Key(tindex);
321  info.key = std::unique_ptr<ValueTypeInfo>(new ValueTypeInfo(key_type()()));
322  info.val = std::unique_ptr<ValueTypeInfo>(new ValueTypeInfo(val_type()()));
323  return info;
324  }
325 };
326 
327 } // namespace detail
328 
329 template <typename ValueType>
331  return TargetKindAttrMap<ValueType>(GetAttrMapContainer(attr_name));
332 }
333 
334 template <typename ValueType>
336  const ValueType& value, int plevel) {
337  ICHECK_GT(plevel, 0) << "plevel in set_attr must be greater than 0";
339  rv = value;
340  UpdateAttr(attr_name, rv, plevel);
341  return *this;
342 }
343 
345  kind_->device_type = device_type;
346  return *this;
347 }
348 
349 inline TargetKindRegEntry& TargetKindRegEntry::set_default_keys(std::vector<String> keys) {
350  kind_->default_keys = keys;
351  return *this;
352 }
353 
354 template <typename FLambda>
356  using FType = typename tvm::runtime::detail::function_signature<FLambda>::FType;
357  kind_->preprocessor = tvm::runtime::TypedPackedFunc<FType>(std::move(f)).packed();
358  return *this;
359 }
360 
361 template <typename ValueType>
363  ICHECK(!kind_->key2vtype_.count(key))
364  << "AttributeError: add_attr_option failed because '" << key << "' has been set once";
365  kind_->key2vtype_[key] = detail::ValueTypeInfoMaker<ValueType>()();
366  return *this;
367 }
368 
369 template <typename ValueType>
371  ObjectRef default_value) {
372  add_attr_option<ValueType>(key);
373  kind_->key2default_[key] = default_value;
374  return *this;
375 }
376 
378  if (kind_->name.empty()) {
379  kind_->name = name;
380  }
381  return *this;
382 }
383 
384 #define TVM_TARGET_KIND_REGISTER_VAR_DEF \
385  static DMLC_ATTRIBUTE_UNUSED ::tvm::TargetKindRegEntry& __make_##TargetKind
386 
387 namespace attr {
388 //
389 // Distinguished TargetKind attribute names.
390 //
391 
404 constexpr const char* kIsExternalCodegen = "is_external_codegen";
405 
413 constexpr const char* kRelayToTIR = "RelayToTIR";
414 
415 } // namespace attr
416 
434 #define TVM_REGISTER_TARGET_KIND(TargetKindName, DeviceType) \
435  TVM_STR_CONCAT(TVM_TARGET_KIND_REGISTER_VAR_DEF, __COUNTER__) = \
436  ::tvm::TargetKindRegEntry::RegisterOrGet(TargetKindName) \
437  .set_name() \
438  .set_device_type(DeviceType) \
439  .add_attr_option<Array<String>>("keys") \
440  .add_attr_option<String>("tag") \
441  .add_attr_option<String>("device") \
442  .add_attr_option<String>("model") \
443  .add_attr_option<Array<String>>("libs") \
444  .add_attr_option<Target>("host") \
445  .add_attr_option<Integer>("from_device")
446 
447 } // namespace tvm
448 
449 #endif // TVM_TARGET_TARGET_KIND_H_
TargetKindRegEntry & set_device_type(int device_type)
Set DLPack&#39;s device_type the target.
Definition: target_kind.h:344
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:1357
Definition: target_kind.h:267
constexpr const char * kRelayToTIR
A TargetKind attribute of type FTVMRelayToTIR. If set, then the target kind name also corresponds to ...
Definition: target_kind.h:413
TargetKindRegEntry & set_default_keys(std::vector< String > keys)
Set DLPack&#39;s device_type the target.
Definition: target_kind.h:349
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:86
TargetKindRegEntry & set_name()
Set name of the TargetKind to be the same as registry if it is empty.
Definition: target_kind.h:377
std::true_type type
Definition: target_kind.h:273
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:128
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:362
TargetKindAttrMap(const AttrRegistryMapContainerMap< TargetKind > &map)
Definition: target_kind.h:162
Helper structure to register TargetKind.
Definition: target_kind.h:175
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:270
Definition: transform.h:363
Reference to string objects.
Definition: string.h:124
Please refer to TypedPackedFunc<R(Args..)>.
Definition: packed_func.h:60
Definition: target_kind.h:66
tvm::Type Type
Definition: type.h:47
Array< String > default_keys
Default keys of the target.
Definition: target_kind.h:82
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:75
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:404
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:671
int device_type
Device type of target kind.
Definition: target_kind.h:80
TargetKindRegEntry & set_attr(const String &attr_name, const ValueType &value, int plevel=10)
Register additional attributes to target_kind.
Definition: target_kind.h:335
std::false_type type
Definition: target_kind.h:268
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:1268
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:84
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:72
static TargetKindAttrMap< ValueType > GetAttrMap(const String &attr_name)
Get the attribute map given the attribute name.
Definition: target_kind.h:330
String name
Name of the target kind.
Definition: target_kind.h:78
#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:355