-
Notifications
You must be signed in to change notification settings - Fork 3.8k
[Relay][FastMath] Relay pass to use fast exp/tanh #4873
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
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
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,79 @@ | ||
| /* | ||
| * 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 fast_math.cc | ||
| * \brief Replaces non linear activation functions with their fast but approximate counterparts. | ||
| */ | ||
| #include <tvm/relay/analysis.h> | ||
| #include <tvm/relay/expr_functor.h> | ||
| #include <tvm/relay/attrs/nn.h> | ||
| #include <tvm/relay/transform.h> | ||
| #include <tvm/relay/op.h> | ||
| #include "pattern_util.h" | ||
|
|
||
| namespace tvm { | ||
| namespace relay { | ||
|
|
||
| class FastMathMutator : public ExprMutator { | ||
| public: | ||
| FastMathMutator() | ||
| : exp_op_(Op::Get("exp")), | ||
| tanh_op_(Op::Get("tanh")) {} | ||
|
|
||
| Expr VisitExpr_(const CallNode* n) { | ||
| auto new_n = ExprMutator::VisitExpr_(n); | ||
| if (n->op == exp_op_) { | ||
| return FastExp(new_n.as<CallNode>()->args[0]); | ||
| } else if (n->op == tanh_op_) { | ||
| return FastTanh(new_n.as<CallNode>()->args[0]); | ||
| } | ||
| return new_n; | ||
| } | ||
|
|
||
| private: | ||
| // Cache the following ops. They will be used in the passes repeatedly for | ||
| // operator equivalence checking so that the registry lookup overhead can be | ||
| // reduced. | ||
| const Op& exp_op_; | ||
| const Op& tanh_op_; | ||
| }; | ||
|
|
||
| Expr FastMath(const Expr& e) { | ||
| return FastMathMutator().Mutate(e); | ||
| } | ||
|
|
||
| namespace transform { | ||
|
|
||
| Pass FastMath() { | ||
| runtime::TypedPackedFunc<Function(Function, IRModule, PassContext)> pass_func = | ||
| [=](Function f, IRModule m, PassContext pc) { | ||
| return Downcast<Function>(FastMath(f)); | ||
| }; | ||
| return CreateFunctionPass(pass_func, 4, "FastMath", | ||
| {tir::StringImmNode::make("InferType")}); | ||
| } | ||
|
|
||
| TVM_REGISTER_GLOBAL("relay._transform.FastMath") | ||
| .set_body_typed(FastMath); | ||
|
|
||
| } // 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
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,52 @@ | ||
| # 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.ir import IRModule | ||
| from tvm import relay | ||
| from tvm.relay.transform import FastMath | ||
|
|
||
| def test_exp(): | ||
| x = relay.var("x", shape=(1, 16, 16, 16), dtype="float32") | ||
| y = relay.exp(x) | ||
| func = relay.Function([x], y) | ||
| mod = tvm.IRModule.from_expr(func) | ||
|
|
||
| fast_mod = FastMath()(mod) | ||
| assert "fast_exp" in fast_mod.astext() | ||
|
|
||
| # Check that FastMath option works for relay.build. | ||
| with relay.build_config(opt_level=3, required_pass=['FastMath']): | ||
| fast_mod = relay.optimize(mod, target='llvm', params=None) | ||
| assert "fast_exp" in fast_mod[0].astext() | ||
|
|
||
| def test_tanh(): | ||
| x = relay.var("x", shape=(1, 16, 16, 16), dtype="float32") | ||
| y = relay.tanh(x) | ||
| func = relay.Function([x], y) | ||
| mod = tvm.IRModule.from_expr(func) | ||
|
|
||
| fast_mod = FastMath()(mod) | ||
| assert "fast_tanh" in fast_mod.astext() | ||
|
|
||
| # Check that FastMath option works for relay.build. | ||
| with relay.build_config(opt_level=3, required_pass=['FastMath']): | ||
| fast_mod = relay.optimize(mod, target='llvm', params=None) | ||
| assert "fast_tanh" in fast_mod[0].astext() | ||
|
|
||
| if __name__ == "__main__": | ||
| test_exp() | ||
| test_tanh() |
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
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.