|
| 1 | +// Copyright (c) 2024 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "paddle/fluid/pir/transforms/onednn/depthwise_conv_onednn_pass.h" |
| 16 | + |
| 17 | +#include "paddle/fluid/pir/dialect/operator/ir/onednn_op.h" |
| 18 | +#include "paddle/fluid/pir/dialect/operator/ir/pd_op.h" |
| 19 | +#include "paddle/fluid/pir/drr/include/drr_pattern_base.h" |
| 20 | + |
| 21 | +#include "paddle/pir/include/pass/pass.h" |
| 22 | +#include "paddle/pir/include/pass/pass_registry.h" |
| 23 | + |
| 24 | +namespace { |
| 25 | + |
| 26 | +class DepthwiseConvPattern : public paddle::drr::DrrPatternBase { |
| 27 | + private: |
| 28 | + std::string depthwise_conv_name_; |
| 29 | + |
| 30 | + public: |
| 31 | + explicit DepthwiseConvPattern(const std::string &conv_name) |
| 32 | + : depthwise_conv_name_(conv_name) {} |
| 33 | + |
| 34 | + std::string name() const override { return "DepthwiseConvPattern"; } |
| 35 | + |
| 36 | + uint32_t benefit() const override { return 2; } |
| 37 | + |
| 38 | + void operator()(paddle::drr::DrrPatternContext *ctx) const override { |
| 39 | + paddle::drr::SourcePattern pat = ctx->SourcePattern(); |
| 40 | + |
| 41 | + const auto &depthwise_conv = |
| 42 | + pat.Op(depthwise_conv_name_, |
| 43 | + {{"strides", pat.Attr("strides")}, |
| 44 | + {"paddings", pat.Attr("paddings")}, |
| 45 | + {"padding_algorithm", pat.Attr("padding_algorithm")}, |
| 46 | + {"dilations", pat.Attr("dilations")}, |
| 47 | + {"groups", pat.Attr("groups")}, |
| 48 | + {"data_format", pat.Attr("data_format")}}); |
| 49 | + |
| 50 | + depthwise_conv({&pat.Tensor("input"), &pat.Tensor("filter")}, |
| 51 | + {&pat.Tensor("conv_out")}); |
| 52 | + |
| 53 | + pat.RequireNativeCall([&](const paddle::drr::MatchContext &match_ctx) { |
| 54 | + std::set<std::string> padding_algorithm = {"EXPLICIT", "SAME", "VALID"}; |
| 55 | + std::set<std::string> data_format = {"NCHW", "NHWC", "AnyLayout"}; |
| 56 | + if (padding_algorithm.count( |
| 57 | + match_ctx.Attr<std::string>("padding_algorithm")) == 0 || |
| 58 | + data_format.count(match_ctx.Attr<std::string>("data_format")) == 0 || |
| 59 | + match_ctx.Attr<int>("groups") < 1) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + return true; |
| 63 | + }); |
| 64 | + |
| 65 | + paddle::drr::ResultPattern res = pat.ResultPattern(); |
| 66 | + |
| 67 | + const auto &conv2d = |
| 68 | + res.Op(paddle::dialect::Conv2dOp::name(), |
| 69 | + {{ |
| 70 | + {"strides", pat.Attr("strides")}, |
| 71 | + {"paddings", pat.Attr("paddings")}, |
| 72 | + {"padding_algorithm", pat.Attr("padding_algorithm")}, |
| 73 | + {"dilations", pat.Attr("dilations")}, |
| 74 | + {"groups", pat.Attr("groups")}, |
| 75 | + {"data_format", pat.Attr("data_format")}, |
| 76 | + }}); |
| 77 | + |
| 78 | + conv2d({&res.Tensor("input"), &res.Tensor("filter")}, |
| 79 | + {&res.Tensor("conv_out")}); |
| 80 | + } |
| 81 | +}; |
| 82 | + |
| 83 | +class DepthwiseConvMKLDNNPass : public pir::PatternRewritePass { |
| 84 | + public: |
| 85 | + DepthwiseConvMKLDNNPass() |
| 86 | + : pir::PatternRewritePass("depthwise_conv_mkldnn_pass", 2) {} |
| 87 | + |
| 88 | + pir::RewritePatternSet InitializePatterns(pir::IrContext *context) override { |
| 89 | + pir::RewritePatternSet ps(context); |
| 90 | + ps.Add(paddle::drr::Create<DepthwiseConvPattern>( |
| 91 | + context, paddle::dialect::DepthwiseConv2dOp::name())); |
| 92 | + return ps; |
| 93 | + } |
| 94 | +}; |
| 95 | + |
| 96 | +} // namespace |
| 97 | + |
| 98 | +namespace pir { |
| 99 | + |
| 100 | +std::unique_ptr<Pass> CreateDepthwiseConvMKLDNNPass() { |
| 101 | + // pd_op.depthwise_conv -> pd_op.conv2d |
| 102 | + return std::make_unique<DepthwiseConvMKLDNNPass>(); |
| 103 | +} |
| 104 | + |
| 105 | +} // namespace pir |
| 106 | + |
| 107 | +REGISTER_IR_PASS(depthwise_conv_onednn_pass, DepthwiseConvMKLDNNPass); |
0 commit comments