-
Notifications
You must be signed in to change notification settings - Fork 3.7k
[Relay][Text Format] Text Printer Refactor and Debug Printing #2605
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
c13b2ae
aec4724
1595a46
7b6ce8b
c11260a
0a3210b
9f56672
1f8ff51
414fab4
497f4eb
c921cd0
b7cdf43
eabd52a
fd25456
bd21270
1a58f64
073029d
14e083f
1ba8a82
3ad0ad1
fb3d910
bba6b97
1d78b97
24f255f
61033d0
73ee892
00cbd9f
9795c10
2a7e94d
1d45241
f6c26f5
10fe7bf
7aa71cf
cf91b02
46835e0
d2cfe74
9b4bad7
0549d26
d117835
d6d4065
772aad9
6d5767c
1bb55bb
69370d2
589f778
f27c130
3586edf
84b9c13
e6e2d94
7fa7cff
cbd7c89
da6642f
89f58f6
f3a5728
d090704
9d8a167
1a26c84
a587a36
fb1345f
3a5d1de
5807815
e7201fa
3184de0
f5081bd
c8ab556
da68638
c7c8a67
81ea258
fd43b91
2eb7f6e
c9e7d88
a528fe2
3a4e41d
7f9921d
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| /*! | ||
| * Copyright (c) 2019 by Contributors | ||
| * \file src/tvm/relay/doc.cc | ||
| * \brief Doc ADT used for pretty printing. | ||
| * Based on Section 1 of https://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf. | ||
| */ | ||
| #include <memory> | ||
| #include <vector> | ||
| #include "doc.h" | ||
|
|
||
| namespace tvm { | ||
| namespace relay { | ||
|
|
||
| // Text constructor | ||
| DocAtom Text(const std::string& str) { | ||
| return std::make_shared<TextNode>(str); | ||
| } | ||
|
|
||
| // Line constructor | ||
| DocAtom Line(int indent = 0) { | ||
| return std::make_shared<LineNode>(indent); | ||
| } | ||
|
|
||
| Doc::Doc(const std::string& str) { | ||
| if (str == "\n") { | ||
| this->stream_ = {Line()}; | ||
| } else { | ||
| this->stream_ = {Text(str)}; | ||
| } | ||
| } | ||
|
|
||
| // DSL function implementations | ||
|
|
||
| Doc& Doc::operator<<(const Doc& right) { | ||
| assert(this != &right); | ||
| this->stream_.insert(this->stream_.end(), right.stream_.begin(), right.stream_.end()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I dont think this is correct. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Or just use a for loop. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also check against this == &right There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You still has to catch against this == &right. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. see latest commit |
||
| return *this; | ||
| } | ||
|
|
||
| Doc& Doc::operator<<(const std::string& right) { | ||
| return *this << Doc(right); | ||
| } | ||
|
|
||
| Doc Indent(int indent, const Doc& doc) { | ||
| Doc ret; | ||
| for (auto atom : doc.stream_) { | ||
| if (auto text = std::dynamic_pointer_cast<TextNode>(atom)) { | ||
| ret.stream_.push_back(text); | ||
| } else if (auto line = std::dynamic_pointer_cast<LineNode>(atom)) { | ||
| ret.stream_.push_back(Line(indent + line->indent)); | ||
| } else {assert(false);} | ||
| } | ||
| return ret; | ||
| } | ||
|
|
||
| std::string Doc::str() { | ||
| std::ostringstream os; | ||
| for (auto atom : this->stream_) { | ||
| if (auto text = std::dynamic_pointer_cast<TextNode>(atom)) { | ||
| os << text->str; | ||
| } else if (auto line = std::dynamic_pointer_cast<LineNode>(atom)) { | ||
| os << "\n" << std::string(line->indent, ' '); | ||
| } else {assert(false);} | ||
| } | ||
| return os.str(); | ||
| } | ||
|
|
||
| Doc PrintVec(const std::vector<Doc>& vec, const Doc& sep) { | ||
| Doc seq; | ||
| if (vec.size() != 0) { | ||
| seq = vec[0]; | ||
| for (size_t i = 1; i < vec.size(); i++) { | ||
| seq << sep << vec[i]; | ||
| } | ||
| } | ||
| return seq; | ||
joshpoll marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| Doc PrintBool(bool value) { | ||
| if (value) { | ||
| return Doc("True"); | ||
| } else { | ||
| return Doc("False"); | ||
| } | ||
| } | ||
|
|
||
| Doc PrintDType(DataType dtype) { | ||
| return Doc(runtime::TVMType2String(Type2TVMType(dtype))); | ||
| } | ||
|
|
||
| Doc PrintString(const std::string& value) { | ||
| // TODO(M.K.): add escape. | ||
| Doc doc; | ||
| return doc << "\"" << value << "\""; | ||
| } | ||
|
|
||
| } // namespace relay | ||
| } // namespace tvm | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| /*! | ||
| * Copyright (c) 2019 by Contributors | ||
| * \file tvm/relay/doc.h | ||
| * \brief Doc ADT used for pretty printing. | ||
| * Based on Section 1 of | ||
| * https://homepages.inf.ed.ac.uk/wadler/papers/prettier/prettier.pdf, but with | ||
| * a vector instead of an implicitly linked list. | ||
| */ | ||
| #ifndef TVM_RELAY_IR_DOC_H_ | ||
| #define TVM_RELAY_IR_DOC_H_ | ||
|
|
||
| #include <tvm/relay/expr.h> | ||
| #include <memory> | ||
| #include <string> | ||
| #include <vector> | ||
|
|
||
| namespace tvm { | ||
| namespace relay { | ||
|
|
||
| // Doc Atom ADT | ||
| struct DocAtomNode { | ||
| virtual ~DocAtomNode() = default; | ||
| }; | ||
|
|
||
| using DocAtom = std::shared_ptr<DocAtomNode>; | ||
|
|
||
| struct TextNode : DocAtomNode { | ||
| std::string str; | ||
|
|
||
| explicit TextNode(const std::string& str) : str(str) {} | ||
| }; | ||
|
|
||
| struct LineNode : DocAtomNode { | ||
| int indent; | ||
|
|
||
| explicit LineNode(int indent) : indent(indent) {} | ||
| }; | ||
|
|
||
| // Doc is a stream-like interface | ||
| class Doc { | ||
| public: | ||
| Doc() {} | ||
| explicit Doc(const std::string& str); | ||
|
|
||
| // Append right to this. | ||
| Doc& operator<<(const Doc& right); | ||
| // Like above, but automatically lifts string to a Doc. | ||
| Doc& operator<<(const std::string& right); | ||
| // Like above, but converts right to a string first. | ||
| template<typename T> | ||
| Doc& operator<<(const T& right) { | ||
| std::ostringstream os; | ||
| os << right; | ||
| return *this << os.str(); | ||
| } | ||
|
|
||
| // Indent a doc stream. | ||
| friend Doc Indent(int indent, const Doc& doc); | ||
|
|
||
| // Wadler's `layout` | ||
| std::string str(); | ||
|
|
||
| private: | ||
| std::vector<DocAtom> stream_; | ||
| }; | ||
|
|
||
| // DSL functions | ||
|
|
||
| // Render vectors of docs with a separator. e.g. PrintVec([1, 2, 3], f) -> 1f2f3 | ||
| Doc PrintVec(const std::vector<Doc>& vec, const Doc& sep = Doc(", ")); | ||
| // Print a constant bool value. | ||
| Doc PrintBool(bool value); | ||
| // Print a data type. | ||
| Doc PrintDType(DataType dtype); | ||
| // Print a string. | ||
| Doc PrintString(const std::string& value); | ||
| /*! | ||
| * \brief special method to print out const scalar | ||
| * \param dtype The data type | ||
| * \param data The pointer to hold the data. | ||
| */ | ||
| template<typename T> | ||
| Doc PrintConstScalar(DataType dtype, const T* data) { | ||
| std::ostringstream os; | ||
| if (dtype == Int(32)) { | ||
| os << data[0]; | ||
| } else if (dtype == Float(32)) { | ||
| os << data[0] << 'f'; | ||
| } else if (dtype == Bool()) { | ||
| return PrintBool(data[0] != 0); | ||
| } else { | ||
| os << dtype << "(" << data[0] << ")"; | ||
| } | ||
| return Doc(os.str()); | ||
| } | ||
|
|
||
| } // namespace relay | ||
| } // namespace tvm | ||
| #endif // TVM_RELAY_IR_DOC_H_ |
Uh oh!
There was an error while loading. Please reload this page.