24#ifndef TVM_TARGET_TARGET_H_
25#define TVM_TARGET_TARGET_H_
27#include <tvm/ffi/reflection/registry.h>
52 ffi::Optional<ffi::ObjectRef>
host;
56 ffi::Array<ffi::String>
keys;
58 ffi::Map<ffi::String, Any>
attrs;
68 ffi::Optional<Target>
GetHost()
const;
71 if (ffi::Optional<int64_t> device_type = GetAttr<int64_t>(
"target_device_type")) {
72 return static_cast<int>(device_type.value());
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; });
91 namespace refl = tvm::ffi::reflection;
92 refl::ObjectDef<TargetObj>()
107 template <
typename TObjectRef>
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>>();
115 return default_value;
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));
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));
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)));
173 explicit Target(ffi::UnsafeInit tag, ffi::ObjectPtr<TargetObj> n) : ffi::ObjectRef(tag) {
174 data_ = std::move(n);
183 if ((*this)->GetHost()) {
184 return WithHost(*
this, std::nullopt);
198inline constexpr ffi::reflection::AttrKey<ffi::Map<ffi::String, ffi::Any>> kBackendConfig{
#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