Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions include/tvm/relay/dataflow_pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
#include <tvm/relay/type.h>

#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

namespace tvm {
Expand Down Expand Up @@ -537,6 +539,45 @@ DFPattern IsTuple(const Array<DFPattern>& fields);
/*! \brief Syntatic Sugar for creating a TupleGetItemPattern*/
DFPattern IsTupleGetItem(const DFPattern tuple, int index = -1);

/*! \brief A printer class to print pattern. */
class DFPatternPrinter : public ReprPrinter {
public:
std::stringstream string_stream{};

std::unordered_map<DFPattern, std::pair<size_t, std::string>, ObjectPtrHash, ObjectPtrEqual>
memo_{};
/*! \brief Subpatterns that are encountered more than once during printing. If a subpattern has
* already printed, only the pattern ID will be printed in the next encounter of the same pattern.
* This avoids printing a subpattern infinitely many times is the considered pattern involves
* recursion.*/
std::vector<DFPattern> auxiliary_patterns{};

DFPatternPrinter(std::ostream& stream) // NOLINT(*)
: ReprPrinter(stream) {}
TVM_DLL void Print(const ObjectRef& node);
using FType = NodeFunctor<void(const ObjectRef&, DFPatternPrinter*)>;
TVM_DLL static FType& vtable();
};

inline std::ostream& operator<<(std::ostream& os,
const DFPattern& n) { // NOLINT(*)
std::stringstream string_stream{}, tmp_stream{};
DFPatternPrinter printer{tmp_stream};
printer.Print(n);
string_stream << "Main pattern:" << std::endl;
string_stream << printer.string_stream.str();
string_stream << std::endl;
string_stream << "Auxiliary patterns:";
for (const DFPattern& pat : printer.auxiliary_patterns) {
string_stream << std::endl;
string_stream << printer.memo_[pat].second;
}
os << string_stream.str();
return os;
}

String PrettyPrint(const DFPattern& pattern);

} // namespace relay
} // namespace tvm
#endif // TVM_RELAY_DATAFLOW_PATTERN_H_
Loading