tvm
name_supply.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_IR_NAME_SUPPLY_H_
25 #define TVM_IR_NAME_SUPPLY_H_
26 
27 #include <algorithm>
28 #include <string>
29 #include <unordered_map>
30 #include <utility>
31 
32 #include "tvm/ir/expr.h"
33 
34 namespace tvm {
35 
39 class NameSupplyNode : public Object {
40  public:
44  NameSupplyNode() = default;
45 
51  NameSupplyNode(const String& prefix, std::unordered_map<std::string, int> name_map)
52  : prefix_(prefix), name_map(std::move(name_map)) {}
53 
62  String FreshName(const String& name, bool add_prefix = true, bool add_underscore = true);
63 
71  String ReserveName(const String& name, bool add_prefix = true);
72 
80  bool ContainsName(const String& name, bool add_prefix = true);
81 
82  void VisitAttrs(AttrVisitor* v) {}
83 
84  // Prefix for all GlobalVar names. It can be empty.
85  std::string prefix_;
86 
87  static constexpr const char* _type_key = "NameSupply";
88  static constexpr const bool _type_has_method_sequal_reduce = false;
89  static constexpr const bool _type_has_method_shash_reduce = false;
91 
92  private:
94  String add_prefix_to_name(const String& name);
95 
102  std::string GetUniqueName(std::string name, bool add_underscore = true);
103 
105  std::unordered_map<std::string, int> name_map;
106 };
107 
112 class NameSupply : public ObjectRef {
113  public:
119  TVM_DLL explicit NameSupply(const String& prefix = "",
120  std::unordered_map<std::string, int> name_map = {});
121 
128  template <typename Iter, typename Lambda>
129  TVM_DLL explicit NameSupply(Iter begin, Iter end, Lambda f)
130  : NameSupply("", GetNameMap(begin, end, f)) {}
131 
133 
134  private:
135  template <typename Iter, typename Lambda>
136  static std::unordered_map<std::string, int> GetNameMap(Iter begin, Iter end, Lambda f) {
137  // static_assert is more reader-friendly than SFINAE when template specialization is not needed.
138  static_assert(std::is_convertible<decltype(f(*begin)), std::string>::value,
139  "Lambda f must has a signature of [?](*it) -> string {}");
140  std::unordered_map<std::string, int> name_map;
141  for (auto it = begin; it != end; ++it) {
142  const std::string& name = f(*it);
143  const size_t idx_last_first_num = std::distance(
144  std::find_if(name.rbegin(), name.rend(), [](char c) { return !std::isdigit(c); }),
145  name.rend());
146  // name = {O = others}{D = consecutive digits}
147  // let O -> prefix;
148  std::string prefix = name.substr(0, idx_last_first_num);
149  ICHECK(prefix.size() > 0 && std::isalpha(prefix[0])) << "Invalid variable name: " << name;
150  if (0 == name_map.count(prefix)) name_map[prefix] = 0;
151  if (idx_last_first_num < name.size()) { // has some digits.
152  // let D's nearest natural number -> idx;
153  // note: stoul("000123") = 123;
154  name_map[prefix] = std::max(name_map[prefix], std::stoi(name.substr(idx_last_first_num)));
155  }
156  }
157  return name_map;
158  }
159 };
160 
161 } // namespace tvm
162 
163 #endif // TVM_IR_NAME_SUPPLY_H_
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
NameSupply can be used to generate unique names.
Definition: name_supply.h:39
NameSupplyNode(const String &prefix, std::unordered_map< std::string, int > name_map)
Constructor.
Definition: name_supply.h:51
String FreshName(const String &name, bool add_prefix=true, bool add_underscore=true)
Generates a unique name from this NameSupply.
void VisitAttrs(AttrVisitor *v)
Definition: name_supply.h:82
TVM_DECLARE_FINAL_OBJECT_INFO(NameSupplyNode, Object)
static constexpr const char * _type_key
Definition: name_supply.h:87
String ReserveName(const String &name, bool add_prefix=true)
Reserves an existing name with this NameSupply.
std::string prefix_
Definition: name_supply.h:85
NameSupplyNode()=default
Empty constructor. Needed by the TVM_REGISTER_NODE_TYPE macro.
static constexpr const bool _type_has_method_shash_reduce
Definition: name_supply.h:89
bool ContainsName(const String &name, bool add_prefix=true)
Checks if this NameSupply already generated a name.
static constexpr const bool _type_has_method_sequal_reduce
Definition: name_supply.h:88
Managed reference class to NameSupplyNode.
Definition: name_supply.h:112
NameSupply(Iter begin, Iter end, Lambda f)
Construct NameSupply with a name map created from the given iterator range and the functor.
Definition: name_supply.h:129
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(NameSupply, ObjectRef, NameSupplyNode)
NameSupply(const String &prefix="", std::unordered_map< std::string, int > name_map={})
Constructor.
Base class of all object reference.
Definition: object.h:519
base class of all object containers.
Definition: object.h:171
Reference to string objects.
Definition: string.h:98
Base expr nodes in TVM.
runtime implementation for LibTorch/TorchScript.
Definition: analyzer.h:36
PrimExpr max(PrimExpr a, PrimExpr b, Span span=Span())
take maximum of two values