Skip to content
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

[Relay][Text Format] Fix Pretty Printing Annotations #3041

Merged
merged 2 commits into from
Apr 25, 2019
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
24 changes: 13 additions & 11 deletions src/relay/ir/pretty_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -156,14 +156,19 @@ class PrettyPrinter :
*/
Doc PrintOptionalInfo(const Expr& expr) {
Doc doc;
// additional information in comment.
if (annotate_ != nullptr) {
return doc << " /* " << annotate_(expr) << " */";
} else if (expr->checked_type_.defined()) {
return doc << " /* ty=" << Print(expr->checked_type()) << " */";
// default annotations
if (annotate_ == nullptr) {
if ((expr.as<ConstantNode>() || expr.as<CallNode>()) && expr->checked_type_.defined()) {
doc << " /* ty=" << Print(expr->checked_type()) << " */";
}
} else {
return doc;
std::string annotated_expr = annotate_(expr);
if (annotated_expr != "") {
doc << annotated_expr;
}
}

return doc;
}

// indent a new body
Expand Down Expand Up @@ -361,9 +366,7 @@ class PrettyPrinter :
printed_expr = VisitExpr(expr);
}

if (expr.as<CallNode>()) {
printed_expr << PrintOptionalInfo(expr);
}
printed_expr << PrintOptionalInfo(expr);

// add expr to doc
if (expr.as<VarNode>()) {
Expand Down Expand Up @@ -409,8 +412,7 @@ class PrettyPrinter :
}
// default fall-back, record it as meta node.
Doc doc;
return doc << Print(GetRef<NodeRef>(op), true)
<< PrintOptionalInfo(GetRef<Expr>(op));
return doc << Print(GetRef<NodeRef>(op), true);
}

Doc VisitExpr_(const TupleNode* op) final {
Expand Down
2 changes: 1 addition & 1 deletion tests/python/relay/test_ir_text_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def test_env():
assert "def @myf" in str(env)
assert "add(%0, %0) /* ty=float32 */" in text
assert "add(%0, %0) /* ty=float32 */" in str(env)
show(env.astext(annotate=lambda x: str(x.checked_type.dtype)))
show(env.astext(annotate=lambda x: str(x.checked_type.dtype) if type(x) == relay.Call else ""))
show(text)


Expand Down