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] Fix for recursive let #5757

Merged
merged 6 commits into from
Jun 11, 2020
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
11 changes: 11 additions & 0 deletions python/tvm/relay/transform/memory_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,17 @@ def visit_function(self, fn):
fn.type_params,
fn.attrs)

def visit_let(self, let):
bindings = []
while isinstance(let, expr.Let):
new_var = self.visit(let.var)
new_val = self.visit(let.value)
bindings.append((new_var, new_val))
let = let.body

new_body = self.visit(let)
return mk_let(bindings, new_body)

@function_pass(opt_level=0)
class MemoryPlan:
"""An explicit pass wrapper around StorageCoalesce."""
Expand Down
21 changes: 15 additions & 6 deletions src/printer/relay_text_printer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -364,12 +364,21 @@ Doc RelayTextPrinter::VisitExpr_(const IfNode* op) {
}

Doc RelayTextPrinter::VisitExpr_(const LetNode* op) {
Doc doc;
doc << "let " << AllocVar(op->var) << " = " << Print(op->value, false, true) << ";"
<< Doc::NewLine();
// we use a scope here so GNF hoisting doesn't escape too far
// and nested, unique lets are not hoisted
doc << PrintScope(op->body);
int n = 0;
Expr let = GetRef<Let>(op);
while (auto let_node = let.as<LetNode>()) {
Doc doc;
doc << "let " << AllocVar(let_node->var) << " = " << Print(let_node->value, false, true) << ";"
<< Doc::NewLine();
doc_stack_.push_back(doc);
let = let_node->body;
++n;
}
Doc doc = PrintScope(let);
for (int i = 0; i < n; ++i) {
doc = doc_stack_.back() << doc;
doc_stack_.pop_back();
}
return doc;
}

Expand Down