tvm
Loading...
Searching...
No Matches
target_kind.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_KIND_H_
25#define TVM_TARGET_TARGET_KIND_H_
26
27#include <tvm/ffi/container/map.h>
28#include <tvm/ffi/function.h>
29#include <tvm/ffi/reflection/registry.h>
30#include <tvm/runtime/base.h>
32
33#include <utility>
34#include <vector>
35
36namespace tvm {
37
38class Target;
39
47 ffi::TypedFunction<ffi::Map<ffi::String, ffi::Any>(ffi::Map<ffi::String, ffi::Any>)>;
48
49class TargetInternal;
50
52class TargetKindObj : public ffi::Object {
53 public:
55 ffi::String name;
59 ffi::Array<ffi::String> default_keys;
60
61 static void RegisterReflection() {
62 namespace refl = tvm::ffi::reflection;
63 refl::ObjectDef<TargetKindObj>()
64 .def_ro("name", &TargetKindObj::name)
65 .def_ro("default_device_type", &TargetKindObj::default_device_type,
66 refl::AttachFieldFlag::SEqHashIgnore())
67 .def_ro("default_keys", &TargetKindObj::default_keys,
68 refl::AttachFieldFlag::SEqHashIgnore());
69 }
70
71 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindUniqueInstance;
72 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("target.TargetKind", TargetKindObj, ffi::Object);
73 // Shared instances are mutated in place, so the class is mutable.
74 static constexpr bool _type_mutable = true;
75
76 private:
78 ConfigSchema schema_;
79 friend class TargetKindRegistry;
80 friend class TargetKindDef;
81 friend class TargetInternal;
82};
83
88class TargetKind : public ffi::ObjectRef {
89 public:
90 explicit TargetKind(ffi::ObjectPtr<TargetKindObj> data) : ffi::ObjectRef(data) {
91 TVM_FFI_ICHECK(data != nullptr);
92 }
98 TVM_TIRX_DLL static ffi::Optional<TargetKind> Get(const ffi::String& target_kind_name);
100 TargetKindObj* operator->() { return static_cast<TargetKindObj*>(data_.get()); }
101
103
104 private:
105 friend class TargetInternal;
106};
107
112 public:
124 TVM_TIRX_DLL TargetKind RegisterOrGet(const ffi::String& name);
130 TVM_TIRX_DLL ffi::Optional<TargetKind> Get(const ffi::String& name);
135 TVM_TIRX_DLL ffi::Array<ffi::String> ListTargetKinds();
141 TVM_TIRX_DLL ffi::Map<ffi::String, ffi::String> ListTargetKindOptions(const TargetKind& kind);
142
143 private:
144 ffi::Map<ffi::String, TargetKind> kinds_;
145};
146
162 public:
167 explicit TargetKindDef(const ffi::String& name)
168 : kind_(TargetKindRegistry::Global()->RegisterOrGet(name)) {}
175 kind_->default_device_type = device_type;
176 return *this;
177 }
183 TargetKindDef& set_default_keys(std::vector<ffi::String> keys) {
184 kind_->default_keys = keys;
185 return *this;
186 }
193 kind_->schema_.set_canonicalizer(std::move(canonicalizer));
194 return *this;
195 }
204 template <typename ValueType, typename... Traits>
205 TargetKindDef& def_option(const ffi::String& key, Traits&&... traits) {
206 kind_->schema_.def_option<ValueType>(key, std::forward<Traits>(traits)...);
207 return *this;
208 }
209
210 private:
211 TargetKind kind_;
212};
213
214} // namespace tvm
215
216#endif // TVM_TARGET_TARGET_KIND_H_
#define TVM_TIRX_DLL
Definition base.h:55
Dynamic config schema for map-like options.
Definition config_schema.h:52
void set_canonicalizer(Canonicalizer f)
Set whole-object canonicalizer.
Definition config_schema.h:97
ConfigSchema & def_option(const ffi::String &key, Traits &&... traits)
Declare a typed option.
Definition config_schema.h:86
Temporary fluent builder for registering a target kind.
Definition target_kind.h:161
TargetKindDef & set_default_keys(std::vector< ffi::String > keys)
Set default target keys.
Definition target_kind.h:183
TargetKindDef & def_option(const ffi::String &key, Traits &&... traits)
Declare a typed target option in the kind's ConfigSchema.
Definition target_kind.h:205
TargetKindDef & set_target_canonicalizer(FTargetCanonicalizer canonicalizer)
Set the canonicalizer used when constructing targets of this kind.
Definition target_kind.h:192
TargetKindDef & set_default_device_type(int device_type)
Set the default DLPack device type.
Definition target_kind.h:174
TargetKindDef(const ffi::String &name)
Register or retrieve the canonical kind with this name.
Definition target_kind.h:167
Target kind, specifies the kind of the target.
Definition target_kind.h:52
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition target_kind.h:71
int default_device_type
Device type of target kind.
Definition target_kind.h:57
ffi::String name
Name of the target kind.
Definition target_kind.h:55
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("target.TargetKind", TargetKindObj, ffi::Object)
friend class TargetInternal
Definition target_kind.h:81
static void RegisterReflection()
Definition target_kind.h:61
ffi::Array< ffi::String > default_keys
Default keys of the target.
Definition target_kind.h:59
static constexpr bool _type_mutable
Definition target_kind.h:74
Process-wide registry of canonical target kinds, keyed by name.
Definition target_kind.h:111
TVM_TIRX_DLL ffi::Array< ffi::String > ListTargetKinds()
List registered kind names.
TVM_TIRX_DLL ffi::Map< ffi::String, ffi::String > ListTargetKindOptions(const TargetKind &kind)
List declared option names and types for a kind.
TVM_TIRX_DLL ffi::Optional< TargetKind > Get(const ffi::String &name)
Look up a registered kind.
static TVM_TIRX_DLL TargetKindRegistry * Global()
Access the process-wide registry.
TVM_TIRX_DLL TargetKind RegisterOrGet(const ffi::String &name)
Register a name or return its existing canonical kind.
Managed reference class to TargetKindObj.
Definition target_kind.h:88
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(TargetKind, ffi::ObjectRef, TargetKindObj)
static TVM_TIRX_DLL ffi::Optional< TargetKind > Get(const ffi::String &target_kind_name)
Retrieve the TargetKind given its name.
TargetKind(ffi::ObjectPtr< TargetKindObj > data)
Definition target_kind.h:90
friend class TargetInternal
Definition target_kind.h:105
TargetKindObj * operator->()
Mutable access to the container class
Definition target_kind.h:100
Minimal schema for dynamic config canonicalization and validation.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41
ffi::TypedFunction< ffi::Map< ffi::String, ffi::Any >(ffi::Map< ffi::String, ffi::Any >)> FTargetCanonicalizer
Target canonicalizer applied on instantiation of a given TargetKind.
Definition target_kind.h:47