forked from PaddlePaddle/Paddle
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[PIR]Add Fused dropout add pass (PaddlePaddle#58272)
* tmp * fix flashattn compile bug * modify unsqueeze * modify part complex bug * modify * modify cpu * [PIR]Migrate maximum into pir * Polish code * add ir_grad of static_gradient * add test * modify bug * modify * test_with_pir * close one test * add_math_op_patch * modify * modify * add mean fill_constant test * modify cpu int32 test * get_shape_tensor * delete * add default place * add dropout unittest * Update test/legacy_test/test_elementwise_div_op.py * modify review comment * modify add test * modify logic and sub * modify * add fused_gemm_epilogue pass * polish code * udate cmake * fused_dropout_add_pass * modify cmake * modify * Update test/cpp/pir/pattern_rewrite/CMakeLists.txt * Update paddle/fluid/pir/transforms/fusion/fused_dropout_add_pass.cc Co-authored-by: Yuanle Liu <yuanlehome@163.com> * Update paddle/fluid/pir/transforms/fusion/fused_dropout_add_pass.cc Co-authored-by: Yuanle Liu <yuanlehome@163.com> * Update paddle/fluid/pybind/pir.cc * Update test/ir/new_ir/fused_pass/test_fused_dropout_add.py * Update test/ir/new_ir/fused_pass/test_fused_dropout_add.py * Update paddle/fluid/pir/transforms/fusion/fused_dropout_add_pass.cc * add ci converage --------- Co-authored-by: zyfncg <zhangyunfei07@baidu.com> Co-authored-by: 0x45f <wangzhen45@baidu.com> Co-authored-by: Yuanle Liu <yuanlehome@163.com>
- Loading branch information
Showing
11 changed files
with
320 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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 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
146 changes: 146 additions & 0 deletions
146
paddle/fluid/pir/transforms/fusion/fused_dropout_add_pass.cc
This file contains 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,146 @@ | ||
// Copyright (c) 2023 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/fusion/fused_dropout_add_pass.h" | ||
|
||
#include "paddle/fluid/pir/drr/api/drr_pattern_base.h" | ||
#include "paddle/pir/pass/pass.h" | ||
#include "paddle/pir/pass/pass_registry.h" | ||
#include "paddle/pir/pattern_rewrite/pattern_rewrite_driver.h" | ||
|
||
namespace { | ||
|
||
class FusedDropoutAddPattern | ||
: public pir::drr::DrrPatternBase<FusedDropoutAddPattern> { | ||
public: | ||
void operator()(pir::drr::DrrPatternContext *ctx) const override { | ||
pir::drr::SourcePattern pat = ctx->SourcePattern(); | ||
const auto &dropout = pat.Op("pd_op.dropout", | ||
{{"p", pat.Attr("p")}, | ||
{"is_test", pat.Attr("is_test")}, | ||
{"mode", pat.Attr("mod")}, | ||
{"seed", pat.Attr("seed")}, | ||
{"fix_seed", pat.Attr("fix_seed")}}); | ||
const auto &add = pat.Op("pd_op.add"); | ||
|
||
dropout({&pat.Tensor("x"), &pat.Tensor("seed_tensor")}, | ||
{&pat.Tensor("dropout_out"), &pat.Tensor("mask")}); | ||
pat.Tensor("add_out") = add(pat.Tensor("dropout_out"), pat.Tensor("y")); | ||
|
||
pir::drr::ResultPattern res = pat.ResultPattern(); | ||
const auto &fused_dropout_add = | ||
res.Op("pd_op.fused_dropout_add", | ||
{{{"p", pat.Attr("p")}, | ||
{"is_test", pat.Attr("is_test")}, | ||
{"mode", pat.Attr("mod")}, | ||
{"seed", pat.Attr("seed")}, | ||
{"fix_seed", pat.Attr("fix_seed")}}}); | ||
fused_dropout_add( | ||
{&res.Tensor("x"), &res.Tensor("y"), &res.Tensor("seed_tensor")}, | ||
{&res.Tensor("add_out"), &res.Tensor("mask")}); | ||
} | ||
}; | ||
|
||
class FusedDropoutGradAddGradPattern | ||
: public pir::drr::DrrPatternBase<FusedDropoutAddPattern> { | ||
public: | ||
void operator()(pir::drr::DrrPatternContext *ctx) const override { | ||
pir::drr::SourcePattern pat = ctx->SourcePattern(); | ||
const auto &dropout = pat.Op("pd_op.dropout", | ||
{{"p", pat.Attr("p")}, | ||
{"is_test", pat.Attr("is_test")}, | ||
{"mode", pat.Attr("mod")}, | ||
{"seed", pat.Attr("seed")}, | ||
{"fix_seed", pat.Attr("fix_seed")}}); | ||
const auto &add = pat.Op("pd_op.add"); | ||
|
||
const auto &add_grad = pat.Op("pd_op.add_grad"); | ||
const auto &dropout_grad = pat.Op("pd_op.dropout_grad", | ||
{{"p", pat.Attr("p")}, | ||
{"is_test", pat.Attr("is_test")}, | ||
{"mode", pat.Attr("mod")}}); | ||
|
||
dropout({&pat.Tensor("x"), &pat.Tensor("seed_tensor")}, | ||
{&pat.Tensor("dropout_out"), &pat.Tensor("mask")}); | ||
pat.Tensor("add_out") = add(pat.Tensor("dropout_out"), pat.Tensor("y")); | ||
add_grad({&pat.Tensor("dropout_out"), | ||
&pat.Tensor("y"), | ||
&pat.Tensor("add_out_grad")}, | ||
{&pat.Tensor("dropout_out_grad"), &pat.Tensor("y_grad")}); | ||
dropout_grad({&pat.Tensor("mask"), &pat.Tensor("dropout_out_grad")}, | ||
{&pat.Tensor("x_grad")}); | ||
|
||
pir::drr::ResultPattern res = pat.ResultPattern(); | ||
const auto &fused_dropout_add = | ||
res.Op("pd_op.fused_dropout_add", | ||
{{{"p", pat.Attr("p")}, | ||
{"is_test", pat.Attr("is_test")}, | ||
{"mode", pat.Attr("mod")}, | ||
{"seed", pat.Attr("seed")}, | ||
{"fix_seed", pat.Attr("fix_seed")}}}); | ||
|
||
const auto &fused_dropout_add_grad = | ||
res.Op("pd_op.fused_dropout_add_grad", | ||
{{{"p", pat.Attr("p")}, | ||
{"is_test", pat.Attr("is_test")}, | ||
{"mode", pat.Attr("mod")}, | ||
{"fix_seed", pat.Attr("fix_seed")}}}); | ||
|
||
fused_dropout_add( | ||
{&res.Tensor("x"), &res.Tensor("y"), &res.Tensor("seed_tensor")}, | ||
{&res.Tensor("add_out"), &res.Tensor("mask")}); | ||
fused_dropout_add_grad({&res.Tensor("mask"), &res.Tensor("add_out_grad")}, | ||
{&res.Tensor("x_grad"), &res.Tensor("y_grad")}); | ||
} | ||
}; | ||
|
||
class FusedDropoutAddPass : public pir::Pass { | ||
public: | ||
FusedDropoutAddPass() : pir::Pass("fused_dropout_add_pass", 1) {} | ||
|
||
bool Initialize(pir::IrContext *context) override { | ||
pir::RewritePatternSet ps(context); | ||
|
||
ps.Add(FusedDropoutAddPattern().Build(context)); | ||
ps.Add(FusedDropoutGradAddGradPattern().Build(context)); | ||
patterns_ = pir::FrozenRewritePatternSet(std::move(ps)); | ||
return true; | ||
} | ||
|
||
void Run(pir::Operation *op) override { | ||
pir::GreedyRewriteConfig cfg; | ||
cfg.use_top_down_traversal = true; | ||
cfg.max_iterations = 10; | ||
pir::ApplyPatternsGreedily(op->region(0), patterns_, cfg); | ||
} | ||
|
||
bool CanApplyOn(pir::Operation *op) const override { | ||
return op->isa<::pir::ModuleOp>() && op->num_regions() > 0; | ||
} | ||
|
||
private: | ||
pir::FrozenRewritePatternSet patterns_; | ||
}; | ||
|
||
} // namespace | ||
|
||
namespace pir { | ||
|
||
std::unique_ptr<Pass> CreateFusedDropoutAddPass() { | ||
return std::make_unique<FusedDropoutAddPass>(); | ||
} | ||
|
||
} // namespace pir | ||
|
||
REGISTER_IR_PASS(fused_dropout_add_pass, FusedDropoutAddPass); |
26 changes: 26 additions & 0 deletions
26
paddle/fluid/pir/transforms/fusion/fused_dropout_add_pass.h
This file contains 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) 2023 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> CreateFusedDropoutAddPass(); | ||
|
||
} // namespace pir |
This file contains 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 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 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 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 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 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,5 @@ | ||
file( | ||
GLOB TEST_INTERP_CASES | ||
RELATIVE "${CMAKE_CURRENT_SOURCE_DIR}" | ||
"test_*.py") | ||
string(REPLACE ".py" "" TEST_INTERP_CASES "${TEST_INTERP_CASES}") |
Oops, something went wrong.