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/ir/with_context.h>
31 #include <tvm/runtime/device_api.h>
32 #include <tvm/target/target_kind.h>
33 
34 #include <string>
35 
36 namespace tvm {
37 
38 class TargetInternal;
39 class Target;
40 
45 class TargetNode : public ffi::Object {
46  public:
50  ffi::Optional<ffi::ObjectRef> host;
52  ffi::String tag;
54  ffi::Array<ffi::String> keys;
56  ffi::Map<ffi::String, Any> attrs;
57 
62  TVM_DLL const std::string& str() const;
64  TVM_DLL ffi::Map<ffi::String, ffi::Any> ToConfig() const;
66  TVM_DLL ffi::Optional<Target> GetHost() const;
68  TVM_DLL int GetTargetDeviceType() const;
69 
78  TVM_DLL bool HasKey(const std::string& query_key) const;
79 
80  static void RegisterReflection() {
81  namespace refl = tvm::ffi::reflection;
82  refl::ObjectDef<TargetNode>()
83  .def_ro("kind", &TargetNode::kind)
84  .def_ro("tag", &TargetNode::tag)
85  .def_ro("keys", &TargetNode::keys)
86  .def_ro("attrs", &TargetNode::attrs)
87  .def_ro("host", &TargetNode::host);
88  }
89 
97  template <typename TObjectRef>
98  ffi::Optional<TObjectRef> GetAttr(
99  const std::string& attr_key,
100  ffi::Optional<TObjectRef> default_value = ffi::Optional<TObjectRef>(std::nullopt)) const {
101  auto it = attrs.find(attr_key);
102  if (it != attrs.end()) {
103  return (*it).second.as_or_throw<ffi::Optional<TObjectRef>>();
104  } else {
105  return default_value;
106  }
107  }
115  template <typename TObjectRef>
116  ffi::Optional<TObjectRef> GetAttr(const std::string& attr_key, TObjectRef default_value) const {
117  return GetAttr<TObjectRef>(attr_key, ffi::Optional<TObjectRef>(default_value));
118  }
119 
120  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
121  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("target.Target", TargetNode, ffi::Object);
122 
123  private:
125  mutable std::string str_repr_;
126 
127  friend class TargetInternal;
128 };
129 
134 class Target : public ffi::ObjectRef {
135  public:
137  TVM_DLL explicit Target(std::nullptr_t) { data_ = nullptr; }
142  TVM_DLL explicit Target(const ffi::String& tag_or_config_or_target_str);
147  TVM_DLL explicit Target(const ffi::Map<ffi::String, ffi::Any>& config);
156  TVM_DLL static tvm::Target Current(bool allow_not_defined = true);
162  TVM_DLL explicit Target(Target target, Target host);
164 
165  static Target WithHost(const Target& target, const Target& host);
166 
169 
170  private:
171  Target(TargetKind kind, ffi::Optional<ffi::ObjectRef> host, ffi::String tag,
172  ffi::Array<ffi::String> keys, ffi::Map<ffi::String, ffi::Any> attrs);
173 
174  // enable with syntax.
175  friend class TargetInternal;
176  friend class With<Target>;
182  TVM_DLL void EnterWithScope();
187  TVM_DLL void ExitWithScope();
188 };
189 
198 
199 } // namespace tvm
200 #endif // TVM_TARGET_TARGET_H_
Managed reference class to TargetKindNode.
Definition: target_kind.h:103
Compilation target.
Definition: target.h:45
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:116
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:52
ffi::Map< ffi::String, Any > attrs
Collection of attributes (includes feature.* keys set by canonicalizer)
Definition: target.h:56
ffi::Optional< ffi::ObjectRef > host
Target host information, must be Target type.
Definition: target.h:50
static void RegisterReflection()
Definition: target.h:80
ffi::Optional< Target > GetHost() const
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("target.Target", TargetNode, ffi::Object)
friend class TargetInternal
Definition: target.h:127
ffi::Array< ffi::String > keys
Keys for this target.
Definition: target.h:54
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: target.h:120
bool HasKey(const std::string &query_key) const
Check if the target contains a key.
TargetKind kind
The kind of the target device.
Definition: target.h:48
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:98
ffi::Map< ffi::String, ffi::Any > ToConfig() const
Managed reference class to TargetNode.
Definition: target.h:134
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:137
Target WithoutHost() const
friend class TargetInternal
Definition: target.h:175
Target(const ffi::Map< ffi::String, ffi::Any > &config)
Construct a Target using a JSON-like configuration.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Target, ffi::ObjectRef, TargetNode)
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.
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition: with_context.h:59
Abstract device memory management API.
Base expr nodes in TVM.
Function nodes.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
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.