-
Notifications
You must be signed in to change notification settings - Fork 5.9k
[PIR] pir onednn bn act fuse pass #61307
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
wanghuancoder
merged 25 commits into
PaddlePaddle:develop
from
wanghuancoder:pir_onednn_bn_act_fuse_pass
Jan 31, 2024
Merged
Changes from 24 commits
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
2990b78
pir onednn add conv_bias_pass
wanghuancoder 71aa640
refine
wanghuancoder 1effd6d
refine
wanghuancoder b9725eb
refine
wanghuancoder cc44153
refine
wanghuancoder 3d1453a
refine
wanghuancoder bfb380e
refine
wanghuancoder 333fd41
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
wanghuancoder 987e223
refine
wanghuancoder d3dc3d7
refine
wanghuancoder dd50d08
refine
wanghuancoder c03c822
Merge branch 'develop' of https://github.com/PaddlePaddle/Paddle into…
wanghuancoder 31796de
refine
wanghuancoder 518b7d1
refine
wanghuancoder 8b30bd3
refine
wanghuancoder cc2d164
merge
wanghuancoder 72a1808
refine
wanghuancoder 9b9d0d2
add conv3d_bias_fuse_pass
wanghuancoder 891e41e
refine
wanghuancoder 67e4b5c
pir onednn add batch_norm_act_fuse_pass
wanghuancoder 76e0838
refine
wanghuancoder d9e0a47
Merge branch 'develop' into pir_onednn_conv3d_bias_pass
wanghuancoder 869119a
merge
wanghuancoder c51a008
Merge branch 'pir_onednn_conv3d_bias_pass' of https://github.com/wang…
wanghuancoder 0d2327e
refine
wanghuancoder 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
129 changes: 129 additions & 0 deletions
129
paddle/fluid/pir/transforms/onednn/batch_norm_act_fuse_pass.cc
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,129 @@ | ||
| // Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed 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. | ||
|
|
||
| #include "paddle/fluid/pir/transforms/onednn/batch_norm_act_fuse_pass.h" | ||
|
|
||
| #include "paddle/fluid/pir/dialect/operator/ir/onednn_op.h" | ||
| #include "paddle/fluid/pir/dialect/operator/ir/pd_op.h" | ||
| #include "paddle/fluid/pir/drr/include/drr_pattern_base.h" | ||
|
|
||
| #include "paddle/pir/pass/pass.h" | ||
| #include "paddle/pir/pass/pass_registry.h" | ||
|
|
||
| namespace { | ||
| class BatchNormActFusePattern : public paddle::drr::DrrPatternBase { | ||
| public: | ||
| BatchNormActFusePattern(std::string bn_name, std::string fused_bn_name) | ||
| : bn_name_(bn_name), fused_bn_name_(fused_bn_name) {} | ||
| void operator()(paddle::drr::DrrPatternContext *ctx) const override { | ||
| paddle::drr::SourcePattern pat = ctx->SourcePattern(); | ||
|
|
||
| const auto &bn = | ||
| pat.Op(bn_name_, | ||
| {{"momentum", pat.Attr("momentum")}, | ||
| {"epsilon", pat.Attr("epsilon")}, | ||
| {"data_format", pat.Attr("data_format")}, | ||
| {"use_global_stats", pat.Attr("use_global_stats")}, | ||
| {"trainable_statistics", pat.Attr("trainable_statistics")}, | ||
| {"is_test", pat.Attr("is_test")}}); | ||
| const auto &relu = pat.Op(paddle::dialect::ReluOp::name()); | ||
| bn({&pat.Tensor("x"), | ||
| &pat.Tensor("mean"), | ||
| &pat.Tensor("variance"), | ||
| &pat.Tensor("scale"), | ||
| &pat.Tensor("bias")}, | ||
| {&pat.Tensor("bn_out"), | ||
| &pat.Tensor("mean_out"), | ||
| &pat.Tensor("variance_out"), | ||
| &pat.Tensor("saved_mean"), | ||
| &pat.Tensor("saved_variance"), | ||
| &pat.Tensor("reserve_space")}); | ||
| pat.Tensor("relu_out") = relu(pat.Tensor("bn_out")); | ||
|
|
||
| pat.RequireNativeCall([&](const paddle::drr::MatchContext &match_ctx) { | ||
| float epsilon = match_ctx.Attr<float>("epsilon"); | ||
| if (epsilon < 0.0 || epsilon > 0.001 || | ||
| match_ctx.Attr<bool>("trainable_statistics") == true || | ||
| match_ctx.Attr<bool>("is_test") == false) { | ||
| return false; | ||
| } | ||
| return true; | ||
| }); | ||
|
|
||
| paddle::drr::ResultPattern res = pat.ResultPattern(); | ||
|
|
||
| const auto &fused_bn = | ||
| res.Op(fused_bn_name_, | ||
| {{ | ||
| {"is_test", res.BoolAttr(true)}, | ||
| {"momentum", pat.Attr("momentum")}, | ||
| {"epsilon", pat.Attr("epsilon")}, | ||
| {"data_format", pat.Attr("data_format")}, | ||
| {"use_global_stats", pat.Attr("use_global_stats")}, | ||
| {"trainable_statistics", res.BoolAttr(false)}, | ||
| {"fuse_with_relu", res.BoolAttr(true)}, | ||
| }}); | ||
|
|
||
| fused_bn({&res.Tensor("x"), | ||
| &res.Tensor("mean"), | ||
| &res.Tensor("variance"), | ||
| &res.Tensor("scale"), | ||
| &res.Tensor("bias")}, | ||
| {&res.Tensor("relu_out"), | ||
| &res.Tensor("mean_out"), | ||
| &res.Tensor("variance_out"), | ||
| &res.Tensor("saved_mean"), | ||
| &res.Tensor("saved_variance"), | ||
| &res.Tensor("reserve_space")}); | ||
| } | ||
|
|
||
| std::string name() const override { return "BatchNormActFusePattern"; } | ||
|
|
||
| uint32_t benefit() const override { return 2; } | ||
|
|
||
| private: | ||
| std::string bn_name_; | ||
| std::string fused_bn_name_; | ||
| }; | ||
|
|
||
| class BatchNormActFusePass : public pir::PatternRewritePass { | ||
| public: | ||
| BatchNormActFusePass() | ||
| : pir::PatternRewritePass("batch_norm_act_fuse_pass", 2) {} | ||
|
|
||
| pir::RewritePatternSet InitializePatterns(pir::IrContext *context) override { | ||
| pir::RewritePatternSet ps(context); | ||
| ps.Add(BatchNormActFusePattern(paddle::dialect::BatchNormOp::name(), | ||
| paddle::onednn::dialect::BatchNormOp::name()) | ||
| .Build(context)); | ||
| ps.Add( | ||
| BatchNormActFusePattern(paddle::dialect::BatchNorm_Op::name(), | ||
| paddle::onednn::dialect::BatchNorm_Op::name()) | ||
| .Build(context)); | ||
| return ps; | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| namespace pir { | ||
|
|
||
| std::unique_ptr<Pass> CreateBatchNormActFusePass() { | ||
| // pd_op.batch_norm + pd_op.relu -> onednn_op.batch_norm | ||
| return std::make_unique<BatchNormActFusePass>(); | ||
| } | ||
|
|
||
| } // namespace pir | ||
|
|
||
| REGISTER_IR_PASS(batch_norm_act_fuse_pass, BatchNormActFusePass); | ||
26 changes: 26 additions & 0 deletions
26
paddle/fluid/pir/transforms/onednn/batch_norm_act_fuse_pass.h
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,26 @@ | ||
| // Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. | ||
| // | ||
| // Licensed 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. | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <memory> | ||
| #include "paddle/pir/core/dll_decl.h" | ||
|
|
||
| namespace pir { | ||
|
|
||
| class Pass; | ||
|
|
||
| IR_API std::unique_ptr<Pass> CreateBatchNormActFusePass(); | ||
|
|
||
| } // namespace pir |
Oops, something went wrong.
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.
const&?