trt_exec.cc 3.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
// 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.
14

15 16
#include <llvm/Support/CommandLine.h>
#include <mlir/Pass/PassManager.h>
17

18 19
#include <iostream>
#include <string>
20

21
#include "paddle/infrt/common/global.h"
22
#include "paddle/infrt/dialect/infrt/pass/infrt_weights_unfold_pass.h"
23 24 25
#include "paddle/infrt/dialect/mlir_loader.h"
#include "paddle/infrt/dialect/tensorrt/trt_graph_fuse_pass.h"
#include "paddle/infrt/dialect/tensorrt/trt_graph_split_pass.h"
26
#include "paddle/infrt/dialect/tensorrt/trt_op_converter_pass.h"
27
#include "paddle/infrt/dialect/tensorrt/trt_op_teller_pass.h"
W
Wilber 已提交
28 29 30 31 32 33 34 35 36
#include "paddle/infrt/dialect/tensorrt/trt_type_convert_pass.h"
#include "paddle/infrt/host_context/core_runtime.h"
#include "paddle/infrt/host_context/kernel_registry.h"
#include "paddle/infrt/host_context/mlir_to_runtime_translate.h"
#include "paddle/infrt/kernel/basic_kernels.h"
#include "paddle/infrt/kernel/control_flow_kernels.h"
#include "paddle/infrt/kernel/tensor_kernels.h"
#include "paddle/infrt/kernel/tensor_shape_kernels.h"
#include "paddle/infrt/kernel/tensorrt/registry.h"
37
#include "paddle/infrt/kernel/test_kernels.h"
W
Wilber 已提交
38 39 40 41 42 43 44

#ifdef INFRT_WITH_PHI
#include "paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h"
#include "paddle/infrt/dialect/phi/pass/phi_op_convert_pass.h"
#include "paddle/infrt/kernel/phi/infershaped/infershaped_kernel_launchers.h"
#include "paddle/infrt/kernel/phi/registry.h"
#endif
45

46 47
#include <mlir/Transforms/Passes.h>

48 49 50 51 52 53 54 55 56 57 58
int main(int argc, char** argv) {
  static llvm::cl::opt<std::string> input_file(
      llvm::cl::Positional,
      llvm::cl::desc("Specify input filename"),
      llvm::cl::init("-"));

  llvm::cl::ParseCommandLineOptions(argc, argv);

  mlir::MLIRContext* context = infrt::Global::getMLIRContext();
  auto module = infrt::dialect::LoadMlirFile(input_file.c_str(), context);

W
Wilber 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  infrt::host_context::KernelRegistry registry;

  ::infrt::kernel::RegisterBasicKernels(&registry);
  ::infrt::kernel::RegisterTestKernels(&registry);
  ::infrt::kernel::RegisterTensorShapeKernels(&registry);
  ::infrt::kernel::RegisterTensorKernels(&registry);
  ::infrt::kernel::RegisterControlFlowKernels(&registry);
#ifdef INFRT_WITH_PHI
  ::infrt::kernel::RegisterPhiKernels(&registry);
  ::infrt::kernel::RegisterInferShapeLaunchers(&registry);
#endif
#if defined(INFRT_WITH_GPU) && defined(INFRT_WITH_TRT)
  ::infrt::kernel::RegisterTrtKernels(&registry);
#endif

  context->loadAllAvailableDialects();
W
Wilber 已提交
75
  // module->dump();
76 77 78
  mlir::PassManager pm(context);

  mlir::OpPassManager& trt_pass_manager = pm.nest<mlir::FuncOp>();
79
  trt_pass_manager.addPass(::infrt::CreateInfrtWeightsUnfoldPass());
80 81 82 83
  trt_pass_manager.addPass(std::make_unique<infrt::trt::TRTOpTellerPass>());
  trt_pass_manager.addPass(std::make_unique<infrt::trt::TRTGraphFusePass>());
  trt_pass_manager.addPass(std::make_unique<infrt::trt::TRTGraphSplitPass>(1));
  trt_pass_manager.addPass(std::make_unique<infrt::trt::TRTOpConverterPass>());
W
Wilber 已提交
84
  trt_pass_manager.addPass(infrt::trt::CreateTrtTypeConvertPass());
85
  trt_pass_manager.addPass(::mlir::createCanonicalizerPass());
86 87 88 89
  if (mlir::failed(pm.run(*module))) {
    std::cout << "\npass failed!\n" << std::endl;
    return 4;
  }
90
  module->dump();
W
Wilber 已提交
91
  ::infrt::host_context::TestMlir(module.get(), &registry);
92 93
  return 0;
}