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/ir/expr.h>
28 #include <tvm/ir/module.h>
29 #include <tvm/node/node.h>
30 #include <tvm/support/with.h>
31 #include <tvm/target/target_kind.h>
32 
33 #include <string>
34 #include <unordered_set>
35 #include <vector>
36 
37 namespace tvm {
38 
39 class TargetInternal;
40 class Target;
41 
46 class TargetNode : public Object {
47  public:
63  TVM_DLL const std::string& str() const;
65  TVM_DLL Map<String, ObjectRef> Export() const;
67  TVM_DLL Optional<Target> GetHost() const;
68 
76  String ToDebugString() const;
77 
79  v->Visit("kind", &kind);
80  v->Visit("tag", &tag);
81  v->Visit("keys", &keys);
82  v->Visit("attrs", &attrs);
83  v->Visit("host", &host);
84  }
85 
93  template <typename TObjectRef>
95  const std::string& attr_key,
96  Optional<TObjectRef> default_value = Optional<TObjectRef>(nullptr)) const {
97  static_assert(std::is_base_of<ObjectRef, TObjectRef>::value,
98  "Can only call GetAttr with ObjectRef types.");
99  auto it = attrs.find(attr_key);
100  if (it != attrs.end()) {
101  return Downcast<Optional<TObjectRef>>((*it).second);
102  } else {
103  return default_value;
104  }
105  }
113  template <typename TObjectRef>
114  Optional<TObjectRef> GetAttr(const std::string& attr_key, TObjectRef default_value) const {
115  return GetAttr<TObjectRef>(attr_key, Optional<TObjectRef>(default_value));
116  }
118  TVM_DLL std::vector<std::string> GetKeys() const;
120  TVM_DLL std::unordered_set<std::string> GetLibs() const;
121 
122  bool SEqualReduce(const TargetNode* other, SEqualReducer equal) const;
123  void SHashReduce(SHashReducer hash_reduce) const;
124 
125  static constexpr const char* _type_key = "Target";
126  static constexpr const bool _type_has_method_sequal_reduce = true;
127  static constexpr const bool _type_has_method_shash_reduce = true;
129 
130  private:
132  mutable std::string str_repr_;
133 
134  friend class TargetInternal;
135 };
136 
141 class Target : public ObjectRef {
142  public:
144  TVM_DLL explicit Target(std::nullptr_t) { data_ = nullptr; }
149  TVM_DLL explicit Target(const String& tag_or_config_or_target_str);
154  TVM_DLL explicit Target(const Map<String, ObjectRef>& config);
163  TVM_DLL static tvm::Target Current(bool allow_not_defined = true);
170  TVM_DLL explicit Target(Target target, Target host);
178  static Target WithHost(const Target& target, const Target& host);
179 
186  bool IsExternalCodegen() const;
187 
202  bool IsExternalCodegenFor(const Target& that) const;
203 
204  private:
205  Target(TargetKind kind, Optional<ObjectRef> host, String tag, Array<String> keys,
206  Map<String, ObjectRef> attrs);
207 
208  // enable with syntax.
209  friend class TargetInternal;
210  friend class With<Target>;
216  TVM_DLL void EnterWithScope();
221  TVM_DLL void ExitWithScope();
222 };
223 
231 void CheckAndUpdateHostConsistency(Target* target, Target* host);
232 
241 
242 } // namespace tvm
243 #endif // TVM_TARGET_TARGET_H_
friend class TargetInternal
Definition: target.h:134
std::unordered_set< std::string > GetLibs() const
Get the keys for this target as an unordered_set of string.
TVM_DECLARE_FINAL_OBJECT_INFO(TargetNode, Object)
Definitions and helper macros for IR/AST nodes.
Compilation target.
Definition: target.h:46
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:102
Target(std::nullptr_t)
Construct a null Target.
Definition: target.h:144
Base expr nodes in TVM.
IRModule that holds the functions and type definitions.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:102
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
Optional< Target > GetHost() const
void SHashReduce(SHashReducer hash_reduce) const
const std::string & str() const
The raw string representation of the target.
bool SEqualReduce(const TargetNode *other, SEqualReducer equal) const
base class of all object containers.
Definition: object.h:167
TargetKind kind
The kind of the target device.
Definition: target.h:49
String tag
Tag of the the target, can be empty.
Definition: target.h:53
Managed reference class to TargetKindNode.
Definition: target_kind.h:128
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
Target kind registry.
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:270
iterator find(const K &key) const
Definition: map.h:1380
String ToDebugString() const
Returns a human readable representation of Target which includes all fields, especially the host...
static constexpr const bool _type_has_method_sequal_reduce
Definition: target.h:126
Reference to string objects.
Definition: string.h:124
Optional< TObjectRef > GetAttr(const std::string &attr_key, TObjectRef default_value) const
Get an entry from attrs of the target.
Definition: target.h:114
#define TVM_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:713
std::vector< std::string > GetKeys() const
Get the keys for this target as a vector of string.
RAII wrapper function to enter and exit a context object similar to python&#39;s with syntax...
Definition: with.h:57
Managed reference class to TargetNode.
Definition: target.h:141
Map< String, ObjectRef > attrs
Collection of attributes.
Definition: target.h:57
Array< String > keys
Keys for this target.
Definition: target.h:55
Map< String, ObjectRef > Export() const
Base class of all object reference.
Definition: object.h:511
void VisitAttrs(AttrVisitor *v)
Definition: target.h:78
static constexpr const bool _type_has_method_shash_reduce
Definition: target.h:127
Optional< ObjectRef > host
Target host information, must be Target type.
Definition: target.h:51
iterator end() const
Definition: map.h:1378
static constexpr const char * _type_key
Definition: target.h:125
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
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
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:94
RAII wrapper function to enter and exit a context object similar to python&#39;s with syntax...
void CheckAndUpdateHostConsistency(Target *target, Target *host)
Check and update host field of the given legacy target and target host pair. Note that this function ...