Skip to content

Commit 25ca250

Browse files
author
Florin-Gabriel Blanaru
committed
Add documentation for NameSupply and GlobalVarSupply
1 parent 6962c2f commit 25ca250

24 files changed

+285
-126
lines changed

include/tvm/ir/global_var_supply.h

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@
1717
* under the License.
1818
*/
1919

20+
/*!
21+
* \file tvm/ir/global_var_supply.h
22+
* \brief GlobalVarSupply that can be used to generate unique \class GlobalVar.
23+
*/
2024
#ifndef TVM_IR_GLOBAL_VAR_SUPPLY_H_
2125
#define TVM_IR_GLOBAL_VAR_SUPPLY_H_
2226

@@ -29,20 +33,51 @@
2933

3034
namespace tvm {
3135

36+
/*!
37+
* \brief GlobalVarSupply can be used to generate unique GlobalVars.
38+
*/
3239
class GlobalVarSupplyNode : public Object {
3340
public:
41+
/*!
42+
* \brief Empty constructor. Will use an empty NameSupply.
43+
*/
3444
GlobalVarSupplyNode() : GlobalVarSupplyNode(NameSupply("")) {}
3545

36-
explicit GlobalVarSupplyNode(NameSupply name_supply);
37-
46+
/*!
47+
* \brief Constructor.
48+
* \param name_supply The NameSupply to use for generating the names of fresh GlobalVars.
49+
* \param name_to_var_map An optional map.
50+
*/
51+
explicit GlobalVarSupplyNode(NameSupply name_supply,
52+
std::unordered_map<std::string, GlobalVar> name_to_var_map = {});
53+
54+
/*!
55+
* \brief Generates a unique GlobalVar from this supply.
56+
* \param name The name from which the name of the GlobalVar is derived.
57+
* \param add_prefix If set to true, then the prefix of the contained NameSupply will be prepended
58+
* to the name. \return A unique GlobalVar.
59+
*/
3860
GlobalVar FreshGlobal(String name, bool add_prefix = true);
3961

62+
/*!
63+
* \brief Looks up for a GlobalVar with the given name in this supply.
64+
* If no entry is found, creates one, places it in the cache and returns it.
65+
* \param name The name of the GlobalVar to search for.
66+
* \param add_prefix If set to true, the prefix of the contained NameSupply will be prepended to
67+
* the name before performing the search. \return A cached GlobalVar.
68+
*/
4069
GlobalVar UniqueGlobalFor(const String& name, bool add_prefix = true);
4170

71+
/*!
72+
* \brief Reserves an existing GlobalVar with this supply.
73+
* \param var The GlobalVar to be registered.
74+
* \param allow_conflict Allow conflict with other GlobalVars that have the same name.
75+
*/
4276
void ReserveGlobalVar(const GlobalVar& var, bool allow_conflict = false);
4377

44-
void VisitAttrs(AttrVisitor* v) { v->Visit("name_supply", &name_supply_); }
78+
void VisitAttrs(AttrVisitor* v) {}
4579

80+
/*! \brief The NameSupply used to generate unique name hints to GlobalVars. */
4681
NameSupply name_supply_;
4782

4883
static constexpr const char* _type_key = "GlobalVarSupply";
@@ -52,28 +87,37 @@ class GlobalVarSupplyNode : public Object {
5287

5388
private:
5489
std::unordered_map<std::string, GlobalVar> name_to_var_map_;
55-
56-
friend class GlobalVarSupply;
5790
};
5891

92+
/*!
93+
* \brief Managed reference class to GlobalVarSupplyNode.
94+
* \sa GlobalVarSupplyNode
95+
*/
5996
class GlobalVarSupply : public ObjectRef {
6097
public:
61-
TVM_DLL explicit GlobalVarSupply(const NameSupply& name_supply = NameSupply(),
98+
/*!
99+
* \brief Constructor.
100+
* \param name_supply The NameSupply to be used when generating new GlobalVars.
101+
* \param name_to_var_map An optional map.
102+
*/
103+
TVM_DLL explicit GlobalVarSupply(const NameSupply& name_supply,
62104
std::unordered_map<std::string, GlobalVar> name_to_var_map = {});
63105

106+
/*!
107+
* \brief Constructs a supply from an array of IRModules. GlobalVars generated by this supply are
108+
* guaranteed not to conflict with any GlobalVars that belong to the modules. \param modules Array
109+
* of IRModules.
110+
*/
64111
TVM_DLL explicit GlobalVarSupply(const Array<IRModule>& modules);
65112

113+
/*!
114+
* \brief Constructs a GlobalVarSupply from an IRModule. GlobalVars generated by this supply are
115+
* guaranteed not to conflict with GlobalVars that belong to the modules. \param module The
116+
* IRModule.
117+
*/
66118
TVM_DLL explicit GlobalVarSupply(const IRModule module);
67119

68-
explicit GlobalVarSupply(ObjectPtr<Object> n) : ObjectRef(n) {}
69-
/*! \return mutable pointers to the node. */
70-
GlobalVarSupplyNode* operator->() const {
71-
auto* ptr = get_mutable();
72-
ICHECK(ptr != nullptr);
73-
return static_cast<GlobalVarSupplyNode*>(ptr);
74-
}
75-
76-
TVM_DEFINE_OBJECT_REF_COW_METHOD(GlobalVarSupplyNode);
120+
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(GlobalVarSupply, ObjectRef, GlobalVarSupplyNode);
77121
};
78122

79123
} // namespace tvm

include/tvm/ir/module.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,7 @@ namespace attr {
480480
*
481481
* \sa tvm::runtime::String
482482
*/
483-
constexpr const char* kModuleName = "name";
483+
constexpr const char* kModuleName = "mod_name";
484484

485485
/*!
486486
* \brief Executor targeted by the module

include/tvm/ir/name_supply.h

Lines changed: 61 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -17,31 +17,66 @@
1717
* under the License.
1818
*/
1919

20+
/*!
21+
* \file tvm/ir/name_supply.h
22+
* \brief NameSupply that can be used to generate unique variable names.
23+
*/
2024
#ifndef TVM_IR_NAME_SUPPLY_H_
2125
#define TVM_IR_NAME_SUPPLY_H_
2226

2327
#include <string>
2428
#include <unordered_map>
29+
#include <utility>
2530

2631
#include "tvm/ir/expr.h"
2732

2833
namespace tvm {
2934

35+
/*!
36+
* \brief NameSupply can be used to generate unique names.
37+
*/
3038
class NameSupplyNode : public Object {
3139
public:
32-
NameSupplyNode() : NameSupplyNode("") {}
33-
34-
explicit NameSupplyNode(const String& prefix);
35-
40+
/*!
41+
* \brief Empty constructor. Needed by the TVM_REGISTER_NODE_TYPE macro.
42+
*/
43+
NameSupplyNode() = default;
44+
45+
/*!
46+
* \brief Constructor.
47+
* \param prefix The prefix to be used with this NameSupply.
48+
* \param name_map The map used to guarantee uniqueness.
49+
*/
50+
NameSupplyNode(const String& prefix, std::unordered_map<std::string, int> name_map)
51+
: prefix_(prefix), name_map(std::move(name_map)) {}
52+
53+
/*!
54+
* \brief Generates a unique name from this NameSupply.
55+
* \param name The name from which the generated name is derived.
56+
* \param add_prefix If set to true, then the prefix of this NameSupply will be prepended to the
57+
* name. \return A unique name.
58+
*/
3659
String FreshName(const String& name, bool add_prefix = true);
3760

61+
/*!
62+
* \brief Reserves an existing name with this NameSupply.
63+
* \param name The name to be reserved.
64+
* \param add_prefix If set to true, then the prefix of this NameSupply will be prepended to the
65+
* name before reserving it. \return The name that was reserved with the NameSupply. It can be
66+
* different if a prefix is added.
67+
*/
3868
String ReserveName(const String& name, bool add_prefix = true);
3969

70+
/*!
71+
* \brief Checks if this NameSupply already generated a name.
72+
* \param name The name to check.
73+
* \param add_prefix If set to true, then the prefix of this NameSupply will be prepended to the
74+
* name before checking for it. \return True if the name has already been generated. False
75+
* otherwise.
76+
*/
4077
bool ContainsName(const String& name, bool add_prefix = true);
4178

42-
void Clear();
43-
44-
void VisitAttrs(AttrVisitor* v) { v->Visit("prefix", &prefix_); }
79+
void VisitAttrs(AttrVisitor* v) {}
4580

4681
// Prefix for all GlobalVar names. It can be empty.
4782
std::string prefix_;
@@ -52,32 +87,35 @@ class NameSupplyNode : public Object {
5287
TVM_DECLARE_FINAL_OBJECT_INFO(NameSupplyNode, Object);
5388

5489
private:
55-
String prefix_module_name(const String& name);
56-
90+
/*! \brief Helper function to add the NameSupply prefix to the name. */
91+
String add_prefix_to_name(const String& name);
92+
93+
/*!
94+
* \brief Function that will generate a unique name.
95+
* \param name The name to be used as a base.
96+
* \return A unique name.
97+
*/
5798
std::string GetUniqueName(std::string name);
5899

59-
// Key is function_name. Value is a counter.
100+
/*! \brief A map that is used to generate unique names. */
60101
std::unordered_map<std::string, int> name_map;
61-
62-
friend class NameSupply;
63102
};
64103

104+
/*!
105+
* \brief Managed reference class to NameSupplyNode.
106+
* \sa NameSupplyNode
107+
*/
65108
class NameSupply : public ObjectRef {
66109
public:
67-
TVM_DLL explicit NameSupply();
68-
110+
/*!
111+
* \brief Constructor.
112+
* \param prefix The prefix to be used with this NameSupply.
113+
* \param name_map An optional map.
114+
*/
69115
TVM_DLL explicit NameSupply(const String& prefix,
70116
std::unordered_map<std::string, int> name_map = {});
71117

72-
explicit NameSupply(ObjectPtr<Object> n) : ObjectRef(n) {}
73-
/*! \return mutable pointers to the node. */
74-
NameSupplyNode* operator->() const {
75-
auto* ptr = get_mutable();
76-
ICHECK(ptr != nullptr);
77-
return static_cast<NameSupplyNode*>(ptr);
78-
}
79-
80-
TVM_DEFINE_OBJECT_REF_COW_METHOD(NameSupplyNode);
118+
TVM_DEFINE_MUTABLE_OBJECT_REF_METHODS(NameSupply, ObjectRef, NameSupplyNode);
81119
};
82120

83121
} // namespace tvm

python/tvm/ir/supply.py

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,23 +33,52 @@ def __init__(self, prefix=""):
3333
self.__init_handle_by_constructor__(_ffi_api.NameSupply, prefix)
3434

3535
def fresh_name(self, name, add_prefix=True):
36+
"""Generates a unique name from this NameSupply.
37+
38+
Parameters
39+
----------
40+
name: String
41+
The name from which the generated name is derived.
42+
43+
add_prefix: bool
44+
If set to true, then the prefix of this NameSupply will be prepended to the name.
45+
"""
3646
return _ffi_api.NameSupply_FreshName(self, name, add_prefix)
3747

3848
def reserve_name(self, name, add_prefix=True):
49+
"""Reserves an existing name with this NameSupply.
50+
51+
Parameters
52+
----------
53+
name: String
54+
The name to be reserved.
55+
56+
add_prefix: bool
57+
If set to true, then the prefix of this NameSupply will be prepended to the name
58+
before reserving it.
59+
"""
3960
return _ffi_api.NameSupply_ReserveName(self, name, add_prefix)
4061

4162
def contains_name(self, name, add_prefix=True):
42-
return _ffi_api.NameSupply_ContainsName(self, name, add_prefix)
63+
"""Checks if this NameSupply already generated a name.
4364
44-
def clear(self):
45-
return _ffi_api.NameSupply_Clear(self)
65+
Parameters
66+
----------
67+
name: String
68+
The name to check.
69+
70+
add_prefix: bool
71+
If set to true, then the prefix of this NameSupply will be prepended to the name
72+
before checking for it.
73+
"""
74+
return _ffi_api.NameSupply_ContainsName(self, name, add_prefix)
4675

4776

4877
@tvm._ffi.register_object("GlobalVarSupply")
4978
class GlobalVarSupply(Object):
5079
"""GlobalVarSupply that holds a mapping between names and GlobalVars.
5180
52-
GlobalVarSupply can be used to generate new GlobalVars with an unique name.
81+
GlobalVarSupply can be used to generate new GlobalVars with a unique name.
5382
It also can be used to retrieve previously generated GlobalVars based on a name.
5483
5584
Parameters
@@ -70,10 +99,43 @@ def __init__(self, value=None):
7099
self.__init_handle_by_constructor__(_ffi_api.GlobalVarSupply_IRModule, value)
71100

72101
def fresh_global(self, name, add_prefix=True):
102+
"""Generates a unique GlobalVar from this supply.
103+
104+
Parameters
105+
----------
106+
name: String
107+
The name from which the name of the GlobalVar is derived.
108+
109+
add_prefix: bool
110+
If set to true, then the prefix of the contained NameSupply will be prepended
111+
to the name.
112+
"""
73113
return _ffi_api.GlobalVarSupply_FreshGlobal(self, name, add_prefix)
74114

75115
def unique_global_for(self, name, add_prefix=True):
116+
"""Looks up for a GlobalVar with the given name in this supply. If no entry is found
117+
, creates one, places it in the cache and returns it.
118+
119+
Parameters
120+
----------
121+
name: String
122+
The name of the GlobalVar to search for.
123+
124+
add_prefix: bool
125+
If set to true, the prefix of the contained NameSupply will be prepended to the
126+
name before performing the search.
127+
"""
76128
return _ffi_api.GlobalVarSupply_UniqueGlobalFor(self, name, add_prefix)
77129

78130
def reserve_global(self, global_var, allow_conflict=False):
131+
"""Reserves an existing GlobalVar with this supply.
132+
133+
Parameters
134+
----------
135+
global_var: GlobalVar
136+
The GlobalVar to be registered.
137+
138+
allow_conflict: bool
139+
Allow conflict with other GlobalVars that have the same name
140+
"""
79141
return _ffi_api.GlobalVarSupply_ReserveGlobalVar(self, global_var, allow_conflict)

src/auto_scheduler/feature.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1372,7 +1372,8 @@ void GetPerStoreFeaturesWorkerFunc(const SearchTask& task, const State& state, i
13721372
auto pass_ctx = tvm::transform::PassContext::Current();
13731373

13741374
auto mod = ScheduleToModule(sch, Array<ObjectRef>{tensors.begin(), tensors.end()}, name,
1375-
std::unordered_map<te::Tensor, te::Buffer>(), GlobalVarSupply());
1375+
std::unordered_map<te::Tensor, te::Buffer>(),
1376+
GlobalVarSupply(NameSupply("")));
13761377

13771378
bool disable_vectorize =
13781379
pass_ctx->GetConfig<Bool>("tir.disable_vectorize", Bool(false)).value();

src/contrib/hybrid/codegen_hybrid.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ class CodeGenHybrid : public ExprFunctor<void(const PrimExpr&, std::ostream&)>,
147147
/*! \brief Print the current indent spaces. */
148148
inline void PrintIndent();
149149
/*! \brief NameSupply for allocated ids. */
150-
NameSupply ids_allocated = NameSupply();
150+
NameSupply ids_allocated = NameSupply("");
151151
/*!
152152
* \brief Keys are either (tensors, value_index) or (variables, 0).
153153
* Values are the corresponding IDs.*/

src/driver/driver_api.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,8 @@ TVM_REGISTER_GLOBAL("driver.schedule_to_module")
303303
c_binds.insert({kv.first, kv.second});
304304
}
305305
}
306-
IRModule mod = ScheduleToModule(std::move(sch), args, name, c_binds, GlobalVarSupply());
306+
IRModule mod =
307+
ScheduleToModule(std::move(sch), args, name, c_binds, GlobalVarSupply(NameSupply("")));
307308
return mod;
308309
});
309310

@@ -366,7 +367,8 @@ TVM_REGISTER_GLOBAL("driver.lower_schedule")
366367
c_binds.insert({kv.first, kv.second});
367368
}
368369
}
369-
return LowerSchedule(std::move(sch), args, name, c_binds, GlobalVarSupply(), simple_mode);
370+
return LowerSchedule(std::move(sch), args, name, c_binds, GlobalVarSupply(NameSupply("")),
371+
simple_mode);
370372
});
371373

372374
/**

0 commit comments

Comments
 (0)