-
Notifications
You must be signed in to change notification settings - Fork 26.3k
JIT pass for add relu fusion. #39343
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
Closed
Closed
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
5196a66
JIT pass for add relu fusion.
kimishpatel 13411d3
Update on "JIT pass for add relu fusion."
kimishpatel 05d43ec
Update on "JIT pass for add relu fusion."
kimishpatel 160911f
Update on "JIT pass for add relu fusion."
kimishpatel ae421ce
Update on "JIT pass for add relu fusion."
kimishpatel 1a01cf3
Update on "JIT pass for add relu fusion."
kimishpatel 75e2bb2
Update on "JIT pass for add relu fusion."
kimishpatel 611edc6
Update on "JIT pass for add relu fusion."
kimishpatel 603d7b4
Update on "JIT pass for add relu fusion."
kimishpatel d1b4bf4
Update on "JIT pass for add relu fusion."
kimishpatel 8a5d609
Update on "JIT pass for add relu fusion."
kimishpatel a51f7c2
Update on "JIT pass for add relu fusion."
kimishpatel b39d12d
Update on "JIT pass for add relu fusion."
kimishpatel b2628b4
Update on "JIT pass for add relu fusion."
kimishpatel 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| #include <torch/csrc/jit/passes/fuse_relu.h> | ||
| #include <torch/csrc/jit/ir/ir.h> | ||
| #include <torch/csrc/jit/ir/subgraph_matcher.h> | ||
| #include <torch/csrc/jit/passes/subgraph_rewrite.h> | ||
|
|
||
| namespace torch { | ||
| namespace jit { | ||
|
|
||
| namespace { | ||
| void fuseAddReluImpl(std::shared_ptr<Graph>& graph) { | ||
| SubgraphRewriter rewriter; | ||
|
|
||
| std::string add_relu_0 = R"( | ||
| graph(%a, %b, %alpha): | ||
| %add_res = aten::add(%a, %b, %alpha) | ||
| %res = aten::relu(%add_res) | ||
| return (%res))"; | ||
| std::string add_relu_fused = R"( | ||
| graph(%a, %b, %alpha): | ||
| %res = aten::add_relu(%a, %b, %alpha) | ||
| return (%res))"; | ||
| rewriter.RegisterRewritePattern(add_relu_0, add_relu_fused); | ||
|
|
||
| std::string add_relu_1 = R"( | ||
| graph(%a, %b, %alpha): | ||
| %add_res = aten::add(%a, %b, %alpha) | ||
| %res = aten::relu_(%add_res) | ||
| return (%res))"; | ||
| rewriter.RegisterRewritePattern(add_relu_1, add_relu_fused); | ||
|
|
||
| std::string add_inplace_relu_1 = R"( | ||
| graph(%a, %b, %alpha): | ||
| %add_res = aten::add_(%a, %b, %alpha) | ||
| %res = aten::relu_(%add_res) | ||
| return (%res))"; | ||
| std::string add_inplace_relu_fused = R"( | ||
| graph(%a, %b, %alpha): | ||
| %res = aten::add_relu_(%a, %b, %alpha) | ||
| return (%res))"; | ||
| rewriter.RegisterRewritePattern(add_inplace_relu_1, add_inplace_relu_fused); | ||
|
|
||
| std::string add_out_relu = R"( | ||
| graph(%a, %b, %alpha, %out): | ||
| %add_res = aten::add(%a, %b, %alpha, %out) | ||
| %res = aten::relu_(%add_res) | ||
| return (%res))"; | ||
| std::string add_out_relu_fused = R"( | ||
| graph(%a, %b, %alpha, %out): | ||
| %res = aten::add_relu(%a, %b, %alpha, %out) | ||
| return (%res))"; | ||
|
|
||
| rewriter.RegisterRewritePattern(add_out_relu, add_out_relu_fused); | ||
|
|
||
| rewriter.runOnGraph(graph); | ||
| // NB: Patterns that are left out are add_ + relu and add_out + relu | ||
| // This is because inplace mutation of the testor done by add_ will be lost if | ||
| // inplace mutatation of the same tensor actually does add+relu | ||
| } | ||
| } // namespace | ||
|
|
||
| void FuseAddRelu(script::Module& module) { | ||
| auto graph = module.get_method("forward").graph(); | ||
| fuseAddReluImpl(graph); | ||
| } | ||
|
|
||
| void FuseAddRelu(std::shared_ptr<Graph>& graph) { | ||
| fuseAddReluImpl(graph); | ||
| } | ||
| } // namespace jit | ||
| } // namespace torch |
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,11 @@ | ||
| #pragma once | ||
|
|
||
| #include <torch/csrc/jit/api/module.h> | ||
| #include <torch/csrc/jit/ir/ir.h> | ||
|
|
||
| namespace torch { | ||
| namespace jit { | ||
| TORCH_API void FuseAddRelu(script::Module& module); | ||
| TORCH_API void FuseAddRelu(std::shared_ptr<Graph>& graph); | ||
| } // namespace jit | ||
| } // namespace torch |
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.
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.
Why?
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.
I think this was out of habit for quantized stuff. I was trying to bias values of tensor rather than uniformly distributed between 0 and 1 to be able to catch error which are not easily canceled out. In this specific instance it may not matter.