test_abs.cc 5.1 KB
Newer Older
H
huzhiqiang 已提交
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.

15
// clang-format off
H
huzhiqiang 已提交
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <gtest/gtest.h>
#include <llvm/Support/CommandLine.h>
#include <mlir/Pass/PassManager.h>
#include <iostream>
#include <string>

#include "llvm/Support/DynamicLibrary.h"
#include "paddle/infrt/common/global.h"
#include "paddle/infrt/dialect/mlir_loader.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/phi/infershaped/infershaped_kernel_launchers.h"
#include "paddle/infrt/kernel/phi/registry.h"
#include "paddle/infrt/kernel/tensor_kernels.h"
#include "paddle/infrt/kernel/tensor_shape_kernels.h"
#include "paddle/infrt/kernel/test_kernels.h"

#include "paddle/infrt/kernel/phi/infershaped/infershaped_utils.h"
#include "paddle/phi/backends/cpu/cpu_context.h"
#include "paddle/phi/common/place.h"
#include "paddle/phi/core/dense_tensor.h"
#include "paddle/phi/core/meta_tensor.h"

#include "paddle/infrt/dialect/infrt/ir/basic_kernels.h"
#include "paddle/infrt/dialect/infrt/ir/infrt_dialect.h"

#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/host_context/paddle_mlir.h"

#include "paddle/infrt/dialect/dense_tensor.h"
#include "paddle/infrt/dialect/phi/ir/infrt_phi_tensor.h"
#include "paddle/infrt/dialect/phi/ir/phi_base.h"
#include "paddle/infrt/dialect/phi/ir/phi_kernels.h"
53
// clang-format on
H
huzhiqiang 已提交
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76

static llvm::cl::list<std::string> cl_shared_libs(  // NOLINT
    "shared_libs",
    llvm::cl::desc("Specify shared library with kernels."),
    llvm::cl::ZeroOrMore,
    llvm::cl::MiscFlags::CommaSeparated);

TEST(ABS_MODEL, convert_and_execute) {
  std::string model_file_name = "./abs.pdmodel";
  std::string params_file_name = "./abs.pdiparams";
  // convert model
  MLIRModelGenImpl myGen;
  auto module_ = myGen.ImportPaddleModel(model_file_name, params_file_name);
  module_.dump();
  // pick kernel
  mlir::MLIRContext* context = infrt::Global::getMLIRContext();
  context->allowUnregisteredDialects();
  context->getOrLoadDialect<mlir::StandardOpsDialect>();

  context->getOrLoadDialect<infrt::InfrtDialect>();
  context->getOrLoadDialect<infrt::ts::TensorShapeDialect>();
  context->getOrLoadDialect<infrt::InfrtDialect>();
  context->getOrLoadDialect<infrt::dt::DTDialect>();
77
  context->getOrLoadDialect<infrt::pd::PaddleDialect>();
H
huzhiqiang 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90

  context->getOrLoadDialect<infrt::phi::PHIDenseTensorDialect>();
  context->getOrLoadDialect<infrt::phi::PHICPUKernelDialect>();
  context->getOrLoadDialect<infrt::phi::PHIGPUKernelDialect>();
  context->getOrLoadDialect<infrt::phi::PHIDialect>();

  context->loadAllAvailableDialects();
  mlir::PassManager pm(context);

  mlir::OpPassManager& phi_pass_manager = pm.nest<mlir::FuncOp>();
  std::vector<infrt::Place> valid_places = {{infrt::TargetType::CPU,
                                             infrt::PrecisionType::FLOAT32,
                                             infrt::LayoutType::NCHW}};
91 92
  phi_pass_manager.addPass(infrt::CreatePhiOpCvtPass(valid_places));
  phi_pass_manager.addPass(infrt::CreateInfrtOpFusePass());
H
huzhiqiang 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128

  if (mlir::failed(pm.run(module_))) {
    std::cout << "\npass failed!\n" << std::endl;
  }
  module_.dump();

  // executate
  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);
  infrt::kernel::RegisterPhiKernels(&registry);
  infrt::kernel::RegisterInferShapeLaunchers(&registry);
  // load extra shared library
  for (const auto& lib_path : cl_shared_libs) {
    std::string err;
    llvm::sys::DynamicLibrary dynLib =
        llvm::sys::DynamicLibrary::getPermanentLibrary(lib_path.c_str(), &err);
    if (!dynLib.isValid()) {
      llvm::errs() << "Load shared library failed. Error: " << err << "\n";
      break;
    }
    if (auto reg_sym = dynLib.SearchForAddressOfSymbol("RegisterKernels")) {
      auto reg_func =
          reinterpret_cast<void (*)(infrt::host_context::KernelRegistry*)>(
              reg_sym);
      reg_func(&registry);
    } else {
      llvm::outs() << "Symbol \"RegisterKernels\" not found in \"" << lib_path
                   << "\". Skip.\n";
    }
  }
  infrt::host_context::TestMlir(module_, &registry);
}