@@ -21,6 +21,41 @@ using ::tvm::Node;
2121using ::tvm::NodeRef;
2222using ::tvm::AttrVisitor;
2323
24+ /* !
25+ * \brief save the node as well as all the node it depends on as json.
26+ * This can be used to serialize any TVM object
27+ *
28+ * \return the string representation of the node.
29+ */
30+ std::string SaveJSON (const NodeRef& node);
31+
32+ /* !
33+ * \brief Internal implementation of LoadJSON
34+ * Load tvm Node object from json and return a shared_ptr of Node.
35+ * \param json_str The json string to load from.
36+ *
37+ * \return The shared_ptr of the Node.
38+ */
39+ std::shared_ptr<Node> LoadJSON_ (std::string json_str);
40+
41+ /* !
42+ * \brief Load the node from json string.
43+ * This can be used to deserialize any TVM object.
44+ *
45+ * \param json_str The json string to load from.
46+ *
47+ * \tparam NodeType the nodetype
48+ *
49+ * \code
50+ * Expr e = LoadJSON<Expr>(json_str);
51+ * \endcode
52+ */
53+ template <typename NodeType,
54+ typename = typename std::enable_if<std::is_base_of<NodeRef, NodeType>::value>::type >
55+ inline NodeType LoadJSON (const std::string& json_str) {
56+ return NodeType (LoadJSON_ (json_str));
57+ }
58+
2459/* ! \brief typedef the factory function of data iterator */
2560using NodeFactory = std::function<std::shared_ptr<Node> ()>;
2661/* !
@@ -32,8 +67,9 @@ struct NodeFactoryReg
3267};
3368
3469#define TVM_REGISTER_NODE_TYPE (TypeName ) \
35- DMLC_REGISTRY_REGISTER (::tvm::NodeFactoryReg, NodeFactoryReg, TypeName) \
36- .set_body([]() { return std::make_shared<TypeName>(); })
70+ static DMLC_ATTRIBUTE_UNUSED ::tvm::NodeFactoryReg & __make_Node ## _ ## TypeName ## __ = \
71+ ::dmlc::Registry<::tvm::NodeFactoryReg>::Get()->__REGISTER__ (TypeName::_type_key) \
72+ .set_body([]() { return std::make_shared<TypeName>(); })
3773
3874} // namespace tvm
3975#endif // TVM_BASE_H_
0 commit comments