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/ffi/function.h>
28 #include <tvm/ffi/reflection/registry.h>
29 #include <tvm/ir/config_schema.h>
31 #include <tvm/runtime/object.h>
32 
33 #include <memory>
34 #include <unordered_map>
35 #include <utility>
36 #include <vector>
37 
38 namespace tvm {
39 
40 class Target;
41 
49  ffi::TypedFunction<ffi::Map<ffi::String, ffi::Any>(ffi::Map<ffi::String, ffi::Any>)>;
50 
51 class TargetInternal;
52 
53 template <typename>
54 class TargetKindAttrMap;
55 
57 class TargetKindNode : public Object {
58  public:
60  ffi::String name;
64  ffi::Array<ffi::String> default_keys;
67 
68  static void RegisterReflection() {
69  namespace refl = tvm::ffi::reflection;
70  refl::ObjectDef<TargetKindNode>()
71  .def_ro("name", &TargetKindNode::name)
72  .def_ro("default_device_type", &TargetKindNode::default_device_type,
73  refl::AttachFieldFlag::SEqHashIgnore())
74  .def_ro("default_keys", &TargetKindNode::default_keys,
75  refl::AttachFieldFlag::SEqHashIgnore());
76  }
77 
78  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindUniqueInstance;
79  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("target.TargetKind", TargetKindNode, Object);
80 
81  private:
83  uint32_t AttrRegistryIndex() const { return index_; }
85  ffi::String AttrRegistryName() const { return name; }
87  ir::ConfigSchema schema_;
89  uint32_t index_;
90 
91  template <typename, typename>
92  friend class AttrRegistry;
93  template <typename>
95  friend class TargetKindRegEntry;
96  friend class TargetInternal;
97 };
98 
103 class TargetKind : public ObjectRef {
104  public:
105  TargetKind() = default;
106  explicit TargetKind(ObjectPtr<TargetKindNode> data) : ObjectRef(data) {
107  TVM_FFI_ICHECK(data != nullptr);
108  }
110  template <typename ValueType>
111  static inline TargetKindAttrMap<ValueType> GetAttrMap(const ffi::String& attr_name);
117  TVM_DLL static ffi::Optional<TargetKind> Get(const ffi::String& target_kind_name);
119  TargetKindNode* operator->() { return static_cast<TargetKindNode*>(data_.get()); }
120 
122 
123  private:
124  TVM_DLL static const AttrRegistryMapContainerMap<TargetKind>& GetAttrMapContainer(
125  const ffi::String& attr_name);
126  friend class TargetKindRegEntry;
127  friend class TargetInternal;
128 };
129 
134 template <typename ValueType>
135 class TargetKindAttrMap : public AttrRegistryMap<TargetKind, ValueType> {
136  public:
138  using TParent::count;
139  using TParent::get;
140  using TParent::operator[];
142 };
143 
145 static constexpr const char* kTvmRuntimeCpp = "c++";
146 
148 static constexpr const char* kTvmRuntimeCrt = "c";
149 
155  public:
169  template <typename ValueType>
170  inline TargetKindRegEntry& set_attr(const ffi::String& attr_name, const ValueType& value,
171  int plevel = 10);
181  inline TargetKindRegEntry& set_default_keys(std::vector<ffi::String> keys);
194  template <typename ValueType, typename... Traits>
195  inline TargetKindRegEntry& add_attr_option(const ffi::String& key, Traits&&... traits);
197  inline TargetKindRegEntry& set_name();
202  TVM_DLL static ffi::Array<ffi::String> ListTargetKinds();
207  TVM_DLL static ffi::Map<ffi::String, ffi::String> ListTargetKindOptions(const TargetKind& kind);
208 
214  TVM_DLL static TargetKindRegEntry& RegisterOrGet(const ffi::String& target_kind_name);
215 
216  private:
217  TargetKind kind_;
218  ffi::String name;
219 
221  explicit TargetKindRegEntry(uint32_t reg_index) : kind_(ffi::make_object<TargetKindNode>()) {
222  kind_->index_ = reg_index;
223  }
230  TVM_DLL void UpdateAttr(const ffi::String& key, ffi::Any value, int plevel);
231  template <typename, typename>
232  friend class AttrRegistry;
233  friend class TargetKind;
234 };
235 
236 template <typename ValueType>
237 inline TargetKindAttrMap<ValueType> TargetKind::GetAttrMap(const ffi::String& attr_name) {
238  return TargetKindAttrMap<ValueType>(GetAttrMapContainer(attr_name));
239 }
240 
241 template <typename ValueType>
242 inline TargetKindRegEntry& TargetKindRegEntry::set_attr(const ffi::String& attr_name,
243  const ValueType& value, int plevel) {
244  TVM_FFI_ICHECK_GT(plevel, 0) << "plevel in set_attr must be greater than 0";
245  ffi::Any rv;
246  rv = value;
247  UpdateAttr(attr_name, rv, plevel);
248  return *this;
249 }
250 
253  return *this;
254 }
255 
256 inline TargetKindRegEntry& TargetKindRegEntry::set_default_keys(std::vector<ffi::String> keys) {
257  kind_->default_keys = keys;
258  return *this;
259 }
260 
262  FTargetCanonicalizer canonicalizer) {
263  kind_->target_canonicalizer = canonicalizer;
264  kind_->schema_.set_canonicalizer(canonicalizer);
265  return *this;
266 }
267 
268 template <typename ValueType, typename... Traits>
270  Traits&&... traits) {
271  kind_->schema_.def_option<ValueType>(key, std::forward<Traits>(traits)...);
272  return *this;
273 }
274 
276  if (kind_->name.empty()) {
277  kind_->name = name;
278  }
279  return *this;
280 }
281 
282 #define TVM_TARGET_KIND_REGISTER_VAR_DEF \
283  static TVM_ATTRIBUTE_UNUSED ::tvm::TargetKindRegEntry& __make_##TargetKind
284 
302 #define TVM_REGISTER_TARGET_KIND(TargetKindName, DeviceType) \
303  TVM_STR_CONCAT(TVM_TARGET_KIND_REGISTER_VAR_DEF, __COUNTER__) = \
304  ::tvm::TargetKindRegEntry::RegisterOrGet(TargetKindName) \
305  .set_name() \
306  .set_default_device_type(DeviceType) \
307  .add_attr_option<ffi::String>("kind") \
308  .add_attr_option<ffi::Array<ffi::String>>("keys") \
309  .add_attr_option<ffi::String>("tag") \
310  .add_attr_option<ffi::String>("device") \
311  .add_attr_option<ffi::String>("model") \
312  .add_attr_option<ffi::Array<ffi::String>>("libs") \
313  .add_attr_option<Target>("host") \
314  .add_attr_option<int64_t>("from_device") \
315  .add_attr_option<int64_t>("target_device_type")
316 
317 } // namespace tvm
318 
319 #endif // TVM_TARGET_TARGET_KIND_H_
Attribute map used in registry.
Generic attribute map.
Definition: attr_registry_map.h:38
ffi::Map<Key, ValueType> used to store meta-data.
Definition: attr_registry_map.h:105
ValueType get(const TargetKind &key, ValueType def_value) const
get the corresponding value element at key with default value.
Definition: attr_registry_map.h:136
int count(const TargetKind &key) const
Check if the map has op as key.
Definition: attr_registry_map.h:117
Definition: instruction.h:30
ffi::Map<TargetKind, ValueType> used to store meta-information about TargetKind
Definition: target_kind.h:135
TargetKindAttrMap(const AttrRegistryMapContainerMap< TargetKind > &map)
Definition: target_kind.h:141
Target kind, specifies the kind of the target.
Definition: target_kind.h:57
int default_device_type
Device type of target kind.
Definition: target_kind.h:62
FTargetCanonicalizer target_canonicalizer
Function used to canonicalize a JSON target during creation.
Definition: target_kind.h:66
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("target.TargetKind", TargetKindNode, Object)
friend class TargetInternal
Definition: target_kind.h:96
ffi::String name
Name of the target kind.
Definition: target_kind.h:60
ffi::Array< ffi::String > default_keys
Default keys of the target.
Definition: target_kind.h:64
static void RegisterReflection()
Definition: target_kind.h:68
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: target_kind.h:78
Helper structure to register TargetKind.
Definition: target_kind.h:154
TargetKindRegEntry & set_name()
Set name of the TargetKind to be the same as registry if it is empty.
Definition: target_kind.h:275
static ffi::Map< ffi::String, ffi::String > ListTargetKindOptions(const TargetKind &kind)
Get all supported option names and types for a given Target kind.
TargetKindRegEntry & add_attr_option(const ffi::String &key, Traits &&... traits)
Register a valid configuration option and its ValueType for validation.
Definition: target_kind.h:269
TargetKindRegEntry & set_default_keys(std::vector< ffi::String > keys)
Set DLPack's device_type the target.
Definition: target_kind.h:256
static TargetKindRegEntry & RegisterOrGet(const ffi::String &target_kind_name)
Register or get a new entry.
TargetKindRegEntry & set_target_canonicalizer(FTargetCanonicalizer canonicalizer)
Set the canonicalizer function applied upon target creation.
Definition: target_kind.h:261
static ffi::Array< ffi::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:251
TargetKindRegEntry & set_attr(const ffi::String &attr_name, const ValueType &value, int plevel=10)
Register additional attributes to target_kind.
Definition: target_kind.h:242
Managed reference class to TargetKindNode.
Definition: target_kind.h:103
TargetKind(ObjectPtr< TargetKindNode > data)
Definition: target_kind.h:106
friend class TargetInternal
Definition: target_kind.h:127
static TargetKindAttrMap< ValueType > GetAttrMap(const ffi::String &attr_name)
Get the attribute map given the attribute name.
Definition: target_kind.h:237
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TargetKind, ObjectRef, TargetKindNode)
TargetKindNode * operator->()
Mutable access to the container class
Definition: target_kind.h:119
TargetKind()=default
static ffi::Optional< TargetKind > Get(const ffi::String &target_kind_name)
Retrieve the TargetKind given its name.
ConfigSchema & def_option(const ffi::String &key, Traits &&... traits)
Declare a typed option.
Definition: config_schema.h:87
void set_canonicalizer(Canonicalizer f)
Set whole-object canonicalizer.
Definition: config_schema.h:98
Minimal schema for dynamic config canonicalization and validation.
Definition: repr_printer.h:91
constexpr const char * device_type
The device type.
Definition: stmt.h:913
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37
ffi::TypedFunction< ffi::Map< ffi::String, ffi::Any >(ffi::Map< ffi::String, ffi::Any >)> FTargetCanonicalizer
Target canonicalizer applied on instantiation of a given TargetKind.
Definition: target_kind.h:49
A managed object in the TVM runtime.