tvm
target.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_H_
25 #define TVM_TARGET_TARGET_H_
26 
27 #include <tvm/ffi/reflection/registry.h>
28 #include <tvm/ir/expr.h>
29 #include <tvm/ir/function.h>
31 #include <tvm/node/node.h>
32 #include <tvm/runtime/device_api.h>
33 #include <tvm/support/with.h>
34 #include <tvm/target/target_kind.h>
35 
36 #include <string>
37 #include <unordered_set>
38 #include <vector>
39 
40 namespace tvm {
41 
42 class TargetInternal;
43 class Target;
44 
49 class TargetNode : public Object {
50  public:
54  Optional<ObjectRef> host;
56  String tag;
58  Array<String> keys;
60  Map<String, Any> attrs;
62  Map<String, Any> features;
63 
69  TVM_DLL const std::string& str() const;
71  TVM_DLL Map<String, ffi::Any> Export() const;
73  TVM_DLL Optional<Target> GetHost() const;
75  TVM_DLL int GetTargetDeviceType() const;
76 
85  TVM_DLL bool HasKey(const std::string& query_key) const;
86 
94  String ToDebugString() const;
95 
96  static void RegisterReflection() {
97  namespace refl = tvm::ffi::reflection;
98  refl::ObjectDef<TargetNode>()
99  .def_ro("kind", &TargetNode::kind)
100  .def_ro("tag", &TargetNode::tag)
101  .def_ro("keys", &TargetNode::keys)
102  .def_ro("attrs", &TargetNode::attrs)
103  .def_ro("features", &TargetNode::features)
104  .def_ro("host", &TargetNode::host);
105  }
106 
114  template <typename TObjectRef>
115  Optional<TObjectRef> GetAttr(
116  const std::string& attr_key,
117  Optional<TObjectRef> default_value = Optional<TObjectRef>(std::nullopt)) const {
118  auto it = attrs.find(attr_key);
119  if (it != attrs.end()) {
120  return Downcast<Optional<TObjectRef>>((*it).second);
121  } else {
122  return default_value;
123  }
124  }
132  template <typename TObjectRef>
133  Optional<TObjectRef> GetAttr(const std::string& attr_key, TObjectRef default_value) const {
134  return GetAttr<TObjectRef>(attr_key, Optional<TObjectRef>(default_value));
135  }
136 
156  template <typename TObjectRef>
157  Optional<TObjectRef> GetFeature(const std::string& feature_key,
158  Optional<TObjectRef> default_value = std::nullopt) const {
159  if (auto feature = features.Get(feature_key)) {
160  return Downcast<TObjectRef>(feature.value());
161  } else {
162  return default_value;
163  }
164  }
165  // variant that uses TObjectRef to enable implicit conversion to default value.
166  template <typename TObjectRef>
167  Optional<TObjectRef> GetFeature(const std::string& attr_key, TObjectRef default_value) const {
168  return GetFeature<TObjectRef>(attr_key, Optional<TObjectRef>(default_value));
169  }
170 
172  TVM_DLL std::vector<std::string> GetKeys() const;
174  TVM_DLL std::unordered_set<std::string> GetLibs() const;
175 
176  static constexpr const char* _type_key = "target.Target";
177  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
179 
180  private:
182  mutable std::string str_repr_;
183 
184  friend class TargetInternal;
185 };
186 
191 class Target : public ObjectRef {
192  public:
194  TVM_DLL explicit Target(std::nullptr_t) { data_ = nullptr; }
199  TVM_DLL explicit Target(const String& tag_or_config_or_target_str);
204  TVM_DLL explicit Target(const Map<String, ffi::Any>& config);
213  TVM_DLL static tvm::Target Current(bool allow_not_defined = true);
219  TVM_DLL explicit Target(Target target, Target host);
227  static Target WithHost(const Target& target, const Target& host);
228 
231 
232  private:
233  Target(TargetKind kind, Optional<ObjectRef> host, String tag, Array<String> keys,
234  Map<String, ffi::Any> attrs);
235 
236  // enable with syntax.
237  friend class TargetInternal;
238  friend class With<Target>;
244  TVM_DLL void EnterWithScope();
249  TVM_DLL void ExitWithScope();
250 };
251 
260 
261 } // namespace tvm
262 #endif // TVM_TARGET_TARGET_H_
Attribute map used in registry.
Managed reference class to TargetKindNode.
Definition: target_kind.h:127
Compilation target.
Definition: target.h:49
int GetTargetDeviceType() const
Optional< TObjectRef > GetAttr(const std::string &attr_key, TObjectRef default_value) const
Get an entry from attrs of the target.
Definition: target.h:133
std::unordered_set< std::string > GetLibs() const
Get the keys for this target as an unordered_set of string.
String tag
Tag of the target, can be empty.
Definition: target.h:56
const std::string & str() const
The raw string representation of the target.
String ToDebugString() const
Returns a human readable representation of Target which includes all fields, especially the host....
static constexpr const char * _type_key
Definition: target.h:176
Map< String, Any > features
Target features.
Definition: target.h:62
static void RegisterReflection()
Definition: target.h:96
friend class TargetInternal
Definition: target.h:184
Map< String, Any > attrs
Collection of attributes.
Definition: target.h:60
Map< String, ffi::Any > Export() const
TVM_DECLARE_FINAL_OBJECT_INFO(TargetNode, Object)
Optional< Target > GetHost() const
Optional< TObjectRef > GetAttr(const std::string &attr_key, Optional< TObjectRef > default_value=Optional< TObjectRef >(std::nullopt)) const
Get an entry from attrs of the target.
Definition: target.h:115
Optional< TObjectRef > GetFeature(const std::string &attr_key, TObjectRef default_value) const
Definition: target.h:167
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: target.h:177
bool HasKey(const std::string &query_key) const
Check if the target contains a key.
std::vector< std::string > GetKeys() const
Get the keys for this target as a vector of string.
Optional< ObjectRef > host
Target host information, must be Target type.
Definition: target.h:54
TargetKind kind
The kind of the target device.
Definition: target.h:52
Optional< TObjectRef > GetFeature(const std::string &feature_key, Optional< TObjectRef > default_value=std::nullopt) const
Get a Target feature.
Definition: target.h:157
Array< String > keys
Keys for this target.
Definition: target.h:58
Managed reference class to TargetNode.
Definition: target.h:191
Target(Target target, Target host)
Construct a Target given target and host.
static Target WithHost(const Target &target, const Target &host)
Create a new Target object with given target (w.o host) and target host.
Target(std::nullptr_t)
Construct a null Target.
Definition: target.h:194
Target WithoutHost() const
Target(const String &tag_or_config_or_target_str)
Construct a Target given a string.
friend class TargetInternal
Definition: target.h:237
static tvm::Target Current(bool allow_not_defined=true)
Get the current target context from thread local storage.
TVM_DEFINE_OBJECT_REF_METHODS(Target, ObjectRef, TargetNode)
Target(const Map< String, ffi::Any > &config)
Construct a Target using a JSON-like configuration.
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition: with.h:58
Abstract device memory management API.
Base expr nodes in TVM.
Function nodes.
Definition: repr_printer.h:91
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
void CheckAndUpdateHostConsistency(Target *target, Target *host)
Check and update host field of the given legacy target and target host pair. Note that this function ...
Definitions and helper macros for IR/AST nodes.
Target kind registry.
RAII wrapper function to enter and exit a context object similar to python's with syntax.