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;
62
63 static void RegisterReflection() {
64 namespace refl = tvm::ffi::reflection;
65 refl::ObjectDef<TargetKindObj>()
66 .def_ro("name", &TargetKindObj::name)
67 .def_ro("default_device_type", &TargetKindObj::default_device_type,
68 refl::AttachFieldFlag::SEqHashIgnore())
69 .def_ro("default_keys", &TargetKindObj::default_keys,
70 refl::AttachFieldFlag::SEqHashIgnore());
71 }
72
73 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindUniqueInstance;
74 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("target.TargetKind", TargetKindObj, ffi::Object);
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
109static constexpr const char* kTvmRuntimeCpp = "c++";
110
112static constexpr const char* kTvmRuntimeCrt = "c";
113
118 public:
130 TVM_TIRX_DLL TargetKind RegisterOrGet(const ffi::String& name);
136 TVM_TIRX_DLL ffi::Optional<TargetKind> Get(const ffi::String& name);
141 TVM_TIRX_DLL ffi::Array<ffi::String> ListTargetKinds();
147 TVM_TIRX_DLL ffi::Map<ffi::String, ffi::String> ListTargetKindOptions(const TargetKind& kind);
148
149 private:
150 ffi::Map<ffi::String, TargetKind> kinds_;
151};
152
168 public:
173 explicit TargetKindDef(const ffi::String& name)
174 : kind_(TargetKindRegistry::Global()->RegisterOrGet(name)) {}
181 kind_->default_device_type = device_type;
182 return *this;
183 }
189 TargetKindDef& set_default_keys(std::vector<ffi::String> keys) {
190 kind_->default_keys = keys;
191 return *this;
192 }
199 kind_->target_canonicalizer = canonicalizer;
200 kind_->schema_.set_canonicalizer(canonicalizer);
201 return *this;
202 }
211 template <typename ValueType, typename... Traits>
212 TargetKindDef& def_option(const ffi::String& key, Traits&&... traits) {
213 kind_->schema_.def_option<ValueType>(key, std::forward<Traits>(traits)...);
214 return *this;
215 }
216
217 private:
218 TargetKind kind_;
219};
220
221} // namespace tvm
222
223#endif // TVM_TARGET_TARGET_KIND_H_
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:167
TargetKindDef & set_default_keys(std::vector< ffi::String > keys)
Set default target keys.
Definition target_kind.h:189
TargetKindDef & def_option(const ffi::String &key, Traits &&... traits)
Declare a typed target option in the kind's ConfigSchema.
Definition target_kind.h:212
TargetKindDef & set_target_canonicalizer(FTargetCanonicalizer canonicalizer)
Set the canonicalizer used when constructing targets of this kind.
Definition target_kind.h:198
TargetKindDef & set_default_device_type(int device_type)
Set the default DLPack device type.
Definition target_kind.h:180
TargetKindDef(const ffi::String &name)
Register or retrieve the canonical kind with this name.
Definition target_kind.h:173
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:73
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:63
ffi::Array< ffi::String > default_keys
Default keys of the target.
Definition target_kind.h:59
FTargetCanonicalizer target_canonicalizer
Function used to canonicalize a JSON target during creation.
Definition target_kind.h:61
Process-wide registry of canonical target kinds, keyed by name.
Definition target_kind.h:117
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 attr.h:23
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
#define TVM_TIRX_DLL
Definition base.h:83