trt_op_converter_pass.cc 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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/tensorrt/trt_op_converter_pass.h"
15 16
#include <mlir/IR/Builders.h>
#include <mlir/Transforms/DialectConversion.h>
17
#include "paddle/infrt/dialect/pd/ir/pd_ops.h"
S
Shang Zhizhou 已提交
18
#include "paddle/infrt/dialect/tensorrt/trt_dialect_types.h"
19 20 21 22 23 24

namespace infrt {
namespace trt {

#include "paddle/infrt/dialect/tensorrt/pd_lower_to_trt.cpp.inc"  // NOLINT

S
Shang Zhizhou 已提交
25
struct PD2TRT_GraphLower : public ::mlir::RewritePattern {
26
  explicit PD2TRT_GraphLower(::mlir::MLIRContext *context)
S
Shang Zhizhou 已提交
27 28 29
      : ::mlir::RewritePattern("pd.graph", 1, context, {"trt.create_engine"}) {}
  ::mlir::LogicalResult matchAndRewrite(
      ::mlir::Operation *op, ::mlir::PatternRewriter &rewriter) const override {
30
    auto casted_op = ::llvm::dyn_cast<infrt::pd::GraphOp>(op);
S
Shang Zhizhou 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
    ::mlir::Operation::operand_range inputs = casted_op.inputs();
    auto ods_loc = rewriter.getFusedLoc(op->getLoc());
    CreateEngineOp create_engine_op;
    // inputs
    ::mlir::SmallVector<::mlir::Value, 4> trt_inputs;
    for (auto v : inputs) {
      trt_inputs.push_back(v);
    }
    create_engine_op = rewriter.create<CreateEngineOp>(
        ods_loc,
        ::llvm::SmallVector<mlir::Type, 4>(1, EngineType::get()),
        trt_inputs,
        true /*run_once*/);
    ::mlir::Block *block = new ::mlir::Block;
    block->getOperations().splice(block->begin(),
                                  casted_op.getBody()->getOperations(),
                                  casted_op.getBody()->begin(),
                                  casted_op.getBody()->end());
    create_engine_op.body().push_back(block);

    // trt.execute
    // outputs
    ::llvm::SmallVector<::mlir::Type, 4> execute_outputs_types;
    for (auto v : casted_op.getODSResults(0)) {
      execute_outputs_types.push_back(v.getType());
    }
    // inputs
    ::mlir::SmallVector<::mlir::Value, 4> execute_inputs(
        create_engine_op.getODSResults(0));
    for (auto v : inputs) {
      execute_inputs.push_back(v);
    }
    auto execute_op = rewriter.create<ExecuteOp>(
        ods_loc, execute_outputs_types, execute_inputs);

    ::llvm::SmallVector<::mlir::Value, 4> replace_values;
    for (auto v :
         ::llvm::SmallVector<::mlir::Value, 4>{execute_op.getODSResults(0)}) {
      replace_values.push_back(v);
    }
    rewriter.replaceOp(op, replace_values);
    return ::mlir::success();
  }
};

76 77 78
void TRTOpConverterPass::runOnOperation() {
  // The first thing to define is the conversion target. This will define the
  // final target for this lowering.
79
  ::mlir::ConversionTarget target(getContext());
80 81 82 83 84 85 86 87

  // We define the specific operations, or dialects, that are legal targets for
  // this lowering. In our case, we are lowering to TensorRTDialect from
  // PaddleDialect
  target.addLegalDialect<TensorRTDialect>();

  // Now that the conversion target has been defined, we just need to provide
  // the set of patterns that will lower the TensorRT operations.
88
  ::mlir::RewritePatternSet patterns(&getContext());
89
  populateWithGenerated(patterns);
S
Shang Zhizhou 已提交
90
  patterns.add<PD2TRT_GraphLower>(&getContext());
91 92 93 94

  // With the target and rewrite patterns defined, we can now attempt the
  // conversion. The conversion will signal failure if any of our `illegal`
  // operations were not converted successfully.
95
  if (::mlir::failed(
96 97 98 99 100 101
          applyPartialConversion(getOperation(), target, std::move(patterns))))
    signalPassFailure();
}

}  // namespace trt
}  // namespace infrt