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>
30 #include <tvm/node/cast.h>
31 #include <tvm/runtime/device_api.h>
32 #include <tvm/support/with.h>
33 #include <tvm/target/target_kind.h>
34 
35 #include <string>
36 
37 namespace tvm {
38 
39 class TargetInternal;
40 class Target;
41 
46 class TargetNode : public Object {
47  public:
51  ffi::Optional<ObjectRef> host;
53  ffi::String tag;
55  ffi::Array<ffi::String> keys;
57  ffi::Map<ffi::String, Any> attrs;
58 
63  TVM_DLL const std::string& str() const;
65  TVM_DLL ffi::Map<ffi::String, ffi::Any> ToConfig() const;
67  TVM_DLL ffi::Optional<Target> GetHost() const;
69  TVM_DLL int GetTargetDeviceType() const;
70 
79  TVM_DLL bool HasKey(const std::string& query_key) const;
80 
81  static void RegisterReflection() {
82  namespace refl = tvm::ffi::reflection;
83  refl::ObjectDef<TargetNode>()
84  .def_ro("kind", &TargetNode::kind)
85  .def_ro("tag", &TargetNode::tag)
86  .def_ro("keys", &TargetNode::keys)
87  .def_ro("attrs", &TargetNode::attrs)
88  .def_ro("host", &TargetNode::host);
89  }
90 
98  template <typename TObjectRef>
99  ffi::Optional<TObjectRef> GetAttr(
100  const std::string& attr_key,
101  ffi::Optional<TObjectRef> default_value = ffi::Optional<TObjectRef>(std::nullopt)) const {
102  auto it = attrs.find(attr_key);
103  if (it != attrs.end()) {
104  return Downcast<ffi::Optional<TObjectRef>>((*it).second);
105  } else {
106  return default_value;
107  }
108  }
116  template <typename TObjectRef>
117  ffi::Optional<TObjectRef> GetAttr(const std::string& attr_key, TObjectRef default_value) const {
118  return GetAttr<TObjectRef>(attr_key, ffi::Optional<TObjectRef>(default_value));
119  }
120 
121  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
123 
124  private:
126  mutable std::string str_repr_;
127 
128  friend class TargetInternal;
129 };
130 
135 class Target : public ObjectRef {
136  public:
138  TVM_DLL explicit Target(std::nullptr_t) { data_ = nullptr; }
143  TVM_DLL explicit Target(const ffi::String& tag_or_config_or_target_str);
148  TVM_DLL explicit Target(const ffi::Map<ffi::String, ffi::Any>& config);
157  TVM_DLL static tvm::Target Current(bool allow_not_defined = true);
163  TVM_DLL explicit Target(Target target, Target host);
165 
166  static Target WithHost(const Target& target, const Target& host);
167 
170 
171  private:
172  Target(TargetKind kind, ffi::Optional<ObjectRef> host, ffi::String tag,
173  ffi::Array<ffi::String> keys, ffi::Map<ffi::String, ffi::Any> attrs);
174 
175  // enable with syntax.
176  friend class TargetInternal;
177  friend class With<Target>;
183  TVM_DLL void EnterWithScope();
188  TVM_DLL void ExitWithScope();
189 };
190 
199 
200 } // namespace tvm
201 #endif // TVM_TARGET_TARGET_H_
Value casting helpers.
Managed reference class to TargetKindNode.
Definition: target_kind.h:103
Compilation target.
Definition: target.h:46
int GetTargetDeviceType() const
ffi::Optional< TObjectRef > GetAttr(const std::string &attr_key, TObjectRef default_value) const
Get an entry from attrs of the target.
Definition: target.h:117
const std::string & str() const
The JSON string representation of the target.
ffi::String tag
Tag of the target, can be empty.
Definition: target.h:53
ffi::Map< ffi::String, Any > attrs
Collection of attributes (includes feature.* keys set by canonicalizer)
Definition: target.h:57
static void RegisterReflection()
Definition: target.h:81
ffi::Optional< Target > GetHost() const
friend class TargetInternal
Definition: target.h:128
ffi::Array< ffi::String > keys
Keys for this target.
Definition: target.h:55
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: target.h:121
bool HasKey(const std::string &query_key) const
Check if the target contains a key.
ffi::Optional< ObjectRef > host
Target host information, must be Target type.
Definition: target.h:51
TargetKind kind
The kind of the target device.
Definition: target.h:49
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:99
ffi::Map< ffi::String, ffi::Any > ToConfig() const
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("target.Target", TargetNode, Object)
Managed reference class to TargetNode.
Definition: target.h:135
Target(Target target, Target host)
Construct a Target given target and host.
static Target WithHost(const Target &target, const Target &host)
Target(std::nullptr_t)
Construct a null Target.
Definition: target.h:138
Target WithoutHost() const
friend class TargetInternal
Definition: target.h:176
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:59
Abstract device memory management API.
Base expr nodes in TVM.
Function nodes.
Definition: repr_printer.h:91
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
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 ...
Target kind registry.
RAII wrapper function to enter and exit a context object similar to python's with syntax.