tvm
Loading...
Searching...
No Matches
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>
32
33#include <algorithm>
34#include <string>
35
36namespace tvm {
37
38class TargetInternal;
39class Target;
40
45class TargetObj : public ffi::Object {
46 public:
47 explicit TargetObj(TargetKind kind) : kind(std::move(kind)) {}
48 explicit TargetObj(ffi::UnsafeInit tag) : kind(tag) {}
52 ffi::Optional<ffi::ObjectRef> host;
54 ffi::String tag;
56 ffi::Array<ffi::String> keys;
58 ffi::Map<ffi::String, Any> attrs;
59
64 TVM_TIRX_DLL ffi::String str() const;
66 TVM_TIRX_DLL ffi::Map<ffi::String, ffi::Any> ToConfig() const;
68 ffi::Optional<Target> GetHost() const;
70 int GetTargetDeviceType() const {
71 if (ffi::Optional<int64_t> device_type = GetAttr<int64_t>("target_device_type")) {
72 return static_cast<int>(device_type.value());
73 }
75 }
76
85 bool HasKey(const ffi::String& query_key) const {
86 return std::any_of(keys.begin(), keys.end(),
87 [&query_key](const auto& key) { return key == query_key; });
88 }
89
90 static void RegisterReflection() {
91 namespace refl = tvm::ffi::reflection;
92 refl::ObjectDef<TargetObj>()
93 .def_ro("kind", &TargetObj::kind)
94 .def_ro("tag", &TargetObj::tag)
95 .def_ro("keys", &TargetObj::keys)
96 .def_ro("attrs", &TargetObj::attrs)
97 .def_ro("host", &TargetObj::host);
98 }
99
107 template <typename TObjectRef>
108 ffi::Optional<TObjectRef> GetAttr(
109 const std::string& attr_key,
110 ffi::Optional<TObjectRef> default_value = ffi::Optional<TObjectRef>(std::nullopt)) const {
111 auto it = attrs.find(attr_key);
112 if (it != attrs.end()) {
113 return (*it).second.as_or_throw<ffi::Optional<TObjectRef>>();
114 } else {
115 return default_value;
116 }
117 }
125 template <typename TObjectRef>
126 ffi::Optional<TObjectRef> GetAttr(const std::string& attr_key, TObjectRef default_value) const {
127 return GetAttr<TObjectRef>(attr_key, ffi::Optional<TObjectRef>(default_value));
128 }
129
131 template <typename T>
132 ffi::Optional<T> GetAttr(ffi::reflection::AttrKey<T> key,
133 ffi::Optional<typename ffi::reflection::AttrKey<T>::ValueType>
134 default_value = std::nullopt) const {
135 return GetAttr<T>(key.name, std::move(default_value));
136 }
137
139 template <typename T>
140 ffi::Optional<T> GetAttr(ffi::reflection::AttrKey<T> key,
141 typename ffi::reflection::AttrKey<T>::ValueType default_value) const {
142 return GetAttr(key, ffi::Optional<T>(std::move(default_value)));
143 }
144
145 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
146 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("target.Target", TargetObj, ffi::Object);
147 // Shared instances are mutated in place, so the class is mutable.
148 static constexpr bool _type_mutable = true;
149};
150
155class Target : public ffi::ObjectRef {
156 public:
161 TVM_TIRX_DLL explicit Target(const ffi::String& tag_or_config_or_target_str);
166 TVM_TIRX_DLL explicit Target(const ffi::Map<ffi::String, ffi::Any>& config);
172 explicit Target(Target target, ffi::Optional<Target> host) : Target(WithHost(target, host)) {}
173 explicit Target(ffi::UnsafeInit tag, ffi::ObjectPtr<TargetObj> n) : ffi::ObjectRef(tag) {
174 data_ = std::move(n);
175 }
176
178
179 TVM_TIRX_DLL static Target WithHost(const Target& target, const ffi::Optional<Target>& host);
180
183 if ((*this)->GetHost()) {
184 return WithHost(*this, std::nullopt);
185 } else {
186 return *this;
187 }
188 }
189
190 private:
191 friend class TargetInternal;
192};
193
194inline ffi::Optional<Target> TargetObj::GetHost() const { return this->host.as<Target>(); }
195
196namespace attr {
198inline constexpr ffi::reflection::AttrKey<ffi::Map<ffi::String, ffi::Any>> kBackendConfig{
199 "backend_config"};
200} // namespace attr
201
202} // namespace tvm
203#endif // TVM_TARGET_TARGET_H_
Base expr nodes in TVM.
Function nodes.
#define TVM_TIRX_DLL
Definition base.h:55
int default_device_type
Device type of target kind.
Definition target_kind.h:57
Managed reference class to TargetKindObj.
Definition target_kind.h:88
Compilation target.
Definition target.h:45
TVM_TIRX_DLL ffi::String str() const
The JSON string representation of the target.
bool HasKey(const ffi::String &query_key) const
Check if the target contains a key.
Definition target.h:85
ffi::Optional< ffi::ObjectRef > host
Target host information, must be Target type.
Definition target.h:52
static void RegisterReflection()
Definition target.h:90
TargetObj(TargetKind kind)
Definition target.h:47
ffi::Optional< T > GetAttr(ffi::reflection::AttrKey< T > key, ffi::Optional< typename ffi::reflection::AttrKey< T >::ValueType > default_value=std::nullopt) const
Get an optional Target attribute using the value type declared by its key.
Definition target.h:132
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("target.Target", TargetObj, ffi::Object)
ffi::Optional< TObjectRef > GetAttr(const std::string &attr_key, TObjectRef default_value) const
Get an entry from attrs of the target.
Definition target.h:126
ffi::Optional< T > GetAttr(ffi::reflection::AttrKey< T > key, typename ffi::reflection::AttrKey< T >::ValueType default_value) const
Use a default value when the typed Target attribute is absent.
Definition target.h:140
TargetObj(ffi::UnsafeInit tag)
Definition target.h:48
int GetTargetDeviceType() const
Definition target.h:70
static constexpr bool _type_mutable
Definition target.h:148
ffi::Optional< Target > GetHost() const
Definition target.h:194
ffi::Array< ffi::String > keys
Keys for this target.
Definition target.h:56
ffi::String tag
Tag of the target, can be empty.
Definition target.h:54
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition target.h:145
TargetKind kind
The kind of the target device.
Definition target.h:50
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:108
TVM_TIRX_DLL ffi::Map< ffi::String, ffi::Any > ToConfig() const
ffi::Map< ffi::String, Any > attrs
Collection of attributes (includes feature.* keys set by canonicalizer)
Definition target.h:58
Managed reference class to TargetObj.
Definition target.h:155
TVM_TIRX_DLL Target(const ffi::Map< ffi::String, ffi::Any > &config)
Construct a Target using a JSON-like configuration.
Target(ffi::UnsafeInit tag, ffi::ObjectPtr< TargetObj > n)
Definition target.h:173
Target WithoutHost() const
Definition target.h:182
static TVM_TIRX_DLL Target WithHost(const Target &target, const ffi::Optional< Target > &host)
TVM_TIRX_DLL Target(const ffi::String &tag_or_config_or_target_str)
Construct a Target given a string.
friend class TargetInternal
Definition target.h:191
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Target, ffi::ObjectRef, TargetObj)
Target(Target target, ffi::Optional< Target > host)
Construct a Target given target and host.
Definition target.h:172
Abstract device memory management API.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
Target kind registry.