tvm
tag.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_TAG_H_
25 #define TVM_TARGET_TAG_H_
26 
28 #include <tvm/node/node.h>
29 #include <tvm/target/target.h>
30 
31 #include <utility>
32 
33 namespace tvm {
34 
36 class TargetTagNode : public Object {
37  public:
42 
44  v->Visit("name", &name);
45  v->Visit("config", &config);
46  }
47 
48  static constexpr const char* _type_key = "TargetTag";
50 
51  private:
53  uint32_t AttrRegistryIndex() const { return index_; }
55  String AttrRegistryName() const { return name; }
57  uint32_t index_;
58 
59  template <typename, typename>
60  friend class AttrRegistry;
61  template <typename>
63  friend class TargetTagRegEntry;
64 };
65 
70 class TargetTag : public ObjectRef {
71  public:
77  TVM_DLL static Optional<Target> Get(const String& target_tag_name);
82  TVM_DLL static Map<String, Target> ListTags();
90  TVM_DLL static Target AddTag(String name, Map<String, ObjectRef> config, bool override);
91 
93 
94  private:
96  TargetTagNode* operator->() { return static_cast<TargetTagNode*>(data_.get()); }
97  friend class TargetTagRegEntry;
98 };
99 
101  public:
112  inline TargetTagRegEntry& with_config(String key, ObjectRef value);
114  inline TargetTagRegEntry& set_name();
120  TVM_DLL static TargetTagRegEntry& RegisterOrGet(const String& target_tag_name);
121 
122  private:
123  TargetTag tag_;
124  String name;
125 
127  explicit TargetTagRegEntry(uint32_t reg_index) : tag_(make_object<TargetTagNode>()) {
128  tag_->index_ = reg_index;
129  }
130  template <typename, typename>
131  friend class AttrRegistry;
132  friend class TargetTag;
133 };
134 
136  tag_->config = std::move(config);
137  return *this;
138 }
139 
141  tag_->config.Set(key, value);
142  return *this;
143 }
144 
146  if (tag_->name.empty()) {
147  tag_->name = name;
148  }
149  return *this;
150 }
151 
152 #define TVM_TARGET_TAG_REGISTER_VAR_DEF \
153  static DMLC_ATTRIBUTE_UNUSED ::tvm::TargetTagRegEntry& __make_##TargetTag
154 
160 #define TVM_REGISTER_TARGET_TAG(TargetTagName) \
161  TVM_STR_CONCAT(TVM_TARGET_TAG_REGISTER_VAR_DEF, __COUNTER__) = \
162  ::tvm::TargetTagRegEntry::RegisterOrGet(TargetTagName).set_name()
163 
164 } // namespace tvm
165 
166 #endif // TVM_TARGET_TAG_H_
Attribute map used in registry.
Generic attribute map.
Definition: attr_registry_map.h:38
Definition: executor.h:43
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
A target tag.
Definition: tag.h:36
TVM_DECLARE_FINAL_OBJECT_INFO(TargetTagNode, Object)
static constexpr const char * _type_key
Definition: tag.h:48
Map< String, ObjectRef > config
Config map to generate the target.
Definition: tag.h:41
String name
Name of the target.
Definition: tag.h:39
void VisitAttrs(AttrVisitor *v)
Definition: tag.h:43
Definition: tag.h:100
static TargetTagRegEntry & RegisterOrGet(const String &target_tag_name)
Register or get a new entry.
TargetTagRegEntry & set_name()
Set name of the TargetTag to be the same as registry if it is empty.
Definition: tag.h:145
TargetTagRegEntry & set_config(Map< String, ObjectRef > config)
Set the config dict corresponding to the target tag.
Definition: tag.h:135
TargetTagRegEntry & with_config(String key, ObjectRef value)
Add a key-value pair to the config dict.
Definition: tag.h:140
Managed reference class to TargetTagNode.
Definition: tag.h:70
static Target AddTag(String name, Map< String, ObjectRef > config, bool override)
Add a tag into the registry.
TVM_DEFINE_OBJECT_REF_METHODS(TargetTag, ObjectRef, TargetTagNode)
static Map< String, Target > ListTags()
List all names of the existing target tags.
static Optional< Target > Get(const String &target_tag_name)
Retrieve the Target given it the name of target tag.
Managed reference class to TargetNode.
Definition: target.h:200
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics,...
Definition: map.h:1271
Base class of all object reference.
Definition: object.h:519
ObjectPtr< Object > data_
Internal pointer that backs the reference.
Definition: object.h:605
base class of all object containers.
Definition: object.h:171
Optional container that to represent to a Nullable variant of T.
Definition: optional.h:51
Reference to string objects.
Definition: string.h:98
bool empty() const
Retun if the string is empty.
Definition: string.h:208
ObjectPtr< ArrayNode > make_object()
Definition: array.h:908
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
Definitions and helper macros for IR/AST nodes.
Compilation target object.