-
Notifications
You must be signed in to change notification settings - Fork 3.5k
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][PASS] Common subexpression elimination #2639
Merged
+171
−0
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/*! | ||
* Copyright (c) 2019 by Contributors | ||
* | ||
* \file eliminate_common_subexpr.cc | ||
* \brief Combine common subexpressions. | ||
* | ||
* This is an optimization pass that eliminates common subexpressions. During the pass, it tries | ||
* to replace an expression with a previously appeared expression with the same input and | ||
* attributes. The fskip callback argument allows us to skip specific expressions. | ||
*/ | ||
#include <tvm/relay/pass.h> | ||
#include <tvm/relay/expr_functor.h> | ||
#include <unordered_map> | ||
#include "./pattern_util.h" | ||
|
||
namespace tvm { | ||
namespace relay { | ||
|
||
class CommonSubexprEliminator : public ExprMutator { | ||
public: | ||
explicit CommonSubexprEliminator(runtime::TypedPackedFunc<bool(Expr)> fskip): fskip_(fskip) {} | ||
|
||
Expr VisitExpr_(const CallNode* call) final { | ||
static auto op_stateful = Op::GetAttr<TOpIsStateful>("TOpIsStateful"); | ||
Expr new_expr = ExprMutator::VisitExpr_(call); | ||
const CallNode* new_call = new_expr.as<CallNode>(); | ||
CHECK(new_call); | ||
const OpNode* op = new_call->op.as<OpNode>(); | ||
AttrsEqual attrs_equal; | ||
|
||
if (new_call->args.size() == 0 || op == nullptr || op_stateful.get(GetRef<Op>(op), false)) { | ||
return new_expr; | ||
} | ||
if (fskip_ != nullptr && fskip_(new_expr)) { | ||
return new_expr; | ||
} | ||
|
||
auto it = expr_map_.find(new_call->op); | ||
if (it != expr_map_.end()) { | ||
for (const CallNode* candidate : it->second) { | ||
bool is_equivalent = true; | ||
if (!attrs_equal(new_call->attrs, candidate->attrs)) { | ||
continue; | ||
} | ||
for (size_t i = 0; i < new_call->args.size(); i++) { | ||
if (!new_call->args[i].same_as(candidate->args[i]) && | ||
!IsEqualScalar(new_call->args[i], candidate->args[i])) { | ||
is_equivalent = false; | ||
break; | ||
} | ||
} | ||
if (!is_equivalent) continue; | ||
return GetRef<Call>(candidate); | ||
} | ||
} | ||
expr_map_[new_call->op].push_back(new_call); | ||
return new_expr; | ||
} | ||
|
||
std::unordered_map<Expr, std::vector<const CallNode*>, NodeHash, NodeEqual> expr_map_; | ||
runtime::TypedPackedFunc<bool(Expr)> fskip_; | ||
}; | ||
|
||
Expr EliminateCommonSubexpr(const Expr& expr, PackedFunc callback) { | ||
return CommonSubexprEliminator(callback)(expr); | ||
} | ||
|
||
TVM_REGISTER_API("relay._ir_pass.eliminate_common_subexpr") | ||
.set_body_typed<Expr(Expr, PackedFunc)>(EliminateCommonSubexpr); | ||
|
||
} // namespace relay | ||
} // namespace tvm |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
"""Test eliminate common subexpr pass""" | ||
from tvm import relay | ||
from tvm.relay.op import register_alter_op_layout | ||
from tvm.relay import ir_pass | ||
|
||
|
||
def test_simple(): | ||
def before(): | ||
x = relay.var("x", shape=(1, 16)) | ||
y1 = relay.nn.relu(x) | ||
y2 = relay.nn.relu(x) | ||
y1 = relay.add(y1, relay.const(1.0, "float32")) | ||
y2 = relay.add(y2, relay.const(1.0, "float32")) | ||
y = relay.add(y1, y2) | ||
f = relay.Function([x], y) | ||
return f | ||
|
||
def expected(): | ||
x = relay.var("x", shape=(1, 16)) | ||
y = relay.nn.relu(x) | ||
y = relay.add(y, relay.const(1.0, "float32")) | ||
y = relay.add(y, y) | ||
f = relay.Function([x], y) | ||
return f | ||
|
||
z = before() | ||
z = ir_pass.eliminate_common_subexpr(z) | ||
assert ir_pass.alpha_equal(z, expected()) | ||
|
||
|
||
def test_callback(): | ||
def before(): | ||
x = relay.var("x", shape=(1, 16)) | ||
y1 = relay.nn.relu(x) | ||
y2 = relay.nn.relu(x) | ||
y1 = relay.add(y1, relay.const(1.0, "float32")) | ||
y2 = relay.add(y2, relay.const(1.0, "float32")) | ||
y = relay.add(y1, y2) | ||
f = relay.Function([x], y) | ||
return f | ||
|
||
def expected(): | ||
x = relay.var("x", shape=(1, 16)) | ||
y = relay.nn.relu(x) | ||
y1 = relay.add(y, relay.const(1.0, "float32")) | ||
y2 = relay.add(y, relay.const(1.0, "float32")) | ||
y = relay.add(y1, y2) | ||
f = relay.Function([x], y) | ||
return f | ||
|
||
def fskip(expr): | ||
if isinstance(expr, relay.expr.Call) and expr.op.name == 'add': | ||
return True | ||
return False | ||
|
||
z = before() | ||
z = ir_pass.eliminate_common_subexpr(z, fskip) | ||
assert ir_pass.alpha_equal(z, expected()) | ||
|
||
|
||
if __name__ == "__main__": | ||
test_simple() | ||
test_callback() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
constant_a is nullptr with
relay.var("x", shape=(1, 16))
in your test script. Is this what you expect?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, this function is intended to enable combining different Constant instance with the same value