tvm
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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/ir/expr.h>
28 #include <tvm/node/node.h>
29 #include <tvm/support/with.h>
30 #include <tvm/target/target_kind.h>
31 
32 #include <string>
33 #include <unordered_set>
34 #include <vector>
35 
36 namespace tvm {
37 
38 class TargetInternal;
39 class Target;
40 
45 class TargetNode : public Object {
46  public:
59 
65  TVM_DLL const std::string& str() const;
67  TVM_DLL Map<String, ObjectRef> Export() const;
69  TVM_DLL Optional<Target> GetHost() const;
71  TVM_DLL int GetTargetDeviceType() const;
72 
81  TVM_DLL bool HasKey(const std::string& query_key) const;
82 
91 
93  v->Visit("kind", &kind);
94  v->Visit("tag", &tag);
95  v->Visit("keys", &keys);
96  v->Visit("attrs", &attrs);
97  v->Visit("features", &features);
98  v->Visit("host", &host);
99  }
100 
108  template <typename TObjectRef>
110  const std::string& attr_key,
111  Optional<TObjectRef> default_value = Optional<TObjectRef>(nullptr)) const {
112  static_assert(std::is_base_of<ObjectRef, TObjectRef>::value,
113  "Can only call GetAttr with ObjectRef types.");
114  auto it = attrs.find(attr_key);
115  if (it != attrs.end()) {
116  // For backwards compatibility, return through TVMRetValue.
117  // This triggers any automatic conversions registered with
118  // PackedFuncValueConverter. Importantly, this allows use of
119  // `GetAttr<Integer>` and `GetAttr<Bool>` for properties that
120  // are stored internally as `runtime::Box<int64_t>` and
121  // `runtime::Box<bool>`.
123  ret = (*it).second;
124  return ret;
125  } else {
126  return default_value;
127  }
128  }
136  template <typename TObjectRef>
137  Optional<TObjectRef> GetAttr(const std::string& attr_key, TObjectRef default_value) const {
138  return GetAttr<TObjectRef>(attr_key, Optional<TObjectRef>(default_value));
139  }
140 
160  template <typename TObjectRef>
162  const std::string& feature_key,
163  Optional<TObjectRef> default_value = Optional<TObjectRef>(nullptr)) const {
164  Optional<TObjectRef> feature = Downcast<Optional<TObjectRef>>(features.Get(feature_key));
165  if (!feature) {
166  return default_value;
167  }
168  return feature;
169  }
170  // variant that uses TObjectRef to enable implicit conversion to default value.
171  template <typename TObjectRef>
172  Optional<TObjectRef> GetFeature(const std::string& attr_key, TObjectRef default_value) const {
173  return GetFeature<TObjectRef>(attr_key, Optional<TObjectRef>(default_value));
174  }
175 
177  TVM_DLL std::vector<std::string> GetKeys() const;
179  TVM_DLL std::unordered_set<std::string> GetLibs() const;
180 
181  bool SEqualReduce(const TargetNode* other, SEqualReducer equal) const;
182  void SHashReduce(SHashReducer hash_reduce) const;
183 
184  static constexpr const char* _type_key = "Target";
185  static constexpr const bool _type_has_method_sequal_reduce = true;
186  static constexpr const bool _type_has_method_shash_reduce = true;
188 
189  private:
191  mutable std::string str_repr_;
192 
193  friend class TargetInternal;
194 };
195 
200 class Target : public ObjectRef {
201  public:
203  TVM_DLL explicit Target(std::nullptr_t) { data_ = nullptr; }
208  TVM_DLL explicit Target(const String& tag_or_config_or_target_str);
213  TVM_DLL explicit Target(const Map<String, ObjectRef>& config);
222  TVM_DLL static tvm::Target Current(bool allow_not_defined = true);
228  TVM_DLL explicit Target(Target target, Target host);
236  static Target WithHost(const Target& target, const Target& host);
237 
240 
241  private:
243  Map<String, ObjectRef> attrs);
244 
245  // enable with syntax.
246  friend class TargetInternal;
247  friend class With<Target>;
253  TVM_DLL void EnterWithScope();
258  TVM_DLL void ExitWithScope();
259 };
260 
269 
270 } // namespace tvm
271 #endif // TVM_TARGET_TARGET_H_
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
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:135
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:121
Managed reference class to TargetKindNode.
Definition: target_kind.h:120
Compilation target.
Definition: target.h:45
Optional< TObjectRef > GetAttr(const std::string &attr_key, Optional< TObjectRef > default_value=Optional< TObjectRef >(nullptr)) const
Get an entry from attrs of the target.
Definition: target.h:109
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:137
static constexpr const bool _type_has_method_shash_reduce
Definition: target.h:186
void SHashReduce(SHashReducer hash_reduce) const
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:52
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:184
static constexpr const bool _type_has_method_sequal_reduce
Definition: target.h:185
Map< String, ObjectRef > attrs
Collection of attributes.
Definition: target.h:56
Optional< TObjectRef > GetFeature(const std::string &feature_key, Optional< TObjectRef > default_value=Optional< TObjectRef >(nullptr)) const
Get a Target feature.
Definition: target.h:161
friend class TargetInternal
Definition: target.h:193
TVM_DECLARE_FINAL_OBJECT_INFO(TargetNode, Object)
Optional< Target > GetHost() const
Map< String, ObjectRef > features
Target features.
Definition: target.h:58
Optional< TObjectRef > GetFeature(const std::string &attr_key, TObjectRef default_value) const
Definition: target.h:172
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:50
TargetKind kind
The kind of the target device.
Definition: target.h:48
bool SEqualReduce(const TargetNode *other, SEqualReducer equal) const
void VisitAttrs(AttrVisitor *v)
Definition: target.h:92
Array< String > keys
Keys for this target.
Definition: target.h:54
Map< String, ObjectRef > Export() const
Managed reference class to TargetNode.
Definition: target.h:200
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:203
Target WithoutHost() const
Target(const String &tag_or_config_or_target_str)
Construct a Target given a string.
friend class TargetInternal
Definition: target.h:246
Target(const Map< String, ObjectRef > &config)
Construct a Target using a JSON-like configuration.
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)
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition: with.h:58
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:289
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
Base class of all object reference.
Definition: object.h:520
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:606
base class of all object containers.
Definition: object.h:172
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Reference to string objects.
Definition: string.h:97
Return Value container, Unlike TVMArgValue, which only holds reference and do not delete the underlyi...
Definition: packed_func.h:946
Base expr nodes in TVM.
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
PrimExpr ret(PrimExpr value, Span span=Span())
Return the value.
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
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.