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  ffi::Optional<ObjectRef> host;
56  ffi::String tag;
58  ffi::Array<ffi::String> keys;
60  ffi::Map<ffi::String, Any> attrs;
62  ffi::Map<ffi::String, Any> features;
63 
69  TVM_DLL const std::string& str() const;
71  TVM_DLL ffi::Map<ffi::String, ffi::Any> Export() const;
73  TVM_DLL ffi::Optional<Target> GetHost() const;
75  TVM_DLL int GetTargetDeviceType() const;
76 
85  TVM_DLL bool HasKey(const std::string& query_key) const;
86 
94  ffi::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  ffi::Optional<TObjectRef> GetAttr(
116  const std::string& attr_key,
117  ffi::Optional<TObjectRef> default_value = ffi::Optional<TObjectRef>(std::nullopt)) const {
118  auto it = attrs.find(attr_key);
119  if (it != attrs.end()) {
120  return Downcast<ffi::Optional<TObjectRef>>((*it).second);
121  } else {
122  return default_value;
123  }
124  }
132  template <typename TObjectRef>
133  ffi::Optional<TObjectRef> GetAttr(const std::string& attr_key, TObjectRef default_value) const {
134  return GetAttr<TObjectRef>(attr_key, ffi::Optional<TObjectRef>(default_value));
135  }
136 
156  template <typename TObjectRef>
157  ffi::Optional<TObjectRef> GetFeature(
158  const std::string& feature_key,
159  ffi::Optional<TObjectRef> default_value = std::nullopt) const {
160  if (auto feature = features.Get(feature_key)) {
161  return Downcast<TObjectRef>(feature.value());
162  } else {
163  return default_value;
164  }
165  }
166  // variant that uses TObjectRef to enable implicit conversion to default value.
167  template <typename TObjectRef>
168  ffi::Optional<TObjectRef> GetFeature(const std::string& attr_key,
169  TObjectRef default_value) const {
170  return GetFeature<TObjectRef>(attr_key, ffi::Optional<TObjectRef>(default_value));
171  }
172 
174  TVM_DLL std::vector<std::string> GetKeys() const;
176  TVM_DLL std::unordered_set<std::string> GetLibs() const;
177 
178  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
180 
181  private:
183  mutable std::string str_repr_;
184 
185  friend class TargetInternal;
186 };
187 
192 class Target : public ObjectRef {
193  public:
195  TVM_DLL explicit Target(std::nullptr_t) { data_ = nullptr; }
200  TVM_DLL explicit Target(const ffi::String& tag_or_config_or_target_str);
205  TVM_DLL explicit Target(const ffi::Map<ffi::String, ffi::Any>& config);
214  TVM_DLL static tvm::Target Current(bool allow_not_defined = true);
220  TVM_DLL explicit Target(Target target, Target host);
228  static Target WithHost(const Target& target, const Target& host);
229 
232 
233  private:
234  Target(TargetKind kind, ffi::Optional<ObjectRef> host, ffi::String tag,
235  ffi::Array<ffi::String> keys, ffi::Map<ffi::String, ffi::Any> attrs);
236 
237  // enable with syntax.
238  friend class TargetInternal;
239  friend class With<Target>;
245  TVM_DLL void EnterWithScope();
250  TVM_DLL void ExitWithScope();
251 };
252 
261 
262 } // namespace tvm
263 #endif // TVM_TARGET_TARGET_H_
Attribute map used in registry.
Managed reference class to TargetKindNode.
Definition: target_kind.h:126
Compilation target.
Definition: target.h:49
int GetTargetDeviceType() const
std::unordered_set< std::string > GetLibs() const
Get the keys for this target as an unordered_set of string.
ffi::Optional< TObjectRef > GetAttr(const std::string &attr_key, TObjectRef default_value) const
Get an entry from attrs of the target.
Definition: target.h:133
const std::string & str() const
The raw string representation of the target.
ffi::String tag
Tag of the target, can be empty.
Definition: target.h:56
ffi::Map< ffi::String, Any > attrs
Collection of attributes.
Definition: target.h:60
static void RegisterReflection()
Definition: target.h:96
ffi::Map< ffi::String, Any > features
Target features.
Definition: target.h:62
ffi::Optional< Target > GetHost() const
ffi::String ToDebugString() const
Returns a human readable representation of Target which includes all fields, especially the host....
friend class TargetInternal
Definition: target.h:185
ffi::Array< ffi::String > keys
Keys for this target.
Definition: target.h:58
ffi::Optional< TObjectRef > GetFeature(const std::string &feature_key, ffi::Optional< TObjectRef > default_value=std::nullopt) const
Get a Target feature.
Definition: target.h:157
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: target.h:178
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.
ffi::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
ffi::Optional< TObjectRef > GetAttr(const std::string &attr_key, ffi::Optional< TObjectRef > default_value=ffi::Optional< TObjectRef >(std::nullopt)) const
Get an entry from attrs of the target.
Definition: target.h:115
ffi::Optional< TObjectRef > GetFeature(const std::string &attr_key, TObjectRef default_value) const
Definition: target.h:168
ffi::Map< ffi::String, ffi::Any > Export() const
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("target.Target", TargetNode, Object)
Managed reference class to TargetNode.
Definition: target.h:192
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:195
Target WithoutHost() const
friend class TargetInternal
Definition: target.h:238
Target(const ffi::Map< ffi::String, ffi::Any > &config)
Construct a Target using a JSON-like configuration.
Target(const ffi::String &tag_or_config_or_target_str)
Construct a Target given a string.
static tvm::Target Current(bool allow_not_defined=true)
Get the current target context from thread local storage.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Target, ObjectRef, TargetNode)
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.