From ec510bfd974a76ca58022cbf062399e35dd8202e 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: Thu, 31 Mar 2022 08:49:36 +0800 Subject: [PATCH] [Infrt] add result check for some infrt op. (#41167) --- paddle/infrt/api/infrt_api.cc | 4 +- .../dialect/infrt/pass/infrt_op_fuse_pass.cc | 2 +- .../dialect/infrt/pass/infrt_op_fuse_pass.h | 2 +- paddle/infrt/dialect/phi/CMakeLists.txt | 4 -- .../dialect/phi/pass/phi_op_convert_pass.cc | 4 +- .../dialect/phi/pass/phi_op_convert_pass.h | 4 +- paddle/infrt/dialect/phi/phi_ir_exec.cc | 49 ------------------- paddle/infrt/host_context/mlir_exec.cc | 4 +- .../dialect/phi/kernels/resnet50_ops.mlir | 43 ++++++++++++++++ paddle/infrt/tests/models/test_abs.cc | 4 +- 10 files changed, 55 insertions(+), 65 deletions(-) delete mode 100644 paddle/infrt/dialect/phi/phi_ir_exec.cc create mode 100644 paddle/infrt/tests/dialect/phi/kernels/resnet50_ops.mlir diff --git a/paddle/infrt/api/infrt_api.cc b/paddle/infrt/api/infrt_api.cc index 91668dc176..ca6b6f81e3 100644 --- a/paddle/infrt/api/infrt_api.cc +++ b/paddle/infrt/api/infrt_api.cc @@ -238,8 +238,8 @@ int InfRtPredictor::Init(const InfRtConfig& config) { std::vector<::infrt::Place> valid_places = {{::infrt::TargetType::CPU, ::infrt::PrecisionType::FLOAT32, ::infrt::LayoutType::NCHW}}; - phi_pass_manager.addPass(::infrt::createPhiOpCvtPass(valid_places)); - phi_pass_manager.addPass(::infrt::createInfrtOpFusePass()); + phi_pass_manager.addPass(CreatePhiOpCvtPass(valid_places)); + phi_pass_manager.addPass(CreateInfrtOpFusePass()); if (mlir::failed(pm.run(module_op))) { std::cout << "\npass failed!\n" << std::endl; return 4; diff --git a/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.cc b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.cc index a674e395da..63be5ca909 100644 --- a/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.cc +++ b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.cc @@ -53,7 +53,7 @@ void InfrtOpFusePass::runOnFunction() { } // namespace -std::unique_ptr infrt::createInfrtOpFusePass() { +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 index ef349a7bbc..bca60b7c0d 100644 --- a/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h +++ b/paddle/infrt/dialect/infrt/pass/infrt_op_fuse_pass.h @@ -19,6 +19,6 @@ namespace infrt { /* * infrtOpFusePass. */ -std::unique_ptr createInfrtOpFusePass(); +std::unique_ptr CreateInfrtOpFusePass(); } // namespace infrt diff --git a/paddle/infrt/dialect/phi/CMakeLists.txt b/paddle/infrt/dialect/phi/CMakeLists.txt index 436ff0a404..67f6bb8a2d 100644 --- a/paddle/infrt/dialect/phi/CMakeLists.txt +++ b/paddle/infrt/dialect/phi/CMakeLists.txt @@ -5,10 +5,6 @@ endif() add_subdirectory(ir) add_subdirectory(pass) -add_executable(phi-ir-exec phi_ir_exec.cc) -target_link_libraries(phi-ir-exec infrt) - - add_executable(phi-exec phi_exec.cc) target_link_libraries(phi-exec infrt) diff --git a/paddle/infrt/dialect/phi/pass/phi_op_convert_pass.cc b/paddle/infrt/dialect/phi/pass/phi_op_convert_pass.cc index bfc43125b8..e3fdd5ae5b 100644 --- a/paddle/infrt/dialect/phi/pass/phi_op_convert_pass.cc +++ b/paddle/infrt/dialect/phi/pass/phi_op_convert_pass.cc @@ -261,11 +261,11 @@ void PhiOpConvertPass::getDependentDialects( mlir::PassRegistration phi_op_convert; -std::unique_ptr infrt::createPhiOpCvtPass( +std::unique_ptr infrt::CreatePhiOpCvtPass( std::vector valid_places) { return std::make_unique(valid_places); } -std::unique_ptr infrt::createPhiOpCvtPass() { +std::unique_ptr infrt::CreatePhiOpCvtPass() { return std::make_unique(); } diff --git a/paddle/infrt/dialect/phi/pass/phi_op_convert_pass.h b/paddle/infrt/dialect/phi/pass/phi_op_convert_pass.h index 5a2c0ee96e..c426bbf115 100644 --- a/paddle/infrt/dialect/phi/pass/phi_op_convert_pass.h +++ b/paddle/infrt/dialect/phi/pass/phi_op_convert_pass.h @@ -21,8 +21,8 @@ namespace infrt { * phiOpCvtPass. * Convert the general operators from pd Dialect to phi dialect. */ -std::unique_ptr createPhiOpCvtPass(std::vector valid_places); +std::unique_ptr CreatePhiOpCvtPass(std::vector valid_places); -std::unique_ptr createPhiOpCvtPass(); +std::unique_ptr CreatePhiOpCvtPass(); } // namespace infrt diff --git a/paddle/infrt/dialect/phi/phi_ir_exec.cc b/paddle/infrt/dialect/phi/phi_ir_exec.cc deleted file mode 100644 index 0beb5bff29..0000000000 --- a/paddle/infrt/dialect/phi/phi_ir_exec.cc +++ /dev/null @@ -1,49 +0,0 @@ -// 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. -#include -#include -#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_convert_pass.h" - -int main(int argc, char** argv) { - static llvm::cl::opt 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); - context->loadAllAvailableDialects(); - module->dump(); - mlir::PassManager pm(context); - - mlir::OpPassManager& phi_pass_manager = pm.nest(); - std::vector valid_places = {{infrt::TargetType::CPU, - infrt::PrecisionType::FLOAT32, - infrt::LayoutType::NCHW}}; - phi_pass_manager.addPass(infrt::createPhiOpCvtPass(valid_places)); - phi_pass_manager.addPass(infrt::createInfrtOpFusePass()); - if (mlir::failed(pm.run(*module))) { - std::cout << "\npass failed!\n" << std::endl; - return 4; - } - module->dump(); - return 0; -} diff --git a/paddle/infrt/host_context/mlir_exec.cc b/paddle/infrt/host_context/mlir_exec.cc index 81bf873ddf..6ad51a02bd 100644 --- a/paddle/infrt/host_context/mlir_exec.cc +++ b/paddle/infrt/host_context/mlir_exec.cc @@ -98,8 +98,8 @@ int main(int argc, char** argv) { std::vector valid_places = {{infrt::TargetType::CPU, infrt::PrecisionType::FLOAT32, infrt::LayoutType::NCHW}}; - phi_pass_manager.addPass(infrt::createPhiOpCvtPass(valid_places)); - phi_pass_manager.addPass(infrt::createInfrtOpFusePass()); + phi_pass_manager.addPass(infrt::CreatePhiOpCvtPass(valid_places)); + phi_pass_manager.addPass(infrt::CreateInfrtOpFusePass()); #endif if (mlir::failed(pm.run(*module))) { diff --git a/paddle/infrt/tests/dialect/phi/kernels/resnet50_ops.mlir b/paddle/infrt/tests/dialect/phi/kernels/resnet50_ops.mlir new file mode 100644 index 0000000000..391ab3b651 --- /dev/null +++ b/paddle/infrt/tests/dialect/phi/kernels/resnet50_ops.mlir @@ -0,0 +1,43 @@ +// RUN: infrtexec -i %s | FileCheck %s +module { + func @main() { + %ctx = "phi_dt.create_context.cpu" (): () -> !phi.context + %0 = "phi_dt.create_inited_dense_tensor.cpu.f32" (%ctx) {value = 2.0 : f32, layout=#infrt.layout, lod=[1:i64], dims=[1, 3, 6, 6]}: (!phi.context) -> (!infrt.dense_tensor) + %1 = "phi_dt.create_inited_dense_tensor.cpu.f32" (%ctx) {value = 2.0 : f32, layout=#infrt.layout, lod=[1:i64], dims=[1, 3, 3, 3]}: (!phi.context) -> (!infrt.dense_tensor) + + %2 = "pd.conv2d"(%0, %1) {data_format = "NCHW", dilations = [1 : i32, 1 : i32], groups = 1 : si32, padding_algorithm = "EXPLICIT", paddings = [3 : i32, 3 : i32], strides = [2 : i32, 2 : i32]} : (!infrt.dense_tensor, !infrt.dense_tensor) -> !infrt.dense_tensor + // CHECK: dense_tensor: shape=shape[1, 1, 5, 5], value=[0,0,0,0,0,0,48,72,72,24,0,72,108,108,36,0,72,108,108,36,0,24,36,36,12] + phi_dt.print_tensor (%2 : !infrt.dense_tensor) + + %3 = "pd.relu"(%2) : (!infrt.dense_tensor) -> !infrt.dense_tensor + // dense_tensor: shape=shape[1, 1, 5, 5], value=[0,0,0,0,0,0,48,72,72,24,0,72,108,108,36,0,72,108,108,36,0,24,36,36,12] + phi_dt.print_tensor (%3 : !infrt.dense_tensor) + + %4 = "pd.pool2d"(%2) {adaptive = false, ceil_mode = false, data_format = "NCHW", exclusive = true, global_pooling = false, ksize = [2 : i32, 2 : i32], padding_algorithm = "EXPLICIT", paddings = [1 : i32, 1 : i32], pooling_type = "avg", strides = [2 : i32, 2 : i32]} : (!infrt.dense_tensor) -> !infrt.dense_tensor + // CHECK: dense_tensor: shape=shape[1, 1, 3, 3], value=[0,0,0,0,75,60,0,60,48] + phi_dt.print_tensor (%4 : !infrt.dense_tensor) + + %5 = "pd.flatten_contiguous_range"(%4) {start_axis = 1 : si32, stop_axis = 3 : si32} : (!infrt.dense_tensor) -> !infrt.dense_tensor + // CHECK: dense_tensor: shape=shape[1, 9], value=[0,0,0,0,75,60,0,60,48] + phi_dt.print_tensor (%5 : !infrt.dense_tensor) + + %6 = "pd.elementwise_add"(%5, %5) {axis = 1 : si32} : (!infrt.dense_tensor, !infrt.dense_tensor) -> !infrt.dense_tensor + // CHECK: dense_tensor: shape=shape[1, 9], value=[0,0,0,0,150,120,0,120,96] + phi_dt.print_tensor (%6 : !infrt.dense_tensor) + + %7 = "phi_dt.create_inited_dense_tensor.cpu.f32" (%ctx) {value = 4.0 : f32, layout=#infrt.layout, lod=[1:i64], dims=[9, 3]}: (!phi.context) -> (!infrt.dense_tensor) + %8 = "pd.matmul_v2"(%5, %7) {trans_x = false, trans_y = false} : (!infrt.dense_tensor, !infrt.dense_tensor) -> !infrt.dense_tensor + // CHECK: dense_tensor: shape=shape[1, 3], value=[972,972,972] + phi_dt.print_tensor (%8 : !infrt.dense_tensor) + + %scale = "phi_dt.create_inited_dense_tensor.cpu.f32" (%ctx) {value=1.0:f32, layout=#infrt.layout, lod=[1], dims=[3]}: (!phi.context) -> (!infrt.dense_tensor) + %bias = "phi_dt.create_inited_dense_tensor.cpu.f32" (%ctx) {value=1.8:f32, layout=#infrt.layout, lod=[1], dims=[3]}: (!phi.context) -> (!infrt.dense_tensor) + %mean = "phi_dt.create_inited_dense_tensor.cpu.f32" (%ctx) {value=2.0:f32, layout=#infrt.layout, lod=[1], dims=[3]}: (!phi.context) -> (!infrt.dense_tensor) + %var = "phi_dt.create_inited_dense_tensor.cpu.f32" (%ctx) {value=0.0:f32, layout=#infrt.layout, lod=[1], dims=[3]}: (!phi.context) -> (!infrt.dense_tensor) + %Y, %MeanOut, %VarianceOut = "pd.batch_norm"(%1, %scale, %bias, %mean, %var) {data_layout = "NCHW", epsilon = 0.01 : f32, momentum = 0.5 : f32} : (!infrt.dense_tensor, !infrt.dense_tensor, !infrt.dense_tensor, !infrt.dense_tensor, !infrt.dense_tensor) -> (!infrt.dense_tensor, !infrt.dense_tensor, !infrt.dense_tensor) + // CHECK: dense_tensor: shape=shape[1, 3, 3, 3], value=[1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8,1.8] + phi_dt.print_tensor (%Y : !infrt.dense_tensor) + + infrt.return + } +} diff --git a/paddle/infrt/tests/models/test_abs.cc b/paddle/infrt/tests/models/test_abs.cc index 49266910db..89bbe78ffe 100644 --- a/paddle/infrt/tests/models/test_abs.cc +++ b/paddle/infrt/tests/models/test_abs.cc @@ -86,8 +86,8 @@ TEST(ABS_MODEL, convert_and_execute) { std::vector valid_places = {{infrt::TargetType::CPU, infrt::PrecisionType::FLOAT32, infrt::LayoutType::NCHW}}; - phi_pass_manager.addPass(infrt::createPhiOpCvtPass(valid_places)); - phi_pass_manager.addPass(infrt::createInfrtOpFusePass()); + phi_pass_manager.addPass(infrt::CreatePhiOpCvtPass(valid_places)); + phi_pass_manager.addPass(infrt::CreateInfrtOpFusePass()); if (mlir::failed(pm.run(module_))) { std::cout << "\npass failed!\n" << std::endl; -- GitLab