|
19 | 19 |
|
20 | 20 | /*! |
21 | 21 | * \file tvm/base.h |
22 | | - * \brief Defines the base data structure |
| 22 | + * \brief Base utilities |
23 | 23 | */ |
24 | 24 | #ifndef TVM_BASE_H_ |
25 | 25 | #define TVM_BASE_H_ |
26 | 26 |
|
27 | 27 | #include <dmlc/logging.h> |
28 | | -#include <dmlc/registry.h> |
29 | | -#include <tvm/node/node.h> |
30 | | -#include <string> |
31 | | -#include <memory> |
32 | | -#include <functional> |
33 | 28 | #include <utility> |
34 | | -#include "runtime/registry.h" |
35 | 29 |
|
36 | 30 | namespace tvm { |
37 | 31 |
|
38 | | -using ::tvm::Node; |
39 | | -using ::tvm::NodeRef; |
40 | | -using ::tvm::AttrVisitor; |
41 | | - |
42 | | -/*! |
43 | | - * \brief Macro to define common node ref methods. |
44 | | - * \param TypeName The name of the NodeRef. |
45 | | - * \param BaseTypeName The Base type. |
46 | | - * \param NodeName The node container type. |
47 | | - */ |
48 | | -#define TVM_DEFINE_NODE_REF_METHODS(TypeName, BaseTypeName, NodeName) \ |
49 | | - TypeName() {} \ |
50 | | - explicit TypeName(::tvm::ObjectPtr<::tvm::Object> n) \ |
51 | | - : BaseTypeName(n) {} \ |
52 | | - const NodeName* operator->() const { \ |
53 | | - return static_cast<const NodeName*>(data_.get()); \ |
54 | | - } \ |
55 | | - operator bool() const { return this->defined(); } \ |
56 | | - using ContainerType = NodeName; |
57 | | - |
58 | | -/*! |
59 | | - * \brief Macro to define CopyOnWrite function in a NodeRef. |
60 | | - * \param NodeName The Type of the Node. |
61 | | - * |
62 | | - * CopyOnWrite will generate a unique copy of the internal node. |
63 | | - * The node will be copied if it is referenced by multiple places. |
64 | | - * The function returns the raw pointer to the node to allow modification |
65 | | - * of the content. |
66 | | - * |
67 | | - * \code |
68 | | - * |
69 | | - * MyCOWNodeRef ref, ref2; |
70 | | - * ref2 = ref; |
71 | | - * ref.CopyOnWrite()->value = new_value; |
72 | | - * assert(ref2->value == old_value); |
73 | | - * assert(ref->value == new_value); |
74 | | - * |
75 | | - * \endcode |
76 | | - */ |
77 | | -#define TVM_DEFINE_NODE_REF_COW(NodeName) \ |
78 | | - NodeName* CopyOnWrite() { \ |
79 | | - CHECK(data_ != nullptr); \ |
80 | | - if (!data_.unique()) { \ |
81 | | - NodePtr<NodeName> n = make_node<NodeName>(*(operator->())); \ |
82 | | - ObjectPtr<Object>(std::move(n)).swap(data_); \ |
83 | | - } \ |
84 | | - return static_cast<NodeName*>(data_.get()); \ |
85 | | - } |
86 | | - |
87 | | -/*! \brief Macro to make it easy to define node ref type given node */ |
88 | | -#define TVM_DEFINE_NODE_REF(TypeName, NodeName) \ |
89 | | - class TypeName : public ::tvm::NodeRef { \ |
90 | | - public: \ |
91 | | - TVM_DEFINE_NODE_REF_METHODS(TypeName, ::tvm::NodeRef, NodeName); \ |
92 | | - }; \ |
93 | | - |
94 | | -/*! |
95 | | - * \brief Macro to make it easy to define node ref type that |
96 | | - * has a CopyOnWrite member function. |
97 | | - */ |
98 | | -#define TVM_DEFINE_COW_NODE_REF(TypeName, BaseType, NodeName) \ |
99 | | - class TypeName : public BaseType { \ |
100 | | - public: \ |
101 | | - TVM_DEFINE_NODE_REF_METHODS(TypeName, BaseType, NodeName); \ |
102 | | - TVM_DEFINE_NODE_REF_COW(NodeName); \ |
103 | | - }; |
104 | | - |
105 | 32 | /*! |
106 | 33 | * \brief RAII wrapper function to enter and exit a context object |
107 | 34 | * similar to python's with syntax. |
@@ -146,100 +73,6 @@ class With { |
146 | 73 | ContextType ctx_; |
147 | 74 | }; |
148 | 75 |
|
149 | | -/*! |
150 | | - * \brief save the node as well as all the node it depends on as json. |
151 | | - * This can be used to serialize any TVM object |
152 | | - * |
153 | | - * \return the string representation of the node. |
154 | | - */ |
155 | | -std::string SaveJSON(const NodeRef& node); |
156 | | - |
157 | | -/*! |
158 | | - * \brief Internal implementation of LoadJSON |
159 | | - * Load tvm Node object from json and return a shared_ptr of Node. |
160 | | - * \param json_str The json string to load from. |
161 | | - * |
162 | | - * \return The shared_ptr of the Node. |
163 | | - */ |
164 | | -ObjectPtr<Object> LoadJSON_(std::string json_str); |
165 | | - |
166 | | -/*! |
167 | | - * \brief Load the node from json string. |
168 | | - * This can be used to deserialize any TVM object. |
169 | | - * |
170 | | - * \param json_str The json string to load from. |
171 | | - * |
172 | | - * \tparam NodeType the nodetype |
173 | | - * |
174 | | - * \code |
175 | | - * Expr e = LoadJSON<Expr>(json_str); |
176 | | - * \endcode |
177 | | - */ |
178 | | -template<typename NodeType, |
179 | | - typename = typename std::enable_if<std::is_base_of<NodeRef, NodeType>::value>::type > |
180 | | -inline NodeType LoadJSON(const std::string& json_str) { |
181 | | - return NodeType(LoadJSON_(json_str)); |
182 | | -} |
183 | | - |
184 | | -/*! |
185 | | - * \brief Registry entry for NodeFactory. |
186 | | - * |
187 | | - * There are two types of Nodes that can be serialized. |
188 | | - * The normal node requires a registration a creator function that |
189 | | - * constructs an empty Node of the corresponding type. |
190 | | - * |
191 | | - * The global singleton(e.g. global operator) where only global_key need to be serialized, |
192 | | - * in this case, FGlobalKey need to be defined. |
193 | | - */ |
194 | | -struct NodeFactoryReg { |
195 | | - /*! |
196 | | - * \brief creator function. |
197 | | - * \param global_key Key that identifies a global single object. |
198 | | - * If this is not empty then FGlobalKey |
199 | | - * \return The created function. |
200 | | - */ |
201 | | - using FCreate = std::function<NodePtr<Node>(const std::string& global_key)>; |
202 | | - /*! |
203 | | - * \brief Global key function, only needed by global objects. |
204 | | - * \param node The node pointer. |
205 | | - * \return node The global key to the node. |
206 | | - */ |
207 | | - using FGlobalKey = std::function<std::string(const Node* node)>; |
208 | | - /*! \brief registered name */ |
209 | | - std::string name; |
210 | | - /*! |
211 | | - * \brief The creator function |
212 | | - */ |
213 | | - FCreate fcreator = nullptr; |
214 | | - /*! |
215 | | - * \brief The global key function. |
216 | | - */ |
217 | | - FGlobalKey fglobal_key = nullptr; |
218 | | - // setter of creator |
219 | | - NodeFactoryReg& set_creator(FCreate f) { // NOLINT(*) |
220 | | - this->fcreator = f; |
221 | | - return *this; |
222 | | - } |
223 | | - // setter of creator |
224 | | - NodeFactoryReg& set_global_key(FGlobalKey f) { // NOLINT(*) |
225 | | - this->fglobal_key = f; |
226 | | - return *this; |
227 | | - } |
228 | | - // global registry singleton |
229 | | - TVM_DLL static ::dmlc::Registry<::tvm::NodeFactoryReg> *Registry(); |
230 | | -}; |
231 | | - |
232 | | -/*! |
233 | | - * \brief Register a Node type |
234 | | - * \note This is necessary to enable serialization of the Node. |
235 | | - */ |
236 | | -#define TVM_REGISTER_NODE_TYPE(TypeName) \ |
237 | | - TVM_REGISTER_OBJECT_TYPE(TypeName); \ |
238 | | - static DMLC_ATTRIBUTE_UNUSED ::tvm::NodeFactoryReg & __make_Node ## _ ## TypeName ## __ = \ |
239 | | - ::tvm::NodeFactoryReg::Registry()->__REGISTER__(TypeName::_type_key) \ |
240 | | - .set_creator([](const std::string&) { return ::tvm::make_node<TypeName>(); }) |
241 | | - |
242 | | - |
243 | 76 | #define TVM_STRINGIZE_DETAIL(x) #x |
244 | 77 | #define TVM_STRINGIZE(x) TVM_STRINGIZE_DETAIL(x) |
245 | 78 | #define TVM_DESCRIBE(...) describe(__VA_ARGS__ "\n\nFrom:" __FILE__ ":" TVM_STRINGIZE(__LINE__)) |
|
0 commit comments