Skip to content

Commit

Permalink
[Relay] Fix for recursive let (apache#5757)
Browse files Browse the repository at this point in the history
* Make let processing iterative

* Try again

* Fix pretty printer overflow

* cleanup

* fix lint

* Fix text printer

Co-authored-by: Jared Roesch <roeschinc@gmail.com>
Co-authored-by: Jared Roesch <jroesch@octoml.ai>
  • Loading branch information
3 people authored and Trevor Morris committed Jun 12, 2020
1 parent e7a3b38 commit 827e103
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 6 deletions.
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

0 comments on commit 827e103

Please sign in to comment.