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>
30#include <tvm/ir/with_context.h>
33
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 const std::string& str() const;
66 TVM_TIRX_DLL ffi::Map<ffi::String, ffi::Any> ToConfig() const;
68 TVM_TIRX_DLL ffi::Optional<Target> GetHost() const;
71
80 TVM_TIRX_DLL bool HasKey(const std::string& query_key) const;
81
82 static void RegisterReflection() {
83 namespace refl = tvm::ffi::reflection;
84 refl::ObjectDef<TargetObj>()
85 .def_ro("kind", &TargetObj::kind)
86 .def_ro("tag", &TargetObj::tag)
87 .def_ro("keys", &TargetObj::keys)
88 .def_ro("attrs", &TargetObj::attrs)
89 .def_ro("host", &TargetObj::host);
90 }
91
99 template <typename TObjectRef>
100 ffi::Optional<TObjectRef> GetAttr(
101 const std::string& attr_key,
102 ffi::Optional<TObjectRef> default_value = ffi::Optional<TObjectRef>(std::nullopt)) const {
103 auto it = attrs.find(attr_key);
104 if (it != attrs.end()) {
105 return (*it).second.as_or_throw<ffi::Optional<TObjectRef>>();
106 } else {
107 return default_value;
108 }
109 }
117 template <typename TObjectRef>
118 ffi::Optional<TObjectRef> GetAttr(const std::string& attr_key, TObjectRef default_value) const {
119 return GetAttr<TObjectRef>(attr_key, ffi::Optional<TObjectRef>(default_value));
120 }
121
123 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("target.Target", TargetObj, ffi::Object);
124
125 private:
127 mutable std::string str_repr_;
128
129 friend class TargetInternal;
130};
131
136class Target : public ffi::ObjectRef {
137 public:
142 TVM_TIRX_DLL explicit Target(const ffi::String& tag_or_config_or_target_str);
147 TVM_TIRX_DLL explicit Target(const ffi::Map<ffi::String, ffi::Any>& config);
156 TVM_TIRX_DLL static ffi::Optional<Target> Current(bool allow_not_defined = true);
162 TVM_TIRX_DLL explicit Target(Target target, ffi::Optional<Target> host);
163 explicit Target(ffi::UnsafeInit tag, ffi::ObjectPtr<TargetObj> n) : ffi::ObjectRef(tag) {
164 data_ = std::move(n);
165 }
166
168
169 static Target WithHost(const Target& target, const ffi::Optional<Target>& host);
170
173
174 private:
175 Target(TargetKind kind, ffi::Optional<ffi::ObjectRef> host, ffi::String tag,
176 ffi::Array<ffi::String> keys, ffi::Map<ffi::String, ffi::Any> attrs);
177
178 // enable with syntax.
179 friend class TargetInternal;
180 friend class With<Target>;
186 TVM_TIRX_DLL void EnterWithScope();
191 TVM_TIRX_DLL void ExitWithScope();
192};
193
194} // namespace tvm
195#endif // TVM_TARGET_TARGET_H_
Managed reference class to TargetKindObj.
Definition target_kind.h:88
Compilation target.
Definition target.h:45
ffi::Optional< ffi::ObjectRef > host
Target host information, must be Target type.
Definition target.h:52
static void RegisterReflection()
Definition target.h:82
TargetObj(TargetKind kind)
Definition target.h:47
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:118
TargetObj(ffi::UnsafeInit tag)
Definition target.h:48
friend class TargetInternal
Definition target.h:129
TVM_TIRX_DLL int GetTargetDeviceType() const
TVM_TIRX_DLL const std::string & str() const
The JSON string representation of the target.
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:122
TVM_TIRX_DLL ffi::Optional< Target > GetHost() const
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:100
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
TVM_TIRX_DLL bool HasKey(const std::string &query_key) const
Check if the target contains a key.
Managed reference class to TargetObj.
Definition target.h:136
TVM_TIRX_DLL Target(const ffi::Map< ffi::String, ffi::Any > &config)
Construct a Target using a JSON-like configuration.
static TVM_TIRX_DLL ffi::Optional< Target > Current(bool allow_not_defined=true)
Get the current target context from thread local storage.
static Target WithHost(const Target &target, const ffi::Optional< Target > &host)
Target(ffi::UnsafeInit tag, ffi::ObjectPtr< TargetObj > n)
Definition target.h:163
Target WithoutHost() const
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:179
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Target, ffi::ObjectRef, TargetObj)
TVM_TIRX_DLL Target(Target target, ffi::Optional< Target > host)
Construct a Target given target and host.
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 attr.h:23
#define TVM_TIRX_DLL
Definition base.h:83
Target kind registry.
RAII wrapper function to enter and exit a context object similar to python's with syntax.