-
Notifications
You must be signed in to change notification settings - Fork 3.6k
[Relay][Pass] Add ExtractOperators pass #8996
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
Show all changes
11 commits
Select commit
Hold shift + click to select a range
a75674d
add extractor
anwang2009 1ffb2d6
extract to array
anwang2009 70f49d6
add comments
anwang2009 2f11222
lint
anwang2009 7323302
Update tests/python/relay/test_analysis_extract_operators.py
anwang2009 4e7dc0e
op freqs
anwang2009 f71b55e
add comment
anwang2009 ea4bc24
Update python/tvm/relay/analysis/analysis.py
anwang2009 ee17c5b
oops
anwang2009 0d44b50
mixedmode visitor
anwang2009 02f2f55
oops
anwang2009 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,75 @@ | ||
/* | ||
* 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 extract_operators.cc | ||
* \brief Extract unique operators from an IRModule | ||
*/ | ||
#include <tvm/node/structural_hash.h> | ||
#include <tvm/relay/analysis.h> | ||
#include <tvm/relay/expr.h> | ||
#include <tvm/relay/expr_functor.h> | ||
|
||
namespace tvm { | ||
namespace relay { | ||
|
||
class OperatorExtractorWrapper : private MixedModeVisitor { | ||
public: | ||
explicit OperatorExtractorWrapper(const IRModule& mod) : mod_(mod) {} | ||
|
||
Map<String, tvm::Integer> Extract() { | ||
VisitExpr(this->mod_->Lookup("main")); | ||
|
||
return operator_freqs_; | ||
} | ||
|
||
private: | ||
const IRModule mod_; | ||
/*! \brief Map of operator to frequency. */ | ||
Map<String, tvm::Integer> operator_freqs_; | ||
|
||
void VisitExpr_(const CallNode* n) final { | ||
VisitExpr(n->op); | ||
|
||
auto op = n->op.as<OpNode>(); | ||
if (op) { | ||
auto it = operator_freqs_.find(op->name); | ||
ICHECK(it != operator_freqs_.end()) | ||
<< "Call's OpNode must be visited and registered before access"; | ||
operator_freqs_.Set(op->name, 1 + operator_freqs_.at(op->name)); | ||
} | ||
|
||
MixedModeVisitor::VisitExpr_(n); | ||
} | ||
|
||
void VisitExpr_(const OpNode* n) final { | ||
// NOTE: OpNode is visited only once for every operator kind | ||
// regardless of how many times that op appears in the graph. | ||
operator_freqs_.Set(n->name, 0U); | ||
} | ||
}; | ||
|
||
Map<String, tvm::Integer> ExtractOperatorsPacked(const IRModule& mod) { | ||
return OperatorExtractorWrapper(mod).Extract(); | ||
} | ||
|
||
TVM_REGISTER_GLOBAL("relay.analysis.ExtractOperators").set_body_typed(ExtractOperatorsPacked); | ||
|
||
} // 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,107 @@ | ||
# 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. | ||
"""Test function extraction""" | ||
import pytest | ||
import tvm | ||
from tvm import relay | ||
from tvm.relay.testing.resnet import get_workload | ||
from tvm.relay.testing import run_opt_pass | ||
|
||
|
||
def get_conv_net(): | ||
"""This gets the net for: | ||
conv2d | ||
/ | | ||
/ | | ||
conv2d | | ||
\ | | ||
\ | | ||
elemwise add | ||
| | ||
""" | ||
dshape = (1, 1, 5, 1) | ||
x = relay.var("x", shape=dshape) | ||
y = relay.nn.conv2d(x, relay.var("w1"), kernel_size=(3, 3), padding=(1, 1), channels=1) | ||
x1 = relay.nn.conv2d(y, relay.var("w2"), kernel_size=(3, 3), padding=(1, 1), channels=1) | ||
|
||
z = relay.add(y, x1) | ||
|
||
return tvm.IRModule.from_expr(z) | ||
|
||
|
||
def get_conv2d(): | ||
x = relay.var("x", shape=(1, 56, 56, 64)) | ||
weight1 = relay.var("weight1", shape=(3, 3, 64, 32)) | ||
y = relay.nn.conv2d( | ||
x, | ||
weight1, | ||
channels=32, | ||
kernel_size=(3, 3), | ||
padding=(1, 1), | ||
data_layout="NHWC", | ||
kernel_layout="HWIO", | ||
) | ||
return tvm.IRModule.from_expr(y) | ||
|
||
|
||
def test_extract_identity(): | ||
mod = get_conv2d() | ||
op_freqs = relay.analysis.list_op_freqs(mod) | ||
assert len(op_freqs) == 1 | ||
assert op_freqs["nn.conv2d"] == 1 | ||
|
||
|
||
def test_extract_conv_net(): | ||
mod = get_conv_net() | ||
op_freqs = relay.analysis.list_op_freqs(mod) | ||
assert len(op_freqs) == 2 | ||
assert op_freqs["add"] == 1 | ||
assert op_freqs["nn.conv2d"] == 2 | ||
|
||
|
||
def test_extract_fused(): | ||
mod = get_conv_net() | ||
mod = relay.transform.InferType()(mod) | ||
mod = relay.transform.FuseOps(3)(mod) | ||
|
||
op_freqs = relay.analysis.list_op_freqs(mod) | ||
assert len(op_freqs) == 2 | ||
assert op_freqs["add"] == 1 | ||
assert op_freqs["nn.conv2d"] == 2 | ||
|
||
|
||
def test_extract_resnet(): | ||
mod, _params = get_workload() | ||
expected_op_freqs = { | ||
"nn.batch_norm": 19, | ||
"nn.conv2d": 21, | ||
"nn.relu": 18, | ||
"nn.max_pool2d": 1, | ||
"add": 8, | ||
"nn.global_avg_pool2d": 1, | ||
"nn.batch_flatten": 1, | ||
"nn.dense": 1, | ||
"nn.bias_add": 1, | ||
"nn.softmax": 1, | ||
} | ||
op_freqs = relay.analysis.list_op_freqs(mod) | ||
assert len(op_freqs) == len 43A0 (expected_op_freqs) | ||
assert all([op_freqs[op] == expected_op_freqs[op] for op in expected_op_freqs]) | ||
|
||
|
||
if __name__ == "__main__": | ||
pytest.main([__file__]) |
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.