diff --git a/paddle/infrt/api/infrt_api.cc b/paddle/infrt/api/infrt_api.cc index 91668dc176eaa09e7c4c98173d18730c3c9af02b..ca6b6f81e38b9598c0cd1e304eb05e0a6702345f 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 a674e395da4ab405b028d1a84399ab358ba1473f..63be5ca9095638bee8f39df35ab1b51f3085afac 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 ef349a7bbc4c6531bb7c02cb69a1bd5c427af080..bca60b7c0d963be34f6d57030c9a496cbee7d603 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 436ff0a40480cfe2d0907309a8eecf8cdc32adb2..67f6bb8a2d7bbfa604614e4909169c08ea18e1b3 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 bfc43125b8be9951f14f81422713e5025cd23857..e3fdd5ae5bb9fb0c5cc2297700185271bb5dfa30 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 5a2c0ee96ed0de120f8667d8f2fb91314c02e9ac..c426bbf11518b217830d0077263bfc503d67d9e6 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 0beb5bff29f6df73be75a18611a5207bb1e3aad7..0000000000000000000000000000000000000000 --- 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 81bf873ddf0cf3f1a94489bd3b0b2769274b1b4a..6ad51a02bda29432970bc389e1d77e7ecf6e6c0a 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 0000000000000000000000000000000000000000..391ab3b65196d5e68242e449e93598251c5130f4 --- /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 49266910dbd278fb8d429534134097751cf8b6b1..89bbe78ffe27a13eabb45dd6e7b75ae49a618a5f 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;