From b798fb071e8f2861f6c59b073f3389ea1d897fde Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=8E=8B=E6=98=8E=E5=86=AC?= <78149749+winter-wang@users.noreply.github.com> Date: Mon, 7 Mar 2022 21:38:16 +0800 Subject: [PATCH] [infrt] fold the infrt.cvtTensorOp. test=develop (#40214) --- cmake/external/llvm.cmake | 4 +- paddle/infrt/CMakeLists.txt | 2 - paddle/infrt/dialect/infrt/CMakeLists.txt | 2 + .../infrt/dialect/infrt/pass/CMakeLists.txt | 7 +++ .../infrt/dialect/infrt/pass/infrt_op_fuse.td | 23 ++++++++ .../dialect/infrt/pass/infrt_op_fuse_pass.cc | 52 +++++++++++++++++++ .../dialect/infrt/pass/infrt_op_fuse_pass.h | 24 +++++++++ paddle/infrt/dialect/pd_op_base.td | 2 +- paddle/infrt/dialect/phi/phi_ir_exec.cc | 2 + .../dialect/{pten => phi}/dense_tensor.mlir | 0 .../pten_pass.mlir => phi/phi_pass.mlir} | 0 tools/infrt/custom_pdop.td | 2 +- 12 files changed, 114 insertions(+), 6 deletions(-) create mode 100644 paddle/infrt/dialect/infrt/pass/CMakeLists.txt create mode 100644 paddle/infrt/dialect/infrt/pass/infrt_op_fuse.td create mode 100644 paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.cc create mode 100644 paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h rename paddle/infrt/tests/dialect/{pten => phi}/dense_tensor.mlir (100%) rename paddle/infrt/tests/dialect/{pten/pten_pass.mlir => phi/phi_pass.mlir} (100%) diff --git a/cmake/external/llvm.cmake b/cmake/external/llvm.cmake index a7a9e85ffd..9f6fd32ad9 100644 --- a/cmake/external/llvm.cmake +++ b/cmake/external/llvm.cmake @@ -100,8 +100,8 @@ endfunction() function(mlir_add_rewriter td_base) set(LLVM_TARGET_DEFINITIONS ${td_base}.td) mlir_tablegen(${td_base}.cpp.inc -gen-rewriters "-I${CMAKE_SOURCE_DIR}/infrt/dialect/pass") - add_public_tablegen_target(${td_base}_IncGen) - add_custom_target(${td_base}_inc DEPENDS ${td_base}_IncGen) + add_public_tablegen_target(MLIR${td_base}IncGen) + add_dependencies(mlir-headers MLIR${td_base}IncGen) endfunction() # Execute the mlir script with infrt-exec program. diff --git a/paddle/infrt/CMakeLists.txt b/paddle/infrt/CMakeLists.txt index f2768f3dfa..ed29b5b44c 100644 --- a/paddle/infrt/CMakeLists.txt +++ b/paddle/infrt/CMakeLists.txt @@ -95,9 +95,7 @@ set(infrt_mlir_incs dense_tensor_inc pd_ops_inc pd_extra_ops_inc - rewrite_inc trt_ops_inc - pd_lower_to_trt_inc ) if (INFRT_WITH_PHI) diff --git a/paddle/infrt/dialect/infrt/CMakeLists.txt b/paddle/infrt/dialect/infrt/CMakeLists.txt index daf710e0ba..08ce2d4707 100644 --- a/paddle/infrt/dialect/infrt/CMakeLists.txt +++ b/paddle/infrt/dialect/infrt/CMakeLists.txt @@ -13,3 +13,5 @@ mlir_tablegen(infrt_opsAttributes.h.inc -gen-attrdef-decls -dialect=infrt) mlir_tablegen(infrt_opsAttributes.cpp.inc -gen-attrdef-defs -dialect=infrt) add_public_tablegen_target(MLIRinfrt_opsAttributesIncGen) add_dependencies(mlir-headers MLIRinfrt_opsAttributesIncGen) + +add_subdirectory(pass) diff --git a/paddle/infrt/dialect/infrt/pass/CMakeLists.txt b/paddle/infrt/dialect/infrt/pass/CMakeLists.txt new file mode 100644 index 0000000000..19c12251a2 --- /dev/null +++ b/paddle/infrt/dialect/infrt/pass/CMakeLists.txt @@ -0,0 +1,7 @@ +core_gather_headers() + +gather_srcs(infrt_src SRCS + infrt_op_fuse_pass.cc + ) + +mlir_add_rewriter(infrt_op_fuse) diff --git a/paddle/infrt/dialect/infrt/pass/infrt_op_fuse.td b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse.td new file mode 100644 index 0000000000..ef702650b6 --- /dev/null +++ b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse.td @@ -0,0 +1,23 @@ +#ifndef INFRT_OP_FUSE +#define INFRT_OP_FUSE + +include "mlir/Interfaces/SideEffectInterfaces.td" +include "paddle/infrt/dialect/infrt/infrt_ops.td" +include "paddle/infrt/dialect/pd_ops.td" + +def FuseCvtTensorPattern : Pat< + (Infrt_CvtTensorOp (Infrt_CvtTensorOp $arg)), + (Infrt_CvtTensorOp $arg)>; + +def FuseFeedCvtTensorPattern : Pat< + (Infrt_CvtTensorOp (PD_FeedOp $name)), + (PD_FeedOp $name)>; + +def TypesAreIdentical : Constraint>; +def RedundantCvtTensorOptPattern : Pat< + (Infrt_CvtTensorOp:$res $arg), (replaceWithValue $arg), + [(TypesAreIdentical $res, $arg)]>; + + + +#endif // INFRT_OP_FUSE diff --git a/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.cc b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.cc new file mode 100644 index 0000000000..cb16e05441 --- /dev/null +++ b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.cc @@ -0,0 +1,52 @@ +// Copyright (c) 2022 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/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h" + +#include +#include "paddle/infrt/dialect/infrt/infrt_dialect.h" +#include "paddle/infrt/dialect/pd_ops.h" +namespace { +#include "paddle/infrt/dialect/infrt/pass/infrt_op_fuse.cpp.inc" // NOLINT + +/* + * infrtOpFusePass. + */ +struct InfrtOpFusePass + : public mlir::PassWrapper { + public: + ::llvm::StringRef getName() const override { return "infrtOpFusePass"; } + void runOnFunction() override; +}; +// Implementation of the InfrtOpFusePass. +void InfrtOpFusePass::runOnFunction() { + ::mlir::RewritePatternSet patterns(&getContext()); + populateWithGenerated(patterns); + (void)applyPatternsAndFoldGreedily(getOperation(), std::move(patterns)); + // Fuse pd.return Operation + auto terminator_op = getFunction().front().getTerminator(); + if (nullptr == terminator_op) return; + for (auto operand : terminator_op->getOperands()) { + auto *op1 = operand.getDefiningOp(); + auto cvt_op = ::llvm::dyn_cast<::infrt::CvtTensorOp>(op1); + if (!cvt_op) continue; + mlir::Value value = cvt_op.input(); + operand.replaceAllUsesWith(value); + cvt_op.erase(); + } +} +} // namespace +std::unique_ptr infrt::createInfrtOpFusePass() { + return std::make_unique(); +} diff --git a/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h new file mode 100644 index 0000000000..ef349a7bbc --- /dev/null +++ b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h @@ -0,0 +1,24 @@ +// Copyright (c) 2021 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 + +namespace infrt { +/* + * infrtOpFusePass. + */ +std::unique_ptr createInfrtOpFusePass(); + +} // namespace infrt diff --git a/paddle/infrt/dialect/pd_op_base.td b/paddle/infrt/dialect/pd_op_base.td index 266bdf60de..26425e3945 100644 --- a/paddle/infrt/dialect/pd_op_base.td +++ b/paddle/infrt/dialect/pd_op_base.td @@ -75,7 +75,7 @@ def PD_ElementType : Type; def PD_Tensor1 : TensorOf<[PD_ElementType]>; -def PD_Tensor : AnyTypeOf<[PD_Tensor1, LoDTensor],"pd.ttype">; +def PD_Tensor : AnyTypeOf<[PD_Tensor1, LoDTensor, DenseTensor],"pd.ttype">; def PD_Tensor_Array : VectorOf<[PD_Tensor]>; diff --git a/paddle/infrt/dialect/phi/phi_ir_exec.cc b/paddle/infrt/dialect/phi/phi_ir_exec.cc index 1df929895b..559fb90a64 100644 --- a/paddle/infrt/dialect/phi/phi_ir_exec.cc +++ b/paddle/infrt/dialect/phi/phi_ir_exec.cc @@ -16,6 +16,7 @@ #include #include #include "paddle/infrt/common/global.h" +#include "paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h" #include "paddle/infrt/dialect/mlir_loader.h" #include "paddle/infrt/dialect/phi/pass/phi_op_cvt_pass.h" @@ -38,6 +39,7 @@ int main(int argc, char** argv) { infrt::PrecisionType::FLOAT32, infrt::LayoutType::NCHW}}; phi_pass_manager.addPass(std::make_unique(valid_places)); + phi_pass_manager.addPass(infrt::createInfrtOpFusePass()); if (mlir::failed(pm.run(*module))) { std::cout << "\npass failed!\n" << std::endl; return 4; diff --git a/paddle/infrt/tests/dialect/pten/dense_tensor.mlir b/paddle/infrt/tests/dialect/phi/dense_tensor.mlir similarity index 100% rename from paddle/infrt/tests/dialect/pten/dense_tensor.mlir rename to paddle/infrt/tests/dialect/phi/dense_tensor.mlir diff --git a/paddle/infrt/tests/dialect/pten/pten_pass.mlir b/paddle/infrt/tests/dialect/phi/phi_pass.mlir similarity index 100% rename from paddle/infrt/tests/dialect/pten/pten_pass.mlir rename to paddle/infrt/tests/dialect/phi/phi_pass.mlir diff --git a/tools/infrt/custom_pdop.td b/tools/infrt/custom_pdop.td index 83e2957831..2139fbd815 100644 --- a/tools/infrt/custom_pdop.td +++ b/tools/infrt/custom_pdop.td @@ -1,4 +1,4 @@ -def PD_FeedOp : PD_Op<"feed"> { +def PD_FeedOp : PD_Op<"feed", [NoSideEffect]> { let summary = "Feed Op"; let description = [{ -- GitLab