-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[Relay] Add DefuseOps pass #6946
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
Merged
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 hidden or 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 hidden or 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,88 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
/*! | ||
* | ||
* \file src/relay/transforms/defuse_ops.cc | ||
* \brief This is an inverse operation of fusion pass. It transforms a fused | ||
* program returned by relay::transform::FuseOps into the program before FuseOps. | ||
* (i.e., x == DefuseOps(FuseOps(x))) | ||
*/ | ||
|
||
#include <tvm/relay/attrs/transform.h> | ||
#include <tvm/relay/expr_functor.h> | ||
#include <tvm/relay/transform.h> | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
|
||
#include "pattern_utils.h" | ||
|
||
namespace tvm { | ||
namespace relay { | ||
|
||
class DefuseOpsMutator : public ExprMutator { | ||
public: | ||
class FuncBodyMutator : public ExprMutator { | ||
public: | ||
explicit FuncBodyMutator(const Array<Expr>& args) : ExprMutator() { args_ = args; } | ||
|
||
Expr VisitExpr_(const VarNode* n) { | ||
const std::string& name = n->name_hint(); | ||
ICHECK(!name.empty() && (name[0] == 'p')); | ||
std::string id_str = name.substr(1); | ||
int id = std::stoi(id_str); | ||
ICHECK(id >= 0 && size_t(id) < args_.size()); | ||
return args_[id]; | ||
} | ||
|
||
private: | ||
Array<Expr> args_; | ||
}; | ||
|
||
Expr VisitExpr_(const CallNode* n) { | ||
auto new_n = ExprMutator::VisitExpr_(n); | ||
|
||
if (const auto* call = new_n.as<CallNode>()) { | ||
if (const auto* func = call->op.as<FunctionNode>()) { | ||
if (func->body->IsInstance<CallNode>()) { | ||
return FuncBodyMutator(call->args).Mutate(func->body); | ||
} | ||
} | ||
} | ||
return new_n; | ||
} | ||
}; | ||
|
||
Expr DefuseOps(const Expr& expr) { return DefuseOpsMutator().Mutate(expr); } | ||
|
||
namespace transform { | ||
|
||
Pass DefuseOps() { | ||
runtime::TypedPackedFunc<Function(Function, IRModule, PassContext)> pass_func = | ||
[=](Function f, IRModule m, PassContext pc) { return Downcast<Function>(DefuseOps(f)); }; | ||
return CreateFunctionPass(pass_func, 3, "DefuseOps", {"InferType"}); | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("relay._transform.DefuseOps").set_body_typed(DefuseOps); | ||
|
||
} // namespace transform | ||
|
||
} // namespace relay | ||
} // namespace tvm |
This file contains hidden or 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,68 @@ | ||
# Licensed to the Apache Software Foundation (ASF) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The ASF licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
import tvm | ||
from tvm import relay | ||
from tvm.relay import transform | ||
from tvm.relay.testing import run_opt_pass | ||
|
||
|
||
def test_defuse_simple(): | ||
"""Simple testcase.""" | ||
|
||
def before(): | ||
x = relay.var("x", shape=(10, 20)) | ||
y = relay.add(x, relay.const(1, "float32")) | ||
z = relay.exp(y) | ||
w = relay.squeeze(z) | ||
return relay.Function([x], w) | ||
|
||
x = before() | ||
x = run_opt_pass(x, transform.InferType()) | ||
fused = run_opt_pass(x, transform.FuseOps()) | ||
defused = run_opt_pass(fused, transform.DefuseOps()) | ||
|
||
assert tvm.ir.structural_equal(x, defused) | ||
|
||
|
||
def test_inception_like(): | ||
def conv(data): | ||
y = relay.nn.conv2d(data, relay.var("w"), kernel_size=(3, 3), padding=(1, 1), channels=16) | ||
return relay.nn.relu(data=y) | ||
|
||
def inception_like(data): | ||
c0 = conv(data) | ||
c1 = conv(data) | ||
return relay.concatenate((c0, c1), axis=1) | ||
|
||
def before(dshape): | ||
x = relay.var("x", shape=dshape) | ||
in1 = inception_like(x) | ||
in2 = inception_like(in1) | ||
return relay.Function(relay.analysis.free_vars(in2), in2) | ||
|
||
dshape = (1, 16, 64, 64) | ||
x = before(dshape) | ||
x = run_opt_pass(x, transform.InferType()) | ||
fused = run_opt_pass(x, transform.FuseOps()) | ||
defused = run_opt_pass(fused, transform.DefuseOps()) | ||
|
||
assert tvm.ir.structural_equal(x, defused) | ||
|
||
|
||
if __name__ == "__main__": | ||
test_defuse_simple() | ||
test_inception_like() |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.