junrushao1994 commented on code in PR #12048:
URL: https://github.com/apache/tvm/pull/12048#discussion_r922704433
##########
src/script/printer/python_doc_printer.cc:
##########
@@ -57,6 +82,179 @@ void PythonDocPrinter::PrintTypedDoc(const LiteralDoc& doc)
{
}
}
+void PythonDocPrinter::PrintTypedDoc(const IdDoc& doc) { output_ << doc->name;
}
+
+void PythonDocPrinter::PrintTypedDoc(const AttrAccessDoc& doc) {
+ PrintDoc(doc->value);
+ output_ << "." << doc->attr;
+}
+
+void PythonDocPrinter::PrintTypedDoc(const IndexDoc& doc) {
+ PrintDoc(doc->value);
+ if (doc->indices.size() == 0) {
+ output_ << "[()]";
+ } else {
+ output_ << "[";
+ PrintJoinedDocs(doc->indices, ", ");
+ output_ << "]";
+ }
+}
+
+const std::string OperatorToString(OperationDocNode::Kind operation_kind) {
+ static const std::vector<std::string> OP_STR_TABLE = []() {
+ using OpKind = OperationDocNode::Kind;
+ std::map<OpKind, std::string> raw_table = {
+ {OpKind::kUSub, "-"}, //
+ {OpKind::kInvert, "~"}, //
+ {OpKind::kAdd, "+"}, //
+ {OpKind::kSub, "-"}, //
+ {OpKind::kMult, "*"}, //
+ {OpKind::kDiv, "/"}, //
+ {OpKind::kFloorDiv, "//"}, //
+ {OpKind::kMod, "%"}, //
+ {OpKind::kPow, "**"}, //
+ {OpKind::kLShift, "<<"}, //
+ {OpKind::kRShift, ">>"}, //
+ {OpKind::kBitAnd, "&"}, //
+ {OpKind::kBitOr, "|"}, //
+ {OpKind::kBitXor, "^"}, //
+ {OpKind::kLt, "<"}, //
+ {OpKind::kLtE, "<="}, //
+ {OpKind::kEq, "=="}, //
+ {OpKind::kNotEq, "!="}, //
+ {OpKind::kGt, ">"}, //
+ {OpKind::kGtE, ">="}, //
+ };
+
+ std::vector<std::string> table;
+ table.resize(static_cast<int>(OperationDocNode::Kind::kSpecialEnd) + 1);
+
+ for (const auto& kv : raw_table) {
+ table[static_cast<int>(kv.first)] = kv.second;
+ }
+
+ return table;
+ }();
+
+ auto op_index = static_cast<int>(operation_kind);
+ ICHECK_LT(op_index, OP_STR_TABLE.size());
+ const std::string str = OP_STR_TABLE[op_index];
+ if (str.empty()) {
+ LOG(FATAL) << "OperationDocNode::Kind " << static_cast<int>(operation_kind)
+ << " cannot be converted to operator token in Python directly.";
+ throw;
+ }
+ return str;
+}
+
+void PythonDocPrinter::PrintTypedDoc(const OperationDoc& doc) {
+ using OpKind = OperationDocNode::Kind;
+ if (doc->kind < OpKind::kUnaryEnd) {
+ // Unary Operators
+ ICHECK_EQ(doc->operands.size(), 1);
+ output_ << OperatorToString(doc->kind);
+ PrintDoc(doc->operands[0]);
+ } else if (doc->kind < OpKind::kBinaryEnd) {
+ // Binary Operator
+ ICHECK_EQ(doc->operands.size(), 2);
+ PrintDoc(doc->operands[0]);
+ output_ << " " << OperatorToString(doc->kind) << " ";
+ PrintDoc(doc->operands[1]);
+ } else if (doc->kind == OpKind::kIfThenElse) {
+ CHECK_EQ(doc->operands.size(), 3)
+ << "ValueError: IfThenElse requires 3 operands, but got " <<
doc->operands.size();
Review Comment:
You may use ICHECK to be consistent with previous ones
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]