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

dicrease the complexity of CalcDep from exponential to linear #4053

Merged
merged 1 commit into from
Oct 7, 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
10 changes: 9 additions & 1 deletion src/relay/pass/dead_code.cc
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,15 @@ class CalcDep : private ExprVisitor {
VarMap<size_t> use_map_;

void VisitExpr(const Expr& e) final {
return ExprFunctor<void(const Expr& e)>::VisitExpr(e);
visit_counter_[e.get()]++;
// The dce code seprate variable into three parts:
// used 0 times (remove)
// used 1 times (inline)
// used 2 times (dont do anything).
if (visit_counter_[e.get()] <= 2) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why <=2?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the dce code seprate variable into three parts:
used 0 times (remove)
used 1 times (inline)
used 2 times (dont do anything).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

okay, let's add a comment

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

using TParent = ExprFunctor<void(const Expr&)>;
TParent::VisitExpr(e);
}
}

void VisitExpr_(const LetNode* l) final {
Expand Down
9 changes: 9 additions & 0 deletions tests/python/relay/test_pass_dead_code_elimination.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,9 @@
from tvm.relay import Function, transform
from tvm.relay.analysis import alpha_equal, graph_equal, free_vars, assert_alpha_equal
from tvm.relay.op import log, add, equal, subtract
from tvm.relay.testing import inception_v3

import pytest

class env:
def __init__(self):
Expand Down Expand Up @@ -129,6 +131,12 @@ def test_tuple_get_item():
assert alpha_equal(Function(free_vars(dced), dced), Function(free_vars(g), g))


@pytest.mark.timeout(timeout=10, method="thread")
def test_complexity():
g = inception_v3.get_net(1, 1000, (3, 299, 299), 'float32')
run_opt_pass(g, transform.DeadCodeElimination())


if __name__ == "__main__":
test_let()
test_used_let()
Expand All @@ -138,3 +146,4 @@ def test_tuple_get_item():
test_recursion_dead()
test_op_let()
test_tuple_get_item()
test_complexity()